Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a file then read it back to verify its contents, ensuring you're getting what's on the disk not the cache

I'm using native/C++/Win32/MFC code on Windows to save a document file via MFC serialization. I've inserted my own CFile-derived class in the writing process giving me access to the data as its being written. This allows me to compute a checksum (or hash, etc) on the data as its going out to the file.

After the files has saved, I'd like to allow the option of verifying the file. The idea would be to re-open the file and read through it verifying the checksum/hash/etc.

I'm wondering, though, if its possible that after having just written the file, the OS could be giving me unwritten data when I read the file back right away. In this case, the test doesn't really tell me that the file looks good on the disk.

Is my concern valid? If so, is there any way to avoid this issue?

like image 840
Nerdtron Avatar asked Jan 10 '12 15:01

Nerdtron


2 Answers

If you are using CFile, you can call CFile::Flush to ensure everything is written to disk. According to the documenatation

virtual void Flush( );

Forces any data remaining in the file buffer to be written to the file

like image 179
parapura rajkumar Avatar answered Sep 30 '22 13:09

parapura rajkumar


If you really want to do this then you can avoid disk caching and buffering by specifying FILE_FLAG_NO_BUFFERING and/or FILE_FLAG_WRITE_THROUGH when opening the file. Beware that using these options will complicate things.

The file or device is being opened with no system caching for data reads and writes. This flag does not affect hard disk caching or memory mapped files. There are strict requirements for successfully working with files opened with CreateFile using the FILE_FLAG_NO_BUFFERING flag, for details see File Buffering.

A simpler alternative is to call FlushFileBuffers just before you close the file handle.

like image 40
David Heffernan Avatar answered Sep 30 '22 12:09

David Heffernan