繁体   English   中英

获取卷的加密状态

[英]Get Encryption Status for Volume

我正在获取所有已安装内部卷的阵列,并检查该卷是否已使用FV2加密。 这是特定的代码:

[url getResourceValue:&volType forKey:NSURLVolumeIsEncryptedKey  error:&error];
    if (volType)
    {
        encrypted = TRUE;
    }

问题是无论卷是否加密,此代码都将返回true。

NSURLVolumeIsEncryptedKey的文档 ,它看起来像volType将是一个NSNumber(即包的原始值作为对象的事情)。 使用boolValue从中解压缩BOOL。

NSURL *url = [NSURL fileURLWithPath:NSHomeDirectory()];
NSNumber *volumeType = nil;
NSError *error = nil;
if (![url getResourceValue:&volumeType forKey:NSURLVolumeIsEncryptedKey error:&error]) {
    NSLog(@"Failed to get resource value: %@", error);
} else {
    BOOL isEncrypted = [volumeType boolValue];
    NSLog(@"Encrypted: %@", isEncrypted ? @"YES" : @"NO");
}

如果系统上有多个分区,则该API似乎无法正常工作。 我已经在2台Mac电脑上对其进行了测试,其中一台具有3个分区,而另一台仅具有root(单个分区)。 以下快速代码在仅具有根分区(单个分区)的系统上运行良好。

func checkIfHomeDirIsEncrypted() -> Bool? {
    let url:URL = URL.init(fileURLWithPath: NSHomeDirectory())
    do {
        let resourceValues:URLResourceValues = try url.resourceValues(forKeys: [URLResourceKey.volumeIsEncryptedKey, URLResourceKey.volumeIsRootFileSystemKey])
        if let isEncrypted:Bool = resourceValues.volumeIsEncrypted {
            return isEncrypted
        }
        print("Failed to get status")
        return nil
    }
    catch {
        print("Failed to get resource values")
        return nil
    }
}

if let isEncrypted:Bool = checkIfHomeDirIsEncrypted() {
    print("Volume is \(isEncrypted ? "encrypted":"not encrypted")")
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM