diff --git a/Autoupdate/SPUDeltaArchive.h b/Autoupdate/SPUDeltaArchive.h index f69bcde9..e0309df7 100644 --- a/Autoupdate/SPUDeltaArchive.h +++ b/Autoupdate/SPUDeltaArchive.h @@ -9,10 +9,11 @@ #import @protocol SPUDeltaArchiveProtocol; +@class SPUDeltaArchiveHeader; NS_ASSUME_NONNULL_BEGIN -id _Nullable SPUDeltaArchiveForReading(NSString *patchFile); -id _Nullable SPUDeltaArchiveForWriting(NSString *patchFile); +// Opens patch file for reading and decodes the archive header +id SPUDeltaArchiveReadPatchAndHeader(NSString *patchFile, SPUDeltaArchiveHeader * _Nullable __autoreleasing * _Nullable outHeader); NS_ASSUME_NONNULL_END diff --git a/Autoupdate/SPUDeltaArchive.m b/Autoupdate/SPUDeltaArchive.m index a2d889cf..b7e65eff 100644 --- a/Autoupdate/SPUDeltaArchive.m +++ b/Autoupdate/SPUDeltaArchive.m @@ -8,17 +8,107 @@ #import "SPUDeltaArchive.h" #import "SPUDeltaArchiveProtocol.h" +#import "SPUSparkleDeltaArchive.h" #import "SPUXarDeltaArchive.h" +#import #include "AppKitPrevention.h" -id _Nullable SPUDeltaArchiveForReading(NSString *patchFile) +SPUDeltaCompressionMode SPUDeltaCompressionModeDefault = (SPUDeltaCompressionMode)UINT8_MAX; + +id SPUDeltaArchiveReadPatchAndHeader(NSString *patchFile, SPUDeltaArchiveHeader * _Nullable __autoreleasing * _Nullable outHeader) { - return [[SPUXarDeltaArchive alloc] initWithPatchFileForReading:patchFile]; + id sparkleArchive = [[SPUSparkleDeltaArchive alloc] initWithPatchFileForReading:patchFile]; + + SPUDeltaArchiveHeader *header = [sparkleArchive readHeader]; + if (header == nil) { + NSError *archiveError = sparkleArchive.error; + if (archiveError != nil && [archiveError.domain isEqualToString:SPARKLE_DELTA_ARCHIVE_ERROR_DOMAIN] && archiveError.code == SPARKLE_DELTA_ARCHIVE_ERROR_CODE_BAD_MAGIC) { + // Retry with XAR archive if the magic value is unexpected + [sparkleArchive close]; + + id xarArchive = [[SPUXarDeltaArchive alloc] initWithPatchFileForReading:patchFile]; + + SPUDeltaArchiveHeader *xarHeader = [xarArchive readHeader]; + if (outHeader != NULL) { + *outHeader = xarHeader; + } + return xarArchive; + } else { + if (outHeader != NULL) { + *outHeader = nil; + } + return sparkleArchive; + } + } else { + if (outHeader != NULL) { + *outHeader = header; + } + return sparkleArchive; + } } -id _Nullable SPUDeltaArchiveForWriting(NSString *patchFile) +@implementation SPUDeltaArchiveItem + +@synthesize relativeFilePath = _relativeFilePath; +@synthesize physicalFilePath = _physicalFilePath; +@synthesize clonedRelativePath = _clonedRelativePath; +@synthesize commands = _commands; +@synthesize permissions = _permissions; +@synthesize context = _context; +@synthesize originalMode = _originalMode; +@synthesize codedDataLength = _codedDataLength; + +- (instancetype)initWithRelativeFilePath:(NSString *)relativeFilePath commands:(SPUDeltaItemCommands)commands permissions:(uint16_t)permissions { - return [[SPUXarDeltaArchive alloc] initWithPatchFileForWriting:patchFile]; + self = [super init]; + if (self != nil) { + _relativeFilePath = [relativeFilePath copy]; + _commands = commands; + _permissions = permissions; + } + return self; } + +@end + +@implementation SPUDeltaArchiveHeader +{ + unsigned char _beforeTreeHash[CC_SHA1_DIGEST_LENGTH]; + unsigned char _afterTreeHash[CC_SHA1_DIGEST_LENGTH]; +} + +@synthesize majorVersion = _majorVersion; +@synthesize minorVersion = _minorVersion; + +- (instancetype)initWithMajorVersion:(uint16_t)majorVersion minorVersion:(uint16_t)minorVersion beforeTreeHash:(const unsigned char *)beforeTreeHash afterTreeHash:(const unsigned char *)afterTreeHash +{ + self = [super init]; + if (self != nil) + { + _majorVersion = majorVersion; + _minorVersion = minorVersion; + + memcpy(_beforeTreeHash, beforeTreeHash, sizeof(_beforeTreeHash)); + memcpy(_afterTreeHash, afterTreeHash, sizeof(_afterTreeHash)); + } + return self; +} + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdirect-ivar-access" + +- (unsigned char *)beforeTreeHash +{ + return _beforeTreeHash; +} + +- (unsigned char *)afterTreeHash +{ + return _afterTreeHash; +} + +#pragma clang diagnostic pop + +@end diff --git a/Autoupdate/SPUDeltaArchiveProtocol.h b/Autoupdate/SPUDeltaArchiveProtocol.h index a96ac831..ff2d5c77 100644 --- a/Autoupdate/SPUDeltaArchiveProtocol.h +++ b/Autoupdate/SPUDeltaArchiveProtocol.h @@ -11,41 +11,101 @@ NS_ASSUME_NONNULL_BEGIN // Attributes for an item we extract/write to the archive -typedef NS_ENUM(uint8_t, SPUDeltaFileAttributes) { - SPUDeltaFileAttributesDelete = (1u << 0), - SPUDeltaFileAttributesExtract = (1u << 1), - SPUDeltaFileAttributesModifyPermissions = (1u << 2), - SPUDeltaFileAttributesBinaryDiff = (1u << 3), + +// Note: BinaryDiff cannot coexist together with Delete +typedef NS_ENUM(uint8_t, SPUDeltaItemCommands) { + SPUDeltaItemCommandEndMarker = 0, + SPUDeltaItemCommandDelete = (1u << 0), + SPUDeltaItemCommandExtract = (1u << 1), + SPUDeltaItemCommandModifyPermissions = (1u << 2), + SPUDeltaItemCommandBinaryDiff = (1u << 3), + SPUDeltaItemCommandClone = (1u << 4), }; +// Compression mode to use during patch creation +typedef NS_ENUM(uint8_t, SPUDeltaCompressionMode) { + SPUDeltaCompressionModeNone = 0, + SPUDeltaCompressionModeBzip2, + SPUDeltaCompressionModeLZMA, + SPUDeltaCompressionModeLZFSE, + SPUDeltaCompressionModeLZ4, + SPUDeltaCompressionModeZLIB +}; + +// For Swift access +extern SPUDeltaCompressionMode SPUDeltaCompressionModeDefault; + +#define DEFAULT_BZIP2_COMPRESSION_LEVEL 9 + +// Represents header for our archive +@interface SPUDeltaArchiveHeader : NSObject + +- (instancetype)initWithMajorVersion:(uint16_t)majorVersion minorVersion:(uint16_t)minorVersion beforeTreeHash:(const unsigned char *)beforeTreeHash afterTreeHash:(const unsigned char *)afterTreeHash; + +@property (nonatomic, readonly) uint16_t majorVersion; +@property (nonatomic, readonly) uint16_t minorVersion; +@property (nonatomic, readonly) unsigned char *beforeTreeHash; +@property (nonatomic, readonly) unsigned char *afterTreeHash; + +@end + +// 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; + +@property (nonatomic, readonly) NSString *relativeFilePath; +@property (nonatomic, nullable) NSString *physicalFilePath; +@property (nonatomic, nullable) NSString *clonedRelativePath; +@property (nonatomic, readonly) SPUDeltaItemCommands commands; +@property (nonatomic, readonly) uint16_t permissions; + +// Private properties +@property (nonatomic, nullable) const void *context; +@property (nonatomic) uint16_t originalMode; +@property (nonatomic) uint64_t codedDataLength; + +@end + // A protocol for reading and writing binary delta patches +// Operations must be done in order. The header must first be read or written before any other operations. +// For reading, file items cannot be extracted out of order. @protocol SPUDeltaArchiveProtocol @property (nonatomic, readonly, class) BOOL maySupportSafeExtraction; +// If non-nil, there was an error with reading or writing data from the archive +@property (nonatomic, readonly, nullable) NSError *error; + // Closes file for reading/writing, called in -dealloc if it's not called manually - (void)close; // For reading // Retrieves metadata for the archive including major/minor version and expected bundle hashes -- (void)getMajorDeltaVersion:(nullable uint16_t *)outMajorDiffVersion minorDeltaVersion:(nullable uint16_t *)outMinorDiffVersion beforeTreeHash:(NSString * _Nullable __autoreleasing * _Nullable)outBeforeTreeHash afterTreeHash:(NSString * _Nullable __autoreleasing * _Nullable)outAfterTreeHash; +- (nullable SPUDeltaArchiveHeader *)readHeader; // Enumerate through items in the patch file and read the path, attributes, permissions (if permission attribute is available), and way to stop enumeration -- (BOOL)enumerateItems:(void (^)(const void *item, NSString *relativePath, SPUDeltaFileAttributes attributes, uint16_t permissions, BOOL *stop))itemHandler; +- (void)enumerateItems:(void (^)(SPUDeltaArchiveItem *item, BOOL *stop))itemHandler; // Extract a file item from the patch file to a destination file -- (BOOL)extractItem:(const void *)item destination:(NSString *)destinationPath; +// The item's physical file path must be set as a destination +- (BOOL)extractItem:(SPUDeltaArchiveItem *)item; + +// ------------ // For writing // Set metadata for archive including major/minor version and expected bundle hashes -- (void)setMajorVersion:(uint16_t)majorVersion minorVersion:(uint16_t)minorVersion beforeTreeHash:(NSString *)beforeTreeHash afterTreeHash:(NSString *)afterTreeHash; +- (void)writeHeader:(SPUDeltaArchiveHeader *)header; // Add item to patch file -// File path must be provided if there is a extract or binary delta attribute +// Physical file path must be provided if there is an extract or binary delta attribute // Permissions are used only if there is a modify permissions attribute -- (void)addRelativeFilePath:(NSString *)relativeFilePath realFilePath:(nullable NSString *)filePath attributes:(SPUDeltaFileAttributes)attributes permissions:(uint16_t)permissions; +- (void)addItem:(SPUDeltaArchiveItem *)item; + +// Finishes encoding items after having added all of them +- (void)finishEncodingItems; @end diff --git a/Autoupdate/SPUSparkleDeltaArchive.h b/Autoupdate/SPUSparkleDeltaArchive.h new file mode 100644 index 00000000..2bea2285 --- /dev/null +++ b/Autoupdate/SPUSparkleDeltaArchive.h @@ -0,0 +1,74 @@ +// +// SPUSparkleDeltaArchive.h +// Sparkle +// +// Created by Mayur Pawashe on 12/30/21. +// Copyright © 2021 Sparkle Project. All rights reserved. +// + +#import + +#import "SPUDeltaArchiveProtocol.h" + +NS_ASSUME_NONNULL_BEGIN + +#define SPARKLE_DELTA_ARCHIVE_ERROR_DOMAIN @"Sparkle Delta Archive" +#define SPARKLE_DELTA_ARCHIVE_ERROR_CODE_BAD_MAGIC 1 +#define SPARKLE_DELTA_ARCHIVE_ERROR_CODE_BAD_COMPRESSION_VALUE 2 +#define SPARKLE_DELTA_ARCHIVE_ERROR_CODE_BAD_CHUNK_SIZE 3 +#define SPARKLE_DELTA_ARCHIVE_ERROR_CODE_BAD_CLONE_LOOKUP 4 +#define SPARKLE_DELTA_ARCHIVE_ERROR_CODE_TOO_MANY_FILES 5 +#define SPARKLE_DELTA_ARCHIVE_ERROR_CODE_LINK_TOO_LONG 6 + +/* + Modern container format for binary delta archives. + + Delta archive format has four sections which are the header, the relative file path table, the commands, and the data blobs. + + The relative file path table records all the file paths the archive needs to know about. + The commands are the operations to be recorded (eg: extract, clone, binary diff, delete). + The commands may additionally have more metadata such as file permission modes, relative path indexes in the case of clones, or file sizes for the data blobs. + The data blobs contain all file data from extract and binary diff outputs. + + The implementation design of this archive is such that we do not seek backwards or skip ahead to fetch data. + We go through the archive when writing or reading from it in a single pass. + + -- UNCOMPRESSED -- + + [ HEADER ] + magic (length: 4) + compression (length: 1) + + -- COMPRESSED -- + + [ HEADER (part 2)] + majorVersion (length: 2) + minorVersion (length: 2) + beforeTreeHash (length: 40) + afterTreeHash (length: 40) + + [ RELATIVE FILE PATH TABLE ] + sizeOfRelativeFilePathTable (length: 8 bytes) + List of null terminated path strings joined together (N paths) + + [ COMMANDS ] + [ Command ] + Set of command types for entry (length: 1 byte) + Additional metadata for command + (M commands where M <= N paths) + (Indexes for commands refer to indexes to relative file path table, excluding extraneous trailing entries in relative path table used for clones) + (Last command denotes a null end marker) + + [ DATA BLOBS ] + All raw binary data joined together + (P number of blobs where P <= M commands) + (Indexes for data blobs refer to indexes for a filtered list of applicable commands that have data content) + */ +@interface SPUSparkleDeltaArchive : NSObject + +- (instancetype)initWithPatchFileForWriting:(NSString *)patchFile compression:(SPUDeltaCompressionMode)compression compressionLevel:(int32_t)compressionLevel; +- (instancetype)initWithPatchFileForReading:(NSString *)patchFile; + +@end + +NS_ASSUME_NONNULL_END diff --git a/Autoupdate/SPUSparkleDeltaArchive.m b/Autoupdate/SPUSparkleDeltaArchive.m new file mode 100644 index 00000000..b7851ff2 --- /dev/null +++ b/Autoupdate/SPUSparkleDeltaArchive.m @@ -0,0 +1,1104 @@ +// +// SPUSparkleDeltaArchive.m +// Sparkle +// +// Created by Mayur Pawashe on 12/30/21. +// Copyright © 2021 Sparkle Project. All rights reserved. +// + +#import "SPUSparkleDeltaArchive.h" +#import +#import +#import +#import "SUBinaryDeltaCommon.h" +#import + +#include "AppKitPrevention.h" + +#define SPARKLE_DELTA_FORMAT_MAGIC "spk!" +#define PARTIAL_IO_CHUNK_SIZE 16384 // this must be >= PATH_MAX +#define COMPRESSION_BUFFER_SIZE 65536 +#define SPARKLE_BZIP2_ERROR_DOMAIN @"Sparkle BZIP2" +#define SPARKLE_COMPRESSION_ERROR_DOMAIN @"Sparkle Compression" + +@interface SPUSparkleDeltaArchive () + +@property (nonatomic) FILE *file; +@property (nonatomic) BZFILE *bzipFile; +@property (nonatomic) compression_stream compressionStream; +@property (nonatomic) BOOL initializedCompressionStream; +@property (nonatomic, readonly) NSString *patchFile; +@property (nonatomic) SPUDeltaCompressionMode compression; +@property (nonatomic, readonly) BOOL writeMode; +@property (nonatomic) NSError *error; +@property (nonatomic) void *partialChunkBuffer; +@property (nonatomic) void *compressionBuffer; + +@property (nonatomic) NSMutableArray *writableItems; +@property (nonatomic) int32_t writableCompressionLevel; + +@end + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdirect-ivar-access" + +@implementation SPUSparkleDeltaArchive + +@synthesize file = _file; +@synthesize bzipFile = _bzipFile; +@synthesize compressionStream = _compressionStream; +@synthesize initializedCompressionStream = _initializedCompressionStream; +@synthesize patchFile = _patchFile; +@synthesize compression = _compression; +@synthesize writeMode = _writeMode; +@synthesize writableItems = _writableItems; +@synthesize writableCompressionLevel = _writableCompressionLevel; +@synthesize error = _error; +@synthesize partialChunkBuffer = _partialChunkBuffer; +@synthesize compressionBuffer = _compressionBuffer; + ++ (BOOL)maySupportSafeExtraction +{ + return YES; +} + +- (instancetype)initWithPatchFileForWriting:(NSString *)patchFile compression:(SPUDeltaCompressionMode)compression compressionLevel:(int32_t)compressionLevel +{ + self = [super init]; + if (self != nil) { + _patchFile = [patchFile copy]; + _writableItems = [NSMutableArray array]; + _compression = (compression == SPUDeltaCompressionModeDefault) ? SPUDeltaCompressionModeLZMA : compression; + _writableCompressionLevel = compressionLevel; + _writeMode = YES; + } + return self; +} + +- (instancetype)initWithPatchFileForReading:(NSString *)patchFile +{ + self = [super init]; + if (self != nil) { + _patchFile = [patchFile copy]; + } + return self; +} + +- (void)dealloc +{ + [self close]; +} + +- (void)close +{ + if (self.bzipFile != NULL) { + if (!self.writeMode) { + int bzerror = 0; + BZ2_bzReadClose(&bzerror, self.bzipFile); + } + + self.bzipFile = NULL; + } else if (self.initializedCompressionStream) { + compression_stream_destroy(&_compressionStream); + self.initializedCompressionStream = NO; + } + + if (self.file != NULL) { + fclose(self.file); + self.file = NULL; + } + + free(self.partialChunkBuffer); + self.partialChunkBuffer = NULL; + + free(self.compressionBuffer); + self.compressionBuffer = NULL; +} + +- (BOOL)createBuffers +{ + self.partialChunkBuffer = calloc(1, PARTIAL_IO_CHUNK_SIZE); + if (self.partialChunkBuffer == NULL) { + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to calloc() %d bytes for partial chunk buffer.", PARTIAL_IO_CHUNK_SIZE] }]; + return NO; + } + + if (self.initializedCompressionStream) { + self.compressionBuffer = calloc(1, COMPRESSION_BUFFER_SIZE); + if (self.compressionBuffer == NULL) { + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to calloc() %d bytes for compression buffer.", COMPRESSION_BUFFER_SIZE] }]; + return NO; + } + } + + return YES; +} + +- (BOOL)_readBuffer:(void *)buffer length:(int32_t)length +{ + if (self.error != nil) { + return NO; + } + + switch (self.compression) { + case SPUDeltaCompressionModeNone: { + if (fread(buffer, (size_t)length, 1, self.file) < 1) { + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to read %d uncompressed bytes from archive.", length] }]; + return NO; + } else { + return YES; + } + } + case SPUDeltaCompressionModeBzip2: { + int bzerror = 0; + int bytesRead = BZ2_bzRead(&bzerror, self.bzipFile, buffer, length); + + switch (bzerror) { + case BZ_OK: + case BZ_STREAM_END: + if (bytesRead < length) { + self.error = [NSError errorWithDomain:SPARKLE_BZIP2_ERROR_DOMAIN code:0 userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Only %d out of %d expected bytes were read from the bz2 archive.", bytesRead, length] }]; + return NO; + } else { + return YES; + } + case BZ_IO_ERROR: + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: @"Encountered unexpected IO error when reading compressed bytes from bz2 archive." }]; + return NO; + default: + self.error = [NSError errorWithDomain:SPARKLE_BZIP2_ERROR_DOMAIN code:bzerror userInfo:@{ NSLocalizedDescriptionKey: @"Encountered unexpected error when reading compressed bytes from bz2 archive." }]; + return NO; + } + } + case SPUDeltaCompressionModeLZMA: + case SPUDeltaCompressionModeLZFSE: + case SPUDeltaCompressionModeLZ4: + case SPUDeltaCompressionModeZLIB: { + FILE *file = self.file; + void *compressionBuffer = self.compressionBuffer; + + _compressionStream.dst_ptr = buffer; + _compressionStream.dst_size = (size_t)length; + + while (_compressionStream.dst_size > 0) { + // Go through the current incomplete chunk before reading another one + + if (_compressionStream.src_size == 0) { + size_t bytesRead = fread(compressionBuffer, 1, COMPRESSION_BUFFER_SIZE, file); + if (bytesRead < COMPRESSION_BUFFER_SIZE) { + if (feof(file) == 0) { + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to read %d compressed raw bytes from archive.", length] }]; + return NO; + } + } + + // Reset source buffer + _compressionStream.src_ptr = compressionBuffer; + _compressionStream.src_size = bytesRead; + } + + compression_status status = compression_stream_process(&_compressionStream, feof(file) != 0 ? COMPRESSION_STREAM_FINALIZE : 0); + if (status == COMPRESSION_STATUS_ERROR) { + self.error = [NSError errorWithDomain:SPARKLE_COMPRESSION_ERROR_DOMAIN code:COMPRESSION_STATUS_ERROR userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to read %d compressed bytes.", length] }]; + return NO; + } + + if (status == COMPRESSION_STATUS_END && _compressionStream.dst_size > 0) { + // We're expecting more bytes but we can't read any more bytes + self.error = [NSError errorWithDomain:SPARKLE_DELTA_ARCHIVE_ERROR_DOMAIN code:-1 userInfo:@{ NSLocalizedDescriptionKey: @"Failed to decompress and read bytes because we reached EOF" }]; + return NO; + } + } + + return YES; + } + } +} + +static compression_algorithm _compressionAlgorithmForMode(SPUDeltaCompressionMode compressionMode) +{ + switch (compressionMode) { + case SPUDeltaCompressionModeLZMA: + return COMPRESSION_LZMA; + case SPUDeltaCompressionModeLZFSE: + return COMPRESSION_LZFSE; + case SPUDeltaCompressionModeLZ4: + return COMPRESSION_LZ4; + case SPUDeltaCompressionModeZLIB: + return COMPRESSION_ZLIB; + case SPUDeltaCompressionModeNone: + case SPUDeltaCompressionModeBzip2: + assert(false); + } + + assert(false); +} + +- (nullable SPUDeltaArchiveHeader *)readHeader +{ + NSString *patchFile = self.patchFile; + + char patchFilePath[PATH_MAX + 1] = {0}; + if (![patchFile getFileSystemRepresentation:patchFilePath maxLength:sizeof(patchFilePath) - 1]) { + self.error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadInvalidFileNameError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to open and represent as a file system representation: %@", patchFile] }]; + return nil; + } + + FILE *file = fopen(patchFilePath, "rb"); + if (file == NULL) { + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to open patch file for writing value due to io error: %@", patchFile] }]; + return nil; + } + + self.file = file; + + char magic[5] = {0}; + if (fread(magic, sizeof(magic) - 1, 1, file) < 1) { + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to read magic value from patch file: %@", patchFile] }]; + return nil; + } + + if (strncmp(magic, SPARKLE_DELTA_FORMAT_MAGIC, sizeof(magic) - 1) != 0) { + self.error = [NSError errorWithDomain:SPARKLE_DELTA_ARCHIVE_ERROR_DOMAIN code:SPARKLE_DELTA_ARCHIVE_ERROR_CODE_BAD_MAGIC userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Patch file does not have '%@' magic value", @SPARKLE_DELTA_FORMAT_MAGIC] }]; + return nil; + } + + SPUDeltaCompressionMode compression = 0; + if (fread(&compression, sizeof(compression), 1, file) < 1) { + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to read compression value from patch file: %@", patchFile] }]; + return nil; + } + + self.compression = compression; + + switch (compression) { + case SPUDeltaCompressionModeNone: + break; + case SPUDeltaCompressionModeBzip2: { + int bzerror = 0; + + BZFILE *bzipFile = BZ2_bzReadOpen(&bzerror, file, 0, 0, NULL, 0); + if (bzipFile == NULL) { + switch (bzerror) { + case BZ_IO_ERROR: + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to open patch as bz2 file due to io error: %@", patchFile] }]; + break; + default: + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:bzerror userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to open patch as bz2 file: %@", patchFile] }]; + break; + } + return nil; + } + + self.bzipFile = bzipFile; + + break; + } + case SPUDeltaCompressionModeLZMA: + case SPUDeltaCompressionModeLZFSE: + case SPUDeltaCompressionModeLZ4: + case SPUDeltaCompressionModeZLIB: { + if (compression_stream_init(&_compressionStream, COMPRESSION_STREAM_DECODE, _compressionAlgorithmForMode(compression)) != COMPRESSION_STATUS_OK) { + self.error = [NSError errorWithDomain:SPARKLE_COMPRESSION_ERROR_DOMAIN code:COMPRESSION_STATUS_ERROR userInfo:@{ NSLocalizedDescriptionKey: @"Failed to open compression stream for reading" }]; + return nil; + } + + self.initializedCompressionStream = YES; + + break; + } + default: + self.error = [NSError errorWithDomain:SPARKLE_DELTA_ARCHIVE_ERROR_DOMAIN code:SPARKLE_DELTA_ARCHIVE_ERROR_CODE_BAD_COMPRESSION_VALUE userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Compression value read %d is not recognized.", compression] }]; + return nil; + } + + if (![self createBuffers]) { + return nil; + } + + uint16_t majorVersion = 0; + if (![self _readBuffer:&majorVersion length:sizeof(majorVersion)]) { + return nil; + } + + uint16_t minorVersion = 0; + if (![self _readBuffer:&minorVersion length:sizeof(minorVersion)]) { + return nil; + } + + unsigned char beforeTreeHash[CC_SHA1_DIGEST_LENGTH] = {0}; + if (![self _readBuffer:beforeTreeHash length:sizeof(beforeTreeHash)]) { + return nil; + } + + unsigned char afterTreeHash[CC_SHA1_DIGEST_LENGTH] = {0}; + if (![self _readBuffer:afterTreeHash length:sizeof(afterTreeHash)]) { + return nil; + } + + return [[SPUDeltaArchiveHeader alloc] initWithMajorVersion:majorVersion minorVersion:minorVersion beforeTreeHash:beforeTreeHash afterTreeHash:afterTreeHash]; +} + +- (NSArray *)_readRelativeFilePaths +{ + if (self.error != nil) { + return nil; + } + + uint64_t filePathSectionSize = 0; + if (![self _readBuffer:&filePathSectionSize length:sizeof(filePathSectionSize)]) { + return nil; + } + + if (filePathSectionSize == 0) { + // Nothing has actually changed if there are no entries + return @[]; + } + + char *fileTableData = calloc(1, filePathSectionSize); + if (fileTableData == NULL) { + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to calloc() %llu bytes for relative file paths.", filePathSectionSize] }]; + return nil; + } + + { + // Read all the paths in chunks + uint64_t bytesLeftoverToCopy = filePathSectionSize; + while (bytesLeftoverToCopy > 0) { + uint64_t currentBlockSize = (bytesLeftoverToCopy >= PARTIAL_IO_CHUNK_SIZE) ? PARTIAL_IO_CHUNK_SIZE : bytesLeftoverToCopy; + + if (![self _readBuffer:fileTableData + (filePathSectionSize - bytesLeftoverToCopy) length:(int32_t)currentBlockSize]) { + free(fileTableData); + return nil; + } + + bytesLeftoverToCopy -= currentBlockSize; + } + } + + // Read all relative file paths separated by null terminators + NSFileManager *fileManager = [NSFileManager defaultManager]; + NSMutableArray *relativeFilePaths = [[NSMutableArray alloc] init]; + uint64_t currentStartIndex = 0; + for (uint64_t index = 0; index < filePathSectionSize; index++) { + if (fileTableData[index] == '\0') { + NSString *relativePath = [fileManager stringWithFileSystemRepresentation:&fileTableData[currentStartIndex] length:index - currentStartIndex]; + if (relativePath == nil) { + self.error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadInvalidFileNameError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Relative path cannot be decoded as a file system representation: %@", relativePath] }]; + + free(fileTableData); + return nil; + } + + currentStartIndex = index + 1; + [relativeFilePaths addObject:relativePath]; + } + } + + free(fileTableData); + + return relativeFilePaths; +} + +- (void)enumerateItems:(void (^)(SPUDeltaArchiveItem * _Nonnull, BOOL * _Nonnull))itemHandler +{ + // Parse all relative file paths + NSArray *relativeFilePaths = [self _readRelativeFilePaths]; + if (relativeFilePaths == nil) { + return; + } + + if (relativeFilePaths.count == 0) { + // No diff changes + return; + } + + if (relativeFilePaths.count > UINT32_MAX) { + // Very unlikely but we should guard against this + // Clones rely on 32-bit indexes + self.error = [NSError errorWithDomain:SPARKLE_DELTA_ARCHIVE_ERROR_DOMAIN code:SPARKLE_DELTA_ARCHIVE_ERROR_CODE_TOO_MANY_FILES userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"There are too many file entries to apply a patch for (more than %u)", UINT32_MAX] }]; + return; + } + + // Parse through all commands + NSMutableArray *archiveItems = [[NSMutableArray alloc] init]; + { + uint64_t currentItemIndex = 0; + while (YES) { + SPUDeltaItemCommands commands = 0; + if (![self _readBuffer:&commands length:sizeof(commands)]) { + break; + } + + // Test if we're done + if (commands == SPUDeltaItemCommandEndMarker) { + break; + } + + // Check if we need to decode additional data + uint16_t decodedMode = 0; + uint64_t decodedDataLength = 0; + NSString *clonedRelativePath = nil; + + if ((commands & SPUDeltaItemCommandClone) != 0) { + if ((commands & SPUDeltaItemCommandModifyPermissions) != 0) { + // Decode file permission changes for clone + if (![self _readBuffer:&decodedMode length:sizeof(decodedMode)]) { + break; + } + } + + // Decode relative file path for original source file + uint32_t cloneRelativePathIndex = 0; + if (![self _readBuffer:&cloneRelativePathIndex length:sizeof(cloneRelativePathIndex)]) { + break; + } + + if ((NSUInteger)cloneRelativePathIndex >= relativeFilePaths.count) { + self.error = [NSError errorWithDomain:SPARKLE_DELTA_ARCHIVE_ERROR_DOMAIN code:SPARKLE_DELTA_ARCHIVE_ERROR_CODE_BAD_CLONE_LOOKUP userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Index %u is past relative path table bounds of length %lu", cloneRelativePathIndex, (unsigned long)relativeFilePaths.count] }]; + break; + } + + clonedRelativePath = relativeFilePaths[cloneRelativePathIndex]; + } else if ((commands & SPUDeltaItemCommandExtract) != 0 || (commands & SPUDeltaItemCommandBinaryDiff) != 0) { + if (![self _readBuffer:&decodedMode length:sizeof(decodedMode)]) { + break; + } + + // Decode data length + // Length doesn't matter for directory names (we already track the name in the relative path) + if (S_ISREG(decodedMode)) { + if (![self _readBuffer:&decodedDataLength length:sizeof(decodedDataLength)]) { + break; + } + } else if (S_ISLNK(decodedMode)) { + uint16_t decodedLinkLength = 0; + if (![self _readBuffer:&decodedLinkLength length:sizeof(decodedLinkLength)]) { + break; + } + + decodedDataLength = decodedLinkLength; + } + } else if ((commands & SPUDeltaItemCommandModifyPermissions) != 0) { + // Decode file permissions + if (![self _readBuffer:&decodedMode length:sizeof(decodedMode)]) { + break; + } + } + + SPUDeltaArchiveItem *archiveItem = [[SPUDeltaArchiveItem alloc] initWithRelativeFilePath:relativeFilePaths[currentItemIndex] commands:commands permissions:decodedMode]; + + archiveItem.codedDataLength = decodedDataLength; + archiveItem.clonedRelativePath = clonedRelativePath; + + [archiveItems addObject:archiveItem]; + + currentItemIndex++; + } + } + + if (self.error != nil) { + return; + } + + // Feed items back to caller + BOOL exitedEarly = NO; + for (SPUDeltaArchiveItem *item in archiveItems) { + itemHandler(item, &exitedEarly); + if (exitedEarly) { + break; + } + } +} + +- (BOOL)extractItem:(SPUDeltaArchiveItem *)item +{ + NSString *physicalFilePath = item.physicalFilePath; + assert(physicalFilePath != nil); + + SPUDeltaItemCommands commands = item.commands; + assert((commands & SPUDeltaItemCommandExtract) != 0 || (commands & SPUDeltaItemCommandBinaryDiff) != 0); + + uint16_t mode = item.permissions; + + NSFileManager *fileManager = [NSFileManager defaultManager]; + if ((commands & SPUDeltaItemCommandBinaryDiff) != 0 || S_ISREG(mode) || S_ISLNK(mode)) { + // Handle regular files + // Binary diffs are always on regular files only + + uint64_t decodedLength = item.codedDataLength; + + if ((commands & SPUDeltaItemCommandBinaryDiff) != 0 || S_ISREG(mode)) { + // Regular files + + char physicalFilePathString[PATH_MAX + 1] = {0}; + if (![physicalFilePath getFileSystemRepresentation:physicalFilePathString maxLength:sizeof(physicalFilePathString) - 1]) { + self.error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadInvalidFileNameError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Path to extract cannot be decoded and expressed as a file system representation: %@", physicalFilePath] }]; + return NO; + } + + FILE *outputFile = fopen(physicalFilePathString, "wb"); + if (outputFile == NULL) { + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to fopen() %@", physicalFilePath] }]; + return NO; + } + + if (decodedLength > 0) { + // Write out archive contents to file in chunks + + uint64_t bytesLeftoverToCopy = decodedLength; + while (bytesLeftoverToCopy > 0) { + uint64_t currentBlockSize = (bytesLeftoverToCopy >= PARTIAL_IO_CHUNK_SIZE) ? PARTIAL_IO_CHUNK_SIZE : bytesLeftoverToCopy; + + void *tempBuffer = self.partialChunkBuffer; + + if (![self _readBuffer:tempBuffer length:(int32_t)currentBlockSize]) { + break; + } + + if (fwrite(tempBuffer, currentBlockSize, 1, outputFile) < 1) { + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to fwrite() %llu bytes during extraction.", currentBlockSize] }]; + break; + } + + bytesLeftoverToCopy -= currentBlockSize; + } + } + + fclose(outputFile); + + if (self.error != nil) { + return NO; + } + + if (chmod(physicalFilePathString, mode) != 0) { + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to chmod() mode %d on %@", mode, physicalFilePath] }]; + return NO; + } + } else { + // Link files + + if (PARTIAL_IO_CHUNK_SIZE < decodedLength) { + // Something is seriously wrong + self.error = [NSError errorWithDomain:SPARKLE_DELTA_ARCHIVE_ERROR_DOMAIN code:SPARKLE_DELTA_ARCHIVE_ERROR_CODE_BAD_CHUNK_SIZE userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"PARTIAL_IO_CHUNK_SIZE (%d) < decodedLength (%llu)", PARTIAL_IO_CHUNK_SIZE, decodedLength] }]; + return NO; + } + + if (decodedLength > PATH_MAX) { + // Link is too long + self.error = [NSError errorWithDomain:SPARKLE_DELTA_ARCHIVE_ERROR_DOMAIN code:SPARKLE_DELTA_ARCHIVE_ERROR_CODE_LINK_TOO_LONG userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Decoded length for link (%llu) is too long.", decodedLength] }]; + return NO; + } + + char buffer[PATH_MAX + 1] = {0}; + if (![self _readBuffer:buffer length:(int32_t)decodedLength]) { + return NO; + } + + NSString *destinationPath = [fileManager stringWithFileSystemRepresentation:buffer length:decodedLength]; + + if (destinationPath == nil) { + self.error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadInvalidFileNameError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Destination path for link %@ cannot be created in a file system representation: %@",physicalFilePath, destinationPath] }]; + return NO; + } + + NSError *createLinkError = nil; + if (![fileManager createSymbolicLinkAtPath:physicalFilePath withDestinationPath:destinationPath error:&createLinkError]) { + self.error = createLinkError; + return NO; + } + + char physicalFilePathString[PATH_MAX + 1] = {0}; + if (![physicalFilePath getFileSystemRepresentation:physicalFilePathString maxLength:sizeof(physicalFilePathString) - 1]) { + self.error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadInvalidFileNameError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Link path to extract cannot be decoded and expressed as a file system representation: %@", physicalFilePath] }]; + return NO; + } + + // We shouldn't fail if setting permissions on symlinks fail + // Apple filesystems have file permissions for symbolic links but other linux file systems don't + // So this may have no effect on some file systems over the network + lchmod(physicalFilePathString, mode); + } + } else if (S_ISDIR(mode)) { + NSError *createDirectoryError = nil; + if (![fileManager createDirectoryAtPath:physicalFilePath withIntermediateDirectories:NO attributes:@{NSFilePosixPermissions: @(mode)} error:&createDirectoryError]) { + self.error = createDirectoryError; + return NO; + } + } + + return YES; +} + +- (BOOL)_writeBuffer:(void *)buffer length:(int32_t)length +{ + if (self.error != nil) { + return NO; + } + + switch (self.compression) { + case SPUDeltaCompressionModeNone: { + BOOL success = (fwrite(buffer, (size_t)length, 1, self.file) == 1); + if (!success) { + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to write %d uncompressed bytes.", length] }]; + } + + return success; + } + case SPUDeltaCompressionModeBzip2: { + int bzerror = 0; + BZ2_bzWrite(&bzerror, self.bzipFile, buffer, length); + switch (bzerror) { + case BZ_OK: + return YES; + case BZ_IO_ERROR: + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to write %d compressed bz2 bytes due to io error.", length] }]; + return NO; + default: + self.error = [NSError errorWithDomain:SPARKLE_BZIP2_ERROR_DOMAIN code:bzerror userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to write %d compressed bz2 bytes.", length] }]; + return NO; + } + } + case SPUDeltaCompressionModeLZMA: + case SPUDeltaCompressionModeLZFSE: + case SPUDeltaCompressionModeLZ4: + case SPUDeltaCompressionModeZLIB: { + _compressionStream.src_ptr = buffer; + _compressionStream.src_size = (size_t)length; + + FILE *file = self.file; + void *compressionBuffer = self.compressionBuffer; + while (_compressionStream.src_size > 0) { + // Reset destination buffer + _compressionStream.dst_ptr = compressionBuffer; + _compressionStream.dst_size = COMPRESSION_BUFFER_SIZE; + + if (compression_stream_process(&_compressionStream, 0) == COMPRESSION_STATUS_ERROR) { + self.error = [NSError errorWithDomain:SPARKLE_COMPRESSION_ERROR_DOMAIN code:COMPRESSION_STATUS_ERROR userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to write %d compressed bytes.", length] }]; + return NO; + } + + size_t compressedBytesWritten = (size_t)(_compressionStream.dst_ptr - (uint8_t *)compressionBuffer); + if (compressedBytesWritten > 0 && fwrite(compressionBuffer, compressedBytesWritten, 1, file) < 1) { + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to write %zu compressed bytes.", compressedBytesWritten] }]; + return NO; + } + } + + return YES; + } + } +} + +- (void)writeHeader:(SPUDeltaArchiveHeader *)header +{ + char patchFilePath[PATH_MAX + 1] = {0}; + if (![self.patchFile getFileSystemRepresentation:patchFilePath maxLength:sizeof(patchFilePath) - 1]) { + self.error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadInvalidFileNameError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to open header and represent as a file system representation: %@", self.patchFile] }]; + return; + } + + FILE *file = fopen(patchFilePath, "wb"); + if (file == NULL) { + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to open patch file for writing: %@", self.patchFile] }]; + return; + } + + self.file = file; + + char magic[] = SPARKLE_DELTA_FORMAT_MAGIC; + if (fwrite(magic, sizeof(magic) - 1, 1, file) < 1) { + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: @"Failed to write magic value due to io error" }]; + return; + } + + SPUDeltaCompressionMode compression = self.compression; + if (fwrite(&compression, sizeof(compression), 1, file) < 1) { + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: @"Failed to write compression value due to io error" }]; + return; + } + + switch (compression) { + case SPUDeltaCompressionModeNone: + break; + case SPUDeltaCompressionModeBzip2: { + int bzerror = 0; + // Compression level can be 1 - 9 + int blockSize100k = self.writableCompressionLevel; + + BZFILE *bzipFile = BZ2_bzWriteOpen(&bzerror, file, blockSize100k, 0, 0); + if (bzipFile == NULL) { + switch (bzerror) { + case BZ_IO_ERROR: + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: @"Failed to open bz2 stream for writing due to io error" }]; + break; + default: + self.error = [NSError errorWithDomain:SPARKLE_BZIP2_ERROR_DOMAIN code:bzerror userInfo:@{ NSLocalizedDescriptionKey: @"Failed to open bz2 stream for writing" }]; + break; + } + + return; + } + + self.bzipFile = bzipFile; + + break; + } + case SPUDeltaCompressionModeLZMA: + case SPUDeltaCompressionModeLZFSE: + case SPUDeltaCompressionModeLZ4: + case SPUDeltaCompressionModeZLIB: { + if (compression_stream_init(&_compressionStream, COMPRESSION_STREAM_ENCODE, _compressionAlgorithmForMode(compression)) != COMPRESSION_STATUS_OK) { + self.error = [NSError errorWithDomain:SPARKLE_COMPRESSION_ERROR_DOMAIN code:COMPRESSION_STATUS_ERROR userInfo:@{ NSLocalizedDescriptionKey: @"Failed to open compression stream for writing" }]; + + return; + } + + self.initializedCompressionStream = YES; + break; + } + } + + if (![self createBuffers]) { + return; + } + + uint16_t majorVersion = header.majorVersion; + [self _writeBuffer:&majorVersion length:sizeof(majorVersion)]; + + uint16_t minorVersion = header.minorVersion; + [self _writeBuffer:&minorVersion length:sizeof(minorVersion)]; + + [self _writeBuffer:header.beforeTreeHash length:CC_SHA1_DIGEST_LENGTH]; + [self _writeBuffer:header.afterTreeHash length:CC_SHA1_DIGEST_LENGTH]; +} + +- (void)addItem:(SPUDeltaArchiveItem *)item +{ + [self.writableItems addObject:item]; +} + +- (void)finishEncodingItems +{ + if (self.error != nil) { + return; + } + + NSMutableArray *writableItems = self.writableItems; + + // Build relative path table for tracking file clones + NSMutableDictionary *relativePathToIndexTable = [NSMutableDictionary dictionary]; + uint32_t currentRelativePathIndex = 0; + for (SPUDeltaArchiveItem *item in writableItems) { + NSString *relativePath = item.relativeFilePath; + assert(relativePath != nil); + + relativePathToIndexTable[relativePath] = @(currentRelativePathIndex); + currentRelativePathIndex++; + } + + // Clone commands reference relative file paths in this table but sometimes there may not + // be an entry if extraction for an original item was skipped. Fill out any missing file path entries. + // For example, if A.app has Contents/A and B.app has Contents/A and Contents/A and Contents/B, + // where A and B's contents are the same and A is the same in both apps, normally we would not record Contents/A because its extraction was skipped. However now B is a clone of A so we need a record for A. + NSMutableArray *newClonedPathEntries = [NSMutableArray array]; + for (SPUDeltaArchiveItem *item in writableItems) { + NSString *clonedRelativePath = item.clonedRelativePath; + + if (clonedRelativePath != nil && relativePathToIndexTable[clonedRelativePath] == nil) { + [newClonedPathEntries addObject:clonedRelativePath]; + + relativePathToIndexTable[clonedRelativePath] = @(currentRelativePathIndex); + currentRelativePathIndex++; + } + } + + if (relativePathToIndexTable.count > UINT32_MAX) { + // Very unlikely but we should guard against this + // Clones rely on 32-bit indexes + self.error = [NSError errorWithDomain:SPARKLE_DELTA_ARCHIVE_ERROR_DOMAIN code:SPARKLE_DELTA_ARCHIVE_ERROR_CODE_TOO_MANY_FILES userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"There are too many file entries to create a patch for (more than %u)", UINT32_MAX] }]; + return; + } + + // Compute length of path section to write + uint64_t totalPathLength = 0; + for (SPUDeltaArchiveItem *item in writableItems) { + NSString *relativePath = item.relativeFilePath; + + char relativePathString[PATH_MAX + 1] = {0}; + if (![relativePath getFileSystemRepresentation:relativePathString maxLength:sizeof(relativePathString) - 1]) { + self.error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadInvalidFileNameError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Relative path cannot be retrieved and expressed as a file system representation: %@", relativePath] }]; + break; + } + + totalPathLength += strlen(relativePathString) + 1; + } + for (NSString *clonedPathEntry in newClonedPathEntries) { + char relativePathString[PATH_MAX + 1] = {0}; + if (![clonedPathEntry getFileSystemRepresentation:relativePathString maxLength:sizeof(relativePathString) - 1]) { + self.error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadInvalidFileNameError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Relative path for clone cannot be retrieved and expressed as a file system representation: %@", clonedPathEntry] }]; + break; + } + + totalPathLength += strlen(relativePathString) + 1; + } + + if (self.error != nil) { + return; + } + + // Write total expected length of path section + if (![self _writeBuffer:&totalPathLength length:sizeof(totalPathLength)]) { + return; + } + + // Write all of the relative paths + for (SPUDeltaArchiveItem *item in writableItems) { + NSString *relativePath = item.relativeFilePath; + + char pathBuffer[PATH_MAX + 1] = {0}; + if (![relativePath getFileSystemRepresentation:pathBuffer maxLength:PATH_MAX]) { + self.error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadInvalidFileNameError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Relative path cannot be encoded and expressed as a file system representation: %@", relativePath] }]; + break; + } + + if (![self _writeBuffer:pathBuffer length:(int32_t)strlen(pathBuffer) + 1]) { + break; + } + } + for (NSString *clonedPathEntry in newClonedPathEntries) { + char pathBuffer[PATH_MAX + 1] = {0}; + if (![clonedPathEntry getFileSystemRepresentation:pathBuffer maxLength:PATH_MAX]) { + self.error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadInvalidFileNameError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Relative path for clone cannot be encoded and expressed as a file system representation: %@", clonedPathEntry] }]; + break; + } + + if (![self _writeBuffer:pathBuffer length:(int32_t)strlen(pathBuffer) + 1]) { + break; + } + } + + if (self.error != nil) { + return; + } + + // Encode the items + for (SPUDeltaArchiveItem *item in writableItems) { + // Store commands + SPUDeltaItemCommands commands = item.commands; + if (![self _writeBuffer:&commands length:sizeof(commands)]) { + break; + } + + // Check if we need to encode additional data + if ((commands & SPUDeltaItemCommandClone) != 0) { + // Store any desired file permissions changes for the clone + if ((commands & SPUDeltaItemCommandModifyPermissions) != 0) { + uint16_t permissions = item.permissions; + if (![self _writeBuffer:&permissions length:sizeof(permissions)]) { + break; + } + } + + // Store index to relative path table + NSString *clonedRelativePath = item.clonedRelativePath; + assert(clonedRelativePath != nil); + + NSNumber *relativePathIndex = relativePathToIndexTable[clonedRelativePath]; + if (relativePathIndex == nil) { + // We have quite a problem here + self.error = [NSError errorWithDomain:SPARKLE_DELTA_ARCHIVE_ERROR_DOMAIN code:SPARKLE_DELTA_ARCHIVE_ERROR_CODE_BAD_CLONE_LOOKUP userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Relative file path index for %@ could not be located", clonedRelativePath] }]; + break; + } + + uint32_t relativePathCIndex = relativePathIndex.unsignedIntValue; + if (![self _writeBuffer:&relativePathCIndex length:sizeof(relativePathCIndex)]) { + break; + } + } else if ((commands & SPUDeltaItemCommandExtract) != 0 || (commands & SPUDeltaItemCommandBinaryDiff) != 0) { + NSString *physicalPath = item.physicalFilePath; + assert(physicalPath != nil); + + char physicalFilePathString[PATH_MAX + 1] = {0}; + if (![physicalPath getFileSystemRepresentation:physicalFilePathString maxLength:sizeof(physicalFilePathString) - 1]) { + self.error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadInvalidFileNameError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Path cannot be decoded and expressed as a file system representation while encoding items: %@", physicalPath] }]; + break; + } + + struct stat fileInfo = {0}; + if (lstat(physicalFilePathString, &fileInfo) != 0) { + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to lstat() on %@", physicalPath] }]; + break; + } + + uint16_t originalMode = fileInfo.st_mode; + item.originalMode = originalMode; + + uint16_t encodedMode; + if ((commands & SPUDeltaItemCommandModifyPermissions) != 0) { + uint16_t permissions = item.permissions; + encodedMode = (originalMode & ~PERMISSION_FLAGS) | permissions; + } else { + encodedMode = originalMode; + } + + // Store file mode (including desired permissions) + if (![self _writeBuffer:&encodedMode length:sizeof(encodedMode)]) { + break; + } + + // Store data length + // Length doesn't matter for directory names (we already track the name in the relative path) + if (S_ISREG(originalMode)) { + uint64_t dataLength = (uint64_t)fileInfo.st_size; + if (![self _writeBuffer:&dataLength length:sizeof(dataLength)]) { + break; + } + + item.codedDataLength = dataLength; + } else if (S_ISLNK(originalMode)) { + off_t fileSize = fileInfo.st_size; + if (fileSize > UINT16_MAX) { + self.error = [NSError errorWithDomain:SPARKLE_DELTA_ARCHIVE_ERROR_DOMAIN code:SPARKLE_DELTA_ARCHIVE_ERROR_CODE_LINK_TOO_LONG userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Link path has a destination that is too long: %llu bytes", fileSize] }]; + break; + } + + uint16_t dataLength = (uint16_t)fileSize; + if (![self _writeBuffer:&dataLength length:sizeof(dataLength)]) { + break; + } + + item.codedDataLength = dataLength; + } + } else if ((commands & SPUDeltaItemCommandModifyPermissions) != 0) { + // Store file permissions + uint16_t permissions = item.permissions; + if (![self _writeBuffer:&permissions length:sizeof(permissions)]) { + break; + } + } + } + + if (self.error != nil) { + return; + } + + // Encode end command marker + SPUDeltaItemCommands endCommand = SPUDeltaItemCommandEndMarker; + if (![self _writeBuffer:&endCommand length:sizeof(endCommand)]) { + return; + } + + // Encode all of our file contents + void *tempBuffer = self.partialChunkBuffer; + for (SPUDeltaArchiveItem *item in writableItems) { + SPUDeltaItemCommands commands = item.commands; + if ((commands & SPUDeltaItemCommandExtract) != 0 || (commands & SPUDeltaItemCommandBinaryDiff) != 0) { + NSString *physicalPath = item.physicalFilePath; + assert(physicalPath != nil); + + mode_t originalMode = item.originalMode; + if (S_ISREG(originalMode)) { + // Write out file contents to archive in chunks + + uint64_t totalItemSize = item.codedDataLength; + if (totalItemSize > 0) { + char physicalFilePathString[PATH_MAX + 1] = {0}; + if (![physicalPath getFileSystemRepresentation:physicalFilePathString maxLength:sizeof(physicalFilePathString) - 1]) { + self.error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadInvalidFileNameError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Path to finish encoding cannot be decoded and expressed as a file system representation: %@", physicalPath] }]; + break; + } + + FILE *inputFile = fopen(physicalFilePathString, "rb"); + if (inputFile == NULL) { + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to open file for reading while encoding items: %@", physicalPath] }]; + break; + } + + uint64_t bytesLeftoverToCopy = totalItemSize; + while (bytesLeftoverToCopy > 0) { + uint64_t currentBlockSize = (bytesLeftoverToCopy >= PARTIAL_IO_CHUNK_SIZE) ? PARTIAL_IO_CHUNK_SIZE : bytesLeftoverToCopy; + + if (fread(tempBuffer, currentBlockSize, 1, inputFile) < 1) { + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to read %llu chunk bytes while encoding items", currentBlockSize] }]; + break; + } + + if (![self _writeBuffer:tempBuffer length:(int32_t)currentBlockSize]) { + break; + } + + bytesLeftoverToCopy -= currentBlockSize; + } + + fclose(inputFile); + + if (self.error != nil) { + break; + } + } + } else if (S_ISLNK(originalMode)) { + char physicalFilePathString[PATH_MAX + 1] = {0}; + if (![physicalPath getFileSystemRepresentation:physicalFilePathString maxLength:sizeof(physicalFilePathString) - 1]) { + self.error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadInvalidFileNameError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Link path to finish encoding cannot be decoded and expressed as a file system representation: %@", physicalPath] }]; + break; + } + + char linkDestination[PATH_MAX + 1] = {0}; + ssize_t linkDestinationLength = readlink(physicalFilePathString, linkDestination, PATH_MAX); + if (linkDestinationLength < 0) { + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to readlink() file at %@", physicalPath] }]; + break; + } + + if (![self _writeBuffer:linkDestination length:(int32_t)strlen(linkDestination)]) { + break; + } + } + } + } + + // Close up and write final data to compressed streams + + if (self.bzipFile != NULL) { + int bzerror = 0; + BZ2_bzWriteClose64(&bzerror, self.bzipFile, 0, NULL, NULL, NULL, NULL); + if (bzerror == BZ_IO_ERROR) { + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: @"Failed to write and close bzip2 file due to IO error" }]; + return; + } + } else if (self.initializedCompressionStream) { + void *compressionBuffer = self.compressionBuffer; + FILE *file = self.file; + + _compressionStream.src_size = 0; + + while (_compressionStream.dst_size > 0) { + _compressionStream.dst_ptr = compressionBuffer; + _compressionStream.dst_size = COMPRESSION_BUFFER_SIZE; + + compression_status status = compression_stream_process(&_compressionStream, COMPRESSION_STREAM_FINALIZE); + if (status == COMPRESSION_STATUS_ERROR) { + self.error = [NSError errorWithDomain:SPARKLE_COMPRESSION_ERROR_DOMAIN code:COMPRESSION_STATUS_ERROR userInfo:@{ NSLocalizedDescriptionKey: @"Failed to write final bits of Compression based file" }]; + return; + } + + size_t compressedBytesToWrite = (size_t)(_compressionStream.dst_ptr - (uint8_t *)compressionBuffer); + if (compressedBytesToWrite > 0) { + if (fwrite(compressionBuffer, compressedBytesToWrite, 1, file) < 1) { + self.error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: @"Failed to write and close Compression based file due to io error" }]; + return; + } + } + + if (status == COMPRESSION_STATUS_END) { + // We're done + break; + } + } + } +} + +@end + +#pragma clang diagnostic pop diff --git a/Autoupdate/SPUXarDeltaArchive.h b/Autoupdate/SPUXarDeltaArchive.h index db93d378..e9f71ae1 100644 --- a/Autoupdate/SPUXarDeltaArchive.h +++ b/Autoupdate/SPUXarDeltaArchive.h @@ -12,10 +12,11 @@ NS_ASSUME_NONNULL_BEGIN +// Legacy container format for binary delta archives @interface SPUXarDeltaArchive : NSObject -- (nullable instancetype)initWithPatchFileForWriting:(NSString *)patchFile; -- (nullable instancetype)initWithPatchFileForReading:(NSString *)patchFile; +- (instancetype)initWithPatchFileForWriting:(NSString *)patchFile compression:(SPUDeltaCompressionMode)compression compressionLevel:(int32_t)compressionLevel; +- (instancetype)initWithPatchFileForReading:(NSString *)patchFile; @end diff --git a/Autoupdate/SPUXarDeltaArchive.m b/Autoupdate/SPUXarDeltaArchive.m index 37f6e89b..24c98c99 100644 --- a/Autoupdate/SPUXarDeltaArchive.m +++ b/Autoupdate/SPUXarDeltaArchive.m @@ -9,7 +9,8 @@ #import "SPUXarDeltaArchive.h" #include #include "SUBinaryDeltaCommon.h" -#include +#import +#import #include "AppKitPrevention.h" @@ -37,41 +38,55 @@ extern char *xar_get_safe_path(xar_file_t f) __attribute__((weak_import)); #define BINARY_DELTA_KEY "binary-delta" #define MODIFY_PERMISSIONS_KEY "mod-permissions" +// Errors +#define SPARKLE_DELTA_XAR_ARCHIVE_ERROR_DOMAIN @"Sparkle XAR Archive" +#define SPARKLE_DELTA_XAR_ARCHIVE_ERROR_CODE_OPEN_FAILURE 1 +#define SPARKLE_DELTA_XAR_ARCHIVE_ERROR_CODE_ADD_FAILURE 2 +#define SPARKLE_DELTA_XAR_ARCHIVE_ERROR_CODE_EXTRACT_FAILURE 3 +#define SPARKLE_DELTA_XAR_ARCHIVE_ERROR_CODE_UNSUPPORTED_COMPRESSION_FAILURE 4 + @interface SPUXarDeltaArchive () @property (nonatomic) xar_t x; +@property (nonatomic, readonly) int32_t xarMode; @property (nonatomic, readonly) NSMutableDictionary *fileTable; +@property (nonatomic, readonly) NSString *patchFile; +@property (nonatomic) NSError *error; + +@property (nonatomic, readonly) SPUDeltaCompressionMode writableCompression; +@property (nonatomic, readonly) int32_t writableCompressionLevel; @end @implementation SPUXarDeltaArchive @synthesize x = _x; +@synthesize xarMode = _xarMode; +@synthesize patchFile = _patchFile; @synthesize fileTable = _fileTable; +@synthesize writableCompression = _writableCompression; +@synthesize writableCompressionLevel = _writableCompressionLevel; +@synthesize error = _error; -- (nullable instancetype)initWithPatchFileForWriting:(NSString *)patchFile +- (instancetype)initWithPatchFileForWriting:(NSString *)patchFile compression:(SPUDeltaCompressionMode)compression compressionLevel:(int32_t)compressionLevel { self = [super init]; if (self != nil) { - _x = xar_open(patchFile.fileSystemRepresentation, WRITE); - if (_x == NULL) { - return nil; - } - + _patchFile = [patchFile copy]; + _xarMode = WRITE; + _writableCompression = (compression == SPUDeltaCompressionModeDefault ? SPUDeltaCompressionModeBzip2 : compression); + _writableCompressionLevel = compressionLevel; _fileTable = [NSMutableDictionary dictionary]; - xar_opt_set(_x, XAR_OPT_COMPRESSION, "bzip2"); } return self; } -- (nullable instancetype)initWithPatchFileForReading:(NSString *)patchFile +- (instancetype)initWithPatchFileForReading:(NSString *)patchFile { self = [super init]; if (self != nil) { - _x = xar_open(patchFile.fileSystemRepresentation, READ); - if (_x == NULL) { - return nil; - } + _patchFile = [patchFile copy]; + _xarMode = READ; } return self; } @@ -95,8 +110,17 @@ extern char *xar_get_safe_path(xar_file_t f) __attribute__((weak_import)); return HAS_XAR_GET_SAFE_PATH; } -- (void)getMajorDeltaVersion:(nullable uint16_t *)outMajorDiffVersion minorDeltaVersion:(nullable uint16_t *)outMinorDiffVersion beforeTreeHash:(NSString * _Nullable __autoreleasing * _Nullable)outBeforeTreeHash afterTreeHash:(NSString * _Nullable __autoreleasing * _Nullable)outAfterTreeHash +- (nullable SPUDeltaArchiveHeader *)readHeader { + NSString *patchFile = self.patchFile; + xar_t x = xar_open(patchFile.fileSystemRepresentation, READ); + if (x == NULL) { + self.error = [NSError errorWithDomain:SPARKLE_DELTA_XAR_ARCHIVE_ERROR_DOMAIN code:SPARKLE_DELTA_XAR_ARCHIVE_ERROR_CODE_OPEN_FAILURE userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to xar_open() file for reading: %@", patchFile] }]; + return nil; + } + + self.x = x; + uint16_t majorDiffVersion = FIRST_DELTA_DIFF_MAJOR_VERSION; uint16_t minorDiffVersion = 0; NSString *expectedBeforeHash = nil; @@ -143,33 +167,59 @@ extern char *xar_get_safe_path(xar_file_t f) __attribute__((weak_import)); } } - if (outMajorDiffVersion != NULL) { - *outMajorDiffVersion = majorDiffVersion; - } + unsigned char rawExpectedBeforeHash[CC_SHA1_DIGEST_LENGTH] = {0}; + getRawHashFromDisplayHash(rawExpectedBeforeHash, expectedBeforeHash); - if (outMinorDiffVersion != NULL) { - *outMinorDiffVersion = minorDiffVersion; - } + unsigned char rawExpectedAfterHash[CC_SHA1_DIGEST_LENGTH] = {0}; + getRawHashFromDisplayHash(rawExpectedAfterHash, expectedAfterHash); - if (outBeforeTreeHash != NULL) { - *outBeforeTreeHash = expectedBeforeHash; - } - - if (outAfterTreeHash != NULL) { - *outAfterTreeHash = expectedAfterHash; - } + return [[SPUDeltaArchiveHeader alloc] initWithMajorVersion:majorDiffVersion minorVersion:minorDiffVersion beforeTreeHash:rawExpectedBeforeHash afterTreeHash:rawExpectedAfterHash]; } - -- (void)setMajorVersion:(uint16_t)majorVersion minorVersion:(uint16_t)minorVersion beforeTreeHash:(NSString *)beforeTreeHash afterTreeHash:(NSString *)afterTreeHash +- (void)writeHeader:(SPUDeltaArchiveHeader *)header { - xar_subdoc_t attributes = xar_subdoc_new(self.x, BINARY_DELTA_ATTRIBUTES_KEY); + NSString *patchFile = self.patchFile; - xar_subdoc_prop_set(attributes, MAJOR_DIFF_VERSION_KEY, [[NSString stringWithFormat:@"%u", majorVersion] UTF8String]); - xar_subdoc_prop_set(attributes, MINOR_DIFF_VERSION_KEY, [[NSString stringWithFormat:@"%u", minorVersion] UTF8String]); + xar_t x = xar_open(patchFile.fileSystemRepresentation, WRITE); + if (x == NULL) { + self.error = [NSError errorWithDomain:SPARKLE_DELTA_XAR_ARCHIVE_ERROR_DOMAIN code:SPARKLE_DELTA_XAR_ARCHIVE_ERROR_CODE_OPEN_FAILURE userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to xar_open() file for writing: %@", patchFile] }]; + return; + } - xar_subdoc_prop_set(attributes, BEFORE_TREE_SHA1_KEY, [beforeTreeHash UTF8String]); - xar_subdoc_prop_set(attributes, AFTER_TREE_SHA1_KEY, [afterTreeHash UTF8String]); + self.x = x; + + SPUDeltaCompressionMode compression = self.writableCompression; + int32_t compressionLevel = self.writableCompressionLevel; + + switch (compression) { + case SPUDeltaCompressionModeNone: + break; + case SPUDeltaCompressionModeBzip2: { + xar_opt_set(x, XAR_OPT_COMPRESSION, "bzip2"); + + char buffer[256] = {0}; + snprintf(buffer, sizeof(buffer) - 1, "%d", compressionLevel); + xar_opt_set(x, XAR_OPT_COMPRESSIONARG, buffer); + + break; + } + case SPUDeltaCompressionModeLZMA: + case SPUDeltaCompressionModeLZFSE: + case SPUDeltaCompressionModeLZ4: + case SPUDeltaCompressionModeZLIB: { + self.error = [NSError errorWithDomain:SPARKLE_DELTA_XAR_ARCHIVE_ERROR_DOMAIN code:SPARKLE_DELTA_XAR_ARCHIVE_ERROR_CODE_UNSUPPORTED_COMPRESSION_FAILURE userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Version 2 patches only support bzip2 compression."] }]; + + return; + } + } + + xar_subdoc_t attributes = xar_subdoc_new(x, BINARY_DELTA_ATTRIBUTES_KEY); + + xar_subdoc_prop_set(attributes, MAJOR_DIFF_VERSION_KEY, [[NSString stringWithFormat:@"%u", header.majorVersion] UTF8String]); + xar_subdoc_prop_set(attributes, MINOR_DIFF_VERSION_KEY, [[NSString stringWithFormat:@"%u", header.minorVersion] UTF8String]); + + xar_subdoc_prop_set(attributes, BEFORE_TREE_SHA1_KEY, [displayHashFromRawHash(header.beforeTreeHash) UTF8String]); + xar_subdoc_prop_set(attributes, AFTER_TREE_SHA1_KEY, [displayHashFromRawHash(header.afterTreeHash) UTF8String]); } static xar_file_t _xarAddFile(NSMutableDictionary *fileTable, xar_t x, NSString *relativePath, NSString *filePath) @@ -218,30 +268,51 @@ static xar_file_t _xarAddFile(NSMutableDictionary *fileTa return lastParent; } -- (void)addRelativeFilePath:(NSString *)relativeFilePath realFilePath:(nullable NSString *)filePath attributes:(SPUDeltaFileAttributes)attributes permissions:(uint16_t)permissions +- (void)addItem:(SPUDeltaArchiveItem *)item { - xar_file_t newFile = _xarAddFile(self.fileTable, self.x, relativeFilePath, filePath); - assert(newFile != NULL); + if (self.error != nil) { + return; + } - if ((attributes & SPUDeltaFileAttributesDelete) != 0) { + NSString *relativeFilePath = item.relativeFilePath; + NSString *filePath = item.physicalFilePath; + SPUDeltaItemCommands commands = item.commands; + uint16_t permissions = item.permissions; + + xar_file_t newFile = _xarAddFile(self.fileTable, self.x, relativeFilePath, filePath); + if (newFile == NULL) { + self.error = [NSError errorWithDomain:SPARKLE_DELTA_XAR_ARCHIVE_ERROR_DOMAIN code:SPARKLE_DELTA_XAR_ARCHIVE_ERROR_CODE_ADD_FAILURE userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to add xar file entry: %@", relativeFilePath] }]; + return; + } + + if ((commands & SPUDeltaItemCommandDelete) != 0) { xar_prop_set(newFile, DELETE_KEY, "true"); } - if ((attributes & SPUDeltaFileAttributesExtract) != 0) { + if ((commands & SPUDeltaItemCommandExtract) != 0) { xar_prop_set(newFile, EXTRACT_KEY, "true"); } - if ((attributes & SPUDeltaFileAttributesBinaryDiff) != 0) { + if ((commands & SPUDeltaItemCommandBinaryDiff) != 0) { xar_prop_set(newFile, BINARY_DELTA_KEY, "true"); } - if ((attributes & SPUDeltaFileAttributesModifyPermissions) != 0) { + if ((commands & SPUDeltaItemCommandModifyPermissions) != 0) { xar_prop_set(newFile, MODIFY_PERMISSIONS_KEY, [NSString stringWithFormat:@"%u", permissions].UTF8String); } } -- (BOOL)enumerateItems:(void (^)(const void *, NSString *, SPUDeltaFileAttributes, uint16_t, BOOL *))itemHandler +- (void)finishEncodingItems { + // Items are already encoded when they are extracted prior +} + +- (void)enumerateItems:(void (^)(SPUDeltaArchiveItem *, BOOL *))itemHandler +{ + if (self.error != nil) { + return; + } + BOOL exitedEarly = NO; xar_iter_t iter = xar_iter_new(); for (xar_file_t file = xar_file_first(self.x, iter); file; file = xar_file_next(iter)) { @@ -268,23 +339,23 @@ static xar_file_t _xarAddFile(NSMutableDictionary *fileTa NSString *relativePath = @(pathCString); - SPUDeltaFileAttributes attributes = 0; + SPUDeltaItemCommands commands = 0; { const char *value = NULL; if (xar_prop_get(file, DELETE_KEY, &value) == 0) { - attributes |= SPUDeltaFileAttributesDelete; + commands |= SPUDeltaItemCommandDelete; } } { const char *value = NULL; if (xar_prop_get(file, BINARY_DELTA_KEY, &value) == 0) { - attributes |= SPUDeltaFileAttributesBinaryDiff; + commands |= SPUDeltaItemCommandBinaryDiff; } } { const char *value = NULL; if (xar_prop_get(file, EXTRACT_KEY, &value) == 0) { - attributes |= SPUDeltaFileAttributesExtract; + commands |= SPUDeltaItemCommandExtract; } } @@ -292,26 +363,39 @@ static xar_file_t _xarAddFile(NSMutableDictionary *fileTa { const char *value = NULL; if (xar_prop_get(file, MODIFY_PERMISSIONS_KEY, &value) == 0) { - attributes |= SPUDeltaFileAttributesModifyPermissions; + commands |= SPUDeltaItemCommandModifyPermissions; permissions = (uint16_t)[@(value) intValue]; } } - itemHandler(file, relativePath, attributes, permissions, &exitedEarly); + SPUDeltaArchiveItem *item = [[SPUDeltaArchiveItem alloc] initWithRelativeFilePath:relativePath commands:commands permissions:permissions]; + item.context = file; + + itemHandler(item, &exitedEarly); if (exitedEarly) { break; } } xar_iter_free(iter); - - return !exitedEarly; } -- (BOOL)extractItem:(const void *)item destination:(NSString *)destinationPath +- (BOOL)extractItem:(SPUDeltaArchiveItem *)item { - xar_file_t file = item; - return (xar_extract_tofile(self.x, file, destinationPath.fileSystemRepresentation) == 0); + if (self.error != nil) { + return NO; + } + + assert(item.physicalFilePath != nil); + assert(item.context != NULL); + + xar_file_t file = item.context; + if (xar_extract_tofile(self.x, file, item.physicalFilePath.fileSystemRepresentation) != 0) { + self.error = [NSError errorWithDomain:SPARKLE_DELTA_XAR_ARCHIVE_ERROR_DOMAIN code:SPARKLE_DELTA_XAR_ARCHIVE_ERROR_CODE_EXTRACT_FAILURE userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to extract xar file entry to %@", item.physicalFilePath] }]; + return NO; + } + + return YES; } @end diff --git a/Autoupdate/SUBinaryDeltaApply.m b/Autoupdate/SUBinaryDeltaApply.m index c3152386..1f626d6c 100644 --- a/Autoupdate/SUBinaryDeltaApply.m +++ b/Autoupdate/SUBinaryDeltaApply.m @@ -19,13 +19,8 @@ #include "AppKitPrevention.h" -static BOOL applyBinaryDeltaToFile(id archive, const void *item, NSString *sourceFilePath, NSString *destinationFilePath) +static BOOL applyBinaryDeltaToFile(NSString *patchFile, NSString *sourceFilePath, NSString *destinationFilePath) { - NSString *patchFile = temporaryFilename(@"apply-binary-delta"); - if (![archive extractItem:item destination:patchFile]) { - return NO; - } - const char *argv[] = {"/usr/bin/bspatch", [sourceFilePath fileSystemRepresentation], [destinationFilePath fileSystemRepresentation], [patchFile fileSystemRepresentation]}; BOOL success = (bspatch(4, argv) == 0); unlink([patchFile fileSystemRepresentation]); @@ -34,24 +29,23 @@ static BOOL applyBinaryDeltaToFile(id archive, const vo BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFile, BOOL verbose, void (^progressCallback)(double progress), NSError *__autoreleasing *error) { - id archive = SPUDeltaArchiveForReading(patchFile); - if (archive == nil) { + SPUDeltaArchiveHeader *header = nil; + id archive = SPUDeltaArchiveReadPatchAndHeader(patchFile, &header); + if (archive.error != nil) { if (error != NULL) { - *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadUnknownError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Unable to open %@. Giving up.", patchFile] }]; + *error = archive.error; } return NO; } - SUBinaryDeltaMajorVersion majorDiffVersion = FIRST_DELTA_DIFF_MAJOR_VERSION; - uint16_t minorDiffVersion = 0; - - NSString *expectedBeforeHash = nil; - NSString *expectedAfterHash = nil; - progressCallback(0/6.0); - - [archive getMajorDeltaVersion:&majorDiffVersion minorDeltaVersion:&minorDiffVersion beforeTreeHash:&expectedBeforeHash afterTreeHash:&expectedAfterHash]; + SUBinaryDeltaMajorVersion majorDiffVersion = header.majorVersion; + uint16_t minorDiffVersion = header.minorVersion; + + unsigned char *expectedBeforeHash = header.beforeTreeHash; + unsigned char *expectedAfterHash = header.afterTreeHash; + if (majorDiffVersion < FIRST_DELTA_DIFF_MAJOR_VERSION) { if (error != NULL) { *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadCorruptFileError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Unable to identify diff-version %u in delta. Giving up.", majorDiffVersion] }]; @@ -96,9 +90,9 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi } progressCallback(1/6.0); - - NSString *beforeHash = hashOfTreeWithVersion(source, majorDiffVersion); - if (!beforeHash) { + + unsigned char beforeHash[CC_SHA1_DIGEST_LENGTH] = {0}; + if (!getRawHashOfTreeWithVersion(beforeHash, source, majorDiffVersion)) { if (verbose) { fprintf(stderr, "\n"); } @@ -108,12 +102,12 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi return NO; } - if (![beforeHash isEqualToString:expectedBeforeHash]) { + if (memcmp(beforeHash, expectedBeforeHash, CC_SHA1_DIGEST_LENGTH) != 0) { if (verbose) { fprintf(stderr, "\n"); } if (error != NULL) { - *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadUnknownError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Source doesn't have expected hash (%@ != %@). Giving up.", expectedBeforeHash, beforeHash] }]; + *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadUnknownError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Source doesn't have expected hash (%@ != %@). Giving up.", displayHashFromRawHash(expectedBeforeHash), displayHashFromRawHash(beforeHash)] }]; } return NO; } @@ -153,7 +147,14 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi } NSFileManager *fileManager = [[NSFileManager alloc] init]; - if (![archive enumerateItems:^(const void * _Nonnull item, NSString * _Nonnull relativePath, SPUDeltaFileAttributes attributes, uint16_t permissions, BOOL *stop) { + // Ensure error is cleared out in advance + if (error != NULL) { + *error = nil; + } + + [archive enumerateItems:^(SPUDeltaArchiveItem *item, BOOL *stop) { + NSString *relativePath = item.relativeFilePath; + if ([relativePath.pathComponents containsObject:@".."]) { if (error != NULL) { *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileWriteUnknownError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Relative path '%@' contains '..' path component", relativePath] }]; @@ -189,8 +190,8 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi // Files that have no property set that we check for will get ignored // This is important because they aren't part of the delta, just part of the directory structure - - if ((attributes & SPUDeltaFileAttributesDelete) != 0) { + SPUDeltaItemCommands commands = item.commands; + if ((commands & SPUDeltaItemCommandDelete) != 0) { if (!removeTree(destinationFilePath)) { if (verbose) { fprintf(stderr, "\n"); @@ -205,8 +206,51 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi removedFile = YES; } - if ((attributes & SPUDeltaFileAttributesBinaryDiff) != 0) { - if (!applyBinaryDeltaToFile(archive, item, sourceFilePath, destinationFilePath)) { + if ((commands & SPUDeltaItemCommandClone) != 0) { + NSString *clonedRelativePath = item.clonedRelativePath; + if ([clonedRelativePath.pathComponents containsObject:@".."]) { + if (error != NULL) { + *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileWriteUnknownError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Relative path for clone '%@' contains '..' path component", clonedRelativePath] }]; + } + *stop = YES; + return; + } + + NSString *clonedOriginalPath = [source stringByAppendingPathComponent:clonedRelativePath]; + + NSError *copyError = nil; + if (![fileManager copyItemAtPath:clonedOriginalPath toPath:destinationFilePath error:©Error]) { + if (verbose) { + fprintf(stderr, "\n"); + } + if (error != NULL) { + *error = copyError; + } + + *stop = YES; + return; + } + + if (verbose) { + fprintf(stderr, "\n✂️ %s %s -> %s", VERBOSE_CLONED, [clonedRelativePath fileSystemRepresentation], [relativePath fileSystemRepresentation]); + } + } else if ((commands & SPUDeltaItemCommandBinaryDiff) != 0) { + NSString *tempDiffFile = temporaryFilename(@"apply-binary-delta"); + item.physicalFilePath = tempDiffFile; + + if (![archive extractItem:item]) { + if (verbose) { + fprintf(stderr, "\n"); + } + if (error != NULL) { + *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileWriteUnknownError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Unable to extract diffed file to %@", tempDiffFile], NSUnderlyingErrorKey: (NSError * _Nonnull)archive.error }]; + } + + *stop = YES; + return; + } + + if (!applyBinaryDeltaToFile(tempDiffFile, sourceFilePath, destinationFilePath)) { if (verbose) { fprintf(stderr, "\n"); } @@ -220,13 +264,14 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi if (verbose) { fprintf(stderr, "\n🔨 %s %s", VERBOSE_PATCHED, [relativePath fileSystemRepresentation]); } - } else if ((attributes & SPUDeltaFileAttributesExtract) != 0) { // extract and permission modifications don't coexist - if (![archive extractItem:item destination:destinationFilePath]) { + } else if ((commands & SPUDeltaItemCommandExtract) != 0) { // extract and permission modifications don't coexist + item.physicalFilePath = destinationFilePath; + if (![archive extractItem:item]) { if (verbose) { fprintf(stderr, "\n"); } if (error != NULL) { - *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileWriteUnknownError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Unable to extract file to %@", destinationFilePath] }]; + *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileWriteUnknownError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Unable to extract file to %@", destinationFilePath], NSUnderlyingErrorKey: (NSError * _Nonnull)archive.error }]; } *stop = YES; return; @@ -243,8 +288,8 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi fprintf(stderr, "\n❌ %s %s", VERBOSE_DELETED, [relativePath fileSystemRepresentation]); } - if ((attributes & SPUDeltaFileAttributesModifyPermissions) != 0) { - mode_t mode = (mode_t)permissions; + if ((commands & SPUDeltaItemCommandModifyPermissions) != 0) { + mode_t mode = (mode_t)item.permissions; if (!modifyPermissions(destinationFilePath, mode)) { if (verbose) { fprintf(stderr, "\n"); @@ -260,19 +305,30 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi fprintf(stderr, "\n👮 %s %s (0%o)", VERBOSE_MODIFIED, [relativePath fileSystemRepresentation], mode); } } - }]) { - return NO; - } + }]; [archive close]; + + // Set error from enumerating items if we have encountered an error and haven't set it yet + NSError *archiveError = archive.error; + if (archiveError != nil) { + if (verbose) { + fprintf(stderr, "\n"); + } + if (error != NULL && *error == nil) { + *error = archiveError; + } + return NO; + } progressCallback(5/6.0); if (verbose) { fprintf(stderr, "\nVerifying destination..."); } - NSString *afterHash = hashOfTreeWithVersion(destination, majorDiffVersion); - if (afterHash == nil) { + + unsigned char afterHash[CC_SHA1_DIGEST_LENGTH] = {0}; + if (!getRawHashOfTreeWithVersion(afterHash, destination, majorDiffVersion)) { if (verbose) { fprintf(stderr, "\n"); } @@ -282,12 +338,12 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi return NO; } - if (![afterHash isEqualToString:expectedAfterHash]) { + if (memcmp(afterHash, expectedAfterHash, CC_SHA1_DIGEST_LENGTH) != 0) { if (verbose) { fprintf(stderr, "\n"); } if (error != NULL) { - *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadUnknownError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Destination doesn't have expected hash (%@ != %@). Giving up.", expectedAfterHash, afterHash] }]; + *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadUnknownError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Destination doesn't have expected hash (%@ != %@). Giving up.", displayHashFromRawHash(expectedAfterHash), displayHashFromRawHash(afterHash)] }]; } removeTree(destination); return NO; diff --git a/Autoupdate/SUBinaryDeltaCommon.h b/Autoupdate/SUBinaryDeltaCommon.h index 36dee682..bca50eb3 100644 --- a/Autoupdate/SUBinaryDeltaCommon.h +++ b/Autoupdate/SUBinaryDeltaCommon.h @@ -15,9 +15,6 @@ #define PERMISSION_FLAGS (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX) -#define IS_VALID_PERMISSIONS(mode) \ - (((mode & PERMISSION_FLAGS) == 0755) || ((mode & PERMISSION_FLAGS) == 0644)) - #define APPLE_CODE_SIGN_XATTR_CODE_DIRECTORY_KEY "com.apple.cs.CodeDirectory" #define APPLE_CODE_SIGN_XATTR_CODE_REQUIREMENTS_KEY "com.apple.cs.CodeRequirements" #define APPLE_CODE_SIGN_XATTR_CODE_SIGNATURE_KEY "com.apple.cs.CodeSignature" @@ -29,25 +26,32 @@ #define VERBOSE_PATCHED "Patched" // file is patched when applying a patch #define VERBOSE_UPDATED "Updated" // file's contents are updated #define VERBOSE_MODIFIED "Modified" // file's metadata is modified +#define VERBOSE_CLONED "Cloned" // file is cloned in content from a differently named file #define MAJOR_VERSION_IS_AT_LEAST(actualMajor, expectedMajor) (actualMajor >= expectedMajor) -// Each major version will be assigned a name of a color // Changes that break backwards compatibility will have different major versions // Changes that affect creating but not applying patches will have different minor versions - typedef NS_ENUM(uint16_t, SUBinaryDeltaMajorVersion) { // Note: support for creating or applying version 1 deltas have been removed SUBinaryDeltaMajorVersion1 = 1, - SUBinaryDeltaMajorVersion2 = 2 + SUBinaryDeltaMajorVersion2 = 2, + SUBinaryDeltaMajorVersion3 = 3 }; +// For Swift access +extern SUBinaryDeltaMajorVersion SUBinaryDeltaMajorVersionDefault; + #define FIRST_DELTA_DIFF_MAJOR_VERSION SUBinaryDeltaMajorVersion1 #define FIRST_SUPPORTED_DELTA_MAJOR_VERSION SUBinaryDeltaMajorVersion2 -#define LATEST_DELTA_DIFF_MAJOR_VERSION SUBinaryDeltaMajorVersion2 +#define LATEST_DELTA_DIFF_MAJOR_VERSION SUBinaryDeltaMajorVersion3 extern int compareFiles(const FTSENT **a, const FTSENT **b); +BOOL getRawHashOfTreeWithVersion(unsigned char *hashBuffer, NSString *path, uint16_t majorVersion); +BOOL getRawHashOfTreeAndFileTablesWithVersion(unsigned char *hashBuffer, NSString *path, uint16_t majorVersion, NSMutableDictionary *> *hashToFileKeyDictionary, NSMutableDictionary *fileKeyToHashDictionary); +NSString *displayHashFromRawHash(const unsigned char *hash); +void getRawHashFromDisplayHash(unsigned char *hash, NSString *hexHash); extern NSString *hashOfTreeWithVersion(NSString *path, uint16_t majorVersion); extern NSString *hashOfTree(NSString *path); extern BOOL removeTree(NSString *path); diff --git a/Autoupdate/SUBinaryDeltaCommon.m b/Autoupdate/SUBinaryDeltaCommon.m index cd04a8a7..d2f98f2a 100644 --- a/Autoupdate/SUBinaryDeltaCommon.m +++ b/Autoupdate/SUBinaryDeltaCommon.m @@ -19,6 +19,8 @@ #include "AppKitPrevention.h" +SUBinaryDeltaMajorVersion SUBinaryDeltaMajorVersionDefault = SUBinaryDeltaMajorVersion2; + int compareFiles(const FTSENT **a, const FTSENT **b) { return strcoll((*a)->fts_name, (*b)->fts_name); @@ -45,6 +47,8 @@ uint16_t latestMinorVersionForMajorVersion(SUBinaryDeltaMajorVersion majorVersio return 2; case SUBinaryDeltaMajorVersion2: return 3; + case SUBinaryDeltaMajorVersion3: + return 0; } return 0; } @@ -136,18 +140,23 @@ static BOOL _hashOfFileContents(unsigned char *hash, FTSENT *ent) return YES; } -NSString *hashOfTreeWithVersion(NSString *path, uint16_t __unused majorVersion) +BOOL getRawHashOfTreeWithVersion(unsigned char *hashBuffer, NSString *path, uint16_t majorVersion) +{ + return getRawHashOfTreeAndFileTablesWithVersion(hashBuffer, path, majorVersion, nil, nil); +} + +BOOL getRawHashOfTreeAndFileTablesWithVersion(unsigned char *hashBuffer, NSString *path, uint16_t __unused majorVersion, NSMutableDictionary *> *hashToFileKeyDictionary, NSMutableDictionary *fileKeyToHashDictionary) { char pathBuffer[PATH_MAX] = { 0 }; if (![path getFileSystemRepresentation:pathBuffer maxLength:sizeof(pathBuffer)]) { - return nil; + return NO; } char *const sourcePaths[] = { pathBuffer, 0 }; FTS *fts = fts_open(sourcePaths, FTS_PHYSICAL | FTS_NOCHDIR, compareFiles); if (!fts) { perror("fts_open"); - return nil; + return NO; } CC_SHA1_CTX hashContext; @@ -167,9 +176,25 @@ NSString *hashOfTreeWithVersion(NSString *path, uint16_t __unused majorVersion) unsigned char fileHash[CC_SHA1_DIGEST_LENGTH]; if (!_hashOfFileContents(fileHash, ent)) { - return nil; + return NO; } CC_SHA1_Update(&hashContext, fileHash, sizeof(fileHash)); + + // For file hash tables we only track regular files + if (ent->fts_info == FTS_F) { + NSData *hashKey = [NSData dataWithBytes:fileHash length:sizeof(fileHash)]; + + if (hashToFileKeyDictionary != nil) { + if (hashToFileKeyDictionary[hashKey] == nil) { + hashToFileKeyDictionary[hashKey] = [NSMutableArray array]; + } + [hashToFileKeyDictionary[hashKey] addObject:relativePath]; + } + + if (fileKeyToHashDictionary != nil) { + fileKeyToHashDictionary[relativePath] = hashKey; + } + } const char *relativePathBytes = [relativePath fileSystemRepresentation]; CC_SHA1_Update(&hashContext, relativePathBytes, (CC_LONG)strlen(relativePathBytes)); @@ -188,17 +213,43 @@ NSString *hashOfTreeWithVersion(NSString *path, uint16_t __unused majorVersion) } fts_close(fts); - unsigned char hash[CC_SHA1_DIGEST_LENGTH]; - CC_SHA1_Final(hash, &hashContext); + CC_SHA1_Final(hashBuffer, &hashContext); + + return YES; +} - char hexHash[CC_SHA1_DIGEST_LENGTH * 2 + 1]; - size_t i; - for (i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) +void getRawHashFromDisplayHash(unsigned char *hash, NSString *hexHash) +{ + const char *hexString = hexHash.UTF8String; + if (hexString == NULL) { + return; + } + + for (size_t blockIndex = 0; blockIndex < CC_SHA1_DIGEST_LENGTH; blockIndex++) { + const char *currentBlock = hexString + blockIndex * 2; + char convertedBlock[3] = {currentBlock[0], currentBlock[1], '\0'}; + hash[blockIndex] = (unsigned char)strtol(convertedBlock, NULL, 16); + } +} + +NSString *displayHashFromRawHash(const unsigned char *hash) +{ + char hexHash[CC_SHA1_DIGEST_LENGTH * 2 + 1] = {0}; + for (size_t i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) { sprintf(hexHash + i * 2, "%02x", hash[i]); - + } return @(hexHash); } +NSString *hashOfTreeWithVersion(NSString *path, uint16_t majorVersion) +{ + unsigned char hash[CC_SHA1_DIGEST_LENGTH]; + if (!getRawHashOfTreeWithVersion(hash, path, majorVersion)) { + return nil; + } + return displayHashFromRawHash(hash); +} + extern NSString *hashOfTree(NSString *path) { return hashOfTreeWithVersion(path, LATEST_DELTA_DIFF_MAJOR_VERSION); diff --git a/Autoupdate/SUBinaryDeltaCreate.h b/Autoupdate/SUBinaryDeltaCreate.h index 7111b503..e6315815 100644 --- a/Autoupdate/SUBinaryDeltaCreate.h +++ b/Autoupdate/SUBinaryDeltaCreate.h @@ -10,8 +10,9 @@ #define SUBINARYDELTACREATE_H #import "SUBinaryDeltaCommon.h" +#import "SPUDeltaArchiveProtocol.h" @class NSString; -BOOL createBinaryDelta(NSString *source, NSString *destination, NSString *patchFile, SUBinaryDeltaMajorVersion majorVersion, BOOL verbose, NSError * __autoreleasing *error); +BOOL createBinaryDelta(NSString *source, NSString *destination, NSString *patchFile, SUBinaryDeltaMajorVersion majorVersion, SPUDeltaCompressionMode compression, int32_t compressionLevel, BOOL verbose, NSError * __autoreleasing *error); #endif diff --git a/Autoupdate/SUBinaryDeltaCreate.m b/Autoupdate/SUBinaryDeltaCreate.m index 0678fb4e..09cbd0e7 100644 --- a/Autoupdate/SUBinaryDeltaCreate.m +++ b/Autoupdate/SUBinaryDeltaCreate.m @@ -10,7 +10,8 @@ #import #include "SUBinaryDeltaCommon.h" #import "SPUDeltaArchiveProtocol.h" -#import "SPUDeltaArchive.h" +#import "SPUSparkleDeltaArchive.h" +#import "SPUXarDeltaArchive.h" #import #include #include @@ -90,15 +91,6 @@ static NSDictionary *infoForFile(FTSENT *ent) INFO_SIZE_KEY: @(size) }; } -static bool isSymLink(const FTSENT *ent) -{ - if (ent->fts_info == FTS_SL) - { - return (true); - } - return false; -} - static bool aclExists(const FTSENT *ent) { // macOS does not currently support ACLs for symlinks @@ -193,7 +185,7 @@ static BOOL shouldSkipDeltaCompression(NSDictionary *originalInfo, NSDictionary return NO; } -static BOOL shouldDeleteThenExtract(NSDictionary *originalInfo, NSDictionary *newInfo) +static BOOL shouldDeleteThenExtract(NSDictionary *originalInfo, NSDictionary *newInfo, SUBinaryDeltaMajorVersion majorVersion) { if (!originalInfo) { return NO; @@ -202,6 +194,14 @@ static BOOL shouldDeleteThenExtract(NSDictionary *originalInfo, NSDictionary *ne if ([(NSNumber *)originalInfo[INFO_TYPE_KEY] unsignedShortValue] != [(NSNumber *)newInfo[INFO_TYPE_KEY] unsignedShortValue]) { return YES; } + + // If the same file exists in both old and new trees and they have different content, + // we should always issue deleting the old file before extracting the new + // I believe xar container in version 2 format automatically did this if the file types were the same + // but we don't want to rely on that implicitness + if (MAJOR_VERSION_IS_AT_LEAST(majorVersion, SUBinaryDeltaMajorVersion3)) { + return YES; + } return NO; } @@ -250,7 +250,54 @@ static BOOL shouldChangePermissions(NSDictionary *originalInfo, NSDictionary *ne return YES; } -BOOL createBinaryDelta(NSString *source, NSString *destination, NSString *patchFile, SUBinaryDeltaMajorVersion majorVersion, BOOL verbose, NSError *__autoreleasing *error) +#define MIN_SIZE_FOR_CLONE 4096 +static NSString *cloneableRelativePath(NSDictionary *afterFileKeyToHashDictionary, NSDictionary *> *beforeHashToFileKeyDictionary, NSDictionary *originalTreeState, NSDictionary *newInfo, NSString *key, NSNumber * __autoreleasing *outPermissions) +{ + if (afterFileKeyToHashDictionary == nil) { + return nil; + } + + // Avoid clones for small files. Small files can compress very well, sometimes better than tracking clones. + if ([(NSNumber *)newInfo[INFO_SIZE_KEY] unsignedLongLongValue] <= MIN_SIZE_FOR_CLONE) { + return nil; + } + + NSData *keyHash = afterFileKeyToHashDictionary[key]; + if (keyHash == nil) { + return nil; + } + + for (NSString *oldRelativePath in beforeHashToFileKeyDictionary[keyHash]) { + NSDictionary *oldCloneInfo = originalTreeState[oldRelativePath]; + if (oldCloneInfo == nil) { + continue; + } + + if ([(NSNumber *)oldCloneInfo[INFO_TYPE_KEY] unsignedShortValue] != FTS_F || [(NSNumber *)newInfo[INFO_TYPE_KEY] unsignedShortValue] != FTS_F) { + continue; + } + + NSString *clonePath = oldCloneInfo[INFO_PATH_KEY]; + NSString *newPath = newInfo[INFO_PATH_KEY]; + + if (![[NSFileManager defaultManager] contentsEqualAtPath:clonePath andPath:newPath]) { + continue; + } + + NSNumber *newPermissions = newInfo[INFO_PERMISSIONS_KEY]; + if ([(NSNumber *)oldCloneInfo[INFO_PERMISSIONS_KEY] unsignedShortValue] != [newPermissions unsignedShortValue]) { + if (outPermissions != NULL) { + *outPermissions = newPermissions; + } + } + + return oldRelativePath; + } + + return nil; +} + +BOOL createBinaryDelta(NSString *source, NSString *destination, NSString *patchFile, SUBinaryDeltaMajorVersion majorVersion, SPUDeltaCompressionMode compression, int32_t compressionLevel, BOOL verbose, NSError *__autoreleasing *error) { assert(source); assert(destination); @@ -328,9 +375,10 @@ BOOL createBinaryDelta(NSString *source, NSString *destination, NSString *patchF } fts_close(fts); - NSString *beforeHash = hashOfTreeWithVersion(source, majorVersion); - - if (!beforeHash) { + NSMutableDictionary *> *beforeHashToFileKeyDictionary = MAJOR_VERSION_IS_AT_LEAST(majorVersion, SUBinaryDeltaMajorVersion3) ? [NSMutableDictionary dictionary] : nil; + + unsigned char beforeHash[CC_SHA1_DIGEST_LENGTH] = {0}; + if (!getRawHashOfTreeAndFileTablesWithVersion(beforeHash, source, majorVersion, beforeHashToFileKeyDictionary, nil)) { if (verbose) { fprintf(stderr, "\n"); } @@ -390,23 +438,10 @@ BOOL createBinaryDelta(NSString *source, NSString *destination, NSString *patchF return NO; } - // We should validate permissions and ACLs even if we don't store the info in the diff in the case of ACLs, - // or in the case of permissions if the patch version is 1 - + // We should validate ACLs even if we don't store the info in the diff in the case of ACLs // We should also not allow files with code signed extended attributes since Apple doesn't recommend inserting these // inside an application, and since we don't preserve extended attribitutes anyway - mode_t permissions = [(NSNumber*)info[INFO_PERMISSIONS_KEY] unsignedShortValue]; - if (!isSymLink(ent) && !IS_VALID_PERMISSIONS(permissions)) { - if (verbose) { - fprintf(stderr, "\n"); - } - if (error != NULL) { - *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadUnknownError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Invalid file permissions after-tree on file %@ (only permissions with modes 0755 and 0644 are supported)", @(ent->fts_path)] }]; - } - return NO; - } - if (aclExists(ent)) { if (verbose) { fprintf(stderr, "\n"); @@ -449,8 +484,10 @@ BOOL createBinaryDelta(NSString *source, NSString *destination, NSString *patchF } fts_close(fts); - NSString *afterHash = hashOfTreeWithVersion(destination, majorVersion); - if (!afterHash) { + NSMutableDictionary *afterFileKeyToHashDictionary = MAJOR_VERSION_IS_AT_LEAST(majorVersion, SUBinaryDeltaMajorVersion3) ? [NSMutableDictionary dictionary] : nil; + + unsigned char afterHash[CC_SHA1_DIGEST_LENGTH] = {0}; + if (!getRawHashOfTreeAndFileTablesWithVersion(afterHash, destination, majorVersion, nil, afterFileKeyToHashDictionary)) { if (verbose) { fprintf(stderr, "\n"); } @@ -469,18 +506,25 @@ BOOL createBinaryDelta(NSString *source, NSString *destination, NSString *patchF fprintf(stderr, "\nWriting to temporary file %s...", [temporaryFile fileSystemRepresentation]); } - id archive = SPUDeltaArchiveForWriting(temporaryFile); - if (archive == nil) { + id archive; + if (majorVersion >= SUBinaryDeltaMajorVersion3) { + archive = [[SPUSparkleDeltaArchive alloc] initWithPatchFileForWriting:temporaryFile compression:compression compressionLevel:compressionLevel]; + } else { + archive = [[SPUXarDeltaArchive alloc] initWithPatchFileForWriting:temporaryFile compression:compression compressionLevel:compressionLevel]; + } + + SPUDeltaArchiveHeader *header = [[SPUDeltaArchiveHeader alloc] initWithMajorVersion:majorVersion minorVersion:minorVersion beforeTreeHash:beforeHash afterTreeHash:afterHash]; + + [archive writeHeader:header]; + if (archive.error != nil) { if (verbose) { fprintf(stderr, "\n"); } if (error != NULL) { - *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileWriteUnknownError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to write to %@", temporaryFile] }]; + *error = archive.error; } return NO; } - - [archive setMajorVersion:majorVersion minorVersion:minorVersion beforeTreeHash:beforeHash afterTreeHash:afterHash]; NSOperationQueue *deltaQueue = [[NSOperationQueue alloc] init]; NSMutableArray *deltaOperations = [NSMutableArray array]; @@ -500,7 +544,7 @@ BOOL createBinaryDelta(NSString *source, NSString *destination, NSString *patchF id value = [newTreeState valueForKey:key]; if ([(NSObject *)value isEqual:[NSNull null]]) { - [archive addRelativeFilePath:key realFilePath:nil attributes:SPUDeltaFileAttributesDelete permissions:0]; + [archive addItem:[[SPUDeltaArchiveItem alloc] initWithRelativeFilePath:key commands:SPUDeltaItemCommandDelete permissions:0]]; if (verbose) { fprintf(stderr, "\n❌ %s %s", VERBOSE_REMOVED, [key fileSystemRepresentation]); @@ -513,24 +557,52 @@ BOOL createBinaryDelta(NSString *source, NSString *destination, NSString *patchF if (shouldSkipDeltaCompression(originalInfo, newInfo)) { if (shouldSkipExtracting(originalInfo, newInfo)) { if (shouldChangePermissions(originalInfo, newInfo)) { - [archive addRelativeFilePath:key realFilePath:nil attributes:SPUDeltaFileAttributesModifyPermissions permissions:[(NSNumber *)newInfo[INFO_PERMISSIONS_KEY] unsignedShortValue]]; + [archive addItem:[[SPUDeltaArchiveItem alloc] initWithRelativeFilePath:key commands:SPUDeltaItemCommandModifyPermissions permissions:[(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]); } } } else { - NSString *path = [destination stringByAppendingPathComponent:key]; - - SPUDeltaFileAttributes attributes = shouldDeleteThenExtract(originalInfo, newInfo) ? (SPUDeltaFileAttributesDelete | SPUDeltaFileAttributesExtract) : SPUDeltaFileAttributesExtract; - - [archive addRelativeFilePath:key realFilePath:path attributes:attributes permissions:0]; + // Check if the new file can be cloned from an old existing one located at a different path + NSNumber *newPermissionsFromClone = nil; + NSString *clonedRelativePath = cloneableRelativePath(afterFileKeyToHashDictionary, beforeHashToFileKeyDictionary, originalTreeState, newInfo, key, &newPermissionsFromClone); + if (clonedRelativePath != nil) { + SPUDeltaItemCommands commands = SPUDeltaItemCommandClone; + if (shouldDeleteThenExtract(originalInfo, newInfo, majorVersion)) { + commands |= SPUDeltaItemCommandDelete; + } + if (newPermissionsFromClone != nil) { + commands |= SPUDeltaItemCommandModifyPermissions; + } + + SPUDeltaArchiveItem *item = [[SPUDeltaArchiveItem alloc] initWithRelativeFilePath:key commands:commands permissions:newPermissionsFromClone.unsignedShortValue]; + // Physical path for clones points to the old file + item.physicalFilePath = [source stringByAppendingPathComponent:clonedRelativePath]; + item.clonedRelativePath = clonedRelativePath; + + [archive addItem:item]; + + if (verbose) { + fprintf(stderr, "\n✂️ %s %s -> %s", VERBOSE_CLONED, [clonedRelativePath fileSystemRepresentation], [key fileSystemRepresentation]); + } + } else { + // Otherwise add a new file + NSString *path = [destination stringByAppendingPathComponent:key]; + + SPUDeltaItemCommands commands = shouldDeleteThenExtract(originalInfo, newInfo, majorVersion) ? (SPUDeltaItemCommandDelete | SPUDeltaItemCommandExtract) : SPUDeltaItemCommandExtract; + + SPUDeltaArchiveItem *item = [[SPUDeltaArchiveItem alloc] initWithRelativeFilePath:key commands:commands permissions:0]; + item.physicalFilePath = path; + + [archive addItem:item]; - if (verbose) { - if (originalInfo) { - fprintf(stderr, "\n✏️ %s %s", VERBOSE_UPDATED, [key fileSystemRepresentation]); - } else { - fprintf(stderr, "\n✅ %s %s", VERBOSE_ADDED, [key fileSystemRepresentation]); + if (verbose) { + if (originalInfo) { + fprintf(stderr, "\n✏️ %s %s", VERBOSE_UPDATED, [key fileSystemRepresentation]); + } else { + fprintf(stderr, "\n✅ %s %s", VERBOSE_ADDED, [key fileSystemRepresentation]); + } } } } @@ -547,6 +619,7 @@ BOOL createBinaryDelta(NSString *source, NSString *destination, NSString *patchF [deltaQueue waitUntilAllOperationsAreFinished]; + BOOL deltaOperationsFailed = NO; for (CreateBinaryDeltaOperation *operation in deltaOperations) { NSString *resultPath = [operation resultPath]; if (resultPath == nil) { @@ -556,7 +629,8 @@ BOOL createBinaryDelta(NSString *source, NSString *destination, NSString *patchF if (error != NULL) { *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileWriteUnknownError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to create patch from source %@ and destination %@", operation.relativePath, resultPath] }]; } - return NO; + deltaOperationsFailed = YES; + break; } if (verbose) { @@ -566,11 +640,12 @@ BOOL createBinaryDelta(NSString *source, NSString *destination, NSString *patchF NSNumber *permissions = operation.permissions; NSString *relativePath = operation.relativePath; - SPUDeltaFileAttributes attributes = (permissions != nil) ? (SPUDeltaFileAttributesBinaryDiff | SPUDeltaFileAttributesModifyPermissions) : SPUDeltaFileAttributesBinaryDiff; + SPUDeltaItemCommands commands = (permissions != nil) ? (SPUDeltaItemCommandBinaryDiff | SPUDeltaItemCommandModifyPermissions) : SPUDeltaItemCommandBinaryDiff; - [archive addRelativeFilePath:relativePath realFilePath:resultPath attributes:attributes permissions:permissions.unsignedShortValue]; + SPUDeltaArchiveItem *item = [[SPUDeltaArchiveItem alloc] initWithRelativeFilePath:relativePath commands:commands permissions:permissions.unsignedShortValue]; + item.physicalFilePath = resultPath; - unlink(resultPath.fileSystemRepresentation); + [archive addItem:item]; if (permissions != nil) { if (verbose) { @@ -578,8 +653,36 @@ BOOL createBinaryDelta(NSString *source, NSString *destination, NSString *patchF } } } - + + if (!deltaOperationsFailed) { + [archive finishEncodingItems]; + } + [archive close]; + + // Clean up operations after the archive has finished encoding + for (CreateBinaryDeltaOperation *operation in deltaOperations) { + NSString *resultPath = [operation resultPath]; + if (resultPath != nil) { + unlink(resultPath.fileSystemRepresentation); + } + } + + if (deltaOperationsFailed) { + // We already set an error so let's bail + return NO; + } + + NSError *archiveError = archive.error; + if (archiveError != nil) { + if (verbose) { + fprintf(stderr, "\n"); + } + if (error != NULL) { + *error = archiveError; + } + return NO; + } NSFileManager *filemgr; filemgr = [NSFileManager defaultManager]; diff --git a/Autoupdate/SUBinaryDeltaTool.m b/Autoupdate/SUBinaryDeltaTool.m index 66e97404..029e6eeb 100644 --- a/Autoupdate/SUBinaryDeltaTool.m +++ b/Autoupdate/SUBinaryDeltaTool.m @@ -73,7 +73,7 @@ static BOOL runCreateCommand(NSString *programName, NSArray *args) SUBinaryDeltaMajorVersion patchVersion = !versionComponents ? - LATEST_DELTA_DIFF_MAJOR_VERSION : + SUBinaryDeltaMajorVersionDefault : (SUBinaryDeltaMajorVersion)[[versionComponents[1] componentsSeparatedByString:@"."][0] intValue]; // ignore minor version if provided if (patchVersion < FIRST_DELTA_DIFF_MAJOR_VERSION) { @@ -119,9 +119,37 @@ static BOOL runCreateCommand(NSString *programName, NSArray *args) fprintf(stderr, "Error: after-tree must be a directory\n"); return NO; } + + // Until we have a better way of passing/parsing command line options we'll use environment variables + + SPUDeltaCompressionMode compression; + const char *compressionEnv = getenv("SPARKLE_DELTA_COMPRESSION"); + if (compressionEnv == NULL) { + compression = SPUDeltaCompressionModeDefault; + } else if (strcmp(compressionEnv, "bzip2") == 0) { + compression = SPUDeltaCompressionModeBzip2; + } else if (strcmp(compressionEnv, "lzma") == 0) { + compression = SPUDeltaCompressionModeLZMA; + } else if (strcmp(compressionEnv, "lzfse") == 0) { + compression = SPUDeltaCompressionModeLZFSE; + } else if (strcmp(compressionEnv, "lz4") == 0) { + compression = SPUDeltaCompressionModeLZ4; + } else if (strcmp(compressionEnv, "zlib") == 0) { + compression = SPUDeltaCompressionModeZLIB; + } else { + compression = SPUDeltaCompressionModeNone; + } + + int32_t compressionLevel; + const char *compressionLevelEnv = getenv("SPARKLE_DELTA_COMPRESSION_LEVEL"); + if (compressionLevelEnv == NULL) { + compressionLevel = DEFAULT_BZIP2_COMPRESSION_LEVEL; + } else { + compressionLevel = atoi(compressionLevelEnv); + } NSError *createDiffError = nil; - if (!createBinaryDelta(sourcePath, destPath, patchPath, patchVersion, verbose, &createDiffError)) { + if (!createBinaryDelta(sourcePath, destPath, patchPath, patchVersion, compression, compressionLevel, verbose, &createDiffError)) { fprintf(stderr, "%s\n", [createDiffError.localizedDescription UTF8String]); return NO; } @@ -192,15 +220,15 @@ static BOOL runVersionCommand(NSString *programName, NSArray *args) uint16_t majorDiffVersion = 0; uint16_t minorDiffVersion = 0; - id archive = SPUDeltaArchiveForReading(patchFile); - if (archive == nil) { + SPUDeltaArchiveHeader *header = nil; + id archive = SPUDeltaArchiveReadPatchAndHeader(patchFile, &header); + if (archive.error != nil) { fprintf(stderr, "Unable to open patch %s\n", [patchFile fileSystemRepresentation]); + fprintf(stderr, "%s\n", archive.error.localizedDescription.UTF8String); return NO; } - [archive getMajorDeltaVersion:&majorDiffVersion minorDeltaVersion:&minorDiffVersion beforeTreeHash:NULL afterTreeHash:NULL]; - - if (majorDiffVersion < FIRST_DELTA_DIFF_MAJOR_VERSION) { + if (header.majorVersion < FIRST_DELTA_DIFF_MAJOR_VERSION) { fprintf(stderr, "Unable to retrieve version information from patch %s\n", [patchFile fileSystemRepresentation]); return NO; } diff --git a/Configurations/bsdiff-Release.xcconfig b/Configurations/bsdiff-Release.xcconfig index 58b53f33..62edec49 100644 --- a/Configurations/bsdiff-Release.xcconfig +++ b/Configurations/bsdiff-Release.xcconfig @@ -1,3 +1,6 @@ // Copyright © 2019 Sparkle Project. All rights reserved. #include "bsdiff-Shared.xcconfig" + +// Disable asserts for performance +GCC_PREPROCESSOR_DEFINITIONS = $(GCC_PREPROCESSOR_DEFINITIONS) NDEBUG=1 diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 31422d31..b7ded981 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -215,6 +215,9 @@ 725602D51C83551C00DAA70E /* SUApplicationInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = 725602D31C83551C00DAA70E /* SUApplicationInfo.h */; }; 725602D61C83551C00DAA70E /* SUApplicationInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 725602D41C83551C00DAA70E /* SUApplicationInfo.m */; }; 725B3A82263FBF0C0041AB8E /* testappcast_minimumAutoupdateVersion.xml in Resources */ = {isa = PBXBuildFile; fileRef = 725B3A81263FBF0C0041AB8E /* testappcast_minimumAutoupdateVersion.xml */; }; + 725B81FA2781AEAF0041746F /* libcompression.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 725B81F92781AEA40041746F /* libcompression.tbd */; }; + 725B81FB2781CD930041746F /* libcompression.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 725B81F92781AEA40041746F /* libcompression.tbd */; }; + 725B81FC2781CDC20041746F /* libcompression.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 725B81F92781AEA40041746F /* libcompression.tbd */; }; 725CB9571C7120410064365A /* SPUUserDriver.h in Headers */ = {isa = PBXBuildFile; fileRef = 725CB9561C7120410064365A /* SPUUserDriver.h */; settings = {ATTRIBUTES = (Public, ); }; }; 725CB95A1C7121830064365A /* SPUStandardUserDriver.h in Headers */ = {isa = PBXBuildFile; fileRef = 725CB9581C7121830064365A /* SPUStandardUserDriver.h */; settings = {ATTRIBUTES = (Public, ); }; }; 725CB95B1C7121830064365A /* SPUStandardUserDriver.m in Sources */ = {isa = PBXBuildFile; fileRef = 725CB9591C7121830064365A /* SPUStandardUserDriver.m */; }; @@ -328,6 +331,10 @@ 728337A61C9E6FF40085AA99 /* SPUProbeInstallStatus.h in Headers */ = {isa = PBXBuildFile; fileRef = 728337A41C9E6FF40085AA99 /* SPUProbeInstallStatus.h */; }; 728337A71C9E6FF40085AA99 /* SPUProbeInstallStatus.m in Sources */ = {isa = PBXBuildFile; fileRef = 728337A51C9E6FF40085AA99 /* SPUProbeInstallStatus.m */; }; 728638ED1CAF50CE00783084 /* SUConstants.m in Sources */ = {isa = PBXBuildFile; fileRef = 61299A5F09CA6EB100B7442F /* SUConstants.m */; }; + 728ED34A277DA23400D9238F /* SPUSparkleDeltaArchive.m in Sources */ = {isa = PBXBuildFile; fileRef = 728ED349277DA23400D9238F /* SPUSparkleDeltaArchive.m */; }; + 728ED34B277DA23400D9238F /* SPUSparkleDeltaArchive.m in Sources */ = {isa = PBXBuildFile; fileRef = 728ED349277DA23400D9238F /* SPUSparkleDeltaArchive.m */; }; + 728ED34C277DA23400D9238F /* SPUSparkleDeltaArchive.m in Sources */ = {isa = PBXBuildFile; fileRef = 728ED349277DA23400D9238F /* SPUSparkleDeltaArchive.m */; }; + 728ED34D277DA23400D9238F /* SPUSparkleDeltaArchive.m in Sources */ = {isa = PBXBuildFile; fileRef = 728ED349277DA23400D9238F /* SPUSparkleDeltaArchive.m */; }; 729051E41DC82AC0003DEA7F /* SUOperatingSystem.m in Sources */ = {isa = PBXBuildFile; fileRef = 726F2CE41BC9C33D001971A4 /* SUOperatingSystem.m */; }; 7293A1AE1CEE933800B957A7 /* SPUURLRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = 7293A1AC1CEE933800B957A7 /* SPUURLRequest.h */; }; 7293A1AF1CEE933800B957A7 /* SPUURLRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 7293A1AD1CEE933800B957A7 /* SPUURLRequest.m */; }; @@ -1136,6 +1143,7 @@ 725602D41C83551C00DAA70E /* SUApplicationInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUApplicationInfo.m; sourceTree = ""; }; 72563CA9272E1C5400AF39F0 /* .github */ = {isa = PBXFileReference; lastKnownFileType = folder; path = .github; sourceTree = ""; }; 725B3A81263FBF0C0041AB8E /* testappcast_minimumAutoupdateVersion.xml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = testappcast_minimumAutoupdateVersion.xml; sourceTree = ""; }; + 725B81F92781AEA40041746F /* libcompression.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libcompression.tbd; path = usr/lib/libcompression.tbd; sourceTree = SDKROOT; }; 725CB9561C7120410064365A /* SPUUserDriver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPUUserDriver.h; sourceTree = ""; }; 725CB9581C7121830064365A /* SPUStandardUserDriver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPUStandardUserDriver.h; sourceTree = ""; }; 725CB9591C7121830064365A /* SPUStandardUserDriver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPUStandardUserDriver.m; sourceTree = ""; }; @@ -1239,6 +1247,8 @@ 728337A41C9E6FF40085AA99 /* SPUProbeInstallStatus.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPUProbeInstallStatus.h; sourceTree = ""; }; 728337A51C9E6FF40085AA99 /* SPUProbeInstallStatus.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPUProbeInstallStatus.m; sourceTree = ""; }; 728638EE1CAF589C00783084 /* ConfigDownloader.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = ConfigDownloader.xcconfig; sourceTree = ""; }; + 728ED348277DA23400D9238F /* SPUSparkleDeltaArchive.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SPUSparkleDeltaArchive.h; path = Autoupdate/SPUSparkleDeltaArchive.h; sourceTree = SOURCE_ROOT; }; + 728ED349277DA23400D9238F /* SPUSparkleDeltaArchive.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = SPUSparkleDeltaArchive.m; path = Autoupdate/SPUSparkleDeltaArchive.m; sourceTree = SOURCE_ROOT; }; 7293A1AC1CEE933800B957A7 /* SPUURLRequest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPUURLRequest.h; sourceTree = ""; }; 7293A1AD1CEE933800B957A7 /* SPUURLRequest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPUURLRequest.m; sourceTree = ""; }; 729924921DF4A45000DBCDF5 /* SUUpdateValidator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SUUpdateValidator.h; path = Sparkle/SUUpdateValidator.h; sourceTree = SOURCE_ROOT; }; @@ -1407,6 +1417,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 725B81FB2781CD930041746F /* libcompression.tbd in Frameworks */, 5A06357323FE333600478A72 /* libed25519.a in Frameworks */, EA1E281822B645CE004AA304 /* libbsdiff.a in Frameworks */, 14732BD119610A1200593899 /* AppKit.framework in Frameworks */, @@ -1500,6 +1511,7 @@ EA1E281722B645AE004AA304 /* libbsdiff.a in Frameworks */, 7267E5C91D3D8C4300D1BF90 /* CoreServices.framework in Frameworks */, 7267E5CA1D3D8C4800D1BF90 /* Foundation.framework in Frameworks */, + 725B81FA2781AEAF0041746F /* libcompression.tbd in Frameworks */, 5A06357023FE332300478A72 /* libed25519.a in Frameworks */, 7267E5D61D3D8D3500D1BF90 /* libbz2.tbd in Frameworks */, 7267E5D51D3D8D2800D1BF90 /* libxar.tbd in Frameworks */, @@ -1520,6 +1532,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 725B81FC2781CDC20041746F /* libcompression.tbd in Frameworks */, 1495006F195FCE1800BC5B5B /* Foundation.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; @@ -1602,6 +1615,7 @@ 0867D69AFE84028FC02AAC07 /* Frameworks */ = { isa = PBXGroup; children = ( + 725B81F92781AEA40041746F /* libcompression.tbd */, 0867D6A5FE840307C02AAC07 /* AppKit.framework */, 721BC2081D17A553002BC71E /* Carbon.framework */, 525A278F133D6AE900FD8D70 /* Cocoa.framework */, @@ -1776,6 +1790,8 @@ 725EE481277BF17A00D820CE /* SPUDeltaArchiveProtocol.h */, 725EE484277D375F00D820CE /* SPUDeltaArchive.h */, 725EE485277D375F00D820CE /* SPUDeltaArchive.m */, + 728ED348277DA23400D9238F /* SPUSparkleDeltaArchive.h */, + 728ED349277DA23400D9238F /* SPUSparkleDeltaArchive.m */, 725EE47E277BF13A00D820CE /* SPUXarDeltaArchive.h */, 725EE47F277BF13B00D820CE /* SPUXarDeltaArchive.m */, 7267E5741D3D895B00D1BF90 /* SUBinaryDeltaTool.m */, @@ -3173,6 +3189,7 @@ buildActionMask = 2147483647; files = ( 725EE482277BF44A00D820CE /* SPUXarDeltaArchive.m in Sources */, + 728ED34C277DA23400D9238F /* SPUSparkleDeltaArchive.m in Sources */, 7267E5761D3D895B00D1BF90 /* SUBinaryDeltaApply.m in Sources */, 725EE487277D376000D820CE /* SPUDeltaArchive.m in Sources */, 7267E5781D3D895B00D1BF90 /* SUBinaryDeltaCommon.m in Sources */, @@ -3197,6 +3214,7 @@ 7267E5EE1D3D915900D1BF90 /* SUBinaryDeltaApply.m in Sources */, 7267E5EF1D3D915900D1BF90 /* SUBinaryDeltaCommon.m in Sources */, 7267E5F01D3D915900D1BF90 /* SUBinaryDeltaCreate.m in Sources */, + 728ED34B277DA23400D9238F /* SPUSparkleDeltaArchive.m in Sources */, 142E0E0919A83AAC00E4312B /* SUBinaryDeltaTest.m in Sources */, 7267E5F11D3D917A00D1BF90 /* SUBinaryDeltaUnarchiver.m in Sources */, F8761EB11ADC5068000C9034 /* SUCodeSigningVerifierTest.m in Sources */, @@ -3249,6 +3267,7 @@ 7205C44E1E1304CE00E370AE /* Signatures.swift in Sources */, 7205C44F1E1304CE00E370AE /* FeedXML.swift in Sources */, 7205C4411E13049400E370AE /* main.swift in Sources */, + 728ED34D277DA23400D9238F /* SPUSparkleDeltaArchive.m in Sources */, 7205C45F1E13066F00E370AE /* SUBinaryDeltaApply.m in Sources */, 7205C4621E1306A600E370AE /* SUBinaryDeltaCommon.m in Sources */, FA30773F24CBC3E9007BA37D /* URL+Hashing.swift in Sources */, @@ -3371,6 +3390,7 @@ buildActionMask = 2147483647; files = ( 725EE480277BF13B00D820CE /* SPUXarDeltaArchive.m in Sources */, + 728ED34A277DA23400D9238F /* SPUSparkleDeltaArchive.m in Sources */, 72EF30C42675CFA1008CE987 /* SPUAppcastItemState.m in Sources */, 72EF30C52675CFA1008CE987 /* SPUAppcastItemStateResolver.m in Sources */, 72464F701E1F31E000FB341C /* SUOperatingSystem.m in Sources */, diff --git a/Tests/SUBinaryDeltaTest.m b/Tests/SUBinaryDeltaTest.m index 4075854b..4cfca164 100644 --- a/Tests/SUBinaryDeltaTest.m +++ b/Tests/SUBinaryDeltaTest.m @@ -46,7 +46,7 @@ typedef void (^SUDeltaHandler)(NSFileManager *fileManager, NSString *sourceDirec XCTAssert(YES, @"Pass"); } -- (BOOL)createAndApplyPatchUsingVersion:(SUBinaryDeltaMajorVersion)majorVersion beforeDiffHandler:(SUDeltaHandler)beforeDiffHandler afterDiffHandler:(SUDeltaHandler)afterDiffHandler +- (BOOL)createAndApplyPatchUsingVersion:(SUBinaryDeltaMajorVersion)majorVersion compressionMode:(SPUDeltaCompressionMode)compressionMode beforeDiffHandler:(SUDeltaHandler)beforeDiffHandler afterDiffHandler:(SUDeltaHandler)afterDiffHandler { NSString *sourceDirectory = temporaryDirectory(@"Spąrkle_temp1エンジン"); NSString *destinationDirectory = temporaryDirectory(@"Spąrkle_temp2エンジン"); @@ -65,7 +65,7 @@ typedef void (^SUDeltaHandler)(NSFileManager *fileManager, NSString *sourceDirec } NSError *createDiffError = nil; - BOOL createdDiff = createBinaryDelta(sourceDirectory, destinationDirectory, diffFile, majorVersion, NO, &createDiffError); + BOOL createdDiff = createBinaryDelta(sourceDirectory, destinationDirectory, diffFile, majorVersion, compressionMode, DEFAULT_BZIP2_COMPRESSION_LEVEL, NO, &createDiffError); if (!createdDiff) { NSLog(@"Creating binary diff failed with error: %@", createDiffError); } else if (afterDiffHandler != nil) { @@ -92,7 +92,17 @@ typedef void (^SUDeltaHandler)(NSFileManager *fileManager, NSString *sourceDirec - (BOOL)createAndApplyPatchWithBeforeDiffHandler:(SUDeltaHandler)beforeDiffHandler afterDiffHandler:(SUDeltaHandler)afterDiffHandler { - return [self createAndApplyPatchUsingVersion:LATEST_DELTA_DIFF_MAJOR_VERSION beforeDiffHandler:beforeDiffHandler afterDiffHandler:afterDiffHandler]; + XCTAssertEqual(SUBinaryDeltaMajorVersion3, LATEST_DELTA_DIFF_MAJOR_VERSION); + + BOOL version3DeltaFormatWithLZMASuccess = [self createAndApplyPatchUsingVersion:SUBinaryDeltaMajorVersion3 compressionMode:SPUDeltaCompressionModeLZMA beforeDiffHandler:beforeDiffHandler afterDiffHandler:afterDiffHandler]; + + BOOL version3DeltaFormatWithBZIP2Success = [self createAndApplyPatchUsingVersion:SUBinaryDeltaMajorVersion3 compressionMode:SPUDeltaCompressionModeBzip2 beforeDiffHandler:beforeDiffHandler afterDiffHandler:afterDiffHandler]; + + BOOL version3DeltaFormatWithZLIBSuccess = [self createAndApplyPatchUsingVersion:SUBinaryDeltaMajorVersion3 compressionMode:SPUDeltaCompressionModeZLIB beforeDiffHandler:beforeDiffHandler afterDiffHandler:afterDiffHandler]; + + BOOL version2FormatSuccess = [self createAndApplyPatchUsingVersion:SUBinaryDeltaMajorVersion2 compressionMode:SPUDeltaCompressionModeDefault beforeDiffHandler:beforeDiffHandler afterDiffHandler:afterDiffHandler]; + + return (version3DeltaFormatWithLZMASuccess && version3DeltaFormatWithBZIP2Success && version3DeltaFormatWithZLIBSuccess && version2FormatSuccess); } - (void)createAndApplyPatchWithHandler:(SUDeltaHandler)handler @@ -355,10 +365,10 @@ typedef void (^SUDeltaHandler)(NSFileManager *fileManager, NSString *sourceDirec }]; } -// Make sure old version patches still work for simple cases +// Make sure old version patches are no longer supported - (void)testRegularFileAddedWithVersion1Delta { - XCTAssertFalse([self createAndApplyPatchUsingVersion:SUBinaryDeltaMajorVersion1 beforeDiffHandler:nil afterDiffHandler:nil]); + XCTAssertFalse([self createAndApplyPatchUsingVersion:SUBinaryDeltaMajorVersion1 compressionMode:SPUDeltaCompressionModeDefault beforeDiffHandler:nil afterDiffHandler:nil]); } - (void)testDirectoryAdded @@ -423,12 +433,52 @@ typedef void (^SUDeltaHandler)(NSFileManager *fileManager, NSString *sourceDirec } - (void)testRegularFileMove +{ + [self createAndApplyPatchWithHandler:^(NSFileManager *fileManager, NSString *sourceDirectory, NSString *destinationDirectory) { + NSString *sourceFile = [sourceDirectory stringByAppendingPathComponent:@"A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P/Q/R"]; + NSString *destinationFile = [destinationDirectory stringByAppendingPathComponent:@"A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P/Q/R2"]; + + [fileManager createDirectoryAtURL:[NSURL fileURLWithPath:sourceFile.stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:NULL]; + + [fileManager createDirectoryAtURL:[NSURL fileURLWithPath:destinationFile.stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:NULL]; + + NSData *data = [NSData dataWithBytes:"loltes" length:6]; + XCTAssertTrue([data writeToFile:sourceFile atomically:YES]); + XCTAssertTrue([data writeToFile:destinationFile atomically:YES]); + + XCTAssertFalse([self testDirectoryHashEqualityWithSource:sourceDirectory destination:destinationDirectory]); + }]; +} + +- (void)testLargerRegularFileMoveWithFileInPlace +{ + [self createAndApplyPatchWithHandler:^(NSFileManager *fileManager, NSString *sourceDirectory, NSString *destinationDirectory) { + NSString *sourceFile = [sourceDirectory stringByAppendingPathComponent:@"A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P/Q/R"]; + NSString *destinationFile = [destinationDirectory stringByAppendingPathComponent:@"A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P/Q/R2"]; + NSString *destinationFile2 = [destinationDirectory stringByAppendingPathComponent:@"A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P/Q/R"]; + NSString *destinationFile3 = [destinationDirectory stringByAppendingPathComponent:@"A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P/Q/X"]; + + [fileManager createDirectoryAtURL:[NSURL fileURLWithPath:sourceFile.stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:NULL]; + + [fileManager createDirectoryAtURL:[NSURL fileURLWithPath:destinationFile.stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:NULL]; + + NSData *data = [self bigData2]; + XCTAssertTrue([data writeToFile:sourceFile atomically:YES]); + XCTAssertTrue([data writeToFile:destinationFile atomically:YES]); + XCTAssertTrue([data writeToFile:destinationFile2 atomically:YES]); + XCTAssertTrue([data writeToFile:destinationFile3 atomically:YES]); + + XCTAssertFalse([self testDirectoryHashEqualityWithSource:sourceDirectory destination:destinationDirectory]); + }]; +} + +- (void)testRegularLargerFileMove { [self createAndApplyPatchWithHandler:^(NSFileManager *__unused fileManager, NSString *sourceDirectory, NSString *destinationDirectory) { NSString *sourceFile = [sourceDirectory stringByAppendingPathComponent:@"A"]; NSString *destinationFile = [destinationDirectory stringByAppendingPathComponent:@"B"]; - NSData *data = [NSData dataWithBytes:"loltest" length:7]; + NSData *data = [self bigData2]; XCTAssertTrue([data writeToFile:sourceFile atomically:YES]); XCTAssertTrue([data writeToFile:destinationFile atomically:YES]); @@ -436,6 +486,159 @@ typedef void (^SUDeltaHandler)(NSFileManager *fileManager, NSString *sourceDirec }]; } +- (void)testRegularFileMoveWithPermissionChange +{ + [self createAndApplyPatchWithHandler:^(NSFileManager *fileManager, NSString *sourceDirectory, NSString *destinationDirectory) { + NSString *sourceFile = [sourceDirectory stringByAppendingPathComponent:@"A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P/Q/R"]; + NSString *destinationFile = [destinationDirectory stringByAppendingPathComponent:@"A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P/Q/R2"]; + + [fileManager createDirectoryAtURL:[NSURL fileURLWithPath:sourceFile.stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:NULL]; + + [fileManager createDirectoryAtURL:[NSURL fileURLWithPath:destinationFile.stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:NULL]; + + NSData *data = [NSData dataWithBytes:"loltes" length:6]; + XCTAssertTrue([data writeToFile:sourceFile atomically:YES]); + XCTAssertTrue([data writeToFile:destinationFile atomically:YES]); + + if (chmod([destinationFile fileSystemRepresentation], 0777) != 0) { + NSLog(@"Change Permission Error.."); + XCTFail(@"Failed setting file permissions"); + } + + XCTAssertFalse([self testDirectoryHashEqualityWithSource:sourceDirectory destination:destinationDirectory]); + }]; +} + +- (void)testRegularLargerFileMoveWithPermissionChange +{ + [self createAndApplyPatchWithHandler:^(NSFileManager *__unused fileManager, NSString *sourceDirectory, NSString *destinationDirectory) { + NSString *sourceFile = [sourceDirectory stringByAppendingPathComponent:@"A"]; + NSString *destinationFile = [destinationDirectory stringByAppendingPathComponent:@"B"]; + + NSData *data = [self bigData2]; + XCTAssertTrue([data writeToFile:sourceFile atomically:YES]); + XCTAssertTrue([data writeToFile:destinationFile atomically:YES]); + + if (chmod([destinationFile fileSystemRepresentation], 0777) != 0) { + NSLog(@"Change Permission Error.."); + XCTFail(@"Failed setting file permissions"); + } + + XCTAssertFalse([self testDirectoryHashEqualityWithSource:sourceDirectory destination:destinationDirectory]); + }]; +} + +- (void)testSymbolicLinkMove +{ + [self createAndApplyPatchWithHandler:^(NSFileManager *fileManager, NSString *sourceDirectory, NSString *destinationDirectory) { + + NSString *sourceFile = [sourceDirectory stringByAppendingPathComponent:@"A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P/Q/R"]; + NSString *destinationFile = [destinationDirectory stringByAppendingPathComponent:@"A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P/Q/R2"]; + + [fileManager createDirectoryAtURL:[NSURL fileURLWithPath:sourceFile.stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:NULL]; + + [fileManager createDirectoryAtURL:[NSURL fileURLWithPath:destinationFile.stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:NULL]; + + NSError *error = nil; + + if (![fileManager createSymbolicLinkAtPath:sourceFile withDestinationPath:@"C" error:&error]) { + NSLog(@"Error in creating symlink: %@", error); + XCTFail(@"Failed to create symlink"); + } + + if (![fileManager createSymbolicLinkAtPath:destinationFile withDestinationPath:@"C" error:&error]) { + NSLog(@"Error in creating symlink: %@", error); + XCTFail(@"Failed to create symlink"); + } + + XCTAssertFalse([self testDirectoryHashEqualityWithSource:sourceDirectory destination:destinationDirectory]); + }]; +} + +- (void)testLargerSymbolicLinkMove +{ + [self createAndApplyPatchWithHandler:^(NSFileManager *fileManager, NSString *sourceDirectory, NSString *destinationDirectory) { + NSString *sourceFile = [sourceDirectory stringByAppendingPathComponent:@"A"]; + NSString *destinationFile = [destinationDirectory stringByAppendingPathComponent:@"B"]; + + NSError *error = nil; + + NSString *destinationPath = @"loltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltest"; + + if (![fileManager createSymbolicLinkAtPath:sourceFile withDestinationPath:destinationPath error:&error]) { + NSLog(@"Error in creating symlink: %@", error); + XCTFail(@"Failed to create symlink"); + } + + if (![fileManager createSymbolicLinkAtPath:destinationFile withDestinationPath:destinationPath error:&error]) { + NSLog(@"Error in creating symlink: %@", error); + XCTFail(@"Failed to create symlink"); + } + + XCTAssertFalse([self testDirectoryHashEqualityWithSource:sourceDirectory destination:destinationDirectory]); + }]; +} + +- (void)testSymbolicLinkMoveWithPermissionChange +{ + [self createAndApplyPatchWithHandler:^(NSFileManager *fileManager, NSString *sourceDirectory, NSString *destinationDirectory) { + NSString *sourceFile = [sourceDirectory stringByAppendingPathComponent:@"A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P/Q/R"]; + NSString *destinationFile = [destinationDirectory stringByAppendingPathComponent:@"A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P/Q/R2"]; + + [fileManager createDirectoryAtURL:[NSURL fileURLWithPath:sourceFile.stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:NULL]; + + [fileManager createDirectoryAtURL:[NSURL fileURLWithPath:destinationFile.stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:NULL]; + + NSError *error = nil; + + if (![fileManager createSymbolicLinkAtPath:sourceFile withDestinationPath:@"C" error:&error]) { + NSLog(@"Error in creating symlink: %@", error); + XCTFail(@"Failed to create symlink"); + } + + if (![fileManager createSymbolicLinkAtPath:destinationFile withDestinationPath:@"C" error:&error]) { + NSLog(@"Error in creating symlink: %@", error); + XCTFail(@"Failed to create symlink"); + } + + if (lchmod([destinationFile fileSystemRepresentation], 0777) != 0) { + NSLog(@"Change Permission Error.."); + XCTFail(@"Failed setting file permissions"); + } + + XCTAssertFalse([self testDirectoryHashEqualityWithSource:sourceDirectory destination:destinationDirectory]); + }]; +} + +- (void)testLargerSymbolicLinkMoveWithPermissionChange +{ + [self createAndApplyPatchWithHandler:^(NSFileManager *fileManager, NSString *sourceDirectory, NSString *destinationDirectory) { + NSString *sourceFile = [sourceDirectory stringByAppendingPathComponent:@"A"]; + NSString *destinationFile = [destinationDirectory stringByAppendingPathComponent:@"B"]; + + NSError *error = nil; + + NSString *destinationPath = @"loltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltest"; + + if (![fileManager createSymbolicLinkAtPath:sourceFile withDestinationPath:destinationPath error:&error]) { + NSLog(@"Error in creating symlink: %@", error); + XCTFail(@"Failed to create symlink"); + } + + if (![fileManager createSymbolicLinkAtPath:destinationFile withDestinationPath:destinationPath error:&error]) { + NSLog(@"Error in creating symlink: %@", error); + XCTFail(@"Failed to create symlink"); + } + + if (lchmod([destinationFile fileSystemRepresentation], 0777) != 0) { + NSLog(@"Change Permission Error.."); + XCTFail(@"Failed setting file permissions"); + } + + XCTAssertFalse([self testDirectoryHashEqualityWithSource:sourceDirectory destination:destinationDirectory]); + }]; +} + - (void)testCaseSensitiveRegularFileMove { [self createAndApplyPatchWithHandler:^(NSFileManager *__unused fileManager, NSString *sourceDirectory, NSString *destinationDirectory) { @@ -607,7 +810,7 @@ typedef void (^SUDeltaHandler)(NSFileManager *fileManager, NSString *sourceDirec }]; } -- (void)testInvalidPermissionsInAfterTree +- (void)testOddPermissionsInAfterTree { BOOL success = [self createAndApplyPatchWithBeforeDiffHandler:^(NSFileManager *fileManager, NSString *sourceDirectory, NSString *destinationDirectory) { NSString *sourceFile = [sourceDirectory stringByAppendingPathComponent:@"A"]; @@ -623,10 +826,9 @@ typedef void (^SUDeltaHandler)(NSFileManager *fileManager, NSString *sourceDirec XCTFail(@"Failed setting file permissions"); } - // This would fail for version 1.0 XCTAssertFalse([self testDirectoryHashEqualityWithSource:sourceDirectory destination:destinationDirectory]); } afterDiffHandler:nil]; - XCTAssertFalse(success); + XCTAssertTrue(success); } - (void)testBadPermissionsInBeforeTree @@ -778,6 +980,35 @@ typedef void (^SUDeltaHandler)(NSFileManager *fileManager, NSString *sourceDirec }]; } +- (void)testLargerRegularFileToSymlinkChange +{ + [self createAndApplyPatchWithHandler:^(NSFileManager *fileManager, NSString *sourceDirectory, NSString *destinationDirectory) { + NSString *sourceFile1 = [sourceDirectory stringByAppendingPathComponent:@"A"]; + NSString *sourceFile2 = [sourceDirectory stringByAppendingPathComponent:@"B"]; + + NSString *destinationFile1 = [destinationDirectory stringByAppendingPathComponent:@"A"]; + NSString *destinationFile2 = [destinationDirectory stringByAppendingPathComponent:@"B"]; + + NSData *data = [NSData dataWithBytes:"loltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltest" length:420]; + + XCTAssertTrue([data writeToFile:sourceFile1 atomically:YES]); + XCTAssertTrue([data writeToFile:sourceFile2 atomically:YES]); + + XCTAssertTrue([data writeToFile:destinationFile1 atomically:YES]); + + NSString *destination = @"loltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltest"; + + NSError *error = nil; + if (![fileManager createSymbolicLinkAtPath:destinationFile2 withDestinationPath:destination error:&error]) { + NSLog(@"Error in creating symlink: %@", error); + XCTFail(@"Failed to create symlink"); + } + + // This would fail with version 1.0 + XCTAssertFalse([self testDirectoryHashEqualityWithSource:sourceDirectory destination:destinationDirectory]); + }]; +} + - (void)testSymlinkToRegularFileChange { [self createAndApplyPatchWithHandler:^(NSFileManager *fileManager, NSString *sourceDirectory, NSString *destinationDirectory) { @@ -787,7 +1018,7 @@ typedef void (^SUDeltaHandler)(NSFileManager *fileManager, NSString *sourceDirec NSString *destinationFile1 = [destinationDirectory stringByAppendingPathComponent:@"A"]; NSString *destinationFile2 = [destinationDirectory stringByAppendingPathComponent:@"B"]; - NSData *data = [NSData dataWithBytes:"loltest" length:7]; + NSData *data = [NSData dataWithBytes:"loltes" length:6]; XCTAssertTrue([data writeToFile:sourceFile1 atomically:YES]); @@ -804,13 +1035,40 @@ typedef void (^SUDeltaHandler)(NSFileManager *fileManager, NSString *sourceDirec }]; } +- (void)testLargerSymlinkToRegularFileChange +{ + [self createAndApplyPatchWithHandler:^(NSFileManager *fileManager, NSString *sourceDirectory, NSString *destinationDirectory) { + NSString *sourceFile1 = [sourceDirectory stringByAppendingPathComponent:@"A"]; + NSString *sourceFile2 = [sourceDirectory stringByAppendingPathComponent:@"B"]; + + NSString *destinationFile1 = [destinationDirectory stringByAppendingPathComponent:@"A"]; + NSString *destinationFile2 = [destinationDirectory stringByAppendingPathComponent:@"B"]; + + NSData *data = [NSData dataWithBytes:"loltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltest" length:420]; + + XCTAssertTrue([data writeToFile:sourceFile1 atomically:YES]); + + XCTAssertTrue([data writeToFile:destinationFile1 atomically:YES]); + XCTAssertTrue([data writeToFile:destinationFile2 atomically:YES]); + + NSString *destination = @"loltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltestloltest"; + NSError *error = nil; + if (![fileManager createSymbolicLinkAtPath:sourceFile2 withDestinationPath:destination error:&error]) { + NSLog(@"Error in creating symlink: %@", error); + XCTFail(@"Failed to create symlink"); + } + + XCTAssertFalse([self testDirectoryHashEqualityWithSource:sourceDirectory destination:destinationDirectory]); + }]; +} + - (void)testRegularFileToDirectoryChange { [self createAndApplyPatchWithHandler:^(NSFileManager *fileManager, NSString *sourceDirectory, NSString *destinationDirectory) { NSString *sourceFile = [sourceDirectory stringByAppendingPathComponent:@"A"]; NSString *destinationFile = [destinationDirectory stringByAppendingPathComponent:@"A"]; - NSData *data = [NSData dataWithBytes:"loltest" length:7]; + NSData *data = [NSData dataWithBytes:"loltes" length:6]; XCTAssertTrue([data writeToFile:sourceFile atomically:YES]); NSError *error = nil; diff --git a/generate_appcast/ArchiveItem.swift b/generate_appcast/ArchiveItem.swift index 951facd4..b471a8a7 100644 --- a/generate_appcast/ArchiveItem.swift +++ b/generate_appcast/ArchiveItem.swift @@ -27,7 +27,7 @@ class DeltaUpdate { class func create(from: ArchiveItem, to: ArchiveItem, archivePath: URL) throws -> DeltaUpdate { var applyDiffError: NSError? - if !createBinaryDelta(from.appPath.path, to.appPath.path, archivePath.path, .version2, false, &applyDiffError) { + if !createBinaryDelta(from.appPath.path, to.appPath.path, archivePath.path, SUBinaryDeltaMajorVersionDefault, SPUDeltaCompressionModeDefault, DEFAULT_BZIP2_COMPRESSION_LEVEL, false, &applyDiffError) { throw applyDiffError! }