我正在使用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/