Stored Files Once Contained Only NUL Characters

We have a problem in our Windows 8.1 (WinRT) application, which sometimes leads to damage to our saved file. Files have the correct file size, but the file contains only NUL characters. The file must contain a serialized object as XML.

In an attempt to find the problem, we do not overwrite the file, we do the following:

  • Serialize the current object in a temporary file.
  • Check the contents of the temporary file
  • Copy the current file (in .timestamp.xml.bak)
  • Move / replace temp file with current file

In most cases, all this works fine, but sometimes the .timestamp.xml.bak file and the content file get corrupted. In addition, the log file is corrupted (also only NUL characters). The entire file consists of NUL characters. When I look at the trail of bak files and the main file, I see that the main file is enlarged. This should be correct because a new XML element has been added. But it does not contain XML.

I have no clue how and why this is happening. This occurs in approximately 5% of the files that need to be edited, and each damaged file occurs after 5-20 attempts to save. This also happens on several tables.

Here is a piece of code that creates corrupted files:

StorageFile file = await lDataFld.CreateFileAsync(filename + ".tmp",  CreationCollisionOption.OpenIfExists);

StorageFile oldFile = await dataFld.GetFileAsync(filename + ".xml");
if (oldFile != null)
{
await oldFile.CopyAsync(dataFld, string.Format("{0}.{1}.xml.bak", filename, DateTime.Now.ToString("yyyyMMddHHmmssfffffff")), NameCollisionOption.ReplaceExisting);
}
await file.MoveAndReplaceAsync(await dataFld.GetFileAsync(filename + ".xml"));

Logger.Log(string.Format("Saved {0}.", filename));

Can someone tell me how we ended up with files containing only NUL characters and how / why this happens? And even better, how can this be fixed.

: - , .

+4

All Articles