簡體   English   中英

如何以編程方式獲取 Linux 中目錄的可用磁盤空間

[英]How do I programmatically get the free disk space for a directory in Linux

是否有一個函數可以返回給定目錄路徑的驅動器分區上有多少可用空間?

檢查man statvfs(2)

我相信您可以將“可用空間”計算為f_bsize * f_bfree

NAME
       statvfs, fstatvfs - get file system statistics

SYNOPSIS
       #include <sys/statvfs.h>

       int statvfs(const char *path, struct statvfs *buf);
       int fstatvfs(int fd, struct statvfs *buf);

DESCRIPTION
       The function statvfs() returns information about a mounted file system.
       path is the pathname of any file within the mounted file  system.   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 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 */
           };

您可以使用 boost::filesystem:

struct space_info  // returned by space function
{
    uintmax_t capacity;
    uintmax_t free; 
    uintmax_t available; // free space available to a non-privileged process
};

space_info   space(const path& p);
space_info   space(const path& p, system::error_code& ec);

例子:

#include <boost/filesystem.hpp>
using namespace boost::filesystem;
space_info si = space(".");
cout << si.available << endl;

返回: space_info 類型的對象。 space_info 對象的值是通過使用 POSIX statvfs() 來獲得一個 POSIX struct statvfs,然后將其 f_blocks、f_bfree 和 f_bavail 成員乘以它的 f_frsize 成員,並將結果分配給 capacity、free 和分別可用的成員。 任何無法確定其值的成員都應設置為-1。

使用 C++17

您可以使用std::filesystem::space

#include <iostream>  // only needed for screen output

#include <filesystem>
namespace fs = std::filesystem;

int main()
{
    fs::space_info tmp = fs::space("/tmp");

    std::cout << "Free space: " << tmp.free << '\n'
              << "Available space: " << tmp.available << '\n';
}

您可以使用 Qt 類 QStorageInfo 來獲取硬盤空閑空間:首先,您應該包含標題:

#include <QStorageInfo> 
#define GB (1024 * 1024 * 1024)
bool CheckHardiskFree(const QString &strDisk)
{
    QStorageInfo storage(strDisk);
    if(storage.isValid() && storage.isReady())
    {
     double useGb =(storage.bytesTotal()-storage.bytesAvailable()) * 1.0/ GB;
     double freeGb =storage.bytesAvailable() * 1.0 / GB;
     double allGb =storage.bytesTotal()* 1.0 / GB;
     return true;
    }
    return false;
}

可以使用這樣的管道將命令的輸出輸入到程序中:

char cmd[]="df -h /path/to/directory" ;
FILE* apipe = popen(cmd, "r");
// if the popen succeeds read the commands output into the program with 
while (  fgets( line, 132 , apipe) )
{  // handle the readed lines
} 
pclose(apipe);
// -----------------------------------

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM