From 3a5dc79da3d0475b3a3551bcc4e141c2fc41215a Mon Sep 17 00:00:00 2001 From: Miltiadis Vasilakis Date: Thu, 29 Dec 2011 16:11:32 +0200 Subject: [PATCH] Modify sync to use only X-Object-Hash. --- pithos-apple-common | 2 +- pithos-macos/PithosLocalObjectState.h | 8 ++--- pithos-macos/PithosLocalObjectState.m | 56 +++++++++++---------------------- pithos-macos/PithosSyncDaemon.m | 27 +++++++--------- 4 files changed, 34 insertions(+), 59 deletions(-) diff --git a/pithos-apple-common b/pithos-apple-common index 6d3af6e..40db2cf 160000 --- a/pithos-apple-common +++ b/pithos-apple-common @@ -1 +1 @@ -Subproject commit 6d3af6ef2539e07a14f3d860c4bb1ec063f9fe8a +Subproject commit 40db2cf7a8a1aa88098e1e224b29a3570c806aa2 diff --git a/pithos-macos/PithosLocalObjectState.h b/pithos-macos/PithosLocalObjectState.h index 806d4dc..42d5748 100644 --- a/pithos-macos/PithosLocalObjectState.h +++ b/pithos-macos/PithosLocalObjectState.h @@ -38,14 +38,12 @@ #import @interface PithosLocalObjectState : NSObject { - NSString *md5; - NSString *hashMapHash; NSString *filePath; BOOL isDirectory; + NSString *hash; NSString *tmpFilePath; BOOL exists; - NSString *hash; } - (id)initWithFile:(NSString *)aFilePath blockHash:(NSString *)blockHash blockSize:(NSUInteger)blockSize; @@ -55,13 +53,11 @@ + (id)localObjectStateWithHash:(NSString *)aHash directory:(BOOL)anIsDirectory; - (BOOL)isEqualToState:(PithosLocalObjectState *)aState; -@property (nonatomic, retain) NSString *md5; -@property (nonatomic, retain) NSString *hashMapHash; @property (nonatomic, retain) NSString *filePath; @property (nonatomic, assign) BOOL isDirectory; +@property (nonatomic, retain) NSString *hash; @property (nonatomic, retain) NSString *tmpFilePath; @property (nonatomic, readonly) BOOL exists; -@property (nonatomic, retain) NSString *hash; @end diff --git a/pithos-macos/PithosLocalObjectState.m b/pithos-macos/PithosLocalObjectState.m index f519862..f63a361 100644 --- a/pithos-macos/PithosLocalObjectState.m +++ b/pithos-macos/PithosLocalObjectState.m @@ -37,11 +37,10 @@ #import "PithosLocalObjectState.h" #import "PithosUtilities.h" -#import "FileMD5Hash.h" #import "HashMapHash.h" @implementation PithosLocalObjectState -@synthesize md5, hashMapHash, filePath, isDirectory, exists, hash, tmpFilePath; +@synthesize filePath, isDirectory, exists, hash, tmpFilePath; #pragma mark - #pragma mark Object Lifecycle @@ -50,11 +49,9 @@ if ((self = [self init])) { self.filePath = aFilePath; if ([[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory] && !isDirectory) { - self.md5 = (NSString *)FileMD5HashCreateWithPath((CFStringRef)aFilePath, - FileHashDefaultChunkSizeForReadingData); - self.hashMapHash = [HashMapHash calculateHashMapHash:[HashMapHash calculateObjectHashMap:aFilePath - withBlockHash:blockHash - andBlockSize:blockSize]]; + self.hash = [HashMapHash calculateHashMapHash:[HashMapHash calculateObjectHashMap:aFilePath + withBlockHash:blockHash + andBlockSize:blockSize]]; } } return self; @@ -64,10 +61,8 @@ if ((self = [self init])) { if (anIsDirectory) isDirectory = YES; - else if ([aHash length] == 32) - self.md5 = aHash; else if ([aHash length] == 64) - self.hashMapHash = aHash; + self.hash = aHash; } return self; } @@ -87,14 +82,13 @@ - (void)dealloc { self.tmpFilePath = nil; [filePath release]; - [hashMapHash release]; - [md5 release]; + [hash release]; [super dealloc]; } - (NSString *)description { - return [NSString stringWithFormat:@"md5: %@, hashMapHash: %@, filePath: %@, isDirectory: %d", - md5, hashMapHash, filePath, isDirectory]; + return [NSString stringWithFormat:@"hash: %@, filePath: %@, isDirectory: %d", + hash, filePath, isDirectory]; } - (BOOL)isEqualToState:(PithosLocalObjectState *)aState { @@ -111,8 +105,8 @@ // Object exists, while the other doesn't return NO; else - // Both objects exist, check that they have at least one hash in common - return ([self.md5 isEqualToString:aState.md5] || [self.hashMapHash isEqualToString:aState.hashMapHash]); + // Both objects exist, check that they have the same hash + return [self.hash isEqualToString:aState.hash]; } #pragma mark - @@ -121,8 +115,7 @@ - (void)setIsDirectory:(BOOL)anIsDirectory { isDirectory = anIsDirectory; if (isDirectory) { - self.md5 = nil; - self.hashMapHash = nil; + self.hash = nil; self.tmpFilePath = nil; } } @@ -145,24 +138,15 @@ } - (BOOL)exists { - return (isDirectory || md5 || hashMapHash); -} - -- (NSString *)hash { - if (hashMapHash) - return hashMapHash; - else - return md5; + return (isDirectory || hash); } - (void)setHash:(NSString *)aHash { - self.md5 = nil; - self.hashMapHash = nil; - if ([aHash length] == 32) { - self.md5 = aHash; - } else if ([aHash length] == 64) { - self.hashMapHash = aHash; - } + [hash release]; + if ([aHash length] == 64) + hash = [aHash retain]; + else + hash = nil; } #pragma mark - @@ -170,8 +154,7 @@ - (id)initWithCoder:(NSCoder *)decoder { if ((self = [super init])) { - self.md5 = [decoder decodeObjectForKey:@"md5"]; - self.hashMapHash = [decoder decodeObjectForKey:@"hashMapHash"]; + self.hash = [decoder decodeObjectForKey:@"hash"]; self.filePath = [decoder decodeObjectForKey:@"filePath"]; self.isDirectory = [decoder decodeBoolForKey:@"isDirectory"]; self.tmpFilePath = [decoder decodeObjectForKey:@"tmpFilePath"]; @@ -180,8 +163,7 @@ } - (void)encodeWithCoder:(NSCoder *)encoder { - [encoder encodeObject:md5 forKey:@"md5"]; - [encoder encodeObject:hashMapHash forKey:@"hashMapHash"]; + [encoder encodeObject:hash forKey:@"hash"]; [encoder encodeObject:filePath forKey:@"filePath"]; [encoder encodeBool:isDirectory forKey:@"isDirectory"]; [encoder encodeObject:tmpFilePath forKey:@"tmpFilePath"]; diff --git a/pithos-macos/PithosSyncDaemon.m b/pithos-macos/PithosSyncDaemon.m index c8008d6..9d355ac 100644 --- a/pithos-macos/PithosSyncDaemon.m +++ b/pithos-macos/PithosSyncDaemon.m @@ -609,19 +609,16 @@ } - (BOOL)findLocalCopyForObjectWithHash:(NSString *)hash forFile:(NSString *)filePath { - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - NSUInteger hashLength = [hash length]; - if ((hashLength != 32) && (hashLength != 64)) { - [pool drain]; + if ([hash length] != 64) return NO; - } + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; PithosLocalObjectState *localState; NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL isDirectory; NSError *error = nil; for (NSString *localFilePath in [currentLocalObjectStates allKeys]) { localState = [currentLocalObjectStates objectForKey:localFilePath]; - if (!localState.isDirectory && ([hash isEqualToString:localState.md5] || [hash isEqualToString:localState.hashMapHash]) && + if (!localState.isDirectory && [hash isEqualToString:localState.hash] && [fileManager fileExistsAtPath:localFilePath isDirectory:&isDirectory] && !isDirectory) { if ([localFilePath hasPrefix:containerDirectoryPath]) { if (![fileManager copyItemAtPath:localFilePath toPath:filePath error:&error] || error) { @@ -670,7 +667,7 @@ error:&remoteError isDirectory:&remoteIsDirectory sharingAccount:nil]; - if (!object || !object.hash) { + if (!object || !object.objectHash) { // Delete local object if (remoteObjectExists) { // Remote object created in the meantime, just mark the sync cycle as incomplete, but do delete the local object @@ -738,7 +735,7 @@ error:error]; } else { fileCreated = YES; - storedState.hash = object.hash; + storedState.hash = object.objectHash; storedState.tmpFilePath = nil; [self saveLocalState]; } @@ -765,7 +762,7 @@ error:error]; } // Check first if a local copy exists - if ([self findLocalCopyForObjectWithHash:object.hash forFile:filePath]) { + if ([self findLocalCopyForObjectWithHash:object.objectHash forFile:filePath]) { [self startAndEndActivityWithType:PithosActivityOther message:[NSString stringWithFormat:@"Sync: Downloading '%@' (100%%)", object.name]]; } else { @@ -822,7 +819,7 @@ error:error]; } // Check first if a local copy exists - if ([self findLocalCopyForObjectWithHash:object.hash forFile:filePath]) { + if ([self findLocalCopyForObjectWithHash:object.objectHash forFile:filePath]) { [self startAndEndActivityWithType:PithosActivityOther message:[NSString stringWithFormat:@"Sync: Downloading '%@' (100%%)", object.name]]; // Delete incomplete temp download @@ -1123,7 +1120,7 @@ remoteObjectState.isDirectory = YES; object.contentType = @"application/directory"; } else { - remoteObjectState.hash = remoteObject.hash; + remoteObjectState.hash = remoteObject.objectHash; } } @@ -1138,7 +1135,7 @@ object.bytes = remoteObject.bytes; object.version = remoteObject.version; object.contentType = remoteObject.contentType; - object.hash = remoteObject.hash; + object.objectHash = remoteObject.objectHash; [self updateLocalStateWithObject:object localFilePath:filePath]; } else if (!remoteObject && !currentLocalObjectState.exists) { // Server state hasn't changed @@ -1197,7 +1194,7 @@ object.bytes = remoteObject.bytes; object.version = remoteObject.version; object.contentType = remoteObject.contentType; - object.hash = remoteObject.hash; + object.objectHash = remoteObject.objectHash; [self updateLocalStateWithObject:object localFilePath:filePath]; } if (choice == NSAlertSecondButtonReturn) { [self updateServerStateWithCurrentState:currentLocalObjectState @@ -1387,7 +1384,7 @@ totalBytes:activity.totalBytes currentBytes:activity.totalBytes]; - storedState.hash = object.hash; + storedState.hash = object.objectHash; storedState.tmpFilePath = nil; [self saveLocalState]; @@ -1558,7 +1555,7 @@ NSUInteger currentBytes = activity.currentBytes; if (objectRequest.responseStatusCode == 201) { NSLog(@"Sync::object created: %@", objectRequest.url); - storedState.hash = [objectRequest eTag]; + storedState.hash = [objectRequest objectHash]; [self saveLocalState]; [self endActivity:activity withMessage:[objectRequest.userInfo objectForKey:@"finishedActivityMessage"] -- 1.7.10.4