Created
March 5, 2011 02:12
-
-
Save fernyb/856031 to your computer and use it in GitHub Desktop.
Obj-C wrapper method for, File Manager API, to overwrite a file.
This file contains hidden or 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
@interface NSFileManager (FRBMethods) | |
- (BOOL)overwriteFileAtPath:(NSString *)src toPath:(NSString *)dst; | |
@end |
This file contains hidden or 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
@implementation NSFileManager (FRBMethods) | |
- (BOOL)overwriteFileAtPath:(NSString *)src toPath:(NSString *)dst | |
{ | |
FSRef dstRef; // <= should be the destination path with no filename | |
FSRef srcRef; // <= should be the source path with filename | |
OSStatus err; | |
err = FSPathMakeRefWithOptions((UInt8 *)[[dst stringByDeletingLastPathComponent] fileSystemRepresentation], kFSPathMakeRefDoNotFollowLeafSymlink, &dstRef, NULL); | |
if (err != noErr) { | |
NSLog(@"* Error with dst, FSPathMakeRefWithOptions: %d", err); | |
return NO; | |
} | |
err = FSPathMakeRefWithOptions((UInt8 *)[src fileSystemRepresentation], kFSPathMakeRefDoNotFollowLeafSymlink, &srcRef, NULL); | |
if (err != noErr) { | |
NSLog(@"* Error with src, FSPathMakeRefWithOptions: %d", err); | |
return NO; | |
} | |
err = FSMoveObjectSync(&srcRef, &dstRef, (CFStringRef)[dst lastPathComponent], NULL, kFSFileOperationOverwrite); | |
if (err != noErr) { | |
NSLog(@"* Error with FSMoveObjectSync, %d", err); | |
return NO; | |
} | |
return YES; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment