gpt4 book ai didi

cocoa - 使用 FileAttributeKey.posixPermissions 与使用 stat -f %A 有什么不同?

转载 作者:行者123 更新时间:2023-12-03 17:09:02 26 4
gpt4 key购买 nike

我正在尝试获取文件的 posix 权限,但是当我使用 key FileAttributeKey.posixPermissions (最初为 NSFilePosixPermissions)来检索数字 posix 权限值时,它返回511 (-r-x--x--x),但是当我在终端中使用stat -f %A/path/to/file时,它返回777 (-rwxrwxrwx) 这是正确的(我使用了chmod 777/path/to/file,所以它应该是777 (-rwxrwxrwx) >).

这是我使用 FileAttributeKey.posixPermissions (Swift 4) 的代码:

var numericalValue = "000"
if let attributes = try? FileManager.default.attributesOfItem(atPath: "/path/to/file") {
if let posixPermissions = attributes[.posixPermissions] as? NSNumber {
numericalValue = posixPermissions.stringValue
}
}

我不知道发生了什么,我想知道从 FileAttributeKey.posixPermissions 返回的值与 stat -f %A/path/to 的输出有何不同/filestat -x/path/to/file,有人可以帮我弄清楚吗?

最佳答案

您需要以八进制显示结果:

if let attributes = try? FileManager.default.attributesOfItem(atPath: "/path/to/file") {
if let posixPermissions = attributes[.posixPermissions] as? NSNumber {
let octal = String(posixPermissions.intValue, radix: 8, uppercase: false)
print(octal)
}
}

关于cocoa - 使用 FileAttributeKey.posixPermissions 与使用 stat -f %A 有什么不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54922727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com