# determine NFS file, C



## bigearsbilly (Jan 5, 2012)

I am doing some stuff with mmap. I want to determine whether a file is on an NFS partition or a local
filesystem. stat(2) doesn't seem to have a method.

Anyone?


----------



## xibo (Jan 5, 2012)

You can use GETMNTINFO(3) 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) and walking to the left until the next '/' character each time (or evaluate the longest matching sequence).


----------



## SirDice (Jan 5, 2012)

The stuct stat contains a st_dev that has the ID of the device containing the file.


----------



## bigearsbilly (Jan 5, 2012)

I found: statfs(2) 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 (Feb 16, 2012)

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;

}
```


----------

