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
|
#include <efsw/Debug.hpp>
#include <efsw/DirWatcherGeneric.hpp>
#include <efsw/FileSystem.hpp>
#include <efsw/String.hpp>
namespace efsw {
DirWatcherGeneric::DirWatcherGeneric( DirWatcherGeneric* parent, WatcherGeneric* ws,
const std::string& directory, bool recursive,
bool reportNewFiles ) :
Parent( parent ), Watch( ws ), Recursive( recursive ), Deleted( false ) {
resetDirectory( directory );
if ( !reportNewFiles ) {
DirSnap.scan();
} else {
DirectorySnapshotDiff Diff = DirSnap.scan();
if ( Diff.changed() ) {
FileInfoList::iterator it;
DiffIterator( FilesCreated ) {
handleAction( ( *it ).Filepath, Actions::Add );
}
}
}
}
DirWatcherGeneric::~DirWatcherGeneric() {
/// If the directory was deleted mark the files as deleted
if ( Deleted ) {
DirectorySnapshotDiff Diff = DirSnap.scan();
if ( !DirSnap.exists() ) {
FileInfoList::iterator it;
DiffIterator( FilesDeleted ) {
handleAction( ( *it ).Filepath, Actions::Delete );
}
DiffIterator( DirsDeleted ) {
handleAction( ( *it ).Filepath, Actions::Delete );
}
}
}
DirWatchMap::iterator it = Directories.begin();
for ( ; it != Directories.end(); ++it ) {
if ( Deleted ) {
/// If the directory was deleted, mark the flag for file deletion
it->second->Deleted = true;
}
efSAFE_DELETE( it->second );
}
}
void DirWatcherGeneric::resetDirectory( std::string directory ) {
std::string dir( directory );
/// Is this a recursive watch?
if ( Watch->Directory != directory ) {
if ( !( directory.size() &&
( directory.at( 0 ) == FileSystem::getOSSlash() ||
directory.at( directory.size() - 1 ) == FileSystem::getOSSlash() ) ) ) {
/// Get the real directory
if ( NULL != Parent ) {
std::string parentPath( Parent->DirSnap.DirectoryInfo.Filepath );
FileSystem::dirAddSlashAtEnd( parentPath );
FileSystem::dirAddSlashAtEnd( directory );
dir = parentPath + directory;
} else {
efDEBUG( "resetDirectory(): Parent is NULL. Fatal error." );
}
}
}
DirSnap.setDirectoryInfo( dir );
}
void DirWatcherGeneric::handleAction( const std::string& filename, unsigned long action,
std::string oldFilename ) {
Watch->Listener->handleFileAction( Watch->ID, DirSnap.DirectoryInfo.Filepath,
FileSystem::fileNameFromPath( filename ), (Action)action,
oldFilename );
}
void DirWatcherGeneric::addChilds( bool reportNewFiles ) {
if ( Recursive ) {
/// Create the subdirectories watchers
std::string dir;
for ( FileInfoMap::iterator it = DirSnap.Files.begin(); it != DirSnap.Files.end(); it++ ) {
if ( it->second.isDirectory() && it->second.isReadable() &&
!FileSystem::isRemoteFS( it->second.Filepath ) ) {
/// Check if the directory is a symbolic link
std::string curPath;
std::string link( FileSystem::getLinkRealPath( it->second.Filepath, curPath ) );
dir = it->first;
if ( "" != link ) {
/// Avoid adding symlinks directories if it's now enabled
if ( !Watch->WatcherImpl->mFileWatcher->followSymlinks() ) {
continue;
}
/// If it's a symlink check if the realpath exists as a watcher, or
/// if the path is outside the current dir
if ( Watch->WatcherImpl->pathInWatches( link ) ||
Watch->pathInWatches( link ) ||
!Watch->WatcherImpl->linkAllowed( curPath, link ) ) {
continue;
} else {
dir = link;
}
} else {
if ( Watch->pathInWatches( dir ) || Watch->WatcherImpl->pathInWatches( dir ) ) {
continue;
}
}
if ( reportNewFiles ) {
handleAction( dir, Actions::Add );
}
Directories[dir] =
new DirWatcherGeneric( this, Watch, dir, Recursive, reportNewFiles );
Directories[dir]->addChilds( reportNewFiles );
}
}
}
}
void DirWatcherGeneric::watch( bool reportOwnChange ) {
DirectorySnapshotDiff Diff = DirSnap.scan();
if ( reportOwnChange && Diff.DirChanged && NULL != Parent ) {
Watch->Listener->handleFileAction(
Watch->ID, FileSystem::pathRemoveFileName( DirSnap.DirectoryInfo.Filepath ),
FileSystem::fileNameFromPath( DirSnap.DirectoryInfo.Filepath ), Actions::Modified );
}
if ( Diff.changed() ) {
FileInfoList::iterator it;
MovedList::iterator mit;
/// Files
DiffIterator( FilesCreated ) {
handleAction( ( *it ).Filepath, Actions::Add );
}
DiffIterator( FilesModified ) {
handleAction( ( *it ).Filepath, Actions::Modified );
}
DiffIterator( FilesDeleted ) {
handleAction( ( *it ).Filepath, Actions::Delete );
}
DiffMovedIterator( FilesMoved ) {
handleAction( ( *mit ).second.Filepath, Actions::Moved, ( *mit ).first );
}
/// Directories
DiffIterator( DirsCreated ) {
createDirectory( ( *it ).Filepath );
}
DiffIterator( DirsModified ) {
handleAction( ( *it ).Filepath, Actions::Modified );
}
DiffIterator( DirsDeleted ) {
handleAction( ( *it ).Filepath, Actions::Delete );
removeDirectory( ( *it ).Filepath );
}
DiffMovedIterator( DirsMoved ) {
handleAction( ( *mit ).second.Filepath, Actions::Moved, ( *mit ).first );
moveDirectory( ( *mit ).first, ( *mit ).second.Filepath );
}
}
/// Process the subdirectories looking for changes
for ( DirWatchMap::iterator dit = Directories.begin(); dit != Directories.end(); ++dit ) {
/// Just watch
dit->second->watch();
}
}
void DirWatcherGeneric::watchDir( std::string& dir ) {
DirWatcherGeneric* watcher = Watch->WatcherImpl->mFileWatcher->allowOutOfScopeLinks()
? findDirWatcher( dir )
: findDirWatcherFast( dir );
if ( NULL != watcher ) {
watcher->watch( true );
}
}
DirWatcherGeneric* DirWatcherGeneric::findDirWatcherFast( std::string dir ) {
// remove the common base ( dir should always start with the same base as the watcher )
efASSERT( !dir.empty() );
efASSERT( dir.size() >= DirSnap.DirectoryInfo.Filepath.size() );
efASSERT( DirSnap.DirectoryInfo.Filepath ==
dir.substr( 0, DirSnap.DirectoryInfo.Filepath.size() ) );
if ( dir.size() >= DirSnap.DirectoryInfo.Filepath.size() ) {
dir = dir.substr( DirSnap.DirectoryInfo.Filepath.size() - 1 );
}
if ( dir.size() == 1 ) {
efASSERT( dir[0] == FileSystem::getOSSlash() );
return this;
}
size_t level = 0;
std::vector<std::string> dirv = String::split( dir, FileSystem::getOSSlash(), false );
DirWatcherGeneric* watcher = this;
while ( level < dirv.size() ) {
// search the dir level in the current watcher
DirWatchMap::iterator it = watcher->Directories.find( dirv[level] );
// found? continue with the next level
if ( it != watcher->Directories.end() ) {
watcher = it->second;
level++;
} else {
// couldn't found the folder level?
// directory not watched
return NULL;
}
}
return watcher;
}
DirWatcherGeneric* DirWatcherGeneric::findDirWatcher( std::string dir ) {
if ( DirSnap.DirectoryInfo.Filepath == dir ) {
return this;
} else {
DirWatcherGeneric* watcher = NULL;
for ( DirWatchMap::iterator it = Directories.begin(); it != Directories.end(); ++it ) {
watcher = it->second->findDirWatcher( dir );
if ( NULL != watcher ) {
return watcher;
}
}
}
return NULL;
}
DirWatcherGeneric* DirWatcherGeneric::createDirectory( std::string newdir ) {
FileSystem::dirRemoveSlashAtEnd( newdir );
newdir = FileSystem::fileNameFromPath( newdir );
DirWatcherGeneric* dw = NULL;
/// Check if the directory is a symbolic link
std::string parentPath( DirSnap.DirectoryInfo.Filepath );
FileSystem::dirAddSlashAtEnd( parentPath );
std::string dir( parentPath + newdir );
FileSystem::dirAddSlashAtEnd( dir );
FileInfo fi( dir );
if ( !fi.isDirectory() || !fi.isReadable() || FileSystem::isRemoteFS( dir ) ) {
return NULL;
}
std::string curPath;
std::string link( FileSystem::getLinkRealPath( dir, curPath ) );
bool skip = false;
if ( "" != link ) {
/// Avoid adding symlinks directories if it's now enabled
if ( !Watch->WatcherImpl->mFileWatcher->followSymlinks() ) {
skip = true;
}
/// If it's a symlink check if the realpath exists as a watcher, or
/// if the path is outside the current dir
if ( Watch->WatcherImpl->pathInWatches( link ) || Watch->pathInWatches( link ) ||
!Watch->WatcherImpl->linkAllowed( curPath, link ) ) {
skip = true;
} else {
dir = link;
}
} else {
if ( Watch->pathInWatches( dir ) || Watch->WatcherImpl->pathInWatches( dir ) ) {
skip = true;
}
}
if ( !skip ) {
handleAction( newdir, Actions::Add );
/// Creates the new directory watcher of the subfolder and check for new files
dw = new DirWatcherGeneric( this, Watch, dir, Recursive );
dw->addChilds();
dw->watch();
/// Add it to the list of directories
Directories[newdir] = dw;
}
return dw;
}
void DirWatcherGeneric::removeDirectory( std::string dir ) {
FileSystem::dirRemoveSlashAtEnd( dir );
dir = FileSystem::fileNameFromPath( dir );
DirWatcherGeneric* dw = NULL;
DirWatchMap::iterator dit;
/// Folder deleted
/// Search the folder, it should exists
dit = Directories.find( dir );
if ( dit != Directories.end() ) {
dw = dit->second;
/// Flag it as deleted so it fire the event for every file inside deleted
dw->Deleted = true;
/// Delete the DirWatcherGeneric
efSAFE_DELETE( dw );
/// Remove the directory from the map
Directories.erase( dit->first );
}
}
void DirWatcherGeneric::moveDirectory( std::string oldDir, std::string newDir ) {
FileSystem::dirRemoveSlashAtEnd( oldDir );
oldDir = FileSystem::fileNameFromPath( oldDir );
FileSystem::dirRemoveSlashAtEnd( newDir );
newDir = FileSystem::fileNameFromPath( newDir );
DirWatcherGeneric* dw = NULL;
DirWatchMap::iterator dit;
/// Directory existed?
dit = Directories.find( oldDir );
if ( dit != Directories.end() ) {
dw = dit->second;
/// Remove the directory from the map
Directories.erase( dit->first );
Directories[newDir] = dw;
dw->resetDirectory( newDir );
}
}
bool DirWatcherGeneric::pathInWatches( std::string path ) {
if ( DirSnap.DirectoryInfo.Filepath == path ) {
return true;
}
for ( DirWatchMap::iterator it = Directories.begin(); it != Directories.end(); ++it ) {
if ( it->second->pathInWatches( path ) ) {
return true;
}
}
return false;
}
} // namespace efsw
|