I hope I am not wrong here with my (first posted) question.
I’m getting wrong/different resourceValues with following code (it lists all directory items in the sandbox directory).
The „URL-method“ returns for some directory items other resourceValues for execute/read/write than the „path-methods“.
For instance folder „SystemData“:
- with „URL-method“: isExecutable=true, isReadable=true, isWritable=true
- with „path-methods“: isExecutableFile: true, isReadableFile: false, isWritableFile: false
The PosixPermissions for this folder gives me 755 … which is not correct.
So two questions:
- Why am I getting different values depending on method and which one are correct?
- Why are the PosixPermissions wrong?
I'm thankful for any help!
Xcode Version 12.4 (12D4e)
Tested on real device (iPhone 11, iOS 14.4)
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)
}
}