std::filesystem:: space
|
ヘッダーで定義
<filesystem>
|
||
|
std::
filesystem
::
space_info
space
(
const
std::
filesystem
::
path
&
p
)
;
|
(1) | (C++17以降) |
|
std::
filesystem
::
space_info
space
(
const
std::
filesystem
::
path
&
p,
std:: error_code & ec ) noexcept ; |
(2) | (C++17以降) |
p
で指定されたパス名が存在するファイルシステムに関する情報を、POSIX
statvfs
によって取得されるように決定します。
POSIXの
struct
statvfs
のメンバーから以下のように設定された、
filesystem::space_info
型のオブジェクトを生成して返します:
-
space_info.capacityは f_blocks * f_frsize によって設定されたかのように設定されます。 -
space_info.freeは f_bfree * f_frsize に設定されます。 -
space_info.availableは f_bavail * f_frsize に設定されます。 - 決定できないメンバーはすべて static_cast < std:: uintmax_t > ( - 1 ) に設定されます。
例外を投げないオーバーロードは、エラー時にすべてのメンバーを static_cast < std:: uintmax_t > ( - 1 ) に設定します。
目次 |
パラメータ
| p | - | 検査対象のパス |
| ec | - | 例外を投げないオーバーロードにおけるエラー報告用出力パラメータ |
戻り値
ファイルシステム情報(
filesystem::space_info
オブジェクト)。
例外
noexceptでマークされていないオーバーロードは、
メモリ確保に失敗した場合
std::bad_alloc
をスローする可能性があります。
注記
space_info.available
は
space_info.free
より少ない場合があります。
例
#include <cstdint> #include <filesystem> #include <iostream> #include <locale> std::uintmax_t disk_usage_percent(const std::filesystem::space_info& si, bool is_privileged = false) noexcept { if (constexpr std::uintmax_t X(-1); si.capacity == 0 || si.free == 0 || si.available == 0 || si.capacity == X || si.free == X || si.available == X ) return 100; std::uintmax_t unused_space = si.free, capacity = si.capacity; if (!is_privileged) { const std::uintmax_t privileged_only_space = si.free - si.available; unused_space -= privileged_only_space; capacity -= privileged_only_space; } const std::uintmax_t used_space{capacity - unused_space}; return 100 * used_space / capacity; } void print_disk_space_info(auto const& dirs, int width = 14) { (std::cout << std::left).imbue(std::locale("en_US.UTF-8")); for (const auto s : {"Capacity", "Free", "Available", "Use%", "Dir"}) std::cout << "│ " << std::setw(width) << s << ' '; for (std::cout << '\n'; auto const& dir : dirs) { std::error_code ec; const std::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() { const auto dirs = {"/dev/null", "/tmp", "/home", "/proc", "/null"}; print_disk_space_info(dirs); }
出力例:
│ 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)
|
ファイルシステム上の空き領域と利用可能な領域に関する情報
(class) |