Swift iOS 上的文件管理器:wrong/different execute/read/write 的资源值
Swift FileManager on iOS: wrong/different resourceValues for execute/read/write
iOS 上的文件管理器:wrong/different execute/read/write
的资源值
我正在使用以下代码获取 wrong/different 资源值。
某些目录项的“URL-方法”returns execute/read/write 的其他资源值不同于“路径方法”。
例如文件夹“SystemData”:
- with "URL-method": isExecutable=true, isReadable=true, isWritable=true
- 使用“路径方法”:isExecutableFile:true,isReadableFile:false,isWritableFile:false
这个文件夹的 PosixPermissions 给了我 755 …这是不正确的。
所以两个问题:
- 为什么我会根据方法得到不同的值,而哪一个是正确的?
- 为什么 PosixPermissions 错误?
Xcode 版本 12.4 (12D4e) 和 iOS 14.4。
在真实设备上测试 (iPhone 11)
func FMTest() {
let fM = FileManager.default
do {
let currentRootDirURL = try fM.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).resourceValues(forKeys:[.parentDirectoryURLKey]).parentDirectory!
let dirItems: [URL] = try fM.contentsOfDirectory(
at: currentRootDirURL,
includingPropertiesForKeys:[.nameKey,.isExecutableKey,.isReadableKey,.isWritableKey]
)
for dirItem in dirItems {
// "URL-method"
let dirItemAttributes = try dirItem.resourceValues(forKeys:[.nameKey,.isExecutableKey,.isReadableKey,.isWritableKey])
print("+========\nnameKey: \(String(describing: dirItemAttributes.name))")
print("URL:")
print("..isExecutable: \(String(describing: dirItemAttributes.isExecutable))")
print("..isReadable: \(String(describing: dirItemAttributes.isReadable))")
print("..isWritable: \(String(describing: dirItemAttributes.isWritable))")
// "path-method"
let exec = fM.isExecutableFile(atPath: dirItem.path)
let read = fM.isReadableFile(atPath: dirItem.path)
let write = fM.isWritableFile(atPath: dirItem.path)
print("\nPATH:")
print("..isExecutableFile: \(exec)")
print("..isReadableFile: \(read)")
print("..isWritableFile: \(write)")
// try? just for avoiding "Operation not permitted" error in this example code
if let dirItemAttributesPATH = try? fM.attributesOfItem(atPath: dirItem.path) {
// "PosixPermission"
let dirItemPosixPermissionsDecimal = dirItemAttributesPATH[.posixPermissions] as? NSNumber
let dirItemPosixPermisionsOctal = String(dirItemPosixPermissionsDecimal!.intValue, radix: 8, uppercase: false)
print("\ndirItemPosixPermisionsOctal: \(dirItemPosixPermisionsOctal)\n+========\n")
} else {
print("\ndirItemPosixPermisionsOctal: - \n+========\n")
}
}
} catch {
print(error)
}
}
iOS 上的文件管理器:wrong/different execute/read/write
的资源值我正在使用以下代码获取 wrong/different 资源值。
某些目录项的“URL-方法”returns execute/read/write 的其他资源值不同于“路径方法”。 例如文件夹“SystemData”:
- with "URL-method": isExecutable=true, isReadable=true, isWritable=true
- 使用“路径方法”:isExecutableFile:true,isReadableFile:false,isWritableFile:false
这个文件夹的 PosixPermissions 给了我 755 …这是不正确的。
所以两个问题:
- 为什么我会根据方法得到不同的值,而哪一个是正确的?
- 为什么 PosixPermissions 错误?
Xcode 版本 12.4 (12D4e) 和 iOS 14.4。 在真实设备上测试 (iPhone 11)
func FMTest() {
let fM = FileManager.default
do {
let currentRootDirURL = try fM.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).resourceValues(forKeys:[.parentDirectoryURLKey]).parentDirectory!
let dirItems: [URL] = try fM.contentsOfDirectory(
at: currentRootDirURL,
includingPropertiesForKeys:[.nameKey,.isExecutableKey,.isReadableKey,.isWritableKey]
)
for dirItem in dirItems {
// "URL-method"
let dirItemAttributes = try dirItem.resourceValues(forKeys:[.nameKey,.isExecutableKey,.isReadableKey,.isWritableKey])
print("+========\nnameKey: \(String(describing: dirItemAttributes.name))")
print("URL:")
print("..isExecutable: \(String(describing: dirItemAttributes.isExecutable))")
print("..isReadable: \(String(describing: dirItemAttributes.isReadable))")
print("..isWritable: \(String(describing: dirItemAttributes.isWritable))")
// "path-method"
let exec = fM.isExecutableFile(atPath: dirItem.path)
let read = fM.isReadableFile(atPath: dirItem.path)
let write = fM.isWritableFile(atPath: dirItem.path)
print("\nPATH:")
print("..isExecutableFile: \(exec)")
print("..isReadableFile: \(read)")
print("..isWritableFile: \(write)")
// try? just for avoiding "Operation not permitted" error in this example code
if let dirItemAttributesPATH = try? fM.attributesOfItem(atPath: dirItem.path) {
// "PosixPermission"
let dirItemPosixPermissionsDecimal = dirItemAttributesPATH[.posixPermissions] as? NSNumber
let dirItemPosixPermisionsOctal = String(dirItemPosixPermissionsDecimal!.intValue, radix: 8, uppercase: false)
print("\ndirItemPosixPermisionsOctal: \(dirItemPosixPermisionsOctal)\n+========\n")
} else {
print("\ndirItemPosixPermisionsOctal: - \n+========\n")
}
}
} catch {
print(error)
}
}