2012-08-09 83 views
1

所以,我需要在目录中搜索Unix Executable files。我遍历目录和我正在搜索的文件的路径。我试过的一些方法。如何以编程方式在OS X中搜索unix可执行文件?

1.当文件扩展名

Unix Executable file没有文件扩展名的帮助,但有些文件的文件也没有扩展名。所以,它失败了。

2.用的NSFileManager

NSDicitionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil]; 

的帮助,它不具有任何独特属性找到Unix的可执行文件。

3. MDItemRef的帮助

它具有属性称为kMDItemContentType但它给一些只unix executable files的正确结果。

MDItemRef  inspectedRef; 
CFArrayRef  inspectedRefAttributeNames; 
CFDictionaryRef inspectedRefAttributeValues; 
inspectedRef = MDItemCreate(kCFAllocatorDefault,(CFStringRef)filePath); 
if(inspectedRef) { 
    inspectedRefAttributeNames = MDItemCopyAttributeNames(inspectedRef); 
    inspectedRefAttributeValues = MDItemCopyAttributes(inspectedRef,inspectedRefAttributeNames); 
    NSDictionary *attribDict = (__bridge NSDictionary*)inspectedRefAttributeValues; 
    if([[attribDict objectForKey:@"kMDItemContentType"] isEqualToString:@"public.unix-executable"]) 
     NSLog(@"Unix Executable file"); 
} 

4.用unix命令的帮助 “文件”

NSTask *unixTask = [[NSTask alloc] init]; 
[unixTask setStandardOutput:newPipe]; 
[unixTask setLaunchPath:@"/usr/bin/file"]; 
[unixTask setArguments:[NSArray arrayWithObject:filePath]]; 
[unixTask launch]; 
[unixTask waitUntilExit]; 
[unixTask terminationStatus]; 

while ((inData = [readHandle availableData]) && [inData length]) { 
    returnValue= [[NSString alloc] initWithData:inData encoding:[NSString defaultCStringEncoding]]; 
    returnValue = [returnValue substringToIndex:[returnValue length]-1]; 
    NSLog(@"%@",returnValue); 
} 

这里,从returnValue我能找到它是否是UNIX可执行与否。但过程非常缓慢。所以,我的问题是如何以有效的方式搜索unix可执行文件?

回答

2

尝试使用NSURL的getResourceValue:forKey:error:或resourceValuesForKeys:error:方法并请求NSURLTypeIdentifierKey。

附录:

如果什么@Aravindhanarvi说是正确的,在10.6有错误和上面的解决方案是不可靠的。更糟糕的是@petur解决方案也不适用于缺少NSURLIsExecutableKey。

另一种方法是退回到NSFileManager并使用isExecutableFileAtPath:和attributesOfItemAtPath:error:(特别是NSFilePosixPermissions和NSFileType属性)等方法来实现@petur建议的相同逻辑。

+0

感谢您的回答。对于文档类型文件也显示为Unix可执行文件:( – Aravindhan 2012-08-10 08:32:59

+0

我不知道你以前的评论意味着什么。你的意思是NSURLTypeIdentifierKey属性是“public.unix-executable”,但查找器列出文件为“文件“?这听起来很奇怪。 – 2012-08-10 09:32:14

+0

雅..我不是为什么它显示文件文件为unix可执行文件。 – Aravindhan 2012-08-10 09:40:20

2

想到了这一点,只需将url指向您要用作基地的目录即可。 这是ARC代码。

数组文件包含指向找到的每个可执行文件的url指针。

@autoreleasepool { 

    NSFileManager *defaultFileManager = [NSFileManager defaultManager]; 
    NSURL *url = [NSURL URLWithString:@"/private/tmp/"]; // Search path 
    NSDirectoryEnumerator *dirEnumerator = [defaultFileManager enumeratorAtURL:url includingPropertiesForKeys:[NSArray arrayWithObjects:NSURLNameKey, nil] options:0 errorHandler:nil]; 

    NSMutableArray *files = [NSMutableArray array]; 

    // extract non-executable files 
    for (NSURL *file in dirEnumerator) { 
     NSNumber *isExecutable; 
     NSNumber *isDirectory; // Directories have the executable flag set, but we are not interested in them 
     NSError *error, *error2; 
     [file getResourceValue:&isExecutable forKey:NSURLIsExecutableKey error:&error]; 
     [file getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:&error2]; 

     // Deal with errors 
     if (error) 
      NSLog(@"%@", [error localizedDescription]); 
     else if (error2) 
      NSLog(@"%@", [error2 localizedDescription]); 
     else if ([isExecutable boolValue] && ![isDirectory boolValue]) { 
      [files addObject:file]; 
     } 

    // print out all executable files to the console 
    for (id i in files) 
     NSLog(@"%@", [i description]); 

} 
+0

NSURLIsExecutableKey仅在10.7中存在。我想在10.6中做到这一点。 – Aravindhan 2012-08-10 08:51:13

+0

下次请在问题主体中注明这样的限制:) – 2012-08-11 09:30:46

相关问题