How do I get the volume/disk of a Path (directory/file)

Any advice on how to get which volume/disk a given Path is on? I need to ensure there is ample space on that volume, and keep track of it as the program is running. I figure I can use canonicalize to get the "real" Path. On windows it sounds like I can use GetVolumeInformationByHandleW. I'm unsure on Linux/MacOS - I was suggested the df util, but parsing the output of a command-line utility seems pretty janky and unreliable.


You could look at how the uutils project reimplements df in Rust:

It looks like on Linux they parse the contents of /proc/self/mountinfo and /etc/mtab, and on macOS they call a (BSD-specific) C library function, getmntinfo.

1 Like