diff --git a/Sparkle/SUBinaryDeltaApply.m b/Sparkle/SUBinaryDeltaApply.m index 2f35e50b..ae45a45e 100644 --- a/Sparkle/SUBinaryDeltaApply.m +++ b/Sparkle/SUBinaryDeltaApply.m @@ -141,12 +141,19 @@ int applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFil fprintf(stderr, "Unable to patch %s to destination %s\n", [sourceFilePath fileSystemRepresentation], [destinationFilePath fileSystemRepresentation]); return 1; } - } else { + } else if (xar_prop_get(file, "mod-permissions", &value)) { if (xar_extract_tofile(x, file, [destinationFilePath fileSystemRepresentation]) != 0) { fprintf(stderr, "Unable to extract file to %s\n", [destinationFilePath fileSystemRepresentation]); return 1; } } + + if (!xar_prop_get(file, "mod-permissions", &value)) { + if (!modifyPermissions(destinationFilePath, (mode_t)[[NSString stringWithUTF8String:value] intValue])) { + fprintf(stderr, "Unable to modify permissions (%s) on file %s\n", value, [destinationFilePath fileSystemRepresentation]); + return 1; + } + } } xar_close(x); diff --git a/Sparkle/SUBinaryDeltaCommon.h b/Sparkle/SUBinaryDeltaCommon.h index fb89ff78..8b0ed67e 100644 --- a/Sparkle/SUBinaryDeltaCommon.h +++ b/Sparkle/SUBinaryDeltaCommon.h @@ -41,6 +41,7 @@ extern NSString *hashOfTreeWithVersion(NSString *path, uint16_t majorVersion, ui extern NSString *hashOfTree(NSString *path); extern BOOL removeTree(NSString *path); extern BOOL copyTree(NSString *source, NSString *dest); +extern BOOL modifyPermissions(NSString *path, mode_t desiredPermissions); extern NSString *pathRelativeToDirectory(NSString *directory, NSString *path); NSString *temporaryFilename(NSString *base); NSString *temporaryDirectory(NSString *base); diff --git a/Sparkle/SUBinaryDeltaCommon.m b/Sparkle/SUBinaryDeltaCommon.m index 77045e8d..98f110bf 100644 --- a/Sparkle/SUBinaryDeltaCommon.m +++ b/Sparkle/SUBinaryDeltaCommon.m @@ -200,3 +200,21 @@ BOOL copyTree(NSString *source, NSString *dest) { return [[NSFileManager defaultManager] copyItemAtPath:source toPath:dest error:nil]; } + +BOOL modifyPermissions(NSString *path, mode_t desiredPermissions) +{ + NSFileManager *fileManager = [NSFileManager defaultManager]; + NSDictionary *attributes = [fileManager attributesOfItemAtPath:path error:nil]; + if (!attributes) { + return NO; + } + NSNumber *permissions = attributes[NSFilePosixPermissions]; + if (!permissions) { + return NO; + } + mode_t newMode = ([permissions unsignedShortValue] & ~PERMISSION_FLAGS) | desiredPermissions; + if (![fileManager setAttributes:@{NSFilePosixPermissions : @(newMode)} ofItemAtPath:path error:nil]) { + return NO; + } + return YES; +} diff --git a/Sparkle/SUBinaryDeltaCreate.m b/Sparkle/SUBinaryDeltaCreate.m index 1ac3ea8e..e2610454 100644 --- a/Sparkle/SUBinaryDeltaCreate.m +++ b/Sparkle/SUBinaryDeltaCreate.m @@ -25,21 +25,24 @@ extern int bsdiff(int argc, const char **argv); @interface CreateBinaryDeltaOperation : NSOperation @property (copy) NSString *relativePath; @property (strong) NSString *resultPath; +@property (strong) NSNumber *permissions; @property (strong) NSString *_fromPath; @property (strong) NSString *_toPath; -- (id)initWithRelativePath:(NSString *)relativePath oldTree:(NSString *)oldTree newTree:(NSString *)newTree; +- (id)initWithRelativePath:(NSString *)relativePath oldTree:(NSString *)oldTree newTree:(NSString *)newTree permissions:(NSNumber *)permissions; @end @implementation CreateBinaryDeltaOperation @synthesize relativePath = _relativePath; @synthesize resultPath = _resultPath; +@synthesize permissions = _permissions; @synthesize _fromPath = _fromPath; @synthesize _toPath = _toPath; -- (id)initWithRelativePath:(NSString *)relativePath oldTree:(NSString *)oldTree newTree:(NSString *)newTree +- (id)initWithRelativePath:(NSString *)relativePath oldTree:(NSString *)oldTree newTree:(NSString *)newTree permissions:(NSNumber *)permissions { if ((self = [super init])) { self.relativePath = relativePath; + self.permissions = permissions; self._fromPath = [oldTree stringByAppendingPathComponent:relativePath]; self._toPath = [newTree stringByAppendingPathComponent:relativePath]; } @@ -109,7 +112,8 @@ static BOOL shouldSkipDeltaCompression(NSDictionary* originalInfo, NSDictionary return YES; } - if ([originalInfo[INFO_PERMISSIONS_KEY] unsignedShortValue] != [newInfo[INFO_PERMISSIONS_KEY] unsignedShortValue]) { + if ([originalInfo[INFO_HASH_KEY] isEqual:newInfo[INFO_HASH_KEY]]) { + // this is possible if just the permissions have changed return YES; } @@ -125,14 +129,27 @@ static BOOL shouldDeleteThenExtract(NSDictionary* originalInfo, NSDictionary *ne if ([originalInfo[INFO_TYPE_KEY] unsignedShortValue] != [newInfo[INFO_TYPE_KEY] unsignedShortValue]) { return YES; } - - if ([originalInfo[INFO_PERMISSIONS_KEY] unsignedShortValue] != [newInfo[INFO_PERMISSIONS_KEY] unsignedShortValue]) { - return YES; - } return NO; } +static BOOL shouldChangePermissions(NSDictionary *originalInfo, NSDictionary *newInfo) +{ + if (!originalInfo) { + return NO; + } + + if ([originalInfo[INFO_TYPE_KEY] unsignedShortValue] != [newInfo[INFO_TYPE_KEY] unsignedShortValue]) { + return NO; + } + + if ([originalInfo[INFO_PERMISSIONS_KEY] unsignedShortValue] == [newInfo[INFO_PERMISSIONS_KEY] unsignedShortValue]) { + return NO; + } + + return YES; +} + int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFile) { NSMutableDictionary *originalTreeState = [NSMutableDictionary dictionary]; @@ -293,14 +310,22 @@ int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFi NSDictionary *originalInfo = originalTreeState[key]; NSDictionary *newInfo = newTreeState[key]; if (shouldSkipDeltaCompression(originalInfo, newInfo)) { - NSString *path = [destination stringByAppendingPathComponent:key]; - xar_file_t newFile = xar_add_frompath(x, 0, [key fileSystemRepresentation], [path fileSystemRepresentation]); - assert(newFile); - if (shouldDeleteThenExtract(originalInfo, newInfo)) { - xar_prop_set(newFile, "delete-then-extract", "true"); + if (shouldChangePermissions(originalInfo, newInfo)) { + xar_file_t newFile = xar_add_frombuffer(x, 0, [key fileSystemRepresentation], (char *)"", 1); + assert(newFile); + xar_prop_set(newFile, "mod-permissions", [[NSString stringWithFormat:@"%u", [newInfo[INFO_PERMISSIONS_KEY] unsignedShortValue]] UTF8String]); + } else { + NSString *path = [destination stringByAppendingPathComponent:key]; + xar_file_t newFile = xar_add_frompath(x, 0, [key fileSystemRepresentation], [path fileSystemRepresentation]); + assert(newFile); + + if (shouldDeleteThenExtract(originalInfo, newInfo)) { + xar_prop_set(newFile, "delete-then-extract", "true"); + } } } else { - CreateBinaryDeltaOperation *operation = [[CreateBinaryDeltaOperation alloc] initWithRelativePath:key oldTree:source newTree:destination]; + NSNumber *permissions = shouldChangePermissions(originalInfo, newInfo) ? newInfo[INFO_PERMISSIONS_KEY] : nil; + CreateBinaryDeltaOperation *operation = [[CreateBinaryDeltaOperation alloc] initWithRelativePath:key oldTree:source newTree:destination permissions:permissions]; [deltaQueue addOperation:operation]; [deltaOperations addObject:operation]; } @@ -318,6 +343,10 @@ int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFi assert(newFile); xar_prop_set(newFile, "binary-delta", "true"); unlink([resultPath fileSystemRepresentation]); + + if (operation.permissions) { + xar_prop_set(newFile, "mod-permissions", [[NSString stringWithFormat:@"%u", [operation.permissions unsignedShortValue]] UTF8String]); + } } xar_close(x);