What could cause an XML file to be filled with null characters?

前端 未结 5 1513
忘掉有多难
忘掉有多难 2020-12-28 18:01

This is a tricky question. I suspect it will require some advanced knowledge of file systems to answer.

I have a WPF application, \"App1,\" targeting .NET framework

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-28 18:45

    It's well known that this can happen if there is power loss. This occurs after a cached write that extends a file (it can be a new or existing file), and power loss occurs shortly thereafter. In this scenario the file has 3 expected possible states when the machine comes back up:

    1) The file doesn't exist at all or has its original length, as if the write never happened.

    2) The file has the expected length as if the write happened, but the data is zeros.

    3) The file has the expected length and the correct data that was written.

    State 2 is what you are describing. It occurs because when you do the cached write, NTFS initially just extends the file size accordingly but leaves VDL (valid data length) untouched. Data beyond VDL always reads back as zeros. The data you were intending to write is sitting in memory in the file cache. It will eventually get written to disk, usually within a few seconds, and following that VDL will get advanced on disk to reflect the data written. If power loss occurs before the data is written or before VDL gets increased, you will end up in state 2.

    This is fairly easy to repro, for example by copying a file (the copy engine uses cached writes), and then immediately pulling the power plug on your computer.

提交回复
热议问题