From 3c78139ec85f2be3305bc04b6e93eeea70af37c0 Mon Sep 17 00:00:00 2001 From: Zorg Date: Fri, 17 Apr 2015 17:50:21 -0400 Subject: [PATCH] Use enums for major/minor versions instead of #defines This should be more typesafe --- Sparkle/SUBinaryDeltaApply.m | 10 +++++----- Sparkle/SUBinaryDeltaCommon.h | 24 +++++++++++++++--------- Sparkle/SUBinaryDeltaCommon.m | 15 +++++++++++++-- Sparkle/SUBinaryDeltaCreate.h | 4 +++- Sparkle/SUBinaryDeltaCreate.m | 12 ++++++------ Sparkle/SUBinaryDeltaTool.m | 2 +- 6 files changed, 43 insertions(+), 24 deletions(-) diff --git a/Sparkle/SUBinaryDeltaApply.m b/Sparkle/SUBinaryDeltaApply.m index 9afd14ca..d318464b 100644 --- a/Sparkle/SUBinaryDeltaApply.m +++ b/Sparkle/SUBinaryDeltaApply.m @@ -33,8 +33,8 @@ int applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFil return 1; } - uint16_t majorDiffVersion = FIRST_DELTA_DIFF_MAJOR_VERSION; - uint16_t minorDiffVersion = FIRST_DELTA_DIFF_MINOR_VERSION; + SUBinaryDeltaMajorVersion majorDiffVersion = FIRST_DELTA_DIFF_MAJOR_VERSION; + SUBinaryDeltaMinorVersion minorDiffVersion = FIRST_DELTA_DIFF_MINOR_VERSION; NSString *expectedBeforeHashv1 = nil; NSString *expectedAfterHashv1 = nil; @@ -50,12 +50,12 @@ int applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFil // available in version 2.0 or later xar_subdoc_prop_get(subdoc, MAJOR_DIFF_VERSION_KEY, &value); if (value) - majorDiffVersion = (uint16_t)[@(value) intValue]; + majorDiffVersion = (SUBinaryDeltaMajorVersion)[@(value) intValue]; // available in version 2.0 or later xar_subdoc_prop_get(subdoc, MINOR_DIFF_VERSION_KEY, &value); if (value) - minorDiffVersion = (uint16_t)[@(value) intValue]; + minorDiffVersion = (SUBinaryDeltaMinorVersion)[@(value) intValue]; // available in version 2.0 or later xar_subdoc_prop_get(subdoc, BEFORE_TREE_SHA1_KEY, &value); @@ -89,7 +89,7 @@ int applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFil return 1; } - BOOL usesNewTreeHash = MAJOR_VERSION_IS_AT_LEAST(majorDiffVersion, BEIGE_MAJOR_VERSION); + BOOL usesNewTreeHash = MAJOR_VERSION_IS_AT_LEAST(majorDiffVersion, SUBeigeMajorVersion); NSString *expectedBeforeHash = usesNewTreeHash ? expectedNewBeforeHash : expectedBeforeHashv1; NSString *expectedAfterHash = usesNewTreeHash ? expectedNewAfterHash : expectedAfterHashv1; diff --git a/Sparkle/SUBinaryDeltaCommon.h b/Sparkle/SUBinaryDeltaCommon.h index 61338ab9..a2c21a96 100644 --- a/Sparkle/SUBinaryDeltaCommon.h +++ b/Sparkle/SUBinaryDeltaCommon.h @@ -31,23 +31,28 @@ #define AFTER_TREE_SHA1_OLD_KEY "before-sha1" #define MAJOR_VERSION_IS_AT_LEAST(actualMajor, expectedMajor) (actualMajor >= expectedMajor) -#define LATEST_MINOR_VERSION_FOR_MAJOR_VERSION(major) \ - (major == AZURE_MAJOR_VERSION ? AZURE_MINOR_VERSION : BEIGE_MINOR_VERSION) // 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 -#define AZURE_MAJOR_VERSION 1 -#define AZURE_MINOR_VERSION 0 +typedef NS_ENUM(uint16_t, SUBinaryDeltaMajorVersion) +{ + SUAzureMajorVersion = 1, + SUBeigeMajorVersion = 2 +}; -#define BEIGE_MAJOR_VERSION 2 -#define BEIGE_MINOR_VERSION 0 +// Only keep track of the latest minor version for each major version +typedef NS_ENUM(uint16_t, SUBinaryDeltaMinorVersion) +{ + SUAzureMinorVersion = 0, + SUBeigeMinorVersion = 0, +}; -#define FIRST_DELTA_DIFF_MAJOR_VERSION 1 -#define FIRST_DELTA_DIFF_MINOR_VERSION 0 +#define FIRST_DELTA_DIFF_MAJOR_VERSION SUAzureMajorVersion +#define FIRST_DELTA_DIFF_MINOR_VERSION SUAzureMinorVersion -#define LATEST_DELTA_DIFF_MAJOR_VERSION BEIGE_MAJOR_VERSION +#define LATEST_DELTA_DIFF_MAJOR_VERSION SUBeigeMajorVersion @class NSString; @class NSData; @@ -63,4 +68,5 @@ extern NSString *pathRelativeToDirectory(NSString *directory, NSString *path); NSString *temporaryFilename(NSString *base); NSString *temporaryDirectory(NSString *base); NSString *stringWithFileSystemRepresentation(const char*); +SUBinaryDeltaMinorVersion latestMinorVersionForMajorVersion(SUBinaryDeltaMajorVersion majorVersion); #endif diff --git a/Sparkle/SUBinaryDeltaCommon.m b/Sparkle/SUBinaryDeltaCommon.m index 30560368..9bde20c3 100644 --- a/Sparkle/SUBinaryDeltaCommon.m +++ b/Sparkle/SUBinaryDeltaCommon.m @@ -35,6 +35,17 @@ NSString *stringWithFileSystemRepresentation(const char *input) { return [[NSFileManager defaultManager] stringWithFileSystemRepresentation:input length:strlen(input)]; } +SUBinaryDeltaMinorVersion latestMinorVersionForMajorVersion(SUBinaryDeltaMajorVersion majorVersion) +{ + switch (majorVersion) { + case SUAzureMajorVersion: + return SUAzureMinorVersion; + case SUBeigeMajorVersion: + return SUBeigeMinorVersion; + } + return 0; +} + NSString *temporaryFilename(NSString *base) { NSString *template = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.XXXXXXXXXX", base]]; @@ -148,7 +159,7 @@ NSString *hashOfTreeWithVersion(NSString *path, uint16_t majorVersion) 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, BEIGE_MAJOR_VERSION)) { + if (ent->fts_info == FTS_D && !MAJOR_VERSION_IS_AT_LEAST(majorVersion, SUBeigeMajorVersion)) { continue; } @@ -165,7 +176,7 @@ NSString *hashOfTreeWithVersion(NSString *path, uint16_t majorVersion) const char *relativePathBytes = [relativePath fileSystemRepresentation]; CC_SHA1_Update(&hashContext, relativePathBytes, (CC_LONG)strlen(relativePathBytes)); - if (MAJOR_VERSION_IS_AT_LEAST(majorVersion, BEIGE_MAJOR_VERSION)) { + 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; diff --git a/Sparkle/SUBinaryDeltaCreate.h b/Sparkle/SUBinaryDeltaCreate.h index d9a00ce2..c608fdac 100644 --- a/Sparkle/SUBinaryDeltaCreate.h +++ b/Sparkle/SUBinaryDeltaCreate.h @@ -9,7 +9,9 @@ #ifndef SUBINARYDELTACREATE_H #define SUBINARYDELTACREATE_H +#import "SUBinaryDeltaCommon.h" + @class NSString; -int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFile, uint16_t majorVersion); +int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFile, SUBinaryDeltaMajorVersion majorVersion); #endif diff --git a/Sparkle/SUBinaryDeltaCreate.m b/Sparkle/SUBinaryDeltaCreate.m index 4a3d7472..17de119a 100644 --- a/Sparkle/SUBinaryDeltaCreate.m +++ b/Sparkle/SUBinaryDeltaCreate.m @@ -184,7 +184,7 @@ static BOOL shouldChangePermissions(NSDictionary *originalInfo, NSDictionary *ne return YES; } -int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFile, uint16_t majorVersion) +int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFile, SUBinaryDeltaMajorVersion majorVersion) { if (majorVersion < FIRST_DELTA_DIFF_MAJOR_VERSION) { fprintf(stderr, "Version provided (%u) is not valid", majorVersion); @@ -196,7 +196,7 @@ int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFi return 1; } - uint16_t minorVersion = LATEST_MINOR_VERSION_FOR_MAJOR_VERSION(majorVersion); + SUBinaryDeltaMinorVersion minorVersion = latestMinorVersionForMajorVersion(majorVersion); NSMutableDictionary *originalTreeState = [NSMutableDictionary dictionary]; @@ -326,9 +326,9 @@ int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFi // Version 1 patches don't have a major or minor version field, so we need to differentiate between the hash keys const char *beforeHashKey = - MAJOR_VERSION_IS_AT_LEAST(majorVersion, BEIGE_MAJOR_VERSION) ? BEFORE_TREE_SHA1_KEY : BEFORE_TREE_SHA1_OLD_KEY; + MAJOR_VERSION_IS_AT_LEAST(majorVersion, SUBeigeMajorVersion) ? BEFORE_TREE_SHA1_KEY : BEFORE_TREE_SHA1_OLD_KEY; const char *afterHashKey = - MAJOR_VERSION_IS_AT_LEAST(majorVersion, BEIGE_MAJOR_VERSION) ? AFTER_TREE_SHA1_KEY : AFTER_TREE_SHA1_OLD_KEY; + MAJOR_VERSION_IS_AT_LEAST(majorVersion, SUBeigeMajorVersion) ? AFTER_TREE_SHA1_KEY : AFTER_TREE_SHA1_OLD_KEY; xar_subdoc_prop_set(attributes, beforeHashKey, [beforeHash UTF8String]); xar_subdoc_prop_set(attributes, afterHashKey, [afterHash UTF8String]); @@ -359,7 +359,7 @@ int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFi NSDictionary *originalInfo = originalTreeState[key]; NSDictionary *newInfo = newTreeState[key]; if (shouldSkipDeltaCompression(originalInfo, newInfo)) { - if (MAJOR_VERSION_IS_AT_LEAST(majorVersion, BEIGE_MAJOR_VERSION) && shouldSkipExtracting(originalInfo, newInfo)) { + if (MAJOR_VERSION_IS_AT_LEAST(majorVersion, SUBeigeMajorVersion) && shouldSkipExtracting(originalInfo, newInfo)) { if (shouldChangePermissions(originalInfo, newInfo)) { xar_file_t newFile = xar_add_frombuffer(x, 0, [key fileSystemRepresentation], (char *)"", 1); assert(newFile); @@ -376,7 +376,7 @@ int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFi } } else { NSNumber *permissions = - (MAJOR_VERSION_IS_AT_LEAST(majorVersion, BEIGE_MAJOR_VERSION) && shouldChangePermissions(originalInfo, newInfo)) ? + (MAJOR_VERSION_IS_AT_LEAST(majorVersion, SUBeigeMajorVersion) && shouldChangePermissions(originalInfo, newInfo)) ? newInfo[INFO_PERMISSIONS_KEY] : nil; CreateBinaryDeltaOperation *operation = [[CreateBinaryDeltaOperation alloc] initWithRelativePath:key oldTree:source newTree:destination permissions:permissions]; diff --git a/Sparkle/SUBinaryDeltaTool.m b/Sparkle/SUBinaryDeltaTool.m index 63c2f6e4..fc09d50c 100644 --- a/Sparkle/SUBinaryDeltaTool.m +++ b/Sparkle/SUBinaryDeltaTool.m @@ -34,7 +34,7 @@ int main(int __unused argc, char __unused *argv[]) return 1; } - uint16_t patchVersion = 0; + SUBinaryDeltaMajorVersion patchVersion; NSString *versionField = ([command isEqualToString:@"create"] && args.count == 6) ? args[2] : nil; if (!versionField) { patchVersion = LATEST_DELTA_DIFF_MAJOR_VERSION;