Re: Temporary files
Re: Temporary files
- Subject: Re: Temporary files
- From: Ryan Britton <email@hidden>
- Date: Tue, 10 Oct 2006 09:14:00 -0700
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