Linux 使用statvfs讀取文件系統信息


本文轉載自:https://blog.csdn.net/papiping/article/details/6980573

在測試過程中,f_bfree的值比f_frsize的值大於10%的尺寸大小,意味着16G的磁盤

f_bfree測試出來的結果是17G,f_frsize測試的結果是15G

 

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 unprivileged users */用戶級的空閑節點

   fsfilcnt_t     f_files;    /* # inodes */節點

   fsfilcnt_t     f_ffree;    /* # free inodes */空閑的節點

   fsfilcnt_t     f_favail;   /* # free inodes for unprivileged users */用戶級的空閑節點

   unsigned long  f_fsid;     /* file system ID */

   unsigned long  f_flag;     /* mount flags */

   unsigned long  f_namemax;  /* maximum filename length */

 

};

測試環境:ext4

1)分析f_bsize和f_frsize的異同

表示每塊包含字節的多少

打印結果都是4096字節,顯然都是一樣的

2)分析f_bfree和f_bavail

表示磁盤剩余空閑的容量

打印結果是

磁盤為16G的情況下,f_bfree比f_bavail大809MB

磁盤為1.5T的情況下,f_bfree比f_bavail大2585MB

並沒有我們想象的成線性增長

實際情況下采用df -Th

f_bavail更加接近顯示的值

3)f_blocks表示磁盤的整體空間

4)計算磁盤各個方面的數值如下:

磁盤的整體空間:(buf.f_blocks *  buf.f_frsize)/1024/1024)MB

磁盤的空閑空間:(buf.f_bfree *  buf.f_frsize)/1024/1024)MB

磁盤的用戶級空閑空間:(buf.f_bavail *  buf.f_frsize)/1024/1024)MB

 

 

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<mntent.h>

#include<sys/statvfs.h>

 

 

//pszDevicePath是設備的文件描述符,pszDeviceMountPoint是設備的掛載點

intGetDeviceMountPoint(constchar*pszDevicePath,char*pszDeviceMountPoint)

{

structmntent*pent;

FILE*pFile;

 

pFile=setmntent("/proc/mounts","r");

if(pFile==NULL){

perror("setmntent");

return-1;

}

while(NULL!=(pent=getmntent(pFile)))

{

if(strcmp(pent->mnt_fsname,pszDevicePath)==0)

{

sprintf(pszDeviceMountPoint,"%s",pent->mnt_dir);

printf("Device:%smountpointis:%s\n",pszDevicePath,pszDeviceMountPoint);

}

}

endmntent(pFile);

return0;

}

//通過statvfs查詢掛載點上文件系統的空間大小

intGetDeviceRoomSize(constchar*pszDeviceMountPoint)

{

structstatvfsbuf;

intrv=statvfs(pszDeviceMountPoint,&buf);

 

if(!rv)

{

printf("wholespace:%uMbytes\n",(buf.f_blocks*buf.f_frsize)/1024/1024);

printf("availspace:%uMbytes\n",(buf.f_bavail*buf.f_frsize)/1024/1024);

 

}

else

{

perror("statvfs");

return-1;

}

return(buf.f_blocks*buf.f_frsize)/1024/1024;

}

 

intmain(intargc,char*argv[])

{

intnDiskRoomSize=0;

charszMountPoint[32]={0};

GetDeviceMountPoint(argv[1],szMountPoint);

nDiskRoomSize=GetDeviceRoomSize(szMountPoint);

printf("DiskRoomSize=%dMB\n",nDiskRoomSize);

return 0;

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM