如何以编程方式获取 Linux 中目录的可用磁盘空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3992171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I programmatically get the free disk space for a directory in Linux
提问by Matt
Is there a function that returns how much space is free on a drive partition given a directory path?
是否有一个函数可以返回给定目录路径的驱动器分区上有多少可用空间?
采纳答案by John Ledbetter
check man statvfs(2)
查看 man statvfs(2)
I believe you can calculate 'free space' as f_bsize * f_bfree
.
我相信您可以将“可用空间”计算为 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 */
};
回答by Sandeep
You can use boost::filesystem:
您可以使用 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);
Example:
例子:
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
space_info si = space(".");
cout << si.available << endl;
Returns: An object of type space_info. The value of the space_info object is determined as if by using POSIX statvfs() to obtain a POSIX struct statvfs, and then multiplying its f_blocks, f_bfree, and f_bavail members by its f_frsize member, and assigning the results to the capacity, free, and available members respectively. Any members for which the value cannot be determined shall be set to -1.
返回: space_info 类型的对象。space_info 对象的值的确定如同使用 POSIX statvfs() 获得一个 POSIX struct statvfs,然后将其 f_blocks、f_bfree 和 f_bavail 成员与其 f_frsize 成员相乘,并将结果分配给容量、空闲和分别可用的成员。任何无法确定其值的成员应设置为-1。
回答by eric stockman
One can get the output of a command into a program by using a pipe like this:
可以使用这样的管道将命令的输出输入到程序中:
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);
// -----------------------------------
回答by Elmar
With C++17
使用 C++17
You can use std::filesystem::space
:
您可以使用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';
}