Laurent Crivello
Laurent Crivello

Reputation: 3931

Setting permissions in Cocoa

I want to set permissions on files with Cocoa using the following code:

permissions=0644;
attr = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt: permissions] forKey:NSFilePosixPermissions];
[fileManager setAttributes:attr ofItemAtPath:filename error:nil];

This works fine. However my 'permissions' variable is an integer that I calculate and is therefore 644 instead of 0644, and in this case fails. How can I convert an integer being 644 to 0644 ?

Thanks.

Upvotes: 0

Views: 203

Answers (1)

Ken Thomases
Ken Thomases

Reputation: 90531

Why do you calculate it that way? 0644 is an octal number. It's equivalent to 420 in decimal (base 10). If you computed 644 when you meant 0644 a.k.a. 420, then your computation is incorrect.

If you're computing permissions masks, it's best to use the masks defined in <sys/stat.h>, such as S_IRUSR and S_IXOTH.

Upvotes: 2

Related Questions