2014-02-06 84 views
8

在StackOverflow和其他地方有很多关于如何清除Mac隔离区属性的信息。 在我的情况下,我想设置它。 这是为了测试我的应用程序是否已正确签名,以便用户在下载它后会很热地获得“不受信任的开发人员”警告。使用xattr设置Mac OSX隔离区属性

我的应用程序特别大(我们从大型文件下载站点发布,而不是商店),并且上传和下载以测试此应用程序并不方便。 我在过去一周的代码签名过程中发生过一些战斗,所以这个测试对我来说很重要。

一旦文件具有检疫财产我知道怎样才能改变它的数值:

0002 = downloaded but never opened (this is the one that causes the warning) 
0022 = app aborted by user from the warning dialogue (you hit 'cancel' in the dialogue) 
0062 = app opened (at least) once (you hit 'open' in the dialogue) 

但我不知道如何给它的财产在首位。

回答

7

这个代码不难,但你需要FSRef来做到这一点,这是不赞同的。也就是说,它仍然在10.9上运行。您必须链接CoreServices。

int main(int argc, const char * argv[]) { 
    @autoreleasepool { 
    if (argc != 2) { 
     printf("quarantine <path>\n"); 
     exit(1); 
    } 

    NSString *path = @(argv[1]); 
    OSStatus result; 
    FSRef pathRef; 
    result = FSPathMakeRef((UInt8*)[path UTF8String], &pathRef, 0); 
    if (result != noErr) { 
     NSLog(@"Error making ref (%d): %s", result, GetMacOSStatusCommentString(result)); 
     exit(result); 
    } 

    NSDictionary *quarantineProperties = @{(__bridge id)kLSQuarantineTypeKey: (__bridge id)kLSQuarantineTypeOtherDownload}; 

    result = LSSetItemAttribute(&pathRef, 
           kLSRolesAll, 
           kLSItemQuarantineProperties, 
           (__bridge CFTypeRef)quarantineProperties); 

    if (result != noErr) { 
     NSLog(@"Error setting attribute (%d): %s", result, GetMacOSStatusCommentString(result)); 
    } 
    exit(result); 
    } 
    return 0; 
} 

另一种方法是将隔离信息从一个文件复制到另一个文件。可以序列XATTR信息是这样的:

xattr -p com.apple.quarantine file > file.xattr 

您可以然后将这些属性来一次像这样的文件:

xattr -w com.apple.quarantine "`cat file.xattr`" file 

(即应该工作,但我还没有与隔离测试它我使用类似的技术来保存代码签名并重新应用它们。)

+1

将该属性复制到文本文件中,然后用'xattr -w'写入它确实有效。 –