質問

NSFILEPOSIXPERMISSIONS INTEGERから人間の読み取り可能な文字列(@"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;

他のヒント

まあ、私はあなたがそうするような配列を作成できると思います:

NSArray *convertToAlpha = [NSArray arrayWithObjects:@"---",@"--x",@"-w-",@"--wx",@"r--",@"r-x",@"rw-",@"rwx", nil];

次に、NSFILEPOSIXPERMISSIONSをOctalに転倒した後、結果の数をコンポネット数字に分割し、ConvertToAlphaを使用して各数字をその英数字表現にマッピングしてください。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top