From bcab57d8f5035c28fc01dd7247759e28828528c6 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 12 Apr 2015 14:46:26 -0400 Subject: [PATCH] Handle exec. file changes, versioning, more error handling BinaryDelta can now create proper diffs in the case if a non-executable file becomes an executable file, or vise versa. Currently for simplicit y and backward-compatibility we delete then extract if an executable difference is found, rather than creating a diff. Files that contain an executable difference are unlikely to be large anyway. We also change the tree hash function so it can track executable permission differences on files. It also now tracks file types so it is less collision-resistant for example in the case where a regular file becoming a symlink could have resulted in the same hash. Because the tree hash function changed, major & minor versioning to the format was introduced. Older minor versions remain compatible, but older major versions do not. In this case, we've only bumped the minor version. In this case, older versions can apply a patch created by a newer version, and newer versions can apply a patch created by an older version. More error handling is now checked, some code improvements were made, and some adjustments were made to the tests with regard to the changes above. --- Sparkle/SUBinaryDeltaApply.m | 99 ++++++++++++++++++++++++++----- Sparkle/SUBinaryDeltaCommon.h | 30 +++++++++- Sparkle/SUBinaryDeltaCommon.m | 94 ++++++++++++++++++++---------- Sparkle/SUBinaryDeltaCreate.m | 106 +++++++++++++++++++++++++++------- Tests/SUBinaryDeltaTest.m | 36 ++++-------- 5 files changed, 269 insertions(+), 96 deletions(-) diff --git a/Sparkle/SUBinaryDeltaApply.m b/Sparkle/SUBinaryDeltaApply.m index ecb5c79e..2f35e50b 100644 --- a/Sparkle/SUBinaryDeltaApply.m +++ b/Sparkle/SUBinaryDeltaApply.m @@ -15,38 +15,84 @@ #include #include -static void applyBinaryDeltaToFile(xar_t x, xar_file_t file, NSString *sourceFilePath, NSString *destinationFilePath) +static BOOL applyBinaryDeltaToFile(xar_t x, xar_file_t file, NSString *sourceFilePath, NSString *destinationFilePath) { NSString *patchFile = temporaryFilename(@"apply-binary-delta"); xar_extract_tofile(x, file, [patchFile fileSystemRepresentation]); const char *argv[] = {"/usr/bin/bspatch", [sourceFilePath fileSystemRepresentation], [destinationFilePath fileSystemRepresentation], [patchFile fileSystemRepresentation]}; - bspatch(4, (char **)argv); + BOOL success = (bspatch(4, (char **)argv) == 0); unlink([patchFile fileSystemRepresentation]); + return success; } int applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFile) { - xar_t x = xar_open([patchFile UTF8String], READ); + xar_t x = xar_open([patchFile fileSystemRepresentation], READ); if (!x) { - fprintf(stderr, "Unable to open %s. Giving up.\n", [patchFile UTF8String]); + fprintf(stderr, "Unable to open %s. Giving up.\n", [patchFile fileSystemRepresentation]); return 1; } + + uint16_t majorDiffVersion = FIRST_DELTA_DIFF_MAJOR_VERSION; + uint16_t minorDiffVersion = FIRST_DELTA_DIFF_MINOR_VERSION; - NSString *expectedBeforeHash = nil; - NSString *expectedAfterHash = nil; + NSString *expectedBeforeHashv1_0 = nil; + NSString *expectedAfterHashv1_0 = nil; + + NSString *expectedBeforeHashv1_1 = nil; + NSString *expectedAfterHashv1_1 = nil; + xar_subdoc_t subdoc; for (subdoc = xar_subdoc_first(x); subdoc; subdoc = xar_subdoc_next(subdoc)) { if (!strcmp(xar_subdoc_name(subdoc), "binary-delta-attributes")) { const char *value = 0; + + // only available in version 1.1 or later + xar_subdoc_prop_get(subdoc, "major-version", &value); + if (value) + majorDiffVersion = (uint16_t)[@(value) intValue]; + + // only available in version 1.1 or later + xar_subdoc_prop_get(subdoc, "minor-version", &value); + if (value) + minorDiffVersion = (uint16_t)[@(value) intValue]; + + // deprecated since version 1.1 xar_subdoc_prop_get(subdoc, "before-sha1", &value); if (value) - expectedBeforeHash = @(value); + expectedBeforeHashv1_0 = @(value); + // deprecated since version 1.1 xar_subdoc_prop_get(subdoc, "after-sha1", &value); if (value) - expectedAfterHash = @(value); + expectedAfterHashv1_0 = @(value); + + // only available in version 1.1 or later + xar_subdoc_prop_get(subdoc, "before-sha1-v1.1", &value); + if (value) + expectedBeforeHashv1_1 = @(value); + + // only available in version 1.1 or later + xar_subdoc_prop_get(subdoc, "after-sha1-v1.1", &value); + if (value) + expectedAfterHashv1_1 = @(value); } } + + if (majorDiffVersion < FIRST_DELTA_DIFF_MAJOR_VERSION) { + fprintf(stderr, "Unable to identify diff-version %u in delta. Giving up.\n", majorDiffVersion); + return 1; + } + + if (majorDiffVersion > LATEST_DELTA_DIFF_MAJOR_VERSION) { + fprintf(stderr, "A later version is needed to apply this patch (on major version %u, but patch requests version %u).\n", LATEST_DELTA_DIFF_MAJOR_VERSION, majorDiffVersion); + return 1; + } + + BOOL usesPatch1_1 = DIFF_VERSION_IS_AT_LEAST(majorDiffVersion, minorDiffVersion, 1, 1); + + NSString *expectedBeforeHash = usesPatch1_1 ? expectedBeforeHashv1_1 : expectedBeforeHashv1_0; + NSString *expectedAfterHash = usesPatch1_1 ? expectedAfterHashv1_1 : expectedAfterHashv1_0; if (!expectedBeforeHash || !expectedAfterHash) { fprintf(stderr, "Unable to find before-sha1 or after-sha1 metadata in delta. Giving up.\n"); @@ -54,7 +100,11 @@ int applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFil } fprintf(stdout, "Verifying source... "); - NSString *beforeHash = hashOfTree(source); + NSString *beforeHash = hashOfTreeWithVersion(source, majorDiffVersion, minorDiffVersion); + if (!beforeHash) { + fprintf(stderr, "Unable to calculate hash of tree %s\n", [source fileSystemRepresentation]); + return 1; + } if (![beforeHash isEqualToString:expectedBeforeHash]) { fprintf(stderr, "Source doesn't have expected hash (%s != %s). Giving up.\n", [expectedBeforeHash UTF8String], [beforeHash UTF8String]); @@ -62,8 +112,14 @@ int applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFil } fprintf(stdout, "\nCopying files... "); - removeTree(destination); - copyTree(source, destination); + if (!removeTree(destination)) { + fprintf(stderr, "Failed to remove %s\n", [destination fileSystemRepresentation]); + return 1; + } + if (!copyTree(source, destination)) { + fprintf(stderr, "Failed to copy %s to %s\n", [source fileSystemRepresentation], [destination fileSystemRepresentation]); + return 1; + } fprintf(stdout, "\nPatching... "); xar_file_t file; @@ -80,15 +136,26 @@ int applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFil continue; } - if (!xar_prop_get(file, "binary-delta", &value)) - applyBinaryDeltaToFile(x, file, sourceFilePath, destinationFilePath); - else - xar_extract_tofile(x, file, [destinationFilePath fileSystemRepresentation]); + if (!xar_prop_get(file, "binary-delta", &value)) { + if (!applyBinaryDeltaToFile(x, file, sourceFilePath, destinationFilePath)) { + fprintf(stderr, "Unable to patch %s to destination %s\n", [sourceFilePath fileSystemRepresentation], [destinationFilePath fileSystemRepresentation]); + return 1; + } + } else { + if (xar_extract_tofile(x, file, [destinationFilePath fileSystemRepresentation]) != 0) { + fprintf(stderr, "Unable to extract file to %s\n", [destinationFilePath fileSystemRepresentation]); + return 1; + } + } } xar_close(x); fprintf(stdout, "\nVerifying destination... "); - NSString *afterHash = hashOfTree(destination); + NSString *afterHash = hashOfTreeWithVersion(destination, majorDiffVersion, minorDiffVersion); + if (!afterHash) { + fprintf(stderr, "Unable to calculate hash of tree %s\n", [destination fileSystemRepresentation]); + return 1; + } if (![afterHash isEqualToString:expectedAfterHash]) { fprintf(stderr, "Destination doesn't have expected hash (%s != %s). Giving up.\n", [expectedAfterHash UTF8String], [afterHash UTF8String]); diff --git a/Sparkle/SUBinaryDeltaCommon.h b/Sparkle/SUBinaryDeltaCommon.h index a0bc5328..ea5f56ee 100644 --- a/Sparkle/SUBinaryDeltaCommon.h +++ b/Sparkle/SUBinaryDeltaCommon.h @@ -9,17 +9,41 @@ #ifndef SUBINARYDELTACOMMON_H #define SUBINARYDELTACOMMON_H +#define _DARWIN_NO_64_BIT_INODE 1 + #include +// Only track executable bits, which is what VCS's like git and hg do as well +// Other permission bits might be too sketchy to track +#define EXECUTABLE_PERMISSIONS (S_IXUSR | S_IXGRP | S_IXOTH) + +#define DIFF_VERSION_IS_AT_LEAST(actualMajor, actualMinor, expectedMajor, expectedMinor) \ + ((actualMajor > expectedMajor) || (actualMajor == expectedMajor && actualMinor >= expectedMinor)) + +#define FIRST_DELTA_DIFF_MAJOR_VERSION 1 +#define FIRST_DELTA_DIFF_MINOR_VERSION 0 + +#define LATEST_DELTA_DIFF_MAJOR_VERSION 1 +#define LATEST_DELTA_DIFF_MAJOR_VERSION_STR _LATEST_DELTA_DIFF_VERSION_STR(LATEST_DELTA_DIFF_MAJOR_VERSION) + +#define LATEST_DELTA_DIFF_MINOR_VERSION 1 +#define LATEST_DELTA_DIFF_MINOR_VERSION_STR _LATEST_DELTA_DIFF_VERSION_STR(LATEST_DELTA_DIFF_MINOR_VERSION) + +// See https://gcc.gnu.org/onlinedocs/cpp/Stringification.html#Stringification +#define __LATEST_DELTA_DIFF_VERSION_STR(s) #s +#define _LATEST_DELTA_DIFF_VERSION_STR(s) __LATEST_DELTA_DIFF_VERSION_STR(s) + @class NSString; @class NSData; extern int compareFiles(const FTSENT **a, const FTSENT **b); -extern NSData *hashOfFile(FTSENT *ent); +extern NSData *hashOfFileContents(FTSENT *ent); +extern NSString *hashOfTreeWithVersion(NSString *path, uint16_t majorVersion, uint16_t minorVersion); extern NSString *hashOfTree(NSString *path); -extern void removeTree(NSString *path); -extern void copyTree(NSString *source, NSString *dest); +extern BOOL removeTree(NSString *path); +extern BOOL copyTree(NSString *source, NSString *dest); extern NSString *pathRelativeToDirectory(NSString *directory, NSString *path); NSString *temporaryFilename(NSString *base); +NSString *temporaryDirectory(NSString *base); NSString *stringWithFileSystemRepresentation(const char*); #endif diff --git a/Sparkle/SUBinaryDeltaCommon.m b/Sparkle/SUBinaryDeltaCommon.m index 1aa6e1ce..6441ad28 100644 --- a/Sparkle/SUBinaryDeltaCommon.m +++ b/Sparkle/SUBinaryDeltaCommon.m @@ -56,6 +56,22 @@ NSString *temporaryFilename(NSString *base) return stringWithFileSystemRepresentation(buffer); } +NSString *temporaryDirectory(NSString *base) +{ + NSString *template = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.XXXXXXXXXX", base]]; + NSMutableData *data = [NSMutableData data]; + [data appendBytes:template.fileSystemRepresentation length:strlen(template.fileSystemRepresentation) + 1]; + + char *buffer = data.mutableBytes; + char *templateResult = mkdtemp(buffer); + if (templateResult == NULL) { + perror("mkdtemp"); + return nil; + } + + return stringWithFileSystemRepresentation(templateResult); +} + static void _hashOfBuffer(unsigned char *hash, const char* buffer, ssize_t bufferLength) { assert(bufferLength >= 0 && bufferLength <= UINT32_MAX); @@ -65,59 +81,57 @@ static void _hashOfBuffer(unsigned char *hash, const char* buffer, ssize_t buffe CC_SHA1_Final(hash, &hashContext); } -static void _hashOfFile(unsigned char* hash, FTSENT *ent) +static BOOL _hashOfFileContents(unsigned char* hash, FTSENT *ent) { if (ent->fts_info == FTS_SL) { char linkDestination[MAXPATHLEN + 1]; ssize_t linkDestinationLength = readlink(ent->fts_path, linkDestination, MAXPATHLEN); if (linkDestinationLength < 0) { perror("readlink"); - return; + return NO; } _hashOfBuffer(hash, linkDestination, linkDestinationLength); - return; - } - - if (ent->fts_info == FTS_F) { + } else if (ent->fts_info == FTS_F) { int fileDescriptor = open(ent->fts_path, O_RDONLY); if (fileDescriptor == -1) { perror("open"); - return; + return NO; } ssize_t fileSize = ent->fts_statp->st_size; if (fileSize == 0) { _hashOfBuffer(hash, NULL, 0); - close(fileDescriptor); - return; + } else { + void *buffer = mmap(0, (size_t)fileSize, PROT_READ, MAP_FILE | MAP_PRIVATE, fileDescriptor, 0); + if (buffer == (void*)-1) { + close(fileDescriptor); + perror("mmap"); + return NO; + } + + _hashOfBuffer(hash, buffer, fileSize); + munmap(buffer, (size_t)fileSize); } - - void *buffer = mmap(0, (size_t)fileSize, PROT_READ, MAP_FILE | MAP_PRIVATE, fileDescriptor, 0); - if (buffer == (void*)-1) { - close(fileDescriptor); - perror("mmap"); - return; - } - - _hashOfBuffer(hash, buffer, fileSize); - munmap(buffer, (size_t)fileSize); close(fileDescriptor); - return; - } - - if (ent->fts_info == FTS_D) + } else if (ent->fts_info == FTS_D) { memset(hash, 0xdd, CC_SHA1_DIGEST_LENGTH); + } else { + return NO; + } + return YES; } -NSData *hashOfFile(FTSENT *ent) +NSData *hashOfFileContents(FTSENT *ent) { unsigned char fileHash[CC_SHA1_DIGEST_LENGTH]; - _hashOfFile(fileHash, ent); + if (!_hashOfFileContents(fileHash, ent)) { + return nil; + } return [NSData dataWithBytes:fileHash length:CC_SHA1_DIGEST_LENGTH]; } -NSString *hashOfTree(NSString *path) +NSString *hashOfTreeWithVersion(NSString *path, uint16_t majorVersion, uint16_t minorVersion) { const char *sourcePaths[] = {[path fileSystemRepresentation], 0}; FTS *fts = fts_open((char* const*)sourcePaths, FTS_PHYSICAL | FTS_NOCHDIR, compareFiles); @@ -135,12 +149,24 @@ NSString *hashOfTree(NSString *path) continue; unsigned char fileHash[CC_SHA1_DIGEST_LENGTH]; - _hashOfFile(fileHash, ent); + _hashOfFileContents(fileHash, ent); + if (!_hashOfFileContents(fileHash, ent)) { + return nil; + } CC_SHA1_Update(&hashContext, fileHash, sizeof(fileHash)); NSString *relativePath = pathRelativeToDirectory(path, stringWithFileSystemRepresentation(ent->fts_path)); const char *relativePathBytes = [relativePath fileSystemRepresentation]; CC_SHA1_Update(&hashContext, relativePathBytes, (CC_LONG)strlen(relativePathBytes)); + + if (DIFF_VERSION_IS_AT_LEAST(majorVersion, minorVersion, 1, 1)) { + uint16_t mode = ent->fts_statp->st_mode; + uint16_t type = ent->fts_info; + uint16_t executablePermissions = mode & EXECUTABLE_PERMISSIONS; + + CC_SHA1_Update(&hashContext, &type, sizeof(type)); + CC_SHA1_Update(&hashContext, &executablePermissions, sizeof(executablePermissions)); + } } fts_close(fts); @@ -155,12 +181,18 @@ NSString *hashOfTree(NSString *path) return @(hexHash); } -void removeTree(NSString *path) +extern NSString *hashOfTree(NSString *path) { - [[NSFileManager defaultManager] removeItemAtPath:path error:nil]; + return hashOfTreeWithVersion(path, LATEST_DELTA_DIFF_MAJOR_VERSION, LATEST_DELTA_DIFF_MINOR_VERSION); } -void copyTree(NSString *source, NSString *dest) +BOOL removeTree(NSString *path) { - [[NSFileManager defaultManager] copyItemAtPath:source toPath:dest error:nil]; + NSFileManager *fileManager = [NSFileManager defaultManager]; + return ![fileManager fileExistsAtPath:path] ? YES : [fileManager removeItemAtPath:path error:nil]; +} + +BOOL copyTree(NSString *source, NSString *dest) +{ + return [[NSFileManager defaultManager] copyItemAtPath:source toPath:dest error:nil]; } diff --git a/Sparkle/SUBinaryDeltaCreate.m b/Sparkle/SUBinaryDeltaCreate.m index 728b57aa..6fb974fd 100644 --- a/Sparkle/SUBinaryDeltaCreate.m +++ b/Sparkle/SUBinaryDeltaCreate.m @@ -6,8 +6,6 @@ // Copyright (c) 2015 Sparkle Project. All rights reserved. // -#define _DARWIN_NO_64_BIT_INODE 1 - #import "SUBinaryDeltaCreate.h" #import #include "SUBinaryDeltaCommon.h" @@ -59,14 +57,26 @@ extern int bsdiff(int argc, const char **argv); @end +#define INFO_HASH_KEY @"hash" +#define INFO_TYPE_KEY @"type" +#define INFO_EXECUTABLE_PERMISSIONS_KEY @"executable_permissions" +#define INFO_SIZE_KEY @"size" + static NSDictionary *infoForFile(FTSENT *ent) { - NSData *hash = hashOfFile(ent); - NSNumber *size = @0; - if (ent->fts_info != FTS_D) { - size = @(ent->fts_statp->st_size); + NSData *hash = hashOfFileContents(ent); + if (!hash) { + return nil; } - return @{@"hash": hash, @"type": @(ent->fts_info), @"size": size}; + + off_t size = (ent->fts_info != FTS_D) ? ent->fts_statp->st_size : 0; + + assert(ent->fts_statp != NULL); + + mode_t mode = ent->fts_statp->st_mode; + mode_t executablePermissions = mode & EXECUTABLE_PERMISSIONS; + + return @{INFO_HASH_KEY: hash, INFO_TYPE_KEY: @(ent->fts_info), INFO_EXECUTABLE_PERMISSIONS_KEY : @(executablePermissions), INFO_SIZE_KEY: @(size)}; } static NSString *absolutePath(NSString *path) @@ -83,10 +93,12 @@ static NSString *temporaryPatchFile(NSString *patchFile) return [NSString stringWithFormat:@"%@/.%@.tmp", directory, file]; } -static BOOL shouldSkipDeltaCompression(NSString * __unused key, NSDictionary* originalInfo, NSDictionary *newInfo) +#define MIN_FILE_SIZE_FOR_CREATING_DELTA 4096 + +static BOOL shouldSkipDeltaCompression(NSDictionary* originalInfo, NSDictionary *newInfo) { - unsigned long long fileSize = [newInfo[@"size"] unsignedLongLongValue]; - if (fileSize < 4096) { + unsigned long long fileSize = [newInfo[INFO_SIZE_KEY] unsignedLongLongValue]; + if (fileSize < MIN_FILE_SIZE_FOR_CREATING_DELTA) { return YES; } @@ -94,20 +106,28 @@ static BOOL shouldSkipDeltaCompression(NSString * __unused key, NSDictionary* or return YES; } - if ([originalInfo[@"type"] unsignedShortValue] != [newInfo[@"type"] unsignedShortValue]) { + if ([originalInfo[INFO_TYPE_KEY] unsignedShortValue] != [newInfo[INFO_TYPE_KEY] unsignedShortValue]) { + return YES; + } + + if ([originalInfo[INFO_EXECUTABLE_PERMISSIONS_KEY] unsignedShortValue] != [newInfo[INFO_EXECUTABLE_PERMISSIONS_KEY] unsignedShortValue]) { return YES; } return NO; } -static BOOL shouldDeleteThenExtract(NSString * __unused key, NSDictionary* originalInfo, NSDictionary *newInfo) +static BOOL shouldDeleteThenExtract(NSDictionary* originalInfo, NSDictionary *newInfo) { if (!originalInfo) { return NO; } - if ([originalInfo[@"type"] unsignedShortValue] != [newInfo[@"type"] unsignedShortValue]) { + if ([originalInfo[INFO_TYPE_KEY] unsignedShortValue] != [newInfo[INFO_TYPE_KEY] unsignedShortValue]) { + return YES; + } + + if ([originalInfo[INFO_EXECUTABLE_PERMISSIONS_KEY] unsignedShortValue] != [newInfo[INFO_EXECUTABLE_PERMISSIONS_KEY] unsignedShortValue]) { return YES; } @@ -125,7 +145,7 @@ int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFi return 1; } - fprintf(stdout, "Processing %s...", [source UTF8String]); + fprintf(stdout, "Processing %s...", [source fileSystemRepresentation]); FTSENT *ent = 0; while ((ent = fts_read(fts))) { if (ent->fts_info != FTS_F && ent->fts_info != FTS_SL && ent->fts_info != FTS_D) { @@ -138,11 +158,24 @@ int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFi } NSDictionary *info = infoForFile(ent); + if (!info) { + fprintf(stderr, "Failed to retrieve info for file %s", ent->fts_path); + return 1; + } originalTreeState[key] = info; } fts_close(fts); - NSString *beforeHash = hashOfTree(source); + NSString *beforeHashv1_0 = hashOfTreeWithVersion(source, 1, 0); + if (!beforeHashv1_0) { + fprintf(stderr, "Failed to generate version 1.0 hash for tree %s ; this patch won't be apply-able from older versions", [source fileSystemRepresentation]); + } + + NSString *beforeHashv1_1 = hashOfTree(source); + if (!beforeHashv1_1) { + fprintf(stderr, "Failed to generate latest hash for tree %s", [source fileSystemRepresentation]); + return 1; + } NSMutableDictionary *newTreeState = [NSMutableDictionary dictionary]; for (NSString *key in originalTreeState) @@ -150,7 +183,7 @@ int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFi newTreeState[key] = [NSNull null]; } - fprintf(stdout, "\nProcessing %s... ", [destination UTF8String]); + fprintf(stdout, "\nProcessing %s... ", [destination fileSystemRepresentation]); sourcePaths[0] = [destination fileSystemRepresentation]; fts = fts_open((char* const*)sourcePaths, FTS_PHYSICAL | FTS_NOCHDIR, compareFiles); if (!fts) { @@ -170,6 +203,10 @@ int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFi } NSDictionary *info = infoForFile(ent); + if (!info) { + fprintf(stderr, "Failed to retrieve info from file %s", ent->fts_path); + return 1; + } NSDictionary *oldInfo = originalTreeState[key]; if ([info isEqual:oldInfo]) { @@ -177,7 +214,7 @@ int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFi } else { newTreeState[key] = info; - if (oldInfo && [oldInfo[@"type"] unsignedShortValue] == FTS_D && [info[@"type"] unsignedShortValue] != FTS_D) { + if (oldInfo && [oldInfo[INFO_TYPE_KEY] unsignedShortValue] == FTS_D && [info[INFO_TYPE_KEY] unsignedShortValue] != FTS_D) { NSArray *parentPathComponents = key.pathComponents; for (NSString *childPath in originalTreeState) { @@ -192,16 +229,37 @@ int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFi } fts_close(fts); - NSString *afterHash = hashOfTree(destination); + NSString *afterHashv1_0 = hashOfTreeWithVersion(destination, 1, 0); + if (!afterHashv1_0) { + fprintf(stderr, "Failed to generate version 1.0 hash for tree %s ; this patch won't be apply-able from older versions", [destination fileSystemRepresentation]); + } + + NSString *afterHashv1_1 = hashOfTree(destination); + if (!afterHashv1_1) { + fprintf(stderr, "Failed to generate latest hash for tree %s", [destination fileSystemRepresentation]); + return 1; + } fprintf(stdout, "\nGenerating delta... "); NSString *temporaryFile = temporaryPatchFile(patchFile); xar_t x = xar_open([temporaryFile fileSystemRepresentation], WRITE); xar_opt_set(x, XAR_OPT_COMPRESSION, "bzip2"); + xar_subdoc_t attributes = xar_subdoc_new(x, "binary-delta-attributes"); - xar_subdoc_prop_set(attributes, "before-sha1", [beforeHash UTF8String]); - xar_subdoc_prop_set(attributes, "after-sha1", [afterHash UTF8String]); + + xar_subdoc_prop_set(attributes, "major-version", LATEST_DELTA_DIFF_MAJOR_VERSION_STR); + xar_subdoc_prop_set(attributes, "minor-version", LATEST_DELTA_DIFF_MINOR_VERSION_STR); + + if (beforeHashv1_0) { + xar_subdoc_prop_set(attributes, "before-sha1", [beforeHashv1_0 UTF8String]); + } + if (afterHashv1_0) { + xar_subdoc_prop_set(attributes, "after-sha1", [afterHashv1_0 UTF8String]); + } + + xar_subdoc_prop_set(attributes, "before-sha1-v1.1", [beforeHashv1_1 UTF8String]); + xar_subdoc_prop_set(attributes, "after-sha1-v1.1", [afterHashv1_1 UTF8String]); NSOperationQueue *deltaQueue = [[NSOperationQueue alloc] init]; NSMutableArray *deltaOperations = [NSMutableArray array]; @@ -228,11 +286,11 @@ int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFi NSDictionary *originalInfo = originalTreeState[key]; NSDictionary *newInfo = newTreeState[key]; - if (shouldSkipDeltaCompression(key, originalInfo, newInfo)) { + 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(key, originalInfo, newInfo)) { + if (shouldDeleteThenExtract(originalInfo, newInfo)) { xar_prop_set(newFile, "delete-then-extract", "true"); } } else { @@ -246,6 +304,10 @@ int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFi for (CreateBinaryDeltaOperation *operation in deltaOperations) { NSString *resultPath = [operation resultPath]; + if (!resultPath) { + fprintf(stderr, "Failed to create patch from source %s and destination %s\n", [[operation relativePath] fileSystemRepresentation], [resultPath fileSystemRepresentation]); + return 1; + } xar_file_t newFile = xar_add_frompath(x, 0, [[operation relativePath] fileSystemRepresentation], [resultPath fileSystemRepresentation]); assert(newFile); xar_prop_set(newFile, "binary-delta", "true"); diff --git a/Tests/SUBinaryDeltaTest.m b/Tests/SUBinaryDeltaTest.m index 6f913e2c..2e5ecdd1 100644 --- a/Tests/SUBinaryDeltaTest.m +++ b/Tests/SUBinaryDeltaTest.m @@ -18,22 +18,6 @@ @implementation SUBinaryDeltaTest -static NSString *temporaryDirectory(NSString *base) -{ - NSString *template = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.XXXXXXXXXX", base]]; - NSMutableData *data = [NSMutableData data]; - [data appendBytes:template.fileSystemRepresentation length:strlen(template.fileSystemRepresentation) + 1]; - - char *buffer = data.mutableBytes; - char *templateResult = mkdtemp(buffer); - if (templateResult == NULL) { - perror("mkdtemp"); - return nil; - } - - return stringWithFileSystemRepresentation(templateResult); -} - - (void)testTemporaryDirectory { NSString *tmp1 = temporaryDirectory(@"Sparkle"); @@ -82,7 +66,13 @@ static NSString *temporaryDirectory(NSString *base) XCTAssertNotNil(source); XCTAssertNotNil(destination); - return [hashOfTree(source) isEqualToString:hashOfTree(destination)]; + NSString *beforeHash = hashOfTree(source); + NSString *afterHash = hashOfTree(destination); + + XCTAssertNotNil(beforeHash); + XCTAssertNotNil(afterHash); + + return [beforeHash isEqualToString:afterHash]; } - (void)testEmptyDataDiff @@ -271,10 +261,7 @@ static NSString *temporaryDirectory(NSString *base) }]; } -/* -// This fails in the hash equality check -// The delta tool also fails at doing file permission changes, though this test won't be able to detect it -- (void)testFilePermissionChangedWithHashCheck +- (void)testExecutableFilePermissionChangedWithHashCheck { [self createAndApplyPatchWithHandler:^(NSFileManager *fileManager, NSString *sourceDirectory, NSString *destinationDirectory) { NSString *sourceFile = [sourceDirectory stringByAppendingPathComponent:@"A"]; @@ -285,15 +272,15 @@ static NSString *temporaryDirectory(NSString *base) XCTAssertTrue([data writeToFile:destinationFile atomically:YES]); NSError *error = nil; - if (![fileManager setAttributes:@{NSFilePosixPermissions : @0777} ofItemAtPath:destinationFile error:&error]) { + if (![fileManager setAttributes:@{NSFilePosixPermissions : @0755} ofItemAtPath:destinationFile error:&error]) { NSLog(@"Change Permission Error: %@", error); XCTFail(@"Failed setting file permissions"); } + // This would fail for version 1.0 XCTAssertFalse([self testDirectoryHashEqualityWithSource:sourceDirectory destination:destinationDirectory]); }]; } - */ - (void)testRegularFileToSymlinkChange { @@ -304,7 +291,7 @@ static NSString *temporaryDirectory(NSString *base) NSString *destinationFile1 = [destinationDirectory stringByAppendingPathComponent:@"A"]; NSString *destinationFile2 = [destinationDirectory stringByAppendingPathComponent:@"B"]; - NSData *data = [NSData dataWithBytes:"loltest" length:7]; + NSData *data = [NSData dataWithBytes:"A" length:1]; XCTAssertTrue([data writeToFile:sourceFile1 atomically:YES]); XCTAssertTrue([data writeToFile:sourceFile2 atomically:YES]); @@ -317,6 +304,7 @@ static NSString *temporaryDirectory(NSString *base) XCTFail(@"Failed to create symlink"); } + // This would fail with version 1.0 XCTAssertFalse([self testDirectoryHashEqualityWithSource:sourceDirectory destination:destinationDirectory]); }]; }