【问题标题】:How do I find disk name with statvfs?如何使用 statvfs 查找磁盘名称?
【发布时间】:2017-09-16 19:46:03
【问题描述】:

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

【问题讨论】:

    标签: linux filesystems posix system-calls


    【解决方案1】:

    使用getmntent():

    概要

       #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);
    

    ...

    描述

    ...

    mntent结构定义如下:

    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字段与通过在文件系统的挂载点上调用statvfs()从@987654331返回的struct mntent获取的挂载文件系统的f_fsid进行匹配@。

    【讨论】:

      猜你喜欢
      • 2020-03-01
      • 1970-01-01
      • 2016-05-29
      • 2021-02-25
      • 2020-03-31
      • 2019-03-11
      • 2012-07-03
      • 2010-11-06
      • 2013-02-06
      相关资源
      最近更新 更多