Skip to content

Instantly share code, notes, and snippets.

@zorgiepoo
Last active October 9, 2015 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zorgiepoo/93cf1901419187c512f8 to your computer and use it in GitHub Desktop.
Save zorgiepoo/93cf1901419187c512f8 to your computer and use it in GitHub Desktop.
Can't figure out how to copy a symbolic link, or a directory containing a symbolic link, that is originating on a different Network Volume (eg: in my case it's via AFP, or is it SMB2 these days..?)
// Note that on the command line cp -R /Volumes/mayur/bar.txt /Users/msp/Desktop/bar.txt
// works just fine; the symbolic link is copied, and it's not followed
// But I have *no idea* how to do this programatically as shown below
// (Note: My end goal is being able to copy an app from a volume to my local disk, but
// it fails when it encounters a symbolic link)
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSURL *sourceURL = [NSURL fileURLWithPath:@"/Volumes/mayur/bar.txt"]; // this is a symbolic link
NSURL *destinationURL = [NSURL fileURLWithPath:@"/Users/msp/Desktop/bar.txt"];
// this outputs 1, so the source does exist
NSLog(@"%d", [sourceURL checkResourceIsReachableAndReturnError:NULL]);
// This code below fails and outputs:
/*
Failed to copy item because: Error Domain=NSCocoaErrorDomain Code=512 "“bar.txt” couldn’t be copied to “Desktop”." UserInfo={NSSourceFilePathErrorKey=/Volumes/mayur/bar.txt, NSUserStringVariant=(
Copy
), NSDestinationFilePath=/Users/msp/Desktop/bar.txt, NSFilePath=/Volumes/mayur/bar.txt, NSUnderlyingError=0x600000042730 {Error Domain=NSPOSIXErrorDomain Code=22 "Invalid argument"}}
*/
NSError *error = nil;
if (![[NSFileManager defaultManager] copyItemAtURL:sourceURL toURL:destinationURL error:&error]) {
NSLog(@"Failed to copy item because: %@", error);
}
// the code below doesn't work either; it outputs
// Failed copying file with copyfile(3): 22 - Invalid argument
/*
if (copyfile(sourceURL.fileSystemRepresentation, destinationURL.fileSystemRepresentation, NULL, COPYFILE_STAT | COPYFILE_DATA | COPYFILE_NOFOLLOW) != 0) {
NSLog(@"Failed copying file with copyfile(3): %d - %s", errno, strerror(errno));
}
*/
}
@zorgiepoo
Copy link
Author

Looks like deprecated FSCopyObjectSync() works. Sigh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment