B. Dimitrov
B. Dimitrov

Reputation: 85

iOS: "open: Permission denied"

First of all excuse me for my english. This is my first question here. I have read a lot of solutions to both common and uncommon problems on stackoverflow and IMO stackoverflow's community is the most trustful in the branch.

Currently I am developing an iOS application and recently I've encountered a problem and after some research I didn't find solution. When I am using fopen to open existing file I am getting the following error:

open: Permission denied

The following is the code snipped which causes the error:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
BOOL isDir = NO;
NSError *error;
if (! [[NSFileManager defaultManager] fileExistsAtPath:cachePath
                                           isDirectory:&isDir] && isDir == NO) {

        [[NSFileManager defaultManager] createDirectoryAtPath:cachePath 
                                  withIntermediateDirectories:NO
                                                  attributes:nil
                                                       error:&error];
}        

NSString* filePath = [cachePath stringByAppendingPathComponent:
                                [NSString stringWithFormat:@"tmpmem%i.bin", count++]];

const char *cPath = [filePath cStringUsingEncoding:NSISOLatin1StringEncoding];

int file = open(cPath, O_RDWR | O_CREAT);

if (file == -1)
{
    perror("open");
    return NULL;
}

I am looking forward for your help. Thank you in advance.

Regards, B. Dimitrov

Upvotes: 3

Views: 4747

Answers (2)

B. Dimitrov
B. Dimitrov

Reputation: 85

Problem solved. I checked the posix permissions of the folder and they were 493, then I set them to 975 and everything is working as expected. One new question arised: what 9 means in posix permissions since the flags are 4 (Read), 2 (Write) and 1 (Execute)?

Upvotes: 0

Adam Zalcman
Adam Zalcman

Reputation: 27233

Most likely there is nothing wrong with your application. Common causes of this error include lack of search permission for a directory on the path to the file named by cPath, lack of permission to open the file for reading or writing (since you request both with O_RDWR) or the file doesn't exist and the permission to write to the directory in which it should be created is lacking (since you request file creation in case it doesn't exist with O_CREAT).

You should inspect the file and the pathname and ensure all necessary permissions are appropriately set.

Upvotes: 2

Related Questions