Re: How can I do a 100% bullet proof 'Save'?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Johan Rosengren (johan.rosengren_at_telia.com)
Date: 09/23/04


Date: Thu, 23 Sep 2004 18:19:44 +0200

Simon,

Onr thought that comes to mind is not deleting the original file, but
renaming it instead. If the renaming of the new file went ok, it could be
deleted (or kept as a backup). Swapping names as an atomic operation seems
like an impossibility :-)

Johan Rosengren
Abstrakt Mekanik AB

"Simon" <sorde@gotadsl.co.uk> a écrit dans le message de
news:4152d4cc$0$10791$65c69314@mercury.nildram.net...
> I have an app that saves to a file. I want my save to be a 100% safe -
i.e.
> if the save fails for any reason, the file on disk is left completely
> unchanged. So this is what I do:
>
> I get Windows to generate a temporary file name for me in the destination
> folder and I save to that file.
> If the save succeeds, I delete the old file and rename my saved file to
give
> it the old name.
>
> If anything fails at any point, I leave the old file alone and tell the
user
> that there's a problem.
>
> That has worked very well up to now, but I have just had a user who got an
> error message which could only happen if (a) the initial save had
succeeded,
> (b) the old file had been successfully deleted and (c) the attempt to
rename
> the saved file (with the temp name) to use the old name, had failed.
>
> I have no idea how that could have failed, but apparently it did. In fact
> it wasn't disastorous because my program left the saved file where it was
> with its temp name, and put out a detailed error message explaining what
had
> happened, and advising the user to manually rename this file herself-
which
> she did without problems. But I would like to be sure that nothing like
> that could ever happen again. So does anyone have any suggestions -
either:
> how this could have happened (and how I could have prevented it)? Or
> perhaps suggestions of an alternative approach that would be absolutely
> bulletproof. It occurred to me that what I could really do with is a
> "SwapNames" function which would swap the 2 file names as an atomic
> transaction, and be incapable of failing with the job partly done. But I
> don't know of any such feature in the Win32 API. Anyway, all suggestions
> welcome.
>
> Simon
>
>



Relevant Pages

  • How can I do a 100% bullet proof Save?
    ... I delete the old file and rename my saved file to give ... If anything fails at any point, I leave the old file alone and tell the user ... the saved file (with the temp name) to use the old name, ...
    (microsoft.public.vc.mfc)
  • Re: Infinity......
    ... I cannot rename all of A elements by replacing ... the same absolute size, then the absolute size of A ... Yes, there is a way of renaming the elements of B, ... if there is a bijection between sets A and B, ...
    (sci.math)
  • Acritum Sophisticated Rename 3.00
    ... Sophisticated Rename is a powerful Windows utility that simplifies the ... You specify the rules for a renaming task ... Exif editor can conduct various Exif date and time manipulations, ... Exif or ID3 tag into the renaming template. ...
    (comp.software.shareware.announce)
  • Re: =?iso-8859-5?b?3NDZ4eLe4A==?= - =?iso-8859-5?b?5N7i3tPg0OTY4tU=?=
    ... directories, based upon flexible criteria. ... can quickly rename them all to "htm" or other. ... also add incrementing numbers to the file name, add a suffix or prefix, ... different renaming methods and also supports MP3 tags. ...
    (soc.culture.bulgaria)
  • Re: Something was missing at the beginning of renaming
    ... > Something was missing in my previous message about renaming. ... > If you need to batch rename and number, you can do this easily with XP. ... > Copy the photos to a new folder. ...
    (microsoft.public.windowsxp.photos)