Temporary files best practice?

  • I want to save several temporary files in NSTemporaryDirectory(). What is
    the best practice to name these files?

    Cheers,

    Knut
  • Knut,

    Try tmpnam(). For example:

    - (NSString*)temporaryFilePath
    {
      returrn [NSString stringWithUTF8String: tmpnam(nil)];
    }

    Enjoy!

    Peace, Alan

    --
    My blog: cocoalatte.info

    // Things I've said -------------------------
    "Maturity resides in the mind."
    "Silence is the Universe's greatest gift."
    "When the World realizes that personal beliefs are not something to
    argue or fight over, it shall evolve."
  • On Sep 2, 2007, at 5:55 AM, Knut Lorenzen wrote:

    > I want to save several temporary files in NSTemporaryDirectory().
    > What is
    > the best practice to name these files?
    >
    > Cheers,
    >
    > Knut

    I would recommend putting all of the files in a folder named
    according to reverse DNS: com.mydomain.myapp.  Then you can name your
    individual files whatever you want.

    -Jeff
  • What about using -[NSProcessInfo globallyUniqueString] to name your
    folder?

    Cheers,
        Andrew

    On Sep 2, 2007, at 3:55 AM, Knut Lorenzen wrote:

    > I want to save several temporary files in NSTemporaryDirectory().
    > What is
    > the best practice to name these files?
    >
    > Cheers,
    >
    > Knut
  • El 2/9/2007, a las 16:10, Andrew Merenbach escribió:

    > What about using -[NSProcessInfo globallyUniqueString] to name your
    > folder?
    >
    > Cheers,
    > Andrew

    Any method based on generating/getting a name and then testing for
    existence and creating a folder or file with that name has a race
    condition. See the mkstemp man page for a better solution:

          The mkstemp() function makes the same replacement to the
    template and
          creates the template file, mode 0600, returning a file
    descriptor opened
          for reading and writing.  This avoids the race between testing
    for a
          file's existence and opening it for use.

    Cheers,
    Wincent
  • Alan Smith schrieb dereinst (am 02.09.2007 14:42 Uhr) via
    <alanrogersmith...>:

    > Try tmpnam(). For example:
    >
    > - (NSString*)temporaryFilePath
    > {
    > returrn [NSString stringWithUTF8String: tmpnam(nil)];
    > }

    Alan,

    that's what I was looking for - problem solved. Thanks!

    Cheers,

    Knut
  • Jeff Johnson schrieb dereinst (am 02.09.2007 14:45 Uhr) via
    <publicposting...>:

    > I would recommend putting all of the files in a folder named
    > according to reverse DNS: com.mydomain.myapp.  Then you can name your
    > individual files whatever you want.

    Jeff,

    a neat trick that I'll remember. In this case I need unique filenames and
    will use Alan's suggestion, but thanks for your thoughts!

    Cheers,

    Knut
  • What about creating temporary folder subfolders, though?  Looking at
    the mkstemp man page, it doesn't sound as though mkstemp supports
    making folders.  If one wishes to write property-list-style data, the
    file descriptor from mkstemp() would be inadequate unless on wishes
    to convert to NSData, right?

    Best wishes,
        Andrew

    On Sep 2, 2007, at 7:34 AM, Wincent Colaiuta wrote:

    > El 2/9/2007, a las 16:10, Andrew Merenbach escribió:
    >
    >> What about using -[NSProcessInfo globallyUniqueString] to name
    >> your folder?
    >>
    >> Cheers,
    >> Andrew
    >
    > Any method based on generating/getting a name and then testing for
    > existence and creating a folder or file with that name has a race
    > condition. See the mkstemp man page for a better solution:
    >
    > The mkstemp() function makes the same replacement to the
    > template and
    > creates the template file, mode 0600, returning a file
    > descriptor opened
    > for reading and writing.  This avoids the race between testing
    > for a
    > file's existence and opening it for use.
    >
    > Cheers,
    > Wincent
    >
  • On Sep 2, 2007, at 1:10 PM, Andrew Merenbach wrote:
    > What about creating temporary folder subfolders, though?  Looking
    > at the mkstemp man page, it doesn't sound as though mkstemp
    > supports making folders.  If one wishes to write property-list-
    > style data, the file descriptor from mkstemp() would be inadequate
    > unless on wishes to convert to NSData, right?

    Look at mkdtemp() in the same man page.  It'll create a directory,
    and then you can put whatever you want in that directory.

    -Dave
  • On 02/09/07, Andrew Merenbach <merenbach...> wrote:
    > What about creating temporary folder subfolders, though?  Looking at
    > the mkstemp man page, it doesn't sound as though mkstemp supports
    > making folders.  If one wishes to write property-list-style data, the
    > file descriptor from mkstemp() would be inadequate unless on wishes
    > to convert to NSData, right?

    Not exactly hard, though, is it? You could try something like this
    (typed in Mail, no error handling, only for illustration purposes):

    NSString *errorString = nil;
    NSData *plistData = [NSPropertyListSerialization
    dataFromPropertyList:aPlist format:NSPropertyListBinaryFormat_v1_0
    errorDescription:&errorString];
    char *tmpNam = strcpy("/tmp/com.my.app.XXX.plist");
    int fd = mkstemps(tmpNam, 6);
    NSFileHandle *fh = [[NSFileHandle alloc] initWithFileDescriptor:fd
    closeOnDealloc:YES];
    [fd writeData:plistData];
    [fd release];
    free(tmpName);

    You could either keep the fd around or hold on to tmpNam if you want
    to reference the file later.

    -- Finlay
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