Domanda

c'è un modo per ottenere stringa leggibile (@ "drwxr-xr-x", per esempio) da un intero NSFilePosixPermissions?

È stato utile?

Soluzione

L'attributo autorizzazioni di sistema di file è semplicemente un valore unsigned long. Il codice di seguito potrebbe ovviamente essere reso più efficiente ma mostra [più o meno] ciò che deve essere fatto per ottenere la stringa che si desidera:

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

Altri suggerimenti

Bene Credo che si può creare una matrice in questo modo:

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

Poi, dopo le tranlating NSFilePosixPermissions a ottale, diviso il numero risultante nelle sue cifre componenet e utilizzare convertToAlpha per mappare ogni cifra per la sua rappresentazione alfanumerica ....

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top