diff options
Diffstat (limited to 'liblfjail/zfs.cc')
| -rw-r--r-- | liblfjail/zfs.cc | 55 |
1 files changed, 28 insertions, 27 deletions
diff --git a/liblfjail/zfs.cc b/liblfjail/zfs.cc index 5ecd826..bc0caee 100644 --- a/liblfjail/zfs.cc +++ b/liblfjail/zfs.cc @@ -28,12 +28,12 @@ namespace { constexpr std::string path_zpool = "/sbin/zpool"; constexpr std::string path_zfs = "/sbin/zfs"; -// Run a command (probably a zfs command), split its output into lines, -// and return each line. -// -// The spawn_how argument is the same as lfjail::spawn(). -auto spawn_lines(exec::executor auto &&executor) - -> std::generator<std::string_view> +/* + * Run a command and split each line of output into whitespace-separated + * columns. + */ +auto columns(exec::executor auto &&executor) + -> std::generator<std::vector<std::string_view>> { std::string output; @@ -43,17 +43,9 @@ auto spawn_lines(exec::executor auto &&executor) if (!result) throw zfs_error("failed to run zfs command"); - co_yield std::ranges::elements_of(words(output, '\n')); -} - -// Like spawn_lines, but each line is returned as a vector of words. -auto spawn_words(auto &&spawn_how) - -> std::generator<std::vector<std::string_view>> -{ - auto lines = spawn_lines(std::forward<decltype(spawn_how)>(spawn_how)); - - for (auto &&line : lines) + for (auto &&line : words(output, '\n')) { co_yield std::vector(std::from_range, words(line)); + } } // Run a command which produces two columns of output, assumed to be @@ -61,7 +53,7 @@ auto spawn_words(auto &&spawn_how) // each line. auto set_values(auto &&map, std::string command, auto &&...args) -> void { - auto lines = spawn_words( + auto lines = columns( exec::execl(std::move(command), std::move(args)...)); for (auto &&words : lines) { @@ -86,22 +78,22 @@ auto get_pool(std::string_view name) -> pool { std::pair{"name"sv, [&ret](auto &&v) { ret.name = v; }}, }; - set_values(handlers, path_zpool, "zpool", "get", - "-H", "-oproperty,value", "all", name); + set_values(handlers, path_zpool, + "zpool", "get", "-H", "-oproperty,value", "all", name); return ret; } auto get_pools() -> std::generator<pool> { - auto lines = spawn_words( - exec::execl(path_zpool, "zpool", "get", "-H", "-o", "name")); + auto lines = columns(exec::execl(path_zpool, + "zpool", "get", "-H", "-o", "name")); for (auto &&words : lines) co_yield get_pool(words[0]); } -auto get_filesystem(std::string_view name) -> filesystem { - auto ret = filesystem{}; +auto get_dataset(std::string_view name) -> dataset { + auto ret = dataset{}; auto handlers = std::unordered_map{ std::pair{"name"sv, [&ret](auto &&v) { ret.name = v; }}, @@ -113,12 +105,21 @@ auto get_filesystem(std::string_view name) -> filesystem { return ret; } -auto get_filesystems() -> std::generator<filesystem> { - auto lines = spawn_words( - exec::execl(path_zfs, "zfs", "get", "-H", "-o", "name")); +auto get_dataset_names() -> std::generator<std::string> { + auto lines = columns(exec::execl(path_zfs, + "zfs", "get", "-H", "-o", "name")); for (auto &&words : lines) - co_yield get_filesystem(words[0]); + co_yield std::string(words[0]); +} + +auto get_datasets() -> std::generator<dataset> { + for (auto &&dataset : get_dataset_names()) + co_yield get_dataset(dataset); +} + +auto dataset_exists(std::string name) -> bool { + return std::ranges::contains(get_dataset_names(), name); } } // namespace lfjail::zfs |
