Skip to content

Commit b6e9eb1

Browse files
committedApr 10, 2025
Fix: Fixed crash that would sometimes occur when updating
1 parent c489032 commit b6e9eb1

File tree

1 file changed

+54
-17
lines changed

1 file changed

+54
-17
lines changed
 

‎src/Files.App/Services/App/AppUpdateSideloadService.cs

+54-17
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// Copyright (c) Files Community
22
// Licensed under the MIT License.
33

4-
using CommunityToolkit.WinUI.Helpers;
54
using Microsoft.Extensions.Logging;
65
using System.IO;
76
using System.Net.Http;
7+
using System.Runtime.InteropServices;
88
using System.Xml.Serialization;
99
using Windows.ApplicationModel;
1010
using Windows.Management.Deployment;
@@ -123,34 +123,71 @@ public async Task CheckForUpdatesAsync()
123123

124124
public async Task CheckAndUpdateFilesLauncherAsync()
125125
{
126-
var destFolderPath = Path.Combine(UserDataPaths.GetDefault().LocalAppData, "Files");
127-
var destExeFilePath = Path.Combine(destFolderPath, "Files.App.Launcher.exe");
128-
129-
if (File.Exists(destExeFilePath))
126+
try
130127
{
128+
var destFolderPath = Path.Combine(UserDataPaths.GetDefault().LocalAppData, "Files");
129+
var destExeFilePath = Path.Combine(destFolderPath, "Files.App.Launcher.exe");
130+
131+
if (!File.Exists(destExeFilePath))
132+
return;
133+
131134
var hashEqual = false;
132-
var srcHashFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/FilesOpenDialog/Files.App.Launcher.exe.sha256"));
135+
var srcHashFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/FilesOpenDialog/Files.App.Launcher.exe.sha256"))
136+
.AsTask().ConfigureAwait(false);
133137
var destHashFilePath = Path.Combine(destFolderPath, "Files.App.Launcher.exe.sha256");
134138

135139
if (File.Exists(destHashFilePath))
136140
{
137-
await using var srcStream = (await srcHashFile.OpenReadAsync()).AsStream();
138-
await using var destStream = File.OpenRead(destHashFilePath);
139-
140-
hashEqual = HashEqual(srcStream, destStream);
141+
try
142+
{
143+
await using var srcStream = (await srcHashFile.OpenReadAsync().AsTask().ConfigureAwait(false)).AsStream();
144+
await using var destStream = File.OpenRead(destHashFilePath);
145+
hashEqual = HashEqual(srcStream, destStream);
146+
}
147+
catch (COMException ex)
148+
{
149+
Logger?.LogWarning(ex, "Failed to compare hash files");
150+
return;
151+
}
152+
catch (IOException ex)
153+
{
154+
Logger?.LogWarning(ex, "IO error while reading hash files");
155+
return;
156+
}
141157
}
142158

143159
if (!hashEqual)
144160
{
145-
var srcExeFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/FilesOpenDialog/Files.App.Launcher.exe"));
146-
var destFolder = await StorageFolder.GetFolderFromPathAsync(destFolderPath);
147-
148-
await srcExeFile.CopyAsync(destFolder, "Files.App.Launcher.exe", NameCollisionOption.ReplaceExisting);
149-
await srcHashFile.CopyAsync(destFolder, "Files.App.Launcher.exe.sha256", NameCollisionOption.ReplaceExisting);
150-
151-
Logger?.LogInformation("Files.App.Launcher updated.");
161+
try
162+
{
163+
var srcExeFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/FilesOpenDialog/Files.App.Launcher.exe"))
164+
.AsTask().ConfigureAwait(false);
165+
var destFolder = await StorageFolder.GetFolderFromPathAsync(destFolderPath).AsTask().ConfigureAwait(false);
166+
167+
await srcExeFile.CopyAsync(destFolder, "Files.App.Launcher.exe", NameCollisionOption.ReplaceExisting)
168+
.AsTask().ConfigureAwait(false);
169+
await srcHashFile.CopyAsync(destFolder, "Files.App.Launcher.exe.sha256", NameCollisionOption.ReplaceExisting)
170+
.AsTask().ConfigureAwait(false);
171+
172+
Logger?.LogInformation("Files.App.Launcher updated.");
173+
}
174+
catch (COMException ex)
175+
{
176+
Logger?.LogError(ex, ex.Message);
177+
return;
178+
}
179+
catch (UnauthorizedAccessException ex)
180+
{
181+
Logger?.LogError(ex, ex.Message);
182+
return;
183+
}
152184
}
153185
}
186+
catch (Exception ex)
187+
{
188+
Logger?.LogError(ex, ex.Message);
189+
return;
190+
}
154191

155192
bool HashEqual(Stream a, Stream b)
156193
{

0 commit comments

Comments
 (0)