Dan Paradox
Dan Paradox

Reputation: 1380

Detecting file system in C

Is there a way to tell whether a file is in local disk or NFS in C? The code should be portable across various linux distros and shouldn't rely on system calls (e.g. stat -f).

Upvotes: 4

Views: 2457

Answers (1)

cnicutar
cnicutar

Reputation: 182734

You want to use statfs from <sys/vfs.h>.

int statfs(const char *path, struct statfs *buf);

struct statfs {
    __SWORD_TYPE f_type;    /* type of file system (see below) */

Here's how to use it:

struct statfs s;
if (statfs("/etc", &s))
    perror("statfs");

switch (s->f_type) {
case EXT2_SUPER_MAGIC:
    break;
case EXT3_SUPER_MAGIC:
    break;
default:
    break;
}

Also:

  • You are confusing "external commands" and "system calls". They are very very different things
  • The stat(1) command is very portable among Linux distros.

Upvotes: 8

Related Questions