-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
FileSystemEventHandlers.cs
584 lines (512 loc) · 30.2 KB
/
FileSystemEventHandlers.cs
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
//
// Copyright (c) Roland Pihlakas 2019 - 2023
//
// Roland Pihlakas licenses this file to you under the GNU Lesser General Public License, ver 2.1.
// See the LICENSE file for more information.
//
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using myoddweb.directorywatcher.interfaces;
namespace FolderSync
{
internal class WatcherContext
{
public readonly DummyFileSystemEvent Event;
public readonly CancellationToken Token;
public readonly bool ForHistory;
public readonly bool IsSrcPath;
public readonly bool IsInitialScan;
public CachedFileInfo FileInfo;
public BoolRef FileInfoRefreshed; //NB! need bool ref to share fileinfo refresh status between mirror and history contexts
public CachedFileInfo OtherFileInfo;
public DateTime Time
{
[DebuggerStepThrough]
get
{
return Event?.DateTimeUtc ?? DateTime.UtcNow;
}
}
[DebuggerStepThrough]
#pragma warning disable CA1068 //should take CancellationToken as the last parameter
public WatcherContext(DummyFileSystemEvent eventObj, CancellationToken token, bool forHistory, bool isSrcPath, bool isInitialScan, BoolRef fileInfoRefreshedBoolRef)
#pragma warning restore CA1068
{
Event = eventObj;
Token = token;
ForHistory = forHistory;
IsSrcPath = isSrcPath;
IsInitialScan = isInitialScan;
FileInfo = eventObj?.FileSystemInfo != null ? new CachedFileInfo(eventObj?.FileSystemInfo) : null;
FileInfoRefreshed = fileInfoRefreshedBoolRef ?? new BoolRef();
//FileInfo type is a file from directory scan and has stale file length.
//NB! if FileInfo is null then it is okay to set FileInfoRefreshed = true since if will be populated later with up-to-date information
FileInfoRefreshed.Value = !(eventObj?.FileSystemInfo is FileInfo);
}
}
internal partial class ConsoleWatch
{
private static readonly AsyncLockQueueDictionary<string> FileEventLocks = new AsyncLockQueueDictionary<string>();
#pragma warning disable S1118 //Warning S1118 Hide this public constructor by making it 'protected'.
public ConsoleWatch(IWatcher3 watch)
#pragma warning restore S1118
{
//_consoleColor = Console.ForegroundColor;
//watch.OnErrorAsync += OnErrorAsync;
watch.OnAddedAsync += (fse, token) => OnAddedAsync(new DummyFileSystemEvent(fse), token, isInitialScan: false);
watch.OnRemovedAsync += (fse, token) => OnRemovedAsync(new DummyFileSystemEvent(fse), token, isInitialScan: false);
watch.OnRenamedAsync += (fse, token) => OnRenamedAsync(fse, token);
watch.OnTouchedAsync += (fse, token) => OnTouchedAsync(new DummyFileSystemEvent(fse), token, isInitialScan: false);
}
#if false
private async Task OnErrorAsync(IEventError ee, CancellationToken token)
{
try
{
await AddMessage(ConsoleColor.Red, $"[!]:{ee.Message}", context);
}
catch (Exception ex)
{
await WriteException(ex, context);
}
}
#endif
public static async Task FileUpdated(WatcherContext context, bool isWatchedFileCheckDone)
{
if (
(
isWatchedFileCheckDone
|| IsWatchedFile(context.Event.FullName, context.ForHistory, context.IsSrcPath)
)
&& (await NeedsUpdate(context)) //NB!
)
{
var otherFullName = await GetOtherFullName(context);
using (await Global.FileOperationLocks.LockAsync(context.Event.FullName.ToUpperInvariantOnWindows(), otherFullName.ToUpperInvariantOnWindows(), context.Token))
{
using (await Global.FileOperationSemaphore.LockAsync())
{
long maxFileSize = Math.Min(FileExtensions.MaxByteArraySize, Global.MaxFileSizeMB * (1024 * 1024));
//TODO: consider destination disk free space here together with the file size already before reading the file
Tuple<byte[], long> fileDataTuple = null;
try
{
#if false //TODO
if (context.ForHistory)
{
//open the original file. Need to open using FileInfo so that if the source contains multiple files by same name (like in Google Drive) then the correct file is opened
fileDataTuple = await FileExtensions.ReadAllBytesAsync
(
//Extensions.GetLongPath(context.Event.FullName),
context.Event.FileSystemInfo as FileInfo,
/*allowVSS: */Global.AllowVSS,
context.Token,
maxFileSize,
retryCount: Global.RetryCountOnSrcFileOpenError,
readBufferKB: Global.ReadBufferKB,
bufferReadDelayMs: Global.BufferReadDelayMs
);
}
else
#endif
{ //TODO: cache length of this file read
//open file by filename, so that if the source contains multiple files by same name (like in Google Drive) then same file is opened always. This avoid unnecessary overwrites in case these duplicate files in cloud drive have different sizes
fileDataTuple = await FileExtensions.ReadAllBytesAsync
(
Extensions.GetLongPath(context.Event.FullName),
/*allowVSS: */Global.AllowVSS,
context.Token,
maxFileSize,
retryCount: Global.RetryCountOnSrcFileOpenError,
readBufferKB: Global.ReadBufferKB,
bufferReadDelayMs: Global.BufferReadDelayMs
);
}
if (fileDataTuple.Item1 == null) //maximum length exceeded
{
if (fileDataTuple.Item2 >= 0)
await AddMessage(ConsoleColor.Red, $"Error synchronising updates from file {context.Event.FullName} : fileLength > maxFileSize : {fileDataTuple.Item2} > {maxFileSize}", context);
else
await AddMessage(ConsoleColor.Red, $"Error synchronising updates from file {context.Event.FullName} : unable to read file content", context);
return; //TODO: log error?
}
//recompare file size so that if the source contains multiple files by same name (like in Google Drive) then the actually opened file's size is considered. This avoid unnecessary overwrites in case these duplicate files in cloud drive have different sizes
if (context.FileInfo.Length != fileDataTuple.Item1.Length)
{
var dirlistFileLen = context.FileInfo.Length;
context.FileInfo.Length = fileDataTuple.Item1.Length;
bool needsUpdate = await NeedsUpdate(context); //recheck the need to update based on updated file size information
if (!needsUpdate)
{
await AddMessage(ConsoleColor.Red, $"Skipping file {context.Event.FullName} : file length in dirlist != file content length. Content length matches target file length : {dirlistFileLen} (dirlist) != {context.FileInfo.Length} (content)", context);
return;
}
}
}
catch (FileNotFoundException) //file was removed by the time queue processing got to it
{
return;
}
//save without transformations
await ConsoleWatch.SaveFileModifications(fileDataTuple.Item1, context);
}
}
}
}
private static async Task FileDeleted(WatcherContext context, bool isWatchedFileCheckDone)
{
if (
isWatchedFileCheckDone
|| IsWatchedFile(context.Event.FullName, context.ForHistory, context.IsSrcPath)
)
{
await RefreshFileInfo(context); //NB! verify that the file is still deleted
if (!await GetFileExists(context)) //NB! verify that the file is still deleted
{
var otherFullName = await GetOtherFullName(context);
var otherFileInfo = new FileInfoRef(null, context.Token);
await DeleteFile(otherFileInfo, otherFullName, context);
}
else //NB! file appears to be recreated
{
//await FileUpdated(fullName, isSrcPath, context);
}
}
}
#pragma warning disable AsyncFixer01
private static async Task OnRenamedAsync(IRenamedFileSystemEvent fse, CancellationToken token)
{
try
{
//NB! create separate context to properly handle disk free space checks on cases where file is renamed from src path to dest path (not a recommended practice though!)
var prevFileFSE = new DummyFileSystemEvent(fse.PreviousFileSystemInfo);
var newFileFSE = new DummyFileSystemEvent(fse);
var previousFullNameInvariant = prevFileFSE.FullName.ToUpperInvariantOnWindows();
bool previousPathIsSrcPath = IsSrcPath(previousFullNameInvariant);
var previousContext = new WatcherContext(prevFileFSE, token, forHistory: false, isSrcPath: previousPathIsSrcPath, isInitialScan: false, fileInfoRefreshedBoolRef: null);
var newFullNameInvariant = newFileFSE.FileSystemInfo.FullName.ToUpperInvariantOnWindows();
bool newPathIsSrcPath = IsSrcPath(newFullNameInvariant);
var fileInfoRefreshedBoolRef = new BoolRef();
var newContexts = new WatcherContext[] {
(
IsWatchedFile(prevFileFSE.FullName, forHistory: false, newPathIsSrcPath)
|| IsWatchedFile(newFileFSE.FullName, forHistory: false, newPathIsSrcPath)
)
? new WatcherContext(newFileFSE, token, forHistory: false, isSrcPath: newPathIsSrcPath, isInitialScan: false, fileInfoRefreshedBoolRef: fileInfoRefreshedBoolRef)
: null,
(
IsWatchedFile(prevFileFSE.FullName, forHistory: true, newPathIsSrcPath)
|| IsWatchedFile(newFileFSE.FullName, forHistory: true, newPathIsSrcPath)
)
? new WatcherContext(newFileFSE, token, forHistory: true, isSrcPath: newPathIsSrcPath, isInitialScan: false, fileInfoRefreshedBoolRef: fileInfoRefreshedBoolRef)
: null
};
foreach (var newContext in newContexts)
{
if (newContext == null) //not a watched file
continue;
if (newContext.ForHistory && !newContext.IsSrcPath)
continue;
try
{
if (newFileFSE.IsFile)
{
var prevFileIsWatchedFile = IsWatchedFile(prevFileFSE.FileSystemInfo.FullName, newContext.ForHistory, newPathIsSrcPath);
var newFileIsWatchedFile = IsWatchedFile(newFileFSE.FileSystemInfo.FullName, newContext.ForHistory, newPathIsSrcPath);
/*
if (prevFileIsWatchedFile
|| newFileIsWatchedFile)
*/
{
await AddMessage(ConsoleColor.Cyan, $"[{(newFileFSE.IsFile ? "F" : "D")}][R]:{prevFileFSE.FullName} > {newFileFSE.FileSystemInfo.FullName}", newContext);
//NB! if file is renamed to cs~ or resx~ then that means there will be yet another write to same file, so lets skip this event here - NB! skip the event here, including delete event of the previous file
if (!newFileFSE.FileSystemInfo.FullName.EndsWith("~"))
{
//using (await Global.FileOperationLocks.LockAsync(rfse.FileSystemInfo.FullName, rfse.PreviousFileSystemInfo.FullName, context.Token)) //comment-out: prevent deadlock
{
if (newFileIsWatchedFile)
{
using (await FileEventLocks.LockAsync(newFileFSE.FileSystemInfo.FullName, token))
{
await FileUpdated(newContext, isWatchedFileCheckDone: false);
}
}
if (prevFileIsWatchedFile)
{
if (
!newContext.ForHistory //history files have a different name format than the original file names and would not cause a undefined behaviour
&& newFileIsWatchedFile //both files were watched files
&& previousContext.IsSrcPath != newContext.IsSrcPath
&&
(
Global.BidirectionalMirror //move in either direction between src and mirrorDest
|| previousContext.IsSrcPath //src -> mirrorDest move
)
)
{
//the file was moved from one watched path to another watched path, which is illegal, lets ignore the file move
await AddMessage(ConsoleColor.Red, $"Ignoring file delete in the source path since the move was to the other managed path : {prevFileFSE.FullName} > {newFileFSE.FileSystemInfo.FullName}", previousContext);
}
else
{
//NB! invalidate file cache even if the src deletions are not mirrored
var otherFullName = await GetOtherFullName(previousContext);
CachedFileInfo dummy;
var otherLongPath = Extensions.GetLongPath(otherFullName);
Global.DestAndHistoryFileInfoCache.TryRemove(otherLongPath.ToUpperInvariantOnWindows(), out dummy);
await InvalidateFileDataInPersistentCache(previousContext);
if (
newContext.ForHistory
|| (previousContext.IsSrcPath && Global.MirrorIgnoreSrcDeletions)
|| (!previousContext.IsSrcPath && Global.MirrorIgnoreDestDeletions)
)
{
continue;
}
else
{
using (await FileEventLocks.LockAsync(prevFileFSE.FileSystemInfo.FullName, token))
{
await FileDeleted(previousContext, isWatchedFileCheckDone: false);
}
}
}
} //if (prevFileIsWatchedFile)
}
} //if (!fse.FileSystemInfo.FullName.EndsWith("~"))
}
}
else
{
await AddMessage(ConsoleColor.Cyan, $"[{(newFileFSE.IsFile ? "F" : "D")}][R]:{prevFileFSE.FullName} > {newFileFSE.FileSystemInfo.FullName}", newContext);
//TODO trigger update / delete event for all files in new folder
}
}
catch (Exception ex)
{
await WriteException(ex, newContext);
}
}
}
catch (Exception ex) when (
/*ex.GetInnermostException() is IOException //this includes DriveNotFoundException
|| ex.GetInnermostException() is TimeoutException
|| */ex.GetInnermostException() is UnauthorizedAccessException //sometimes accessing the source file during new WatcherContext() constructor also causes access errors
)
{
await AddMessage(ConsoleColor.Red, $"Error synchronising updates from file rename {fse.PreviousFileSystemInfo.FullName} > {fse.FileSystemInfo.FullName} : unable to access file metadata", DateTime.Now, token: token);
}
catch (Exception ex)
{
await WriteException(ex);
}
} //private static async Task OnRenamedAsync(IRenamedFileSystemEvent fse, CancellationToken token)
internal static async Task OnRemovedAsync(DummyFileSystemEvent fse, CancellationToken token, bool isInitialScan)
{
try
{
var fullNameInvariant = fse.FileSystemInfo.FullName.ToUpperInvariantOnWindows();
bool isSrcPath = IsSrcPath(fullNameInvariant);
var fileInfoRefreshedBoolRef = new BoolRef();
var contexts = new WatcherContext[] {
//TODO: do not fail synchronisation in case of access permission error after delete operation since we actually do not need access to sources file?
IsWatchedFile(fse.FullName, forHistory: false, isSrcPath)
? new WatcherContext(fse, token, forHistory: false, isSrcPath: isSrcPath, isInitialScan: isInitialScan, fileInfoRefreshedBoolRef: fileInfoRefreshedBoolRef)
: null,
IsWatchedFile(fse.FullName, forHistory: true, isSrcPath)
? new WatcherContext(fse, token, forHistory: true, isSrcPath: isSrcPath, isInitialScan: isInitialScan, fileInfoRefreshedBoolRef: fileInfoRefreshedBoolRef)
: null
};
foreach (var context in contexts)
{
if (context == null) //not a watched file
continue;
if (context.ForHistory && !context.IsSrcPath)
continue;
context.FileInfo.Exists = false;
try
{
if (fse.IsFile)
{
//if (IsWatchedFile(fse.FileSystemInfo.FullName, context.ForHistory, isSrcPath))
{
//NB! invalidate file cache even if the src deletions are not mirrored
var otherFullName = await GetOtherFullName(context);
CachedFileInfo dummy;
var otherLongPath = Extensions.GetLongPath(otherFullName);
Global.DestAndHistoryFileInfoCache.TryRemove(otherLongPath.ToUpperInvariantOnWindows(), out dummy);
await InvalidateFileDataInPersistentCache(context);
if (
context.ForHistory
|| (context.IsSrcPath && Global.MirrorIgnoreSrcDeletions)
|| (!context.IsSrcPath && Global.MirrorIgnoreDestDeletions)
)
{
continue;
}
else
{
await AddMessage(ConsoleColor.Yellow, $"[{(fse.IsFile ? "F" : "D")}][-]:{fse.FileSystemInfo.FullName}", context);
using (await FileEventLocks.LockAsync(fse.FileSystemInfo.FullName, token))
{
await FileDeleted(context, isWatchedFileCheckDone: true);
}
}
}
}
else //if (fse.IsFile)
{
//nothing to do here: the files are likely already deleted by now
}
}
catch (Exception ex)
{
await WriteException(ex, context);
}
}
}
catch (Exception ex) when (
/*ex.GetInnermostException() is IOException //this includes DriveNotFoundException
|| ex.GetInnermostException() is TimeoutException
|| */ex.GetInnermostException() is UnauthorizedAccessException //sometimes accessing the source file during new WatcherContext() constructor also causes access errors
)
{
await AddMessage(ConsoleColor.Red, $"Error synchronising updates from file removal {fse.FileSystemInfo.FullName} : unable to access file metadata", DateTime.Now, token: token);
}
catch (Exception ex)
{
await WriteException(ex);
}
} //internal static async Task OnRemovedAsync(IFileSystemEvent fse, CancellationToken token)
internal static async Task OnAddedAsync(DummyFileSystemEvent fse, CancellationToken token, bool isInitialScan)
{
try
{
var fullNameInvariant = fse.FileSystemInfo.FullName.ToUpperInvariantOnWindows();
bool isSrcPath = IsSrcPath(fullNameInvariant);
var fileInfoRefreshedBoolRef = new BoolRef();
var contexts = new WatcherContext[] {
IsWatchedFile(fse.FullName, forHistory: false, isSrcPath)
? new WatcherContext(fse, token, forHistory: false, isSrcPath: isSrcPath, isInitialScan: isInitialScan, fileInfoRefreshedBoolRef: fileInfoRefreshedBoolRef)
: null,
IsWatchedFile(fse.FullName, forHistory: true, isSrcPath)
? new WatcherContext(fse, token, forHistory: true, isSrcPath: isSrcPath, isInitialScan: isInitialScan, fileInfoRefreshedBoolRef: fileInfoRefreshedBoolRef)
: null
};
foreach (var context in contexts)
{
if (context == null) //not a watched file
continue;
if (context.ForHistory && !context.IsSrcPath)
continue;
context.FileInfo.Exists = true;
try
{
if (fse.IsFile)
{
//if (IsWatchedFile(fse.FileSystemInfo.FullName, context.ForHistory, isSrcPath))
{
if (!context.IsInitialScan)
await AddMessage(ConsoleColor.Green, $"[{(fse.IsFile ? "F" : "D")}][+]:{fse.FileSystemInfo.FullName}", context);
using (await FileEventLocks.LockAsync(fse.FileSystemInfo.FullName, token))
{
await FileUpdated(context, isWatchedFileCheckDone: true);
}
}
}
else
{
//nothing to do here: there are likely no files in here yet
}
}
catch (Exception ex)
{
await WriteException(ex, context);
}
}
}
catch (Exception ex) when (
/*ex.GetInnermostException() is IOException //this includes DriveNotFoundException
|| ex.GetInnermostException() is TimeoutException
|| */ex.GetInnermostException() is UnauthorizedAccessException //sometimes accessing the source file during new WatcherContext() constructor also causes access errors
)
{
await AddMessage(ConsoleColor.Red, $"Error synchronising updates from file add {fse.FileSystemInfo.FullName} : unable to access file metadata", DateTime.Now, token: token);
}
catch (Exception ex)
{
await WriteException(ex);
}
} //internal static async Task OnAddedAsync(IFileSystemEvent fse, CancellationToken token, bool isInitialScan)
internal static async Task OnTouchedAsync(DummyFileSystemEvent fse, CancellationToken token, bool isInitialScan)
{
try
{
var fullNameInvariant = fse.FileSystemInfo.FullName.ToUpperInvariantOnWindows();
bool isSrcPath = IsSrcPath(fullNameInvariant);
var fileInfoRefreshedBoolRef = new BoolRef();
var contexts = new WatcherContext[] {
IsWatchedFile(fse.FullName, forHistory: false, isSrcPath)
? new WatcherContext(fse, token, forHistory: false, isSrcPath: isSrcPath, isInitialScan: isInitialScan, fileInfoRefreshedBoolRef: fileInfoRefreshedBoolRef)
: null,
IsWatchedFile(fse.FullName, forHistory: true, isSrcPath)
? new WatcherContext(fse, token, forHistory: true, isSrcPath: isSrcPath, isInitialScan: isInitialScan, fileInfoRefreshedBoolRef: fileInfoRefreshedBoolRef)
: null
};
foreach (var context in contexts)
{
if (context == null) //not a watched file
continue;
if (context.ForHistory && !context.IsSrcPath)
continue;
context.FileInfo.Exists = true;
try
{
if (fse.IsFile)
{
//if (IsWatchedFile(fse.FileSystemInfo.FullName, context.ForHistory, isSrcPath))
{
//check for file type only after checking IsWatchedFile first since file type checking might already be a slow operation
if (await GetIsFile(context)) //for some reason fse.IsFile is set even for folders
{
await AddMessage(ConsoleColor.White, $"[{(fse.IsFile ? "F" : "D")}][T]:{fse.FileSystemInfo.FullName}", context);
using (await FileEventLocks.LockAsync(fse.FileSystemInfo.FullName, token))
{
await FileUpdated(context, isWatchedFileCheckDone: true);
}
}
}
}
else
{
//nothing to do here: file update events are sent separately anyway
}
}
catch (Exception ex)
{
await WriteException(ex, context);
}
}
}
catch (Exception ex) when (
/*ex.GetInnermostException() is IOException //this includes DriveNotFoundException
|| ex.GetInnermostException() is TimeoutException
|| */ex.GetInnermostException() is UnauthorizedAccessException //sometimes accessing the source file during new WatcherContext() constructor also causes access errors
)
{
await AddMessage(ConsoleColor.Red, $"Error synchronising updates from file touch {fse.FileSystemInfo.FullName} : unable to access file metadata", DateTime.Now, token: token);
}
catch (Exception ex)
{
await WriteException(ex);
}
} //internal static async Task OnTouchedAsync(IFileSystemEvent fse, CancellationToken token)
}
}