| Classes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| File types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <filesystem> | ||
std::filesystem::space_info space(conststd::filesystem::path& p); | (1) | (since C++17) |
std::filesystem::space_info space(conststd::filesystem::path& p, std::error_code& ec)noexcept; | (2) | (since C++17) |
Determines the information about the filesystem on which the pathnamep is located, as if by POSIXstatvfs.
Populates and returns an object of typefilesystem::space_info, set from the members of the POSIXstruct statvfs as follows:
space_info.capacity is set as if byf_blocks* f_frsize.space_info.free is set tof_bfree* f_frsize.space_info.available is set tof_bavail* f_frsize.The non-throwing overload sets all members tostatic_cast<std::uintmax_t>(-1) on error.
Contents |
| p | - | path to examine |
| ec | - | out-parameter for error reporting in the non-throwing overload |
The filesystem information (afilesystem::space_info object).
Any overload not markednoexcept may throwstd::bad_alloc if memory allocation fails.
space_info.available may be less thanspace_info.free.
#include <cstdint>#include <filesystem>#include <iostream>#include <locale> std::uintmax_t disk_usage_percent(conststd::filesystem::space_info& si,bool is_privileged=false)noexcept{if(constexprstd::uintmax_t X(-1); si.capacity==0|| si.free==0|| si.available==0|| si.capacity== X|| si.free== X|| si.available== X)return100; std::uintmax_t unused_space= si.free, capacity= si.capacity;if(!is_privileged){conststd::uintmax_t privileged_only_space= si.free- si.available; unused_space-= privileged_only_space; capacity-= privileged_only_space;}conststd::uintmax_t used_space{capacity- unused_space};return100* used_space/ capacity;} void print_disk_space_info(autoconst& dirs,int width=14){(std::cout<<std::left).imbue(std::locale("en_US.UTF-8")); for(constauto s:{"Capacity","Free","Available","Use%","Dir"})std::cout<<"│ "<<std::setw(width)<< s<<' '; for(std::cout<<'\n';autoconst& dir: dirs){std::error_code ec;conststd::filesystem::space_info si= std::filesystem::space(dir, ec);for(auto x:{si.capacity, si.free, si.available, disk_usage_percent(si)})std::cout<<"│ "<<std::setw(width)<<static_cast<std::intmax_t>(x)<<' ';std::cout<<"│ "<< dir<<'\n';}} int main(){constauto dirs={"/dev/null","/tmp","/home","/proc","/null"}; print_disk_space_info(dirs);}
Possible output:
│ Capacity │ Free │ Available │ Use% │ Dir │ 84,417,331,200 │ 42,732,986,368 │ 40,156,028,928 │ 50 │ /dev/null│ 84,417,331,200 │ 42,732,986,368 │ 40,156,028,928 │ 50 │ /tmp│ -1 │ -1 │ -1 │ 100 │ /home│ 0 │ 0 │ 0 │ 100 │ /proc│ -1 │ -1 │ -1 │ 100 │ /null
(C++17) | information about free and available space on the filesystem (class)[edit] |