From 301f3bf4e3351c8af2a178ad31f15e5a8045a7bd Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 26 Mar 2016 22:54:08 -0400 Subject: [PATCH] Move appCachePath out of SUHost for process independence The behavior depends on what kind of process is invoking is, which is no good. --- Sparkle/SUCoreBasedUpdateDriver.m | 33 +++++++++++++++++++++++++++++-- Sparkle/SUDownloadDriver.h | 2 +- Sparkle/SUDownloadDriver.m | 7 +++++-- Sparkle/SUHost.h | 1 - Sparkle/SUHost.m | 25 ++--------------------- Sparkle/SUInstallerDriver.h | 2 +- Sparkle/SUInstallerDriver.m | 7 +++++-- 7 files changed, 45 insertions(+), 32 deletions(-) diff --git a/Sparkle/SUCoreBasedUpdateDriver.m b/Sparkle/SUCoreBasedUpdateDriver.m index c62df9e2..cc87fdbf 100644 --- a/Sparkle/SUCoreBasedUpdateDriver.m +++ b/Sparkle/SUCoreBasedUpdateDriver.m @@ -12,6 +12,7 @@ #import "SUBasicUpdateDriver.h" #import "SUInstallerDriver.h" #import "SUDownloadDriver.h" +#import "SULog.h" #ifdef _APPKITDEFINES_H #error This is a "core" class and should NOT import AppKit @@ -44,13 +45,41 @@ @synthesize updaterDelegate = _updaterDelegate; @synthesize userAgent = _userAgent; +// If we support sandboxing this component in the future, it is important to note this may return a different path +// For this reason, this method should not be a part of SUHost because its behavior depends on what kind of process it's being invoked from ++ (NSString *)sparkleCachePathForHost:(SUHost *)host +{ + NSArray *cachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); + NSString *cachePath = nil; + if ([cachePaths count]) { + cachePath = [cachePaths objectAtIndex:0]; + } + if (!cachePath) { + SULog(@"Failed to find user's cache directory! Using system default"); + cachePath = NSTemporaryDirectory(); + } + + NSString *name = [host.bundle bundleIdentifier]; + if (!name) { + name = [host name]; + } + + cachePath = [cachePath stringByAppendingPathComponent:name]; + cachePath = [cachePath stringByAppendingPathComponent:@"Sparkle"]; + return cachePath; +} + - (instancetype)initWithHost:(SUHost *)host sparkleBundle:(NSBundle *)sparkleBundle updater:(id)updater updaterDelegate:(nullable id )updaterDelegate delegate:(id)delegate { self = [super init]; if (self != nil) { _delegate = delegate; + + NSString *cachePath = [[self class] sparkleCachePathForHost:host]; + _basicDriver = [[SUBasicUpdateDriver alloc] initWithHost:host updater:updater updaterDelegate:updaterDelegate delegate:self]; - _installerDriver = [[SUInstallerDriver alloc] initWithHost:host sparkleBundle:sparkleBundle updater:updater updaterDelegate:updaterDelegate delegate:self]; + _installerDriver = [[SUInstallerDriver alloc] initWithHost:host cachePath:cachePath sparkleBundle:sparkleBundle updater:updater updaterDelegate:updaterDelegate delegate:self]; + _host = host; _updater = updater; _updaterDelegate = updaterDelegate; @@ -86,7 +115,7 @@ - (void)downloadUpdateFromAppcastItem:(SUAppcastItem *)updateItem { - self.downloadDriver = [[SUDownloadDriver alloc] initWithUpdateItem:updateItem host:self.host userAgent:self.userAgent delegate:self]; + self.downloadDriver = [[SUDownloadDriver alloc] initWithUpdateItem:updateItem host:self.host cachePath:[[self class] sparkleCachePathForHost:self.host] userAgent:self.userAgent delegate:self]; if ([self.updaterDelegate respondsToSelector:@selector(updater:willDownloadUpdate:withRequest:)]) { [self.updaterDelegate updater:self.updater diff --git a/Sparkle/SUDownloadDriver.h b/Sparkle/SUDownloadDriver.h index 939d5d7e..127382fb 100644 --- a/Sparkle/SUDownloadDriver.h +++ b/Sparkle/SUDownloadDriver.h @@ -22,7 +22,7 @@ NS_ASSUME_NONNULL_BEGIN @interface SUDownloadDriver : NSObject -- (instancetype)initWithUpdateItem:(SUAppcastItem *)updateItem host:(SUHost *)host userAgent:(NSString *)userAgent delegate:(id)delegate; +- (instancetype)initWithUpdateItem:(SUAppcastItem *)updateItem host:(SUHost *)host cachePath:(NSString *)cachePath userAgent:(NSString *)userAgent delegate:(id)delegate; - (void)downloadUpdate; diff --git a/Sparkle/SUDownloadDriver.m b/Sparkle/SUDownloadDriver.m index 529587c3..251d6c9a 100644 --- a/Sparkle/SUDownloadDriver.m +++ b/Sparkle/SUDownloadDriver.m @@ -21,6 +21,7 @@ @property (nonatomic, readonly) SUAppcastItem *updateItem; @property (nonatomic, readonly) SUHost *host; +@property (nonatomic, readonly, copy) NSString *cachePath; @property (nonatomic) NSURLDownload *download; @property (nonatomic, copy) NSString *temporaryDirectory; @property (nonatomic, copy) NSString *downloadPath; @@ -33,17 +34,19 @@ @synthesize updateItem = _updateItem; @synthesize request = _request; @synthesize host = _host; +@synthesize cachePath = _cachePath; @synthesize download = _download; @synthesize temporaryDirectory = _temporaryDirectory; @synthesize downloadPath = _downloadPath; @synthesize delegate = _delegate; -- (instancetype)initWithUpdateItem:(SUAppcastItem *)updateItem host:(SUHost *)host userAgent:(NSString *)userAgent delegate:(id)delegate +- (instancetype)initWithUpdateItem:(SUAppcastItem *)updateItem host:(SUHost *)host cachePath:(NSString *)cachePath userAgent:(NSString *)userAgent delegate:(id)delegate { self = [super init]; if (self != nil) { _updateItem = updateItem; _host = host; + _cachePath = [cachePath copy]; _delegate = delegate; _request = [NSMutableURLRequest requestWithURL:updateItem.fileURL]; @@ -94,7 +97,7 @@ { NSString *downloadFileName = [NSString stringWithFormat:@"%@ %@", [self.host name], [self.updateItem versionString]]; - NSString *cachePath = [self.host appCachePath]; + NSString *cachePath = self.cachePath; NSString *tempDir = [cachePath stringByAppendingPathComponent:downloadFileName]; int count = 1; while ([[NSFileManager defaultManager] fileExistsAtPath:tempDir] && count <= 999) diff --git a/Sparkle/SUHost.h b/Sparkle/SUHost.h index 7ef4836b..364851bb 100644 --- a/Sparkle/SUHost.h +++ b/Sparkle/SUHost.h @@ -15,7 +15,6 @@ @property (readonly, copy) NSString *bundlePath; // Even if automatic updates are allowed, this does not indicate that the user has writable permissions to do so conveniently @property (readonly) BOOL allowsAutomaticUpdates; -@property (readonly, copy) NSString *appCachePath; @property (readonly, copy) NSString *installationPath; @property (readonly, copy) NSString *name; @property (readonly, copy) NSString *version; diff --git a/Sparkle/SUHost.m b/Sparkle/SUHost.m index 0df9257a..a5c0d0ba 100644 --- a/Sparkle/SUHost.m +++ b/Sparkle/SUHost.m @@ -20,7 +20,8 @@ // This class should also be process independent // For example, it should not have code that tests writabilty to somewhere on disk, -// as that may depend on the privileges of the process owner +// as that may depend on the privileges of the process owner. Or code that depends on +// if the process is sandboxed or not; eg: finding user caches directory @interface SUHost () @@ -74,28 +75,6 @@ return (developerAllowsAutomaticUpdates == nil || developerAllowsAutomaticUpdates.boolValue); } -- (NSString *)appCachePath -{ - NSArray *cachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); - NSString *cachePath = nil; - if ([cachePaths count]) { - cachePath = [cachePaths objectAtIndex:0]; - } - if (!cachePath) { - SULog(@"Failed to find user's cache directory! Using system default"); - cachePath = NSTemporaryDirectory(); - } - - NSString *name = [self.bundle bundleIdentifier]; - if (!name) { - name = [self name]; - } - - cachePath = [cachePath stringByAppendingPathComponent:name]; - cachePath = [cachePath stringByAppendingPathComponent:@"Sparkle"]; - return cachePath; -} - - (NSString *)installationPath { if (SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME) { diff --git a/Sparkle/SUInstallerDriver.h b/Sparkle/SUInstallerDriver.h index 2e3f834e..97e82fb6 100644 --- a/Sparkle/SUInstallerDriver.h +++ b/Sparkle/SUInstallerDriver.h @@ -27,7 +27,7 @@ NS_ASSUME_NONNULL_BEGIN @interface SUInstallerDriver : NSObject -- (instancetype)initWithHost:(SUHost *)host sparkleBundle:(NSBundle *)sparkleBundle updater:(id)updater updaterDelegate:(nullable id)updaterDelegate delegate:(nullable id)delegate; +- (instancetype)initWithHost:(SUHost *)host cachePath:(NSString *)cachePath sparkleBundle:(NSBundle *)sparkleBundle updater:(id)updater updaterDelegate:(nullable id)updaterDelegate delegate:(nullable id)delegate; - (BOOL)extractDownloadPath:(NSString *)downloadPath withUpdateItem:(SUAppcastItem *)updateItem temporaryDirectory:(NSString *)temporaryDirectory error:(NSError * __autoreleasing *)error; diff --git a/Sparkle/SUInstallerDriver.m b/Sparkle/SUInstallerDriver.m index 556019e3..f07cc8a5 100644 --- a/Sparkle/SUInstallerDriver.m +++ b/Sparkle/SUInstallerDriver.m @@ -28,6 +28,7 @@ @interface SUInstallerDriver () @property (nonatomic, readonly) SUHost *host; +@property (nonatomic, readonly, copy) NSString *cachePath; @property (nonatomic, readonly) NSBundle *sparkleBundle; @property (nonatomic, weak, readonly) id delegate; @property (nonatomic) SUInstallerMessageType currentStage; @@ -46,6 +47,7 @@ @implementation SUInstallerDriver @synthesize host = _host; +@synthesize cachePath = _cachePath; @synthesize sparkleBundle = _sparkleBundle; @synthesize delegate = _delegate; @synthesize currentStage = _currentStage; @@ -58,11 +60,12 @@ @synthesize downloadPath = _downloadPath; @synthesize temporaryDirectory = _temporaryDirectory; -- (instancetype)initWithHost:(SUHost *)host sparkleBundle:(NSBundle *)sparkleBundle updater:(id)updater updaterDelegate:(id)updaterDelegate delegate:(nullable id)delegate +- (instancetype)initWithHost:(SUHost *)host cachePath:(NSString *)cachePath sparkleBundle:(NSBundle *)sparkleBundle updater:(id)updater updaterDelegate:(id)updaterDelegate delegate:(nullable id)delegate { self = [super init]; if (self != nil) { _host = host; + _cachePath = [cachePath copy]; _sparkleBundle = sparkleBundle; _updater = updater; _updaterDelegate = updaterDelegate; @@ -330,7 +333,7 @@ NSString *const relaunchToolName = @"" SPARKLE_RELAUNCH_TOOL_NAME; NSString *const relaunchPathToCopy = [sparkleBundle pathForResource:relaunchToolName ofType:@"app"]; if (relaunchPathToCopy != nil) { - NSString *targetPath = [self.host.appCachePath stringByAppendingPathComponent:[relaunchPathToCopy lastPathComponent]]; + NSString *targetPath = [self.cachePath stringByAppendingPathComponent:[relaunchPathToCopy lastPathComponent]]; SUFileManager *fileManager = [SUFileManager fileManagerAllowingAuthorization:NO]; NSError *error = nil;