Skip to content

Commit 2723726

Browse files
committed
Code Quality: Refactor launcher update logic and remove hash check
Removed SHA256 hash file and related hash comparison logic from Files.App.Launcher update process. Now uses a branch file (Branch.txt) to determine if the launcher should be updated, simplifying the update mechanism. Updated PowerShell command to ensure Branch.txt is created during setup. This streamlines the update process and improves maintainability.
1 parent 311a0d4 commit 2723726

File tree

6 files changed

+33
-70
lines changed

6 files changed

+33
-70
lines changed

src/Files.App.Launcher/Files.App.Launcher.vcxproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@
6666
</Link>
6767
<PostBuildEvent>
6868
<Command>
69-
xcopy /s /y "$(ProjectDir)$(OutDir)*.exe" "$(ProjectDir)..\..\src\Files.App\Assets\FilesOpenDialog"
70-
certutil -hashfile "$(ProjectDir)..\..\src\Files.App\Assets\FilesOpenDialog\$(TargetFileName)" SHA256|findstr /R /V "^SHA256 ^CertUtil"&gt;"$(ProjectDir)..\..\src\Files.App\Assets\FilesOpenDialog\$(TargetFileName).sha256"</Command>
69+
xcopy /s /y "$(ProjectDir)$(OutDir)*.exe" "$(ProjectDir)..\..\src\Files.App\Assets\FilesOpenDialog"</Command>
7170
</PostBuildEvent>
7271
</ItemDefinitionGroup>
7372
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">

src/Files.App/Assets/FilesOpenDialog/Files.App.Launcher.exe.sha256

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/Files.App/Helpers/Application/AppLifecycleHelper.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(async () =>
169169

170170
await updateService.CheckForUpdatesAsync();
171171
await updateService.DownloadMandatoryUpdatesAsync();
172-
await updateService.CheckAndUpdateFilesLauncherAsync();
172+
173+
if (IsAppUpdated)
174+
await updateService.CheckAndUpdateFilesLauncherAsync();
173175
}
174176

175177
/// <summary>

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

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.Extensions.Logging;
55
using System.IO;
66
using System.Net.Http;
7+
using System.Text;
78
using System.Xml.Serialization;
89
using Windows.ApplicationModel;
910
using Windows.Management.Deployment;
@@ -125,52 +126,31 @@ public async Task CheckAndUpdateFilesLauncherAsync()
125126
{
126127
var destFolderPath = Path.Combine(UserDataPaths.GetDefault().LocalAppData, "Files");
127128
var destExeFilePath = Path.Combine(destFolderPath, "Files.App.Launcher.exe");
129+
var branchFilePath = Path.Combine(destFolderPath, "Branch.txt");
128130

131+
// If Files.App.Launcher.exe doesn't exist, no need to update it.
129132
if (!File.Exists(destExeFilePath))
130133
return;
131134

132-
var hashEqual = false;
133-
var srcHashFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/FilesOpenDialog/Files.App.Launcher.exe.sha256"))
134-
.AsTask().ConfigureAwait(false);
135-
var destHashFilePath = Path.Combine(destFolderPath, "Files.App.Launcher.exe.sha256");
136-
137-
if (File.Exists(destHashFilePath))
138-
{
139-
await using var srcStream = (await srcHashFile.OpenReadAsync().AsTask().ConfigureAwait(false)).AsStream();
140-
await using var destStream = File.OpenRead(destHashFilePath);
141-
hashEqual = HashEqual(srcStream, destStream);
142-
}
135+
// Check if the launcher file is associated with the current branch of the app. Users updating from versions earlier than
136+
// v4.0.20 will not have the branch file in which case we create it for them.
137+
if (File.Exists(branchFilePath) && await File.ReadAllTextAsync(branchFilePath, Encoding.UTF8) != "files-dev")
138+
return;
139+
else if (!File.Exists(branchFilePath))
140+
await File.WriteAllTextAsync(branchFilePath, "files-dev", Encoding.UTF8);
143141

144-
if (!hashEqual)
145-
{
146-
var srcExeFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/FilesOpenDialog/Files.App.Launcher.exe"))
147-
.AsTask().ConfigureAwait(false);
148-
var destFolder = await StorageFolder.GetFolderFromPathAsync(destFolderPath).AsTask().ConfigureAwait(false);
142+
var srcExeFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/FilesOpenDialog/Files.App.Launcher.exe"));
143+
var destFolder = await StorageFolder.GetFolderFromPathAsync(destFolderPath);
149144

150-
await srcExeFile.CopyAsync(destFolder, "Files.App.Launcher.exe", NameCollisionOption.ReplaceExisting)
151-
.AsTask().ConfigureAwait(false);
152-
await srcHashFile.CopyAsync(destFolder, "Files.App.Launcher.exe.sha256", NameCollisionOption.ReplaceExisting)
153-
.AsTask().ConfigureAwait(false);
145+
await srcExeFile.CopyAsync(destFolder, "Files.App.Launcher.exe", NameCollisionOption.ReplaceExisting);
154146

155-
Logger?.LogInformation("Files.App.Launcher updated.");
156-
}
147+
App.Logger.LogInformation("Files.App.Launcher updated.");
157148
}
158149
catch (Exception ex)
159150
{
160151
Logger?.LogError(ex, ex.Message);
161152
return;
162153
}
163-
164-
bool HashEqual(Stream a, Stream b)
165-
{
166-
Span<byte> bufferA = stackalloc byte[64];
167-
Span<byte> bufferB = stackalloc byte[64];
168-
169-
a.Read(bufferA);
170-
b.Read(bufferB);
171-
172-
return bufferA.SequenceEqual(bufferB);
173-
}
174154
}
175155

176156
public async Task CheckForReleaseNotesAsync()

src/Files.App/Services/App/AppUpdateStoreService.cs

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.UI.Xaml.Controls;
66
using System.IO;
77
using System.Net.Http;
8+
using System.Text;
89
using Windows.Foundation.Metadata;
910
using Windows.Services.Store;
1011
using Windows.Storage;
@@ -180,43 +181,25 @@ public async Task CheckAndUpdateFilesLauncherAsync()
180181
{
181182
var destFolderPath = Path.Combine(UserDataPaths.GetDefault().LocalAppData, "Files");
182183
var destExeFilePath = Path.Combine(destFolderPath, "Files.App.Launcher.exe");
184+
var branchFilePath = Path.Combine(destFolderPath, "Branch.txt");
183185

184-
if (Path.Exists(destExeFilePath))
185-
{
186-
var hashEqual = false;
187-
var srcHashFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/FilesOpenDialog/Files.App.Launcher.exe.sha256"));
188-
var destHashFilePath = Path.Combine(destFolderPath, "Files.App.Launcher.exe.sha256");
189-
190-
if (Path.Exists(destHashFilePath))
191-
{
192-
await using var srcStream = (await srcHashFile.OpenReadAsync()).AsStream();
193-
await using var destStream = File.OpenRead(destHashFilePath);
194-
195-
hashEqual = HashEqual(srcStream, destStream);
196-
}
197-
198-
if (!hashEqual)
199-
{
200-
var srcExeFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/FilesOpenDialog/Files.App.Launcher.exe"));
201-
var destFolder = await StorageFolder.GetFolderFromPathAsync(destFolderPath);
202-
203-
await srcExeFile.CopyAsync(destFolder, "Files.App.Launcher.exe", NameCollisionOption.ReplaceExisting);
204-
await srcHashFile.CopyAsync(destFolder, "Files.App.Launcher.exe.sha256", NameCollisionOption.ReplaceExisting);
186+
// If Files.App.Launcher.exe doesn't exist, no need to update it.
187+
if (!File.Exists(destExeFilePath))
188+
return;
205189

206-
App.Logger.LogInformation("Files.App.Launcher updated.");
207-
}
208-
}
190+
// Check if the launcher file is associated with the current branch of the app. Users updating from versions earlier than
191+
// v4.0.20 will not have the branch file in which case we create it for them.
192+
if (File.Exists(branchFilePath) && await File.ReadAllTextAsync(branchFilePath, Encoding.UTF8) != "files-dev")
193+
return;
194+
else if (!File.Exists(branchFilePath))
195+
await File.WriteAllTextAsync(branchFilePath, "files-dev", Encoding.UTF8);
209196

210-
bool HashEqual(Stream a, Stream b)
211-
{
212-
Span<byte> bufferA = stackalloc byte[64];
213-
Span<byte> bufferB = stackalloc byte[64];
197+
var srcExeFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/FilesOpenDialog/Files.App.Launcher.exe"));
198+
var destFolder = await StorageFolder.GetFolderFromPathAsync(destFolderPath);
214199

215-
a.Read(bufferA);
216-
b.Read(bufferB);
200+
await srcExeFile.CopyAsync(destFolder, "Files.App.Launcher.exe", NameCollisionOption.ReplaceExisting);
217201

218-
return bufferA.SequenceEqual(bufferB);
219-
}
202+
App.Logger.LogInformation("Files.App.Launcher updated.");
220203
}
221204

222205
private bool HasUpdates()

src/Files.App/ViewModels/Settings/AdvancedViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private async Task SetAsDefaultExplorerAsync()
6868
var dataPath = Environment.ExpandEnvironmentVariables("%LocalAppData%\\Files");
6969
if (IsSetAsDefaultFileManager)
7070
{
71-
if (!await Win32Helper.RunPowershellCommandAsync($"-command \"New-Item -Force -Path '{dataPath}' -ItemType Directory; Copy-Item -Filter *.* -Path '{destFolder}\\*' -Recurse -Force -Destination '{dataPath}'\"", PowerShellExecutionOptions.Hidden))
71+
if (!await Win32Helper.RunPowershellCommandAsync($"-command \"New-Item -Force -Path '{dataPath}' -ItemType Directory; Copy-Item -Filter *.* -Path '{destFolder}\\*' -Recurse -Force -Destination '{dataPath}'; 'files-dev' | Out-File -Encoding utf8 -Force -FilePath '{dataPath}\\Branch.txt'\"", PowerShellExecutionOptions.Hidden))
7272
{
7373
// Error copying files
7474
await DetectResult();

0 commit comments

Comments
 (0)