LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 11-18-2011, 05:44 AM   #1
vipulspn
LQ Newbie
 
Registered: Aug 2009
Posts: 11

Rep: Reputation: 0
How to find network file system server (C++)


Hi,

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.

UINT WINAPI GetDriveType( __in_opt LPCTSTR lpRootPathName);

Input for the api is drive name, If return value is 4 means it is a network drive.

I was wondering if there is similar api for LINUX as well.

Thanks,
Vipul
 
Old 11-18-2011, 06:21 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,146

Rep: Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264
Quote:
Originally Posted by vipulspn View Post
Hi,

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.

UINT WINAPI GetDriveType( __in_opt LPCTSTR lpRootPathName);

Input for the api is drive name, If return value is 4 means it is a network drive.

I was wondering if there is similar api for LINUX as well.

Thanks,
Vipul

You can either call statfs or read /etc/mtab.
 
Old 11-18-2011, 07:51 PM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
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().
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] system() cannot find file the file specified [WINDOWS] smeezekitty Programming 8 08-06-2010 11:20 PM
how to find which file system i am using babu198649 Linux - Newbie 7 02-07-2008 02:41 AM
backup server ; network file system / ftp-server ? markus1982 Linux - Security 5 06-06-2007 05:15 PM
LXer: Network File System (NFS) Server and Client Configuration in Debian LXer Syndicated Linux News 0 03-12-2007 11:01 AM
Where can I find the file over mounten file system Wallace740 Linux - Newbie 2 03-30-2005 02:46 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration