Skip to content

Instantly share code, notes, and snippets.

@sajith
Created August 31, 2017 20:35
Show Gist options
  • Save sajith/6c118be93b889dc9ac9b1f2540677d4d to your computer and use it in GitHub Desktop.
Save sajith/6c118be93b889dc9ac9b1f2540677d4d to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string.h>
#include <sys/statvfs.h>
/*
* How do we find disk usage? You know, the way "df" does? we can
* use statvfs() or statfs() calls! The former is moar standard and
* POSIX-ey, the latter is BSD-ism that Linux happens to have
* implemented.
*
* Results from the below code seems to be consistent with "df -i /"
* and "df --block-size=<bs> /".
*/
int main(int argc, char *argv[])
{
if (argc < 2)
{
std::cout << "Usage: " << argv[0] << " <path>" << std::endl;
return 1;
}
struct statvfs s{0};
if (statvfs(argv[1], &s) < 0)
{
std::cerr << "Error from statvfs(): " << strerror(errno) << std::endl;
return 1;
}
std::cout << "f_bsize : " << s.f_bsize << std::endl; /* file system block size */
std::cout << "f_frsize : " << s.f_frsize << std::endl; /* fragment size */
std::cout << "f_blocks : " << s.f_blocks << std::endl; /* size of fs in f_frsize units */
std::cout << "f_bfree : " << s.f_bfree << std::endl; /* # free blocks */
std::cout << "f_bavail : " << s.f_bavail << std::endl; /* # free blocks for unprivileged users */
std::cout << "f_files : " << s.f_files << std::endl; /* # inodes */
std::cout << "f_ffree : " << s.f_ffree << std::endl; /* # free inodes */
std::cout << "f_favail : " << s.f_favail << std::endl; /* # free inodes for unprivileged users */
std::cout << "f_fsid : " << s.f_fsid << std::endl; /* file system ID */
std::cout << "f_flag : " << s.f_flag << std::endl; /* mount flags */
std::cout << "f_namemax : " << s.f_namemax << std::endl; /* maximum filename length */
return 0;
}
// Local Variables:
// compile-command: "g++ -Wall -std=c++11 statvfs.cc -o statvfs"
// End:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment