xref: /glogg/src/winwatchtowerdriver.cpp (revision fcaa75578d41fbd1a1b35740bd5dc8176496a14b)
1f09fa651SNicolas Bonnefon #include "winwatchtowerdriver.h"
2f09fa651SNicolas Bonnefon 
3f09fa651SNicolas Bonnefon #define WIN32_LEAN_AND_MEAN
4f09fa651SNicolas Bonnefon #include <windows.h>
5f09fa651SNicolas Bonnefon #include <winbase.h>
6f09fa651SNicolas Bonnefon 
7f09fa651SNicolas Bonnefon #include <map>
8f09fa651SNicolas Bonnefon 
9f09fa651SNicolas Bonnefon #include "watchtowerlist.h"
10f09fa651SNicolas Bonnefon #include "utils.h"
11f09fa651SNicolas Bonnefon #include "log.h"
12f09fa651SNicolas Bonnefon 
13f09fa651SNicolas Bonnefon namespace {
14f09fa651SNicolas Bonnefon     std::string shortstringize( const std::wstring& long_string );
15f09fa651SNicolas Bonnefon     std::wstring longstringize( const std::string& short_string );
16f09fa651SNicolas Bonnefon };
17f09fa651SNicolas Bonnefon 
18f09fa651SNicolas Bonnefon // Utility classes
19f09fa651SNicolas Bonnefon 
20f09fa651SNicolas Bonnefon WinNotificationInfoList::WinNotificationInfoList( const char* buffer, size_t buffer_size )
21f09fa651SNicolas Bonnefon {
22f09fa651SNicolas Bonnefon     pointer_ = buffer;
23f09fa651SNicolas Bonnefon     next_ = updateCurrentNotification( pointer_ );
24f09fa651SNicolas Bonnefon }
25f09fa651SNicolas Bonnefon 
26f09fa651SNicolas Bonnefon const char* WinNotificationInfoList::updateCurrentNotification(
27f09fa651SNicolas Bonnefon         const char* new_position )
28f09fa651SNicolas Bonnefon {
29f09fa651SNicolas Bonnefon     using Action = WinNotificationInfo::Action;
30f09fa651SNicolas Bonnefon 
31f09fa651SNicolas Bonnefon     static const std::map<uint16_t, Action> int_to_action = {
32f09fa651SNicolas Bonnefon         { FILE_ACTION_ADDED, Action::ADDED },
33f09fa651SNicolas Bonnefon         { FILE_ACTION_REMOVED, Action::REMOVED },
34f09fa651SNicolas Bonnefon         { FILE_ACTION_MODIFIED, Action::MODIFIED },
35f09fa651SNicolas Bonnefon         { FILE_ACTION_RENAMED_OLD_NAME, Action::RENAMED_OLD_NAME },
36f09fa651SNicolas Bonnefon         { FILE_ACTION_RENAMED_NEW_NAME, Action::RENAMED_NEW_NAME },
37f09fa651SNicolas Bonnefon     };
38f09fa651SNicolas Bonnefon 
39f09fa651SNicolas Bonnefon     uint32_t next_offset = *( reinterpret_cast<const uint32_t*>( new_position ) );
40f09fa651SNicolas Bonnefon     uint32_t action      = *( reinterpret_cast<const uint32_t*>( new_position ) + 1 );
41f09fa651SNicolas Bonnefon     uint32_t length      = *( reinterpret_cast<const uint32_t*>( new_position ) + 2 );
42f09fa651SNicolas Bonnefon 
43f09fa651SNicolas Bonnefon     const std::wstring file_name = { reinterpret_cast<const wchar_t*>( new_position + 12 ), length / 2 };
44f09fa651SNicolas Bonnefon 
45f09fa651SNicolas Bonnefon     LOG(logDEBUG) << "Next: " << next_offset;
46f09fa651SNicolas Bonnefon     LOG(logDEBUG) << "Action: " << action;
47f09fa651SNicolas Bonnefon     LOG(logDEBUG) << "Length: " << length;
48f09fa651SNicolas Bonnefon 
49f09fa651SNicolas Bonnefon     current_notification_ = WinNotificationInfo( int_to_action.at( action ), file_name );
50f09fa651SNicolas Bonnefon 
51f09fa651SNicolas Bonnefon     return ( next_offset == 0 ) ? nullptr : new_position + next_offset;
52f09fa651SNicolas Bonnefon }
53f09fa651SNicolas Bonnefon 
54f09fa651SNicolas Bonnefon const char* WinNotificationInfoList::advanceToNext()
55f09fa651SNicolas Bonnefon {
56f09fa651SNicolas Bonnefon     pointer_ = next_;
57f09fa651SNicolas Bonnefon     if ( pointer_ )
58f09fa651SNicolas Bonnefon         next_ = updateCurrentNotification( pointer_ );
59f09fa651SNicolas Bonnefon 
60f09fa651SNicolas Bonnefon     return pointer_;
61f09fa651SNicolas Bonnefon }
62f09fa651SNicolas Bonnefon 
63*fcaa7557SNicolas Bonnefon // WinWatchTowerDriver::FileChangeToken
64*fcaa7557SNicolas Bonnefon 
65*fcaa7557SNicolas Bonnefon void WinWatchTowerDriver::FileChangeToken::readFromFile(
66*fcaa7557SNicolas Bonnefon         const std::string& file_name )
67*fcaa7557SNicolas Bonnefon {
68*fcaa7557SNicolas Bonnefon     // On Windows, we open the file and get its last written date/time
69*fcaa7557SNicolas Bonnefon     // That seems to work alright in my tests, but who knows for sure?
70*fcaa7557SNicolas Bonnefon 
71*fcaa7557SNicolas Bonnefon     HANDLE hFile = CreateFile(
72*fcaa7557SNicolas Bonnefon             file_name.c_str(),
73*fcaa7557SNicolas Bonnefon             0,
74*fcaa7557SNicolas Bonnefon             FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
75*fcaa7557SNicolas Bonnefon             NULL,
76*fcaa7557SNicolas Bonnefon             OPEN_EXISTING,
77*fcaa7557SNicolas Bonnefon             FILE_ATTRIBUTE_NORMAL,
78*fcaa7557SNicolas Bonnefon             NULL );
79*fcaa7557SNicolas Bonnefon 
80*fcaa7557SNicolas Bonnefon     if ( hFile == (HANDLE)-1 ) {
81*fcaa7557SNicolas Bonnefon         DWORD err = GetLastError();
82*fcaa7557SNicolas Bonnefon         LOG(logERROR) << "FileChangeToken::readFromFile: failed with " << err;
83*fcaa7557SNicolas Bonnefon 
84*fcaa7557SNicolas Bonnefon         low_date_time_ = 0;
85*fcaa7557SNicolas Bonnefon         high_date_time_ = 0;
86*fcaa7557SNicolas Bonnefon 
87*fcaa7557SNicolas Bonnefon         return;
88*fcaa7557SNicolas Bonnefon     }
89*fcaa7557SNicolas Bonnefon     else {
90*fcaa7557SNicolas Bonnefon         BY_HANDLE_FILE_INFORMATION file_info;
91*fcaa7557SNicolas Bonnefon 
92*fcaa7557SNicolas Bonnefon         if ( GetFileInformationByHandle(
93*fcaa7557SNicolas Bonnefon                     hFile,
94*fcaa7557SNicolas Bonnefon                     &file_info ) ) {
95*fcaa7557SNicolas Bonnefon             low_date_time_ = file_info.ftLastWriteTime.dwLowDateTime;
96*fcaa7557SNicolas Bonnefon             high_date_time_ = file_info.ftLastWriteTime.dwHighDateTime;
97*fcaa7557SNicolas Bonnefon 
98*fcaa7557SNicolas Bonnefon             LOG(logDEBUG) << "FileChangeToken::readFromFile: low_date_time_ " << low_date_time_;
99*fcaa7557SNicolas Bonnefon             LOG(logDEBUG) << "FileChangeToken::readFromFile: high_date_time_ " << high_date_time_;
100*fcaa7557SNicolas Bonnefon         }
101*fcaa7557SNicolas Bonnefon         else {
102*fcaa7557SNicolas Bonnefon             DWORD err = GetLastError();
103*fcaa7557SNicolas Bonnefon             LOG(logERROR) << "FileChangeToken::readFromFile: failed with " << err;
104*fcaa7557SNicolas Bonnefon 
105*fcaa7557SNicolas Bonnefon             low_date_time_ = 0;
106*fcaa7557SNicolas Bonnefon             high_date_time_ = 0;
107*fcaa7557SNicolas Bonnefon         }
108*fcaa7557SNicolas Bonnefon     }
109*fcaa7557SNicolas Bonnefon 
110*fcaa7557SNicolas Bonnefon     CloseHandle(hFile);
111*fcaa7557SNicolas Bonnefon }
112*fcaa7557SNicolas Bonnefon 
113f09fa651SNicolas Bonnefon // WinWatchTowerDriver
114f09fa651SNicolas Bonnefon 
115f09fa651SNicolas Bonnefon WinWatchTowerDriver::WinWatchTowerDriver()
116f09fa651SNicolas Bonnefon {
117f09fa651SNicolas Bonnefon     hCompPort_ = CreateIoCompletionPort( INVALID_HANDLE_VALUE,
118f09fa651SNicolas Bonnefon             NULL,
119f09fa651SNicolas Bonnefon             0x0,
120f09fa651SNicolas Bonnefon             0);
121f09fa651SNicolas Bonnefon }
122f09fa651SNicolas Bonnefon 
123f09fa651SNicolas Bonnefon WinWatchTowerDriver::~WinWatchTowerDriver()
124f09fa651SNicolas Bonnefon {
125f09fa651SNicolas Bonnefon }
126f09fa651SNicolas Bonnefon 
127f09fa651SNicolas Bonnefon WinWatchTowerDriver::FileId WinWatchTowerDriver::addFile(
128f09fa651SNicolas Bonnefon         const std::string& file_name )
129f09fa651SNicolas Bonnefon {
130f09fa651SNicolas Bonnefon     // Nothing for Windows
131f09fa651SNicolas Bonnefon     return { };
132f09fa651SNicolas Bonnefon }
133f09fa651SNicolas Bonnefon 
134f09fa651SNicolas Bonnefon WinWatchTowerDriver::SymlinkId WinWatchTowerDriver::addSymlink(
135f09fa651SNicolas Bonnefon         const std::string& file_name )
136f09fa651SNicolas Bonnefon {
137f09fa651SNicolas Bonnefon     // Nothing for Windows
138f09fa651SNicolas Bonnefon     return { };
139f09fa651SNicolas Bonnefon }
140f09fa651SNicolas Bonnefon 
141f09fa651SNicolas Bonnefon // This implementation is blocking, i.e. it will wait until the file
142f09fa651SNicolas Bonnefon // is effectively loaded in the watchtower thread.
143f09fa651SNicolas Bonnefon WinWatchTowerDriver::DirId WinWatchTowerDriver::addDir(
144f09fa651SNicolas Bonnefon         const std::string& file_name )
145f09fa651SNicolas Bonnefon {
146f09fa651SNicolas Bonnefon     DirId dir_id { };
147f09fa651SNicolas Bonnefon 
148f09fa651SNicolas Bonnefon     // Add will be done in the watchtower thread
149f09fa651SNicolas Bonnefon     {
1503104b268SNicolas Bonnefon         /*
151f09fa651SNicolas Bonnefon         std::lock_guard<std::mutex> lk( action_mutex_ );
152f09fa651SNicolas Bonnefon         scheduled_action_ = std::make_unique<Action>( [this, file_name, &dir_id] {
153f09fa651SNicolas Bonnefon             serialisedAddDir( file_name, dir_id );
154f09fa651SNicolas Bonnefon         } );
1553104b268SNicolas Bonnefon         */
1563104b268SNicolas Bonnefon         serialisedAddDir( file_name, dir_id );
157f09fa651SNicolas Bonnefon     }
158f09fa651SNicolas Bonnefon 
159f09fa651SNicolas Bonnefon     // Poke the thread
160f09fa651SNicolas Bonnefon     PostQueuedCompletionStatus( hCompPort_, 0, 0, NULL );
161f09fa651SNicolas Bonnefon 
162f09fa651SNicolas Bonnefon     // Wait for the add task to be completed
163f09fa651SNicolas Bonnefon     {
1643104b268SNicolas Bonnefon         /*
165f09fa651SNicolas Bonnefon         std::unique_lock<std::mutex> lk( action_mutex_ );
166f09fa651SNicolas Bonnefon         action_done_cv_.wait( lk,
167f09fa651SNicolas Bonnefon                 [this]{ return ( scheduled_action_ == nullptr ); } );
1683104b268SNicolas Bonnefon                 */
169f09fa651SNicolas Bonnefon     }
170f09fa651SNicolas Bonnefon 
171dc1c9514SNicolas Bonnefon     LOG(logDEBUG) << "addDir returned " << dir_id.dir_record_;
172f09fa651SNicolas Bonnefon 
173f09fa651SNicolas Bonnefon     return dir_id;
174f09fa651SNicolas Bonnefon }
175f09fa651SNicolas Bonnefon 
176f09fa651SNicolas Bonnefon 
177f09fa651SNicolas Bonnefon void WinWatchTowerDriver::removeFile(
1788b11848fSNicolas Bonnefon         const WinWatchTowerDriver::FileId& )
179f09fa651SNicolas Bonnefon {
180f09fa651SNicolas Bonnefon }
181f09fa651SNicolas Bonnefon 
1828b11848fSNicolas Bonnefon void WinWatchTowerDriver::removeSymlink( const SymlinkId& )
183f09fa651SNicolas Bonnefon {
184f09fa651SNicolas Bonnefon }
185f09fa651SNicolas Bonnefon 
1863104b268SNicolas Bonnefon void WinWatchTowerDriver::removeDir( const DirId& dir_id )
1873104b268SNicolas Bonnefon {
188dc1c9514SNicolas Bonnefon     LOG(logDEBUG) << "Entering driver::removeDir";
1898b11848fSNicolas Bonnefon     if ( dir_id.dir_record_ ) {
1903104b268SNicolas Bonnefon         void* handle = dir_id.dir_record_->handle_;
1913104b268SNicolas Bonnefon 
1923104b268SNicolas Bonnefon         LOG(logDEBUG) << "WinWatchTowerDriver::removeDir handle=" << std::hex << handle;
1933104b268SNicolas Bonnefon 
1943104b268SNicolas Bonnefon         CloseHandle( handle );
1953104b268SNicolas Bonnefon     }
1968b11848fSNicolas Bonnefon     else {
1978b11848fSNicolas Bonnefon         /* Happens when an error occured when creating the dir_record_ */
1988b11848fSNicolas Bonnefon     }
1998b11848fSNicolas Bonnefon }
2003104b268SNicolas Bonnefon 
201f09fa651SNicolas Bonnefon //
202f09fa651SNicolas Bonnefon // Private functions
203f09fa651SNicolas Bonnefon //
204f09fa651SNicolas Bonnefon 
205f09fa651SNicolas Bonnefon // Add a file (run in the context of the WatchTower thread)
206f09fa651SNicolas Bonnefon void WinWatchTowerDriver::serialisedAddDir(
207f09fa651SNicolas Bonnefon         const std::string& dir_name,
208f09fa651SNicolas Bonnefon         DirId& dir_id )
209f09fa651SNicolas Bonnefon {
210dc1c9514SNicolas Bonnefon     bool inserted = false;
211f09fa651SNicolas Bonnefon     auto dir_record = std::make_shared<WinWatchedDirRecord>( dir_name );
212dc1c9514SNicolas Bonnefon     // The index we will be inserting this record (if success), plus 1 (to avoid
213dc1c9514SNicolas Bonnefon     // 0 which is used as a magic value)
214dc1c9514SNicolas Bonnefon     unsigned int index_record = dir_records_.size() + 1;
215f09fa651SNicolas Bonnefon 
216f09fa651SNicolas Bonnefon     LOG(logDEBUG) << "Adding dir for: " << dir_name;
217f09fa651SNicolas Bonnefon 
218f09fa651SNicolas Bonnefon     // Open the directory
219f09fa651SNicolas Bonnefon     HANDLE hDir = CreateFile(
220f09fa651SNicolas Bonnefon #ifdef UNICODE
221f09fa651SNicolas Bonnefon             longstringize( dir_name ).c_str(),
222f09fa651SNicolas Bonnefon #else
223f09fa651SNicolas Bonnefon             ( dir_name ).c_str(),
224f09fa651SNicolas Bonnefon #endif
225f09fa651SNicolas Bonnefon             FILE_LIST_DIRECTORY,
226f09fa651SNicolas Bonnefon             FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
227f09fa651SNicolas Bonnefon             NULL,
228f09fa651SNicolas Bonnefon             OPEN_EXISTING,
229f09fa651SNicolas Bonnefon             FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
230f09fa651SNicolas Bonnefon             NULL );
231f09fa651SNicolas Bonnefon 
2323104b268SNicolas Bonnefon     if ( hDir == INVALID_HANDLE_VALUE ) {
2333104b268SNicolas Bonnefon         LOG(logERROR) << "CreateFile failed for dir " << dir_name;
2343104b268SNicolas Bonnefon     }
235dc1c9514SNicolas Bonnefon     else {
236f09fa651SNicolas Bonnefon         dir_record->handle_ = hDir;
237f09fa651SNicolas Bonnefon 
238f09fa651SNicolas Bonnefon         //create a IO completion port/or associate this key with
239f09fa651SNicolas Bonnefon         //the existing IO completion port
240f09fa651SNicolas Bonnefon         hCompPort_ = CreateIoCompletionPort( hDir,
241f09fa651SNicolas Bonnefon                 hCompPort_, //if m_hCompPort is NULL, hDir is associated with a NEW completion port,
242f09fa651SNicolas Bonnefon                 //if m_hCompPort is NON-NULL, hDir is associated with the existing completion port that the handle m_hCompPort references
243f09fa651SNicolas Bonnefon                 // We use the index (plus 1) of the weak_ptr as a key
244f09fa651SNicolas Bonnefon                 index_record,
245f09fa651SNicolas Bonnefon                 0 );
246f09fa651SNicolas Bonnefon 
247f09fa651SNicolas Bonnefon         LOG(logDEBUG) << "Weak ptr address stored: " << index_record;
248f09fa651SNicolas Bonnefon 
249f09fa651SNicolas Bonnefon         memset( &overlapped_, 0, sizeof overlapped_ );
250f09fa651SNicolas Bonnefon 
251dc1c9514SNicolas Bonnefon         inserted = ReadDirectoryChangesW( hDir,
252f09fa651SNicolas Bonnefon                 dir_record->buffer_,
253f09fa651SNicolas Bonnefon                 dir_record->buffer_length_,
254f09fa651SNicolas Bonnefon                 false,
255f09fa651SNicolas Bonnefon                 FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE,
256f09fa651SNicolas Bonnefon                 &buffer_length_, // not set when using asynchronous mechanisms...
257f09fa651SNicolas Bonnefon                 &overlapped_,
258f09fa651SNicolas Bonnefon                 NULL );          // no completion routine
259f09fa651SNicolas Bonnefon 
260dc1c9514SNicolas Bonnefon         if ( ! inserted ) {
2613104b268SNicolas Bonnefon             LOG(logERROR) << "ReadDirectoryChangesW failed (" << GetLastError() << ")";
262dc1c9514SNicolas Bonnefon             CloseHandle( hDir );
2633104b268SNicolas Bonnefon         }
2648b11848fSNicolas Bonnefon         else {
265f09fa651SNicolas Bonnefon             dir_id.dir_record_ = dir_record;
266f09fa651SNicolas Bonnefon         }
2678b11848fSNicolas Bonnefon     }
268f09fa651SNicolas Bonnefon 
269dc1c9514SNicolas Bonnefon     if ( inserted ) {
270dc1c9514SNicolas Bonnefon         dir_records_.push_back( std::weak_ptr<WinWatchedDirRecord>( dir_record ) );
271dc1c9514SNicolas Bonnefon     }
272dc1c9514SNicolas Bonnefon }
273dc1c9514SNicolas Bonnefon 
274f09fa651SNicolas Bonnefon std::vector<ObservedFile<WinWatchTowerDriver>*> WinWatchTowerDriver::waitAndProcessEvents(
275f09fa651SNicolas Bonnefon         ObservedFileList<WinWatchTowerDriver>* list,
27691f7c705SNicolas Bonnefon         std::unique_lock<std::mutex>* lock,
277*fcaa7557SNicolas Bonnefon         std::vector<ObservedFile<WinWatchTowerDriver>*>* /* not needed in WinWatchTowerDriver */,
278*fcaa7557SNicolas Bonnefon         int timeout_ms )
279f09fa651SNicolas Bonnefon {
280f09fa651SNicolas Bonnefon     std::vector<ObservedFile<WinWatchTowerDriver>*> files_to_notify { };
281f09fa651SNicolas Bonnefon 
2823089b1a1SNicolas Bonnefon     ULONG_PTR key = 0;
283f09fa651SNicolas Bonnefon     DWORD num_bytes = 0;
284f09fa651SNicolas Bonnefon     LPOVERLAPPED lpOverlapped = 0;
285f09fa651SNicolas Bonnefon 
286*fcaa7557SNicolas Bonnefon     if ( timeout_ms == 0 )
287*fcaa7557SNicolas Bonnefon         timeout_ms = INFINITE;
288*fcaa7557SNicolas Bonnefon 
2893104b268SNicolas Bonnefon     lock->unlock();
290dc1c9514SNicolas Bonnefon     LOG(logDEBUG) << "waitAndProcessEvents now blocking...";
291f09fa651SNicolas Bonnefon     BOOL status = GetQueuedCompletionStatus( hCompPort_,
292f09fa651SNicolas Bonnefon             &num_bytes,
293f09fa651SNicolas Bonnefon             &key,
294f09fa651SNicolas Bonnefon             &lpOverlapped,
295*fcaa7557SNicolas Bonnefon             timeout_ms );
2963104b268SNicolas Bonnefon     lock->lock();
297f09fa651SNicolas Bonnefon 
2983104b268SNicolas Bonnefon     LOG(logDEBUG) << "Event (" << status << ") key: " << std::hex << key;
299f09fa651SNicolas Bonnefon 
300f09fa651SNicolas Bonnefon     if ( key ) {
301f09fa651SNicolas Bonnefon         // Extract the dir from the completion key
302f09fa651SNicolas Bonnefon         auto dir_record_ptr = dir_records_[key - 1];
303f09fa651SNicolas Bonnefon         LOG(logDEBUG) << "use_count = " << dir_record_ptr.use_count();
304f09fa651SNicolas Bonnefon 
305f09fa651SNicolas Bonnefon         if ( std::shared_ptr<WinWatchedDirRecord> dir_record = dir_record_ptr.lock() )
306f09fa651SNicolas Bonnefon         {
307f09fa651SNicolas Bonnefon             LOG(logDEBUG) << "Got event for dir " << dir_record.get();
308f09fa651SNicolas Bonnefon 
309f09fa651SNicolas Bonnefon             WinNotificationInfoList notification_info(
310f09fa651SNicolas Bonnefon                     dir_record->buffer_,
311f09fa651SNicolas Bonnefon                     dir_record->buffer_length_ );
312f09fa651SNicolas Bonnefon 
313f09fa651SNicolas Bonnefon             for ( auto notification : notification_info ) {
314f09fa651SNicolas Bonnefon                 std::string file_path = dir_record->path_ + shortstringize( notification.fileName() );
315f09fa651SNicolas Bonnefon                 LOG(logDEBUG) << "File is " << file_path;
316f09fa651SNicolas Bonnefon                 auto file = list->searchByName( file_path );
317f09fa651SNicolas Bonnefon 
318f09fa651SNicolas Bonnefon                 if ( file )
319f09fa651SNicolas Bonnefon                 {
320f09fa651SNicolas Bonnefon                     files_to_notify.push_back( file );
321f09fa651SNicolas Bonnefon                 }
322f09fa651SNicolas Bonnefon             }
323f09fa651SNicolas Bonnefon 
324f09fa651SNicolas Bonnefon             // Re-listen for changes
325f09fa651SNicolas Bonnefon             status = ReadDirectoryChangesW(
326f09fa651SNicolas Bonnefon                     dir_record->handle_,
327f09fa651SNicolas Bonnefon                     dir_record->buffer_,
328f09fa651SNicolas Bonnefon                     dir_record->buffer_length_,
329f09fa651SNicolas Bonnefon                     false,
330f09fa651SNicolas Bonnefon                     FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE,
331f09fa651SNicolas Bonnefon                     &buffer_length_,// not set when using asynchronous mechanisms...
332f09fa651SNicolas Bonnefon                     &overlapped_,
333f09fa651SNicolas Bonnefon                     NULL );          // no completion routine
334f09fa651SNicolas Bonnefon         }
335f09fa651SNicolas Bonnefon         else {
336f09fa651SNicolas Bonnefon             LOG(logWARNING) << "Looks like our dir_record disappeared!";
337f09fa651SNicolas Bonnefon         }
338f09fa651SNicolas Bonnefon     }
339f09fa651SNicolas Bonnefon     else {
340f09fa651SNicolas Bonnefon         LOG(logDEBUG) << "Signaled";
341f09fa651SNicolas Bonnefon     }
342f09fa651SNicolas Bonnefon 
343f09fa651SNicolas Bonnefon     {
344f09fa651SNicolas Bonnefon         std::lock_guard<std::mutex> lk( action_mutex_ );
345f09fa651SNicolas Bonnefon         if ( scheduled_action_ ) {
346f09fa651SNicolas Bonnefon             (*scheduled_action_)();
347f09fa651SNicolas Bonnefon             scheduled_action_ = nullptr;
348f09fa651SNicolas Bonnefon             action_done_cv_.notify_all();
349f09fa651SNicolas Bonnefon         }
350f09fa651SNicolas Bonnefon     }
351f09fa651SNicolas Bonnefon 
352f09fa651SNicolas Bonnefon     /*
353f09fa651SNicolas Bonnefon     // Just in case someone is waiting for an action to complete
354f09fa651SNicolas Bonnefon     std::lock_guard<std::mutex> lk( action_mutex_ );
355f09fa651SNicolas Bonnefon     scheduled_action_ = nullptr;
356f09fa651SNicolas Bonnefon     action_done_cv_.notify_all();
357f09fa651SNicolas Bonnefon     */
358f09fa651SNicolas Bonnefon     return files_to_notify;
359f09fa651SNicolas Bonnefon }
360f09fa651SNicolas Bonnefon 
361f09fa651SNicolas Bonnefon void WinWatchTowerDriver::interruptWait()
362f09fa651SNicolas Bonnefon {
363dc1c9514SNicolas Bonnefon     LOG(logDEBUG) << "Driver::interruptWait()";
364f09fa651SNicolas Bonnefon     PostQueuedCompletionStatus( hCompPort_, 0, 0, NULL );
365f09fa651SNicolas Bonnefon }
366f09fa651SNicolas Bonnefon 
367f09fa651SNicolas Bonnefon namespace {
368f09fa651SNicolas Bonnefon     std::string shortstringize( const std::wstring& long_string )
369f09fa651SNicolas Bonnefon     {
370f09fa651SNicolas Bonnefon         std::string short_result {};
371f09fa651SNicolas Bonnefon 
372f09fa651SNicolas Bonnefon         for ( wchar_t c : long_string ) {
373f09fa651SNicolas Bonnefon             // FIXME: that does not work for non ASCII char!!
374f09fa651SNicolas Bonnefon             char short_c = static_cast<char>( c & 0x00FF );
375f09fa651SNicolas Bonnefon             short_result += short_c;
376f09fa651SNicolas Bonnefon         }
377f09fa651SNicolas Bonnefon 
378f09fa651SNicolas Bonnefon         return short_result;
379f09fa651SNicolas Bonnefon     }
380f09fa651SNicolas Bonnefon 
381f09fa651SNicolas Bonnefon     std::wstring longstringize( const std::string& short_string )
382f09fa651SNicolas Bonnefon     {
383f09fa651SNicolas Bonnefon         std::wstring long_result {};
384f09fa651SNicolas Bonnefon 
385f09fa651SNicolas Bonnefon         for ( char c : short_string ) {
386f09fa651SNicolas Bonnefon             wchar_t long_c = static_cast<wchar_t>( c );
387f09fa651SNicolas Bonnefon             long_result += long_c;
388f09fa651SNicolas Bonnefon         }
389f09fa651SNicolas Bonnefon 
390f09fa651SNicolas Bonnefon         return long_result;
391f09fa651SNicolas Bonnefon     }
392f09fa651SNicolas Bonnefon };
393