Reputation: 983
I have a component that writes his structure to a file. The problem is that the data that he writes needs to be consistent at any system failure. So I need to physical write the data. The problem is that the component doesn't have such an option and to make a filesaver function from component data will take some time and will complicate the program.
The question is: If I wrote the data with component (ex: ComponentX->WriteToFile(filename) ) can I then use Handle = OpenFile(filename) and then FlushFileBuffers(Handle) to ensure the consistency of the data? Or this trick will not work?
I through that this may work because maybe at OpenFile the system uses the handle already in cache and flushing it will result in saving the cached data from the previous operation (component file save) but I'm not sure.
If this might not work is there any other method rather than making the data from the component by my self (with CreateFile, ...)
Upvotes: 1
Views: 3051
Reputation: 5548
No flushing is going to fix your problem. What if system freezes while buffers are flushed?
Multiple solution exist. You can write to 2 (or more) alternating files, for one: you know that if one is corrupt, the previous one will be likely well (barring some ugly freeze during recovery scenarios). This works good if your checkpoint files are small. Writing a checksum with the data helps avoiding doubt when recovering.
Opening the file with FILE_FLAG_NO_BUFFERING
mandates writing directly to disk, avoiding system write-through cache. This works probably better in concert with the previous option, as the system may still fail during a checkpoint, mangling the whole file.
Next, Windows has a performant and easy to use transactional file system.
Finally, if you have no access to the way the checkpoints are written, just set some backup routine so save the checkpoint file to another drive between checkpoints. If the file is lagre, you might use some hot backup programs, available out there for $0 to under $100 in abundance. Windows Robocopy utility may well fit into your requirements here.
Upvotes: 1