Using xattr to set the Mac OSX quarantine property

走远了吗. 提交于 2019-12-05 04:40:34

The code for this isn't hard, but you need FSRef's to do it, which are deprecated. That said, it still works on 10.9. You have to link with 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;
}

Another approach is to just copy the quarantine information from one file to another. You can serialize xattr information like this:

xattr -p com.apple.quarantine file > file.xattr

You can then apply those attributes to another file like this:

xattr -w com.apple.quarantine "`cat file.xattr`" file

(That should work, but I haven't tested it with quarantining in particular. I use a similar technique to save code signatures and reapply them.)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!