Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform chmod() in Objective C

What's the equivalent of C's:

 chmod(MY_FILE, 0777);

in Objective C? I am trying to write to an existing locked file without performing

chmod +x MY_FILE

on the terminal.

like image 469
newbieMACuser Avatar asked Dec 15 '14 05:12

newbieMACuser


People also ask

How to chmod a file?

To change the file permissions using chmod, run chmod <permission> <directory or filename> , swapping in the desired file permissions and the directory or file. The owner can change file permissions for any user, group or others by adding - to remove or + to add certain permissions.

What does chmod does in Linux?

The chmod (short for change mode) command is used to manage file system access permissions on Unix and Unix-like systems. There are three basic file system permissions, or modes, to files and directories: read (r) write (w) execute (x)


2 Answers

You can use Cocoa's -setAttributes:ofItemAtPath:error: to do this job.

[[NSFileManager defaultManager] setAttributes:@{ NSFilePosixPermissions : @0666 }
                                 ofItemAtPath:… 
                                        error:&error];

Of course you need the rights to do that.

like image 177
Amin Negm-Awad Avatar answered Sep 30 '22 23:09

Amin Negm-Awad


You can use C's chmod().

Enter man 2 chmod in the terminal for the documentation and related functions.

like image 23
justin Avatar answered Sep 30 '22 23:09

justin