Question

I'm trying to copy a folder that is read-only, but has files inside it, to another location. When I use -copyItemAtPath:toPath:error: it copies the folder, but not the files. Sample code that demonstrates the error is below.
Changing the permissions of the folder before copying is not an option in this case.
Can anyone offer a suggestion?

The error is:
Error Domain=NSPOSIXErrorDomain Code=13 UserInfo=0x1001102a0 "The operation couldn’t be completed. Permission denied"

The error's userInfo is: {
NSDestinationFilePath = "/Users/xxxxxxx/Desktop/readonlyfilecopy/testdir/Abbey Road.jpg";
NSFilePath = "/Users/xxxxxxx/Desktop/testdir/Abbey Road.jpg";
NSUserStringVariant = Copy;
}

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSString* src = @"/Users/xxxxxxx/Desktop/testdir";
    NSString* target = @"/Users/xxxxxxx/Desktop/readonlyfilecopy/testdir";
    NSError* error = nil;

    [[NSFileManager defaultManager] copyItemAtPath:src toPath:target error:&error];

    if (error) {
        NSLog(@"%@", error);
        NSLog(@"%@", [error userInfo]);
    }

    [pool drain];
    return 0;
}
Was it helpful?

Solution

Are you certain you can read every single file in the folder. If so, then this may actually be a Cocoa bug. It's entirely possible that it's creating a new folder, setting it read-only, and then attempting to copy files into the folder. If you can reproduce this in a small test-case, you should submit a bug to apple.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top