-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Program.cs
1107 lines (940 loc) · 47.7 KB
/
Program.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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) Roland Pihlakas 2019 - 2023
// roland@simplify.ee
//
// 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.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Dasync.Collections;
using Microsoft.Extensions.Configuration;
using Microsoft.Win32;
using myoddweb.directorywatcher;
using myoddweb.directorywatcher.interfaces;
using Nito.AspNetBackgroundTasks;
using Nito.AsyncEx;
using NReco.Text;
namespace FolderSync
{
internal partial class Program
{
//let null char mark start and end of a filename
//https://stackoverflow.com/questions/54205087/how-can-i-create-a-file-with-null-bytes-in-the-filename
//https://stackoverflow.com/questions/1976007/what-characters-are-forbidden-in-windows-and-linux-directory-names
//https://serverfault.com/questions/242110/which-common-characters-are-illegal-in-unix-and-windows-filesystems
public static readonly string NullChar = new string(new char[]{ (char)0 });
public static readonly string DirectorySeparatorChar = new string(new char[] { Path.DirectorySeparatorChar });
private static readonly AsyncManualResetEvent ExitEvent = new AsyncManualResetEvent(false);
private static byte[] GetHash(string inputString)
{
#pragma warning disable SCS0006 //Warning SCS0006 Weak hashing function
HashAlgorithm algorithm = MD5.Create();
#pragma warning restore SCS0006
return algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString));
}
public static string GetHashString(string inputString)
{
StringBuilder sb = new StringBuilder();
foreach (byte b in GetHash(inputString))
sb.Append(b.ToString("X2"));
return sb.ToString();
}
private static void Main()
{
ReadConfig();
var pathHashes = "";
//TODO!!! allow multiple instances with differet settings
pathHashes += "_" + GetHashString(Global.SrcPath);
pathHashes += "_" + GetHashString(Global.MirrorDestPath ?? "");
pathHashes += "_" + GetHashString(Global.HistoryDestPath ?? "");
//NB! prevent multiple instances from starting on same directories
using (var mutex = new Mutex(false, "Global\\" + Assembly.GetExecutingAssembly().GetName().Name + "_" + pathHashes))
{
try
{
if (!mutex.WaitOne(0, false))
{
Console.WriteLine("Instance already running");
return;
}
}
catch (AbandonedMutexException) //The wait completed due to an abandoned mutex. - happens when the other process was killed
{
//ignore it
}
MainTask().Wait();
}
}
private static async Task MainTask()
{
try
{
//Console.WriteLine(Environment.Is64BitProcess ? "x64 version" : "x86 version");
Console.WriteLine("Press Ctrl+C to stop the monitors.");
if (Global.UseIdlePriority)
{
try
{
var CurrentProcess = Process.GetCurrentProcess();
CurrentProcess.PriorityClass = ProcessPriorityClass.Idle;
CurrentProcess.PriorityBoostEnabled = false;
if (ConfigParser.IsWindows)
{
WindowsDllImport.SetIOPriority(CurrentProcess.Handle, WindowsDllImport.PROCESSIOPRIORITY.PROCESSIOPRIORITY_VERY_LOW);
}
}
catch (Exception)
{
Console.WriteLine("Unable to set idle priority.");
}
}
if (Global.UseBackgroundMode)
{
try
{
var CurrentProcess = Process.GetCurrentProcess();
if (ConfigParser.IsWindows)
{
WindowsDllImport.SetPriorityClass(CurrentProcess.Handle, WindowsDllImport.PROCESS_MODE_BACKGROUND_BEGIN);
}
}
catch (Exception)
{
Console.WriteLine("Unable to set background mode.");
}
}
if (Global.Affinity.Count > 0)
{
try
{
var CurrentProcess = Process.GetCurrentProcess();
long affinityMask = 0;
foreach (var affinityEntry in Global.Affinity)
{
if (affinityEntry < 0 || affinityEntry > 63)
throw new ArgumentException("Affinity");
affinityMask |= (long)1 << (int)affinityEntry;
}
CurrentProcess.ProcessorAffinity = new IntPtr(affinityMask);
}
catch (Exception)
{
Console.WriteLine("Unable to set affinity.");
}
}
ThreadPool.SetMinThreads(32, 4096); //TODO: config
//TODO: MaxThreads setting
TaskScheduler.UnobservedTaskException += UnobservedTaskExceptionHandler;
#if DEBUG || true
//Declutter Visual Studio debug output panel.
//Thanks to this piece of code you do not need Visual Studio's built-in exception reporting in Output panel anymore and can turn it off (right-click on the panel and remove checkbox from "Exception Messages").
//This code will report exceptions on its own and does some filtering, so it does not report all exceptions.
AppDomain.CurrentDomain.FirstChanceException += FirstChanceExceptionHandler;
#endif
//start the monitor.
using (var watch = new Watcher())
{
watch.Add(new Request(Extensions.GetLongPath(Global.SrcPath), recursive: true));
if (Global.BidirectionalMirror)
{
watch.Add(new Request(Extensions.GetLongPath(Global.MirrorDestPath), recursive: true));
}
//prepare the console watcher so we can output pretty messages.
var consoleWatch = new ConsoleWatch(watch);
//start watching
//NB! start watching before synchronisation
watch.Start();
var initialSyncMessageContext = new WatcherContext
(
eventObj: null,
token: Global.CancellationToken.Token,
forHistory: false, //unused here
isSrcPath: false, //unused here
isInitialScan: true,
fileInfoRefreshedBoolRef: null
);
BackgroundTaskManager.Run(async () =>
{
await ConsoleWatch.AddMessage(ConsoleColor.White, "Doing initial synchronisation...", initialSyncMessageContext);
await ScanFolders(initialSyncMessageContext: initialSyncMessageContext);
BackgroundTaskManager.Run(async () =>
{
await InitialSyncCountdownEvent.WaitAsync(Global.CancellationToken.Token);
//if (!Global.CancellationToken.IsCancellationRequested)
await ConsoleWatch.AddMessage(ConsoleColor.White, "Done initial synchronisation...", initialSyncMessageContext);
});
if (Global.UsePolling)
{
while (!Global.CancellationToken.IsCancellationRequested)
{
#if !NOASYNC
await Task.Delay(Global.PollingDelay * 1000, Global.CancellationToken.Token);
#else
Global.CancellationToken.Token.WaitHandle.WaitOne(Global.PollingDelay * 1000);
#endif
await ScanFolders(initialSyncMessageContext: null);
}
} //if (Global.UsePolling)
}); //BackgroundTaskManager.Run(async () =>
//listen for the Ctrl+C
await WaitForCtrlC(watch);
Console.WriteLine("Stopping...");
//stop everything.
//watch.Stop(); //comment-out: lets not wait for the watch to stop, since it tends to hang during stop
GC.KeepAlive(consoleWatch);
}
}
catch (Exception ex)
{
await ConsoleWatch.WriteException(ex);
}
finally
{
Console.WriteLine("Exiting...");
Environment.Exit(0);
}
} //private static async Task MainTask()
private static void UnobservedTaskExceptionHandler(object sender, UnobservedTaskExceptionEventArgs e)
{
e.SetObserved();
try
{
Task.Run(async () => ConsoleWatch.WriteException(e.Exception)).Wait();
}
catch (Exception ex)
{
//ignore it
bool qqq = true;
}
}
private static void FirstChanceExceptionHandler(object source, FirstChanceExceptionEventArgs e)
{
//Console.WriteLine("FirstChanceException event raised in {0}: {1}", AppDomain.CurrentDomain.FriendlyName, e.Exception.Message);
try
{
var innerException = e.Exception.GetInnermostException();
if (!(
innerException is TaskCanceledException
//|| innerException is DirectoryNotFoundException
|| innerException is IOException
|| innerException is UnauthorizedAccessException
))
{
Debug.WriteLine($"Exception thrown: '{e.Exception.GetType().FullName}' in {e.Exception.Source} : {e.Exception.ToString()}");
//Task.Run(async () => ConsoleWatch.WriteException(e.Exception));
}
}
catch (Exception ex)
{
//ignore it
bool qqq = true;
}
}
private static Task WaitForCtrlC(Watcher watch)
{
//handle hibernation. Need to stop filesystem monitoring while system is suspended, else the process might start hogging the cpu after system resumes for some reason
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler((sender, e) => OnPowerModeChanged(sender, e, watch));
Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
//need separate handlers for system exit since else this process will block system reboot
//see http://stackoverflow.com/questions/529867/does-application-applicationexit-event-work-to-be-notified-of-exit-in-non-winform
//NB! TODO: this does not catch windows shutdown
Application.ThreadExit += new EventHandler(OnAppMainThreadExit);
Application.ApplicationExit += new EventHandler(OnAppMainThreadExit);
AppDomain.CurrentDomain.DomainUnload += new EventHandler(OnAppMainThreadExit);
AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnAppMainThreadExit);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnAppMainThreadExitUH);
SystemEvents.SessionEnding += new SessionEndingEventHandler(OnSessionEnding);
return ExitEvent.WaitAsync();
}
private static void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e, Watcher watch)
{
if (e.Mode == PowerModes.Resume)
watch.Start();
else if (e.Mode == PowerModes.Suspend)
watch.Stop();
}
private static void SetExitEvent()
{
Global.CancellationToken.Cancel();
//e.Cancel = true;
Console.WriteLine("Stop detected.");
ExitEvent.Set();
}
private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
e.Cancel = true;
SetExitEvent();
}
private static void OnAppMainThreadExit(object sender, EventArgs e)
{
SetExitEvent();
}
private static void OnAppMainThreadExitUH(object sender, UnhandledExceptionEventArgs e)
{
SetExitEvent();
}
private static void OnSessionEnding(object sender, SessionEndingEventArgs e)
{
SetExitEvent();
}
}
internal class BoolRef
{
public bool Value;
}
internal partial class ConsoleWatch
{
private static readonly ConcurrentDictionary<string, DateTime> BidirectionalSynchroniserSavedFileDates = new ConcurrentDictionary<string, DateTime>();
public static bool IsSrcPath(string fullNameInvariant)
{
return Extensions.GetLongPath(fullNameInvariant)
//.ToUpperInvariantOnWindows() //if the path contains ~ character then Path.GetFullPath() changes the path character case back to original (lower) case
.StartsWith(Extensions.GetLongPath(Global.SrcPath));
}
public static bool IsMirrorDestPath(string fullNameInvariant)
{
return Extensions.GetLongPath(fullNameInvariant)
//.ToUpperInvariantOnWindows() //if the path contains ~ character then Path.GetFullPath() changes the path character case back to original (lower) case
.StartsWith(Extensions.GetLongPath(Global.MirrorDestPath));
}
public static bool IsHistoryDestPath(string fullNameInvariant)
{
return Extensions.GetLongPath(fullNameInvariant)
//.ToUpperInvariantOnWindows() //if the path contains ~ character then Path.GetFullPath() changes the path character case back to original (lower) case
.StartsWith(Extensions.GetLongPath(Global.HistoryDestPath));
}
public static string GetNonFullName(string fullName)
{
fullName = Extensions.GetLongPath(fullName);
var fullNameInvariant = fullName.ToUpperInvariantOnWindows();
if (IsHistoryDestPath(fullNameInvariant))
{
return fullName.Substring(Extensions.GetLongPath(Global.HistoryDestPath).Length);
}
else if (IsMirrorDestPath(fullNameInvariant))
{
return fullName.Substring(Extensions.GetLongPath(Global.MirrorDestPath).Length);
}
else if (IsSrcPath(fullNameInvariant))
{
return fullName.Substring(Extensions.GetLongPath(Global.SrcPath).Length);
}
else
{
throw new ArgumentException("Unexpected path provided to GetNonFullName()");
}
}
public static string GetCacheDirName(string dirFullName, bool forHistory)
{
var fullNameInvariant = dirFullName.ToUpperInvariantOnWindows();
var nonFullNameFolder = GetNonFullName(dirFullName);
if (forHistory)
{
if (IsHistoryDestPath(fullNameInvariant))
{
return FolderSyncNetSource.Path.Combine(Global.CachePath, "History", nonFullNameFolder);
}
else
{
throw new ArgumentException("Unexpected path provided to GetCacheDirName()");
}
}
else
{
if (IsMirrorDestPath(fullNameInvariant))
{
return FolderSyncNetSource.Path.Combine(Global.CachePath, "Mirror", nonFullNameFolder);
}
#if false
else if (IsSrcPath(fullNameInvariant))
{
return Path.Combine(Global.CachePath, "DestMirror", nonFullNameFolder);
}
#endif
else
{
throw new ArgumentException("Unexpected path provided to GetCacheDirName()");
}
}
} //public static string GetCacheDirName(string dirFullName, bool forHistory)
public static string GetOtherDirName(string dirFullName, bool forHistory)
{
var fullNameInvariant = dirFullName.ToUpperInvariantOnWindows();
var nonFullNameFolder = GetNonFullName(dirFullName);
if (forHistory)
{
if (IsSrcPath(fullNameInvariant))
{
return FolderSyncNetSource.Path.Combine(Global.HistoryDestPath, nonFullNameFolder);
}
else
{
throw new ArgumentException("Unexpected path provided to GetOtherDirName()");
}
}
else
{
if (IsMirrorDestPath(fullNameInvariant))
{
return FolderSyncNetSource.Path.Combine(Global.SrcPath, nonFullNameFolder);
}
else if (IsSrcPath(fullNameInvariant))
{
return FolderSyncNetSource.Path.Combine(Global.MirrorDestPath, nonFullNameFolder);
}
else
{
throw new ArgumentException("Unexpected path provided to GetOtherDirName()");
}
}
} //public static string GetOtherDirName(string dirFullName, bool forHistory)
public static async Task<string> GetOtherFullName(FileInfo fileInfo, bool forHistory)
//public static async Task<string> GetOtherFullName(FolderSyncNetSource.FileInfo fileInfo, bool forHistory)
{
var fullNameInvariant = fileInfo.FullName.ToUpperInvariantOnWindows();
var nonFullName = GetNonFullName(fileInfo.FullName);
if (forHistory)
{
if (IsSrcPath(fullNameInvariant))
{
var srcFileDate = fileInfo.LastWriteTimeUtc; //await GetFileTime(fileInfo); //NB! here read the current file time, not file time at the event
if (Global.HistoryVersionFormat == "PREFIX_TIMESTAMP")
{
var nonFullNameFolder = FolderSyncNetSource.Path.GetDirectoryName(nonFullName);
var fileName = Path.GetFileName(nonFullName);
return FolderSyncNetSource.Path.Combine(Global.HistoryDestPath, nonFullNameFolder, $"{srcFileDate.Ticks}{Global.HistoryVersionSeparator}{fileName}");
}
else if (Global.HistoryVersionFormat == "TIMESTAMP_BEFORE_EXT")
{
var nonFullNameFolder = FolderSyncNetSource.Path.GetDirectoryName(nonFullName);
var fileNameWithoutExtension = FolderSyncNetSource.Path.GetFileNameWithoutExtension(nonFullName);
var fileExtension = FolderSyncNetSource.Path.GetExtension(nonFullName);
return FolderSyncNetSource.Path.Combine(Global.HistoryDestPath, nonFullNameFolder, $"{fileNameWithoutExtension}{Global.HistoryVersionSeparator}{srcFileDate.Ticks}{fileExtension}");
}
else if (Global.HistoryVersionFormat == "SUFIX_TIMESTAMP")
{
return FolderSyncNetSource.Path.Combine(Global.HistoryDestPath, $"{nonFullName}{Global.HistoryVersionSeparator}{srcFileDate.Ticks}");
}
else
{
throw new ArgumentException("Unexpected HistoryFileNameFormat configuration");
}
}
else
{
throw new ArgumentException("Unexpected path provided to GetOtherFullName()");
}
}
else
{
if (IsMirrorDestPath(fullNameInvariant))
{
return FolderSyncNetSource.Path.Combine(Global.SrcPath, nonFullName);
}
else if (IsSrcPath(fullNameInvariant))
{
return FolderSyncNetSource.Path.Combine(Global.MirrorDestPath, nonFullName);
}
else
{
throw new ArgumentException("Unexpected path provided to GetOtherFullName()");
}
}
} //public static async Task<string> GetOtherFullName(FileInfo fileInfo, bool forHistory)
public static async Task<string> GetOtherFullName(WatcherContext context)
{
var fullNameInvariant = context.Event.FullName.ToUpperInvariantOnWindows();
var nonFullName = GetNonFullName(context.Event.FullName);
if (context.ForHistory)
{
if (IsSrcPath(fullNameInvariant))
{
var srcFileDate = await GetFileTime(context); //NB! here read the current file time, not file time at the event
if (Global.HistoryVersionFormat == "PREFIX_TIMESTAMP")
{
var nonFullNameFolder = FolderSyncNetSource.Path.GetDirectoryName(nonFullName);
var fileName = Path.GetFileName(nonFullName);
return FolderSyncNetSource.Path.Combine(Global.HistoryDestPath, nonFullNameFolder, $"{srcFileDate.Ticks}{Global.HistoryVersionSeparator}{fileName}");
}
else if (Global.HistoryVersionFormat == "TIMESTAMP_BEFORE_EXT")
{
var nonFullNameFolder = FolderSyncNetSource.Path.GetDirectoryName(nonFullName);
var fileNameWithoutExtension = FolderSyncNetSource.Path.GetFileNameWithoutExtension(nonFullName);
var fileExtension = FolderSyncNetSource.Path.GetExtension(nonFullName);
return FolderSyncNetSource.Path.Combine(Global.HistoryDestPath, nonFullNameFolder, $"{fileNameWithoutExtension}{Global.HistoryVersionSeparator}{srcFileDate.Ticks}{fileExtension}");
}
else if (Global.HistoryVersionFormat == "SUFIX_TIMESTAMP")
{
return FolderSyncNetSource.Path.Combine(Global.HistoryDestPath, $"{nonFullName}{Global.HistoryVersionSeparator}{srcFileDate.Ticks}");
}
else
{
throw new ArgumentException("Unexpected HistoryFileNameFormat configuration");
}
}
else
{
throw new ArgumentException("Unexpected path provided to GetOtherFullName()");
}
}
else
{
if (IsMirrorDestPath(fullNameInvariant))
{
return FolderSyncNetSource.Path.Combine(Global.SrcPath, nonFullName);
}
else if (IsSrcPath(fullNameInvariant))
{
return FolderSyncNetSource.Path.Combine(Global.MirrorDestPath, nonFullName);
}
else
{
throw new ArgumentException("Unexpected path provided to GetOtherFullName()");
}
}
} //public static async Task<string> GetOtherFullName(Context context)
public static async Task DeleteFile(FileInfoRef otherFileInfo, string otherFullName, WatcherContext context)
{
try
{
otherFullName = Extensions.GetLongPath(otherFullName);
while (true)
{
context.Token.ThrowIfCancellationRequested();
try
{
#if false
var backupFileInfo = new FileInfoRef(null, context.Token);
if (await GetFileExists(backupFileInfo, otherFullName + "~", isSrcFile: !context.IsSrcPath, forHistory: context.ForHistory))
{
#pragma warning disable SEC0116 //Warning SEC0116 Unvalidated file paths are passed to a file delete API, which can allow unauthorized file system operations (e.g. read, write, delete) to be performed on unintended server files.
await Extensions.FSOperation
(
cancellationAndTimeoutToken => File.Delete(otherFullName + "~"),
otherFullName + "~",
context.Token
);
#pragma warning restore SEC0116
}
#endif
//fileInfo?.Refresh();
if (await GetFileExists(otherFileInfo, otherFullName, isSrcFile: !context.IsSrcPath, forHistory: context.ForHistory))
{
await Extensions.FSOperation
(
cancellationAndTimeoutToken => FolderSyncNetSource.File.Move(otherFullName, otherFullName + "~", overwrite: true),
otherFullName + " " + Path.PathSeparator + " " + otherFullName + "~",
context.Token
);
}
return;
}
catch (IOException) //this includes DriveNotFoundException
{
//retry after delay
#if !NOASYNC
await Task.Delay(1000, context.Token); //TODO: config file?
#else
context.Token.WaitHandle.WaitOne(1000);
#endif
}
}
}
catch (Exception ex)
{
await WriteException(ex, context);
}
} //public static async Task DeleteFile(string fullName, Context context)
public static DateTime GetBidirectionalSynchroniserSaveDate(string fullName)
{
DateTime converterSaveDate;
if (!BidirectionalSynchroniserSavedFileDates.TryGetValue(fullName, out converterSaveDate))
{
converterSaveDate = DateTime.MinValue;
}
return converterSaveDate;
}
public static async Task<bool> NeedsUpdate(WatcherContext context)
{
if (
(!Global.EnableMirror && !context.ForHistory)
|| (!Global.EnableHistory && context.ForHistory)
)
{
return false;
}
//compare file date only when not doing initial sync OR when file content comparison is turned off
if (context.IsInitialScan && !Global.DoNotCompareFileContent)
{
return true;
}
else
{
if (context.FileInfo.Length != null) //a file from directory scan
{
//var fileLength = await GetFileSize(context);
var fileLength = context.FileInfo.Length.Value; //NB! this info might be stale, but lets ignore that issue here
long maxFileSize = Math.Min(FileExtensions.MaxByteArraySize, Global.MaxFileSizeMB * (1024 * 1024));
if (maxFileSize > 0 && fileLength > maxFileSize)
{
await AddMessage(ConsoleColor.Red, $"Error synchronising updates from file {context.Event.FullName} : fileLength > maxFileSize : {fileLength} > {maxFileSize}", context);
return false;
}
}
var synchroniserSaveDate = (Global.BidirectionalMirror && !context.ForHistory) ? GetBidirectionalSynchroniserSaveDate(context.Event.FullName) : DateTime.MinValue;
var fileTime = context.Event.FileSystemInfo.LastWriteTimeUtc; //GetFileTime(fullName);
if (
!Global.BidirectionalMirror //no need to debounce BIDIRECTIONAL file save events when bidirectional save is disabled
|| context.ForHistory
|| fileTime > synchroniserSaveDate.AddSeconds(3) //NB! ignore if the file changed during 3 seconds after bidirectional save //TODO!! config
)
{
var otherFullName = await GetOtherFullName(context);
bool considerDateAsNewer = false;
if (Global.DoNotCompareFileDate)
{
considerDateAsNewer = true;
}
else
{
var otherFileInfoRef = new FileInfoRef(context.OtherFileInfo, context.Token);
var otherFileTime = await GetFileTime(otherFileInfoRef, otherFullName, isSrcFile: !context.IsSrcPath, forHistory: context.ForHistory);
context.OtherFileInfo = otherFileInfoRef.Value;
if (otherFileTime == DateTime.MinValue) //file not found
return true;
if (fileTime > otherFileTime) //NB!
considerDateAsNewer = true;
}
if (considerDateAsNewer)
{
if (Global.DoNotCompareFileContent)
{
if (Global.DoNotCompareFileSize)
{
//if date check was done above then there is no need to do file existence check here
if (Global.DoNotCompareFileDate)
{
var otherFileInfoRef = new FileInfoRef(context.OtherFileInfo, context.Token);
bool otherFileExists = await GetFileExists(otherFileInfoRef, otherFullName, isSrcFile: !context.IsSrcPath, forHistory: context.ForHistory);
context.OtherFileInfo = otherFileInfoRef.Value;
if (!otherFileExists)
{
return true;
}
else
{
return false;
}
}
else
{
return false; //if the other file does not exist then the function returns true in date check
}
}
else //if (Global.DoNotCompareFileSize)
{
var fileLength = await GetFileSize(context);
var otherFileInfoRef = new FileInfoRef(context.OtherFileInfo, context.Token);
var otherFileLength = await GetFileSize(otherFileInfoRef, otherFullName, isSrcFile: !context.IsSrcPath, forHistory: context.ForHistory);
context.OtherFileInfo = otherFileInfoRef.Value;
if (fileLength != otherFileLength)
{
return true;
}
else
{
return false;
}
} //if (Global.DoNotCompareFileSize)
}
else //if (Global.DoNotCompareFileContent)
{
return true;
}
} //if (fileTime > otherFileTime)
}
return false;
} //if (context.IsInitialScan && !Global.DoNotCompareFileContent)
}
private static bool IsWatchedFile(string fullName, bool forHistory, bool isSrcPath)
{
if (
(!Global.EnableMirror && !forHistory)
|| (!Global.EnableHistory && forHistory)
|| (forHistory && !isSrcPath)
)
{
return false;
}
var fullNameInvariant = fullName.ToUpperInvariantOnWindows();
if (
!forHistory
&&
(
Global.MirrorWatchedExtension.Any(x => fullNameInvariant.EndsWith("." + x))
|| Global.MirrorWatchedExtension.Contains("*")
|| Global.MirrorWatchedFileNames.Contains(Path.GetFileName(fullNameInvariant))
)
&&
Global.MirrorExcludedExtensions.All(x => //TODO: optimise
!fullNameInvariant.EndsWith("." + x)
&&
( //handle exclusion patterns in the forms like *xyz
!x.StartsWith("*")
|| fullNameInvariant.Length < x.Length - 1
|| !fullNameInvariant.EndsWith(/*"." + */x.Substring(1)) //NB! the existence of dot is not verified in this case //TODO: use Regex
)
)
)
{
var nonFullNameInvariantWithLeadingSlash = Program.DirectorySeparatorChar + GetNonFullName(fullNameInvariant);
if (
//Global.MirrorIgnorePathsStartingWith.Any(x => nonFullNameInvariantWithLeadingSlash.StartsWith(x))
//|| Global.MirrorIgnorePathsContaining.Any(x => nonFullNameInvariantWithLeadingSlash.Contains(x))
//|| Global.MirrorIgnorePathsEndingWith.Any(x => nonFullNameInvariantWithLeadingSlash.EndsWith(x))
Global.MirrorIgnorePathsContainingACHasAny //needed to avoid exceptions
&& Global.MirrorIgnorePathsContainingAC.ParseText(Program.NullChar + nonFullNameInvariantWithLeadingSlash + Program.NullChar).Any()
)
{
return false;
}
return true;
}
else if (
forHistory
&&
(
Global.HistoryWatchedExtension.Any(x => fullNameInvariant.EndsWith("." + x))
|| Global.HistoryWatchedExtension.Contains("*")
|| Global.HistoryWatchedFileNames.Contains(Path.GetFileName(fullNameInvariant))
)
&&
Global.HistoryExcludedExtensions.All(x => //TODO: optimise
!fullNameInvariant.EndsWith("." + x)
&&
( //handle exclusion patterns in the forms like *xyz
!x.StartsWith("*")
|| fullNameInvariant.Length < x.Length - 1
|| !fullNameInvariant.EndsWith(/*"." + */x.Substring(1)) //NB! the existence of dot is not verified in this case //TODO: use Regex
)
)
)
{
var nonFullNameInvariantWithLeadingSlash = Program.DirectorySeparatorChar + GetNonFullName(fullNameInvariant);
if (
//Global.HistoryIgnorePathsStartingWith.Any(x => nonFullNameInvariantWithLeadingSlash.StartsWith(x))
//|| Global.HistoryIgnorePathsContaining.Any(x => nonFullNameInvariantWithLeadingSlash.Contains(x))
//|| Global.HistoryIgnorePathsEndingWith.Any(x => nonFullNameInvariantWithLeadingSlash.EndsWith(x))
Global.HistoryIgnorePathsContainingACHasAny //needed to avoid exceptions
&& Global.HistoryIgnorePathsContainingAC.ParseText(Program.NullChar + nonFullNameInvariantWithLeadingSlash + Program.NullChar).Any()
)
{
return false;
}
return true;
}
return false;
} //private bool IsWatchedFile(string fullName, bool forHistory)
public static async Task SaveFileModifications(byte[] fileData, WatcherContext context)
{
var otherFullName = await GetOtherFullName(context);
var otherFileInfoRef = new FileInfoRef(context.OtherFileInfo, context.Token);
var longOtherFullName = Extensions.GetLongPath(otherFullName);
long maxFileSize = Math.Min(FileExtensions.MaxByteArraySize, Global.MaxFileSizeMB * (1024 * 1024));
//NB! detect whether the file actually changed
var otherFileDataTuple =
!Global.DoNotCompareFileContent
&&
(await GetFileExists
(
otherFileInfoRef,
otherFullName,
isSrcFile: !context.IsSrcPath,
forHistory: context.ForHistory
))
? await FileExtensions.ReadAllBytesAsync //TODO: optimisation: no need to read the bytes in case the file lengths are different
(
longOtherFullName,
/*allowVSS: */Global.AllowVSS,
context.Token,
maxFileSize,
readBufferKB: Global.ReadBufferKB,
bufferReadDelayMs: Global.BufferReadDelayMs
)
: null;
context.OtherFileInfo = otherFileInfoRef.Value;
if (
(
!Global.DoNotCompareFileContent
&&
(
(otherFileDataTuple?.Item1?.Length ?? -1) != fileData.Length
|| !FileExtensions.BinaryEqual(otherFileDataTuple.Item1, fileData)
)
)
||
Global.DoNotCompareFileContent
)
{
var minDiskFreeSpace = context.ForHistory ? Global.HistoryDestPathMinFreeSpace : (context.IsSrcPath ? Global.MirrorDestPathMinFreeSpace : Global.SrcPathMinFreeSpace);
var actualFreeSpace = minDiskFreeSpace > 0 ? Extensions.CheckDiskSpace(otherFullName) : 0;
if (minDiskFreeSpace > actualFreeSpace - fileData.Length)
{
await AddMessage(ConsoleColor.Red, $"Error synchronising updates from file {context.Event.FullName} : minDiskFreeSpace > actualFreeSpace : {minDiskFreeSpace} > {actualFreeSpace}", context);
return;
}
//if (!context.ForHistory && context.OtherFileInfo.Exists != false) //assume that in case of history files there is no point in making a back copy of the history file even if it exists at the destination
// await DeleteFile(otherFileInfoRef, otherFullName, context);
var otherDirName = Extensions.GetDirPathWithTrailingSlash(FolderSyncNetSource.Path.GetDirectoryName(otherFullName));
var longOtherDirName = Extensions.GetLongPath(otherDirName);
bool newFolderCreated = false;
if (
!Global.CacheDestAndHistoryFolders
|| !Global.CreatedFoldersCache.ContainsKey(longOtherDirName.ToUpperInvariantOnWindows())
)
{
if (!await Extensions.FSOperation
(
cancellationAndTimeoutToken => Directory.Exists(longOtherDirName),
longOtherDirName,
context.Token
))
{
newFolderCreated = true;
if (
Global.CacheDestAndHistoryFolders
&& Global.PersistentCacheDestAndHistoryFolders
&& context.IsSrcPath
&& (!Global.BidirectionalMirror || context.ForHistory)
)
{
//NB! create file cache before creating the folder so that any files that are concurrently added to the folder upon creating it are all added to cache
//We do not just create a cache folder in every case a file is added to an existing folder since that folder will be cached separately by the initial folder scan once it reaches this folder
using (await Global.PersistentCacheLocks.LockAsync(longOtherDirName.ToUpperInvariantOnWindows(), context.Token))
{
var cachedFileInfos = await ReadFileInfoCache(longOtherDirName, context.ForHistory);
if (cachedFileInfos == null) //ensure that the cache was not created yet by a concurrent file write to same folder
{
cachedFileInfos = new Dictionary<string, CachedFileInfo>();
await SaveFileInfoCache(cachedFileInfos, longOtherDirName, context.ForHistory);
}
}
}
await Extensions.FSOperation
(
cancellationAndTimeoutToken => Directory.CreateDirectory(longOtherDirName),
longOtherDirName,
context.Token
);
} //if (!await Extensions.FSOperation(() => Directory.Exists(longOtherDirName), context.Token))
if (Global.CacheDestAndHistoryFolders)
Global.CreatedFoldersCache.TryAdd(longOtherDirName.ToUpperInvariantOnWindows(), true);
}
//invalidate file data in dirlist cache before file write
if (!newFolderCreated) //optimisation
await InvalidateFileDataInPersistentCache(context);
var utcNowBeforeSave = DateTime.UtcNow;