From 3bd1b2551305bbd0e6134784bc9f6d3e29233ca5 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 9 Jan 2022 18:20:50 -0800 Subject: [PATCH] Simplify tracking of file permissions for deltas (#2057) Also fixes minor issue where change in permissions wasn't printed in correct format when applying a patch in verbose mode. --- Autoupdate/SPUDeltaArchive.m | 7 +++---- Autoupdate/SPUDeltaArchiveProtocol.h | 8 +++----- Autoupdate/SPUSparkleDeltaArchive.m | 23 +++++++++++------------ Autoupdate/SPUXarDeltaArchive.m | 10 +++++----- Autoupdate/SUBinaryDeltaApply.m | 4 ++-- Autoupdate/SUBinaryDeltaCreate.m | 12 ++++++------ 6 files changed, 30 insertions(+), 34 deletions(-) diff --git a/Autoupdate/SPUDeltaArchive.m b/Autoupdate/SPUDeltaArchive.m index 940b27ce..232bdda0 100644 --- a/Autoupdate/SPUDeltaArchive.m +++ b/Autoupdate/SPUDeltaArchive.m @@ -56,18 +56,17 @@ id SPUDeltaArchiveReadPatchAndHeader(NSString *patchFil @synthesize clonedRelativePath = _clonedRelativePath; @synthesize sourcePath = _sourcePath; @synthesize commands = _commands; -@synthesize permissions = _permissions; @synthesize xarContext = _xarContext; -@synthesize extractMode = _extractMode; +@synthesize mode = _mode; @synthesize codedDataLength = _codedDataLength; -- (instancetype)initWithRelativeFilePath:(NSString *)relativeFilePath commands:(SPUDeltaItemCommands)commands permissions:(uint16_t)permissions +- (instancetype)initWithRelativeFilePath:(NSString *)relativeFilePath commands:(SPUDeltaItemCommands)commands mode:(uint16_t)mode { self = [super init]; if (self != nil) { _relativeFilePath = [relativeFilePath copy]; _commands = commands; - _permissions = permissions; + _mode = mode; } return self; } diff --git a/Autoupdate/SPUDeltaArchiveProtocol.h b/Autoupdate/SPUDeltaArchiveProtocol.h index ad68ba12..b05bd931 100644 --- a/Autoupdate/SPUDeltaArchiveProtocol.h +++ b/Autoupdate/SPUDeltaArchiveProtocol.h @@ -40,7 +40,7 @@ typedef NS_ENUM(uint8_t, SPUDeltaItemCommands) { // Represents an item we read or write to in our delta archive @interface SPUDeltaArchiveItem : NSObject -- (instancetype)initWithRelativeFilePath:(NSString *)relativeFilePath commands:(SPUDeltaItemCommands)commands permissions:(uint16_t)permissions; +- (instancetype)initWithRelativeFilePath:(NSString *)relativeFilePath commands:(SPUDeltaItemCommands)commands mode:(uint16_t)mode; // The relative file path of the item, eg /Contents/MacOS/Foo @property (nonatomic, readonly) NSString *relativeFilePath; @@ -53,14 +53,12 @@ typedef NS_ENUM(uint8_t, SPUDeltaItemCommands) { @property (nonatomic, nullable) NSString *sourcePath; // The commands that describe the actions to take for this item. @property (nonatomic, readonly) SPUDeltaItemCommands commands; -// Provided change in permissions for the SPUDeltaItemCommandModifyPermissions command -@property (nonatomic, readonly) uint16_t permissions; +// Provided change in permissions for item or tracking file mode for the item +@property (nonatomic) uint16_t mode; // Private properties // xar_file context for Xar delta archiver @property (nonatomic, nullable) const void *xarContext; -// Tracking mode type of item (regular file, symlink) when encoding items. -@property (nonatomic) uint16_t extractMode; // Tracking length of item's data in data section, when encoding items and when extracting items @property (nonatomic) uint64_t codedDataLength; diff --git a/Autoupdate/SPUSparkleDeltaArchive.m b/Autoupdate/SPUSparkleDeltaArchive.m index 431973f3..e2a04570 100644 --- a/Autoupdate/SPUSparkleDeltaArchive.m +++ b/Autoupdate/SPUSparkleDeltaArchive.m @@ -507,7 +507,7 @@ static compression_algorithm _compressionAlgorithmForMode(SPUDeltaCompressionMod } } - SPUDeltaArchiveItem *archiveItem = [[SPUDeltaArchiveItem alloc] initWithRelativeFilePath:relativeFilePaths[currentItemIndex] commands:commands permissions:decodedMode]; + SPUDeltaArchiveItem *archiveItem = [[SPUDeltaArchiveItem alloc] initWithRelativeFilePath:relativeFilePaths[currentItemIndex] commands:commands mode:decodedMode]; archiveItem.codedDataLength = decodedDataLength; archiveItem.clonedRelativePath = clonedRelativePath; @@ -540,7 +540,7 @@ static compression_algorithm _compressionAlgorithmForMode(SPUDeltaCompressionMod SPUDeltaItemCommands commands = item.commands; assert((commands & SPUDeltaItemCommandExtract) != 0 || (commands & SPUDeltaItemCommandBinaryDiff) != 0); - uint16_t mode = item.permissions; + uint16_t mode = item.mode; NSFileManager *fileManager = [NSFileManager defaultManager]; if ((commands & SPUDeltaItemCommandBinaryDiff) != 0 || S_ISREG(mode) || S_ISLNK(mode)) { @@ -969,16 +969,15 @@ static compression_algorithm _compressionAlgorithmForMode(SPUDeltaCompressionMod } uint16_t extractMode = fileInfo.st_mode; - item.extractMode = extractMode; - uint16_t encodedMode; if ((commands & SPUDeltaItemCommandModifyPermissions) != 0) { - uint16_t permissions = item.permissions; - encodedMode = (extractMode & ~PERMISSION_FLAGS) | permissions; + encodedMode = (extractMode & ~PERMISSION_FLAGS) | item.mode; } else { encodedMode = extractMode; } + item.mode = extractMode; + // Store file mode (including desired permissions) if (![self _writeBuffer:&encodedMode length:sizeof(encodedMode)]) { break; @@ -1044,16 +1043,16 @@ static compression_algorithm _compressionAlgorithmForMode(SPUDeltaCompressionMod } extractMode = sourceFileInfo.st_mode; - item.extractMode = extractMode; uint16_t encodedMode; if ((commands & SPUDeltaItemCommandModifyPermissions) != 0) { - uint16_t permissions = item.permissions; - encodedMode = (extractMode & ~PERMISSION_FLAGS) | permissions; + encodedMode = (extractMode & ~PERMISSION_FLAGS) | item.mode; } else { encodedMode = extractMode; } + item.mode = extractMode; + // Store file mode (including desired permissions) if (![self _writeBuffer:&encodedMode length:sizeof(encodedMode)]) { break; @@ -1101,8 +1100,8 @@ static compression_algorithm _compressionAlgorithmForMode(SPUDeltaCompressionMod } } else if ((commands & SPUDeltaItemCommandModifyPermissions) != 0) { // Store file permissions - uint16_t permissions = item.permissions; - if (![self _writeBuffer:&permissions length:sizeof(permissions)]) { + uint16_t mode = item.mode; + if (![self _writeBuffer:&mode length:sizeof(mode)]) { break; } } @@ -1126,7 +1125,7 @@ static compression_algorithm _compressionAlgorithmForMode(SPUDeltaCompressionMod NSString *itemPath = item.itemFilePath; assert(itemPath != nil); - mode_t extractMode = item.extractMode; + mode_t extractMode = item.mode; if ((commands & SPUDeltaItemCommandBinaryDiff) != 0 || S_ISREG(extractMode)) { // Write out file contents to archive in chunks diff --git a/Autoupdate/SPUXarDeltaArchive.m b/Autoupdate/SPUXarDeltaArchive.m index ae1e4ea1..f6389192 100644 --- a/Autoupdate/SPUXarDeltaArchive.m +++ b/Autoupdate/SPUXarDeltaArchive.m @@ -280,7 +280,7 @@ static xar_file_t _xarAddFile(NSMutableDictionary *fileTa NSString *relativeFilePath = item.relativeFilePath; NSString *filePath = item.itemFilePath; SPUDeltaItemCommands commands = item.commands; - uint16_t permissions = item.permissions; + uint16_t mode = item.mode; xar_file_t newFile = _xarAddFile(self.fileTable, self.x, relativeFilePath, filePath); if (newFile == NULL) { @@ -301,7 +301,7 @@ static xar_file_t _xarAddFile(NSMutableDictionary *fileTa } if ((commands & SPUDeltaItemCommandModifyPermissions) != 0) { - xar_prop_set(newFile, MODIFY_PERMISSIONS_KEY, [NSString stringWithFormat:@"%u", permissions].UTF8String); + xar_prop_set(newFile, MODIFY_PERMISSIONS_KEY, [NSString stringWithFormat:@"%u", mode].UTF8String); } } @@ -362,16 +362,16 @@ static xar_file_t _xarAddFile(NSMutableDictionary *fileTa } } - uint16_t permissions = 0; + uint16_t mode = 0; { const char *value = NULL; if (xar_prop_get(file, MODIFY_PERMISSIONS_KEY, &value) == 0) { commands |= SPUDeltaItemCommandModifyPermissions; - permissions = (uint16_t)[@(value) intValue]; + mode = (uint16_t)[@(value) intValue]; } } - SPUDeltaArchiveItem *item = [[SPUDeltaArchiveItem alloc] initWithRelativeFilePath:relativePath commands:commands permissions:permissions]; + SPUDeltaArchiveItem *item = [[SPUDeltaArchiveItem alloc] initWithRelativeFilePath:relativePath commands:commands mode:mode]; item.xarContext = file; itemHandler(item, &exitedEarly); diff --git a/Autoupdate/SUBinaryDeltaApply.m b/Autoupdate/SUBinaryDeltaApply.m index 91c39102..06b5b20d 100644 --- a/Autoupdate/SUBinaryDeltaApply.m +++ b/Autoupdate/SUBinaryDeltaApply.m @@ -345,7 +345,7 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi } if ((commands & SPUDeltaItemCommandModifyPermissions) != 0) { - mode_t mode = (mode_t)item.permissions; + mode_t mode = (mode_t)item.mode; if (!modifyPermissions(destinationFilePath, mode)) { if (verbose) { fprintf(stderr, "\n"); @@ -358,7 +358,7 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi } if (verbose) { - fprintf(stderr, "\n👮 %s %s (0%o)", VERBOSE_MODIFIED, [relativePath fileSystemRepresentation], mode); + fprintf(stderr, "\n👮 %s %s (0%o)", VERBOSE_MODIFIED, [relativePath fileSystemRepresentation], mode & PERMISSION_FLAGS); } } }]; diff --git a/Autoupdate/SUBinaryDeltaCreate.m b/Autoupdate/SUBinaryDeltaCreate.m index 585562b1..799e8c5b 100644 --- a/Autoupdate/SUBinaryDeltaCreate.m +++ b/Autoupdate/SUBinaryDeltaCreate.m @@ -735,7 +735,7 @@ BOOL createBinaryDelta(NSString *source, NSString *destination, NSString *patchF id value = [newTreeState valueForKey:key]; if ([(NSObject *)value isEqual:[NSNull null]]) { - [archive addItem:[[SPUDeltaArchiveItem alloc] initWithRelativeFilePath:key commands:SPUDeltaItemCommandDelete permissions:0]]; + [archive addItem:[[SPUDeltaArchiveItem alloc] initWithRelativeFilePath:key commands:SPUDeltaItemCommandDelete mode:0]]; if (verbose) { fprintf(stderr, "\n❌ %s %s", VERBOSE_REMOVED, [key fileSystemRepresentation]); @@ -748,7 +748,7 @@ BOOL createBinaryDelta(NSString *source, NSString *destination, NSString *patchF if (shouldSkipDeltaCompression(originalInfo, newInfo)) { if (shouldSkipExtracting(originalInfo, newInfo)) { if (shouldChangePermissions(originalInfo, newInfo)) { - [archive addItem:[[SPUDeltaArchiveItem alloc] initWithRelativeFilePath:key commands:SPUDeltaItemCommandModifyPermissions permissions:[(NSNumber *)newInfo[INFO_PERMISSIONS_KEY] unsignedShortValue]]]; + [archive addItem:[[SPUDeltaArchiveItem alloc] initWithRelativeFilePath:key commands:SPUDeltaItemCommandModifyPermissions mode:[(NSNumber *)newInfo[INFO_PERMISSIONS_KEY] unsignedShortValue]]]; if (verbose) { fprintf(stderr, "\n👮 %s %s (0%o -> 0%o)", VERBOSE_MODIFIED, [key fileSystemRepresentation], [(NSNumber *)originalInfo[INFO_PERMISSIONS_KEY] unsignedShortValue], [(NSNumber *)newInfo[INFO_PERMISSIONS_KEY] unsignedShortValue]); @@ -773,7 +773,7 @@ BOOL createBinaryDelta(NSString *source, NSString *destination, NSString *patchF commands |= SPUDeltaItemCommandModifyPermissions; } - SPUDeltaArchiveItem *item = [[SPUDeltaArchiveItem alloc] initWithRelativeFilePath:key commands:commands permissions:(clonePermissionsChanged ? newPermissions.unsignedShortValue : 0)]; + SPUDeltaArchiveItem *item = [[SPUDeltaArchiveItem alloc] initWithRelativeFilePath:key commands:commands mode:(clonePermissionsChanged ? newPermissions.unsignedShortValue : 0)]; // Physical path for clones points to the old file item.itemFilePath = [source stringByAppendingPathComponent:clonedRelativePath]; item.sourcePath = item.itemFilePath; @@ -794,7 +794,7 @@ BOOL createBinaryDelta(NSString *source, NSString *destination, NSString *patchF commands |= SPUDeltaItemCommandDelete; } - SPUDeltaArchiveItem *item = [[SPUDeltaArchiveItem alloc] initWithRelativeFilePath:key commands:commands permissions:0]; + SPUDeltaArchiveItem *item = [[SPUDeltaArchiveItem alloc] initWithRelativeFilePath:key commands:commands mode:0]; item.itemFilePath = path; item.sourcePath = path; @@ -843,7 +843,7 @@ BOOL createBinaryDelta(NSString *source, NSString *destination, NSString *patchF } } - NSNumber *permissions = operation.permissions; + NSNumber *mode = operation.permissions; NSString *relativePath = operation.relativePath; SPUDeltaItemCommands commands = SPUDeltaItemCommandBinaryDiff; @@ -854,7 +854,7 @@ BOOL createBinaryDelta(NSString *source, NSString *destination, NSString *patchF commands |= SPUDeltaItemCommandClone; } - SPUDeltaArchiveItem *item = [[SPUDeltaArchiveItem alloc] initWithRelativeFilePath:relativePath commands:commands permissions:permissions.unsignedShortValue]; + SPUDeltaArchiveItem *item = [[SPUDeltaArchiveItem alloc] initWithRelativeFilePath:relativePath commands:commands mode:mode.unsignedShortValue]; item.itemFilePath = resultPath; item.sourcePath = operation._fromPath; item.clonedRelativePath = clonedRelativePath;