得到文件系统的 管理信息

来源:互联网 发布:网络搭建教程视频 编辑:程序博客网 时间:2024/05/16 04:54
 

#include<stdio.h>
#include<sys/types.h>
#include<sys/statvfs.h>
//该函数的 功能主要是得到文件系统的 管理信息,并计算出文件系统的空余空间率。
int main(int argc, char* argv[])
{
        struct statvfs                  buf;
        int                             stat;
        float                           per;

        sync();
        stat = statvfs(argv[1], &buf);
        if(stat != 0){
                perror("statvfs");
                return 1;
        }

        per = (float) buf.f_bfree/buf.f_blocks*100;
        printf("free percent %4.1f%%\n", per);

        printf("buf.f_bsize %ld\n", buf.f_bsize);
        printf("buf.f_frsize %ld\n", buf.f_frsize);
        printf("buf.f_namemax %ld\n", buf.f_namemax);
        printf("buf.f_bfree %ld\n", buf.f_bfree);
        printf("buf.f_favail %ld\n", buf.f_favail);
        printf("buf.f_fsid %ld\n", buf.f_fsid);
        printf("buf.f_files %ld\n", buf.f_files);
        printf("buf.f_flag %ld\n", buf.f_flag);
        printf("buf.f_blocks %ld\n", buf.f_blocks);
        printf("buf.f_ffree %ld\n", buf.f_ffree);
        printf("buf.f_bavail %ld\n", buf.f_bavail);

        return 0;
}
有两个地方不是很明白:
1.  sync()函数:就是 把内存中保存的 super   block块写到硬盘上去,而super  block存放的是分区也就 是文件系统的 全部管理信息。
在这个函数这个函数中先调用sync(),将文件系统管理信息写到磁盘上(其实不是这样,根据APUE2所描述,它只是将块缓冲区的内容放到写队列中,然后就返回,并不等待实际谢磁盘操作结束)。
问:(1)在后面调用statvfs函数得到管理信息,这个函数按照上面的分析,应该是从硬盘上读出文件系统管理信息的吧?而且信息是存放在文件系统的super  block中,对吧?
           (2)在这个函数中去掉sync(),并不影响程序的 结果,想不 明白?因为内存中的文件系统信息才是最新的,所以有必要在调用statvfs前调用一下sync().

2.DESCRIPTION
       The function statvfs() returns information about a mounted file system.
       path is the pathname of any file within the mounted filesystem.  buf is
       a pointer to a statvfs structure defined approximately as follows:

         struct statvfs {
           unsigned long  f_bsize;    /* file system block size */
           unsigned long  f_frsize;   /* fragment size */
           fsblkcnt_t     f_blocks;   /* size of fs in f_frsize units */
           fsblkcnt_t     f_bfree;    /* # free blocks */
           fsblkcnt_t     f_bavail;   /* # free blocks for non-root */
           fsfilcnt_t     f_files;    /* # inodes */
           fsfilcnt_t     f_ffree;    /* # free inodes */
           fsfilcnt_t     f_favail;   /* # free inodes for non-root */
           unsigned long  f_fsid;     /* file system ID */
           unsigned long  f_flag;     /* mount flags */
           unsigned long  f_namemax;  /* maximum filename length */
         };
能说说mounted file system 和 file system 的分别吗?
编译运行程序:./a.out     参数 1                     ,问题:不管我 的参数是什么(当然是存在的目录或者文件),程序的结果都是一样的!!!!想不 明白?

 

 

 

 

 

原创粉丝点击