【问题标题】:Enumerating over a directory gives me an error when I try to follow aliases当我尝试遵循别名时,枚举目录会给我一个错误
【发布时间】:2012-05-26 05:37:25
【问题描述】:

我希望我的用户能够将文件夹拖放到我的 OSX 应用程序中。然后我查看目录及其子目录中的所有文件。这很好用,但当目录中有别名时就不行了。别名已正确解析(感谢 Matt Gallagher,Apple 真丢脸),但枚举器不允许我枚举目标目录(它只是不返回任何元素和下面的错误)。下面是一些示例代码:

-(void)enumDirAtPath:(NSString*)path  {
    NSString* file_enum = nil;
    NSDirectoryEnumerator* enumerator = [[NSFileManager defaultManager] enumeratorAtPath:path];
    while (file_enum = [enumerator nextObject])
    {
        NSString* file =  [[NSString stringWithFormat:@"%@/%@",path,file_enum] stringByResolvingSymlinksAndAliases];

        BOOL isDirectory = NO;
        [[NSFileManager defaultManager] fileExistsAtPath:file isDirectory: &isDirectory];
        if (!isDirectory) {
            NSLog(@"Adding file: %@",file);
        } else {
            NSLog(@"Found dir: %@",file);
           [self enumDirAtPath: file];
        }
   }
}

enumeratorAtURL:includingPropertiesForKeys:options:errorHandler: 相同的代码给了我这个错误:

错误:错误域=NSCocoaErrorDomain 代码=257“文件“音乐” 无法打开,因为您无权查看。” 用户信息=0x1094cf670 {NSURL=file://localhost/Users/david/Desktop/Musik, NSFilePath=/Users/david/Desktop/Musik, NSUnderlyingError=0x1094d43f0 "操作无法完成。操作不允许"}

即使我只是 dispatch_async 递归调用,它也不会让我这样做。有什么方法可以遍历目录并获取所有内容?

注意:我完全清楚这可能会导致无限循环。这段代码只是为了说明问题。

【问题讨论】:

  • 您可以而且应该使用stringByAppendingPathComponent: 而不是将路径与stringWithFormat: 一起使用。

标签: cocoa nsfilemanager


【解决方案1】:

我非常确定答案就在错误消息中:

无法打开文件“Mus​​ik”,因为您没有查看权限。

在 Finder 应用程序中显示别名的目的地,并通过“获取信息”或终端中的 ls -l@ 检查其权限 — 我敢打赌,您的用户帐户 david 没有被授予以下权限之一:

  • read
  • execute
  • list

最后一个通常在命令行上可见,除非与明确的deny-access-control-entry 结合使用。

【讨论】:

  • 你可能是对的。因为它被沙盒化并且用户将目录拖进来。看起来我没有获得关注别名的权限。我会检查的。 (非沙盒权限都可以)
  • 这是沙盒。感谢您让我检查:D
猜你喜欢
  • 1970-01-01
  • 2019-09-22
  • 2023-03-22
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 2019-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多