@@ -827,31 +827,54 @@ private bool StreamDownloads(UpdateManifest updateManifest, CancellationToken ca
827827 return false ;
828828 }
829829
830+ var isSelf = IsSelf ( currentFile . Path ) ;
831+ if ( isSelf )
832+ {
833+ ShouldRestart = true ;
834+
835+ try
836+ {
837+ if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
838+ {
839+ temporaryFileInfo . UnixFileMode |= UnixFileMode . UserExecute ;
840+ }
841+ }
842+ catch ( Exception exception )
843+ {
844+ ApplicationContext . CurrentContext . Logger . LogWarning ( exception , $ "Failed to make { temporaryFileInfo . Name } executable") ;
845+ }
846+ }
847+
830848 try
831849 {
832850 if ( targetFileInfo . Exists )
833- {
834- targetFileInfo . Delete ( ) ;
851+ {
852+ ApplicationContext . CurrentContext . Logger . LogTrace (
853+ "Attempting to delete: {TargetFilePath}" ,
854+ relativePathToTargetFile
855+ ) ;
856+ targetFileInfo . Delete ( ) ;
835857 }
836858 }
837859 catch ( Exception exception )
838860 {
839861 ApplicationContext . CurrentContext . Logger . LogWarning (
840862 exception ,
841- "Failed to delete {RelativeTargetPath}" ,
863+ "Failed to delete, falling back to rename {RelativeTargetPath}" ,
842864 relativePathToTargetFile
843865 ) ;
844866
845867 FileInfo oldTargetFileInfo = new ( $ "{ targetFileInfo . FullName } .old") ;
846868 try
847869 {
848- targetFileInfo . MoveTo ( oldTargetFileInfo . FullName , true ) ;
870+ FileInfo temporaryRenameInfo = new FileInfo ( targetFileInfo . FullName ) ;
871+ temporaryRenameInfo . MoveTo ( oldTargetFileInfo . FullName , true ) ;
849872 }
850873 catch ( Exception moveToException )
851874 {
852875 ApplicationContext . CurrentContext . Logger . LogWarning (
853876 moveToException ,
854- "Failed to move {RelativeSourcePath} to {RelativeTargetPath}.old" ,
877+ "Failed to rename {RelativeSourcePath} to {RelativeTargetPath}.old" ,
855878 relativePathToTargetFile ,
856879 relativePathToTargetFile
857880 ) ;
@@ -874,6 +897,13 @@ private bool StreamDownloads(UpdateManifest updateManifest, CancellationToken ca
874897 }
875898 catch ( Exception exception )
876899 {
900+ ApplicationContext . CurrentContext . Logger . LogTrace (
901+ exception ,
902+ "Failed to move {SourcePath} to {TargetPath}" ,
903+ temporaryFileInfo . FullName ,
904+ targetFileInfo . FullName
905+ ) ;
906+
877907 ApplicationContext . CurrentContext . Logger . LogError (
878908 exception ,
879909 "Failed to move {RelativeSourcePath} to {RelativeTargetPath}" ,
@@ -1189,7 +1219,8 @@ private bool DownloadUpdates(int workerThreadIndex, CancellationToken cancellati
11891219 return false ;
11901220 }
11911221
1192- if ( IsSelf ( currentFile . Path ) )
1222+ var isSelf = IsSelf ( currentFile . Path ) ;
1223+ if ( isSelf )
11931224 {
11941225 ShouldRestart = true ;
11951226
@@ -1210,12 +1241,36 @@ private bool DownloadUpdates(int workerThreadIndex, CancellationToken cancellati
12101241 {
12111242 if ( targetFileInfo . Exists )
12121243 {
1244+ ApplicationContext . CurrentContext . Logger . LogTrace (
1245+ "Attempting to delete: {TargetFilePath}" ,
1246+ relativePathToTargetFile
1247+ ) ;
12131248 targetFileInfo . Delete ( ) ;
12141249 }
12151250 }
12161251 catch ( Exception exception )
12171252 {
1218- ApplicationContext . CurrentContext . Logger . LogWarning ( exception , $ "Failed to delete { relativePathToTargetFile } ") ;
1253+ ApplicationContext . CurrentContext . Logger . LogWarning (
1254+ exception ,
1255+ "Failed to delete, falling back to rename {RelativeTargetPath}" ,
1256+ relativePathToTargetFile
1257+ ) ;
1258+
1259+ FileInfo oldTargetFileInfo = new ( $ "{ targetFileInfo . FullName } .old") ;
1260+ try
1261+ {
1262+ FileInfo temporaryRenameInfo = new FileInfo ( targetFileInfo . FullName ) ;
1263+ temporaryRenameInfo . MoveTo ( oldTargetFileInfo . FullName , true ) ;
1264+ }
1265+ catch ( Exception moveToException )
1266+ {
1267+ ApplicationContext . CurrentContext . Logger . LogWarning (
1268+ moveToException ,
1269+ "Failed to rename {RelativeSourcePath} to {RelativeTargetPath}.old" ,
1270+ relativePathToTargetFile ,
1271+ relativePathToTargetFile
1272+ ) ;
1273+ }
12191274 }
12201275
12211276 try
@@ -1228,13 +1283,20 @@ private bool DownloadUpdates(int workerThreadIndex, CancellationToken cancellati
12281283 }
12291284
12301285#if DIAGNOSTIC
1231- ApplicationContext . CurrentContext . Logger . LogTraceApplicationContext . CurrentContext . Logger . LogTrace ( $ "Completed for { currentFile . Path } ") ;
1286+ ApplicationContext . CurrentContext . Logger . LogTrace ( $ "Completed for { currentFile . Path } ") ;
12321287#endif
12331288 currentResult . State = DownloadState . Completed ;
12341289 _downloadedBytes += currentFile . Size ;
12351290 }
12361291 catch ( Exception exception )
12371292 {
1293+ ApplicationContext . CurrentContext . Logger . LogTrace (
1294+ exception ,
1295+ "Failed to move {SourcePath} to {TargetPath}" ,
1296+ temporaryFileInfo . FullName ,
1297+ targetFileInfo . FullName
1298+ ) ;
1299+
12381300 ApplicationContext . CurrentContext . Logger . LogError (
12391301 exception ,
12401302 $ "Failed to move { relativePathToTemporaryFile } to { relativePathToTargetFile } "
0 commit comments