Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Jan 28, 2017
1 parent 313352f commit 155816b
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 140 deletions.
55 changes: 26 additions & 29 deletions Sparkle/SUBinaryDeltaApply.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@
#include <stdlib.h>
#include <xar/xar.h>


#include "AppKitPrevention.h"

static BOOL applyBinaryDeltaToFile(xar_t x, xar_file_t file, NSString *sourceFilePath, NSString *destinationFilePath)
{
NSString *patchFile = temporaryFilename(@"apply-binary-delta");
xar_extract_tofile(x, file, [patchFile fileSystemRepresentation]);
const char *argv[] = {"/usr/bin/bspatch", [sourceFilePath fileSystemRepresentation], [destinationFilePath fileSystemRepresentation], [patchFile fileSystemRepresentation]};
const char *argv[] = { "/usr/bin/bspatch", [sourceFilePath fileSystemRepresentation], [destinationFilePath fileSystemRepresentation], [patchFile fileSystemRepresentation] };
BOOL success = (bspatch(4, argv) == 0);
unlink([patchFile fileSystemRepresentation]);
return success;
}

BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFile, BOOL verbose, void (^progressCallback)(double progress), NSError * __autoreleasing *error)
BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFile, BOOL verbose, void (^progressCallback)(double progress), NSError *__autoreleasing *error)
{
xar_t x = xar_open([patchFile fileSystemRepresentation], READ);
if (!x) {
Expand All @@ -37,28 +36,28 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi
}
return NO;
}

SUBinaryDeltaMajorVersion majorDiffVersion = FIRST_DELTA_DIFF_MAJOR_VERSION;
int minorDiffVersion = 0;

NSString *expectedBeforeHashv1 = nil;
NSString *expectedAfterHashv1 = nil;

NSString *expectedNewBeforeHash = nil;
NSString *expectedNewAfterHash = nil;

progressCallback(0/6.0);

xar_subdoc_t subdoc;
for (subdoc = xar_subdoc_first(x); subdoc; subdoc = xar_subdoc_next(subdoc)) {
if (!strcmp(xar_subdoc_name(subdoc), BINARY_DELTA_ATTRIBUTES_KEY)) {
const char *value = 0;

// available in version 2.0 or later
xar_subdoc_prop_get(subdoc, MAJOR_DIFF_VERSION_KEY, &value);
if (value)
majorDiffVersion = (SUBinaryDeltaMajorVersion)[@(value) intValue];

// available in version 2.0 or later
xar_subdoc_prop_get(subdoc, MINOR_DIFF_VERSION_KEY, &value);
if (value)
Expand All @@ -68,40 +67,40 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi
xar_subdoc_prop_get(subdoc, BEFORE_TREE_SHA1_KEY, &value);
if (value)
expectedNewBeforeHash = @(value);

// available in version 2.0 or later
xar_subdoc_prop_get(subdoc, AFTER_TREE_SHA1_KEY, &value);
if (value)
expectedNewAfterHash = @(value);

// only available in version 1.0
xar_subdoc_prop_get(subdoc, BEFORE_TREE_SHA1_OLD_KEY, &value);
if (value)
expectedBeforeHashv1 = @(value);

// only available in version 1.0
xar_subdoc_prop_get(subdoc, AFTER_TREE_SHA1_OLD_KEY, &value);
if (value)
expectedAfterHashv1 = @(value);
}
}

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] }];
}
return NO;
}

if (majorDiffVersion > LATEST_DELTA_DIFF_MAJOR_VERSION) {
if (error != NULL) {
*error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadUnknownError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"A later version is needed to apply this patch (on major version %u, but patch requests version %u).", LATEST_DELTA_DIFF_MAJOR_VERSION, majorDiffVersion] }];
}
return NO;
}

BOOL usesNewTreeHash = MAJOR_VERSION_IS_AT_LEAST(majorDiffVersion, SUBeigeMajorVersion);

NSString *expectedBeforeHash = usesNewTreeHash ? expectedNewBeforeHash : expectedBeforeHashv1;
NSString *expectedAfterHash = usesNewTreeHash ? expectedNewAfterHash : expectedAfterHashv1;

Expand All @@ -116,7 +115,7 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi
fprintf(stderr, "Applying version %u.%u patch...\n", majorDiffVersion, minorDiffVersion);
fprintf(stderr, "Verifying source...");
}

progressCallback(1/6.0);

NSString *beforeHash = hashOfTreeWithVersion(source, majorDiffVersion);
Expand All @@ -143,7 +142,7 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi
if (verbose) {
fprintf(stderr, "\nCopying files...");
}

progressCallback(2/6.0);

if (!removeTree(destination)) {
Expand All @@ -167,7 +166,7 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi
}
return NO;
}

progressCallback(4/6.0);

BOOL hasExtractKeyAvailable = MAJOR_VERSION_IS_AT_LEAST(majorDiffVersion, SUBeigeMajorVersion);
Expand All @@ -186,10 +185,9 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi
// Don't use -[NSFileManager fileExistsAtPath:] because it will follow symbolic links
BOOL fileExisted = verbose && [fileManager attributesOfItemAtPath:destinationFilePath error:nil];
BOOL removedFile = NO;

const char *value;
if (!xar_prop_get(file, DELETE_KEY, &value) ||
(!hasExtractKeyAvailable && !xar_prop_get(file, DELETE_THEN_EXTRACT_OLD_KEY, &value))) {
if (!xar_prop_get(file, DELETE_KEY, &value) || (!hasExtractKeyAvailable && !xar_prop_get(file, DELETE_THEN_EXTRACT_OLD_KEY, &value))) {
if (!removeTree(destinationFilePath)) {
if (verbose) {
fprintf(stderr, "\n");
Expand All @@ -205,7 +203,7 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi
}
continue;
}

removedFile = YES;
}

Expand All @@ -219,13 +217,12 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi
}
return NO;
}

if (verbose) {
fprintf(stderr, "\n🔨 %s %s", VERBOSE_PATCHED, [path fileSystemRepresentation]);
}
} else if ((hasExtractKeyAvailable && !xar_prop_get(file, EXTRACT_KEY, &value)) ||
(!hasExtractKeyAvailable && xar_prop_get(file, MODIFY_PERMISSIONS_KEY, &value))) { // extract and permission modifications don't coexist

} else if ((hasExtractKeyAvailable && !xar_prop_get(file, EXTRACT_KEY, &value)) || (!hasExtractKeyAvailable && xar_prop_get(file, MODIFY_PERMISSIONS_KEY, &value))) { // extract and permission modifications don't coexist

if (xar_extract_tofile(x, file, [destinationFilePath fileSystemRepresentation]) != 0) {
if (verbose) {
fprintf(stderr, "\n");
Expand All @@ -235,7 +232,7 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi
}
return NO;
}

if (verbose) {
if (fileExisted) {
fprintf(stderr, "\n✏️ %s %s", VERBOSE_UPDATED, [path fileSystemRepresentation]);
Expand All @@ -246,7 +243,7 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi
} else if (verbose && removedFile) {
fprintf(stderr, "\n%s %s", VERBOSE_DELETED, [path fileSystemRepresentation]);
}

if (!xar_prop_get(file, MODIFY_PERMISSIONS_KEY, &value)) {
mode_t mode = (mode_t)[[NSString stringWithUTF8String:value] intValue];
if (!modifyPermissions(destinationFilePath, mode)) {
Expand All @@ -258,7 +255,7 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi
}
return NO;
}

if (verbose) {
fprintf(stderr, "\n👮 %s %s (0%o)", VERBOSE_MODIFIED, [path fileSystemRepresentation], mode);
}
Expand Down
26 changes: 13 additions & 13 deletions Sparkle/SUBinaryDeltaCommon.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <sys/stat.h>
#include <xar/xar.h>


#include "AppKitPrevention.h"

int compareFiles(const FTSENT **a, const FTSENT **b)
Expand All @@ -35,7 +34,8 @@ int compareFiles(const FTSENT **a, const FTSENT **b)
return path;
}

NSString *stringWithFileSystemRepresentation(const char *input) {
NSString *stringWithFileSystemRepresentation(const char *input)
{
return [[NSFileManager defaultManager] stringWithFileSystemRepresentation:input length:strlen(input)];
}

Expand Down Expand Up @@ -76,18 +76,18 @@ int latestMinorVersionForMajorVersion(SUBinaryDeltaMajorVersion majorVersion)
NSString *template = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.XXXXXXXXXX", base]];
NSMutableData *data = [NSMutableData data];
[data appendBytes:template.fileSystemRepresentation length:strlen(template.fileSystemRepresentation) + 1];

char *buffer = data.mutableBytes;
char *templateResult = mkdtemp(buffer);
if (templateResult == NULL) {
perror("mkdtemp");
return nil;
}

return stringWithFileSystemRepresentation(templateResult);
}

static void _hashOfBuffer(unsigned char *hash, const char* buffer, ssize_t bufferLength)
static void _hashOfBuffer(unsigned char *hash, const char *buffer, ssize_t bufferLength)
{
assert(bufferLength >= 0 && bufferLength <= UINT32_MAX);
CC_SHA1_CTX hashContext;
Expand All @@ -96,7 +96,7 @@ static void _hashOfBuffer(unsigned char *hash, const char* buffer, ssize_t buffe
CC_SHA1_Final(hash, &hashContext);
}

static BOOL _hashOfFileContents(unsigned char* hash, FTSENT *ent)
static BOOL _hashOfFileContents(unsigned char *hash, FTSENT *ent)
{
if (ent->fts_info == FTS_SL) {
char linkDestination[MAXPATHLEN + 1];
Expand All @@ -119,12 +119,12 @@ static BOOL _hashOfFileContents(unsigned char* hash, FTSENT *ent)
_hashOfBuffer(hash, NULL, 0);
} else {
void *buffer = mmap(0, (size_t)fileSize, PROT_READ, MAP_FILE | MAP_PRIVATE, fileDescriptor, 0);
if (buffer == (void*)-1) {
if (buffer == (void *)-1) {
close(fileDescriptor);
perror("mmap");
return NO;
}

_hashOfBuffer(hash, buffer, fileSize);
munmap(buffer, (size_t)fileSize);
}
Expand All @@ -148,12 +148,12 @@ static BOOL _hashOfFileContents(unsigned char* hash, FTSENT *ent)

NSString *hashOfTreeWithVersion(NSString *path, uint16_t majorVersion)
{
char pathBuffer[PATH_MAX] = {0};
char pathBuffer[PATH_MAX] = { 0 };
if (![path getFileSystemRepresentation:pathBuffer maxLength:sizeof(pathBuffer)]) {
return nil;
}

char * const sourcePaths[] = {pathBuffer, 0};
char *const sourcePaths[] = { pathBuffer, 0 };
FTS *fts = fts_open(sourcePaths, FTS_PHYSICAL | FTS_NOCHDIR, compareFiles);
if (!fts) {
perror("fts_open");
Expand All @@ -170,7 +170,7 @@ static BOOL _hashOfFileContents(unsigned char* hash, FTSENT *ent)
while ((ent = fts_read(fts))) {
if (ent->fts_info != FTS_F && ent->fts_info != FTS_SL && ent->fts_info != FTS_D)
continue;

if (ent->fts_info == FTS_D && !MAJOR_VERSION_IS_AT_LEAST(majorVersion, SUBeigeMajorVersion)) {
continue;
}
Expand All @@ -187,12 +187,12 @@ static BOOL _hashOfFileContents(unsigned char* hash, FTSENT *ent)

const char *relativePathBytes = [relativePath fileSystemRepresentation];
CC_SHA1_Update(&hashContext, relativePathBytes, (CC_LONG)strlen(relativePathBytes));

if (MAJOR_VERSION_IS_AT_LEAST(majorVersion, SUBeigeMajorVersion)) {
uint16_t mode = ent->fts_statp->st_mode;
uint16_t type = ent->fts_info;
uint16_t permissions = mode & PERMISSION_FLAGS;

CC_SHA1_Update(&hashContext, &type, sizeof(type));
CC_SHA1_Update(&hashContext, &permissions, sizeof(permissions));
}
Expand Down
Loading

0 comments on commit 155816b

Please sign in to comment.