Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check in C code whether a directory is on NFS file system?

Tags:

linux

nfs

stat

In my C/C++ program I would like to check whether the data directory specified by user resides on NFS file system. The check is needed because the data processing latency / bandwidth is worse for the remote NFS directory. I would like to issue a warning to the user in case if the data directory is NFS.

How can I do that? I figured there is stat() call that should be able to help me, but the details are not clear.

I am on Linux.

like image 959
Alexey Alexandrov Avatar asked Jul 28 '12 08:07

Alexey Alexandrov


2 Answers

You should use statfs(2) and check f_type.

#include <sys/statfs.h>

struct statfs foo;
if (statfs ("/foo/bar", &foo)) {
    /* error handling */
}

if (foo.f_type == NFS_SUPER_MAGIC) {
    /* nfs warning */
}

I agree with Basile on the usefulness of doing it.

like image 111
InternetSeriousBusiness Avatar answered Oct 03 '22 20:10

InternetSeriousBusiness


You could use the statfs syscall to get information about the file system of a given path (of some file inside that file system).

I'm not sure it is useful to warn the users. The kernel is doing some file caching, and some remote file systems might be faster than some local ones (e.g. on a slow USB stick, or on a CDROM).

like image 38
Basile Starynkevitch Avatar answered Oct 03 '22 21:10

Basile Starynkevitch