2014-02-06 25 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'寫入它確實有效。 –