xref: /glogg/src/winwatchtowerdriver.cpp (revision 8b11848fd9995077713535870cee0df00a8eeea0)
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 
63f09fa651SNicolas Bonnefon // WinWatchTowerDriver
64f09fa651SNicolas Bonnefon 
65f09fa651SNicolas Bonnefon WinWatchTowerDriver::WinWatchTowerDriver()
66f09fa651SNicolas Bonnefon {
67f09fa651SNicolas Bonnefon     hCompPort_ = CreateIoCompletionPort( INVALID_HANDLE_VALUE,
68f09fa651SNicolas Bonnefon             NULL,
69f09fa651SNicolas Bonnefon             0x0,
70f09fa651SNicolas Bonnefon             0);
71f09fa651SNicolas Bonnefon }
72f09fa651SNicolas Bonnefon 
73f09fa651SNicolas Bonnefon WinWatchTowerDriver::~WinWatchTowerDriver()
74f09fa651SNicolas Bonnefon {
75f09fa651SNicolas Bonnefon }
76f09fa651SNicolas Bonnefon 
77f09fa651SNicolas Bonnefon WinWatchTowerDriver::FileId WinWatchTowerDriver::addFile(
78f09fa651SNicolas Bonnefon         const std::string& file_name )
79f09fa651SNicolas Bonnefon {
80f09fa651SNicolas Bonnefon     // Nothing for Windows
81f09fa651SNicolas Bonnefon     return { };
82f09fa651SNicolas Bonnefon }
83f09fa651SNicolas Bonnefon 
84f09fa651SNicolas Bonnefon WinWatchTowerDriver::SymlinkId WinWatchTowerDriver::addSymlink(
85f09fa651SNicolas Bonnefon         const std::string& file_name )
86f09fa651SNicolas Bonnefon {
87f09fa651SNicolas Bonnefon     // Nothing for Windows
88f09fa651SNicolas Bonnefon     return { };
89f09fa651SNicolas Bonnefon }
90f09fa651SNicolas Bonnefon 
91f09fa651SNicolas Bonnefon // This implementation is blocking, i.e. it will wait until the file
92f09fa651SNicolas Bonnefon // is effectively loaded in the watchtower thread.
93f09fa651SNicolas Bonnefon WinWatchTowerDriver::DirId WinWatchTowerDriver::addDir(
94f09fa651SNicolas Bonnefon         const std::string& file_name )
95f09fa651SNicolas Bonnefon {
96f09fa651SNicolas Bonnefon     DirId dir_id { };
97f09fa651SNicolas Bonnefon 
98f09fa651SNicolas Bonnefon     // Add will be done in the watchtower thread
99f09fa651SNicolas Bonnefon     {
1003104b268SNicolas Bonnefon         /*
101f09fa651SNicolas Bonnefon         std::lock_guard<std::mutex> lk( action_mutex_ );
102f09fa651SNicolas Bonnefon         scheduled_action_ = std::make_unique<Action>( [this, file_name, &dir_id] {
103f09fa651SNicolas Bonnefon             serialisedAddDir( file_name, dir_id );
104f09fa651SNicolas Bonnefon         } );
1053104b268SNicolas Bonnefon         */
1063104b268SNicolas Bonnefon         serialisedAddDir( file_name, dir_id );
107f09fa651SNicolas Bonnefon     }
108f09fa651SNicolas Bonnefon 
109f09fa651SNicolas Bonnefon     // Poke the thread
110f09fa651SNicolas Bonnefon     PostQueuedCompletionStatus( hCompPort_, 0, 0, NULL );
111f09fa651SNicolas Bonnefon 
112f09fa651SNicolas Bonnefon     // Wait for the add task to be completed
113f09fa651SNicolas Bonnefon     {
1143104b268SNicolas Bonnefon         /*
115f09fa651SNicolas Bonnefon         std::unique_lock<std::mutex> lk( action_mutex_ );
116f09fa651SNicolas Bonnefon         action_done_cv_.wait( lk,
117f09fa651SNicolas Bonnefon                 [this]{ return ( scheduled_action_ == nullptr ); } );
1183104b268SNicolas Bonnefon                 */
119f09fa651SNicolas Bonnefon     }
120f09fa651SNicolas Bonnefon 
121f09fa651SNicolas Bonnefon     LOG(logDEBUG) << "Returned " << dir_id.dir_record_;
122f09fa651SNicolas Bonnefon 
123f09fa651SNicolas Bonnefon     return dir_id;
124f09fa651SNicolas Bonnefon }
125f09fa651SNicolas Bonnefon 
126f09fa651SNicolas Bonnefon 
127f09fa651SNicolas Bonnefon void WinWatchTowerDriver::removeFile(
128*8b11848fSNicolas Bonnefon         const WinWatchTowerDriver::FileId& )
129f09fa651SNicolas Bonnefon {
130f09fa651SNicolas Bonnefon }
131f09fa651SNicolas Bonnefon 
132*8b11848fSNicolas Bonnefon void WinWatchTowerDriver::removeSymlink( const SymlinkId& )
133f09fa651SNicolas Bonnefon {
134f09fa651SNicolas Bonnefon }
135f09fa651SNicolas Bonnefon 
1363104b268SNicolas Bonnefon void WinWatchTowerDriver::removeDir( const DirId& dir_id )
1373104b268SNicolas Bonnefon {
138*8b11848fSNicolas Bonnefon     if ( dir_id.dir_record_ ) {
1393104b268SNicolas Bonnefon         void* handle = dir_id.dir_record_->handle_;
1403104b268SNicolas Bonnefon 
1413104b268SNicolas Bonnefon         LOG(logDEBUG) << "WinWatchTowerDriver::removeDir handle=" << std::hex << handle;
1423104b268SNicolas Bonnefon 
1433104b268SNicolas Bonnefon         CloseHandle( handle );
1443104b268SNicolas Bonnefon     }
145*8b11848fSNicolas Bonnefon     else {
146*8b11848fSNicolas Bonnefon         /* Happens when an error occured when creating the dir_record_ */
147*8b11848fSNicolas Bonnefon     }
148*8b11848fSNicolas Bonnefon }
1493104b268SNicolas Bonnefon 
150f09fa651SNicolas Bonnefon //
151f09fa651SNicolas Bonnefon // Private functions
152f09fa651SNicolas Bonnefon //
153f09fa651SNicolas Bonnefon 
154f09fa651SNicolas Bonnefon // Add a file (run in the context of the WatchTower thread)
155f09fa651SNicolas Bonnefon void WinWatchTowerDriver::serialisedAddDir(
156f09fa651SNicolas Bonnefon         const std::string& dir_name,
157f09fa651SNicolas Bonnefon         DirId& dir_id )
158f09fa651SNicolas Bonnefon {
159f09fa651SNicolas Bonnefon     auto dir_record = std::make_shared<WinWatchedDirRecord>( dir_name );
160f09fa651SNicolas Bonnefon     dir_records_.push_back( std::weak_ptr<WinWatchedDirRecord>( dir_record ) );
161f09fa651SNicolas Bonnefon     unsigned int index_record = dir_records_.size();
162f09fa651SNicolas Bonnefon 
163f09fa651SNicolas Bonnefon     LOG(logDEBUG) << "Adding dir for: " << dir_name;
164f09fa651SNicolas Bonnefon 
165f09fa651SNicolas Bonnefon     // Open the directory
166f09fa651SNicolas Bonnefon     HANDLE hDir = CreateFile(
167f09fa651SNicolas Bonnefon #ifdef UNICODE
168f09fa651SNicolas Bonnefon             longstringize( dir_name ).c_str(),
169f09fa651SNicolas Bonnefon #else
170f09fa651SNicolas Bonnefon             ( dir_name ).c_str(),
171f09fa651SNicolas Bonnefon #endif
172f09fa651SNicolas Bonnefon             FILE_LIST_DIRECTORY,
173f09fa651SNicolas Bonnefon             FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
174f09fa651SNicolas Bonnefon             NULL,
175f09fa651SNicolas Bonnefon             OPEN_EXISTING,
176f09fa651SNicolas Bonnefon             FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
177f09fa651SNicolas Bonnefon             NULL );
178f09fa651SNicolas Bonnefon 
1793104b268SNicolas Bonnefon     if ( hDir == INVALID_HANDLE_VALUE ) {
1803104b268SNicolas Bonnefon         LOG(logERROR) << "CreateFile failed for dir " << dir_name;
1813104b268SNicolas Bonnefon     }
1823104b268SNicolas Bonnefon 
183f09fa651SNicolas Bonnefon     dir_record->handle_ = hDir;
184f09fa651SNicolas Bonnefon 
185f09fa651SNicolas Bonnefon     //create a IO completion port/or associate this key with
186f09fa651SNicolas Bonnefon     //the existing IO completion port
187f09fa651SNicolas Bonnefon     hCompPort_ = CreateIoCompletionPort( hDir,
188f09fa651SNicolas Bonnefon             hCompPort_, //if m_hCompPort is NULL, hDir is associated with a NEW completion port,
189f09fa651SNicolas Bonnefon             //if m_hCompPort is NON-NULL, hDir is associated with the existing completion port that the handle m_hCompPort references
190f09fa651SNicolas Bonnefon             // We use the index (plus 1) of the weak_ptr as a key
191f09fa651SNicolas Bonnefon             index_record,
192f09fa651SNicolas Bonnefon             0 );
193f09fa651SNicolas Bonnefon 
194f09fa651SNicolas Bonnefon     LOG(logDEBUG) << "Weak ptr address stored: " << index_record;
195f09fa651SNicolas Bonnefon 
196f09fa651SNicolas Bonnefon     memset( &overlapped_, 0, sizeof overlapped_ );
197f09fa651SNicolas Bonnefon 
198f09fa651SNicolas Bonnefon     bool status = ReadDirectoryChangesW( hDir,
199f09fa651SNicolas Bonnefon             dir_record->buffer_,
200f09fa651SNicolas Bonnefon             dir_record->buffer_length_,
201f09fa651SNicolas Bonnefon             false,
202f09fa651SNicolas Bonnefon             FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE,
203f09fa651SNicolas Bonnefon             &buffer_length_, // not set when using asynchronous mechanisms...
204f09fa651SNicolas Bonnefon             &overlapped_,
205f09fa651SNicolas Bonnefon             NULL );          // no completion routine
206f09fa651SNicolas Bonnefon 
2073104b268SNicolas Bonnefon     if ( !status ) {
2083104b268SNicolas Bonnefon         LOG(logERROR) << "ReadDirectoryChangesW failed (" << GetLastError() << ")";
209*8b11848fSNicolas Bonnefon         dir_records_.pop_back();
2103104b268SNicolas Bonnefon     }
211*8b11848fSNicolas Bonnefon     else {
212f09fa651SNicolas Bonnefon         dir_id.dir_record_ = dir_record;
213f09fa651SNicolas Bonnefon     }
214*8b11848fSNicolas Bonnefon }
215f09fa651SNicolas Bonnefon 
216f09fa651SNicolas Bonnefon std::vector<ObservedFile<WinWatchTowerDriver>*> WinWatchTowerDriver::waitAndProcessEvents(
217f09fa651SNicolas Bonnefon         ObservedFileList<WinWatchTowerDriver>* list,
21891f7c705SNicolas Bonnefon         std::unique_lock<std::mutex>* lock,
21991f7c705SNicolas Bonnefon         std::vector<ObservedFile<WinWatchTowerDriver>*>* /* not needed in WinWatchTowerDriver */ )
220f09fa651SNicolas Bonnefon {
221f09fa651SNicolas Bonnefon     std::vector<ObservedFile<WinWatchTowerDriver>*> files_to_notify { };
222f09fa651SNicolas Bonnefon 
223f09fa651SNicolas Bonnefon     unsigned long long key = 0;
224f09fa651SNicolas Bonnefon     DWORD num_bytes = 0;
225f09fa651SNicolas Bonnefon     LPOVERLAPPED lpOverlapped = 0;
226f09fa651SNicolas Bonnefon 
2273104b268SNicolas Bonnefon     lock->unlock();
228f09fa651SNicolas Bonnefon     BOOL status = GetQueuedCompletionStatus( hCompPort_,
229f09fa651SNicolas Bonnefon             &num_bytes,
230f09fa651SNicolas Bonnefon             &key,
231f09fa651SNicolas Bonnefon             &lpOverlapped,
232f09fa651SNicolas Bonnefon             INFINITE );
2333104b268SNicolas Bonnefon     lock->lock();
234f09fa651SNicolas Bonnefon 
2353104b268SNicolas Bonnefon     LOG(logDEBUG) << "Event (" << status << ") key: " << std::hex << key;
236f09fa651SNicolas Bonnefon 
237f09fa651SNicolas Bonnefon     if ( key ) {
238f09fa651SNicolas Bonnefon         // Extract the dir from the completion key
239f09fa651SNicolas Bonnefon         auto dir_record_ptr = dir_records_[key - 1];
240f09fa651SNicolas Bonnefon         LOG(logDEBUG) << "use_count = " << dir_record_ptr.use_count();
241f09fa651SNicolas Bonnefon 
242f09fa651SNicolas Bonnefon         if ( std::shared_ptr<WinWatchedDirRecord> dir_record = dir_record_ptr.lock() )
243f09fa651SNicolas Bonnefon         {
244f09fa651SNicolas Bonnefon             LOG(logDEBUG) << "Got event for dir " << dir_record.get();
245f09fa651SNicolas Bonnefon 
246f09fa651SNicolas Bonnefon             WinNotificationInfoList notification_info(
247f09fa651SNicolas Bonnefon                     dir_record->buffer_,
248f09fa651SNicolas Bonnefon                     dir_record->buffer_length_ );
249f09fa651SNicolas Bonnefon 
250f09fa651SNicolas Bonnefon             for ( auto notification : notification_info ) {
251f09fa651SNicolas Bonnefon                 std::string file_path = dir_record->path_ + shortstringize( notification.fileName() );
252f09fa651SNicolas Bonnefon                 LOG(logDEBUG) << "File is " << file_path;
253f09fa651SNicolas Bonnefon                 auto file = list->searchByName( file_path );
254f09fa651SNicolas Bonnefon 
255f09fa651SNicolas Bonnefon                 if ( file )
256f09fa651SNicolas Bonnefon                 {
257f09fa651SNicolas Bonnefon                     files_to_notify.push_back( file );
258f09fa651SNicolas Bonnefon                 }
259f09fa651SNicolas Bonnefon             }
260f09fa651SNicolas Bonnefon 
261f09fa651SNicolas Bonnefon             // Re-listen for changes
262f09fa651SNicolas Bonnefon             status = ReadDirectoryChangesW(
263f09fa651SNicolas Bonnefon                     dir_record->handle_,
264f09fa651SNicolas Bonnefon                     dir_record->buffer_,
265f09fa651SNicolas Bonnefon                     dir_record->buffer_length_,
266f09fa651SNicolas Bonnefon                     false,
267f09fa651SNicolas Bonnefon                     FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE,
268f09fa651SNicolas Bonnefon                     &buffer_length_,// not set when using asynchronous mechanisms...
269f09fa651SNicolas Bonnefon                     &overlapped_,
270f09fa651SNicolas Bonnefon                     NULL );          // no completion routine
271f09fa651SNicolas Bonnefon         }
272f09fa651SNicolas Bonnefon         else {
273f09fa651SNicolas Bonnefon             LOG(logWARNING) << "Looks like our dir_record disappeared!";
274f09fa651SNicolas Bonnefon         }
275f09fa651SNicolas Bonnefon     }
276f09fa651SNicolas Bonnefon     else {
277f09fa651SNicolas Bonnefon         LOG(logDEBUG) << "Signaled";
278f09fa651SNicolas Bonnefon     }
279f09fa651SNicolas Bonnefon 
280f09fa651SNicolas Bonnefon     {
281f09fa651SNicolas Bonnefon         std::lock_guard<std::mutex> lk( action_mutex_ );
282f09fa651SNicolas Bonnefon         if ( scheduled_action_ ) {
283f09fa651SNicolas Bonnefon             (*scheduled_action_)();
284f09fa651SNicolas Bonnefon             scheduled_action_ = nullptr;
285f09fa651SNicolas Bonnefon             action_done_cv_.notify_all();
286f09fa651SNicolas Bonnefon         }
287f09fa651SNicolas Bonnefon     }
288f09fa651SNicolas Bonnefon 
289f09fa651SNicolas Bonnefon     /*
290f09fa651SNicolas Bonnefon     // Just in case someone is waiting for an action to complete
291f09fa651SNicolas Bonnefon     std::lock_guard<std::mutex> lk( action_mutex_ );
292f09fa651SNicolas Bonnefon     scheduled_action_ = nullptr;
293f09fa651SNicolas Bonnefon     action_done_cv_.notify_all();
294f09fa651SNicolas Bonnefon     */
295f09fa651SNicolas Bonnefon     return files_to_notify;
296f09fa651SNicolas Bonnefon }
297f09fa651SNicolas Bonnefon 
298f09fa651SNicolas Bonnefon void WinWatchTowerDriver::interruptWait()
299f09fa651SNicolas Bonnefon {
300f09fa651SNicolas Bonnefon     PostQueuedCompletionStatus( hCompPort_, 0, 0, NULL );
301f09fa651SNicolas Bonnefon }
302f09fa651SNicolas Bonnefon 
303f09fa651SNicolas Bonnefon namespace {
304f09fa651SNicolas Bonnefon     std::string shortstringize( const std::wstring& long_string )
305f09fa651SNicolas Bonnefon     {
306f09fa651SNicolas Bonnefon         std::string short_result {};
307f09fa651SNicolas Bonnefon 
308f09fa651SNicolas Bonnefon         for ( wchar_t c : long_string ) {
309f09fa651SNicolas Bonnefon             // FIXME: that does not work for non ASCII char!!
310f09fa651SNicolas Bonnefon             char short_c = static_cast<char>( c & 0x00FF );
311f09fa651SNicolas Bonnefon             short_result += short_c;
312f09fa651SNicolas Bonnefon         }
313f09fa651SNicolas Bonnefon 
314f09fa651SNicolas Bonnefon         return short_result;
315f09fa651SNicolas Bonnefon     }
316f09fa651SNicolas Bonnefon 
317f09fa651SNicolas Bonnefon     std::wstring longstringize( const std::string& short_string )
318f09fa651SNicolas Bonnefon     {
319f09fa651SNicolas Bonnefon         std::wstring long_result {};
320f09fa651SNicolas Bonnefon 
321f09fa651SNicolas Bonnefon         for ( char c : short_string ) {
322f09fa651SNicolas Bonnefon             wchar_t long_c = static_cast<wchar_t>( c );
323f09fa651SNicolas Bonnefon             long_result += long_c;
324f09fa651SNicolas Bonnefon         }
325f09fa651SNicolas Bonnefon 
326f09fa651SNicolas Bonnefon         return long_result;
327f09fa651SNicolas Bonnefon     }
328f09fa651SNicolas Bonnefon };
329