summaryrefslogtreecommitdiff
path: root/library/fileHandling.cpp
blob: cda81ef5204d9737b694a35cfb08c25b06dc57ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#include "fileHandling.h"
#include <wx/intl.h>
#include <wx/msgdlg.h>
#include "../algorithm.h"
#include <wx/filename.h>

#ifdef FFS_WIN
#include <wx/msw/wrapwin.h> //includes "windows.h"
#endif  // FFS_WIN


class RecycleBin
{
public:
    RecycleBin() :
            recycleBinAvailable(false)
    {
#ifdef FFS_WIN
        recycleBinAvailable = true;
#endif  // FFS_WIN
    }

    ~RecycleBin() {}

    bool recycleBinExists()
    {
        return recycleBinAvailable;
    }

    bool moveToRecycleBin(const Zstring& filename)
    {
        if (!recycleBinAvailable)   //this method should ONLY be called if recycle bin is available
            throw RuntimeException(_("Initialization of Recycle Bin failed!"));

#ifdef FFS_WIN
        Zstring filenameDoubleNull = filename + wxChar(0);

        SHFILEOPSTRUCT fileOp;
        fileOp.hwnd   = NULL;
        fileOp.wFunc  = FO_DELETE;
        fileOp.pFrom  = filenameDoubleNull.c_str();
        fileOp.pTo    = NULL;
        fileOp.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_SILENT;
        fileOp.fAnyOperationsAborted = false;
        fileOp.hNameMappings         = NULL;
        fileOp.lpszProgressTitle     = NULL;

        if (SHFileOperation(&fileOp   //pointer to an SHFILEOPSTRUCT structure that contains information the function needs to carry out
                           ) != 0 || fileOp.fAnyOperationsAborted) return false;
#else
        assert(false);
#endif

        return true;
    }

private:
    bool recycleBinAvailable;
};

//global instance of recycle bin
RecycleBin recyclerInstance;


bool FreeFileSync::recycleBinExists()
{
    return recyclerInstance.recycleBinExists();
}


inline
bool moveToRecycleBin(const Zstring& filename) throw(RuntimeException)
{
    return recyclerInstance.moveToRecycleBin(filename);
}


inline
void FreeFileSync::removeFile(const Zstring& filename, const bool useRecycleBin)
{
    if (!wxFileExists(filename.c_str())) return; //this is NOT an error situation: the manual deletion relies on it!

    if (useRecycleBin)
    {
        if (!moveToRecycleBin(filename))
            throw FileError(Zstring(_("Error moving to Recycle Bin:")) + wxT("\n\"") + filename + wxT("\""));
        return;
    }

#ifdef FFS_WIN
    //initialize file attributes
    if (!SetFileAttributes(
                filename.c_str(),       //address of filename
                FILE_ATTRIBUTE_NORMAL)) //attributes to set
    {
        Zstring errorMessage = Zstring(_("Error deleting file:")) + wxT("\n\"") + filename + wxT("\"");
        throw FileError(errorMessage + wxT("\n\n") + FreeFileSync::getLastErrorFormatted());
    }

    //remove file, support for \\?\-prefix
    if (DeleteFile(filename.c_str()) == 0)
    {
        Zstring errorMessage = Zstring(_("Error deleting file:")) + wxT("\n\"") + filename + wxT("\"");
        throw FileError(errorMessage + wxT("\n\n") + FreeFileSync::getLastErrorFormatted());
    }
#else
    if (!wxRemoveFile(filename.c_str()))
        throw FileError(Zstring(_("Error deleting file:")) + wxT("\n\"") + filename + wxT("\""));
#endif
}


void FreeFileSync::removeDirectory(const Zstring& directory, const bool useRecycleBin)
{
    if (!wxDirExists(directory)) return; //this is NOT an error situation: the manual deletion relies on it!

    if (useRecycleBin)
    {
        if (!moveToRecycleBin(directory))
            throw FileError(Zstring(_("Error moving to Recycle Bin:")) + wxT("\n\"") + directory + wxT("\""));
        return;
    }

    std::vector<Zstring> fileList;
    std::vector<Zstring> dirList;

    getAllFilesAndDirs(directory, fileList, dirList);

    for (unsigned int j = 0; j < fileList.size(); ++j)
        removeFile(fileList[j], false);

    dirList.insert(dirList.begin(), directory);   //parent directory will be deleted last

    for (int j = int(dirList.size()) - 1; j >= 0 ; --j)
    {
#ifdef FFS_WIN
        //initialize file attributes
        if (!SetFileAttributes(
                    dirList[j].c_str(),     // address of directory name
                    FILE_ATTRIBUTE_NORMAL)) // attributes to set
        {
            Zstring errorMessage = Zstring(_("Error deleting directory:")) + wxT("\n\"") + dirList[j] + wxT("\"");
            throw FileError(errorMessage + wxT("\n\n") + FreeFileSync::getLastErrorFormatted());
        }

        //remove directory, support for \\?\-prefix
        if (RemoveDirectory(dirList[j].c_str()) == 0)
        {
            Zstring errorMessage = Zstring(_("Error deleting directory:")) + wxT("\n\"") + dirList[j] + wxT("\"");
            throw FileError(errorMessage + wxT("\n\n") + FreeFileSync::getLastErrorFormatted());
        }
#else
        if (!wxRmdir(dirList[j].c_str()))
            throw FileError(Zstring(_("Error deleting directory:")) + wxT("\n\"") + dirList[j] + wxT("\""));
#endif
    }
}


void FreeFileSync::createDirectory(const Zstring& directory, const int level)
{
    if (wxDirExists(directory.c_str()))
        return;

    if (level == 50) //catch endless recursion
        return;

    //try to create directory, support for \\?\-prefix
#ifdef FFS_WIN
    if (CreateDirectory(
                directory.c_str(),	// pointer to a directory path string
                NULL               	// pointer to a security descriptor
            ) != 0)
        return;
#else
    if (wxMkdir(directory.c_str())) //wxMkDir has different signature under Linux!
        return;
#endif

    //if not successfull try to create parent folders first
    Zstring parentDir;
    if (endsWithPathSeparator(directory)) //may be valid on first level of recursion at most! Usually never true!
    {
        parentDir = directory.BeforeLast(GlobalResources::FILE_NAME_SEPARATOR);
        parentDir = parentDir.BeforeLast(GlobalResources::FILE_NAME_SEPARATOR);
    }
    else
        parentDir = directory.BeforeLast(GlobalResources::FILE_NAME_SEPARATOR);

    if (parentDir.empty()) return;

    //call function recursively
    createDirectory(parentDir, level + 1);

    //now creation should be possible
#ifdef FFS_WIN
    if (CreateDirectory(
                directory.c_str(), // pointer to a directory path string
                NULL 	           // pointer to a security descriptor
            ) == 0)
    {
        if (level == 0)
        {
            Zstring errorMessage = Zstring(_("Error creating directory:")) + wxT("\n\"") + directory + wxT("\"");
            throw FileError(errorMessage + wxT("\n\n") + FreeFileSync::getLastErrorFormatted());
        }
    }
#else
    if (!wxMkdir(directory.c_str())) //wxMkDir has different signature under Linux!
    {
        if (level == 0)
            throw FileError(Zstring(_("Error creating directory:")) + wxT("\n\"") + directory + wxT("\""));
    }
#endif
}


void FreeFileSync::copyFolderAttributes(const Zstring& source, const Zstring& target)
{
#ifdef FFS_WIN
    DWORD attr = GetFileAttributes(source.c_str()); // address of the name of a file or directory
    if (attr ==  0xFFFFFFFF)
    {
        Zstring errorMessage = Zstring(_("Error reading folder attributes:")) + wxT("\n\"") + source + wxT("\"");
        throw FileError(errorMessage + wxT("\n\n") + FreeFileSync::getLastErrorFormatted());
    }

    if (!SetFileAttributes(
                target.c_str(), // address of filename
                attr))          // address of attributes to set
    {
        Zstring errorMessage = Zstring(_("Error writing folder attributes:")) + wxT("\n\"") + target + wxT("\"");
        throw FileError(errorMessage + wxT("\n\n") + FreeFileSync::getLastErrorFormatted());
    }
#elif defined FFS_LINUX
//Linux doesn't use attributes for files or folders
#endif
}


class FilesDirsOnlyTraverser : public FreeFileSync::FullDetailFileTraverser
{
public:
    FilesDirsOnlyTraverser(std::vector<Zstring>& files, std::vector<Zstring>& dirs) :
            m_files(files),
            m_dirs(dirs) {}

    virtual wxDirTraverseResult OnFile(const Zstring& filename, const FreeFileSync::FileInfo& details)
    {
        m_files.push_back(filename);
        return wxDIR_CONTINUE;
    }
    virtual wxDirTraverseResult OnDir(const Zstring& dirname)
    {
        m_dirs.push_back(dirname);
        return wxDIR_CONTINUE;
    }
    virtual wxDirTraverseResult OnError(const Zstring& errorText)
    {
        throw FileError(errorText);
    }

private:
    std::vector<Zstring>& m_files;
    std::vector<Zstring>& m_dirs;
};


void FreeFileSync::getAllFilesAndDirs(const Zstring& sourceDir, std::vector<Zstring>& files, std::vector<Zstring>& directories) throw(FileError)
{
    files.clear();
    directories.clear();

    //get all files and directories from current directory (and subdirectories)
    FilesDirsOnlyTraverser traverser(files, directories);
    traverseInDetail(sourceDir, false, &traverser);
}


#ifdef FFS_WIN
class CloseOnExit
{
public:
    CloseOnExit(HANDLE searchHandle) : m_searchHandle(searchHandle) {}

    ~CloseOnExit()
    {
        FindClose(m_searchHandle);
    }

private:
    HANDLE m_searchHandle;
};


inline
void getWin32FileInformation(const WIN32_FIND_DATA& input, FreeFileSync::FileInfo& output)
{
    //convert UTC FILETIME to ANSI C format (number of seconds since Jan. 1st 1970 UTC)
    wxULongLong writeTimeLong(input.ftLastWriteTime.dwHighDateTime, input.ftLastWriteTime.dwLowDateTime);
    writeTimeLong /= 10000000;                     //reduce precision to 1 second (FILETIME has unit 10^-7 s)
    writeTimeLong -= wxULongLong(2, 3054539008UL); //timeshift between ansi C time and FILETIME in seconds == 11644473600s
    assert(writeTimeLong.GetHi() == 0);            //it should fit into a 32bit variable now
    output.lastWriteTimeRaw = writeTimeLong.GetLo();

    output.fileSize = wxULongLong(input.nFileSizeHigh, input.nFileSizeLow);
}

#elif defined FFS_LINUX
class EnhancedFileTraverser : public wxDirTraverser
{
public:
    EnhancedFileTraverser(FreeFileSync::FullDetailFileTraverser* sink) : m_sink(sink) {}

    wxDirTraverseResult OnFile(const wxString& filename) //virtual impl.
    {
        struct stat fileInfo;
        if (stat(filename.c_str(), &fileInfo) != 0)
            return m_sink->OnError(Zstring(_("Could not retrieve file info for:")) + wxT("\n\"") + filename.c_str() + wxT("\""));

        FreeFileSync::FileInfo details;
        details.lastWriteTimeRaw = fileInfo.st_mtime; //UTC time(ANSI C format); unit: 1 second
        details.fileSize         = fileInfo.st_size;

        return m_sink->OnFile(filename.c_str(), details);
    }

    wxDirTraverseResult OnDir(const wxString& dirname) //virtual impl.
    {
        return m_sink->OnDir(dirname.c_str());
    }

    wxDirTraverseResult OnOpenError(const wxString& errorText) //virtual impl.
    {
        return m_sink->OnError(errorText.c_str());
    }

private:
    FreeFileSync::FullDetailFileTraverser* m_sink;
};
#endif


class TraverseRecursively
{
public:
    TraverseRecursively(const bool traverseSymbolicLinks, FreeFileSync::FullDetailFileTraverser* sink) :
            m_traverseSymbolicLinks(traverseSymbolicLinks),
            m_sink(sink) {}

    bool traverse(const Zstring& directory, const int level)
    {
#ifdef FFS_WIN
        if (level == 50) //catch endless recursion
        {
            if (m_sink->OnError(Zstring(_("Error traversing directory:")) + wxT("\n\"") + directory + wxT("\"")) == wxDIR_STOP)
                return false;
            else
                return true;
        }

        Zstring directoryFormatted = directory;
        if (!FreeFileSync::endsWithPathSeparator(directoryFormatted))
            directoryFormatted += GlobalResources::FILE_NAME_SEPARATOR;

        const Zstring filespec = directoryFormatted + DefaultChar('*');

        WIN32_FIND_DATA fileMetaData;
        HANDLE searchHandle = FindFirstFile(filespec.c_str(), //pointer to name of file to search for
                                            &fileMetaData);   //pointer to returned information

        if (searchHandle == INVALID_HANDLE_VALUE)
        {
            const DWORD lastError = GetLastError();
            if (lastError == ERROR_FILE_NOT_FOUND)
                return true;

            //else: we have a problem... report it:
            Zstring errorMessage = Zstring(_("Error traversing directory:")) + wxT("\n\"") + directory + wxT("\" ") ;
            if (m_sink->OnError(errorMessage + wxT("\n\n") + FreeFileSync::getLastErrorFormatted(lastError)) == wxDIR_STOP)
                return false;
            else
                return true;
        }
        CloseOnExit dummy(searchHandle);

        do
        {   //don't return "." and ".."
            const wxChar* name = fileMetaData.cFileName;
            if (    name[0] == wxChar('.') &&
                    ((name[1] == wxChar('.') && name[2] == wxChar('\0')) ||
                     name[1] == wxChar('\0')))
                continue;

            const Zstring fullName = directoryFormatted + name;

            if (fileMetaData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) //a directory...
            {
                switch (m_sink->OnDir(fullName))
                {
                case wxDIR_IGNORE:
                    break;
                case wxDIR_CONTINUE:
                    //traverse into symbolic links, junctions, etc. if requested only:
                    if (m_traverseSymbolicLinks || (~fileMetaData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
                        if (!this->traverse(fullName, level + 1))
                            return false;
                    break;
                case wxDIR_STOP:
                    return false;
                default:
                    assert(false);
                    break;
                }
            }
            else //a file...
            {
                FreeFileSync::FileInfo details;
                getWin32FileInformation(fileMetaData, details);

                switch (m_sink->OnFile(fullName, details))
                {
                case wxDIR_IGNORE:
                case wxDIR_CONTINUE:
                    break;
                case wxDIR_STOP:
                    return false;
                default:
                    assert(false);
                    break;
                }
            }
        }
        while (FindNextFile(searchHandle,	 // handle to search
                            &fileMetaData)); // pointer to structure for data on found file

        const DWORD lastError = GetLastError();
        if (lastError == ERROR_NO_MORE_FILES)
            return true; //everything okay

        //else: we have a problem... report it:
        Zstring errorMessage = Zstring(_("Error traversing directory:")) + wxT("\n\"") + directory + wxT("\" ") ;
        if (m_sink->OnError(errorMessage + wxT("\n\n") + FreeFileSync::getLastErrorFormatted(lastError)) == wxDIR_STOP)
            return false;
        else
            return true;
#elif defined FFS_LINUX

        //use standard file traverser and enrich output with additional file information
        //could be improved with custom traversing algorithm for optimized performance
        EnhancedFileTraverser traverser(m_sink);

        wxDir dir(directory.c_str());
        if (dir.IsOpened())
            dir.Traverse(traverser);

        return true;
#else
        adapt this
#endif
    }

private:
    const bool m_traverseSymbolicLinks;
    FreeFileSync::FullDetailFileTraverser* m_sink;
};


void FreeFileSync::traverseInDetail(const Zstring& directory,
                                    const bool traverseSymbolicLinks,
                                    FullDetailFileTraverser* sink)
{
    TraverseRecursively filewalker(traverseSymbolicLinks, sink);
    filewalker.traverse(directory, 0);
}


#ifdef FFS_WIN
inline
Zstring getDriveName(const Zstring& directoryName) //GetVolume() doesn't work under Linux!
{
    const Zstring volumeName = wxFileName(directoryName.c_str()).GetVolume().c_str();
    if (volumeName.empty())
        return Zstring();

    return volumeName + wxFileName::GetVolumeSeparator().c_str() + GlobalResources::FILE_NAME_SEPARATOR;
}


bool FreeFileSync::isFatDrive(const Zstring& directoryName)
{
    const Zstring driveName = getDriveName(directoryName);
    if (driveName.empty())
        return false;

    wxChar fileSystem[32];
    if (!GetVolumeInformation(driveName.c_str(), NULL, 0, NULL, NULL, NULL, fileSystem, 32))
        return false;

    return Zstring(fileSystem).StartsWith(wxT("FAT"));
}
#endif  //FFS_WIN
bgstack15