所以,我需要在目录中搜索Unix Executable files
。我遍历目录和我正在搜索的文件的路径。我尝试过的一些方法。
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 可执行文件?
尝试使用 getResourceValue:forKey:error: 或 resourceValuesForKeys:error: NSURL 的方法和请求 NSURLTypeIdentifierKey。
补遗:
如果@Aravindhanarvi所说的是正确的,那么在 10.6 上存在错误,并且上述解决方案不可靠。更糟糕的是@petur由于缺少NSURLIsExecutableKey,解决方案也是不可能的。
另一种方法是回退到 NSFileManager,并使用 isExecutableFileAtPath: 和 attributesOfItemAtPath:error: (特别是 NSFilePosixPermissions 和 NSFileType 属性)等方法来实现 @petur 建议的相同逻辑。
想出了这个,只需将 url 指向要用作基础的目录即可。这是 ARC 代码。
数组 files 包含一个指向找到的每个可执行文件的 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]);
}