gpt4 book ai didi

objective-c NSFilePosixPermissions 到人类可读的 NSString

转载 作者:太空狗 更新时间:2023-10-30 03:44:22 25 4
gpt4 key购买 nike

有没有办法从 NSFilePosixPermissions 整数中获取人类可读的字符串(例如@"drwxr-xr-x")?

最佳答案

文件系统权限属性只是一个无符号长值。下面的代码显然可以提高效率,但它 [或多或少] 显示了获取所需字符串需要做的事情:

// 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;

关于objective-c NSFilePosixPermissions 到人类可读的 NSString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4127147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com