summaryrefslogtreecommitdiff
path: root/RealtimeSync/watcher.cpp
blob: ef43d8d996f0da9cb409ee9f0a3b145effbddf1e (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
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html       *
// * Copyright (C) 2008-2011 ZenJu (zhnmju123 AT gmx.de)                    *
// **************************************************************************
//
#include "watcher.h"
#include "../shared/system_func.h"
#include "../shared/string_conv.h"
#include "../shared/file_handling.h"
#include "../shared/i18n.h"
#include <stdexcept>
#include <set>
#include <wx/timer.h>
#include <algorithm>
#include "../shared/resolve_path.h"

#ifdef FFS_WIN
#include "notify.h"
#include <wx/msw/wrapwin.h> //includes "windows.h"
#include "../shared/long_path_prefix.h"
#include <boost/shared_ptr.hpp>
#include "../shared/loki/ScopeGuard.h"
#include <boost/scoped_array.hpp>

#elif defined FFS_LINUX
#include "../shared/inotify/inotify-cxx.h"
#include "../shared/file_traverser.h"
#endif

using namespace ffs3;


bool rts::updateUiIsAllowed()
{
    static wxLongLong lastExec;
    const wxLongLong  newExec = wxGetLocalTimeMillis();

    if (newExec - lastExec >= rts::UI_UPDATE_INTERVAL)  //perform ui updates not more often than necessary
    {
        lastExec = newExec;
        return true;
    }
    return false;
}


#ifdef FFS_WIN
//shared_ptr custom deleter
void cleanUpChangeNotifications(const std::vector<HANDLE>* handles)
{
    for (std::vector<HANDLE>::const_iterator i = handles->begin(); i != handles->end(); ++i)
        if (*i != INVALID_HANDLE_VALUE)
            ::FindCloseChangeNotification(*i);

    delete handles; //don't forget!!! custom deleter needs to care for everything!
}

#elif defined FFS_LINUX
class DirsOnlyTraverser : public ffs3::TraverseCallback
{
public:
    DirsOnlyTraverser(std::vector<std::string>& dirs) : m_dirs(dirs) {}

    virtual void onFile(const Zchar* shortName, const Zstring& fullName, const FileInfo& details) {}
    virtual void onSymlink(const Zchar* shortName, const Zstring& fullName, const SymlinkInfo& details) {}
    virtual ReturnValDir onDir(const Zchar* shortName, const Zstring& fullName)
    {
        m_dirs.push_back(fullName.c_str());
        return ReturnValDir(Loki::Int2Type<ReturnValDir::TRAVERSING_DIR_CONTINUE>(), *this);
    }
    virtual void onError(const wxString& errorText)
    {
        throw ffs3::FileError(errorText);
    }

private:
    std::vector<std::string>& m_dirs;
};
#endif


class WatchDirectories //detect changes to directory availability
{
public:
    WatchDirectories() : allExisting_(true) {}

    //initialization
    void addForMonitoring(const Zstring& dirName)
    {
        dirList.insert(dirName);
    }

    bool allExisting() const //polling explicitly allowed!
    {
        const int UPDATE_INTERVAL = 1000; //1 second interval

        const wxLongLong current = wxGetLocalTimeMillis();
        if (current - lastCheck >= UPDATE_INTERVAL)
        {
            lastCheck = current;
            allExisting_ = std::find_if(dirList.begin(), dirList.end(), std::not1(std::ptr_fun(&ffs3::dirExists))) == dirList.end();
        }

        return allExisting_;
    }

private:
    mutable wxLongLong lastCheck;
    mutable bool allExisting_;

    std::set<Zstring, LessFilename> dirList; //save avail. directories, avoid double-entries
};



rts::WaitResult rts::waitForChanges(const std::vector<Zstring>& dirNames, WaitCallback* statusHandler) //throw(FileError)
{
    /*
    #warning cleanup
    {
    const Zstring formattedDir = ffs3::getFormattedDirectoryName(dirNames.front());

        //SE_BACKUP_NAME and SE_RESTORE_NAME <- required by FILE_FLAG_BACKUP_SEMANTICS???

        //open the directory to watch....
        HANDLE hDir = ::CreateFile(ffs3::applyLongPathPrefix(formattedDir).c_str(),
                                   FILE_LIST_DIRECTORY,
                                   FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, //leaving out last flag may prevent files to be deleted WITHIN monitored dir (http://qualapps.blogspot.com/2010/05/understanding-readdirectorychangesw_19.html)
                                   NULL,
                                   OPEN_EXISTING,
                                   FILE_FLAG_BACKUP_SEMANTICS,
                                   NULL);
        if (hDir == INVALID_HANDLE_VALUE)
        {
            const DWORD lastError = ::GetLastError();
            if (    lastError == ERROR_FILE_NOT_FOUND || //no need to check this condition any earlier!
                    lastError == ERROR_BAD_NETPATH)      //
                return CHANGE_DIR_MISSING;

            const wxString errorMessage = wxString(_("Could not initialize directory monitoring:")) + wxT("\n\"") + zToWx(formattedDir) + wxT("\"");
            throw ffs3::FileError(errorMessage + wxT("\n\n") + ffs3::getLastErrorFormatted());
        }

        Loki::ScopeGuard dummy = Loki::MakeGuard(::CloseHandle, hDir);
        (void)dummy; //silence warning "unused variable"


        const size_t bufferSize = sizeof(FILE_NOTIFY_INFORMATION);
        boost::scoped_array<char> tmp(new char[bufferSize]);
        FILE_NOTIFY_INFORMATION& notifyInfo = reinterpret_cast<FILE_NOTIFY_INFORMATION&>(*tmp.get());

        DWORD bytesWritten = 0;

        if (!::ReadDirectoryChangesW(
                hDir,        //__in         HANDLE hDirectory,
                &notifyInfo, //__out        LPVOID lpBuffer,
                bufferSize,  //__in         DWORD nBufferLength,
                true,        //__in         BOOL bWatchSubtree,
                FILE_NOTIFY_CHANGE_FILE_NAME  | //__in         DWORD dwNotifyFilter,
                FILE_NOTIFY_CHANGE_DIR_NAME   |
                FILE_NOTIFY_CHANGE_SIZE       |
                FILE_NOTIFY_CHANGE_LAST_WRITE,
                &bytesWritten, //__out_opt    LPDWORD lpBytesReturned,
                NULL,          //__inout_opt  LPOVERLAPPED lpOverlapped,
                NULL))         //__in_opt     LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
        {
            const wxString errorMessage = wxString(_("Could not initialize directory monitoring:")) + wxT("\n\"") + zToWx(formattedDir) + wxT("\"");
            throw ffs3::FileError(errorMessage + wxT("\n\n") + ffs3::getLastErrorFormatted());
        }
        return CHANGE_DETECTED;
    }
    */









    if (dirNames.empty()) //pathological case, but check is needed nevertheless
        throw ffs3::FileError(_("At least one directory input field is empty."));

    //detect when volumes are removed/are not available anymore
    WatchDirectories dirWatcher;

#ifdef FFS_WIN
    typedef boost::shared_ptr<std::vector<HANDLE> > ChangeNotifList;
    ChangeNotifList changeNotifications(new std::vector<HANDLE>, ::cleanUpChangeNotifications);

    for (std::vector<Zstring>::const_iterator i = dirNames.begin(); i != dirNames.end(); ++i)
    {
        const Zstring formattedDir = ffs3::getFormattedDirectoryName(*i);

        if (formattedDir.empty())
            throw ffs3::FileError(_("At least one directory input field is empty."));

        dirWatcher.addForMonitoring(formattedDir);

        const HANDLE rv = ::FindFirstChangeNotification(
                              ffs3::applyLongPathPrefix(formattedDir).c_str(), //__in  LPCTSTR lpPathName,
                              true,                           //__in  BOOL bWatchSubtree,
                              FILE_NOTIFY_CHANGE_FILE_NAME |
                              FILE_NOTIFY_CHANGE_DIR_NAME  |
                              FILE_NOTIFY_CHANGE_SIZE      |
                              FILE_NOTIFY_CHANGE_LAST_WRITE); //__in  DWORD dwNotifyFilter

        if (rv == INVALID_HANDLE_VALUE)
        {
            const DWORD lastError = ::GetLastError();
            if (    lastError == ERROR_FILE_NOT_FOUND || //no need to check this condition any earlier!
                    lastError == ERROR_BAD_NETPATH)      //
                return CHANGE_DIR_MISSING;

            const wxString errorMessage = wxString(_("Could not initialize directory monitoring:")) + wxT("\n\"") + zToWx(formattedDir) + wxT("\"");
            throw ffs3::FileError(errorMessage + wxT("\n\n") + ffs3::getLastErrorFormatted());
        }

        changeNotifications->push_back(rv);
    }

    if (changeNotifications->size() == 0)
        throw ffs3::FileError(_("At least one directory input field is empty."));


    //detect user request for device removal (e.g. usb stick)
    class HandleVolumeRemoval : public NotifyRequestDeviceRemoval
    {
    public:
        HandleVolumeRemoval(ChangeNotifList& openHandles) :
            NotifyRequestDeviceRemoval(*openHandles), //throw (FileError)
            removalRequested(false),
            operationComplete(false),
            openHandles_(openHandles) {}

        bool requestReceived() const
        {
            return removalRequested;
        }
        bool finished() const
        {
            return operationComplete;
        }

    private:
        virtual void onRequestRemoval(HANDLE hnd) //don't throw!
        {
            openHandles_.reset();    //free all handles
            removalRequested = true; //and make sure they are not used anymore
        }
        virtual void onRemovalFinished(HANDLE hnd, bool successful) //throw()!
        {
            operationComplete = true;
        }

        bool removalRequested;
        bool operationComplete;
        ChangeNotifList& openHandles_;
    } removalRequest(changeNotifications);


    while (true)
    {
        //check for changes within directories:
        const DWORD rv = ::WaitForMultipleObjects( //NOTE: changeNotifications returns valid pointer, because it cannot be empty in this context
                             static_cast<DWORD>(changeNotifications->size()),  //__in  DWORD nCount,
                             &(*changeNotifications)[0],  //__in  const HANDLE *lpHandles,
                             false,                       //__in  BOOL bWaitAll,
                             UI_UPDATE_INTERVAL);         //__in  DWORD dwMilliseconds
        if (WAIT_OBJECT_0 <= rv && rv < WAIT_OBJECT_0 + changeNotifications->size())
            return CHANGE_DETECTED; //directory change detected
        else if (rv == WAIT_FAILED)
            throw ffs3::FileError(wxString(_("Error when monitoring directories.")) + wxT("\n\n") + ffs3::getLastErrorFormatted());
        //else if (rv == WAIT_TIMEOUT)

        if (!dirWatcher.allExisting()) //check for removed devices:
            return CHANGE_DIR_MISSING;

        statusHandler->requestUiRefresh();

        //handle device removal
        if (removalRequest.requestReceived())
        {
            const wxMilliClock_t maxwait = wxGetLocalTimeMillis() + 5000; //HandleVolumeRemoval::finished() not guaranteed!
            while (!removalRequest.finished() && wxGetLocalTimeMillis() < maxwait)
            {
                wxMilliSleep(rts::UI_UPDATE_INTERVAL);
                statusHandler->requestUiRefresh();
            }
            return CHANGE_DIR_MISSING;
        }
    }

#elif defined FFS_LINUX
    std::vector<std::string> fullDirList; //including subdirectories!

    //add all subdirectories
    for (std::vector<Zstring>::const_iterator i = dirNames.begin(); i != dirNames.end(); ++i)
    {
        const Zstring formattedDir = ffs3::getFormattedDirectoryName(*i);

        if (formattedDir.empty())
            throw ffs3::FileError(_("At least one directory input field is empty."));

        dirWatcher.addForMonitoring(formattedDir);


        fullDirList.push_back(formattedDir.c_str());

        try //get all subdirectories
        {
            DirsOnlyTraverser traverser(fullDirList);
            ffs3::traverseFolder(formattedDir, false, traverser); //don't traverse into symlinks (analog to windows build)
        }
        catch (const ffs3::FileError&)
        {
            if (!ffs3::dirExists(formattedDir)) //that's no good locking behavior, but better than nothing
                return CHANGE_DIR_MISSING;

            throw;
        }
    }

    try
    {
        Inotify notifications;
        notifications.SetNonBlock(true);

        for (std::vector<std::string>::const_iterator i = fullDirList.begin(); i != fullDirList.end(); ++i)
        {
            try
            {
                InotifyWatch newWatch(*i,              //dummy object: InotifyWatch may be destructed safely after Inotify::Add()
                                      IN_DONT_FOLLOW | //don't follow symbolic links
                                      IN_ONLYDIR     | //watch directories only
                                      IN_CLOSE_WRITE |
                                      IN_CREATE 	 |
                                      IN_DELETE 	 |
                                      IN_DELETE_SELF |
                                      IN_MODIFY 	 |
                                      IN_MOVE_SELF   |
                                      IN_MOVED_FROM  |
                                      IN_MOVED_TO );
                notifications.Add(newWatch);
            }
            catch (const InotifyException& e)
            {
                if (!ffs3::dirExists(i->c_str())) //that's no good locking behavior, but better than nothing
                    return CHANGE_DIR_MISSING;

                const wxString errorMessage = wxString(_("Could not initialize directory monitoring:")) + wxT("\n\"") + zToWx(i->c_str()) + wxT("\"");
                throw ffs3::FileError(errorMessage + wxT("\n\n") + zToWx(e.GetMessage().c_str()));
            }
        }


        if (notifications.GetWatchCount() == 0)
            throw ffs3::FileError(_("At least one directory input field is empty."));

        while (true)
        {
            notifications.WaitForEvents(); //called in non-blocking mode

            if (notifications.GetEventCount() > 0)
                return CHANGE_DETECTED; //directory change detected

            if (!dirWatcher.allExisting()) //check for removed devices:
                return CHANGE_DIR_MISSING;

            wxMilliSleep(rts::UI_UPDATE_INTERVAL);
            statusHandler->requestUiRefresh();
        }
    }
    catch (const InotifyException& e)
    {
        throw ffs3::FileError(wxString(_("Error when monitoring directories.")) + wxT("\n\n") + zToWx(e.GetMessage().c_str()));
    }
    catch (const std::exception& e)
    {
        throw ffs3::FileError(wxString(_("Error when monitoring directories.")) + wxT("\n\n") + zToWx(e.what()));
    }
#endif
}


void rts::waitForMissingDirs(const std::vector<Zstring>& dirNames, WaitCallback* statusHandler) //throw(FileError)
{
    //new: support for monitoring newly connected directories volumes (e.g.: USB-sticks)

    wxLongLong lastCheck;

    while (true)
    {
        const int UPDATE_INTERVAL = 1000; //1 second interval
        const wxLongLong current = wxGetLocalTimeMillis();
        if (current - lastCheck >= UPDATE_INTERVAL)
        {
            lastCheck = current;

            bool allExisting = true;
            for (std::vector<Zstring>::const_iterator i = dirNames.begin(); i != dirNames.end(); ++i)
            {
    //support specifying volume by name => call getFormattedDirectoryName() repeatedly
                const Zstring formattedDir = ffs3::getFormattedDirectoryName(*i);

                if (formattedDir.empty())
                    throw ffs3::FileError(_("At least one directory input field is empty."));

                if (!ffs3::dirExists(formattedDir))
                {
                    allExisting = false;
                    break;
                }
            }
            if (allExisting) //check for newly arrived devices:
                return;
        }

        wxMilliSleep(rts::UI_UPDATE_INTERVAL);
        statusHandler->requestUiRefresh();
    }
}
bgstack15