有没有办法从 NSFilePosixPermissions 整数中获取人类可读的字符串(例如@"drwxr-xr-x")?
问问题
3197 次
2 回答
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;
于 2010-11-08T20:46:03.110 回答
0
好吧,我想您可以像这样创建一个数组:
NSArray *convertToAlpha = [NSArray arrayWithObjects:@"---",@"--x",@"-w-",@"--wx",@"r--",@"r-x",@"rw-",@"rwx", nil];
然后将 NSFilePosixPermissions 转换为八进制后,将生成的数字拆分为其组件数字,并使用 convertToAlpha 将每个数字映射到其字母数字表示......
于 2010-11-08T19:35:01.613 回答