@@ -41,8 +41,7 @@ internal static class Global
4141
4242
4343
44- internal static readonly AsyncLockQueueDictionary BinaryFileOperationLocks = new AsyncLockQueueDictionary ( ) ;
45- internal static readonly AsyncLockQueueDictionary CodeFileOperationLocks = new AsyncLockQueueDictionary ( ) ;
44+ internal static readonly AsyncLockQueueDictionary FileOperationLocks = new AsyncLockQueueDictionary ( ) ;
4645 //internal static readonly AsyncLock FileOperationAsyncLock = new AsyncLock();
4746 }
4847#pragma warning restore S2223
@@ -432,32 +431,46 @@ public static async Task FileUpdated(string fullName, Context context)
432431 && NeedsUpdate ( fullName ) //NB!
433432 )
434433 {
435- if (
436- fullName . EndsWith ( "." + Global . WatchedCodeExtension )
437- || Global . WatchedCodeExtension == "*"
438- )
434+ var otherFullName = GetOtherFullName ( fullName ) ;
435+ var filenames = new List < string > ( )
436+ {
437+ fullName ,
438+ otherFullName
439+ } ;
440+
441+ //NB! in order to avoid deadlocks, always take the locks in deterministic order
442+ filenames . Sort ( StringComparer . InvariantCultureIgnoreCase ) ;
443+
444+ using ( await Global . FileOperationLocks . LockAsync ( filenames [ 0 ] , context . Token ) )
445+ using ( await Global . FileOperationLocks . LockAsync ( filenames [ 1 ] , context . Token ) )
439446 {
440- if ( fullName . ToUpperInvariant ( ) . StartsWith ( Global . AsyncPath . ToUpperInvariant ( ) ) )
441- {
442- await AsyncToSyncConverter . AsyncFileUpdated ( fullName , context ) ;
443- }
444- else if ( fullName . ToUpperInvariant ( ) . StartsWith ( Global . SyncPath . ToUpperInvariant ( ) ) ) //NB!
447+ if (
448+ fullName . EndsWith ( "." + Global . WatchedCodeExtension )
449+ || Global . WatchedCodeExtension == "*"
450+ )
445451 {
446- await SyncToAsyncConverter . SyncFileUpdated ( fullName , context ) ;
452+ if ( fullName . ToUpperInvariant ( ) . StartsWith ( Global . AsyncPath . ToUpperInvariant ( ) ) )
453+ {
454+ await AsyncToSyncConverter . AsyncFileUpdated ( fullName , context ) ;
455+ }
456+ else if ( fullName . ToUpperInvariant ( ) . StartsWith ( Global . SyncPath . ToUpperInvariant ( ) ) ) //NB!
457+ {
458+ await SyncToAsyncConverter . SyncFileUpdated ( fullName , context ) ;
459+ }
460+ else
461+ {
462+ throw new ArgumentException ( "fullName" ) ;
463+ }
447464 }
448- else
465+ else //Assume ResX file
449466 {
450- throw new ArgumentException ( "fullName" ) ;
451- }
452- }
453- else //Assume ResX file
454- {
455- //@"\\?\" prefix is needed for reading from long paths: https://stackoverflow.com/questions/44888844/directorynotfoundexception-when-using-long-paths-in-net-4-7
456- var fileData = await FileExtensions . ReadAllBytesAsync ( @"\\?\" + fullName , context . Token ) ;
457- var originalData = fileData ;
467+ //@"\\?\" prefix is needed for reading from long paths: https://stackoverflow.com/questions/44888844/directorynotfoundexception-when-using-long-paths-in-net-4-7
468+ var fileData = await FileExtensions . ReadAllBytesAsync ( @"\\?\" + fullName , context . Token ) ;
469+ var originalData = fileData ;
458470
459- //save without transformations
460- await ConsoleWatch . SaveFileModifications ( fullName , fileData , originalData , context ) ;
471+ //save without transformations
472+ await ConsoleWatch . SaveFileModifications ( fullName , fileData , originalData , context ) ;
473+ }
461474 }
462475 }
463476 }
@@ -773,28 +786,15 @@ public static async Task SaveFileModifications(string fullName, byte[] fileData,
773786 || ! FileExtensions . BinaryEqual ( otherFileData , fileData )
774787 )
775788 {
776- var filenames = new List < string > ( )
777- {
778- fullName ,
779- otherFullName
780- } ;
781-
782- //NB! in order to avoid deadlocks, always take the locks in deterministic order
783- filenames . Sort ( StringComparer . InvariantCultureIgnoreCase ) ;
784-
785- using ( await Global . BinaryFileOperationLocks . LockAsync ( filenames [ 0 ] , context . Token ) )
786- using ( await Global . BinaryFileOperationLocks . LockAsync ( filenames [ 1 ] , context . Token ) )
787- {
788- await DeleteFile ( otherFullName , context ) ;
789+ await DeleteFile ( otherFullName , context ) ;
789790
790- Directory . CreateDirectory ( Path . GetDirectoryName ( otherFullName ) ) ;
791+ Directory . CreateDirectory ( Path . GetDirectoryName ( otherFullName ) ) ;
791792
792- //@"\\?\" prefix is needed for writing to long paths: https://stackoverflow.com/questions/44888844/directorynotfoundexception-when-using-long-paths-in-net-4-7
793- await FileExtensions . WriteAllBytesAsync ( @"\\?\" + otherFullName , fileData , context . Token ) ;
793+ //@"\\?\" prefix is needed for writing to long paths: https://stackoverflow.com/questions/44888844/directorynotfoundexception-when-using-long-paths-in-net-4-7
794+ await FileExtensions . WriteAllBytesAsync ( @"\\?\" + otherFullName , fileData , context . Token ) ;
794795
795- var now = DateTime . UtcNow ; //NB! compute now after saving the file
796- ConverterSavedFileDates [ otherFullName ] = now ;
797- }
796+ var now = DateTime . UtcNow ; //NB! compute now after saving the file
797+ ConverterSavedFileDates [ otherFullName ] = now ;
798798
799799
800800 await AddMessage ( ConsoleColor . Magenta , $ "Synchronised updates from file { fullName } ", context ) ;
0 commit comments