Reworked diffing file permissions

My initial attempt did not work correctly for directories that differed in permissions (since it removed them and all the files inside them). We are now setting a property for permission changes which can happen when a binary diff is created, or when it is skipped. Still have to figure out how backwards compatibility is going to work now..
This commit is contained in:
Zorg committed 2015-04-12 23:21:40 -04:00
1 parent d374d99e51
commit 54522aa725
4 files changed
+69 -14

No files matched your search

+8 -1
View File
@@ -141,12 +141,19 @@ int applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFil
fprintf(stderr, "Unable to patch %s to destination %s\n", [sourceFilePath fileSystemRepresentation], [destinationFilePath fileSystemRepresentation]);
return 1;
}
} else {
} else if (xar_prop_get(file, "mod-permissions", &value)) {
if (xar_extract_tofile(x, file, [destinationFilePath fileSystemRepresentation]) != 0) {
fprintf(stderr, "Unable to extract file to %s\n", [destinationFilePath fileSystemRepresentation]);
return 1;
}
}
if (!xar_prop_get(file, "mod-permissions", &value)) {
if (!modifyPermissions(destinationFilePath, (mode_t)[[NSString stringWithUTF8String:value] intValue])) {
fprintf(stderr, "Unable to modify permissions (%s) on file %s\n", value, [destinationFilePath fileSystemRepresentation]);
return 1;
}
}
}
xar_close(x);
+1
View File
@@ -41,6 +41,7 @@ extern NSString *hashOfTreeWithVersion(NSString *path, uint16_t majorVersion, ui
extern NSString *hashOfTree(NSString *path);
extern BOOL removeTree(NSString *path);
extern BOOL copyTree(NSString *source, NSString *dest);
extern BOOL modifyPermissions(NSString *path, mode_t desiredPermissions);
extern NSString *pathRelativeToDirectory(NSString *directory, NSString *path);
NSString *temporaryFilename(NSString *base);
NSString *temporaryDirectory(NSString *base);
+18
View File
@@ -200,3 +200,21 @@ BOOL copyTree(NSString *source, NSString *dest)
{
return [[NSFileManager defaultManager] copyItemAtPath:source toPath:dest error:nil];
}
BOOL modifyPermissions(NSString *path, mode_t desiredPermissions)
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *attributes = [fileManager attributesOfItemAtPath:path error:nil];
if (!attributes) {
return NO;
}
NSNumber *permissions = attributes[NSFilePosixPermissions];
if (!permissions) {
return NO;
}
mode_t newMode = ([permissions unsignedShortValue] & ~PERMISSION_FLAGS) | desiredPermissions;
if (![fileManager setAttributes:@{NSFilePosixPermissions : @(newMode)} ofItemAtPath:path error:nil]) {
return NO;
}
return YES;
}
+42 -13
View File
@@ -25,21 +25,24 @@ extern int bsdiff(int argc, const char **argv);
@interface CreateBinaryDeltaOperation : NSOperation
@property (copy) NSString *relativePath;
@property (strong) NSString *resultPath;
@property (strong) NSNumber *permissions;
@property (strong) NSString *_fromPath;
@property (strong) NSString *_toPath;
- (id)initWithRelativePath:(NSString *)relativePath oldTree:(NSString *)oldTree newTree:(NSString *)newTree;
- (id)initWithRelativePath:(NSString *)relativePath oldTree:(NSString *)oldTree newTree:(NSString *)newTree permissions:(NSNumber *)permissions;
@end
@implementation CreateBinaryDeltaOperation
@synthesize relativePath = _relativePath;
@synthesize resultPath = _resultPath;
@synthesize permissions = _permissions;
@synthesize _fromPath = _fromPath;
@synthesize _toPath = _toPath;
- (id)initWithRelativePath:(NSString *)relativePath oldTree:(NSString *)oldTree newTree:(NSString *)newTree
- (id)initWithRelativePath:(NSString *)relativePath oldTree:(NSString *)oldTree newTree:(NSString *)newTree permissions:(NSNumber *)permissions
{
if ((self = [super init])) {
self.relativePath = relativePath;
self.permissions = permissions;
self._fromPath = [oldTree stringByAppendingPathComponent:relativePath];
self._toPath = [newTree stringByAppendingPathComponent:relativePath];
}
@@ -109,7 +112,8 @@ static BOOL shouldSkipDeltaCompression(NSDictionary* originalInfo, NSDictionary
return YES;
}
if ([originalInfo[INFO_PERMISSIONS_KEY] unsignedShortValue] != [newInfo[INFO_PERMISSIONS_KEY] unsignedShortValue]) {
if ([originalInfo[INFO_HASH_KEY] isEqual:newInfo[INFO_HASH_KEY]]) {
// this is possible if just the permissions have changed
return YES;
}
@@ -125,14 +129,27 @@ static BOOL shouldDeleteThenExtract(NSDictionary* originalInfo, NSDictionary *ne
if ([originalInfo[INFO_TYPE_KEY] unsignedShortValue] != [newInfo[INFO_TYPE_KEY] unsignedShortValue]) {
return YES;
}
if ([originalInfo[INFO_PERMISSIONS_KEY] unsignedShortValue] != [newInfo[INFO_PERMISSIONS_KEY] unsignedShortValue]) {
return YES;
}
return NO;
}
static BOOL shouldChangePermissions(NSDictionary *originalInfo, NSDictionary *newInfo)
{
if (!originalInfo) {
return NO;
}
if ([originalInfo[INFO_TYPE_KEY] unsignedShortValue] != [newInfo[INFO_TYPE_KEY] unsignedShortValue]) {
return NO;
}
if ([originalInfo[INFO_PERMISSIONS_KEY] unsignedShortValue] == [newInfo[INFO_PERMISSIONS_KEY] unsignedShortValue]) {
return NO;
}
return YES;
}
int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFile)
{
NSMutableDictionary *originalTreeState = [NSMutableDictionary dictionary];
@@ -293,14 +310,22 @@ int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFi
NSDictionary *originalInfo = originalTreeState[key];
NSDictionary *newInfo = newTreeState[key];
if (shouldSkipDeltaCompression(originalInfo, newInfo)) {
NSString *path = [destination stringByAppendingPathComponent:key];
xar_file_t newFile = xar_add_frompath(x, 0, [key fileSystemRepresentation], [path fileSystemRepresentation]);
assert(newFile);
if (shouldDeleteThenExtract(originalInfo, newInfo)) {
xar_prop_set(newFile, "delete-then-extract", "true");
if (shouldChangePermissions(originalInfo, newInfo)) {
xar_file_t newFile = xar_add_frombuffer(x, 0, [key fileSystemRepresentation], (char *)"", 1);
assert(newFile);
xar_prop_set(newFile, "mod-permissions", [[NSString stringWithFormat:@"%u", [newInfo[INFO_PERMISSIONS_KEY] unsignedShortValue]] UTF8String]);
} else {
NSString *path = [destination stringByAppendingPathComponent:key];
xar_file_t newFile = xar_add_frompath(x, 0, [key fileSystemRepresentation], [path fileSystemRepresentation]);
assert(newFile);
if (shouldDeleteThenExtract(originalInfo, newInfo)) {
xar_prop_set(newFile, "delete-then-extract", "true");
}
}
} else {
CreateBinaryDeltaOperation *operation = [[CreateBinaryDeltaOperation alloc] initWithRelativePath:key oldTree:source newTree:destination];
NSNumber *permissions = shouldChangePermissions(originalInfo, newInfo) ? newInfo[INFO_PERMISSIONS_KEY] : nil;
CreateBinaryDeltaOperation *operation = [[CreateBinaryDeltaOperation alloc] initWithRelativePath:key oldTree:source newTree:destination permissions:permissions];
[deltaQueue addOperation:operation];
[deltaOperations addObject:operation];
}
@@ -318,6 +343,10 @@ int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFi
assert(newFile);
xar_prop_set(newFile, "binary-delta", "true");
unlink([resultPath fileSystemRepresentation]);
if (operation.permissions) {
xar_prop_set(newFile, "mod-permissions", [[NSString stringWithFormat:@"%u", [operation.permissions unsignedShortValue]] UTF8String]);
}
}
xar_close(x);