macos - 使用xattr设置Mac OSX隔离属性

标签 macos xattr

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

但是我首先不知道如何赋予它属性(property)。

最佳答案

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

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

关于macos - 使用xattr设置Mac OSX隔离属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21591485/

相关文章:

swift - NSTextField 获取购物车位置

ls - “ls -lah”输出中许可权末尾的小点是什么意思?

c - 如何获取文件的扩展属性(UNIX/C)?

macos - 使用终端复制文件内容

Mac 上的 Java : Cannot read resources in executable JAR

Python 模块 xattr 没有属性列表

FUSE 文件系统上的 Linux 功能

macos - 了解 xattr -p com.apple.quarantine 的输出

objective-c - Mac OS X NSUserNotificationCenter 通知获取取消事件/回调

java - JComponent.setVisible() 在 OS X 上无法成功隐藏分隔符