使用xattr设置Mac OSX隔离属性

 童T-Aurora 发布于 2023-01-29 09:58

StackOverflow和其他地方有很多关于如何清除Mac隔离属性的信息.在我的情况下,我想设置它.这是为了测试我的应用程序是否已正确签名,以便用户在下载后可以获得"Untrusted Developer"警告.

我的应用程序特别大(我们从大型文件下载站点分发,而不是商店)并且不方便上传和下载来测试它.过去一周,我与代码签名进行了一些争夺,所以这个测试对我很重要.

一旦文件具有隔离属性,我就会看到如何更改它以获取值:

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 个回答
  • 这个代码并不难,但你需要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
    

    (这应该可行,但我没有特别检查过它.我使用类似的技术来保存代码签名并重新应用它们.)

    2023-01-29 10:01 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有