Determining physical disk home for files (C++)

Started by
5 comments, last by Syranide 18 years, 8 months ago
Uhm, unsure of what I would call it. However, simple, I want to be able to tell on which _physical disk_ a file resides on (that is, "C:\log.txt" and "D:\log.txt" reside on the same physical disk or two separate), it doesn't really matter what I get as long as I can tell whether two or more files reside on the same physical disk. However, going through the documentation, all I could find was volume information, which isn't any satisfying as each disk could have many partitions/volumes. After some additional research I figured I could get some device data using DeviceIoControl, whether or not it is possible, it doesn't really help me in the end, as the file could potentially reside in a directory mount, in which case using the drive letter will produce improper results. Does anyone have any information/knowledge about this?


Advertisement
GetVolumeInformation
That is pretty much what I explained, volume information only gives information to whether they are on the same volume or not, but a physical disk can and commonly has many volumes.


Get the volume for each file, for example C:.

Open this using CreateFile and use the returned handle to called DeviceIOControl with the following control code:

IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS

This will give you a VOLUME_DISK_EXTENTS structure which contains one or more DISK_EXTENT structures. This structure tell you the physical disc that a volume is one.

Compare the two and see if both files are on the same physical disc.
"Absorb what is useful, reject what is useless, and add what is specifically your own." - Lee Jun Fan
Thank you so very much.

I totally overlooked the volume control codes in DeviceIoControl.
Got it working with determining which drive letter belongs to which disk using what you said.

Gonna fiddle around little and see if I can somehow get accurate information on directory mount points too (as files could potentially be on C: disk1 but been mounted as disk2 using directory mounts).


Why do you want to know? I'd be very suspicious of this, mostly because there's no way it can be reliable in all situations.

For example, what happens when you've got a RAID configuration? One file could span multiple physical disks in that case...
The reason I want to know is basically, I want to be able to read from multiple disks simultaneously, and, if I happen to read from the same physical disk, well, then it gets really bad with horrible speeds.

Accurate? Might be true, but I'm very close now to determining a from what disk a certain file is on, even for mount points. And, as far as I can see, it shouldn't pose any problems unless they make drastical changes.

RAID, yes, I guess there could be some hidden problem (perhaps I was slightly unclear of this, but being a minor concern to other I left it out to avoid confusion, if they use RAID I don't care, as long as I'm not reading 2 files from the same disks), so no, shouldn't really pose a problem as I rely on the system to give me the information for a volume/drive. And, really, it can only give me one number regardless of RAID or not, and that is all I want.

EDIT: sure, I'm not saying my solution doesn't have flaws, but as far as I've come, it seems rigid enough to work for my purpose considering the competitor applications doesn't even support this at all, so, worst case ends up being, same performance as the other.


This topic is closed to new replies.

Advertisement