如何以编程方式获取Linux中目录的可用磁盘空间 [英] How do I programmatically get the free disk space for a directory in Linux

查看:342
本文介绍了如何以编程方式获取Linux中目录的可用磁盘空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个函数返回给定目录路径的驱动器分区上有多少空间?

解决方案

code> man statvfs(2)



我相信你可以计算'free space'为 f_bsize * f_bfree

  NAME 
statvfs,fstatvfs - 获取文件系统统计信息

SYNOPSIS
#include< sys / statvfs.h>

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

DESCRIPTION
函数statvfs()返回已装入文件系统的相关信息。
path是装入的文件系统中任何文件的路径名。 buf
是一个指向statvfs结构的指针,大致定义如下:

struct statvfs {
unsigned long f_bsize; / *文件系统块大小* /
unsigned long f_frsize; / * fragment size * /
fsblkcnt_t f_blocks; / * f_frsize单位中fs的大小* /
fsblkcnt_t f_bfree; / *#free blocks * /
fsblkcnt_t f_bavail; / *#空闲块用于非特权用户* /
fsfilcnt_t f_files; / *#inodes * /
fsfilcnt_t f_ffree; / *#free inodes * /
fsfilcnt_t f_favail; / *#免费inode为无特权的用户* /
unsigned long f_fsid; / *文件系统ID * /
unsigned long f_flag; / * mount flags * /
unsigned long f_namemax; / *最大文件名长度* /
};


Is there a function that returns how much space is free on a drive partition given a directory path?

解决方案

check man statvfs(2)

I believe you can calculate 'free space' as 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 */
           };

这篇关于如何以编程方式获取Linux中目录的可用磁盘空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆