Created a new Directory for files but recieving error when trying to retrieve listing

Discussion in 'iOS Programming' started by AndyCodez, Jan 9, 2010.

  1. AndyCodez macrumors regular

    Aug 6, 2009
    I created a Directory for my application via code. I can write just fine to the new folder, but it is not allowing me to retrieve a listing of documents in the folder. aka contentsOfDirectoryAtPath

    This is the error i get when trying to get the contents:
    NSCocoaErrorDomain 257 {
    NSFilePath = "/var/mobile/Applications/13E92A6C-AB08-4D7F-A19C-D6C66BA2A67C/Documents/Audio";
    NSUnderlyingError = Error Domain=NSPOSIXErrorDomain Code=13 "Operation could not be completed. Permission denied";

    This is how i set up the folder to be created

    			NSMutableDictionary *directory = [[NSMutableDictionary alloc] init];
    			[directory setValue:[NSNumber numberWithBool:NO] forKey:NSFileAppendOnly];
    			[directory setValue:0 forKey:NSFileBusy];
    			[directory setValue:[NSDate date] forKey:NSFileCreationDate];
    			[directory setValue:@"User" forKey:NSFileOwnerAccountName];
    			[directory setValue:@"Users" forKey:NSFileGroupOwnerAccountName];
    			[directory setValue:[NSNumber numberWithInt:122] forKey:NSFileDeviceIdentifier];
    			[directory setValue:0 forKey:NSFileExtensionHidden];
    			[directory setValue:[NSNumber numberWithLong:23432] forKey:NSFileGroupOwnerAccountID];
    			//[directory setValue:[NSNumber numberWithLong:3342] forKey:NSFileHFSCreatorCode];
    			//[directory setValue:[NSNumber numberWithLong:3423] forKey:NSFileHFSTypeCode];
    			[directory setValue:[NSNumber numberWithBool:YES] forKey:NSFileImmutable];
    			[directory setValue:[NSDate date] forKey:NSFileModificationDate];
    			[directory setValue:[NSNumber numberWithLong:89798] forKey:NSFilePosixPermissions];
    			[directory setValue:[NSNumber numberWithLong:234] forKey:NSFileReferenceCount];
    			[directory setValue:NSFileSystemFreeSize forKey:NSFileSize];
    			[directory setValue:[NSNumber numberWithLong:5] forKey:NSFileSystemFileNumber];
    			[directory setValue:NSFileTypeDirectory forKey:NSFileType];
    			[fs createDirectoryAtPath:_documents_folder_audio withIntermediateDirectories:NO attributes:directory error:&err];
    Just wondering if someone sees something I missed when setting this new folder up.

    I know some of the values are not what they "should" be, I looked at the documentation on the apple site and it did not do too good of a job explaining how to get these values.
  2. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    What is _documents_folder_audio and what's its value?
  3. Guiyon macrumors 6502a

    Mar 19, 2008
    North Shore, MA
    [directory setValue:[NSNumber numberWithLong:89798] forKey:NSFilePosixPermissions];
    89798 doesn't correspond to any set of POSIX permissions I've ever seen.

    89798 is equal to octal 257306. The lower-level code code is grabbing the lowest 3 or 4 triplets (sticky bits!) which, in this case, is 011 000 110 or 0306. This means you'll be setting the permissions to:
    Owner -> -wx (which is why you can write but not read)
    Group -> ---
    World -> rw-

    The easiest thing would be to just specify the actual permissions you want in octal instead of decimal (ie 0755, 0666, etc). I'm relatively sure just doing the following will yield the right behavior:
    [fs     createDirectoryAtPath: _documents_folder_audio
      withIntermediateDirectories: NO
                       attributes: nil
                            error: &err];
    Also, you probably shouldn't mess with the following keys unless you REALLY know what you're doing. The system most likely has safeguards to prevent the setting of bad/conflicting values but I'd be wary just the same, you could run into some nasty undefined (or unexpected!) behavior:
    NSFileHFSCreatorCode //Useless on a folder
    NSFileHFSTypeCode //Useless on a folder
  4. AndyCodez thread starter macrumors regular

    Aug 6, 2009
    _documents_folder_audio is just a string with the path that I want it to be created. That works fine. But I will look how Guiyon said to do it, I wasn't to sure about the values to be specified for some of the keys. The documentation did not go into that much detail as how to specify these things correctly.

    - Edit: Guiyon was right just running it without the attributes worked. Thank you! I feel like an idiot lol. I couldn't even delete the one I had previously created, I imagine because I screwed up the permissions on it.

Share This Page