Search code examples
cocoapermissionsposixnsfilemanager

Read file permissions in Cocoa



I am trying to read file permissions in Cocoa of a file having rw-r--r-- (hence 0644).
The code I am using is the following:

NSUInteger permissions;
permissions=[[fileManager attributesOfItemAtPath:file error:nil] filePosixPermissions];
NSLog(@"Permissions:%lu",permissions);

And the result is 420 when the expected result should be 644.

Do I do a calculation error ? Thanks !


Solution

  • When you see 0644, it's actually the octal (base 8) representation of the permissions.

    420 is the base 10 equivalent of 0644 in octal.

    NSLog can output octal with:

    NSLog(@"Permissions:%o", permissions);