Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How can I find if mount node(machine) is a network different machine (not the one from which user has logged in)?
To explain it better i have a location say /data/vipul/dir. I want to check if this location is on my local linux server on which I am logged in or some different machine on network. For example on windows we have getdrivetype() api.
How can I find if mount node(machine) is a network different machine (not the one from which user has logged in)?
To explain it better i have a location say /data/vipul/dir. I want to check if this location is on my local linux server on which I am logged in or some different machine on network. For example on windows we have getdrivetype() api.
Linux is more complicated because you can have a lot of different filesystems, even using different mechanisms (FUSE et cetera). There are two approaches. First:
Use statfs() or fstatfs() on the file (name or descriptor, respectively; see man 2 statfs and man 2 fstatfs for details).
The call fills in a struct statfs structure you supply. The field f_type describes the file system type the file is on. The values are the NAME_SUPER_MAGIC constants listed in /usr/include/linux/magic.h (but it is automatically included by #include <sys/vfs.h>).
I suggest a failsafe approach: Use the value to detect only known local filesystem types (ext2, ext3, ext4, btrfs, devfs, proc, and so on; most of them are local). If you encounter any other filesystems, read /proc/mounts line by line. The second field is always the mount point.
Call stat() on the file name or fstat() on the file descriptor (see man 2 fstat for details), and stat() on each mount-point/. in turn. The slashdot at the end making sure you stat the mounted filesystem, and not the mount point. If the results have matching st_dev fields, then that line in /proc/mounts describes the mount the file is on.
When you find the corresponding line in /proc/mounts, the first field should be something like host:volume-or-path if it is a shared volume. It varies a bit between filesystems. (I think FUSE mounts start with fuse: for example; you need to do some Googling over this.)
If you find something that looks like a host name or address, supply it to gethostbyname() (see man 3 gethostbyname for details) to see if it is a valid host name or IPv4 or IPv6 address; it will return non-NULL if it is, NULL if not. If it is valid, then the file is on a networked filesystem.
This first approach is fast for local files, but slow for remote files. I would only do it if your program needs to do it only once, for some specific file. It is the closest logical equivalent to the proprietary interface you described.
The second approach is better if you do the checking often:
First, start by reading /proc/mounts and checking which mount points are remote using the approach I outlined above. Add remote mounts to one list, and all others to a local list. Save the st_dev for each entry.
When you wish to check if a file is local or remote, just stat() or fstat() the file, and find the corresponding st_dev in the two lists. If it is on the local list, the file is on a local filesystem. If it is on the remote list, the file is on a remote filesystem.
If the file is on neither list, it is best to call statfs() or fstatfs() on it, and check if the f_type corresponds to a known local filesystem type. If it does not, then the system may have mounted a new filesystem, and you should reinitialize the lists by rereading /proc/mounts . If there is still no match on the st_dev value, the results are inconclusive; this should never happen, though.
The reason to call fstatfs() on the file is that in some circumstances (when using namespaces, for example) /proc/mounts may not include information on all mounts, but the administrator may have (bind-)mounted those so that they happen to be visible to your process. These are almost always special filesystems like /proc or /sys so checking the filesystem type using statfs() is a good way to avoid rereading /proc/mounts too often.
Also, if your program is not privileged (is not running as root), then you may not be able to stat() the mounts. Then you need to rely on the file system type returned by statfs().
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.