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

后端 未结 4 2081
失恋的感觉
失恋的感觉 2020-12-23 19:42

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

4条回答
  •  借酒劲吻你
    2020-12-23 20:18

    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 
    
           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 */
               };
    

提交回复
热议问题