How to execute chmod () in Objective-C

Which is equivalent to C:

 chmod(MY_FILE, 0777);

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

chmod +x MY_FILE

on the terminal.

+4
source share
3 answers

You can use the C chmod().

Enter man 2 chmodinto the terminal for documentation and related functions.

+3
source

You can use Cocoa -setAttributes:ofItemAtPath:error:to complete this task.

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

Of course, you need the rights to it.

+5
source

, cocoa.

NSTask *changePerms = [[NSTask alloc] init];
[changePerms setLaunchPath:@"/bin/chmod"];
NSArray *chmodArgs = [NSArray arrayWithObjects:@"666", @"/Users/abc/hello.txt", nil]; 
[changePerms setArguments:chmodArgs];
[changePerms launch];

NSFileManager

NSDictionary* attr = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithShort:0766], NSFilePosixPermissions, NULL];

NSError *error = nil;
[[NSFileManager defaultManager] setAttributes:attr ofItemAtPath:@"/Users/abc/Desktop/test.txt" error:&error];

system()

system("chmod 777 /Users/abc/Desktop/test.txt");

, !

-1

All Articles