How can I determine the mount path of a given file on Linux in C? -


i have arbitrary file determine mount point. let's /mnt/bar/foo.txt, have following mount points in addition "normal" linux mount points:

[some device mounted to] -> /mnt/bar [some device mounted to] -> /mnt/other

i've taken @ stat() , statvfs(). statvfs() can give me filesystem id, , stat can give me id of device, neither of these can correlate mount point.

i'm thinking have call readlink() on arbitrary file, , read through /proc/mounts, figuring out path closely matches filename. approach, or there great libc function i'm missing out on to this?

you can combination of getfsent iterate through list of devices, , stat check if file on device.

#include <fstab.h>    /* getfsent() */ #include <sys/stat.h> /* stat() */  struct fstab *getfssearch(const char *path) {     /* stat file in question */     struct stat path_stat;     stat(path, &path_stat);      /* iterate through list of devices */     struct fstab *fs = null;     while( (fs = getfsent()) ) {         /* stat mount point */         struct stat dev_stat;         stat(fs->fs_file, &dev_stat);          /* check if our file , mount point on same device */         if( dev_stat.st_dev == path_stat.st_dev ) {             break;         }     }      return fs; } 

note, brevity there's no error checking there. getfsent not posix function, used convention. works here on os x doesn't use /etc/fstab. not thread safe.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -