Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix: Fixed crash that would sometimes occur when updating #16976

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Files.App (Package)/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<Identity
Name="FilesDev"
Publisher="CN=Files"
Version="3.9.2.0" />
Version="3.9.3.0" />

<Properties>
<DisplayName>Files - Dev</DisplayName>
Expand Down
71 changes: 54 additions & 17 deletions src/Files.App/Services/App/AppUpdateSideloadService.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright (c) Files Community
// Licensed under the MIT License.

using CommunityToolkit.WinUI.Helpers;
using Microsoft.Extensions.Logging;
using System.IO;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Xml.Serialization;
using Windows.ApplicationModel;
using Windows.Management.Deployment;
Expand Down Expand Up @@ -123,34 +123,71 @@ public async Task CheckForUpdatesAsync()

public async Task CheckAndUpdateFilesLauncherAsync()
{
var destFolderPath = Path.Combine(UserDataPaths.GetDefault().LocalAppData, "Files");
var destExeFilePath = Path.Combine(destFolderPath, "Files.App.Launcher.exe");

if (File.Exists(destExeFilePath))
try
{
var destFolderPath = Path.Combine(UserDataPaths.GetDefault().LocalAppData, "Files");
var destExeFilePath = Path.Combine(destFolderPath, "Files.App.Launcher.exe");

if (!File.Exists(destExeFilePath))
return;

var hashEqual = false;
var srcHashFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/FilesOpenDialog/Files.App.Launcher.exe.sha256"));
var srcHashFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/FilesOpenDialog/Files.App.Launcher.exe.sha256"))
.AsTask().ConfigureAwait(false);
var destHashFilePath = Path.Combine(destFolderPath, "Files.App.Launcher.exe.sha256");

if (File.Exists(destHashFilePath))
{
await using var srcStream = (await srcHashFile.OpenReadAsync()).AsStream();
await using var destStream = File.OpenRead(destHashFilePath);

hashEqual = HashEqual(srcStream, destStream);
try
{
await using var srcStream = (await srcHashFile.OpenReadAsync().AsTask().ConfigureAwait(false)).AsStream();
await using var destStream = File.OpenRead(destHashFilePath);
hashEqual = HashEqual(srcStream, destStream);
}
catch (COMException ex)
{
Logger?.LogWarning(ex, "Failed to compare hash files");
return;
}
catch (IOException ex)
{
Logger?.LogWarning(ex, "IO error while reading hash files");
return;
}
}

if (!hashEqual)
{
var srcExeFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/FilesOpenDialog/Files.App.Launcher.exe"));
var destFolder = await StorageFolder.GetFolderFromPathAsync(destFolderPath);

await srcExeFile.CopyAsync(destFolder, "Files.App.Launcher.exe", NameCollisionOption.ReplaceExisting);
await srcHashFile.CopyAsync(destFolder, "Files.App.Launcher.exe.sha256", NameCollisionOption.ReplaceExisting);

Logger?.LogInformation("Files.App.Launcher updated.");
try
{
var srcExeFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/FilesOpenDialog/Files.App.Launcher.exe"))
.AsTask().ConfigureAwait(false);
var destFolder = await StorageFolder.GetFolderFromPathAsync(destFolderPath).AsTask().ConfigureAwait(false);

await srcExeFile.CopyAsync(destFolder, "Files.App.Launcher.exe", NameCollisionOption.ReplaceExisting)
.AsTask().ConfigureAwait(false);
await srcHashFile.CopyAsync(destFolder, "Files.App.Launcher.exe.sha256", NameCollisionOption.ReplaceExisting)
.AsTask().ConfigureAwait(false);

Logger?.LogInformation("Files.App.Launcher updated.");
}
catch (COMException ex)
Copy link
Preview

Copilot AI Mar 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider handling IOException during the file copy operation as well (similar to the hash file comparison block) to ensure all potential I/O errors are caught during the copy process.

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

{
Logger?.LogError(ex, ex.Message);
return;
}
catch (UnauthorizedAccessException ex)
{
Logger?.LogError(ex, ex.Message);
return;
}
}
}
catch (Exception ex)
{
Logger?.LogError(ex, ex.Message);
return;
}

bool HashEqual(Stream a, Stream b)
{
Expand Down
Loading