linux 擷取檔案系統資訊(磁碟資訊),linux系統資訊

來源:互聯網
上載者:User

linux 擷取檔案系統資訊(磁碟資訊),linux系統資訊

源碼如下:

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/vfs.h>//檔案系統資訊結構體struct fileSystem_info{    char fileSystem_format[8];    char fileSystem_total_capacity[11];    char fileSystem_free_capacity[11];    char fileSystem_permissions[3];};/*擷取檔案系統資訊*/int get_fileSystem_info(const char *fileSystem_name, struct fileSystem_info *fi);/*block to kbyte*/static unsigned long kscale(unsigned long m_block, unsigned long m_kbyte);/*convert size to GB MB KB*/static char *convert_size(float m_size, char *dest);int main(){    char buf[1024];    struct fileSystem_info fileSysInfo;    get_fileSystem_info("/",&fileSysInfo);    printf("%s\n",fileSysInfo.fileSystem_format);    printf("%s\n",fileSysInfo.fileSystem_free_capacity);    printf("%s\n",fileSysInfo.fileSystem_total_capacity);    printf("%s\n",fileSysInfo.fileSystem_permissions);    return 0;}/*擷取檔案系統資訊*/int get_fileSystem_info(const char *fileSystem_name,struct fileSystem_info *fi){    struct statfs buf;    float fileSystem_total_size = 0;    float fileSystem_free_size = 0;    if(statfs(fileSystem_name,&buf))    {        fprintf(stderr,"statfs %s\n",strerror(errno));        return -1;    }    switch(buf.f_type)    {    case 0xEF51:    case 0xEF53:        sprintf(fi->fileSystem_format,"EXT");        break;    case 0x4d44:        sprintf(fi->fileSystem_format,"FAT");        break;    case 0x5346544e:        sprintf(fi->fileSystem_format,"NIFS");        break;    default:        sprintf(fi->fileSystem_format,"unknown");        break;    }    bzero(&fi->fileSystem_total_capacity,sizeof(fi->fileSystem_total_capacity));    bzero(&fi->fileSystem_free_capacity,sizeof(fi->fileSystem_free_capacity));    printf("blocks %ld\n",buf.f_blocks);    printf("bfree %ld\n",buf.f_bfree);    printf("bsize %ld\n",buf.f_bsize);    fileSystem_total_size =                    (float)(kscale(buf.f_blocks, buf.f_bsize));    fileSystem_free_size =                    (float)(kscale(buf.f_bfree, buf.f_bsize));    printf("total %f\n",fileSystem_total_size);    printf("free %f\n",fileSystem_free_size);    convert_size(fileSystem_total_size,fi->fileSystem_total_capacity);    convert_size(fileSystem_free_size,fi->fileSystem_free_capacity);    bzero(fi->fileSystem_permissions,sizeof(fi->fileSystem_permissions));    sprintf(fi->fileSystem_permissions,"rw");    return 0;}/*block to kbyte*/static unsigned long kscale(unsigned long m_block, unsigned long m_kbyte){    return ((unsigned long long) m_block * m_kbyte + 1024 / 2 ) /1024;}/*convert size to GB MB KB*/static char *convert_size(float m_size, char *dest){    if((((m_size / 1024.0) / 1024.0)) >= 1.0)    {        sprintf(dest,"%0.2fGB",(m_size/1024.0)/1024.0);    }    else if((m_size / 1024.0) >= 1.0)    {        sprintf(dest,"%0.2fMB",(m_size/1024));    }    else    {        sprintf(dest,"%0.2fKB",m_size);    }    return dest;}

總結:
1、關於 struct statfs 結構體資訊參考:http://blog.csdn.net/u011641885/article/details/46919027
2、對於fileSystem_total_size 使用float 類型,是為了精確度更高。buysbox 中的 fdisk 源碼使用的是整型相除,約為4舍五入。
3、kscale 函數中 使用 unsigned long long 類型 是因為 m_block 與 m_byte 原本是long 型,相乘的結果超出了 long 類型的可以表示資料位元。

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.