I need to be able to run the file using code that works great, but to make sure it succeeds, I must first set the executable bit in the file. I am currently running chmod +x via NSTask, but this seems awkward:
NSString *script = @"/path/to/script.sh"; // At this point, we have already checked if script exists and has a shebang NSFileManager *fileManager = [NSFileManager defaultManager]; if (![fileManager isExecutableFileAtPath:script]) { NSArray *chmodArguments = [NSArray arrayWithObjects:@"+x", script, nil]; NSTask *chmod = [NSTask launchedTaskWithLaunchPath:@"/bin/chmod" arguments:chmodArguments]; [chmod waitUntilExit]; }
Any suggestions? I was not lucky to find code examples, and the only other option is NSFileManager setAttributes:ofItemAtPath:error: with the NSFilePosixPermissions attribute. I will do POSIX read and write logic if I need it, but wanted to see if there is a more elegant method there.
source share