none
File sector position

    Question

  • Some optimizations can be done if you know in which sector on the disk a file is.  However, this information seems hidden in Windows API.  Is there a simple way to find it ?  Would it be different on a hard drive and a DVD ?

    I tried finding it using the file index with GetFileInformationByHandle() but to no avail.

    thanks.
    Friday, July 07, 2006 4:09 PM

Answers

  • The mapping of files to clusters is done by the Master File allocation Table (MFT).  How that mapping is done is not accessible by a normal user mode program.  It is somehow discoverable because disk defragmenters need this info.  I suspect that is a bit of a trade secret.  www.sysinternals.com might have something on it.

    Your best bet to do this without resorting to unportable tricks is to call do overlapped ReadFile() calls.  That allows the disk driver to determine the optimal order of head movements to honor the I/O requests.

    Friday, July 07, 2006 9:27 PM

All replies

  • There are no user-level APIs that can do this - you'd probably need to use kernel APIs, and maybe access the disk driver or try and read disk header information on your own. What sort of optimizations are you thinking of?
    Friday, July 07, 2006 5:55 PM
  • Well for example, I have an asynchronous device that reserves buffers to store data read from files in.  Since I read by clusters and not by sectors, I have to make buffers to read all the files in.  Say for example my buffers are 64K and I'm trying to open 100 1K files.  In memory, I'll have to read 100*64K and access my info from those buffers.  If, instead, I know what sectors the files are stored in, I can save them directly in my buffers and use only 2 buffers that will contain the data of the files place in a sort of queue if the files are contiguous.

    Using the ReadFile() call, I could give it only the handle of the first file and read it all in one shot by calculating the number of bytes to read using the sectors, if the files are contiguous. 
    Friday, July 07, 2006 6:30 PM
  • The mapping of files to clusters is done by the Master File allocation Table (MFT).  How that mapping is done is not accessible by a normal user mode program.  It is somehow discoverable because disk defragmenters need this info.  I suspect that is a bit of a trade secret.  www.sysinternals.com might have something on it.

    Your best bet to do this without resorting to unportable tricks is to call do overlapped ReadFile() calls.  That allows the disk driver to determine the optimal order of head movements to honor the I/O requests.

    Friday, July 07, 2006 9:27 PM