linux - 如何使用 statvfs 查找磁盘名称?

标签 linux filesystems posix system-calls

我正在使用statvfs收集有关特定文件的信息。我还想获取磁盘名称/分区(例如 /dev/sdb1/dev/media 等)。然而 statvfs 结构似乎没有提供此类数据。我在哪里可以找到它?

最佳答案

使用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 */
};

例如:

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

    ...
}

endmntent( fp );

给定文件名,您必须进行一些编码才能将文件名与文件系统安装点相匹配。最简单的方法可能是将文件中 struct statvfs 中的 f_fsid 字段与通过调用 获取的已挂载文件系统的 f_fsid 进行匹配>statvfs() 位于 getmntent() 返回的 struct mntent 文件系统的挂载点上。

关于linux - 如何使用 statvfs 查找磁盘名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43515050/

相关文章:

linux - SUSE Linux 中的 bash_profile 在哪里

linux - 如何将多行从最低值到最高值排序?

php - 传输后数据库无法连接到 Linux 服务器

swift - Mac 操作系统 : Show custom file size in finder attributes and get info without kernel

node.js - Nodejs 中的 fs.readFileSync() 错误

c - waitpid - 在哪些情况下 WIFEXITED 和 WIFSIGNALED 都是假的?

c++ - 什么时候使用 posix_memalign/memalign?

c - 套接字,accept() 函数,无效参数

linux - 将文件转换为 NTFS

c - 了解 C 中的 fork() 顺序