@@ -322,6 +322,8 @@ internal class ConsoleWatch
322322 public static bool DoingInitialSync = false ;
323323#pragma warning restore S2223
324324
325+ private static readonly AsyncLockQueueDictionary FileLocks = new AsyncLockQueueDictionary ( ) ;
326+
325327
326328 public ConsoleWatch ( IWatcher3 watch )
327329 {
@@ -468,7 +470,7 @@ public static async Task FileUpdated(string fullName, Context context)
468470 }
469471 else //Assume ResX file
470472 {
471- var fileData = await FileExtensions . ReadAllTextAsync ( fullName , context . Token ) ;
473+ var fileData = await FileExtensions . ReadAllBytesAsync ( fullName , context . Token ) ;
472474 var originalData = fileData ;
473475
474476 //save without transformations
@@ -567,8 +569,12 @@ private static async Task OnRenamedAsync(IRenamedFileSystemEvent rfse, Cancellat
567569 //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
568570 if ( ! rfse . FileSystemInfo . FullName . EndsWith ( "~" ) )
569571 {
570- await FileUpdated ( rfse . FileSystemInfo . FullName , context ) ;
571- await FileDeleted ( rfse . PreviousFileSystemInfo . FullName , context ) ;
572+ using ( await FileLocks . LockAsync ( rfse . FileSystemInfo . FullName , token ) )
573+ using ( await FileLocks . LockAsync ( rfse . PreviousFileSystemInfo . FullName , token ) )
574+ {
575+ await FileUpdated ( rfse . FileSystemInfo . FullName , context ) ;
576+ await FileDeleted ( rfse . PreviousFileSystemInfo . FullName , context ) ;
577+ }
572578 }
573579 }
574580 }
@@ -596,9 +602,12 @@ private static async Task OnRemovedAsync(IFileSystemEvent fse, CancellationToken
596602 if ( IsWatchedFile ( fse . FileSystemInfo . FullName ) )
597603 {
598604 await AddMessage ( ConsoleColor . Yellow , $ "[{ ( fse . IsFile ? "F" : "D" ) } ][-]:{ fse . FileSystemInfo . FullName } ", context ) ;
599- }
600605
601- await FileDeleted ( fse . FileSystemInfo . FullName , context ) ;
606+ using ( await FileLocks . LockAsync ( fse . FileSystemInfo . FullName , token ) )
607+ {
608+ await FileDeleted ( fse . FileSystemInfo . FullName , context ) ;
609+ }
610+ }
602611 }
603612 else
604613 {
@@ -619,12 +628,15 @@ public static async Task OnAddedAsync(IFileSystemEvent fse, CancellationToken to
619628 {
620629 if ( fse . IsFile )
621630 {
622- //if (IsWatchedFile(fse.FileSystemInfo.FullName))
623- //{
624- // await AddMessage(ConsoleColor.Green, $"[{(fse.IsFile ? "F" : "D")}][+]:{fse.FileSystemInfo.FullName}", context);
625- //}
631+ if ( IsWatchedFile ( fse . FileSystemInfo . FullName ) )
632+ {
633+ //await AddMessage(ConsoleColor.Green, $"[{(fse.IsFile ? "F" : "D")}][+]:{fse.FileSystemInfo.FullName}", context);
626634
627- await FileUpdated ( fse . FileSystemInfo . FullName , context ) ;
635+ using ( await FileLocks . LockAsync ( fse . FileSystemInfo . FullName , token ) )
636+ {
637+ await FileUpdated ( fse . FileSystemInfo . FullName , context ) ;
638+ }
639+ }
628640 }
629641 else
630642 {
@@ -648,9 +660,12 @@ private static async Task OnTouchedAsync(IFileSystemEvent fse, CancellationToken
648660 if ( IsWatchedFile ( fse . FileSystemInfo . FullName ) )
649661 {
650662 await AddMessage ( ConsoleColor . Gray , $ "[{ ( fse . IsFile ? "F" : "D" ) } ][T]:{ fse . FileSystemInfo . FullName } ", context ) ;
651- }
652663
653- await FileUpdated ( fse . FileSystemInfo . FullName , context ) ;
664+ using ( await FileLocks . LockAsync ( fse . FileSystemInfo . FullName , token ) )
665+ {
666+ await FileUpdated ( fse . FileSystemInfo . FullName , context ) ;
667+ }
668+ }
654669 }
655670 else
656671 {
@@ -710,7 +725,10 @@ public static async Task SaveFileModifications(string fullName, string fileData,
710725 ? await FileExtensions . ReadAllTextAsync ( otherFullName , context . Token )
711726 : null ;
712727
713- if ( ( otherFileData ? . Length ?? - 1 ) != fileData . Length || otherFileData != fileData )
728+ if (
729+ ( otherFileData ? . Length ?? - 1 ) != fileData . Length
730+ || otherFileData != fileData
731+ )
714732 {
715733 await DeleteFile ( otherFullName , context ) ;
716734
@@ -722,6 +740,51 @@ public static async Task SaveFileModifications(string fullName, string fileData,
722740 Global . ConverterSavedFileDates [ otherFullName ] = now ;
723741
724742
743+ await AddMessage ( ConsoleColor . Magenta , $ "Synchronised updates from file { fullName } ", context ) ;
744+ }
745+ else if ( false )
746+ {
747+ //touch the file
748+ var now = DateTime . UtcNow ; //NB! compute common now for ConverterSavedFileDates
749+
750+ try
751+ {
752+ File . SetLastWriteTimeUtc ( otherFullName , now ) ;
753+ }
754+ catch ( Exception ex )
755+ {
756+ await ConsoleWatch . WriteException ( ex , context ) ;
757+ }
758+
759+ Global . ConverterSavedFileDates [ otherFullName ] = now ;
760+ }
761+ }
762+
763+ public static async Task SaveFileModifications ( string fullName , byte [ ] fileData , byte [ ] originalData , Context context )
764+ {
765+ var otherFullName = GetOtherFullName ( fullName ) ;
766+
767+
768+ //NB! detect whether the file actually changed
769+ var otherFileData = File . Exists ( otherFullName )
770+ ? await FileExtensions . ReadAllBytesAsync ( otherFullName , context . Token )
771+ : null ;
772+
773+ if (
774+ ( otherFileData ? . Length ?? - 1 ) != fileData . Length
775+ || ! FileExtensions . BinaryEqual ( otherFileData , fileData )
776+ )
777+ {
778+ await DeleteFile ( otherFullName , context ) ;
779+
780+ Directory . CreateDirectory ( Path . GetDirectoryName ( otherFullName ) ) ;
781+
782+ await FileExtensions . WriteAllBytesAsync ( otherFullName , fileData , context . Token ) ;
783+
784+ var now = DateTime . UtcNow ; //NB! compute now after saving the file
785+ Global . ConverterSavedFileDates [ otherFullName ] = now ;
786+
787+
725788 await AddMessage ( ConsoleColor . Magenta , $ "Synchronised updates from file { fullName } ", context ) ;
726789 }
727790 else if ( false )
0 commit comments