Search code examples
linuxnfsstat

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


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.


Solution

  • 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.