Given a path to a file or directory, how can I determine the mount point for that file? For example, if /tmp
is mounted as a tmpfs
filesystem then given the file name /tmp/foo/bar
I want to know that it's stored on a tmpfs
rooted at /tmp
.
This will be in C++ and I'd like to avoid invoking external commands via system()
. The code should be robust--not necessarily against deliberate tampering but definitely in the face of nested mountpoints, symlinks, etc.
I haven't been able to find a simple system call to do this. It looks like I'll have to write the check myself. Here's a rough outline of what I'm planning.
readlink
shell command. How? /etc/mtab
with getmntent()
& co.For #1 is there a simple system call or do I need to read each directory component of the path and resolve them with readlink(2)
if they are symlinks? And handle .
and ..
myself? Seems like a pain.
For #3 I've got various ideas on how to do this. Not sure which is best.
open()
the file, its parent, its parent's parent, etc. using openat(fd, "..")
until I reach one of the /etc/mtab
entries. (How do I know when I do? fstat()
them and compare the inode numbers?)I'm leaning towards the first option but before I code it all up I want to make sure I'm not overlooking anything--ideally a built-in function that does this already!
Mounting a file system creates a binding for the duration of the mount. The binding is between a directory that is already in the file system hierarchy, called the mount point , and the entry point into the file system about to be mounted, called the root of this file system.
In computers, to mount is to make a group of files in a file system structure accessible to a user or user group. In some usages, it means to make a device physically accessible. For instance, in data storage, to mount is to place a data medium (such as a tape cartridge) on a drive in a position to operate.
To see the list of mounted filesystems, type the simple “findmnt” command in the shell as below, which will list all the filesystems in a tree-type format. This snapshot contains all the necessary details about the filesystem; its type, source, and many more.
A mounted folder is an association between a volume and a directory on another volume. When a mounted folder is created, users and applications can access the target volume either by using the path to the mounted folder or by using the volume's drive letter.
This is what I've come up with. It turns out there's usually no need to iterate through the parent directories. All you have to do is get the file's device number and then find the corresponding mount entry with the same device number.
struct mntent *mountpoint(char *filename, struct mntent *mnt, char *buf, size_t buflen) { struct stat s; FILE * fp; dev_t dev; if (stat(filename, &s) != 0) { return NULL; } dev = s.st_dev; if ((fp = setmntent("/proc/mounts", "r")) == NULL) { return NULL; } while (getmntent_r(fp, mnt, buf, buflen)) { if (stat(mnt->mnt_dir, &s) != 0) { continue; } if (s.st_dev == dev) { endmntent(fp); return mnt; } } endmntent(fp); // Should never reach here. errno = EINVAL; return NULL; }
Thanks to @RichardPennington for the heads up on realpath()
, and on comparing device numbers instead of inode numbers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With