| |

How to Get Available Filesystem Space on Linux: a C Function with a C++ Example

It is common for programs to write to files in filesystems on disks. However, what if the disk was almost full when your program writes to the filesystem on a disk? For systems software and mission critical programs, it is a better or must-to-do practice to check the available filesystem space before actually writing to files. On POSIX OSes, we have the statvfs POSIX API to get filesystem statistics (On Linux, you may use the Linux specific statfs, but POSIX APIs are better for portability). In this post, we will introduce a C function and a C++ example to use the statvfs API to get the available filesystem space.

The header that you need to include in your C or C++ programs to use the statvfs API:

// header for statvfs
#include <sys/statvfs.h>

and the prototype of the statvfs is

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

The results will be filled to the buf statvfs struct:

struct statvfs {
    unsigned long  f_bsize;    /* filesystem 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;     /* filesystem ID */
    unsigned long  f_flag;     /* mount flags */
    unsigned long  f_namemax;  /* maximum filename length */
};

For unprivileged users, there are f_bavail available blocks in the filesystem and each block’s size is f_bsize bytes. So the overall available space in the filesystems is

f_bsize * f_bavail

I summarize and implement the method here in one simple function as follows.

long GetAvailableSpace(const char* path)
{
  struct statvfs stat;

  if (statvfs(path, &stat) != 0) {
    // error happens, just quits here
    return -1;
  }

  // the available size is f_bsize * f_bavail
  return stat.f_bsize * stat.f_bavail;
}

If you are using boost in your C++ project, you may alternatively use the space function.

With the above GetAvailableSpace function, a full example in C++ is as follows.

The program can accept a path to a file or a directory and report the available space in the corresponding filesystem. Several examples are as follows. Here, the above C++ program is build to GetAvailableSpace.

Find the available space where a file is in:

$ touch /tmp/a.txt
$ ./GetAvailableSpace /tmp/a.txt 
Available space under `/tmp/a.txt`: 4140032000 bytes.

Find the available space from a directory path (no need to be a mount point):

$ ./GetAvailableSpace /tmp/
Available space under `/tmp/`: 4140032000 bytes.

Note that the directory must exist. Otherwise, it will reports an error:

$ ./GetAvailableSpace /dd/
Available space under `/dd/`: 
ERROR: failed to get available space

Similar Posts

  • Vim Regular Expressions Tutorials

    Good tutorials on Vim regular Expressions. Vim Regular Expressions: http://www.softpanorama.org/Editors/Vimorama/vim_regular_expressions.shtml Another good one in Chinese: http://www.study-area.org/tips/vim/Vim-10.html Read more: Should I buy a Kindle paperwhite or regular Kindle? Profiling Vim to Find Out Which Plugin Makes Vim Slow Vim: Pasting text as is in Vim in Paste Mode Good tutorials for screen on Linux Makefile Tutorials…

  • How to add a new HDFS NameNode metadata directory to an existing cluster?

    We have a running HDFS cluster. Currently, the NameNode metadata data directory has only one directory configured in hdfs-site.xml: <property> <name>dfs.namenode.name.dir</name> <value>file:///home/hadoop/hdfs/</value> <description>NameNode directory for namespace and transaction logs storage.</description> </property> We would like to add a new directory for dfs.namenode.name.dir to make replicas of the metadata on a separated disk for higher data reliability….

  • Two Gnome Easter Eggs: “free the fish” and “gegls from outer space”

    There are funny Gnome easter eggs. One is “free the fish”, and another one is “gegls from outer space”. “free the fish” in Gnome 2 Press Alt+F2 to open “Run Application” dialog Enter “free the fish” Hit Enter “geglx from outer space” in Gnome 2 Press Alt+F2 to open “Run Application” dialog Enter “gegls from…

  • Systems Conferences

    Which ones are good systems conferences? Top ones by ACM and USENIX: OSDI: https://www.usenix.org/conferences/byname/179 SOSP: http://sosp.org/ Other SIGOPS Events: http://www.sigops.org/conf-sponsored.html EuroSys: http://www.eurosys.org/ SoCC: http://www.socc2013.org/ (SoCC 2013) ASPLOS: http://www.sigplan.org/Conferences/ASPLOS/Main VEE: http://www.sigplan.org/vee.htm USENIX ATC: https://www.usenix.org/conferences/byname/131 NSDI: https://www.usenix.org/conferences/byname/178 IEEE Conferences: ICDCS: http://www.temple.edu/cis/icdcs2013/ (2013) IPDPS: http://www.ipdps.org/ Other related ones and workshops: HPCA: Search HPCA ConferenceSC: http://www.supercomp.org/IEEE CLUSTER: http://www.clustercomp.org/ HotCloud:…

  • Any good Java REPL tool/implementation?

    Any good suggestions on a Java REPL implementation like ‘scala’ and ‘python’ or ‘php -a’? The java-repl tool https://github.com/albertlatacz/java-repl/ works nicely for most situations for me. It is released as a .jar. Hence, it is easy to download and run: $ wget –quiet https://github.com/albertlatacz/java-repl/releases/download/428/javarepl-428.jar -O /tmp/javarepo-428.jar && java -jar /tmp/javarepo-428.jar One usage example is as…

Leave a Reply

Your email address will not be published. Required fields are marked *