• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Temporary files
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Temporary files


  • Subject: Re: Temporary files
  • From: John Stiles <email@hidden>
  • Date: Tue, 10 Oct 2006 11:03:41 -0700

There are bigger problems with this snippet than a rare race condition with tempnam:
- This code specifies ASCII encoding, but it's possible that paths may contain UTF8 characters—and in fact it's likely on non-American Macs. Remember, folks, path representations are UTF8, not ASCII!
- This code deletes the original file before moving the new file over. This is risky, and in the worst case it could lead to the old file being erased but the new file being left in chewable and destroyed. It would be better to do an atomic swap if possible, or use the UNIX rename command (which will safely remove a file at the destination path).



On Oct 10, 2006, at 10:54 AM, Nir Soffer wrote:

tempnam is not secure, use mkstemp instead.

On Oct 10, 2006, at 6:14 PM, Ryan Britton wrote:

As far as I know there is no built-in functionality for this. Something like this should work though:

//Create a temporary path
NSString *containingDir = [aPath stringByDeletingLastPathComponent];
char *tempNam = tempnam([containingDir fileSystemRepresentation], NULL);

if (tempNam == NULL)
{
return NO;
}

NSString *tempFileName = [NSString stringWithCString:tempNam encoding:NSASCIIStringEncoding];
free(tempNam);
if (tempFileName == nil)
{
return NO;
}

//Save the file

//Remove old and rename temp file to new
[[NSFileManager defaultManager] removeFileAtPath:aPath handler:nil];
[[NSFileManager defaultManager] movePath:tempFileName toPath:aPath handler:nil];



On Oct 10, 2006, at 8:49 AM, Martin Hairer wrote:

Hello, I was just wondering whether there is a simple Cocoa-ish way of
performing the following: given a path (say pathA) to a file (existing or not),
I would like to get a path (say pathB) with the following properties:


1. pathB should not point to an existing file.
2. pathB should point into a temporary / crushable files folder.
3. (Most important) if a file is written to pathB, it should be possible to move
it to pathA without involving a file copying operation.


There are two approaches that come to mind:

1. Take pathB to point into the same directory as pathA. This doesn't conform to
point 2 and so there could be files left littering the disk if the application happens
to crash before it got the chance to delete the file at pathB.
2. Check on which volume pathA is located and use the Folders API to locate
the temporary files folder on that volume. This should solve the problem, but
somehow doesn't sound like a very clean way of doing it. Also, will this behave
as expected (i.e. conform to point 3 above) if pathA is on a networked volume
or inside a directory structure that was mounted "by hand" in a non-standard
location?


Cocoa does something like this when saving a file (i.e. it really saves it to pathB
and then deletes the file at pathA and swaps the two files) so my question is probably
whether there's a supported way to tap into this functionality without having to
reimplement it. Regards,


Martin

HairerSoft
http://www.hairersoft.com/


_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden

_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden


Best Regards,

Nir Soffer

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40blizzard.com


This email sent to email@hidden

_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: Temporary files
      • From: Jim Correia <email@hidden>
References: 
 >Temporary files (From: Martin Hairer <email@hidden>)
 >Re: Temporary files (From: Ryan Britton <email@hidden>)
 >Re: Temporary files (From: Nir Soffer <email@hidden>)

  • Prev by Date: Re: Temporary files
  • Next by Date: Fwd: Setting NSTextView <--Field
  • Previous by thread: Re: Temporary files
  • Next by thread: Re: Temporary files
  • Index(es):
    • Date
    • Thread