Ignorant
Ignorant

Reputation: 2691

How do I find disk name with statvfs?

I'm using statvfs to collect information on a specific file. I would like to obtain the disk name/partition as well (like /dev/sdb1, /dev/media and so on). However the statvfs struct doesn't seem to provide such data. Where can I find it?

Upvotes: 2

Views: 637

Answers (1)

Andrew Henle
Andrew Henle

Reputation: 1

Use getmntent():

SYNOPSIS

   #include <stdio.h>
   #include <mntent.h>

   FILE *setmntent(const char *filename, const char *type);

   struct mntent *getmntent(FILE *stream);

   int addmntent(FILE *stream, const struct mntent *mnt);

   int endmntent(FILE *streamp);

   char *hasmntopt(const struct mntent *mnt, const char *opt);

...

DESCRIPTION

...

The mntent structure is defined in as follows:

struct mntent {
    char *mnt_fsname;   /* name of mounted filesystem */
    char *mnt_dir;      /* filesystem path prefix */
    char *mnt_type;     /* mount type (see mntent.h) */
    char *mnt_opts;     /* mount options (see mntent.h) */
    int   mnt_freq;     /* dump frequency in days */
    int   mnt_passno;   /* pass number on parallel fsck */
};

For example:

FILE *fp = setmntent( "/etc/mtab", "r" );
for ( ;; )
{
    struct mntent *me = getmntent( fp );
    if ( NULL == me )
    {
        break;
    }

    ...
}

endmntent( fp );

Given a file name, you'll have to do some coding to match the filename to the file system mount point. The easiest way is probably to match the f_fsid field from the struct statvfs from the file to the f_fsid of a mounted filesystem obtained by calling statvfs() on the file system's mount point from the struct mntent returned by getmntent().

Upvotes: 2

Related Questions