diff --git a/Autoupdate/AppInstaller.m b/Autoupdate/AppInstaller.m index 859ce576..bcbf6439 100644 --- a/Autoupdate/AppInstaller.m +++ b/Autoupdate/AppInstaller.m @@ -403,14 +403,13 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; } // This installation path is specific to sparkle and the bundle identifier - NSString *cacheInstallationPath = [[SULocalCacheDirectory cachePathForBundleIdentifier:bundleIdentifier] stringByAppendingPathComponent:@"Installation"]; + NSString *rootCacheInstallationPath = [[SULocalCacheDirectory cachePathForBundleIdentifier:bundleIdentifier] stringByAppendingPathComponent:@"Installation"]; - SUFileManager *fileManager = [SUFileManager defaultManager]; - [fileManager removeItemAtURL:[NSURL fileURLWithPath:cacheInstallationPath] error:NULL]; + [SULocalCacheDirectory removeOldItemsInDirectory:rootCacheInstallationPath]; - NSError *createError = nil; - if (![[NSFileManager defaultManager] createDirectoryAtPath:cacheInstallationPath withIntermediateDirectories:YES attributes:nil error:&createError]) { - SULog(@"Error: Failed to create installation cache directory"); + NSString *cacheInstallationPath = [SULocalCacheDirectory createUniqueDirectoryInDirectory:rootCacheInstallationPath]; + if (cacheInstallationPath == nil) { + SULog(@"Error: Failed to create installation cache directory in %@", rootCacheInstallationPath); [self cleanupAndExitWithStatus:EXIT_FAILURE]; return; } @@ -423,7 +422,7 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; NSURL *downloadDestinationURL = [[NSURL fileURLWithPath:cacheInstallationPath] URLByAppendingPathComponent:installationData.downloadName]; NSError *moveError = nil; - if (![fileManager moveItemAtURL:downloadURL toURL:downloadDestinationURL error:&moveError]) { + if (![[SUFileManager defaultManager] moveItemAtURL:downloadURL toURL:downloadDestinationURL error:&moveError]) { SULog(@"Error: Failed to move download archive to new location: %@", moveError); [self cleanupAndExitWithStatus:EXIT_FAILURE]; return; diff --git a/InstallerLauncher/SUInstallerLauncher.m b/InstallerLauncher/SUInstallerLauncher.m index 2ac04f7d..dbb3448e 100644 --- a/InstallerLauncher/SUInstallerLauncher.m +++ b/InstallerLauncher/SUInstallerLauncher.m @@ -336,14 +336,13 @@ // they could be different (eg: take a look at sparkle-cli). We also can't easily tell if the signature of the service/framework is the same as the bundle it's inside. // The service/framework also need not even be signed in the first place. We'll just assume for now the original bundle hasn't been tampered with - NSString *launcherCachePath = [[SULocalCacheDirectory cachePathForBundleIdentifier:hostBundleIdentifier] stringByAppendingPathComponent:@"Launcher"]; + NSString *rootLauncherCachePath = [[SULocalCacheDirectory cachePathForBundleIdentifier:hostBundleIdentifier] stringByAppendingPathComponent:@"Launcher"]; - NSFileManager *fileManager = [NSFileManager defaultManager]; - [fileManager removeItemAtPath:launcherCachePath error:NULL]; + [SULocalCacheDirectory removeOldItemsInDirectory:rootLauncherCachePath]; - NSError *createCacheError = nil; - if (![fileManager createDirectoryAtPath:launcherCachePath withIntermediateDirectories:YES attributes:nil error:&createCacheError]) { - SULog(@"Failed to create cache directory for progress tool: %@", createCacheError); + NSString *launcherCachePath = [SULocalCacheDirectory createUniqueDirectoryInDirectory:rootLauncherCachePath]; + if (launcherCachePath == nil) { + SULog(@"Failed to create cache directory for progress tool in %@", rootLauncherCachePath); completionHandler(SUInstallerLauncherFailure); return; } diff --git a/PersistentDownloader/SUPersistentDownloader.m b/PersistentDownloader/SUPersistentDownloader.m index bd49c2e3..d0918421 100644 --- a/PersistentDownloader/SUPersistentDownloader.m +++ b/PersistentDownloader/SUPersistentDownloader.m @@ -83,22 +83,13 @@ static NSString *SUPersistentDownloadingReason = @"Downloading persistent file"; - (void)download:(NSURLDownload *)__unused d decideDestinationWithSuggestedFilename:(NSString *)name { - NSString *downloadFileName = self.desiredFilename; - // Remove our old caches path so we don't start accumulating files in there - NSString *persistentDownloadCachePath = [[SULocalCacheDirectory cachePathForBundleIdentifier:self.bundleIdentifier] stringByAppendingPathComponent:@"PersistentDownloads"]; - [[NSFileManager defaultManager] removeItemAtPath:persistentDownloadCachePath error:NULL]; + NSString *rootPersistentDownloadCachePath = [[SULocalCacheDirectory cachePathForBundleIdentifier:self.bundleIdentifier] stringByAppendingPathComponent:@"PersistentDownloads"]; - NSString *tempDir = [persistentDownloadCachePath stringByAppendingPathComponent:downloadFileName]; - int count = 1; - while ([[NSFileManager defaultManager] fileExistsAtPath:tempDir] && count <= 999) - { - tempDir = [persistentDownloadCachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@ %d", downloadFileName, count++]]; - } + [SULocalCacheDirectory removeOldItemsInDirectory:rootPersistentDownloadCachePath]; - // Create the temporary directory if necessary. - BOOL success = [[NSFileManager defaultManager] createDirectoryAtPath:tempDir withIntermediateDirectories:YES attributes:nil error:NULL]; - if (!success) + NSString *tempDir = [SULocalCacheDirectory createUniqueDirectoryInDirectory:rootPersistentDownloadCachePath]; + if (tempDir == nil) { // Okay, something's really broken with this user's file structure. [self.download cancel]; @@ -108,9 +99,19 @@ static NSString *SUPersistentDownloadingReason = @"Downloading persistent file"; [self.delegate downloaderDidFailWithError:error]; } else { - [self.download setDestination:[tempDir stringByAppendingPathComponent:name] allowOverwrite:YES]; + NSString *downloadFileName = self.desiredFilename; + NSString *downloadFileNameDirectory = [tempDir stringByAppendingPathComponent:downloadFileName]; - [self.delegate downloaderDidSetDestinationName:name temporaryDirectory:tempDir]; + NSError *createError = nil; + if (![[NSFileManager defaultManager] createDirectoryAtPath:downloadFileNameDirectory withIntermediateDirectories:NO attributes:nil error:&createError]) { + NSError *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUTemporaryDirectoryError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Can't make a download file name %@ directory inside temporary directory for the update download at %@.", downloadFileName, downloadFileNameDirectory] }]; + + [self.delegate downloaderDidFailWithError:error]; + } else { + [self.download setDestination:[downloadFileNameDirectory stringByAppendingPathComponent:name] allowOverwrite:YES]; + + [self.delegate downloaderDidSetDestinationName:name temporaryDirectory:downloadFileNameDirectory]; + } } } diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index f95036b6..2ed99c0d 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -122,6 +122,7 @@ 721D8A7B1D4963190032E472 /* SULocalCacheDirectory.m in Sources */ = {isa = PBXBuildFile; fileRef = 721BC20D1D1CDE55002BC71E /* SULocalCacheDirectory.m */; }; 721D8A811D498B9B0032E472 /* Updater.app in Copy Tools */ = {isa = PBXBuildFile; fileRef = 721C24451CB753E6005440CB /* Updater.app */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; 721D8A861D4ADFEB0032E472 /* SULocalCacheDirectory.m in Sources */ = {isa = PBXBuildFile; fileRef = 721BC20D1D1CDE55002BC71E /* SULocalCacheDirectory.m */; }; + 721D8A871D4C5BF10032E472 /* SULog.m in Sources */ = {isa = PBXBuildFile; fileRef = 55C14F05136EF6DB00649790 /* SULog.m */; }; 7223E7631AD1AEFF008E3161 /* sais.c in Sources */ = {isa = PBXBuildFile; fileRef = 7223E7611AD1AEFF008E3161 /* sais.c */; }; 7229E1B61C97C91100CB50D0 /* SUUpdateDriver.h in Headers */ = {isa = PBXBuildFile; fileRef = 7229E1B51C97C91100CB50D0 /* SUUpdateDriver.h */; }; 7229E1B91C97CC4D00CB50D0 /* SUScheduledUpdateDriver.h in Headers */ = {isa = PBXBuildFile; fileRef = 7229E1B71C97CC4D00CB50D0 /* SUScheduledUpdateDriver.h */; }; @@ -2669,6 +2670,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 721D8A871D4C5BF10032E472 /* SULog.m in Sources */, 7293A1B11CEE9B9F00B957A7 /* SUURLRequest.m in Sources */, 721BC2101D1CDE55002BC71E /* SULocalCacheDirectory.m in Sources */, 723B5DA71CF7AB0100365F95 /* main.m in Sources */, diff --git a/Sparkle/SULocalCacheDirectory.h b/Sparkle/SULocalCacheDirectory.h index 48f4fcc5..c202e470 100644 --- a/Sparkle/SULocalCacheDirectory.h +++ b/Sparkle/SULocalCacheDirectory.h @@ -17,9 +17,19 @@ NS_ASSUME_NONNULL_BEGIN // This path may depend on the type of running process, // such that sandboxed vs non-sandboxed processes could yield different paths // The caller should create a subdirectory from the path that is returned here so they don't have files that -// conflict with other callers +// conflict with other callers. Once that subdirectory name is decided, the caller can remove old items inside it (using +removeOldItemsInDirectory:) +// and then create a unique temporary directory inside it (using +createUniqueDirectoryInDirectory:) + (NSString *)cachePathForBundleIdentifier:(NSString *)bundleIdentifier; +// Remove old files inside a directory +// A caller may want to invoke this on a directory they own rather than remove and re-create an entire directory +// This does nothing if the supplied directory does not exist yet ++ (void)removeOldItemsInDirectory:(NSString *)directory; + +// Create a unique directory inside a parent directory +// The parent directory doesn't have to exist yet. If it doesn't exist, intermediate directories will be created. ++ (NSString * _Nullable)createUniqueDirectoryInDirectory:(NSString *)directory; + @end NS_ASSUME_NONNULL_END diff --git a/Sparkle/SULocalCacheDirectory.m b/Sparkle/SULocalCacheDirectory.m index 358a7127..7f94ae62 100644 --- a/Sparkle/SULocalCacheDirectory.m +++ b/Sparkle/SULocalCacheDirectory.m @@ -7,6 +7,9 @@ // #import "SULocalCacheDirectory.h" +#import "SULog.h" + +static NSTimeInterval OLD_ITEM_DELETION_INTERVAL = 86400 * 10; // 10 days @implementation SULocalCacheDirectory @@ -23,4 +26,52 @@ return resultPath; } ++ (void)removeOldItemsInDirectory:(NSString *)directory +{ + NSMutableArray *filePathsToRemove = [NSMutableArray array]; + NSFileManager *fileManager = [NSFileManager defaultManager]; + if ([fileManager fileExistsAtPath:directory]) { + NSDirectoryEnumerator *directoryEnumerator = [fileManager enumeratorAtPath:directory]; + NSDate *currentDate = [NSDate date]; + for (NSString *filename in directoryEnumerator) + { + NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:[directory stringByAppendingPathComponent:filename] error:NULL]; + if (fileAttributes != nil) + { + NSDate *lastModificationDate = fileAttributes[NSFileModificationDate]; + if ([currentDate timeIntervalSinceDate:lastModificationDate] >= OLD_ITEM_DELETION_INTERVAL) + { + [filePathsToRemove addObject:[directory stringByAppendingPathComponent:filename]]; + } + } + + [directoryEnumerator skipDescendants]; + } + + for (NSString *filename in filePathsToRemove) + { + [fileManager removeItemAtPath:filename error:NULL]; + } + } +} + ++ (NSString * _Nullable)createUniqueDirectoryInDirectory:(NSString *)directory +{ + NSFileManager *fileManager = [NSFileManager defaultManager]; + NSError *createError = nil; + if (![fileManager createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:&createError]) { + SULog(@"Failed to create directory with intermediate components at %@ with error %@", directory, createError); + return nil; + } + + NSString *templateString = [directory stringByAppendingPathComponent:@"XXXXXXXXX"]; + char buffer[PATH_MAX] = {0}; + if ([templateString getFileSystemRepresentation:buffer maxLength:sizeof(buffer)]) { + if (mkdtemp(buffer) != NULL) { + return [[NSString alloc] initWithUTF8String:buffer]; + } + } + return nil; +} + @end