Skip to content

Commit

Permalink
🚧 properly handle errors on zfs operations
Browse files Browse the repository at this point in the history
  • Loading branch information
vnepogodin committed Sep 24, 2024
1 parent c76930d commit ff53cd8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
7 changes: 4 additions & 3 deletions gucc/include/gucc/zfs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ struct ZfsDataset final {
};

// Creates a zfs volume
void zfs_create_zvol(std::string_view zsize, std::string_view zpath) noexcept;
auto zfs_create_zvol(std::string_view zsize, std::string_view zpath) noexcept -> bool;

// Creates a zfs filesystem, the first parameter is the ZFS path and the second is the mount path
auto zfs_create_dataset(std::string_view zpath, std::string_view zmount) noexcept -> bool;

// Creates a zfs datasets from predefined scheme
auto zfs_create_datasets(const std::vector<ZfsDataset>& zdatasets) noexcept -> bool;

void zfs_destroy_dataset(std::string_view zdataset) noexcept;
auto zfs_destroy_dataset(std::string_view zdataset) noexcept -> bool;

// returns a list of imported zpools
auto zfs_list_pools() noexcept -> std::string;
Expand All @@ -31,7 +31,8 @@ auto zfs_list_devs() noexcept -> std::string;

auto zfs_list_datasets(std::string_view type = "none") noexcept -> std::string;

void zfs_set_property(std::string_view property, std::string_view dataset) noexcept;
// Sets zfs property
auto zfs_set_property(std::string_view property, std::string_view dataset) noexcept -> bool;

} // namespace gucc::fs

Expand Down
15 changes: 9 additions & 6 deletions gucc/src/zfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ using namespace std::string_view_literals;
namespace gucc::fs {

// Creates a zfs volume
void zfs_create_zvol(std::string_view zsize, std::string_view zpath) noexcept {
auto zfs_create_zvol(std::string_view zsize, std::string_view zpath) noexcept -> bool {
#ifdef NDEVENV
utils::exec(fmt::format(FMT_COMPILE("zfs create -V {}M {} 2>>/tmp/cachyos-install.log"), zsize, zpath), true);
return utils::exec_checked(fmt::format(FMT_COMPILE("zfs create -V {}M {} 2>>/tmp/cachyos-install.log"), zsize, zpath));
#else
spdlog::debug("zfs create -V {}M {}", zsize, zpath);
return true;
#endif
}

Expand All @@ -41,11 +42,12 @@ auto zfs_create_datasets(const std::vector<ZfsDataset>& zdatasets) noexcept -> b
return true;
}

void zfs_destroy_dataset(std::string_view zdataset) noexcept {
auto zfs_destroy_dataset(std::string_view zdataset) noexcept -> bool {
#ifdef NDEVENV
utils::exec(fmt::format(FMT_COMPILE("zfs destroy -r {} 2>>/tmp/cachyos-install.log"), zdataset), true);
return utils::exec_checked(fmt::format(FMT_COMPILE("zfs destroy -r {} 2>>/tmp/cachyos-install.log"), zdataset));
#else
spdlog::debug("zfs destroy -r {}", zdataset);
return true;
#endif
}

Expand Down Expand Up @@ -87,11 +89,12 @@ auto zfs_list_datasets(std::string_view type) noexcept -> std::string {
#endif
}

void zfs_set_property(std::string_view property, std::string_view dataset) noexcept {
auto zfs_set_property(std::string_view property, std::string_view dataset) noexcept -> bool {
#ifdef NDEVENV
utils::exec(fmt::format(FMT_COMPILE("zfs set {} {} 2>>/tmp/cachyos-install.log"), property, dataset), true);
return utils::exec_checked(fmt::format(FMT_COMPILE("zfs set {} {} 2>>/tmp/cachyos-install.log"), property, dataset));
#else
spdlog::debug("zfs set {} {}", property, dataset);
return true;
#endif
}

Expand Down
12 changes: 9 additions & 3 deletions src/tui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,9 @@ bool zfs_new_ds(const std::string_view& zmount = "") noexcept {

const auto& zfs_zpath = fmt::format(FMT_COMPILE("{}/{}"), zfs_zpool_name, zfs_dataset_name);
if (zmount == "legacy"sv) {
gucc::fs::zfs_create_dataset(zfs_zpath, zmount);
if (!gucc::fs::zfs_create_dataset(zfs_zpath, zmount)) {
spdlog::error("Failed to create zfs dataset {} at mountpoint {}", zfs_zpath, zmount);
}
} else if (zmount == "zvol"sv) {
static constexpr auto zvol_size_menu_body = "\nEnter the size of the zvol in megabytes(MB)\n"sv;
static constexpr auto zvol_size_menu_validation = "\nYou must enter a number greater than 0\n"sv;
Expand All @@ -1372,7 +1374,9 @@ bool zfs_new_ds(const std::string_view& zmount = "") noexcept {
if (zfs_menu_text == zvol_size_menu_body) { break; }
/* clang-format on */
}
gucc::fs::zfs_create_zvol(zvol_size, zfs_zpath);
if (!gucc::fs::zfs_create_zvol(zvol_size, zfs_zpath)) {
spdlog::error("Failed to create zfs zvol {} with size {}", zfs_zpath, zvol_size);
}
} else {
spdlog::error("HELLO! IMPLEMENT ME!");
return false;
Expand Down Expand Up @@ -1433,7 +1437,9 @@ void zfs_set_property() noexcept {
}

// Set the property
gucc::fs::zfs_set_property(zfs_property_ent, zdataset);
if (!gucc::fs::zfs_set_property(zfs_property_ent, zdataset)) {
spdlog::error("Failed to set zfs property '{}' on dataset '{}'", zfs_property_ent, zdataset);
}
}

void zfs_destroy_dataset() noexcept {
Expand Down

0 comments on commit ff53cd8

Please sign in to comment.