【问题标题】:Using xattr to set the Mac OSX quarantine property使用 xattr 设置 Mac OSX 隔离属性
【发布时间】:2014-02-06 00:15:53
【问题描述】:

在 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)

但我一开始不知道如何赋予它属性。

【问题讨论】:

    标签: macos xattr


    【解决方案1】:

    这方面的代码并不难,但您需要 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
    

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

    【讨论】:

    • 将属性复制到文本文件,然后用xattr -w 写入确实有效。
    猜你喜欢
    • 2011-11-25
    • 2018-04-30
    • 2023-03-27
    • 2016-11-11
    • 2015-08-24
    • 2014-02-25
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多