From 7810e1b3a17a1e0cd8e5341752df837e7f53b54d Mon Sep 17 00:00:00 2001 From: BurntRanch <69512353+BurntRanch@users.noreply.github.com> Date: Thu, 18 Apr 2024 17:22:47 +0300 Subject: [PATCH] several improvements to removePkg, and some modification to make code cleaner. --- include/taur.hpp | 4 +- src/main.cpp | 54 ++++++++++++++++++++++++- src/taur.cpp | 101 +++++++++++++++++++---------------------------- src/util.cpp | 4 +- 4 files changed, 99 insertions(+), 64 deletions(-) diff --git a/include/taur.hpp b/include/taur.hpp index f41f695..29bfef7 100644 --- a/include/taur.hpp +++ b/include/taur.hpp @@ -1,6 +1,7 @@ #ifndef GIT_HPP #define GIT_HPP +#include #include #include #include @@ -42,7 +43,8 @@ class TaurBackend { bool download_pkg(string url, string out_path); optional fetch_pkg(string pkg, bool returnGit); vector fetch_pkgs(vector pkgs, bool returnGit); - bool remove_pkg(alpm_list_t *name, bool searchForeignPackages); + bool remove_pkg(alpm_pkg_t *pkgs, bool ownTransaction = true); + bool remove_pkgs(alpm_list_t *pkgs); bool handle_aur_depends(TaurPkg_t pkg, path out_path, vector localPkgs, bool useGit); bool install_pkg(string pkg_name, string extracted_path, bool onlydownload); bool update_all_aur_pkgs(string cacheDir, bool useGit); diff --git a/src/main.cpp b/src/main.cpp index 7e586b2..9b8650f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,5 @@ +#include +#include #pragma GCC diagnostic ignored "-Wvla" #include "util.hpp" @@ -256,7 +258,57 @@ bool removePkg(alpm_list_t *pkgNames) { if (!pkgNames) return false; - return backend->remove_pkg(pkgNames, config->aurOnly); + alpm_list_t *ret = nullptr; + + alpm_list_t *regexQuery = nullptr; + + for (; pkgNames; pkgNames = pkgNames->next) + regexQuery = alpm_list_add(regexQuery, (void *)(".*" + string((const char *)(pkgNames->data)) + ".*").c_str()); + + if (alpm_db_search(alpm_get_localdb(config->handle), regexQuery, &ret) != 0) + return false; + + size_t ret_length = alpm_list_count(ret); + + if (ret_length == 1) + return backend->remove_pkg((alpm_pkg_t *)ret->data); + + fmt::println("Choose packages to remove, (Seperate by spaces, type * to remove all):"); + + for (size_t i = 0; i < ret_length; i++) { + fmt::println("[{}] {}", i, alpm_pkg_get_name((alpm_pkg_t *)(alpm_list_nth(ret, i)->data))); + } + + string included; + std::getline(std::cin, included); + + vector includedIndexes = split(included, ' '); + + alpm_list_t *finalPackageList = nullptr; + alpm_list_t *finalPackageListStart = nullptr; + + if (included == "*") + return backend->remove_pkgs(ret); + + for (size_t i = 0; i < includedIndexes.size(); i++) { + try { + size_t includedIndex = (size_t)stoi(includedIndexes[i]); + + if (includedIndex >= ret_length) + continue; + + finalPackageList = alpm_list_add(finalPackageList, alpm_list_nth(ret, includedIndex)->data); + + if (finalPackageList != nullptr && finalPackageListStart == nullptr) + finalPackageListStart = finalPackageList; + + } catch (std::invalid_argument const&) { + log_println(LOG_WARN, "Invalid argument! Assuming all."); + return backend->remove_pkgs(ret); + } + } + + return backend->remove_pkgs(finalPackageListStart); } bool updateAll() { diff --git a/src/taur.cpp b/src/taur.cpp index ae1d42e..0f8c203 100644 --- a/src/taur.cpp +++ b/src/taur.cpp @@ -4,7 +4,6 @@ #include "taur.hpp" #include "config.hpp" #include "args.hpp" -#include namespace fs = std::filesystem; @@ -130,81 +129,63 @@ vector TaurBackend::fetch_pkgs(vector pkgs, bool returnGit) { return out; } -bool TaurBackend::remove_pkg(alpm_list_t *pkgQueries, bool aurOnly) { - alpm_list_t *temp_ret = nullptr; - - if (alpm_db_search(alpm_get_localdb(config.handle), pkgQueries, &temp_ret) != 0) +/** Removes a single packages using libalpm. + @param pkgs an alpm package to remove. + @param ownTransaction a bool that dictates whether this function owns the transaction, this is used for remove_pkgs, if not, it will not initialize it and not release it. + @return success. +*/ +bool TaurBackend::remove_pkg(alpm_pkg_t *pkg, bool ownTransaction) { + if (!pkg) return false; - alpm_list_smart_pointer ret(temp_ret, alpm_list_free); - - size_t ret_length = alpm_list_count(ret.get()); - - if (alpm_trans_init(this->config.handle, 0)) { + if (ownTransaction && alpm_trans_init(this->config.handle, 0)) { log_println(LOG_ERROR, "Failed to initialize transaction ({})", alpm_strerror(alpm_errno(this->config.handle))); return false; } - if (ret_length == 0) { - log_println(LOG_ERROR, "Couldn't find any packages!"); - return false; - } else if (ret_length == 1) { - log_println(LOG_INFO, "Removing package {}.", alpm_pkg_get_name((alpm_pkg_t *)(ret->data))); - if (alpm_remove_pkg(this->config.handle, (alpm_pkg_t *)(ret->data))) { - log_println(LOG_ERROR, "Failed to remove package ({})", alpm_strerror(alpm_errno(this->config.handle))); - alpm_trans_release(this->config.handle); - return false; - } + log_println(LOG_INFO, "Removing package {}.", string(alpm_pkg_get_name(pkg))); - return commitTransactionAndRelease(); + if (alpm_remove_pkg(this->config.handle, pkg) != 0) { + log_println(LOG_ERROR, "Failed to remove package ({})", alpm_strerror(alpm_errno(this->config.handle))); + if (ownTransaction) alpm_trans_release(this->config.handle); + return false; } - fmt::println("Choose packages to remove, (Seperate by spaces, type * to remove all):"); - for (size_t i = 0; i < ret_length; i++) { - fmt::println("[{}] {}", i, alpm_pkg_get_name((alpm_pkg_t *)(alpm_list_nth(ret.get(), i)->data))); - } + if (ownTransaction) + return commitTransactionAndRelease(); - string included; - std::getline(std::cin, included); - - if (included == "*") { - for (size_t i = 0; i < ret_length; i++) { - log_println(LOG_INFO, "Removing package {}.", alpm_pkg_get_name((alpm_pkg_t *)(alpm_list_nth(ret.get(), i)->data))); - int code = alpm_remove_pkg(this->config.handle, (alpm_pkg_t *)(alpm_list_nth(ret.get(), i)->data)); - if (code != 0) { - log_println(LOG_ERROR, "Failed to remove package, Exiting!"); - alpm_trans_release(this->config.handle); - return false; - } - } + return true; +} - if (!commitTransactionAndRelease()) { - log_println(LOG_ERROR, "Failed to prepare, commit, or release alpm transaction."); - return false; - } +/** Removes a list of packages using libalpm. + * Will automatically call remove_pkg instead, if the size of pkgs is equal to 1. + @param pkgs alpm list of alpm_pkg_t pointers to remove. + @return success. +*/ +bool TaurBackend::remove_pkgs(alpm_list_t *pkgs) { + if (!pkgs) + return false; - return true; + if (alpm_trans_init(this->config.handle, 0)) { + log_println(LOG_ERROR, "Failed to initialize transaction ({})", alpm_strerror(alpm_errno(this->config.handle))); + return false; } - vector includedIndexes = split(included, ' '); - string finalPackageList = ""; + size_t pkgs_length = alpm_list_count(pkgs); - for (size_t i = 0; i < includedIndexes.size(); i++) { - try { - size_t includedIndex = (size_t)stoi(includedIndexes[i]); - - if (includedIndex >= ret_length) - continue; + if (pkgs_length == 0) { + log_println(LOG_ERROR, "Couldn't find any packages!"); + alpm_trans_release(this->config.handle); + return false; + } else if (pkgs_length == 1) { + return this->remove_pkg((alpm_pkg_t *)(pkgs->data), false) && commitTransactionAndRelease(); + } - log_println(LOG_INFO, "Removing package {}.", alpm_pkg_get_name((alpm_pkg_t *)(alpm_list_nth(ret.get(), includedIndex)->data))); - int code = alpm_remove_pkg(this->config.handle, (alpm_pkg_t *)(alpm_list_nth(ret.get(), includedIndex)->data)); - if (code != 0) { - log_println(LOG_ERROR, "Failed to remove package, Exiting!"); - alpm_trans_release(this->config.handle); - return false; - } - } catch (std::invalid_argument const&) { - log_println(LOG_WARN, "Invalid argument! Assuming all."); + for (; pkgs; pkgs = pkgs->next) { + bool success = this->remove_pkg((alpm_pkg_t *)(pkgs->data), false); + if (!success) { + alpm_trans_release(this->config.handle); + return false; } } diff --git a/src/util.cpp b/src/util.cpp index ceac5cf..6e43af2 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -68,13 +68,13 @@ bool commitTransactionAndRelease(bool soft) { log_println(LOG_INFO, "Changes to be made:"); for (alpm_list_t *addPkgsClone = addPkgs; addPkgsClone; addPkgsClone = addPkgsClone->next) { fmt::print(BOLD_TEXT(config->getThemeValue("green", green)), " ++ "); - fmt::print(fmt::emphasis::bold, "{}", alpm_pkg_get_name((alpm_pkg_t *)(addPkgsClone->data))); + fmt::println(fmt::emphasis::bold, "{}", alpm_pkg_get_name((alpm_pkg_t *)(addPkgsClone->data))); } for (alpm_list_t *removePkgsClone = removePkgs; removePkgsClone; removePkgsClone = removePkgsClone->next) { fmt::print(BOLD_TEXT(config->getThemeValue("red", red)), " -- "); - fmt::print(fmt::emphasis::bold, "{}", alpm_pkg_get_name((alpm_pkg_t *)(removePkgsClone->data))); + fmt::println(fmt::emphasis::bold, "{}", alpm_pkg_get_name((alpm_pkg_t *)(removePkgsClone->data))); } fmt::print("Would you like to proceed with this transaction? [Y/n] ");