NSFileManager -copyItemAtPath:toPath:error: 只读文件夹失败
我正在尝试将一个只读但包含文件的文件夹复制到另一个位置。当我使用 -copyItemAtPath:toPath:error: 时,它复制文件夹,但不复制文件。演示该错误的示例代码如下。
在这种情况下,无法在复制之前更改文件夹的权限。
有人可以提供建议吗?
错误是:
错误域 = NSPOSIXErrorDomain 代码 = 13 UserInfo = 0x1001102a0“操作无法完成。权限被拒绝”
错误的 userInfo 是:{
NSDestinationFilePath = "/Users/xxxxxxx/Desktop/readonlyfilecopy/testdir/Abbey Road.jpg";
NSFilePath = "/Users/xxxxxxx/Desktop/testdir/Abbey Road.jpg";
NSUserStringVariant = 复制;
}
#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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定可以读取文件夹中的每个文件吗?如果是这样,那么这实际上可能是一个 Cocoa bug。它完全有可能创建一个新文件夹,将其设置为只读,然后尝试将文件复制到该文件夹中。如果您可以在一个小测试用例中重现此问题,您应该向苹果提交错误。
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.