@@ -15,20 +15,22 @@ internal class DesktopClientUpdater(
1515 IControlrMutationLock mutationLock ,
1616 IProcessManager processManager ,
1717 IDesktopSessionProvider desktopSessionProvider ,
18+ IFileSystemPathProvider fileSystemPathProvider ,
1819 IEmbeddedDesktopClientProvider embeddedDesktopClientProvider ,
1920 ILogger < DesktopClientUpdater > logger ) : BackgroundService , IDesktopClientUpdater
2021{
2122 private readonly IDesktopSessionProvider _desktopSessionProvider = desktopSessionProvider ;
2223 private readonly IEmbeddedDesktopClientProvider _embeddedDesktopClientProvider = embeddedDesktopClientProvider ;
2324 private readonly IFileSystem _fileSystem = fileSystem ;
25+ private readonly IFileSystemPathProvider _fileSystemPathProvider = fileSystemPathProvider ;
2426 private readonly ILogger < DesktopClientUpdater > _logger = logger ;
2527 private readonly IControlrMutationLock _mutationLock = mutationLock ;
2628 private readonly IProcessManager _processManager = processManager ;
2729 private readonly IServiceControl _serviceControl = serviceControl ;
2830 private readonly ISettingsProvider _settings = settings ;
2931 private readonly ISystemEnvironment _systemEnvironment = systemEnvironment ;
3032 private readonly SemaphoreSlim _updateLock = new ( 1 , 1 ) ;
31-
33+
3234 public async Task < bool > EnsureLatestVersion (
3335 bool acquireGlobalLock ,
3436 CancellationToken cancellationToken )
@@ -49,8 +51,8 @@ public async Task<bool> EnsureLatestVersion(
4951 _logger . LogInformation ( "Ensuring latest version of desktop client." ) ;
5052
5153 var startupDir = _systemEnvironment . StartupDirectory ;
52- var desktopDir = Path . Combine ( startupDir , "DesktopClient" ) ;
53- var binaryPath = AppConstants . GetDesktopExecutablePath ( startupDir ) ;
54+ var desktopDir = _fileSystemPathProvider . GetDesktopClientDirectory ( ) ;
55+ var binaryPath = _fileSystemPathProvider . GetDesktopExecutablePath ( ) ;
5456 var extractedZipPath = Path . Combine ( startupDir , AppConstants . DesktopClientZipFileName ) ;
5557
5658 // 1) Compute embedded zip hash
@@ -174,7 +176,7 @@ private async Task<bool> ExtractDesktopClient(string desktopDir)
174176
175177 _fileSystem . ExtractZipArchive ( targetPath , desktopDir , true ) ;
176178
177- await SetDesktopClientPermissions ( desktopDir ) ;
179+ await SetDesktopClientFileAttributes ( desktopDir ) ;
178180
179181 if ( _systemEnvironment . Platform == SystemPlatform . MacOs || _systemEnvironment . Platform == SystemPlatform . Linux )
180182 {
@@ -205,14 +207,14 @@ private async Task<Result<string>> GetEmbeddedZipHash(CancellationToken cancella
205207
206208 private async Task < string ? > GetFileHash ( string path , CancellationToken cancellationToken )
207209 {
208- if ( ! _fileSystem . FileExists ( path ) )
209- {
210- _logger . LogInformation ( "Extracted desktop client ZIP not found. Update required." ) ;
211- return null ;
212- }
213-
214210 try
215211 {
212+ if ( ! _fileSystem . FileExists ( path ) )
213+ {
214+ _logger . LogInformation ( "Extracted desktop client ZIP not found. Update required." ) ;
215+ return null ;
216+ }
217+
216218 await using var fileStream = _fileSystem . OpenFileStream ( path , FileMode . Open , FileAccess . Read , FileShare . Read ) ;
217219 var hashBytes = await SHA256 . HashDataAsync ( fileStream , cancellationToken ) ;
218220 var extractedHash = Convert . ToHexString ( hashBytes ) ;
@@ -226,41 +228,47 @@ private async Task<Result<string>> GetEmbeddedZipHash(CancellationToken cancella
226228 }
227229 }
228230
229-
230231 private bool IsUpToDate ( string ? extractedHash , string embeddedHash , string binaryPath )
231232 {
232233 return ! string . IsNullOrWhiteSpace ( extractedHash )
233234 && string . Equals ( extractedHash , embeddedHash , StringComparison . OrdinalIgnoreCase )
234235 && _fileSystem . FileExists ( binaryPath ) ;
235236 }
236237
237- private async Task SetDesktopClientPermissions ( string desktopDir )
238+ private async Task SetDesktopClientFileAttributes ( string desktopDir )
238239 {
239- if ( _systemEnvironment . Platform == SystemPlatform . MacOs )
240+ try
240241 {
241- // Ensure the Mac app bundle executable has correct permissions
242- var appBundleExecutablePath = Path . Combine ( desktopDir , "ControlR.app" , "Contents" , "MacOS" , "ControlR.DesktopClient" ) ;
243- if ( _fileSystem . FileExists ( appBundleExecutablePath ) )
242+ if ( _systemEnvironment . Platform == SystemPlatform . MacOs )
244243 {
245- var chmodResult = await _processManager . GetProcessOutput ( "chmod" , "+x " + appBundleExecutablePath , 5000 ) ;
246- if ( ! chmodResult . IsSuccess )
244+ // Ensure the Mac app bundle executable has correct file attributes.
245+ var appBundleExecutablePath = Path . Combine ( desktopDir , "ControlR.app" , "Contents" , "MacOS" , "ControlR.DesktopClient" ) ;
246+ if ( _fileSystem . FileExists ( appBundleExecutablePath ) )
247247 {
248- _logger . LogWarning ( "Failed to set executable permissions on Mac app bundle: {Error}" , chmodResult . Reason ) ;
248+ var chmodResult = await _processManager . GetProcessOutput ( "chmod" , "+x " + appBundleExecutablePath , 5000 ) ;
249+ if ( ! chmodResult . IsSuccess )
250+ {
251+ _logger . LogWarning ( "Failed to set executable permissions on Mac app bundle: {Error}" , chmodResult . Reason ) ;
252+ }
249253 }
250254 }
251- }
252- else if ( _systemEnvironment . Platform == SystemPlatform . Linux )
253- {
254- // Ensure the Linux executable has correct permissions
255- var linuxExecutablePath = Path . Combine ( desktopDir , AppConstants . DesktopClientFileName ) ;
256- if ( _fileSystem . FileExists ( linuxExecutablePath ) )
255+ else if ( _systemEnvironment . Platform == SystemPlatform . Linux )
257256 {
258- var chmodResult = await _processManager . GetProcessOutput ( "chmod" , "+x " + linuxExecutablePath , 5000 ) ;
259- if ( ! chmodResult . IsSuccess )
257+ // Ensure the Linux executable has correct file attributes.
258+ var linuxExecutablePath = Path . Combine ( desktopDir , AppConstants . DesktopClientFileName ) ;
259+ if ( _fileSystem . FileExists ( linuxExecutablePath ) )
260260 {
261- _logger . LogWarning ( "Failed to set executable permissions on Linux executable: {Error}" , chmodResult . Reason ) ;
261+ var chmodResult = await _processManager . GetProcessOutput ( "chmod" , "+x " + linuxExecutablePath , 5000 ) ;
262+ if ( ! chmodResult . IsSuccess )
263+ {
264+ _logger . LogWarning ( "Failed to set executable file attributes on Linux executable: {Error}" , chmodResult . Reason ) ;
265+ }
262266 }
263267 }
264268 }
269+ catch ( Exception ex )
270+ {
271+ _logger . LogError ( ex , "Error setting desktop client file attributes." ) ;
272+ }
265273 }
266274}
0 commit comments