From 08434edf7903b04936ccf358c4de243b655fdc31 Mon Sep 17 00:00:00 2001 From: Zorg Date: Thu, 9 Apr 2015 21:39:11 -0400 Subject: [PATCH] Move creating diffs into separate source file Since a function for creating diffs is now exposed, this should make testing easier This is also consistent with applying diffs being in a separate source file from the delta tool as well --- Sparkle.xcodeproj/project.pbxproj | 6 + Sparkle/SUBinaryDeltaCreate.h | 15 ++ Sparkle/SUBinaryDeltaCreate.m | 263 ++++++++++++++++++++++++++++ Sparkle/SUBinaryDeltaTool.m | 275 ++---------------------------- 4 files changed, 300 insertions(+), 259 deletions(-) create mode 100644 Sparkle/SUBinaryDeltaCreate.h create mode 100644 Sparkle/SUBinaryDeltaCreate.m diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 3b6bb7c0..d3e5cb71 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -168,6 +168,7 @@ 61F83F740DBFE141006FDD30 /* SUBasicUpdateDriver.h in Headers */ = {isa = PBXBuildFile; fileRef = 61F83F6F0DBFE137006FDD30 /* SUBasicUpdateDriver.h */; settings = {ATTRIBUTES = (); }; }; 61FA52880E2D9EA400EF58AD /* Sparkle.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8DC2EF5B0486A6940098B216 /* Sparkle.framework */; settings = {ATTRIBUTES = (Required, ); }; }; 7223E7631AD1AEFF008E3161 /* sais.c in Sources */ = {isa = PBXBuildFile; fileRef = 7223E7611AD1AEFF008E3161 /* sais.c */; }; + 7268AC631AD634C200C3E0C1 /* SUBinaryDeltaCreate.m in Sources */ = {isa = PBXBuildFile; fileRef = 7268AC621AD634C200C3E0C1 /* SUBinaryDeltaCreate.m */; }; 767B61AC1972D488004E0C3C /* SUGuidedPackageInstaller.h in Headers */ = {isa = PBXBuildFile; fileRef = 767B61AA1972D488004E0C3C /* SUGuidedPackageInstaller.h */; }; 767B61AD1972D488004E0C3C /* SUGuidedPackageInstaller.m in Sources */ = {isa = PBXBuildFile; fileRef = 767B61AB1972D488004E0C3C /* SUGuidedPackageInstaller.m */; }; 767B61AE1972D488004E0C3C /* SUGuidedPackageInstaller.m in Sources */ = {isa = PBXBuildFile; fileRef = 767B61AB1972D488004E0C3C /* SUGuidedPackageInstaller.m */; }; @@ -537,6 +538,8 @@ 61F83F700DBFE137006FDD30 /* SUBasicUpdateDriver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUBasicUpdateDriver.m; sourceTree = ""; }; 7223E7611AD1AEFF008E3161 /* sais.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = sais.c; sourceTree = ""; }; 7223E7621AD1AEFF008E3161 /* sais.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = sais.h; sourceTree = ""; }; + 7268AC621AD634C200C3E0C1 /* SUBinaryDeltaCreate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUBinaryDeltaCreate.m; sourceTree = ""; }; + 7268AC641AD634E400C3E0C1 /* SUBinaryDeltaCreate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUBinaryDeltaCreate.h; sourceTree = ""; }; 767B61AA1972D488004E0C3C /* SUGuidedPackageInstaller.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUGuidedPackageInstaller.h; sourceTree = ""; }; 767B61AB1972D488004E0C3C /* SUGuidedPackageInstaller.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUGuidedPackageInstaller.m; sourceTree = ""; }; 8DC2EF5A0486A6940098B216 /* Sparkle-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Sparkle-Info.plist"; sourceTree = ""; }; @@ -787,6 +790,8 @@ children = ( 5D06E8DF0FD68CC7005AE3F6 /* SUBinaryDeltaApply.h */, 5D06E8E00FD68CC7005AE3F6 /* SUBinaryDeltaApply.m */, + 7268AC641AD634E400C3E0C1 /* SUBinaryDeltaCreate.h */, + 7268AC621AD634C200C3E0C1 /* SUBinaryDeltaCreate.m */, 5D06E8E10FD68CC7005AE3F6 /* SUBinaryDeltaCommon.h */, 5D06E8E20FD68CC7005AE3F6 /* SUBinaryDeltaCommon.m */, 5D06E8E30FD68CC7005AE3F6 /* SUBinaryDeltaTool.m */, @@ -1400,6 +1405,7 @@ 5D06E8E90FD68CDB005AE3F6 /* bsdiff.c in Sources */, 14652F7E19A9728A00959E44 /* bspatch.c in Sources */, 14652F7D19A9726700959E44 /* SUBinaryDeltaApply.m in Sources */, + 7268AC631AD634C200C3E0C1 /* SUBinaryDeltaCreate.m in Sources */, 7223E7631AD1AEFF008E3161 /* sais.c in Sources */, 14652F7C19A9725300959E44 /* SUBinaryDeltaCommon.m in Sources */, 5D06E8EA0FD68CDB005AE3F6 /* SUBinaryDeltaTool.m in Sources */, diff --git a/Sparkle/SUBinaryDeltaCreate.h b/Sparkle/SUBinaryDeltaCreate.h new file mode 100644 index 00000000..0ce4b589 --- /dev/null +++ b/Sparkle/SUBinaryDeltaCreate.h @@ -0,0 +1,15 @@ +// +// SUBinaryDeltaCreate.m +// Sparkle +// +// Created by Mayur Pawashe on 4/9/15. +// Copyright (c) 2015 Sparkle Project. All rights reserved. +// + +#ifndef SUBINARYDELTACREATE_H +#define SUBINARYDELTACREATE_H + +@class NSString; +int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFile); + +#endif diff --git a/Sparkle/SUBinaryDeltaCreate.m b/Sparkle/SUBinaryDeltaCreate.m new file mode 100644 index 00000000..728b57aa --- /dev/null +++ b/Sparkle/SUBinaryDeltaCreate.m @@ -0,0 +1,263 @@ +// +// SUBinaryDeltaCreate.m +// Sparkle +// +// Created by Mayur Pawashe on 4/9/15. +// Copyright (c) 2015 Sparkle Project. All rights reserved. +// + +#define _DARWIN_NO_64_BIT_INODE 1 + +#import "SUBinaryDeltaCreate.h" +#import +#include "SUBinaryDeltaCommon.h" +#import +#include +#include +#include +#include +#include +#include +#include +#include +#include + +extern int bsdiff(int argc, const char **argv); + +@interface CreateBinaryDeltaOperation : NSOperation +@property (copy) NSString *relativePath; +@property (strong) NSString *resultPath; +@property (strong) NSString *_fromPath; +@property (strong) NSString *_toPath; +- (id)initWithRelativePath:(NSString *)relativePath oldTree:(NSString *)oldTree newTree:(NSString *)newTree; +@end + +@implementation CreateBinaryDeltaOperation +@synthesize relativePath = _relativePath; +@synthesize resultPath = _resultPath; +@synthesize _fromPath = _fromPath; +@synthesize _toPath = _toPath; + +- (id)initWithRelativePath:(NSString *)relativePath oldTree:(NSString *)oldTree newTree:(NSString *)newTree +{ + if ((self = [super init])) { + self.relativePath = relativePath; + self._fromPath = [oldTree stringByAppendingPathComponent:relativePath]; + self._toPath = [newTree stringByAppendingPathComponent:relativePath]; + } + return self; +} + +- (void)main +{ + NSString *temporaryFile = temporaryFilename(@"BinaryDelta"); + const char *argv[] = {"/usr/bin/bsdiff", [self._fromPath fileSystemRepresentation], [self._toPath fileSystemRepresentation], [temporaryFile fileSystemRepresentation]}; + int result = bsdiff(4, argv); + if (!result) + self.resultPath = temporaryFile; +} + +@end + +static NSDictionary *infoForFile(FTSENT *ent) +{ + NSData *hash = hashOfFile(ent); + NSNumber *size = @0; + if (ent->fts_info != FTS_D) { + size = @(ent->fts_statp->st_size); + } + return @{@"hash": hash, @"type": @(ent->fts_info), @"size": size}; +} + +static NSString *absolutePath(NSString *path) +{ + NSURL *url = [[NSURL alloc] initFileURLWithPath:path]; + return [[url absoluteURL] path]; +} + +static NSString *temporaryPatchFile(NSString *patchFile) +{ + NSString *path = absolutePath(patchFile); + NSString *directory = [path stringByDeletingLastPathComponent]; + NSString *file = [path lastPathComponent]; + return [NSString stringWithFormat:@"%@/.%@.tmp", directory, file]; +} + +static BOOL shouldSkipDeltaCompression(NSString * __unused key, NSDictionary* originalInfo, NSDictionary *newInfo) +{ + unsigned long long fileSize = [newInfo[@"size"] unsignedLongLongValue]; + if (fileSize < 4096) { + return YES; + } + + if (!originalInfo) { + return YES; + } + + if ([originalInfo[@"type"] unsignedShortValue] != [newInfo[@"type"] unsignedShortValue]) { + return YES; + } + + return NO; +} + +static BOOL shouldDeleteThenExtract(NSString * __unused key, NSDictionary* originalInfo, NSDictionary *newInfo) +{ + if (!originalInfo) { + return NO; + } + + if ([originalInfo[@"type"] unsignedShortValue] != [newInfo[@"type"] unsignedShortValue]) { + return YES; + } + + return NO; +} + +int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFile) +{ + NSMutableDictionary *originalTreeState = [NSMutableDictionary dictionary]; + + const char *sourcePaths[] = {[source fileSystemRepresentation], 0}; + FTS *fts = fts_open((char* const*)sourcePaths, FTS_PHYSICAL | FTS_NOCHDIR, compareFiles); + if (!fts) { + perror("fts_open"); + return 1; + } + + fprintf(stdout, "Processing %s...", [source UTF8String]); + FTSENT *ent = 0; + while ((ent = fts_read(fts))) { + if (ent->fts_info != FTS_F && ent->fts_info != FTS_SL && ent->fts_info != FTS_D) { + continue; + } + + NSString *key = pathRelativeToDirectory(source, stringWithFileSystemRepresentation(ent->fts_path)); + if (![key length]) { + continue; + } + + NSDictionary *info = infoForFile(ent); + originalTreeState[key] = info; + } + fts_close(fts); + + NSString *beforeHash = hashOfTree(source); + + NSMutableDictionary *newTreeState = [NSMutableDictionary dictionary]; + for (NSString *key in originalTreeState) + { + newTreeState[key] = [NSNull null]; + } + + fprintf(stdout, "\nProcessing %s... ", [destination UTF8String]); + sourcePaths[0] = [destination fileSystemRepresentation]; + fts = fts_open((char* const*)sourcePaths, FTS_PHYSICAL | FTS_NOCHDIR, compareFiles); + if (!fts) { + perror("fts_open"); + return 1; + } + + + while ((ent = fts_read(fts))) { + if (ent->fts_info != FTS_F && ent->fts_info != FTS_SL && ent->fts_info != FTS_D) { + continue; + } + + NSString *key = pathRelativeToDirectory(destination, stringWithFileSystemRepresentation(ent->fts_path)); + if (![key length]) { + continue; + } + + NSDictionary *info = infoForFile(ent); + NSDictionary *oldInfo = originalTreeState[key]; + + if ([info isEqual:oldInfo]) { + [newTreeState removeObjectForKey:key]; + } else { + newTreeState[key] = info; + + if (oldInfo && [oldInfo[@"type"] unsignedShortValue] == FTS_D && [info[@"type"] unsignedShortValue] != FTS_D) { + NSArray *parentPathComponents = key.pathComponents; + + for (NSString *childPath in originalTreeState) { + NSArray *childPathComponents = childPath.pathComponents; + if (childPathComponents.count > parentPathComponents.count && + [parentPathComponents isEqualToArray:[childPathComponents subarrayWithRange:NSMakeRange(0, parentPathComponents.count)]]) { + [newTreeState removeObjectForKey:childPath]; + } + } + } + } + } + fts_close(fts); + + NSString *afterHash = hashOfTree(destination); + + 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]); + + NSOperationQueue *deltaQueue = [[NSOperationQueue alloc] init]; + NSMutableArray *deltaOperations = [NSMutableArray array]; + + // Sort the keys by preferring the ones from the original tree to appear first + // We want to enforce deleting before extracting in the case paths differ only by case + NSArray *keys = [[newTreeState allKeys] sortedArrayUsingComparator:^NSComparisonResult(NSString *key1, NSString *key2) { + NSComparisonResult insensitiveCompareResult = [key1 caseInsensitiveCompare:key2]; + if (insensitiveCompareResult != NSOrderedSame) { + return insensitiveCompareResult; + } + + return originalTreeState[key1] ? NSOrderedAscending : NSOrderedDescending; + }]; + for (NSString* key in keys) { + id value = [newTreeState valueForKey:key]; + + if ([value isEqual:[NSNull null]]) { + xar_file_t newFile = xar_add_frombuffer(x, 0, [key fileSystemRepresentation], (char *)"", 1); + assert(newFile); + xar_prop_set(newFile, "delete", "true"); + continue; + } + + NSDictionary *originalInfo = originalTreeState[key]; + NSDictionary *newInfo = newTreeState[key]; + if (shouldSkipDeltaCompression(key, 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)) { + xar_prop_set(newFile, "delete-then-extract", "true"); + } + } else { + CreateBinaryDeltaOperation *operation = [[CreateBinaryDeltaOperation alloc] initWithRelativePath:key oldTree:source newTree:destination]; + [deltaQueue addOperation:operation]; + [deltaOperations addObject:operation]; + } + } + + [deltaQueue waitUntilAllOperationsAreFinished]; + + for (CreateBinaryDeltaOperation *operation in deltaOperations) { + NSString *resultPath = [operation resultPath]; + xar_file_t newFile = xar_add_frompath(x, 0, [[operation relativePath] fileSystemRepresentation], [resultPath fileSystemRepresentation]); + assert(newFile); + xar_prop_set(newFile, "binary-delta", "true"); + unlink([resultPath fileSystemRepresentation]); + } + + xar_close(x); + + unlink([patchFile fileSystemRepresentation]); + link([temporaryFile fileSystemRepresentation], [patchFile fileSystemRepresentation]); + unlink([temporaryFile fileSystemRepresentation]); + fprintf(stdout, "Done!\n"); + + return 0; +} diff --git a/Sparkle/SUBinaryDeltaTool.m b/Sparkle/SUBinaryDeltaTool.m index e81d5951..c493d959 100644 --- a/Sparkle/SUBinaryDeltaTool.m +++ b/Sparkle/SUBinaryDeltaTool.m @@ -6,112 +6,13 @@ // Copyright 2009 Mark Rowe. All rights reserved. // -#define _DARWIN_NO_64_BIT_INODE 1 - -#include "SUBinaryDeltaCommon.h" #include "SUBinaryDeltaApply.h" -#include +#include "SUBinaryDeltaCreate.h" #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -extern int bsdiff(int argc, const char **argv); - -@interface CreateBinaryDeltaOperation : NSOperation -@property (copy) NSString *relativePath; -@property (strong) NSString *resultPath; -@property (strong) NSString *_fromPath; -@property (strong) NSString *_toPath; -- (id)initWithRelativePath:(NSString *)relativePath oldTree:(NSString *)oldTree newTree:(NSString *)newTree; -@end - -@implementation CreateBinaryDeltaOperation -@synthesize relativePath = _relativePath; -@synthesize resultPath = _resultPath; -@synthesize _fromPath = _fromPath; -@synthesize _toPath = _toPath; - -- (id)initWithRelativePath:(NSString *)relativePath oldTree:(NSString *)oldTree newTree:(NSString *)newTree +static void printUsage(void) { - if ((self = [super init])) { - self.relativePath = relativePath; - self._fromPath = [oldTree stringByAppendingPathComponent:relativePath]; - self._toPath = [newTree stringByAppendingPathComponent:relativePath]; - } - return self; -} - -- (void)main -{ - NSString *temporaryFile = temporaryFilename(@"BinaryDelta"); - const char *argv[] = {"/usr/bin/bsdiff", [self._fromPath fileSystemRepresentation], [self._toPath fileSystemRepresentation], [temporaryFile fileSystemRepresentation]}; - int result = bsdiff(4, argv); - if (!result) - self.resultPath = temporaryFile; -} - -@end - -static NSDictionary *infoForFile(FTSENT *ent) -{ - NSData *hash = hashOfFile(ent); - NSNumber *size = @0; - if (ent->fts_info != FTS_D) { - size = @(ent->fts_statp->st_size); - } - return @{@"hash": hash, @"type": @(ent->fts_info), @"size": size}; -} - -static NSString *absolutePath(NSString *path) -{ - NSURL *url = [[NSURL alloc] initFileURLWithPath:path]; - return [[url absoluteURL] path]; -} - -static NSString *temporaryPatchFile(NSString *patchFile) -{ - NSString *path = absolutePath(patchFile); - NSString *directory = [path stringByDeletingLastPathComponent]; - NSString *file = [path lastPathComponent]; - return [NSString stringWithFormat:@"%@/.%@.tmp", directory, file]; -} - -static BOOL shouldSkipDeltaCompression(NSString * __unused key, NSDictionary* originalInfo, NSDictionary *newInfo) -{ - unsigned long long fileSize = [newInfo[@"size"] unsignedLongLongValue]; - if (fileSize < 4096) { - return YES; - } - - if (!originalInfo) { - return YES; - } - - if ([originalInfo[@"type"] unsignedShortValue] != [newInfo[@"type"] unsignedShortValue]) { - return YES; - } - - return NO; -} - -static BOOL shouldDeleteThenExtract(NSString * __unused key, NSDictionary* originalInfo, NSDictionary *newInfo) -{ - if (!originalInfo) { - return NO; - } - - if ([originalInfo[@"type"] unsignedShortValue] != [newInfo[@"type"] unsignedShortValue]) { - return YES; - } - - return NO; + fprintf(stderr, "Usage: BinaryDelta [create | apply] before-tree after-tree patch-file\n"); } int main(int __unused argc, char __unused *argv[]) @@ -119,8 +20,7 @@ int main(int __unused argc, char __unused *argv[]) @autoreleasepool { NSArray *args = [[NSProcessInfo processInfo] arguments]; if (args.count != 5) { - usage: - fprintf(stderr, "Usage: BinaryDelta [create | apply] before-tree after-tree patch-file\n"); + printUsage(); return 1; } @@ -130,169 +30,26 @@ int main(int __unused argc, char __unused *argv[]) NSString *patchFile = args[4]; BOOL isDirectory; - [[NSFileManager defaultManager] fileExistsAtPath:oldPath isDirectory:&isDirectory]; - if (!isDirectory) { + if (![[NSFileManager defaultManager] fileExistsAtPath:oldPath isDirectory:&isDirectory] || !isDirectory) { fprintf(stderr, "Usage: before-tree must be a directory\n"); return 1; } - [[NSFileManager defaultManager] fileExistsAtPath:newPath isDirectory:&isDirectory]; - if (!isDirectory) { - fprintf(stderr, "Usage: after-tree must be a directory\n"); - return 1; - } - + int result; if ([command isEqualToString:@"apply"]) { - int result = applyBinaryDelta(oldPath, newPath, patchFile); - return result; - } - if (![command isEqualToString:@"create"]) { - goto usage; - } - - NSMutableDictionary *originalTreeState = [NSMutableDictionary dictionary]; - - const char *sourcePaths[] = {[oldPath fileSystemRepresentation], 0}; - FTS *fts = fts_open((char* const*)sourcePaths, FTS_PHYSICAL | FTS_NOCHDIR, compareFiles); - if (!fts) { - perror("fts_open"); - return 1; - } - - fprintf(stdout, "Processing %s...", [oldPath UTF8String]); - FTSENT *ent = 0; - while ((ent = fts_read(fts))) { - if (ent->fts_info != FTS_F && ent->fts_info != FTS_SL && ent->fts_info != FTS_D) { - continue; - } - - NSString *key = pathRelativeToDirectory(oldPath, stringWithFileSystemRepresentation(ent->fts_path)); - if (![key length]) { - continue; - } - - NSDictionary *info = infoForFile(ent); - originalTreeState[key] = info; - } - fts_close(fts); - - NSString *beforeHash = hashOfTree(oldPath); - - NSMutableDictionary *newTreeState = [NSMutableDictionary dictionary]; - for (NSString *key in originalTreeState) - { - newTreeState[key] = [NSNull null]; - } - - fprintf(stdout, "\nProcessing %s... ", [newPath UTF8String]); - sourcePaths[0] = [newPath fileSystemRepresentation]; - fts = fts_open((char* const*)sourcePaths, FTS_PHYSICAL | FTS_NOCHDIR, compareFiles); - if (!fts) { - perror("fts_open"); - return 1; - } - - - while ((ent = fts_read(fts))) { - if (ent->fts_info != FTS_F && ent->fts_info != FTS_SL && ent->fts_info != FTS_D) { - continue; - } - - NSString *key = pathRelativeToDirectory(newPath, stringWithFileSystemRepresentation(ent->fts_path)); - if (![key length]) { - continue; - } - - NSDictionary *info = infoForFile(ent); - NSDictionary *oldInfo = originalTreeState[key]; - - if ([info isEqual:oldInfo]) { - [newTreeState removeObjectForKey:key]; + result = applyBinaryDelta(oldPath, newPath, patchFile); + } else if ([command isEqualToString:@"create"]) { + if (![[NSFileManager defaultManager] fileExistsAtPath:newPath isDirectory:&isDirectory] || !isDirectory) { + result = 1; + fprintf(stderr, "Usage: after-tree must be a directory\n"); } else { - newTreeState[key] = info; - - if (oldInfo && [oldInfo[@"type"] unsignedShortValue] == FTS_D && [info[@"type"] unsignedShortValue] != FTS_D) { - NSArray *parentPathComponents = key.pathComponents; - - for (NSString *childPath in originalTreeState) { - NSArray *childPathComponents = childPath.pathComponents; - if (childPathComponents.count > parentPathComponents.count && - [parentPathComponents isEqualToArray:[childPathComponents subarrayWithRange:NSMakeRange(0, parentPathComponents.count)]]) { - [newTreeState removeObjectForKey:childPath]; - } - } - } - } - } - fts_close(fts); - - NSString *afterHash = hashOfTree(newPath); - - 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]); - - NSOperationQueue *deltaQueue = [[NSOperationQueue alloc] init]; - NSMutableArray *deltaOperations = [NSMutableArray array]; - - // Sort the keys by preferring the ones from the original tree to appear first - // We want to enforce deleting before extracting in the case paths differ only by case - NSArray *keys = [[newTreeState allKeys] sortedArrayUsingComparator:^NSComparisonResult(NSString *key1, NSString *key2) { - NSComparisonResult insensitiveCompareResult = [key1 caseInsensitiveCompare:key2]; - if (insensitiveCompareResult != NSOrderedSame) { - return insensitiveCompareResult; - } - - return originalTreeState[key1] ? NSOrderedAscending : NSOrderedDescending; - }]; - for (NSString* key in keys) { - id value = [newTreeState valueForKey:key]; - - if ([value isEqual:[NSNull null]]) { - xar_file_t newFile = xar_add_frombuffer(x, 0, [key fileSystemRepresentation], (char *)"", 1); - assert(newFile); - xar_prop_set(newFile, "delete", "true"); - continue; - } - - NSDictionary *originalInfo = originalTreeState[key]; - NSDictionary *newInfo = newTreeState[key]; - if (shouldSkipDeltaCompression(key, originalInfo, newInfo)) { - NSString *path = [newPath stringByAppendingPathComponent:key]; - xar_file_t newFile = xar_add_frompath(x, 0, [key fileSystemRepresentation], [path fileSystemRepresentation]); - assert(newFile); - if (shouldDeleteThenExtract(key, originalInfo, newInfo)) { - xar_prop_set(newFile, "delete-then-extract", "true"); - } - } else { - CreateBinaryDeltaOperation *operation = [[CreateBinaryDeltaOperation alloc] initWithRelativePath:key oldTree:oldPath newTree:newPath]; - [deltaQueue addOperation:operation]; - [deltaOperations addObject:operation]; + result = createBinaryDelta(oldPath, newPath, patchFile); } + } else { + result = 1; + printUsage(); } - [deltaQueue waitUntilAllOperationsAreFinished]; - - for (CreateBinaryDeltaOperation *operation in deltaOperations) { - NSString *resultPath = [operation resultPath]; - xar_file_t newFile = xar_add_frompath(x, 0, [[operation relativePath] fileSystemRepresentation], [resultPath fileSystemRepresentation]); - assert(newFile); - xar_prop_set(newFile, "binary-delta", "true"); - unlink([resultPath fileSystemRepresentation]); - } - - xar_close(x); - - unlink([patchFile fileSystemRepresentation]); - link([temporaryFile fileSystemRepresentation], [patchFile fileSystemRepresentation]); - unlink([temporaryFile fileSystemRepresentation]); - fprintf(stdout, "Done!\n"); - - return 0; + return result; } }