Skip to content

Commit

Permalink
Merge pull request #1018 from sparkle-project/filesystemnormalization
Browse files Browse the repository at this point in the history
fileSystemRepresentation normalization
  • Loading branch information
kornelski committed Jan 28, 2017
2 parents 0fd0331 + 67b1e08 commit 313352f
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 41 deletions.
6 changes: 3 additions & 3 deletions Sparkle/SUBinaryDeltaApply.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi
}

SUBinaryDeltaMajorVersion majorDiffVersion = FIRST_DELTA_DIFF_MAJOR_VERSION;
SUBinaryDeltaMinorVersion minorDiffVersion = FIRST_DELTA_DIFF_MINOR_VERSION;
int minorDiffVersion = 0;

NSString *expectedBeforeHashv1 = nil;
NSString *expectedAfterHashv1 = nil;
Expand All @@ -62,8 +62,8 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi
// available in version 2.0 or later
xar_subdoc_prop_get(subdoc, MINOR_DIFF_VERSION_KEY, &value);
if (value)
minorDiffVersion = (SUBinaryDeltaMinorVersion)[@(value) intValue];
minorDiffVersion = [@(value) intValue];

// available in version 2.0 or later
xar_subdoc_prop_get(subdoc, BEFORE_TREE_SHA1_KEY, &value);
if (value)
Expand Down
11 changes: 1 addition & 10 deletions Sparkle/SUBinaryDeltaCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,7 @@ typedef NS_ENUM(uint16_t, SUBinaryDeltaMajorVersion)
SUBeigeMajorVersion = 2
};

// Only keep track of the latest minor version for each major version
typedef NS_ENUM(uint16_t, SUBinaryDeltaMinorVersion)
{
SUAzureMinorVersion = 1,
SUBeigeMinorVersion = 1,
};

#define FIRST_DELTA_DIFF_MAJOR_VERSION SUAzureMajorVersion
#define FIRST_DELTA_DIFF_MINOR_VERSION ((SUBinaryDeltaMinorVersion)0)

#define LATEST_DELTA_DIFF_MAJOR_VERSION SUBeigeMajorVersion

extern int compareFiles(const FTSENT **a, const FTSENT **b);
Expand All @@ -80,5 +71,5 @@ extern NSString *pathRelativeToDirectory(NSString *directory, NSString *path);
NSString *temporaryFilename(NSString *base);
NSString *temporaryDirectory(NSString *base);
NSString *stringWithFileSystemRepresentation(const char*);
SUBinaryDeltaMinorVersion latestMinorVersionForMajorVersion(SUBinaryDeltaMajorVersion majorVersion);
int latestMinorVersionForMajorVersion(SUBinaryDeltaMajorVersion majorVersion);
#endif
15 changes: 9 additions & 6 deletions Sparkle/SUBinaryDeltaCommon.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ int compareFiles(const FTSENT **a, const FTSENT **b)
return [[NSFileManager defaultManager] stringWithFileSystemRepresentation:input length:strlen(input)];
}

SUBinaryDeltaMinorVersion latestMinorVersionForMajorVersion(SUBinaryDeltaMajorVersion majorVersion)
int latestMinorVersionForMajorVersion(SUBinaryDeltaMajorVersion majorVersion)
{
switch (majorVersion) {
case SUAzureMajorVersion:
return SUAzureMinorVersion;
return 1;
case SUBeigeMajorVersion:
return SUBeigeMinorVersion;
return 2;
}
return (SUBinaryDeltaMinorVersion)0;
return 0;
}

NSString *temporaryFilename(NSString *base)
Expand Down Expand Up @@ -163,6 +163,9 @@ static BOOL _hashOfFileContents(unsigned char* hash, FTSENT *ent)
CC_SHA1_CTX hashContext;
CC_SHA1_Init(&hashContext);

// Ensure the path uses filesystem-specific Unicode normalization #1017
NSString *normalizedPath = stringWithFileSystemRepresentation(pathBuffer);

FTSENT *ent = 0;
while ((ent = fts_read(fts))) {
if (ent->fts_info != FTS_F && ent->fts_info != FTS_SL && ent->fts_info != FTS_D)
Expand All @@ -171,8 +174,8 @@ static BOOL _hashOfFileContents(unsigned char* hash, FTSENT *ent)
if (ent->fts_info == FTS_D && !MAJOR_VERSION_IS_AT_LEAST(majorVersion, SUBeigeMajorVersion)) {
continue;
}
NSString *relativePath = pathRelativeToDirectory(path, stringWithFileSystemRepresentation(ent->fts_path));

NSString *relativePath = pathRelativeToDirectory(normalizedPath, stringWithFileSystemRepresentation(ent->fts_path));
if (relativePath.length == 0)
continue;

Expand Down
6 changes: 3 additions & 3 deletions Sparkle/SUBinaryDeltaCreate.m
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ BOOL createBinaryDelta(NSString *source, NSString *destination, NSString *patchF
assert(destination);
assert(patchFile);
assert(majorVersion >= FIRST_DELTA_DIFF_MAJOR_VERSION && majorVersion <= LATEST_DELTA_DIFF_MAJOR_VERSION);
SUBinaryDeltaMinorVersion minorVersion = latestMinorVersionForMajorVersion(majorVersion);

int minorVersion = latestMinorVersionForMajorVersion(majorVersion);

NSMutableDictionary *originalTreeState = [NSMutableDictionary dictionary];

char pathBuffer[PATH_MAX] = {0};
Expand Down
8 changes: 4 additions & 4 deletions Sparkle/SUBinaryDeltaTool.m
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ static BOOL runVersionCommand(NSString *programName, NSArray *args)
fprintf(stderr, "Unable to open patch %s\n", [patchFile fileSystemRepresentation]);
return NO;
}

SUBinaryDeltaMajorVersion majorDiffVersion = FIRST_DELTA_DIFF_MAJOR_VERSION;
SUBinaryDeltaMinorVersion minorDiffVersion = FIRST_DELTA_DIFF_MINOR_VERSION;
int minorDiffVersion = 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)) {
Expand All @@ -205,7 +205,7 @@ static BOOL runVersionCommand(NSString *programName, NSArray *args)
// available in version 2.0 or later
xar_subdoc_prop_get(subdoc, MINOR_DIFF_VERSION_KEY, &value);
if (value)
minorDiffVersion = (SUBinaryDeltaMinorVersion)[@(value) intValue];
minorDiffVersion = [@(value) intValue];
}
}

Expand Down
6 changes: 4 additions & 2 deletions Sparkle/SUInstaller.m
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,10 @@ + (NSString *)normalizedInstallationPathForHost:(SUHost *)host
assert(bundle != nil);

NSString *normalizedAppPath = [[[bundle bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", [host objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleNameKey], [[bundle bundlePath] pathExtension]]];

return normalizedAppPath;

// Roundtrip string through fileSystemRepresentation to ensure it uses filesystem's Unicode normalization
// rather than arbitrary Unicode form from Info.plist - #1017
return [NSString stringWithUTF8String:[normalizedAppPath fileSystemRepresentation]];
}

@end
26 changes: 13 additions & 13 deletions Tests/SUBinaryDeltaTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,30 @@ @implementation SUBinaryDeltaTest

- (void)testTemporaryDirectory
{
NSString *tmp1 = temporaryDirectory(@"Sparkle");
NSString *tmp2 = temporaryDirectory(@"Sparkle");
NSString *tmp1 = temporaryDirectory(@"Sparklęエンジン");
NSString *tmp2 = temporaryDirectory(@"Sparklęエンジン");
NSLog(@"Temporary directories: %@, %@", tmp1, tmp2);
XCTAssertNotEqualObjects(tmp1, tmp2);
XCTAssert(YES, @"Pass");
}

- (void)testTemporaryFile
{
NSString *tmp1 = temporaryFilename(@"Sparkle");
NSString *tmp2 = temporaryFilename(@"Sparkle");
NSString *tmp1 = temporaryFilename(@"Sparklęエンジン");
NSString *tmp2 = temporaryFilename(@"Sparklęエンジン");
NSLog(@"Temporary files: %@, %@", tmp1, tmp2);
XCTAssertNotEqualObjects(tmp1, tmp2);
XCTAssert(YES, @"Pass");
}

- (BOOL)createAndApplyPatchUsingVersion:(SUBinaryDeltaMajorVersion)majorVersion beforeDiffHandler:(SUDeltaHandler)beforeDiffHandler afterDiffHandler:(SUDeltaHandler)afterDiffHandler
{
NSString *sourceDirectory = temporaryDirectory(@"Sparkle_temp1");
NSString *destinationDirectory = temporaryDirectory(@"Sparkle_temp2");
NSString *diffFile = temporaryFilename(@"Sparkle_diff");
NSString *patchDirectory = temporaryDirectory(@"Sparkle_patch");
NSString *sourceDirectory = temporaryDirectory(@"Spąrkle_temp1エンジン");
NSString *destinationDirectory = temporaryDirectory(@"Spąrkle_temp2エンジン");

NSString *diffFile = temporaryFilename(@"Spąrkle_diffエンジン");
NSString *patchDirectory = temporaryDirectory(@"Spąrkle_patchエンジン");

XCTAssertNotNil(sourceDirectory);
XCTAssertNotNil(destinationDirectory);
XCTAssertNotNil(diffFile);
Expand Down Expand Up @@ -120,9 +120,9 @@ - (void)testEmptyDataDiff
{
[self createAndApplyPatchWithHandler:^(NSFileManager *__unused fileManager, NSString *sourceDirectory, NSString *destinationDirectory) {
NSData *emptyData = [NSData data];
NSString *sourceFile = [sourceDirectory stringByAppendingPathComponent:@"A"];
NSString *destinationFile = [destinationDirectory stringByAppendingPathComponent:@"A"];
NSString *sourceFile = [sourceDirectory stringByAppendingPathComponent:@"AĄエンジン"];
NSString *destinationFile = [destinationDirectory stringByAppendingPathComponent:@"AĄエンジン"];

XCTAssertTrue([emptyData writeToFile:sourceFile atomically:YES]);
XCTAssertTrue([emptyData writeToFile:destinationFile atomically:YES]);

Expand Down

0 comments on commit 313352f

Please sign in to comment.