View Full Version : determine NFS file, C
bigearsbilly
January 5th, 2012, 12:24
I am doing some stuff with mmap. I want to determine whether a file is on an NFS partition or a local
filesystem. stat doesn't seem to have a method.
Anyone?
xibo
January 5th, 2012, 13:28
You can use GETMNTINFO(3) (http://www.freebsd.org/cgi/man.cgi?query=getmntinfo) to query all mount points, and then traverse towards the root until the node's path (as in "filename") is a mount point.
EDIT:
traverse towards root as in either looping with chdir("..") or alternately REALPATH(3) (http://www.freebsd.org/cgi/man.cgi?query=realpath) and walking to the left until the next '/' character each time (or evaluate the longest matching sequence).
SirDice
January 5th, 2012, 13:33
The stuct stat contains a st_dev that has the ID of the device containing the file.
bigearsbilly
January 5th, 2012, 15:39
I found: statfs does it ok. It has:
struct statfs {
...
uint32_t f_type; /* type of filesystem */
char f_fstypename[MFSNAMELEN]; /* filesystem type name */
...
}
f_fstypename == "nfs"
f_type == 6
String compare is a bit clunky. All I need is to find a safe way of checking f_type. Can't see any handy macros or #defines yet.
bigearsbilly
February 16th, 2012, 15:39
Actually I found a much nicer way, if anyone is interested. Works with files and directories too, so just for completeness:
#include <sys/param.h>
#include <sys/mount.h>
int is_local_fs(char const * path)
{
struct statfs sb;
if (statfs(path, &sb) == -1) {
perror(path);
return 0;
}
return sb.f_flags & MNT_LOCAL;
}
vBulletin® v3.8.7, Copyright ©2000-2013, vBulletin Solutions, Inc.