Skip to content

Instantly share code, notes, and snippets.

@mikemadden42
Created January 29, 2022 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikemadden42/913611fc521a90194d31689d3510edb9 to your computer and use it in GitHub Desktop.
Save mikemadden42/913611fc521a90194d31689d3510edb9 to your computer and use it in GitHub Desktop.
Check filesystem
#include <boost/filesystem.hpp>
#include <iostream>
// g++ -o boo boo.cc -lboost_filesystem
// clang++ -o boo boo.cc -lboost_filesystem
int main() {
boost::filesystem::path p("/");
auto freeSpace = boost::filesystem::space(p).free;
std::cout << freeSpace << " Bytes free" << std::endl;
std::cout << freeSpace / (1 << 20) << " MB free" << std::endl;
// free space available to a non-privileged process
auto availableSpace = boost::filesystem::space(p).available;
std::cout << availableSpace << " Bytes available" << std::endl;
std::cout << availableSpace / (1 << 20) << " MB available" << std::endl;
auto totalSpace = boost::filesystem::space(p).capacity;
std::cout << totalSpace << " Bytes capacity" << std::endl;
std::cout << totalSpace / (1 << 20) << " MB capacity" << std::endl;
auto usedSpace = totalSpace - freeSpace;
std::cout << usedSpace << " Bytes used" << std::endl;
std::cout << usedSpace / (1 << 20) << " MB used" << std::endl;
auto usedPercent = static_cast<double>(usedSpace) / static_cast<double>(totalSpace);
std::cout << usedPercent << " percent used" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment