Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting file system in C

Tags:

c

linux

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

like image 936
Dan Paradox Avatar asked Feb 23 '23 05:02

Dan Paradox


1 Answers

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.
like image 194
cnicutar Avatar answered Mar 04 '23 21:03

cnicutar