Last active
December 11, 2015 02:58
-
-
Save cruffenach/4534102 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BOOL noError(NSError **error) { | |
return (error && [*error code] == 0); | |
} | |
BOOL hasError(NSError **error) { | |
return !noError(error); | |
} | |
BOOL deleteFileAtPath(NSString *path, NSError **error) { | |
BOOL success = [[NSFileManager defaultManager] removeItemAtPath:path error:error]; | |
//If we failed because the file or directory didn't exist this is ok. Proceed as if successful. | |
if (!success && noSuchFileOrDirectoryError(error)) return YES; | |
return (success && noError(error)); | |
} | |
BOOL cleanDirectories(NSArray *directories, NSError **error) { | |
for(NSString *directory in directories) { | |
[[NSFileManager defaultManager] createDirectoryAtPath:directory | |
withIntermediateDirectories:YES | |
attributes:nil | |
error:error]; | |
} | |
if (noError(error)) { | |
for(NSString *directory in directories) { | |
NSArray *files = existingFileNamesInDirectory(directory, error); | |
for(NSString *fileName in files) { | |
if (!deleteFileAtPath([directory stringByAppendingPathComponent:fileName], error)) break; | |
} | |
if (hasError(error)) break; | |
} | |
} | |
return noError(error); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment