@@ -21,76 +21,110 @@ public UpdateExistingFilesBackuper(IUpdateRepository updateRepository, ILogger<U
2121
2222 public List < Tuple < string , string > > BackedUpFileSystemInfos { get ; }
2323
24- public async Task BackupExistingFilesAsync ( CancellationToken cancellationToken )
24+ public Task BackupExistingFilesAsync ( CancellationToken cancellationToken )
2525 {
26- await Task . Run ( ( ) => BackupExistingFiles ( cancellationToken ) ) ;
27- }
28-
29- private void BackupExistingFiles ( CancellationToken cancellationToken )
30- {
31- DirectoryInfo applicationBaseDirectoryInfo = new DirectoryInfo ( _updateRepository . UpdateData . ApplicationBaseDirectory ) ;
32-
33- foreach ( var fileSystemInfo in applicationBaseDirectoryInfo . GetFileSystemInfos ( ) )
26+ return Task . Run ( ( ) =>
3427 {
35- if ( cancellationToken . IsCancellationRequested )
28+ try
3629 {
37- _logger . LogWarning ( "UpdateExistingFilesBackuper.BackupExistingFiles: Cancellation requested" ) ;
30+ var applicationBaseDirectoryInfo = new DirectoryInfo ( _updateRepository . UpdateData . ApplicationBaseDirectory ) ;
31+ var filesToBackup = GetFilesToBackup ( applicationBaseDirectoryInfo , cancellationToken ) ;
3832
39- return ;
40- }
41-
42- if ( fileSystemInfo is DirectoryInfo )
43- {
44- if ( ! fileSystemInfo . Name . Equals ( "Contents" , StringComparison . InvariantCultureIgnoreCase ) &&
45- ! fileSystemInfo . Name . Equals ( "ByteSync.app" , StringComparison . InvariantCultureIgnoreCase ) )
33+ foreach ( var fileSystemInfo in filesToBackup )
4634 {
47- _logger . LogInformation ( "UpdateExistingFilesBackuper.BackupExistingFiles: ignored directory {directory}" , fileSystemInfo . FullName ) ;
35+ if ( cancellationToken . IsCancellationRequested )
36+ {
37+ _logger . LogWarning ( "UpdateExistingFilesBackuper.BackupExistingFiles: Cancellation requested" ) ;
38+ return ;
39+ }
4840
49- continue ;
41+ BackupFileSystemInfo ( fileSystemInfo ) ;
5042 }
5143 }
52-
53- if ( fileSystemInfo is FileInfo fi )
44+ catch ( OperationCanceledException )
5445 {
55- // Si l'une des conditions est réunies
56- // - Le Nom ne contient pas ByteSync
57- // - Son extension est dans .log, .dat, .xml, .json ou .zip
58- // - Il commence par unins et finit par .exe
59- // => On l'ignore
60- if ( ! fileSystemInfo . Name . Contains ( "ByteSync" , StringComparison . InvariantCultureIgnoreCase ) ||
61- fi . Extension . ToLower ( ) . In ( ".log" , ".dat" , ".xml" , ".json" , ".zip" ) ||
62- ( fi . Name . StartsWith ( "unins" , StringComparison . InvariantCultureIgnoreCase )
63- && fi . Extension . Equals ( ".exe" , StringComparison . InvariantCultureIgnoreCase ) ) )
64- {
65- _logger . LogInformation ( "UpdateExistingFilesBackuper.BackupExistingFiles: ignored file {file}" , fileSystemInfo . FullName ) ;
66-
67- continue ;
68- }
46+ _logger . LogWarning ( "UpdateExistingFilesBackuper.BackupExistingFiles: Operation was canceled" ) ;
6947 }
70-
71- string previousFullName = fileSystemInfo . FullName ;
72-
73- int cpt = 0 ;
74- var backupDestination = $ "{ fileSystemInfo . FullName } .{ UpdateConstants . BAK_EXTENSION } { cpt } ";
75-
76- while ( File . Exists ( backupDestination ) || Directory . Exists ( backupDestination ) )
48+ catch ( Exception ex )
7749 {
78- cpt += 1 ;
79- backupDestination = $ " { fileSystemInfo . FullName } . { UpdateConstants . BAK_EXTENSION } { cpt } " ;
50+ _logger . LogError ( ex , "UpdateExistingFilesBackuper.BackupExistingFiles: An error occurred" ) ;
51+ throw ;
8052 }
81-
82- _logger . LogInformation ( "UpdateExistingFilesBackuper: Renaming {Source} to {Destination}" , previousFullName , backupDestination ) ;
53+ } , cancellationToken ) ;
54+ }
8355
84- if ( fileSystemInfo is FileInfo fileInfo )
56+ private IEnumerable < FileSystemInfo > GetFilesToBackup ( DirectoryInfo baseDirectory , CancellationToken cancellationToken )
57+ {
58+ var result = new List < FileSystemInfo > ( ) ;
59+
60+ foreach ( var fileSystemInfo in baseDirectory . GetFileSystemInfos ( ) )
61+ {
62+ if ( cancellationToken . IsCancellationRequested )
63+ break ;
64+
65+ if ( fileSystemInfo is DirectoryInfo directoryInfo )
8566 {
86- fileInfo . MoveTo ( backupDestination ) ;
67+ // Only include files specifically named “Contents” or “ByteSync.app”
68+ if ( directoryInfo . Name . Equals ( "Contents" , StringComparison . InvariantCultureIgnoreCase ) ||
69+ directoryInfo . Name . Equals ( "ByteSync.app" , StringComparison . InvariantCultureIgnoreCase ) )
70+ {
71+ result . Add ( fileSystemInfo ) ;
72+ }
73+ else
74+ {
75+ _logger . LogInformation ( "UpdateExistingFilesBackuper.GetFilesToBackup: ignored directory {directory}" , fileSystemInfo . FullName ) ;
76+ }
8777 }
88- else if ( fileSystemInfo is DirectoryInfo directoryInfo )
78+ else if ( fileSystemInfo is FileInfo fileInfo )
8979 {
90- directoryInfo . MoveTo ( backupDestination ) ;
80+ // Only include files that:
81+ // - Contain “ByteSync” in their name
82+ // - Do not have a .log, .dat, .xml, .json or .zip extension
83+ // - Do not start with “unins” if the extension is .exe
84+ bool containsByteSyncName = fileInfo . Name . Contains ( "ByteSync" , StringComparison . InvariantCultureIgnoreCase ) ;
85+ bool hasAllowedExtension = ! fileInfo . Extension . ToLower ( ) . In ( ".log" , ".dat" , ".xml" , ".json" , ".zip" ) ;
86+ bool isUninstaller = fileInfo . Name . StartsWith ( "unins" , StringComparison . InvariantCultureIgnoreCase )
87+ && fileInfo . Extension . Equals ( ".exe" , StringComparison . InvariantCultureIgnoreCase ) ;
88+
89+ if ( containsByteSyncName && hasAllowedExtension && ! isUninstaller )
90+ {
91+ result . Add ( fileSystemInfo ) ;
92+ }
93+ else
94+ {
95+ _logger . LogInformation ( "UpdateExistingFilesBackuper.GetFilesToBackup: ignored file {file}" , fileSystemInfo . FullName ) ;
96+ }
9197 }
98+ }
99+
100+ return result ;
101+ }
102+
103+ private void BackupFileSystemInfo ( FileSystemInfo fileSystemInfo )
104+ {
105+ string previousFullName = fileSystemInfo . FullName ;
106+
107+ int cpt = 0 ;
108+ var backupDestination = $ "{ fileSystemInfo . FullName } .{ UpdateConstants . BAK_EXTENSION } { cpt } ";
109+
110+ while ( File . Exists ( backupDestination ) || Directory . Exists ( backupDestination ) )
111+ {
112+ cpt += 1 ;
113+ backupDestination = $ "{ fileSystemInfo . FullName } .{ UpdateConstants . BAK_EXTENSION } { cpt } ";
114+ }
115+
116+ _logger . LogInformation ( "UpdateExistingFilesBackuper: Renaming {Source} to {Destination}" , previousFullName , backupDestination ) ;
92117
93- BackedUpFileSystemInfos . Add ( new Tuple < string , string > ( previousFullName , backupDestination ) ) ;
118+ if ( fileSystemInfo is FileInfo fileInfo )
119+ {
120+ fileInfo . MoveTo ( backupDestination ) ;
121+ }
122+ else if ( fileSystemInfo is DirectoryInfo directoryInfo )
123+ {
124+ directoryInfo . MoveTo ( backupDestination ) ;
94125 }
126+
127+ BackedUpFileSystemInfos . Add ( new Tuple < string , string > ( previousFullName , backupDestination ) ) ;
95128 }
96- }
129+ }
130+
0 commit comments