2010-11-08 15 views
6

hay una manera de conseguir cadena legible por humanos (@ "drwxr-xr-x", por ejemplo) a partir de un número entero NSFilePosixPermissions?Objective-C NSFilePosixPermissions a NSString legible por humanos

+0

Nop. Completamente imposible sin el Talisman Unicornio y un montón de licor. (Véase operaciones bit a bit: http://en.wikipedia.org/wiki/Bitwise_operation) –

+0

(Up-votó la cuestión, ya que es una buena y estoy siendo un smart-A **.) :-) Gracias Joshua –

+0

! ¡la respuesta aceptada parece estar bien! – Vassilis

Respuesta

4

El sistema de archivos permisos atributo es simplemente un valor de largo sin signo. El código siguiente, obviamente, podría hacerse más eficiente, pero se nota [más o menos] lo que hay que hacer para obtener la cadena que desea:

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

Bueno, yo supongo que se puede crear una matriz de este modo:

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

Luego, después de tranlating los NSFilePosixPermissions a octal, dividir el número resultante en sus dígitos componenete y utilizar convertToAlpha para mapear cada dígito a su representación alfanumérica. ...

+0

xm ... Lo intentaré. Volveré si lo hago, para publicar toda la solución. ¡Gracias! – Vassilis

+0

@VassilisGr, @ennuikiller: ten cuidado, necesitas un símbolo '@' detrás de cada cadena, y también necesitas agregar 'nil' a tu lista de argumentos. – dreamlax

+0

@dreamlax, ¡gracias por las correcciones! – ennuikiller

Cuestiones relacionadas