xref: /glogg/src/winwatchtowerdriver.cpp (revision 3104b26858f76d3848af58b8865d4e7d5735d2f8)
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     {
100*3104b268SNicolas 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         } );
105*3104b268SNicolas Bonnefon         */
106*3104b268SNicolas 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     {
114*3104b268SNicolas 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 ); } );
118*3104b268SNicolas 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(
128f09fa651SNicolas Bonnefon         const WinWatchTowerDriver::FileId& file_id )
129f09fa651SNicolas Bonnefon {
130f09fa651SNicolas Bonnefon }
131f09fa651SNicolas Bonnefon 
132f09fa651SNicolas Bonnefon void WinWatchTowerDriver::removeSymlink( const SymlinkId& symlink_id )
133f09fa651SNicolas Bonnefon {
134f09fa651SNicolas Bonnefon }
135f09fa651SNicolas Bonnefon 
136*3104b268SNicolas Bonnefon void WinWatchTowerDriver::removeDir( const DirId& dir_id )
137*3104b268SNicolas Bonnefon {
138*3104b268SNicolas Bonnefon     void* handle = dir_id.dir_record_->handle_;
139*3104b268SNicolas Bonnefon 
140*3104b268SNicolas Bonnefon     LOG(logDEBUG) << "WinWatchTowerDriver::removeDir handle=" << std::hex << handle;
141*3104b268SNicolas Bonnefon 
142*3104b268SNicolas Bonnefon     CloseHandle( handle );
143*3104b268SNicolas Bonnefon }
144*3104b268SNicolas Bonnefon 
145f09fa651SNicolas Bonnefon //
146f09fa651SNicolas Bonnefon // Private functions
147f09fa651SNicolas Bonnefon //
148f09fa651SNicolas Bonnefon 
149f09fa651SNicolas Bonnefon // Add a file (run in the context of the WatchTower thread)
150f09fa651SNicolas Bonnefon void WinWatchTowerDriver::serialisedAddDir(
151f09fa651SNicolas Bonnefon         const std::string& dir_name,
152f09fa651SNicolas Bonnefon         DirId& dir_id )
153f09fa651SNicolas Bonnefon {
154f09fa651SNicolas Bonnefon     auto dir_record = std::make_shared<WinWatchedDirRecord>( dir_name );
155f09fa651SNicolas Bonnefon     dir_records_.push_back( std::weak_ptr<WinWatchedDirRecord>( dir_record ) );
156f09fa651SNicolas Bonnefon     unsigned int index_record = dir_records_.size();
157f09fa651SNicolas Bonnefon 
158f09fa651SNicolas Bonnefon     LOG(logDEBUG) << "Adding dir for: " << dir_name;
159f09fa651SNicolas Bonnefon 
160f09fa651SNicolas Bonnefon     // Open the directory
161f09fa651SNicolas Bonnefon     HANDLE hDir = CreateFile(
162f09fa651SNicolas Bonnefon #ifdef UNICODE
163f09fa651SNicolas Bonnefon             longstringize( dir_name ).c_str(),
164f09fa651SNicolas Bonnefon #else
165f09fa651SNicolas Bonnefon             ( dir_name ).c_str(),
166f09fa651SNicolas Bonnefon #endif
167f09fa651SNicolas Bonnefon             FILE_LIST_DIRECTORY,
168f09fa651SNicolas Bonnefon             FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
169f09fa651SNicolas Bonnefon             NULL,
170f09fa651SNicolas Bonnefon             OPEN_EXISTING,
171f09fa651SNicolas Bonnefon             FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
172f09fa651SNicolas Bonnefon             NULL );
173f09fa651SNicolas Bonnefon 
174*3104b268SNicolas Bonnefon     if ( hDir == INVALID_HANDLE_VALUE ) {
175*3104b268SNicolas Bonnefon         LOG(logERROR) << "CreateFile failed for dir " << dir_name;
176*3104b268SNicolas Bonnefon     }
177*3104b268SNicolas Bonnefon 
178f09fa651SNicolas Bonnefon     dir_record->handle_ = hDir;
179f09fa651SNicolas Bonnefon 
180f09fa651SNicolas Bonnefon     //create a IO completion port/or associate this key with
181f09fa651SNicolas Bonnefon     //the existing IO completion port
182f09fa651SNicolas Bonnefon     hCompPort_ = CreateIoCompletionPort( hDir,
183f09fa651SNicolas Bonnefon             hCompPort_, //if m_hCompPort is NULL, hDir is associated with a NEW completion port,
184f09fa651SNicolas Bonnefon             //if m_hCompPort is NON-NULL, hDir is associated with the existing completion port that the handle m_hCompPort references
185f09fa651SNicolas Bonnefon             // We use the index (plus 1) of the weak_ptr as a key
186f09fa651SNicolas Bonnefon             index_record,
187f09fa651SNicolas Bonnefon             0 );
188f09fa651SNicolas Bonnefon 
189f09fa651SNicolas Bonnefon     LOG(logDEBUG) << "Weak ptr address stored: " << index_record;
190f09fa651SNicolas Bonnefon 
191f09fa651SNicolas Bonnefon     memset( &overlapped_, 0, sizeof overlapped_ );
192f09fa651SNicolas Bonnefon 
193f09fa651SNicolas Bonnefon     bool status = ReadDirectoryChangesW( hDir,
194f09fa651SNicolas Bonnefon             dir_record->buffer_,
195f09fa651SNicolas Bonnefon             dir_record->buffer_length_,
196f09fa651SNicolas Bonnefon             false,
197f09fa651SNicolas Bonnefon             FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE,
198f09fa651SNicolas Bonnefon             &buffer_length_, // not set when using asynchronous mechanisms...
199f09fa651SNicolas Bonnefon             &overlapped_,
200f09fa651SNicolas Bonnefon             NULL );          // no completion routine
201f09fa651SNicolas Bonnefon 
202*3104b268SNicolas Bonnefon     if ( !status ) {
203*3104b268SNicolas Bonnefon         LOG(logERROR) << "ReadDirectoryChangesW failed (" << GetLastError() << ")";
204*3104b268SNicolas Bonnefon     }
205f09fa651SNicolas Bonnefon 
206f09fa651SNicolas Bonnefon     dir_id.dir_record_ = dir_record;
207f09fa651SNicolas Bonnefon }
208f09fa651SNicolas Bonnefon 
209f09fa651SNicolas Bonnefon std::vector<ObservedFile<WinWatchTowerDriver>*> WinWatchTowerDriver::waitAndProcessEvents(
210f09fa651SNicolas Bonnefon         ObservedFileList<WinWatchTowerDriver>* list,
211*3104b268SNicolas Bonnefon         std::unique_lock<std::mutex>* lock )
212f09fa651SNicolas Bonnefon {
213f09fa651SNicolas Bonnefon     std::vector<ObservedFile<WinWatchTowerDriver>*> files_to_notify { };
214f09fa651SNicolas Bonnefon 
215f09fa651SNicolas Bonnefon     unsigned long long key = 0;
216f09fa651SNicolas Bonnefon     DWORD num_bytes = 0;
217f09fa651SNicolas Bonnefon     LPOVERLAPPED lpOverlapped = 0;
218f09fa651SNicolas Bonnefon 
219*3104b268SNicolas Bonnefon     lock->unlock();
220f09fa651SNicolas Bonnefon     BOOL status = GetQueuedCompletionStatus( hCompPort_,
221f09fa651SNicolas Bonnefon             &num_bytes,
222f09fa651SNicolas Bonnefon             &key,
223f09fa651SNicolas Bonnefon             &lpOverlapped,
224f09fa651SNicolas Bonnefon             INFINITE );
225*3104b268SNicolas Bonnefon     lock->lock();
226f09fa651SNicolas Bonnefon 
227*3104b268SNicolas Bonnefon     LOG(logDEBUG) << "Event (" << status << ") key: " << std::hex << key;
228f09fa651SNicolas Bonnefon 
229f09fa651SNicolas Bonnefon     if ( key ) {
230f09fa651SNicolas Bonnefon         // Extract the dir from the completion key
231f09fa651SNicolas Bonnefon         auto dir_record_ptr = dir_records_[key - 1];
232f09fa651SNicolas Bonnefon         LOG(logDEBUG) << "use_count = " << dir_record_ptr.use_count();
233f09fa651SNicolas Bonnefon 
234f09fa651SNicolas Bonnefon         if ( std::shared_ptr<WinWatchedDirRecord> dir_record = dir_record_ptr.lock() )
235f09fa651SNicolas Bonnefon         {
236f09fa651SNicolas Bonnefon             LOG(logDEBUG) << "Got event for dir " << dir_record.get();
237f09fa651SNicolas Bonnefon 
238f09fa651SNicolas Bonnefon             WinNotificationInfoList notification_info(
239f09fa651SNicolas Bonnefon                     dir_record->buffer_,
240f09fa651SNicolas Bonnefon                     dir_record->buffer_length_ );
241f09fa651SNicolas Bonnefon 
242f09fa651SNicolas Bonnefon             for ( auto notification : notification_info ) {
243f09fa651SNicolas Bonnefon                 std::string file_path = dir_record->path_ + shortstringize( notification.fileName() );
244f09fa651SNicolas Bonnefon                 LOG(logDEBUG) << "File is " << file_path;
245f09fa651SNicolas Bonnefon                 auto file = list->searchByName( file_path );
246f09fa651SNicolas Bonnefon 
247f09fa651SNicolas Bonnefon                 if ( file )
248f09fa651SNicolas Bonnefon                 {
249f09fa651SNicolas Bonnefon                     files_to_notify.push_back( file );
250f09fa651SNicolas Bonnefon                 }
251f09fa651SNicolas Bonnefon             }
252f09fa651SNicolas Bonnefon 
253f09fa651SNicolas Bonnefon             // Re-listen for changes
254f09fa651SNicolas Bonnefon             status = ReadDirectoryChangesW(
255f09fa651SNicolas Bonnefon                     dir_record->handle_,
256f09fa651SNicolas Bonnefon                     dir_record->buffer_,
257f09fa651SNicolas Bonnefon                     dir_record->buffer_length_,
258f09fa651SNicolas Bonnefon                     false,
259f09fa651SNicolas Bonnefon                     FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE,
260f09fa651SNicolas Bonnefon                     &buffer_length_,// not set when using asynchronous mechanisms...
261f09fa651SNicolas Bonnefon                     &overlapped_,
262f09fa651SNicolas Bonnefon                     NULL );          // no completion routine
263f09fa651SNicolas Bonnefon         }
264f09fa651SNicolas Bonnefon         else {
265f09fa651SNicolas Bonnefon             LOG(logWARNING) << "Looks like our dir_record disappeared!";
266f09fa651SNicolas Bonnefon         }
267f09fa651SNicolas Bonnefon     }
268f09fa651SNicolas Bonnefon     else {
269f09fa651SNicolas Bonnefon         LOG(logDEBUG) << "Signaled";
270f09fa651SNicolas Bonnefon     }
271f09fa651SNicolas Bonnefon 
272f09fa651SNicolas Bonnefon     {
273f09fa651SNicolas Bonnefon         std::lock_guard<std::mutex> lk( action_mutex_ );
274f09fa651SNicolas Bonnefon         if ( scheduled_action_ ) {
275f09fa651SNicolas Bonnefon             (*scheduled_action_)();
276f09fa651SNicolas Bonnefon             scheduled_action_ = nullptr;
277f09fa651SNicolas Bonnefon             action_done_cv_.notify_all();
278f09fa651SNicolas Bonnefon         }
279f09fa651SNicolas Bonnefon     }
280f09fa651SNicolas Bonnefon 
281f09fa651SNicolas Bonnefon     /*
282f09fa651SNicolas Bonnefon     // Just in case someone is waiting for an action to complete
283f09fa651SNicolas Bonnefon     std::lock_guard<std::mutex> lk( action_mutex_ );
284f09fa651SNicolas Bonnefon     scheduled_action_ = nullptr;
285f09fa651SNicolas Bonnefon     action_done_cv_.notify_all();
286f09fa651SNicolas Bonnefon     */
287f09fa651SNicolas Bonnefon     return files_to_notify;
288f09fa651SNicolas Bonnefon }
289f09fa651SNicolas Bonnefon 
290f09fa651SNicolas Bonnefon void WinWatchTowerDriver::interruptWait()
291f09fa651SNicolas Bonnefon {
292f09fa651SNicolas Bonnefon     PostQueuedCompletionStatus( hCompPort_, 0, 0, NULL );
293f09fa651SNicolas Bonnefon }
294f09fa651SNicolas Bonnefon 
295f09fa651SNicolas Bonnefon namespace {
296f09fa651SNicolas Bonnefon     std::string shortstringize( const std::wstring& long_string )
297f09fa651SNicolas Bonnefon     {
298f09fa651SNicolas Bonnefon         std::string short_result {};
299f09fa651SNicolas Bonnefon 
300f09fa651SNicolas Bonnefon         for ( wchar_t c : long_string ) {
301f09fa651SNicolas Bonnefon             // FIXME: that does not work for non ASCII char!!
302f09fa651SNicolas Bonnefon             char short_c = static_cast<char>( c & 0x00FF );
303f09fa651SNicolas Bonnefon             short_result += short_c;
304f09fa651SNicolas Bonnefon         }
305f09fa651SNicolas Bonnefon 
306f09fa651SNicolas Bonnefon         return short_result;
307f09fa651SNicolas Bonnefon     }
308f09fa651SNicolas Bonnefon 
309f09fa651SNicolas Bonnefon     std::wstring longstringize( const std::string& short_string )
310f09fa651SNicolas Bonnefon     {
311f09fa651SNicolas Bonnefon         std::wstring long_result {};
312f09fa651SNicolas Bonnefon 
313f09fa651SNicolas Bonnefon         for ( char c : short_string ) {
314f09fa651SNicolas Bonnefon             wchar_t long_c = static_cast<wchar_t>( c );
315f09fa651SNicolas Bonnefon             long_result += long_c;
316f09fa651SNicolas Bonnefon         }
317f09fa651SNicolas Bonnefon 
318f09fa651SNicolas Bonnefon         return long_result;
319f09fa651SNicolas Bonnefon     }
320f09fa651SNicolas Bonnefon };
321