alDiablo
alDiablo

Reputation: 979

Write to a file Xcode MacOS

I'm unable to run this piece of code which writes the pid to a file.

NSString *text = [NSString stringWithFormat:@"%d\n", getpid()];
NSError *error = nil;

if (![text writeToFile:@"/tmp/Frontend.pid" atomically:YES encoding:NSUTF8StringEncoding error:&error]) {
    NSLog(@"Cannot write PID file %@: %@", @"/tmp/Frontend.pid", error);
    return NO;
}

This is the error I get.

2017-11-13 20:19:18.742171+0530 TestThread[7648:273326] Cannot write PID file /tmp/Frontend.pid: Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “Frontend.pid” in the folder “tmp”." UserInfo={NSFilePath=/tmp/Frontend.pid, NSUnderlyingError=0x6040000432a0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

I've changed the permission for the directory to 777 and it still doesn't have enough permissions. Please help.

Upvotes: 3

Views: 3726

Answers (4)

RickJansen
RickJansen

Reputation: 1733

Got the same error (macOS 10.15.7 Catalina), my sandbox appears to be ok (User selected file Read/Write), still the error. As direct writing from an NSData object is not really "user selected" I decided to use an NSSavePanel in between. That worked fine. Used this to save an NSImage as tiff:

NSSavePanel *savePanel = [NSSavePanel savePanel];
savePanel.title = @"Save…”;
savePanel.message = @"Saves the …”;
savePanel.allowedFileTypes = @[ @"tiff" ];
savePanel.nameFieldStringValue = @“image”;
NSModalResponse result = [savePanel runModal];
if (result == NSModalResponseOK) {            
    NSError *error;
    NSData *tiffData = [anImage TIFFRepresentation];
    BOOL success = [tiffData writeToURL:savePanel.URL options:NSDataWritingAtomic error:&error];
    if (!success) NSLog( @"%@", error);
}

Upvotes: 0

Dx_
Dx_

Reputation: 1576

Just to complement. On XCode10 I changed the entitlements as @alDiablo suggested.

Go to your Target, and on Capabilities, enable the User Selected File as Read/Write.

enter image description here

Thanks!

Upvotes: 2

alDiablo
alDiablo

Reputation: 979

Well, I finally fixed it. Here is the default configuration of a new project in xcode which ruined the whole file io thing. I turned off these 2 knobs in the entitlements file and was good to go.

${project_name}.entitlements

Upvotes: 8

greymouser
greymouser

Reputation: 3181

/tmp is a symlink to /private/tmp on macOS. Did you change the permissions of the link, or of the destination?

For example, on my machine I see the following:

$ ls -ld /tmp
lrwxr-xr-x@ 1 root  wheel  11 Oct 14 14:55 /tmp -> private/tmp
$ ls -ld /private/tmp
drwxrwxrwt  16 root  wheel  512 Nov 13 06:43 /private/tmp

This means that only root can modify the link, but anyone should be able to write to the directory. Indeed, I can write files in that directory just fine. Because of the t sticky bit, only users that own the file itself in /tmp (or root or wheel) can delete/rename the file.

I would check that you don't already have a file at /tmp/Frontend.pid with permissions set in such a way that it cannot be overwritten (writeToFile:atomically:encoding:error will overwrite existing files).

Upvotes: 0

Related Questions