简体   繁体   English

如何编写文件然后将其读回以验证其内容,确保您获得磁盘上的内容而不是缓存

[英]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. 我在Windows上使用本机/ C ++ / Win32 / MFC代码通过MFC序列化保存文档文件。 I've inserted my own CFile-derived class in the writing process giving me access to the data as its being written. 我在写入过程中插入了自己的CFile派生类,使我能够在写入数据时访问数据。 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? 如果是这样,有什么办法可以避免这个问题吗?

If you are using CFile , you can call CFile::Flush to ensure everything is written to disk. 如果您使用的是CFile ,则可以调用CFile :: Flush以确保所有内容都写入磁盘。 According to the documenatation 根据文件说明

virtual void Flush( );

Forces any data remaining in the file buffer to be written to the file 强制将文件缓冲区中剩余的任何数据写入文件

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. 如果您确实想这样做,那么您可以通过在打开文件时指定FILE_FLAG_NO_BUFFERING和/或FILE_FLAG_WRITE_THROUGH来避免磁盘缓存和缓冲。 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 . 使用FILE_FLAG_NO_BUFFERING标志成功处理使用CreateFile打开的文件有严格的要求,有关详细信息,请参阅文件缓冲

A simpler alternative is to call FlushFileBuffers just before you close the file handle. 更简单的替代方法是在关闭文件句柄之前调用FlushFileBuffers

I do not know the answer to this question. 我不知道这个问题的答案。 However, I do know where to look. 但是,我知道在哪里看。

SQLite guarantees that data is safely written to disk, no matter what happens - even a power failure. SQLite保证数据安全地写入磁盘,无论发生什么 - 甚至是电源故障。 They must be doing what you need, their code is open source and beautifully commented. 他们必须做你需要的,他们的代码是开源的和精美的评论。

All changes within a single transaction in SQLite either occur completely or not at all, even if the act of writing the change out to the disk is interrupted by SQLite中单个事务中的所有更改要么完全发生,要么根本不发生,即使将更改写入磁盘的操作被中断也是如此

a program crash, 程序崩溃,

an operating system crash, 操作系统崩溃,

or a power failure. 或电源故障。

The claim of the previous paragraph is extensively checked in the SQLite regression test suite using a special test harness that simulates the effects on a database file of operating system crashes and power failures. 在SQLite回归测试套件中使用特殊的测试工具对模型对操作系统崩溃和电源故障的数据库文件的影响进行了广泛的检查。

http://sqlite.org/transactional.html http://sqlite.org/transactional.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM