Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get file path from NTFS index number?

Tags:

c++

winapi

I have dwVolumeSerialNumber, nFileIndexHigh, nFileIndexLow values obtained from a call to GetFileInformationByHandle. How can I get file path from these values?

like image 777
skevar7 Avatar asked Jun 22 '10 08:06

skevar7


2 Answers

Note: The original question had an error in it. Now that the question has been fixed this answer no longer applies.


In general you can't. The information you retrieved just tells you what disk the file is on and how big it is. It does not provide enough information to identify the actual file. Specifically:

  • dwVolumeSerialNumber identifies the volume, and
  • nFileSizeHigh and nFileSizeLow give you the size of the file

If the file happens to be the only file on that volume that is that exact size, you could search the volume for a file of that size. But in general this is both expensive and unreliable, so I don't recomment it.

like image 110
Ray Burns Avatar answered Oct 14 '22 01:10

Ray Burns


This MSDN article shows how to get the path from a file handle.

You use OpenFileById to open a file given its file ID but you also need an open file elsewhere on the same volume, I assume to get the volume serial number.

This blog posting raises an interesting issue that you need to pass in 24 for the structure size (worked out by looking at assembly code).

I leave it as an interesting exercise (I couldn't find an easy answer) how you go from a dwVolumeSerialNumber to having a valid other handle open for that volume or a file on that volume, but maybe you already have enough information for your case. One possibility is to iterate all mounted volumes calling GetVolumeInformation to find the one with matching serial number.

Note: If you don't have the file open then you may not be able to rely on the nFileIndexHigh/Low combo (aka file ID) as described in the BY_HANDLE_FILE_INFORMATION Structure notes, which warns it can change for FAT systems, but In the NTFS file system, a file keeps the same file ID until it is deleted.

like image 45
Andy Dent Avatar answered Oct 14 '22 00:10

Andy Dent