check if a file path is useable

  • Hello,

    I am writing a program where I need to be able to read/write a file
    to a specific directory. I have been able to check if a file is
    readable and writable using the access() function. The problem is
    that I want to make it so that if the file does not already exists I
    can see if it can be created. I am stuck here, since I'm not sure of
    an easy way to get this information. Is there a single method to do
    all this, and if not how can I create a method to do all of this?

    Thanks,
    Mitchell Livingston
  • On Sep 3, 2007, at 10:46 AM, Mitchell Livingston wrote:

    > Hello,
    >
    > I am writing a program where I need to be able to read/write a file
    > to a specific directory. I have been able to check if a file is
    > readable and writable using the access() function. The problem is
    > that I want to make it so that if the file does not already exists
    > I can see if it can be created. I am stuck here, since I'm not sure
    > of an easy way to get this information. Is there a single method to
    > do all this, and if not how can I create a method to do all of this?

    NSFileManager?

    -Shawn
  • Yes, I have been looking at this, but I don't see a way to check if I can simply create a file at a path without actually creating it. Creating a file to check if it's doable seems like a bit too much overhead and a bit messy. I don't see a way to use isWritableFileAtPath: and the others without the file already existing.


    On Monday, September 03, 2007, at 01:52PM, "Shawn Erickson" <shawnce...> wrote:
    >
    > On Sep 3, 2007, at 10:46 AM, Mitchell Livingston wrote:
    >
    >> Hello,
    >>
    >> I am writing a program where I need to be able to read/write a file
    >> to a specific directory. I have been able to check if a file is
    >> readable and writable using the access() function. The problem is
    >> that I want to make it so that if the file does not already exists
    >> I can see if it can be created. I am stuck here, since I'm not sure
    >> of an easy way to get this information. Is there a single method to
    >> do all this, and if not how can I create a method to do all of this?
    >
    > NSFileManager?
    >
    > -Shawn
    >
    >
  • On 3 sep 2007, at 19.56, Mitchell Livingston wrote:

    > Yes, I have been looking at this, but I don't see a way to check if
    > I can simply create a file at a path without actually creating it.
    > Creating a file to check if it's doable seems like a bit too much
    > overhead and a bit messy. I don't see a way to use
    > isWritableFileAtPath: and the others without the file already
    > existing.

    You can create new files if you have write access to the parent folder.

    j o a r
  • On 03.09.2007, at 19:56, Mitchell Livingston wrote:
    > Yes, I have been looking at this, but I don't see a way to check if
    > I can simply create a file at a path without actually creating it.
    > Creating a file to check if it's doable seems like a bit too much
    > overhead and a bit messy. I don't see a way to use
    > isWritableFileAtPath: and the others without the file already
    > existing.

      Considering you will be creating the file afterwards and this is a
    multi-tasking OS, it's the only safe way to do this check, though.
    Between your calls to isWritableFileAtPath: on the parent folder and
    the time you actually create your file, aqnother app may be swapped
    in by the schedules and may change these permissions, or someone who
    has mounted the destination volume over the network may have changed
    permissions.

      So, isFileWritableAtPath: is mainly useful if you want to avoid the
    user selecting a completely unsuitable place, i.e. you can use it to
    restrict the UI, or better to show a warning to the user when they
    try to select a certain destination folder. But the actual error
    handling has to be when you create the file.

    Cheers,
    -- M. Uli Kusterer
    http://www.zathras.de
previous month september 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today