シェルコマンドとdfを除くディスクスペース使用量の取得

シェルコマンドとdfを除くディスクスペース使用量の取得

df情報をどのようにまたはどこで取得できますか?

/私はCプログラムを書いていますが、ルートファイルシステムのサイズ、使用された量、利用可能な量、またはパーセンテージを知りたいです。

普通誰でもこんなことをしますが

df -h

Filesystem      Size  Used Avail Use% Mounted on
/dev/sdc2       550G  168G  355G  33% /
udev            253G  216K  253G   1% /dev
tmpfs           253G  1.9M  253G   1% /dev/shm
/dev/sdc1       195M   13M  183M   7% /boot/efi
/dev/sda1       5.0T  4.9T   26G 100% /data
/dev/sdb1       559G  286G  273G  52% /scratch

私はちょうど値が必要です/。現在CIで働いています

system("df -h > somefile.txt");
fp = fopen( somefile.txt, "r");
/* read contents of file and parse out root file system values */

dfこれはコマンドが常に機能せず、時には無期限に中断され、Cプログラムが中断されるため問題になります。

以下にファイルがあるか含まれていますか/proc/sysサイズ使用される使用%情報?この情報を取得する他の方法はありますかdf

ベストアンサー1

int statfs(const char *path, struct statfs *buf); int fstatfs(int fd, struct statfs *buf);

説明関数statfs()は、マウントされたファイルシステムに関する情報を返します。 pathは、マウントされたファイルシステム上のファイルのパス名です。 buf は statfs 構造へのポインタであり、定義はおおよそ次のようになります。

      struct statfs {
          long    f_type;      type of file system (see below)
          long    f_bsize;     optimal transfer block size
          long    f_blocks;    total data blocks in file system
          long    f_bfree;     free blocks in fs
          long    f_bavail;    free blocks avail to non-superuser
          long    f_files;     total file nodes in file system
          long    f_ffree;     free file nodes in fs
          fsid_t  f_fsid;      file system id
#include <stdio.h>
#include <stdlib.h>
#include <sys/statfs.h>

int main ( int argc, char *argv[] )
{
   char path[80];
   struct statfs buffer;
   int n;
   long int block_size;
   long int total_bytes;
   long int free_bytes;
   double total_gb;

   sprintf( path, "/bin/pwd" );   /* this file should always exist */

   n = statfs( path, &buffer );

   block_size = buffer.f_blocks;
   total_bytes = buffer.f_bsize * block_size;
   total_gb = total_bytes / 1024.0 / 1024.0 / 1024.0;

   printf("total gb =  %lf\n", total_gb );

   return 0;
}

合計GB = 549.952682

私のdf -hの出力

Filesystem      Size  Used Avail Use% Mounted on
/dev/sdc2       550G  168G  355G  33% /

おすすめ記事