Saving / Loading from other folder

Discussion in 'Mac Programming' started by DennisBlah, Sep 22, 2014.

  1. DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #1
    Hi all,

    I'm working on a little app for OSX. Which will be runned at startup for all users. However, it uses a plist file. I would like to read and write the plist file to a different folder.

    Right now it writes to the documents folder of current user. I need to to be written to: /Users/mainAccount/Public

    So all other users will be using the same file.

    Code:
    fileManager = [NSFileManager defaultManager];
        paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        documentsDirectory = [paths objectAtIndex:0];
        path = [documentsDirectory stringByAppendingPathComponent:@"autoUpdater.plist"];
    
    How can I do this properly?
     
  2. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #2
    I would do some research with this method stringByExpandingTildeInPath:
     
  3. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #3
    An app that runs for all users at startup? Are you sure about that? Why would anyone allow such an app to run, especially for all users of the computer? You should think very hard about what you are doing there. There is no way to get that kind of app accepted on the App Store.
     
  4. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #4
    Did I mention its for the appstore? Its for internal use only. Im using it as a launchagent wich will connect to a fileshare check the files and compare the modification datetime with the data i have in the plist.

    However right now I made it so it will copy the file from the shared user folder to documents to edit it and then copy back to shared (with shell commands)
    But its a workaround for what I would be needing.
     
  5. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #5
    Can you give your program the right to write to /Library/Preferences/? That would be the appropriate place to put a global plist.
     
  6. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #6
    That sounds promising. The thing is.. How do I load it from there?
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    In your posted code fragment, you have a 'documentsDirectory' variable that contains the pathname of the directory holding your intended file. So instead of doing the things that lead up to that documentsDirectory, you use the other path instead.

    Your code:
    Code:
    fileManager = [NSFileManager defaultManager];
        paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        documentsDirectory = [paths objectAtIndex:0];
        path = [documentsDirectory stringByAppendingPathComponent:@"autoUpdater.plist"];
    
    Example using /Library/Preferences/:
    Code:
    documentsDirectory = @"/Library/Preferences";
    path = [documentsDirectory stringByAppendingPathComponent:@"autoUpdater.plist"];
    
    That's more complex than necessary, however. You can do the same thing much more simply, because both the directory and the last path component are literal strings:
    Code:
    path = @"/Library/Preferences/autoUpdater.plist";
    

    Note that this code still doesn't address the question of permissions. Specifically, the directory "/Library/Preferences" doesn't have public-write permission, so no one except authenticated admin users can write there. So this may not be the solution you're looking for.

    There's another folder, "/Users/Shared" that DOES have public-write access. It also has the sticky-bit, which you should read about:
    https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man8/sticky.8.html

    Also be aware that whichever user last created the file will be its owner. And know that the default permissions on a newly created file will deny write permission to anyone except the file's owner. So you should look at the NSFileManager capability that reads and writes the permissions of the file (filePosixPermissions).

    Finally, sharing a file that's writable by any user will have security implications. Since you didn't say what the program does, there's no way to examine or evaluate those security implications.
     
  8. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #8
    Hi chown

    Thanks for the help.

    Ill explain what my app does.
    First it pings the fileshare for reachability.
    When reachable i mount it to a hidden folder and make a list if the files in there (except folders)
    Based on ip range one of the subfolders is listed as well.

    The thing I use the plist for is checking if I already copied these files based on last modification date. All .app files will be copied to /applications and all pkg files installed and other files copied to current user desktop

    With the plist I make a crosscheck of all. Skip those that are installed and those which current user already received according the plist.

    Makes a nice table with files and status. Pending, copying, installing error while copying or installing, already up to date

    And closes itself when done

    The outrolled version right now uses applescript to copy the file from shared to current user documents and change permissions
    After editing it will be copied back.

    Only 2 problems I have now, one just solved thanks to you, second is i cant determine users department if they are on wifi. So I skip this folder.

    Hopefully anyone found this interesting to read ;) but now it should be clear what Im working on :)

    Cheers! And thanks again :)
    Never thought that browsing files wud be easy as that :$
     

Share This Page