[fpc-pascal] FreePascal Windows - Force files to write to disk

James Richters james at productionautomation.net
Wed Mar 22 19:17:35 CET 2017


Thanks for the link and information on atomic writes.   It seems like a much better method.   My current scheme is to copy everything to a ramdrive upon boot.  Here I can do what I want with my files and if the machine crashes, the files I started with on the hard drive have not been affected.  Now for the tricky part, If I change a file, and want to save the change back to the hard drive in a way that is either 100% successful or 0% successful.   My thinking is that if I create the new file on my ram drive, and my program is still running, I can then perform the following sequence:

Copy the file to the hard drive with a temporary name
Flush all writes to above file
Delete previous backup file
Rename the original file to backup file name
Rename the new recently copied file to the original name

At this point I should have the current data with the original name and a backup of the previous data in case something goes wrong before the sequence completes.

I would like to do this without shell execute, and it seems my best bet is to use windows api calls instead of pascal functions.   I have FlushFileBuffers() working.  For the copy should I use MoveFileEx()   Should I use DeleteFile() to delete the previous backup and Rename() to rename the files or is there a better way?  Will I need to do something for commit the rename to disk?

Thanks for the help with this

James

-----Original Message-----
From: fpc-pascal [mailto:fpc-pascal-bounces at lists.freepascal.org] On Behalf Of Karoly Balogh (Charlie/SGR)
Sent: Wednesday, March 22, 2017 9:13 AM
To: FPC-Pascal users discussions <fpc-pascal at lists.freepascal.org>
Subject: Re: [fpc-pascal] FreePascal Windows - Force files to write to disk

Hi,

On Wed, 22 Mar 2017, Karoly Balogh (Charlie/SGR) wrote:

> However, please note that transactional file handling on power loss is 
> a quite delicate scenario, so you might not be able to solve all the 
> related problems with this - what happens exactly when you get a power 
> loss during a write, might be matter of pure luck.
>
> The usual way to work this problem around on Linux systems at least, 
> is to write a new file, then do an overwriting rename to the old file name.
> There rename is an "atomic" operation, which will be either committed 
> to the disk or not, but if it was unsuccessful, you won't end up with 
> half-written files. But IIRC Windows didn't support atomic renames. 
> Maybe someone with more Windows knowledge will fix me. You definitely 
> don't want to implement a copy though, and that's anything but atomic.

Actually, this blogpost which I've just found also details the algorithm how you should do it, from a Windows perspective:

https://blogs.msdn.microsoft.com/adioltean/2005/12/28/how-to-do-atomic-writes-in-a-file/

Charlie
_______________________________________________
fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal




More information about the fpc-pascal mailing list