Explorar el Código

Improve the code robustness of disk cache migrate

DreamPiggy hace 7 años
padre
commit
3863264a13
Se han modificado 2 ficheros con 20 adiciones y 14 borrados
  1. 11 7
      SDWebImage/SDDiskCache.m
  2. 9 7
      SDWebImage/SDImageCache.m

+ 11 - 7
SDWebImage/SDDiskCache.m

@@ -231,20 +231,24 @@
 - (void)moveCacheDirectoryFromPath:(nonnull NSString *)srcPath toPath:(nonnull NSString *)dstPath {
     NSParameterAssert(srcPath);
     NSParameterAssert(dstPath);
+    // Check if old path is equal to new path
+    if ([srcPath isEqualToString:dstPath]) {
+        return;
+    }
     BOOL isDirectory;
     // Check if old path is directory
     if (![self.fileManager fileExistsAtPath:srcPath isDirectory:&isDirectory] || !isDirectory) {
         return;
     }
     // Check if new path is directory
-    if (![self.fileManager fileExistsAtPath:dstPath isDirectory:&isDirectory]) {
-        // New directory does not exist, rename directory
-        [self.fileManager moveItemAtPath:srcPath toPath:dstPath error:nil];
-    } else {
+    if (![self.fileManager fileExistsAtPath:dstPath isDirectory:&isDirectory] || !isDirectory) {
         if (!isDirectory) {
-            // New path is not directory, remove directory
+            // New path is not directory, remove file
             [self.fileManager removeItemAtPath:dstPath error:nil];
         }
+        // New directory does not exist, rename directory
+        [self.fileManager moveItemAtPath:srcPath toPath:dstPath error:nil];
+    } else {
         // New directory exist, merge the files
         NSDirectoryEnumerator *dirEnumerator = [self.fileManager enumeratorAtPath:srcPath];
         NSString *file;
@@ -252,9 +256,9 @@
             // Don't handle error, just try to move.
             [self.fileManager moveItemAtPath:[srcPath stringByAppendingPathComponent:file] toPath:[dstPath stringByAppendingPathComponent:file] error:nil];
         }
+        // Remove the old path
+        [self.fileManager removeItemAtPath:srcPath error:nil];
     }
-    // Remove the old path
-    [self.fileManager removeItemAtPath:srcPath error:nil];
 }
 
 #pragma mark - Hash

+ 9 - 7
SDWebImage/SDImageCache.m

@@ -86,6 +86,8 @@
         
         NSAssert([config.diskCacheClass conformsToProtocol:@protocol(SDDiskCache)], @"Custom disk cache class must conform to `SDDiskCache` protocol");
         _diskCache = [[config.diskCacheClass alloc] initWithCachePath:_diskCachePath config:_config];
+        
+        // Check and migrate disk cache directory if need
         [self migrateDiskCacheDirectory];
 
 #if SD_UIKIT
@@ -124,22 +126,22 @@
     return [self.diskCache cachePathForKey:key];
 }
 
-- (nullable NSString *)makeDiskCachePath:(nonnull NSString*)fullNamespace {
+- (nullable NSString *)makeDiskCachePath:(nonnull NSString *)fullNamespace {
     NSArray<NSString *> *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
-    return [paths[0] stringByAppendingPathComponent:fullNamespace];
+    return [paths.firstObject stringByAppendingPathComponent:fullNamespace];
 }
 
 - (void)migrateDiskCacheDirectory {
-    static dispatch_once_t onceToken;
-    dispatch_once(&onceToken, ^{
-        if ([self.diskCache isKindOfClass:[SDDiskCache class]]) {
+    if ([self.diskCache isKindOfClass:[SDDiskCache class]]) {
+        static dispatch_once_t onceToken;
+        dispatch_once(&onceToken, ^{
             NSString *newDefaultPath = [[self makeDiskCachePath:@"default"] stringByAppendingPathComponent:@"com.hackemist.SDImageCache.default"];
             NSString *oldDefaultPath = [[self makeDiskCachePath:@"default"] stringByAppendingPathComponent:@"com.hackemist.SDWebImageCache.default"];
             dispatch_async(self.ioQueue, ^{
                 [((SDDiskCache *)self.diskCache) moveCacheDirectoryFromPath:oldDefaultPath toPath:newDefaultPath];
             });
-        }
-    });
+        });
+    }
 }
 
 #pragma mark - Store Ops