Skip to content

Instantly share code, notes, and snippets.

@fernyb
Created March 5, 2011 02:12
Show Gist options
  • Save fernyb/856031 to your computer and use it in GitHub Desktop.
Save fernyb/856031 to your computer and use it in GitHub Desktop.
Obj-C wrapper method for, File Manager API, to overwrite a file.
@interface NSFileManager (FRBMethods)
- (BOOL)overwriteFileAtPath:(NSString *)src toPath:(NSString *)dst;
@end
@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