For local updates from remote objects, a check is made if the remote object still...
authorMiltiadis Vasilakis <mvasilak@gmail.com>
Fri, 18 Nov 2011 11:33:45 +0000 (13:33 +0200)
committerMiltiadis Vasilakis <mvasilak@gmail.com>
Fri, 18 Nov 2011 11:33:45 +0000 (13:33 +0200)
pithos-macos/PithosSyncDaemon.m
pithos-macos/PithosUtilities.m

index f80e522..9fcc61c 100644 (file)
     BOOL isDirectory;
     BOOL fileExists = [fileManager fileExistsAtPath:filePath isDirectory:&isDirectory];
     PithosLocalObjectState *storedState = [storedLocalObjectStates objectForKey:object.name];
+    // Remote updated info
+    NSError *remoteError;
+    BOOL remoteIsDirectory;
+    BOOL remoteObjectExists = [PithosUtilities objectExistsAtContainerName:containerName 
+                                                                objectName:object.name 
+                                                                     error:&remoteError 
+                                                               isDirectory:&remoteIsDirectory 
+                                                            sharingAccount:nil];
     if (!object || !object.hash) {
         // Delete local object
+        if (remoteObjectExists) {
+            // Remote object created in the meantime, just mark the sync cycle as incomplete, but do delete the local object
+            syncIncomplete = YES;
+        }
         NSLog(@"Sync::delete local object: %@", filePath);
         if (!fileExists || [self moveToTempTrashFile:filePath]) {
             [self startAndEndActivityWithType:PithosActivityOther 
         }
     } else if ([PithosUtilities isContentTypeDirectory:object.contentType]) {
         // Create local directory object
+        if (!remoteObjectExists || !remoteIsDirectory) {
+            // Remote directory object deleted or changed to a file object in the meantime, mark the sync cycle as incomplete and skip
+            syncIncomplete = YES;
+            return;
+        }
         NSLog(@"Sync::create local directory object: %@", filePath);
         BOOL directoryCreated = NO;
         if (!fileExists || (!isDirectory && [self moveToTempTrashFile:filePath])) {
                                       message:[NSString stringWithFormat:@"Sync: Creating directory '%@' locally (finished)", object.name]];
     } else if (object.bytes == 0) {
         // Create local object with zero length
+        if (!remoteObjectExists || remoteIsDirectory) {
+            // Remote file object deleted or changed to a directory object in the meantime, mark the sync cycle as incomplete and skip
+            syncIncomplete = YES;
+            return;
+        }
         NSLog(@"Sync::create local zero length object: %@", filePath);
         BOOL fileCreated = NO;
         if (!fileExists || ((isDirectory || [PithosUtilities bytesOfFile:filePath]) && [self moveToTempTrashFile:filePath])) {
                                       message:[NSString stringWithFormat:@"Sync: Downloading '%@' (100%%)", object.name]];
     } else if (storedState.filePath == nil) {
         // Create new local object
+        if (!remoteObjectExists || remoteIsDirectory) {
+            // Remote file object deleted or changed to a directory object in the meantime, mark the sync cycle as incomplete and skip
+            syncIncomplete = YES;
+            return;
+        }
         // Check first if a local copy exists
         if ([self findLocalCopyForObjectWithHash:object.hash forFile:filePath]) {
             [self startAndEndActivityWithType:PithosActivityOther 
         }
     } else {
         // Resume local object download
+        if (!remoteObjectExists || remoteIsDirectory) {
+            // Remote file object deleted or changed to a directory object in the meantime, mark the sync cycle as incomplete and skip
+            syncIncomplete = YES;
+            return;
+        }
         // Check first if a local copy exists
         if ([self findLocalCopyForObjectWithHash:object.hash forFile:filePath]) {
             [self startAndEndActivityWithType:PithosActivityOther 
             // Local object created in the meantime, just mark the sync cycle as incomplete, but do delete the server object
             syncIncomplete = YES;
         }
-        NSString *safeObjectName = [PithosUtilities safeObjectNameForContainerName:@"trash" 
-                                                                        objectName:object.name];
-        if (safeObjectName) {
+        NSString *safeName;
+        if ([PithosUtilities isContentTypeDirectory:object.contentType])
+            safeName = [PithosUtilities safeSubdirNameForContainerName:@"trash" 
+                                                            subdirName:object.name];
+        else
+            safeName = [PithosUtilities safeObjectNameForContainerName:@"trash" 
+                                                            objectName:object.name];
+        if (safeName) {
             ASIPithosObjectRequest *objectRequest = [PithosUtilities moveObjectRequestWithContainerName:containerName 
                                                                                              objectName:object.name 
                                                                                destinationContainerName:@"trash" 
-                                                                                  destinationObjectName:safeObjectName 
+                                                                                  destinationObjectName:safeName 
                                                                                           checkIfExists:NO];
             if (objectRequest) {
                 objectRequest.delegate = self;
index ac77ffe..c5fc3cc 100644 (file)
     return NO;
 }
 
-// Returns if the called should proceed, after an interactive check if an object exists 
+// Returns if the caller should proceed, after an interactive check if an object exists 
 // at the given container/object path is performed
 + (BOOL)proceedIfObjectExistsAtContainerName:(NSString *)containerName objectName:(NSString *)objectName 
                               sharingAccount:(NSString *)sharingAccount {