I encounter an issue after multiple files operations.
- I create a temporary file in the temporary folder with :File::createTempFile.
- I fill the temporary file with related FileOutputStream returned by createOutputStream()
- I Delete the FileOutputStream.
- Then I move the temporary file into another hard drive location with File::moveFileTo() (who can delete the target existing file if needed)
This works perfectly 99.90% of the time.
But one time, I find a target file with the correct size, but filled with zero.
Of course this can be related to hardware issue, but I prefer to investigate all the code to be sure of it.
First strange stuff that I found in last version of Juce code is that FileOutputStream destructor:
This one ensure to write the buffer and close the handle.
The function flush() should probably used instead of flushBuffer() before closeHandle()
FileOutputStream::flush() call flushInternal() who call Win32 FlushFileBuffers function:
According to MSDN FlushFileBuffers function Flushes the buffers of a specified file and causes all buffered data to be written to a file. This seems to be important when we open file with CreateFile without the FILE_FLAG_WRITE_THROUGH (as FileOutputStream does)
This according to Windows file caching: https://msdn.microsoft.com/en-us/library/windows/desktop/aa364218(v=vs.85).aspx
I do not know what’s going on if we do not do this if the file handle is closed just after a WriteFile call.
Another related thing is that FileOutputStream::flush() actually return void.
I think it can be secure to return a bool determined by the Result FileOutputStream::status set by the flushInternal() call.
Another related thing is that File::moveFileTo() rely on moveInternal() who rely on Win32 function MoveFile().
This function have a MoveFileEx friend who can actually take a MOVEFILE_WRITE_THROUGH flag who made this function does not return until the file is actually moved on the disk.
This let me think that MoveFile() is asynchronous and let dangerous behavior occurred when sending multiple file system operations sequentially.
Some people seems to have issue related to this http://stackoverflow.com/questions/6525943/cause-of-corrupted-file-contents.
To be sure of that I change the code to the MoveFileEx version.
Do you think all of this could be changed ?
Thanks in advance for your concern about all of this.
I’m running on Win64 pro SP1 us.
On SSD hard drive
With Juce build in 64bit with Visual Studio 2015 pro Update 3.