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
|
#include <efsw/FileSystem.hpp>
#include <efsw/FileWatcherWin32.hpp>
#include <efsw/Lock.hpp>
#include <efsw/String.hpp>
#include <efsw/System.hpp>
#if EFSW_PLATFORM == EFSW_PLATFORM_WIN32
namespace efsw {
FileWatcherWin32::FileWatcherWin32( FileWatcher* parent ) :
FileWatcherImpl( parent ), mLastWatchID( 0 ), mThread( NULL ) {
mIOCP = CreateIoCompletionPort( INVALID_HANDLE_VALUE, NULL, 0, 1 );
if ( mIOCP && mIOCP != INVALID_HANDLE_VALUE )
mInitOK = true;
}
FileWatcherWin32::~FileWatcherWin32() {
mInitOK = false;
if ( mIOCP && mIOCP != INVALID_HANDLE_VALUE ) {
PostQueuedCompletionStatus( mIOCP, 0, reinterpret_cast<ULONG_PTR>( this ), NULL );
}
efSAFE_DELETE( mThread );
removeAllWatches();
CloseHandle( mIOCP );
}
WatchID FileWatcherWin32::addWatch( const std::string& directory, FileWatchListener* watcher,
bool recursive ) {
std::string dir( directory );
FileInfo fi( dir );
if ( !fi.isDirectory() ) {
return Errors::Log::createLastError( Errors::FileNotFound, dir );
} else if ( !fi.isReadable() ) {
return Errors::Log::createLastError( Errors::FileNotReadable, dir );
}
FileSystem::dirAddSlashAtEnd( dir );
Lock lock( mWatchesLock );
if ( pathInWatches( dir ) ) {
return Errors::Log::createLastError( Errors::FileRepeated, dir );
}
WatchID watchid = ++mLastWatchID;
WatcherStructWin32* watch = CreateWatch(
String::fromUtf8( dir ).toWideString().c_str(), recursive,
FILE_NOTIFY_CHANGE_CREATION | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_FILE_NAME |
FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_SIZE,
mIOCP );
if ( NULL == watch ) {
return Errors::Log::createLastError( Errors::FileNotFound, dir );
}
// Add the handle to the handles vector
watch->Watch->ID = watchid;
watch->Watch->Watch = this;
watch->Watch->Listener = watcher;
watch->Watch->DirName = new char[dir.length() + 1];
strcpy( watch->Watch->DirName, dir.c_str() );
mWatches.insert( watch );
return watchid;
}
void FileWatcherWin32::removeWatch( const std::string& directory ) {
Lock lock( mWatchesLock );
Watches::iterator iter = mWatches.begin();
for ( ; iter != mWatches.end(); ++iter ) {
if ( directory == ( *iter )->Watch->DirName ) {
removeWatch( *iter );
break;
}
}
}
void FileWatcherWin32::removeWatch( WatchID watchid ) {
Lock lock( mWatchesLock );
Watches::iterator iter = mWatches.begin();
for ( ; iter != mWatches.end(); ++iter ) {
// Find the watch ID
if ( ( *iter )->Watch->ID == watchid ) {
removeWatch( *iter );
return;
}
}
}
void FileWatcherWin32::removeWatch( WatcherStructWin32* watch ) {
Lock lock( mWatchesLock );
DestroyWatch( watch );
mWatches.erase( watch );
}
void FileWatcherWin32::watch() {
if ( NULL == mThread ) {
mThread = new Thread( &FileWatcherWin32::run, this );
mThread->launch();
}
}
void FileWatcherWin32::removeAllWatches() {
Lock lock( mWatchesLock );
Watches::iterator iter = mWatches.begin();
for ( ; iter != mWatches.end(); ++iter ) {
DestroyWatch( ( *iter ) );
}
mWatches.clear();
}
void FileWatcherWin32::run() {
do {
if ( mInitOK && !mWatches.empty() ) {
DWORD numOfBytes = 0;
OVERLAPPED* ov = NULL;
ULONG_PTR compKey = 0;
BOOL res = FALSE;
while ( ( res = GetQueuedCompletionStatus( mIOCP, &numOfBytes, &compKey, &ov,
INFINITE ) ) != FALSE ) {
if ( compKey != 0 && compKey == reinterpret_cast<ULONG_PTR>( this ) ) {
break;
} else {
Lock lock( mWatchesLock );
WatchCallback( numOfBytes, ov );
}
}
} else {
System::sleep( 10 );
}
} while ( mInitOK );
removeAllWatches();
}
void FileWatcherWin32::handleAction( Watcher* watch, const std::string& filename,
unsigned long action, std::string /*oldFilename*/ ) {
Action fwAction;
switch ( action ) {
case FILE_ACTION_RENAMED_OLD_NAME:
watch->OldFileName = filename;
return;
case FILE_ACTION_ADDED:
fwAction = Actions::Add;
break;
case FILE_ACTION_RENAMED_NEW_NAME: {
fwAction = Actions::Moved;
std::string fpath( watch->Directory + filename );
// Update the directory path
if ( watch->Recursive && FileSystem::isDirectory( fpath ) ) {
// Update the new directory path
std::string opath( watch->Directory + watch->OldFileName );
FileSystem::dirAddSlashAtEnd( opath );
FileSystem::dirAddSlashAtEnd( fpath );
for ( Watches::iterator it = mWatches.begin(); it != mWatches.end(); ++it ) {
if ( ( *it )->Watch->Directory == opath ) {
( *it )->Watch->Directory = fpath;
break;
}
}
}
std::string folderPath( static_cast<WatcherWin32*>( watch )->DirName );
std::string realFilename = filename;
std::size_t sepPos = filename.find_last_of( "/\\" );
std::string oldFolderPath =
static_cast<WatcherWin32*>( watch )->DirName +
watch->OldFileName.substr( 0, watch->OldFileName.find_last_of( "/\\" ) );
if ( sepPos != std::string::npos ) {
folderPath += filename.substr( 0, sepPos );
realFilename = filename.substr( sepPos + 1 );
}
if ( folderPath == oldFolderPath ) {
watch->Listener->handleFileAction(
watch->ID, folderPath, realFilename, fwAction,
FileSystem::fileNameFromPath( watch->OldFileName ) );
} else {
watch->Listener->handleFileAction( watch->ID,
static_cast<WatcherWin32*>( watch )->DirName,
filename, fwAction, watch->OldFileName );
}
return;
}
case FILE_ACTION_REMOVED:
fwAction = Actions::Delete;
break;
case FILE_ACTION_MODIFIED:
fwAction = Actions::Modified;
break;
default:
return;
};
std::string folderPath( static_cast<WatcherWin32*>( watch )->DirName );
std::string realFilename = filename;
std::size_t sepPos = filename.find_last_of( "/\\" );
if ( sepPos != std::string::npos ) {
folderPath += filename.substr( 0, sepPos );
realFilename = filename.substr( sepPos + 1 );
}
watch->Listener->handleFileAction( watch->ID, folderPath, realFilename, fwAction );
}
std::list<std::string> FileWatcherWin32::directories() {
std::list<std::string> dirs;
Lock lock( mWatchesLock );
for ( Watches::iterator it = mWatches.begin(); it != mWatches.end(); ++it ) {
dirs.push_back( std::string( ( *it )->Watch->DirName ) );
}
return dirs;
}
bool FileWatcherWin32::pathInWatches( const std::string& path ) {
Lock lock( mWatchesLock );
for ( Watches::iterator it = mWatches.begin(); it != mWatches.end(); ++it ) {
if ( ( *it )->Watch->DirName == path ) {
return true;
}
}
return false;
}
} // namespace efsw
#endif
|