有没有办法从NSFilePosixPermissions整数中获取人类可读的字符串(例如@“drwxr-xr-x”)?
答案 0 :(得分:5)
文件系统权限属性只是一个无符号长整型值。下面的代码显然可以提高效率,但它显示[或多或少]需要做什么来获得你想要的字符串:
// The indices of the items in the permsArray correspond to the POSIX
// permissions. Essentially each bit of the POSIX permissions represents
// a read, write, or execute bit.
NSArray *permsArray = [NSArray arrayWithObjects:@"---", @"--x", @"-w-", @"-wx", @"r--", @"r-x", @"rw-", @"rwx", nil];
NSFileManager *fm = [[[NSFileManager alloc] init] autorelease];
NSMutableString *result = [NSMutableString string];
NSDictionary *attrs = [fm attributesOfItemAtPath:@"some/path.txt" error:NULL];
if (!attrs)
return nil;
NSUInteger perms = [attrs filePosixPermissions];
if ([[attrs fileType] isEqualToString:NSFileTypeDirectory])
[result appendString:@"d"];
else
[result appendString:@"-"];
// loop through POSIX permissions, starting at user, then group, then other.
for (int i = 2; i >= 0; i--)
{
// this creates an index from 0 to 7
unsigned long thisPart = (perms >> (i * 3)) & 0x7;
// we look up this index in our permissions array and append it.
[result appendString:[permsArray objectAtIndex:thisPart]];
}
return result;
答案 1 :(得分:0)
嗯,我想你可以像这样创建一个数组:
NSArray *convertToAlpha = [NSArray arrayWithObjects:@"---",@"--x",@"-w-",@"--wx",@"r--",@"r-x",@"rw-",@"rwx", nil];
然后在将NSFilePosixPermissions转换为八进制之后,将结果数字拆分为其组成部分数字,并使用convertToAlpha将每个数字映射到其字母数字表示....