使用xattr设置Mac OSX隔离属性

9

在StackOverflow和其他地方有很多关于如何清除Mac隔离属性的信息。但我想设置它。

这是为了测试我的应用程序是否已正确签名,以便用户在下载后不会收到“不受信任的开发人员”警告。

我的应用程序特别大(我们从一个大型文件下载站点分发,而不是商店),因此上传和下载以进行测试并不方便。过去一周我一直在与代码签名作斗争,所以这个测试对我来说非常重要。

一旦文件具有隔离属性,我可以看到如何更改其值为:

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)

但我不知道如何首先给它赋予属性。

1个回答

9

这段代码并不难,但需要使用已经被弃用的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 写入确实有效。 - Ted Middleton

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接