Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does SetFileValidData doing ? what is the difference with SetEndOfFile?

Tags:

file

windows

I look for a way to extend a file asynchronously and efficiently .

In a support document Asynchronous Disk I/O Appears as Synchronous on Windows NT, Windows 2000, and Windows XP said:

NOTE: Applications can make the previously mentioned write operation asynchronous by changing the Valid Data Length of the file by using the SetFileValidData function, and then issuing a WriteFile.

in MSDN, SetFileValidData is a function for Sets the valid data length of the specified file.

But I still not understand what is the "valid data", what is the difference between it and the size of file?

I can use SetFilePointerEx and SetEndOfFile to extend the file size, but how do this by SetFileValidData?

SetFileValidData cannot input a argument large than the size of file. In this case, what is the living meaning of SetFileValidData?

like image 549
wenxibo Avatar asked Sep 01 '12 13:09

wenxibo


1 Answers

Please note that SetEndOfFile() doesn't write any zeros to any allocated sectors on disk, it just allocates the space pointers inside MFT records and then updates the space bitmap of the whole file system. But the OS, or FS, will record the valid/logical file length in its MFT record.

If you enlarge the file, from 1GB to 2GB, then the appended 1GB should be all zeros, but the FS won't write the zeros to disks, it refers to this file's valid length to know that the 1GB should be zeros. If you try to read from this enlarged 1GB portion, it will fill zeros directly in RAM and then feedback to your application. But if you write any byte inside this 1GB portion, the FS has to fill with zeros from the original 1GB offset to the current pointer that your application is trying to write to, but not the other bytes from the current location to the tail of the file. Meanwhile, it records the valid/logical length to be from 0 to the current location, the physical size and allocated size is still 2GB.

But, if you use SetFileValidData(), the FS will set the valid length to 2GB directly, and won't bother to fill any zeros. Whatever location you write to, it just writes, but whatever location you read from, you may read out some garbage data which was previously generated by other applications before the file was extended into that disk space.

like image 188
Melonhead Avatar answered Sep 19 '22 11:09

Melonhead