From 63aaba0488d4c2b23d6415cb0c74dfea7e1f811b Mon Sep 17 00:00:00 2001 From: Toni500git Date: Sun, 9 Jun 2024 17:22:27 +0200 Subject: [PATCH 01/14] sync: re-factor installPkg() plus some fix when git pulling packages --- src/main.cpp | 175 ++++++++++++++++++++++++++++----------------------- src/taur.cpp | 8 +-- 2 files changed, 102 insertions(+), 81 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index c02f69b..65a1451 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,6 +17,7 @@ * along with this program. If not, see . */ +#include #pragma GCC diagnostic ignored "-Wvla" #include "args.hpp" @@ -144,7 +145,8 @@ int installPkg(alpm_list_t *pkgNames) { bool useGit = config->useGit; path cacheDir = config->cacheDir; - bool returnStatus = true, stat; + bool returnStatus = true; + bool stat; vector pacmanPkgs; // list of pacman packages to install, to avoid spamming pacman. vector pkgNamesVec; @@ -184,101 +186,69 @@ int installPkg(alpm_list_t *pkgNames) { // this feels horrible despite being 100% correct and probably not problematic. // it just feels like we should ask for every package. + // Toni500 note: do someone agree with this above? because I don't vector AURPkgs = filterAURPkgsNames(pkgNamesVec, alpm_get_syncdbs(config->handle), true); + for (const auto& pkg : pkgNamesVec) { + // Check if pkg is not in aurPkgNamesSet + if (std::find(AURPkgs.begin(), AURPkgs.end(), pkg) == AURPkgs.end()) { + pacmanPkgs.push_back(pkg.data()); + } + } + if (!op.op_s_cleanbuild && !AURPkgs.empty()) pkgsToCleanBuild = askUserForList(AURPkgs, PROMPT_LIST_CLEANBUILDS); if (!config->noconfirm && !AURPkgs.empty()) pkgsToReview = askUserForList(AURPkgs, PROMPT_LIST_REVIEWS); - - for (size_t i = 0; i < pkgNamesVec.size(); i++) { - vector pkgs = backend->search(pkgNamesVec[i], useGit, config->aurOnly, true); + for (auto& pkg_name : AURPkgs) { - optional> oSelectedPkg = askUserForPkg(pkgs, *backend, useGit); + path pkgDir = path(cacheDir) / pkg_name; - if (!oSelectedPkg) { + stat = useGit ? backend->download_git(AUR_URL_GIT(pkg_name), pkgDir) : backend->download_tar(AUR_URL_TAR(pkg_name), pkgDir); + if (!stat) { + log_println(ERROR, _("Failed to download {}"), pkg_name); returnStatus = false; continue; } - vector selectedPkg = oSelectedPkg.value(); - - for (size_t i = 0; i < selectedPkg.size(); i++) { - TaurPkg_t pkg = selectedPkg[i]; - - if (pkg.db_name != "aur") { - pacmanPkgs.push_back(pkg.name); - continue; - } - - path pkgDir = path(cacheDir) / pkg.name; - bool cleanBuild = op.op_s_cleanbuild ? false : std::find(pkgsToCleanBuild.begin(), pkgsToCleanBuild.end(), pkg.name) != pkgsToCleanBuild.end(); - bool review = config->noconfirm ? false : std::find(pkgsToReview.begin(), pkgsToReview.end(), pkg.name) != pkgsToReview.end(); - - if (cleanBuild) { - log_println(INFO, _("Removing {}"), pkgDir.c_str()); - std::filesystem::remove_all(pkgDir); - } - - stat = useGit ? backend->download_git(AUR_URL_GIT(pkg.name), pkgDir) : backend->download_tar(AUR_URL_TAR(pkg.name), pkgDir); - if (!stat) { - log_println(ERROR, _("Failed to download {}"), pkg.name); - returnStatus = false; - continue; - } - - if (review || askUserYorN(YES, PROMPT_YN_EDIT_PKGBUILD, pkg.name)) { - // cmd is just a workaround for making possible - // that editor can have flags, e.g nano --modernbindings - // instead of creating another config variable - // This is really ugly - // because u can't convert std::vector to std::vector - vector _cmd; - vector cmd; - for (auto& str : config->editor) - _cmd.push_back(str); - _cmd.push_back((pkgDir / "PKGBUILD").string()); - - for (auto& str : _cmd) - cmd.push_back(str.c_str()); - - taur_exec(cmd); - - if (!askUserYorN(YES, PROMPT_YN_PROCEED_INSTALL)) - return false; - } - - stat = backend->handle_aur_depends(pkg, cacheDir, backend->get_all_local_pkgs(true), useGit); - - if (!stat) { - log_println(ERROR, _("Installing AUR dependencies for your package has failed.")); - returnStatus = false; - continue; - } - - stat = backend->build_pkg(pkg.name, pkgDir, false); - - if (!stat) { - log_println(ERROR, _("Building your package has failed.")); - returnStatus = false; - continue; - } + } - pkgs_to_install += built_pkg + ' '; + for (string_view& pkg : pkgsToCleanBuild) { + path pkgDir = path(cacheDir) / pkg; + if (!useGit) { + log_println(INFO, _("Removing {}"), pkgDir.c_str()); + std::filesystem::remove_all(pkgDir); + } + else { + log_println(INFO, _("Cleaning {}"), pkgDir.c_str()); + taur_exec({config->git.c_str(), "-C", pkgDir.c_str(), "clean", "-xffd"}); } } - if (!pkgs_to_install.empty()) { - log_println(DEBUG, _("Installing {}"), fmt::join(pkgNamesVec, " ")); - pkgs_to_install.erase(pkgs_to_install.length() - 1); - if (!pacman_exec("-U", split(pkgs_to_install, ' '), false)) { - log_println(ERROR, _("Failed to install {}"), fmt::join(pkgNamesVec, " ")); - returnStatus = false; - } + // cmd is just a workaround for making possible + // that editor can have flags, e.g nano --modernbindings + // instead of creating another config variable + // This is really ugly + // because you can't convert std::vector to std::vector + for (string_view& pkg : pkgsToReview) { + path pkgDir = path(cacheDir) / pkg; + vector _cmd; + vector cmd; + for (auto& str : config->editor) + _cmd.push_back(str); + _cmd.push_back((pkgDir / "PKGBUILD").string()); + + for (auto& str : _cmd) + cmd.push_back(str.c_str()); + + taur_exec(cmd); } - + + if (!pkgsToReview.empty() && !askUserYorN(YES, PROMPT_YN_PROCEED_INSTALL)) + return false; + if (!config->aurOnly && (op.op_s_upgrade || !pacmanPkgs.empty())) { if (op.op_s_upgrade) log_println(INFO, _("Upgrading system packages!")); @@ -301,6 +271,57 @@ int installPkg(alpm_list_t *pkgNames) { backend->update_all_aur_pkgs(cacheDir, useGit); } + for (size_t i = 0; i < AURPkgs.size(); i++) { + vector pkgs = backend->search(AURPkgs[i], useGit, config->aurOnly, true); + + optional> oSelectedPkg = askUserForPkg(pkgs, *backend, useGit); + + if (!oSelectedPkg) { + returnStatus = false; + continue; + } + + vector selectedPkg = oSelectedPkg.value(); + + TaurPkg_t pkg = selectedPkg[0]; + + path pkgDir = path(cacheDir) / pkg.name; + + stat = backend->handle_aur_depends(pkg, cacheDir, backend->get_all_local_pkgs(true), useGit); + + if (!stat) { + log_println(ERROR, _("Installing AUR dependencies for your package has failed.")); + returnStatus = false; + continue; + } + + stat = backend->build_pkg(pkg.name, pkgDir, false); + + if (!stat) { + log_println(ERROR, _("Building {} has failed."), pkg.name); + returnStatus = false; + pkgs_failed_to_build += pkg.name + ' '; + log_println(DEBUG, "pkgs_failed_to_build = {}", pkgs_failed_to_build); + continue; + } + + pkgs_to_install += built_pkg + ' '; + } + + if (!pkgs_to_install.empty()) { + log_println(DEBUG, _("Installing {}"), fmt::join(pkgNamesVec, " ")); + pkgs_to_install.erase(pkgs_to_install.length() - 1); + if (!pacman_exec("-U", split(pkgs_to_install, ' '), false)) { + log_println(ERROR, _("Failed to install {}"), fmt::join(pkgNamesVec, " ")); + returnStatus = false; + } + } + + if (!pkgs_failed_to_build.empty()) { + pkgs_failed_to_build.erase(pkgs_failed_to_build.end() - 1); + log_println(WARN, fg(color.red), _("Failed to upgrade: {}"), pkgs_failed_to_build); + log_println(INFO, fg(color.cyan), _("Tip: try to run taur with \"-S {}\" (e.g \"taur -S {}\")"), pkgs_failed_to_build, pkgs_failed_to_build); + } return returnStatus; } diff --git a/src/taur.cpp b/src/taur.cpp index a88a508..4b55369 100644 --- a/src/taur.cpp +++ b/src/taur.cpp @@ -9,7 +9,7 @@ TaurBackend::TaurBackend(Config& cfg) : config(cfg) {} bool TaurBackend::download_git(string_view url, path out_path) { if (std::filesystem::exists(path(out_path) / ".git")) { - return taur_exec({config.git.c_str(), "-C", out_path.c_str(), "pull", "--rebase", "--autostash", "--ff-only"}); + return taur_exec({config.git.c_str(), "-C", out_path.c_str(), "pull", "--autostash", "--rebase", "--ff-only", "--force"}); } else { if (std::filesystem::exists(path(out_path))) std::filesystem::remove_all(out_path); @@ -238,7 +238,7 @@ bool TaurBackend::build_pkg(string_view pkg_name, string extracted_path, bool al if (!alreadyprepared) { log_println(INFO, _("Verifying package sources..")); - makepkg_exec({"--verifysource", "--skippgpcheck", "-f", "-Cc"}); + makepkg_exec({"--verifysource", "--skippgpcheck", "-fs", "-Cc"}); log_println(INFO, _("Preparing for compilation..")); makepkg_exec({"--nobuild", "--skippgpcheck", "-fs", "-C", "--ignorearch"}); @@ -615,11 +615,11 @@ vector TaurBackend::search(string_view query, bool useGit, bool aurOn if (!aurOnly) pacPkgs = this->search_pac(query); - size_t count = aurPkgs.size() + pacPkgs.size(); + size_t allPkgsSize = aurPkgs.size() + pacPkgs.size(); vector combined; - combined.reserve(count); + combined.reserve(allPkgsSize); if (!aurPkgs.empty()) combined.insert(combined.end(), aurPkgs.begin(), aurPkgs.end()); From 2476d4e2453d3bfad9b9ea3732abbe6dede5816b Mon Sep 17 00:00:00 2001 From: BurntRanch <69512353+BurntRanch@users.noreply.github.com> Date: Sun, 9 Jun 2024 20:22:00 +0300 Subject: [PATCH 02/14] re-added functionality to loop through each selected package --- src/main.cpp | 59 +++++++++++++++++++++++----------------------------- 1 file changed, 26 insertions(+), 33 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 65a1451..38c7255 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -184,9 +184,7 @@ int installPkg(alpm_list_t *pkgNames) { if (!update_aur_cache()) log_println(ERROR, _("Failed to get information about {}"), (config->cacheDir / "packages.aur").string()); - // this feels horrible despite being 100% correct and probably not problematic. - // it just feels like we should ask for every package. - // Toni500 note: do someone agree with this above? because I don't + // I swear there was a comment here.. vector AURPkgs = filterAURPkgsNames(pkgNamesVec, alpm_get_syncdbs(config->handle), true); for (const auto& pkg : pkgNamesVec) { @@ -227,21 +225,16 @@ int installPkg(alpm_list_t *pkgNames) { } } - // cmd is just a workaround for making possible - // that editor can have flags, e.g nano --modernbindings + // cmd is just a workaround for making it possible + // to pass flags to the editor, e.g nano --modernbindings // instead of creating another config variable - // This is really ugly - // because you can't convert std::vector to std::vector for (string_view& pkg : pkgsToReview) { - path pkgDir = path(cacheDir) / pkg; - vector _cmd; + path pkgBuildFile = path(cacheDir) / pkg; vector cmd; - for (auto& str : config->editor) - _cmd.push_back(str); - _cmd.push_back((pkgDir / "PKGBUILD").string()); - for (auto& str : _cmd) + for (auto& str : config->editor) cmd.push_back(str.c_str()); + cmd.push_back((pkgBuildFile / "PKGBUILD").c_str()); taur_exec(cmd); } @@ -274,38 +267,38 @@ int installPkg(alpm_list_t *pkgNames) { for (size_t i = 0; i < AURPkgs.size(); i++) { vector pkgs = backend->search(AURPkgs[i], useGit, config->aurOnly, true); - optional> oSelectedPkg = askUserForPkg(pkgs, *backend, useGit); + optional> oSelectedPkgs = askUserForPkg(pkgs, *backend, useGit); - if (!oSelectedPkg) { + if (!oSelectedPkgs) { returnStatus = false; continue; } - vector selectedPkg = oSelectedPkg.value(); + vector selectedPkgs = oSelectedPkgs.value(); - TaurPkg_t pkg = selectedPkg[0]; + for (TaurPkg_t &pkg : selectedPkgs) { + path pkgDir = path(cacheDir) / pkg.name; - path pkgDir = path(cacheDir) / pkg.name; + stat = backend->handle_aur_depends(pkg, cacheDir, backend->get_all_local_pkgs(true), useGit); - stat = backend->handle_aur_depends(pkg, cacheDir, backend->get_all_local_pkgs(true), useGit); + if (!stat) { + log_println(ERROR, _("Installing AUR dependencies for your package has failed.")); + returnStatus = false; + continue; + } - if (!stat) { - log_println(ERROR, _("Installing AUR dependencies for your package has failed.")); - returnStatus = false; - continue; - } + stat = backend->build_pkg(pkg.name, pkgDir, false); - stat = backend->build_pkg(pkg.name, pkgDir, false); + if (!stat) { + log_println(ERROR, _("Building {} has failed."), pkg.name); + returnStatus = false; + pkgs_failed_to_build += pkg.name + ' '; + log_println(DEBUG, "pkgs_failed_to_build = {}", pkgs_failed_to_build); + continue; + } - if (!stat) { - log_println(ERROR, _("Building {} has failed."), pkg.name); - returnStatus = false; - pkgs_failed_to_build += pkg.name + ' '; - log_println(DEBUG, "pkgs_failed_to_build = {}", pkgs_failed_to_build); - continue; + pkgs_to_install += built_pkg + ' '; } - - pkgs_to_install += built_pkg + ' '; } if (!pkgs_to_install.empty()) { From 1257aaf5ed61e15a7eb6ce581c3453facee74e2b Mon Sep 17 00:00:00 2001 From: Toni500git Date: Sun, 9 Jun 2024 19:24:01 +0200 Subject: [PATCH 03/14] general: use vector instead of vector in taur_exec() --- include/util.hpp | 2 +- src/main.cpp | 20 ++++++++++---------- src/taur.cpp | 4 ++-- src/util.cpp | 44 ++++++++++++++++++++++++-------------------- 4 files changed, 37 insertions(+), 33 deletions(-) diff --git a/include/util.hpp b/include/util.hpp index 0add143..338b760 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -90,7 +90,7 @@ string expandVar(string& str); bool is_numerical(string_view s, bool allowSpace = false); bool taur_read_exec(vector cmd, string& output, bool exitOnFailure = true); void interruptHandler(int); -bool taur_exec(vector cmd, bool exitOnFailure = true); +bool taur_exec(vector cmd, bool exitOnFailure = true); void sanitizeStr(string& str); bool is_package_from_syncdb(const char *name, alpm_list_t *syncdbs); bool commitTransactionAndRelease(bool soft = false); diff --git a/src/main.cpp b/src/main.cpp index 38c7255..0fbb971 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -138,6 +138,7 @@ void execPacman(int argc, char *argv[]) { if (execvp(args[0], const_cast(args.data())) == -1) perror("execvp"); + exit(1); } @@ -187,11 +188,10 @@ int installPkg(alpm_list_t *pkgNames) { // I swear there was a comment here.. vector AURPkgs = filterAURPkgsNames(pkgNamesVec, alpm_get_syncdbs(config->handle), true); - for (const auto& pkg : pkgNamesVec) { - // Check if pkg is not in aurPkgNamesSet - if (std::find(AURPkgs.begin(), AURPkgs.end(), pkg) == AURPkgs.end()) { + for (const auto& pkg : pkgNamesVec) + { + if (std::find(AURPkgs.begin(), AURPkgs.end(), pkg) == AURPkgs.end()) pacmanPkgs.push_back(pkg.data()); - } } if (!op.op_s_cleanbuild && !AURPkgs.empty()) @@ -221,7 +221,7 @@ int installPkg(alpm_list_t *pkgNames) { } else { log_println(INFO, _("Cleaning {}"), pkgDir.c_str()); - taur_exec({config->git.c_str(), "-C", pkgDir.c_str(), "clean", "-xffd"}); + taur_exec({config->git, "-C", pkgDir.c_str(), "clean", "-xffd"}); } } @@ -229,12 +229,12 @@ int installPkg(alpm_list_t *pkgNames) { // to pass flags to the editor, e.g nano --modernbindings // instead of creating another config variable for (string_view& pkg : pkgsToReview) { - path pkgBuildFile = path(cacheDir) / pkg; - vector cmd; - + path pkgDir = path(cacheDir) / pkg; + + vector cmd; for (auto& str : config->editor) - cmd.push_back(str.c_str()); - cmd.push_back((pkgBuildFile / "PKGBUILD").c_str()); + cmd.push_back(str); + cmd.push_back((pkgDir / "PKGBUILD").string()); taur_exec(cmd); } diff --git a/src/taur.cpp b/src/taur.cpp index 4b55369..f56b2a1 100644 --- a/src/taur.cpp +++ b/src/taur.cpp @@ -9,11 +9,11 @@ TaurBackend::TaurBackend(Config& cfg) : config(cfg) {} bool TaurBackend::download_git(string_view url, path out_path) { if (std::filesystem::exists(path(out_path) / ".git")) { - return taur_exec({config.git.c_str(), "-C", out_path.c_str(), "pull", "--autostash", "--rebase", "--ff-only", "--force"}); + return taur_exec({config.git.c_str(), "-C", out_path, "pull", "--autostash", "--rebase", "--ff-only", "--force"}); } else { if (std::filesystem::exists(path(out_path))) std::filesystem::remove_all(out_path); - return taur_exec({config.git.c_str(), "clone", url.data(), out_path.c_str()}); + return taur_exec({config.git.c_str(), "clone", url.data(), out_path}); } } diff --git a/src/util.cpp b/src/util.cpp index 63c04cf..520c1f8 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -425,11 +425,15 @@ bool taur_read_exec(vector cmd, string& output, bool exitOnFailure } /** Executes commands with execvp() and keep the program running without existing - * @param cmd The command to execute + * @param cmd_str The command to execute * @param exitOnFailure Whether to call exit(1) on command failure. * @return true if the command successed, else false */ -bool taur_exec(vector cmd, bool exitOnFailure) { +bool taur_exec(vector cmd_str, bool exitOnFailure) { + + vector cmd; + for (string& str : cmd_str) + cmd.push_back(str.c_str()); int pid = fork(); @@ -466,21 +470,21 @@ bool taur_exec(vector cmd, bool exitOnFailure) { * @return true if the command successed, else false */ bool makepkg_exec(vector const& args, bool exitOnFailure) { - vector ccmd = {config->makepkgBin.c_str()}; + vector cmd = {config->makepkgBin}; if (config->noconfirm) - ccmd.push_back("--noconfirm"); + cmd.push_back("--noconfirm"); if (!config->colors) - ccmd.push_back("--nocolor"); + cmd.push_back("--nocolor"); - ccmd.push_back("--config"); - ccmd.push_back(config->makepkgConf.c_str()); + cmd.push_back("--config"); + cmd.push_back(config->makepkgConf); for (auto& str : args) - ccmd.push_back(str.c_str()); + cmd.push_back(str.c_str()); - return taur_exec(ccmd, exitOnFailure); + return taur_exec(cmd, exitOnFailure); } /** Convinient way to executes pacman commands with taur_exec() and keep the program running without existing @@ -492,29 +496,29 @@ bool makepkg_exec(vector const& args, bool exitOnFailure) { * @return true if the command successed, else false */ bool pacman_exec(string_view op, vector const& args, bool exitOnFailure, bool root) { - vector ccmd; + vector cmd; if (root) - ccmd = {config->sudo.c_str(), "pacman", op.data()}; + cmd = {config->sudo, "pacman", op.data()}; else - ccmd = {"pacman", op.data()}; + cmd = {"pacman", op.data()}; if (config->noconfirm) - ccmd.push_back("--noconfirm"); + cmd.push_back("--noconfirm"); if (config->colors) - ccmd.push_back("--color=auto"); + cmd.push_back("--color=auto"); else - ccmd.push_back("--color=never"); + cmd.push_back("--color=never"); - ccmd.push_back("--config"); - ccmd.push_back(config->pmConfig.c_str()); - ccmd.push_back("--"); + cmd.push_back("--config"); + cmd.push_back(config->pmConfig); + cmd.push_back("--"); for (auto& str : args) - ccmd.push_back(str.c_str()); + cmd.push_back(str); - return taur_exec(ccmd, exitOnFailure); + return taur_exec(cmd, exitOnFailure); } /** Free a list and every single item in it. From 1168e604ccee23f7da56ca53a6ee19b2a0aad6bd Mon Sep 17 00:00:00 2001 From: Toni500git Date: Sun, 9 Jun 2024 21:37:45 +0200 Subject: [PATCH 04/14] sync: fix makepkg command --- src/taur.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/taur.cpp b/src/taur.cpp index f56b2a1..f8a1d6e 100644 --- a/src/taur.cpp +++ b/src/taur.cpp @@ -251,7 +251,7 @@ bool TaurBackend::build_pkg(string_view pkg_name, string extracted_path, bool al /*log_println(INFO, _("Compiling {} in 3 seconds, you can cancel at this point if you can't compile."), pkg_name); sleep(3);*/ - return makepkg_exec({"-f", "--noconfirm", "--noextract", "--noprepare", "--nocheck", "--holdver", "--ignorearch", "-c"}, false); + return makepkg_exec({"-fs", "--noconfirm", "--noextract", "--noprepare", "--nocheck", "--holdver", "--ignorearch", "-c"}, false); } else log_println(INFO, _("{} exists already, skipping..."), built_pkg); From 51a7b6ae66a991cbb4e30318f72b61af3a82ed00 Mon Sep 17 00:00:00 2001 From: BurntRanch <69512353+BurntRanch@users.noreply.github.com> Date: Thu, 20 Jun 2024 01:00:01 +0300 Subject: [PATCH 05/14] Slight code changes, and fix sysupgrade --- src/main.cpp | 2 +- src/taur.cpp | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 0fbb971..96d4d4f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -371,7 +371,7 @@ bool removePkg(alpm_list_t *pkgNames) { try { auto pkg = std::find(pkgs.begin(), pkgs.end(), includedPkgs[i]); - if (pkgs.begin() >= pkg || pkg >= pkgs.end()) + if (pkg >= pkgs.end()) continue; size_t pkgIndex = std::distance(pkgs.begin(), pkg); diff --git a/src/taur.cpp b/src/taur.cpp index f8a1d6e..e6564dd 100644 --- a/src/taur.cpp +++ b/src/taur.cpp @@ -493,8 +493,6 @@ bool TaurBackend::update_all_aur_pkgs(path cacheDir, bool useGit) { goto text; } - // remove the last ' ' so we can pass it to pacman - pkgs_to_install.erase(pkgs_to_install.end() - 1); if (!pacman_exec("-U", split(pkgs_to_install, ' '), false)) { log_println(ERROR, _("Failed to install/upgrade packages")); return false; From 17b4ebb68e87ce011c2a7dc9a13e16a2b1a23575 Mon Sep 17 00:00:00 2001 From: BurntRanch <69512353+BurntRanch@users.noreply.github.com> Date: Fri, 21 Jun 2024 18:34:42 +0300 Subject: [PATCH 06/14] test: load and install each AUR package after upgrade --- include/taur.hpp | 2 +- include/util.hpp | 1 + src/main.cpp | 10 ++++++---- src/taur.cpp | 37 +++++++++++++++++++++++-------------- src/util.cpp | 22 ++++++++++++++++++++++ 5 files changed, 53 insertions(+), 19 deletions(-) diff --git a/include/taur.hpp b/include/taur.hpp index f6dcf4e..9b3eef4 100644 --- a/include/taur.hpp +++ b/include/taur.hpp @@ -66,6 +66,6 @@ class TaurBackend { vector get_all_local_pkgs(bool aurOnly); }; -inline string built_pkg, pkgs_to_install, pkgs_failed_to_build; +inline string built_pkg; #endif diff --git a/include/util.hpp b/include/util.hpp index 338b760..991e704 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -117,6 +117,7 @@ std::optional> askUserForPkg(vector pkgs, TaurBacke string_view binarySearch(const vector& arr, string_view target); vector load_aur_list(); bool update_aur_cache(bool recursiveCall = false); +bool install_package_files(const vector &packageFiles, alpm_handle_t *handle, int flags); template struct is_fmt_convertible { diff --git a/src/main.cpp b/src/main.cpp index 96d4d4f..e71d2c9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -264,6 +264,8 @@ int installPkg(alpm_list_t *pkgNames) { backend->update_all_aur_pkgs(cacheDir, useGit); } + vector pkgs_to_install, pkgs_failed_to_build; + for (size_t i = 0; i < AURPkgs.size(); i++) { vector pkgs = backend->search(AURPkgs[i], useGit, config->aurOnly, true); @@ -292,19 +294,19 @@ int installPkg(alpm_list_t *pkgNames) { if (!stat) { log_println(ERROR, _("Building {} has failed."), pkg.name); returnStatus = false; - pkgs_failed_to_build += pkg.name + ' '; + pkgs_failed_to_build.push_back(pkg.name); log_println(DEBUG, "pkgs_failed_to_build = {}", pkgs_failed_to_build); continue; } - pkgs_to_install += built_pkg + ' '; + pkgs_to_install.push_back(built_pkg); } } if (!pkgs_to_install.empty()) { log_println(DEBUG, _("Installing {}"), fmt::join(pkgNamesVec, " ")); - pkgs_to_install.erase(pkgs_to_install.length() - 1); - if (!pacman_exec("-U", split(pkgs_to_install, ' '), false)) { + + if (!install_package_files(pkgs_to_install, config->handle, config->flags)) { log_println(ERROR, _("Failed to install {}"), fmt::join(pkgNamesVec, " ")); returnStatus = false; } diff --git a/src/taur.cpp b/src/taur.cpp index e6564dd..236aa49 100644 --- a/src/taur.cpp +++ b/src/taur.cpp @@ -3,6 +3,7 @@ #include "taur.hpp" #include "config.hpp" #include "util.hpp" +#include #include TaurBackend::TaurBackend(Config& cfg) : config(cfg) {} @@ -382,14 +383,15 @@ bool TaurBackend::update_all_aur_pkgs(path cacheDir, bool useGit) { return true; } - vector pkgNames; - pkgNames.reserve(pkgs.size()); + vector pkgNames(pkgs.size()); for (size_t i = 0; i < pkgs.size(); ++i) - pkgNames.push_back(pkgs[i].name); + pkgNames[i] = pkgs[i].name; vector onlinePkgs = this->fetch_pkgs(pkgNames, useGit); + vector pkgs_to_install, pkgs_failed_to_build; + int updatedPkgs = 0; int attemptedDownloads = 0; @@ -416,7 +418,9 @@ bool TaurBackend::update_all_aur_pkgs(path cacheDir, bool useGit) { continue; } - if (pkgs[pkgIndex].version == onlinePkgs[i].version) { + bool isGitPackage = hasEnding(pkgs[pkgIndex].name, "-git"); + + if (pkgs[pkgIndex].version == onlinePkgs[i].version && !isGitPackage) { log_println(DEBUG, "pkg {} has no update, local: {}, online: {}, skipping!", pkgs[pkgIndex].name, pkgs[pkgIndex].version, onlinePkgs[i].version); continue; } @@ -436,7 +440,7 @@ bool TaurBackend::update_all_aur_pkgs(path cacheDir, bool useGit) { } // workaround for -git package because they are "special" - if (hasEnding(pkgs[pkgIndex].name, "-git")) { + if (isGitPackage) { alrprepared = true; std::filesystem::current_path(pkgDir); makepkg_exec({"--verifysource", "-fA"}); @@ -475,15 +479,15 @@ bool TaurBackend::update_all_aur_pkgs(path cacheDir, bool useGit) { bool installSuccess = this->build_pkg(pkgs[pkgIndex].name, pkgDir, alrprepared); if (installSuccess) { - pkgs_to_install += built_pkg + ' '; - log_println(DEBUG, "pkgs_to_install = {}", pkgs_to_install); + pkgs_to_install.push_back(built_pkg); + log_println(DEBUG, "pkgs_to_install = {}", fmt::join(pkgs_to_install, ", ")); updatedPkgs++; // prevent duplicated entries pkgs.erase(pkgs.begin() + pkgIndex); } else { - pkgs_failed_to_build += pkgs[pkgIndex].name + ' '; - log_println(DEBUG, "pkgs_failed_to_build = {}", pkgs_failed_to_build); + pkgs_failed_to_build.push_back(pkgs[pkgIndex].name); + log_println(DEBUG, "pkgs_failed_to_build = [{}]", fmt::join(pkgs_failed_to_build, ", ")); } } @@ -493,12 +497,17 @@ bool TaurBackend::update_all_aur_pkgs(path cacheDir, bool useGit) { goto text; } - if (!pacman_exec("-U", split(pkgs_to_install, ' '), false)) { - log_println(ERROR, _("Failed to install/upgrade packages")); - return false; - } + { + // if (!pacman_exec("-U", pkgs_to_install, false)) { + // log_println(ERROR, _("Failed to install/upgrade packages")); + // return false; + // } + + if (install_package_files(pkgs_to_install, this->config.handle, this->config.flags)) + log_println(INFO, _("Upgraded {}/{} packages."), updatedPkgs, attemptedDownloads); - log_println(INFO, _("Upgraded {}/{} packages."), updatedPkgs, attemptedDownloads); + goto text; + } text: if (attemptedDownloads > updatedPkgs) { diff --git a/src/util.cpp b/src/util.cpp index 520c1f8..f5a360a 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -733,6 +733,28 @@ bool update_aur_cache(bool recursiveCall) { return true; } +bool install_package_files(const vector &packageFiles, alpm_handle_t *handle, int flags) { + if (alpm_trans_init(handle, flags)) { + log_println(ERROR, _("Failed to initialize transaction ({})"), alpm_strerror(alpm_errno(handle))); + return false; + } + + for (const string &packageFile : packageFiles) { + alpm_pkg_t *pkg; + if (alpm_pkg_load(handle, packageFile.c_str(), 1, alpm_option_get_local_file_siglevel(handle), &pkg)) { + log_println(ERROR, _("Failed to load package {}! ({})"), packageFile, alpm_strerror(alpm_errno(handle))); + return false; + } + + if (alpm_add_pkg(handle, pkg)) { + log_println(ERROR, _("Failed to add package {}! ({})"), packageFile, alpm_strerror(alpm_errno(handle))); + return false; + } + } + + return commitTransactionAndRelease(true); +} + /** Ask the user to select a package out of a list. * @param pkgs The list of packages, in a vector * @param backend A reference to the TaurBackend responsible for fetching any AUR packages. From 96752247e62fdef314ae2b2575f51aae73a8e2cf Mon Sep 17 00:00:00 2001 From: BurntRanch <69512353+BurntRanch@users.noreply.github.com> Date: Fri, 21 Jun 2024 18:43:37 +0300 Subject: [PATCH 07/14] Revert "test: load and install each AUR package after upgrade" This reverts commit 17b4ebb68e87ce011c2a7dc9a13e16a2b1a23575. --- include/taur.hpp | 2 +- include/util.hpp | 1 - src/main.cpp | 10 ++++------ src/taur.cpp | 37 ++++++++++++++----------------------- src/util.cpp | 22 ---------------------- 5 files changed, 19 insertions(+), 53 deletions(-) diff --git a/include/taur.hpp b/include/taur.hpp index 9b3eef4..f6dcf4e 100644 --- a/include/taur.hpp +++ b/include/taur.hpp @@ -66,6 +66,6 @@ class TaurBackend { vector get_all_local_pkgs(bool aurOnly); }; -inline string built_pkg; +inline string built_pkg, pkgs_to_install, pkgs_failed_to_build; #endif diff --git a/include/util.hpp b/include/util.hpp index 991e704..338b760 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -117,7 +117,6 @@ std::optional> askUserForPkg(vector pkgs, TaurBacke string_view binarySearch(const vector& arr, string_view target); vector load_aur_list(); bool update_aur_cache(bool recursiveCall = false); -bool install_package_files(const vector &packageFiles, alpm_handle_t *handle, int flags); template struct is_fmt_convertible { diff --git a/src/main.cpp b/src/main.cpp index e71d2c9..96d4d4f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -264,8 +264,6 @@ int installPkg(alpm_list_t *pkgNames) { backend->update_all_aur_pkgs(cacheDir, useGit); } - vector pkgs_to_install, pkgs_failed_to_build; - for (size_t i = 0; i < AURPkgs.size(); i++) { vector pkgs = backend->search(AURPkgs[i], useGit, config->aurOnly, true); @@ -294,19 +292,19 @@ int installPkg(alpm_list_t *pkgNames) { if (!stat) { log_println(ERROR, _("Building {} has failed."), pkg.name); returnStatus = false; - pkgs_failed_to_build.push_back(pkg.name); + pkgs_failed_to_build += pkg.name + ' '; log_println(DEBUG, "pkgs_failed_to_build = {}", pkgs_failed_to_build); continue; } - pkgs_to_install.push_back(built_pkg); + pkgs_to_install += built_pkg + ' '; } } if (!pkgs_to_install.empty()) { log_println(DEBUG, _("Installing {}"), fmt::join(pkgNamesVec, " ")); - - if (!install_package_files(pkgs_to_install, config->handle, config->flags)) { + pkgs_to_install.erase(pkgs_to_install.length() - 1); + if (!pacman_exec("-U", split(pkgs_to_install, ' '), false)) { log_println(ERROR, _("Failed to install {}"), fmt::join(pkgNamesVec, " ")); returnStatus = false; } diff --git a/src/taur.cpp b/src/taur.cpp index 236aa49..e6564dd 100644 --- a/src/taur.cpp +++ b/src/taur.cpp @@ -3,7 +3,6 @@ #include "taur.hpp" #include "config.hpp" #include "util.hpp" -#include #include TaurBackend::TaurBackend(Config& cfg) : config(cfg) {} @@ -383,15 +382,14 @@ bool TaurBackend::update_all_aur_pkgs(path cacheDir, bool useGit) { return true; } - vector pkgNames(pkgs.size()); + vector pkgNames; + pkgNames.reserve(pkgs.size()); for (size_t i = 0; i < pkgs.size(); ++i) - pkgNames[i] = pkgs[i].name; + pkgNames.push_back(pkgs[i].name); vector onlinePkgs = this->fetch_pkgs(pkgNames, useGit); - vector pkgs_to_install, pkgs_failed_to_build; - int updatedPkgs = 0; int attemptedDownloads = 0; @@ -418,9 +416,7 @@ bool TaurBackend::update_all_aur_pkgs(path cacheDir, bool useGit) { continue; } - bool isGitPackage = hasEnding(pkgs[pkgIndex].name, "-git"); - - if (pkgs[pkgIndex].version == onlinePkgs[i].version && !isGitPackage) { + if (pkgs[pkgIndex].version == onlinePkgs[i].version) { log_println(DEBUG, "pkg {} has no update, local: {}, online: {}, skipping!", pkgs[pkgIndex].name, pkgs[pkgIndex].version, onlinePkgs[i].version); continue; } @@ -440,7 +436,7 @@ bool TaurBackend::update_all_aur_pkgs(path cacheDir, bool useGit) { } // workaround for -git package because they are "special" - if (isGitPackage) { + if (hasEnding(pkgs[pkgIndex].name, "-git")) { alrprepared = true; std::filesystem::current_path(pkgDir); makepkg_exec({"--verifysource", "-fA"}); @@ -479,15 +475,15 @@ bool TaurBackend::update_all_aur_pkgs(path cacheDir, bool useGit) { bool installSuccess = this->build_pkg(pkgs[pkgIndex].name, pkgDir, alrprepared); if (installSuccess) { - pkgs_to_install.push_back(built_pkg); - log_println(DEBUG, "pkgs_to_install = {}", fmt::join(pkgs_to_install, ", ")); + pkgs_to_install += built_pkg + ' '; + log_println(DEBUG, "pkgs_to_install = {}", pkgs_to_install); updatedPkgs++; // prevent duplicated entries pkgs.erase(pkgs.begin() + pkgIndex); } else { - pkgs_failed_to_build.push_back(pkgs[pkgIndex].name); - log_println(DEBUG, "pkgs_failed_to_build = [{}]", fmt::join(pkgs_failed_to_build, ", ")); + pkgs_failed_to_build += pkgs[pkgIndex].name + ' '; + log_println(DEBUG, "pkgs_failed_to_build = {}", pkgs_failed_to_build); } } @@ -497,18 +493,13 @@ bool TaurBackend::update_all_aur_pkgs(path cacheDir, bool useGit) { goto text; } - { - // if (!pacman_exec("-U", pkgs_to_install, false)) { - // log_println(ERROR, _("Failed to install/upgrade packages")); - // return false; - // } - - if (install_package_files(pkgs_to_install, this->config.handle, this->config.flags)) - log_println(INFO, _("Upgraded {}/{} packages."), updatedPkgs, attemptedDownloads); - - goto text; + if (!pacman_exec("-U", split(pkgs_to_install, ' '), false)) { + log_println(ERROR, _("Failed to install/upgrade packages")); + return false; } + log_println(INFO, _("Upgraded {}/{} packages."), updatedPkgs, attemptedDownloads); + text: if (attemptedDownloads > updatedPkgs) { pkgs_failed_to_build.erase(pkgs_failed_to_build.end() - 1); diff --git a/src/util.cpp b/src/util.cpp index f5a360a..520c1f8 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -733,28 +733,6 @@ bool update_aur_cache(bool recursiveCall) { return true; } -bool install_package_files(const vector &packageFiles, alpm_handle_t *handle, int flags) { - if (alpm_trans_init(handle, flags)) { - log_println(ERROR, _("Failed to initialize transaction ({})"), alpm_strerror(alpm_errno(handle))); - return false; - } - - for (const string &packageFile : packageFiles) { - alpm_pkg_t *pkg; - if (alpm_pkg_load(handle, packageFile.c_str(), 1, alpm_option_get_local_file_siglevel(handle), &pkg)) { - log_println(ERROR, _("Failed to load package {}! ({})"), packageFile, alpm_strerror(alpm_errno(handle))); - return false; - } - - if (alpm_add_pkg(handle, pkg)) { - log_println(ERROR, _("Failed to add package {}! ({})"), packageFile, alpm_strerror(alpm_errno(handle))); - return false; - } - } - - return commitTransactionAndRelease(true); -} - /** Ask the user to select a package out of a list. * @param pkgs The list of packages, in a vector * @param backend A reference to the TaurBackend responsible for fetching any AUR packages. From 366fa558653d78d817dbae242f98f75812e5ac65 Mon Sep 17 00:00:00 2001 From: Toni500git Date: Sat, 22 Jun 2024 22:12:11 +0200 Subject: [PATCH 08/14] config: use constructor instead of manual init() as like as I did on https://github.com/Toni500github/customfetch/commit/3b56cfcdc3dcba42272ce9f646757a113aab37b2 --- include/config.hpp | 34 +----------------------------- src/args.cpp | 15 ++++++------- src/config.cpp | 23 ++++++-------------- src/main.cpp | 52 +++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 60 insertions(+), 64 deletions(-) diff --git a/include/config.hpp b/include/config.hpp index a04ba8f..a16c51a 100644 --- a/include/config.hpp +++ b/include/config.hpp @@ -22,17 +22,6 @@ using std::filesystem::path; // so we don't need to include util.hpp for getConfigValue() string expandVar(string& str); -enum types { - STR, - BOOL -}; - -struct strOrBool { - types valueType; - string stringValue = ""; - bool boolValue = false; -}; - struct _color_t { fmt::rgb red; fmt::rgb green; @@ -77,17 +66,12 @@ class Config { // alpm transaction flags int flags; - std::map overrides; - - Config(); + Config(string_view configFile, string_view themeFile, string_view configDir); ~Config(); - void init(string &configFile, string &themeFile, string_view configDir); void initVars(); void initColors(); - bool isInitialized(); - void loadConfigFile(string_view filename); void loadPacmanConfigFile(string filename); void loadThemeFile(string_view filename); @@ -95,18 +79,6 @@ class Config { // stupid c++ that wants template functions in header template T getConfigValue(const string& value, T fallback) { - auto overridePos = overrides.find(value); - - // user wants a bool (overridable), we found an override matching the name, and the override is a bool. - if constexpr (std::is_same()) - if (overridePos != overrides.end() && overrides[value].valueType == BOOL) - return overrides[value].boolValue; - - // user wants a str (overridable), we found an override matching the name, and the override is a str. - if constexpr (std::is_same()) - if (overridePos != overrides.end() && overrides[value].valueType == STR) - return overrides[value].stringValue; - std::optional ret = this->tbl.at_path(value).value(); if constexpr (toml::is_string) // if we want to get a value that's a string return ret ? expandVar(ret.value()) : expandVar(fallback); @@ -119,7 +91,6 @@ class Config { private: toml::table tbl, theme_tbl; - bool initialized = false; }; extern std::unique_ptr config; @@ -200,7 +171,4 @@ gray = "#5a5a5a" #index = "#ff11cc" )#"; -inline string configfile; -inline string themefile; - #endif diff --git a/src/args.cpp b/src/args.cpp index ddd6df4..5fd81c3 100644 --- a/src/args.cpp +++ b/src/args.cpp @@ -88,34 +88,31 @@ int parsearg_op(int opt, int dryrun) { int parsearg_global(int opt) { switch (opt) { case OP_CACHEDIR: - config->overrides["general.cacheDir"] = {STR, strndup(optarg, PATH_MAX)}; + config->cacheDir = strndup(optarg, PATH_MAX); break; case OP_COLORS: fmt::disable_colors = !((bool)std::atoi(optarg)); - config->overrides["general.colors"] = {BOOL, "", (bool)std::atoi(optarg)}; + config->colors = (bool)std::atoi(optarg); break; case OP_DEBUG: - config->overrides["general.debug"] = {BOOL, "", true}; + config->debug = true; break; case OP_AURONLY: case 'a': - config->overrides["general.aurOnly"] = {BOOL, "", true}; + config->aurOnly = true; break; case OP_SUDO: - config->overrides["general.sudo"] = {STR, strndup(optarg, PATH_MAX)}; + config->sudo = strndup(optarg, PATH_MAX); break; case OP_NOCONFIRM: config->noconfirm = true; break; case OP_USEGIT: case 'g': - config->overrides["general.useGit"] = {BOOL, "", true}; + config->useGit = true; break; case OP_CONFIG: - configfile = strndup(optarg, PATH_MAX); - break; case OP_THEME: - themefile = strndup(optarg, PATH_MAX); break; default: return 1; diff --git a/src/config.cpp b/src/config.cpp index ae70089..ae0f8ab 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -8,8 +8,6 @@ using std::ofstream; using std::ifstream; -Config::Config() {} - Config::~Config() { if (this->handle) { alpm_trans_release(this->handle); @@ -21,10 +19,7 @@ Config::~Config() { } // initialize Config, can only be ran once for each Config instance. -void Config::init(string &configFile, string &themeFile, string_view configDir) { - if (this->initialized) - return; - +Config::Config(string_view configFile, string_view themeFile, string_view configDir) { bool newUser = false; if (!std::filesystem::exists(configDir)) { @@ -36,13 +31,13 @@ void Config::init(string &configFile, string &themeFile, string_view configDir) if (!std::filesystem::exists(configFile)) { log_println(WARN, _("{} not found, generating new one"), configFile); // https://github.com/hyprwm/Hyprland/blob/main/src/config/ConfigManager.cpp#L681 - ofstream f(configFile, std::ios::trunc); + ofstream f(configFile.data(), std::ios::trunc); f << AUTOCONFIG; f.close(); } if (!std::filesystem::exists(themeFile)) { log_println(WARN, _("{} not found, generating new one"), themeFile); - ofstream f(themeFile, std::ios::trunc); + ofstream f(themeFile.data(), std::ios::trunc); f << AUTOTHEME; f.close(); } @@ -50,10 +45,9 @@ void Config::init(string &configFile, string &themeFile, string_view configDir) this->loadConfigFile(configFile); this->loadThemeFile(themeFile); - this->initialized = true; - if (!std::filesystem::exists(config->cacheDir)) { - log_println(WARN, _("TabAUR cache folder was not found, Creating folders at {}!"), config->cacheDir.string()); - std::filesystem::create_directories(config->cacheDir); + if (!std::filesystem::exists(this->cacheDir)) { + log_println(WARN, _("TabAUR cache folder was not found, Creating folders at {}!"), this->cacheDir.string()); + std::filesystem::create_directories(this->cacheDir); } if (newUser) @@ -66,11 +60,6 @@ void Config::init(string &configFile, string &themeFile, string_view configDir) "Thank you!\n")); } -// get initialized variable -bool Config::isInitialized() { - return this->initialized; -} - /* * initialize all the "config.toml" variables * and sanitize them (never trust user's input) diff --git a/src/main.cpp b/src/main.cpp index 96d4d4f..25b2ecf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -548,6 +548,46 @@ static void localize(void) { } #endif +// parseargs() but only for parsing the user config path trough args +// and so we can directly construct Config +static bool parse_config_path(int argc, char* argv[], string& configFile, string& themeFile) { + int opt = 0; + int option_index = 0; + opterr = 0; + const char *optstring = "C:"; + static const struct option opts[] = + { + {"config", required_argument, 0, OP_CONFIG}, + {"theme", required_argument, 0, OP_THEME}, + {0,0,0,0} + }; + + while ((opt = getopt_long(argc, argv, optstring, opts, &option_index)) != -1) { + if (opt == 0 || opt == '?') + continue; + + switch (opt) { + case OP_CONFIG: + configFile = strndup(optarg, PATH_MAX); + if (!std::filesystem::exists(configFile)) + die("config file '{}' doesn't exist", configFile); + + break; + case OP_THEME: + themeFile = strndup(optarg, PATH_MAX); + if (!std::filesystem::exists(themeFile)) + die("theme file '{}' doesn't exist", themeFile); + + break; + + default: + return false; + } + } + + return true; +} + // function taken from pacman int parseargs(int argc, char* argv[]) { // default @@ -556,6 +596,8 @@ int parseargs(int argc, char* argv[]) { int opt = 0; int option_index = 0; int result = 0; + opterr = 1; // re-enable since before we disabled for "invalid option" error + optind = 0; const char *optstring = "DFQRSTUVaihqsurytns"; static const struct option opts[] = { @@ -678,21 +720,21 @@ int parseargs(int argc, char* argv[]) { // main int main(int argc, char *argv[]) { - config = std::make_unique(); string configDir = getConfigDir(); #if defined(ENABLE_NLS) localize(); #endif - configfile = (configDir + "/config.toml"); - themefile = (configDir + "/theme.toml"); + string configfile = (configDir + "/config.toml"); + string themefile = (configDir + "/theme.toml"); + parse_config_path(argc, argv, configfile, themefile); + + config = std::make_unique(configfile, themefile, configDir); if (parseargs(argc, argv)) return 1; - config->init(configfile, themefile, configDir); - if (op.test_colors) { test_colors(); return 0; From b9dcd63269b1ad089fa21bf9ebc2a4cf45b6cc05 Mon Sep 17 00:00:00 2001 From: BurntRanch <69512353+BurntRanch@users.noreply.github.com> Date: Tue, 25 Jun 2024 12:12:52 +0300 Subject: [PATCH 09/14] Fix git packages and add update summary. --- include/util.hpp | 5 +++ src/taur.cpp | 113 ++++++++++++++++++++++++++++++----------------- 2 files changed, 78 insertions(+), 40 deletions(-) diff --git a/include/util.hpp b/include/util.hpp index 338b760..77131fc 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -62,6 +62,7 @@ enum prompt_yn { PROMPT_YN_CONTINUE_WITHOUT_DIFF, PROMPT_YN_EDIT_PKGBUILD, PROMPT_YN_PROCEED_INSTALL, + PROMPT_YN_PROCEED_UPGRADE, PROMPT_YN_PROCEED_TRANSACTION, PROMPT_YN_CLEANBUILD, }; @@ -255,6 +256,10 @@ bool askUserYorN(bool def, prompt_yn pr, Args&&... args) { log_printf(INFO, BOLD, _("Proceed with the installation? {}"), inputs_str); NOCONFIRM(YES); break; + case PROMPT_YN_PROCEED_UPGRADE: + log_printf(INFO, BOLD, _("Would you like to upgrade the above packages? {}"), inputs_str); + NOCONFIRM(YES); + break; case PROMPT_YN_PROCEED_TRANSACTION: log_printf(INFO, BOLD, _("Would you like to proceed with this transaction? {}"), inputs_str); NOCONFIRM(YES); diff --git a/src/taur.cpp b/src/taur.cpp index e6564dd..b9e55c8 100644 --- a/src/taur.cpp +++ b/src/taur.cpp @@ -3,7 +3,9 @@ #include "taur.hpp" #include "config.hpp" #include "util.hpp" +#include #include +#include TaurBackend::TaurBackend(Config& cfg) : config(cfg) {} @@ -375,68 +377,99 @@ bool TaurBackend::handle_aur_depends(TaurPkg_t pkg, path out_path, vector pkgs = this->get_all_local_pkgs(true); + vector localPkgs = this->get_all_local_pkgs(true); - if (pkgs.empty()) { + if (localPkgs.empty()) { log_println(INFO, _("No AUR packages found in your system.")); return true; } vector pkgNames; - pkgNames.reserve(pkgs.size()); + pkgNames.reserve(localPkgs.size()); - for (size_t i = 0; i < pkgs.size(); ++i) - pkgNames.push_back(pkgs[i].name); + for (const TaurPkg_t &pkg : localPkgs) + pkgNames.push_back(pkg.name); vector onlinePkgs = this->fetch_pkgs(pkgNames, useGit); int updatedPkgs = 0; int attemptedDownloads = 0; - if (onlinePkgs.size() != pkgs.size()) - log_println(WARN, _("Couldn't get all packages! (searched {} packages, got {}) Still trying to update the others."), pkgs.size(), onlinePkgs.size()); + if (onlinePkgs.size() != localPkgs.size()) + log_println(WARN, _("Couldn't get all packages! (searched {} packages, got {}) Still trying to update the others."), localPkgs.size(), onlinePkgs.size()); - for (size_t i = 0; i < onlinePkgs.size(); i++) { - size_t pkgIndex; - bool found = false; - bool alrprepared = false; - - for (pkgIndex = 0; pkgIndex < pkgs.size(); pkgIndex++) { - if (pkgs[pkgIndex].name == onlinePkgs[i].name) { - found = true; - break; - } - } + log_println(INFO, "Here's a list of packages that may be updated:"); - log_println(DEBUG, "onlinePkgs.name = {}", onlinePkgs[i].name); - log_println(DEBUG, "onlinePkgs.totaldepends = {}", onlinePkgs[i].totaldepends); + vector> potentialUpgradeTargets; - if (!found) { - log_println(WARN, _("We couldn't find {} in the local pkg database, This shouldn't happen."), onlinePkgs[i].name); + for (size_t i = 0; i < onlinePkgs.size(); i++) { + TaurPkg_t &pkg = onlinePkgs[i]; + auto pkgIteratorInLocalPkgs = std::find_if(localPkgs.begin(), localPkgs.end(), [&pkg] (const TaurPkg_t &element) { return element.name == pkg.name; }); + size_t pkgIndexInLocalPkgs = std::distance(localPkgs.begin(), pkgIteratorInLocalPkgs); + TaurPkg_t &localPkg = localPkgs[pkgIndexInLocalPkgs]; + + if (hasEnding(pkg.name, "-git")) { + potentialUpgradeTargets.push_back(std::make_tuple(pkg, localPkg)); + log_println(INFO, "- {} (from {} to {}, (dev package, may change despite AUR version))", localPkg.name, localPkg.version, pkg.version); continue; } - - if (pkgs[pkgIndex].version == onlinePkgs[i].version) { - log_println(DEBUG, "pkg {} has no update, local: {}, online: {}, skipping!", pkgs[pkgIndex].name, pkgs[pkgIndex].version, onlinePkgs[i].version); + + if ((localPkg.version != pkg.version) && alpm_pkg_vercmp(localPkg.version.c_str(), pkg.version.c_str()) == 0) { + potentialUpgradeTargets.push_back(std::make_tuple(pkg, localPkg)); + log_println(INFO, "- {} (from {} to {})", localPkg.name, localPkg.version, pkg.version); continue; } + } + + if (!askUserYorN(true, PROMPT_YN_PROCEED_UPGRADE)) + return false; + + for (const auto &potentialUpgrade : potentialUpgradeTargets) { + // size_t pkgIndex; + // bool found = false; + bool alrprepared = false; + + // for (pkgIndex = 0; pkgIndex < localPkgs.size(); pkgIndex++) { + // if (localPkgs[pkgIndex].name == onlinePkgs[i].name) { + // found = true; + // break; + // } + // } + + TaurPkg_t potentialUpgradeTargetTo = std::get<0>(potentialUpgrade); + TaurPkg_t potentialUpgradeTargetFrom = std::get<1>(potentialUpgrade); + + log_println(DEBUG, "potentialUpgradeTarget.name = {}", potentialUpgradeTargetTo.name); + log_println(DEBUG, "potentialUpgradeTarget.totaldepends = {}", potentialUpgradeTargetTo.totaldepends); + + // if (!found) { + // log_println(WARN, _("We couldn't find {} in the local pkg database, This shouldn't happen."), onlinePkgs[i].name); + // continue; + // } + + bool isGitPackage = hasEnding(potentialUpgradeTargetTo.name, "-git"); + + // if (!isGitPackage && localPkgs[pkgIndex].version == onlinePkgs[i].version) { + // log_println(DEBUG, "pkg {} has no update, local: {}, online: {}, skipping!", localPkgs[pkgIndex].name, localPkgs[pkgIndex].version, onlinePkgs[i].version); + // continue; + // } - path pkgDir = cacheDir / onlinePkgs[i].name; + path pkgDir = cacheDir / potentialUpgradeTargetTo.name; - log_println(INFO, _("Downloading {}."), onlinePkgs[i].name); + log_println(INFO, _("Downloading {}."), potentialUpgradeTargetTo.name); if (!useGit) std::filesystem::remove_all(pkgDir); - bool downloadSuccess = this->download_pkg(onlinePkgs[i].aur_url, pkgDir); + bool downloadSuccess = this->download_pkg(potentialUpgradeTargetTo.aur_url, pkgDir); if (!downloadSuccess) { - log_println(WARN, _("Failed to download package {}!"), onlinePkgs[i].name); + log_println(WARN, _("Failed to download package {}!"), potentialUpgradeTargetTo.name); continue; } // workaround for -git package because they are "special" - if (hasEnding(pkgs[pkgIndex].name, "-git")) { + if (isGitPackage) { alrprepared = true; std::filesystem::current_path(pkgDir); makepkg_exec({"--verifysource", "-fA"}); @@ -446,7 +479,7 @@ bool TaurBackend::update_all_aur_pkgs(path cacheDir, bool useGit) { string versionInfo = shell_exec("grep 'pkgver=' " + pkgDir.string() + "/PKGBUILD | cut -d= -f2"); if (versionInfo.empty()) { - log_println(WARN, _("Failed to parse version information from {}'s PKGBUILD, You might be able to ignore this safely."), pkgs[pkgIndex].name); + log_println(WARN, _("Failed to parse version information from {}'s PKGBUILD, You might be able to ignore this safely."), potentialUpgradeTargetTo.name); continue; } @@ -459,20 +492,20 @@ bool TaurBackend::update_all_aur_pkgs(path cacheDir, bool useGit) { if (!epoch.empty() && epoch[0] != '\0') versionInfo = epoch + ':' + versionInfo; - log_println(DEBUG, "pkg {} versions: local {} vs online {}", pkgs[pkgIndex].name, pkgs[pkgIndex].version, onlinePkgs[i].version); + log_println(DEBUG, "pkg {} versions: local {} vs online {}", potentialUpgradeTargetTo.name, potentialUpgradeTargetTo.version, potentialUpgradeTargetFrom.version); - if ((alpm_pkg_vercmp(pkgs[pkgIndex].version.data(), versionInfo.c_str())) == 0) { + if ((alpm_pkg_vercmp(potentialUpgradeTargetFrom.version.data(), versionInfo.c_str())) == 0) { - log_println(DEBUG, _("pkg {} has the same version on the AUR than in its PKGBUILD, local: {}, online: {}, PKGBUILD: {}, skipping!"), pkgs[pkgIndex].name, - pkgs[pkgIndex].version, onlinePkgs[i].version, versionInfo); + log_println(DEBUG, _("pkg {} has the same version on the AUR than in its PKGBUILD, local: {}, online: {}, PKGBUILD: {}, skipping!"), potentialUpgradeTargetFrom.name, + potentialUpgradeTargetFrom.version, potentialUpgradeTargetTo.version, versionInfo); continue; } - log_println(INFO, _("Upgrading package {} from version {} to version {}!"), pkgs[pkgIndex].name, pkgs[pkgIndex].version, onlinePkgs[i].version); + log_println(INFO, _("Upgrading package {} from version {} to version {}!"), potentialUpgradeTargetFrom.name, potentialUpgradeTargetTo.version, potentialUpgradeTargetTo.version); attemptedDownloads++; - this->handle_aur_depends(onlinePkgs[i], pkgDir, this->get_all_local_pkgs(true), useGit); - bool installSuccess = this->build_pkg(pkgs[pkgIndex].name, pkgDir, alrprepared); + this->handle_aur_depends(potentialUpgradeTargetTo, pkgDir, this->get_all_local_pkgs(true), useGit); + bool installSuccess = this->build_pkg(potentialUpgradeTargetTo.name, pkgDir, alrprepared); if (installSuccess) { pkgs_to_install += built_pkg + ' '; @@ -480,9 +513,9 @@ bool TaurBackend::update_all_aur_pkgs(path cacheDir, bool useGit) { updatedPkgs++; // prevent duplicated entries - pkgs.erase(pkgs.begin() + pkgIndex); + //localPkgs.erase(localPkgs.begin() + pkgIndex); } else { - pkgs_failed_to_build += pkgs[pkgIndex].name + ' '; + pkgs_failed_to_build += potentialUpgradeTargetTo.name + ' '; log_println(DEBUG, "pkgs_failed_to_build = {}", pkgs_failed_to_build); } } From 91041329d74c6bc3010f0a384d29096c5cb6d403 Mon Sep 17 00:00:00 2001 From: BurntRanch <69512353+BurntRanch@users.noreply.github.com> Date: Sun, 30 Jun 2024 16:15:01 +0300 Subject: [PATCH 10/14] Fix the version comparing in potentialUpgradeTargets --- src/taur.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/taur.cpp b/src/taur.cpp index b9e55c8..9ea3afd 100644 --- a/src/taur.cpp +++ b/src/taur.cpp @@ -414,7 +414,7 @@ bool TaurBackend::update_all_aur_pkgs(path cacheDir, bool useGit) { continue; } - if ((localPkg.version != pkg.version) && alpm_pkg_vercmp(localPkg.version.c_str(), pkg.version.c_str()) == 0) { + if ((localPkg.version != pkg.version) && alpm_pkg_vercmp(pkg.version.c_str(), localPkg.version.c_str()) == 1) { potentialUpgradeTargets.push_back(std::make_tuple(pkg, localPkg)); log_println(INFO, "- {} (from {} to {})", localPkg.name, localPkg.version, pkg.version); continue; @@ -522,8 +522,7 @@ bool TaurBackend::update_all_aur_pkgs(path cacheDir, bool useGit) { if (pkgs_to_install.size() <= 0) { log_println(WARN, _("No packages to be upgraded.")); - // oh no, a goto oh noooo this program is ruined - goto text; + return false; } if (!pacman_exec("-U", split(pkgs_to_install, ' '), false)) { @@ -533,7 +532,6 @@ bool TaurBackend::update_all_aur_pkgs(path cacheDir, bool useGit) { log_println(INFO, _("Upgraded {}/{} packages."), updatedPkgs, attemptedDownloads); -text: if (attemptedDownloads > updatedPkgs) { pkgs_failed_to_build.erase(pkgs_failed_to_build.end() - 1); log_println(WARN, fg(color.red), _("Failed to upgrade: {}"), pkgs_failed_to_build); From 9c9a93d334df70e75482c1df7af1e4e2eb7680ce Mon Sep 17 00:00:00 2001 From: BurntRanch <69512353+BurntRanch@users.noreply.github.com> Date: Mon, 1 Jul 2024 23:25:56 +0300 Subject: [PATCH 11/14] sysupgrade: Fix a bug that caused repeated installing of upgraded AUR Packages --- src/main.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index 25b2ecf..ff108b8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -262,6 +262,8 @@ int installPkg(alpm_list_t *pkgNames) { if (op.op_s_upgrade) { log_println(INFO, _("Upgrading AUR packages!")); backend->update_all_aur_pkgs(cacheDir, useGit); + + pkgs_to_install = ""; // Reset the list. } for (size_t i = 0; i < AURPkgs.size(); i++) { From 013d8895e04d6813891019f95db4f1004207a2b2 Mon Sep 17 00:00:00 2001 From: BurntRanch <69512353+BurntRanch@users.noreply.github.com> Date: Mon, 1 Jul 2024 23:50:26 +0300 Subject: [PATCH 12/14] Add -U support. --- include/args.hpp | 1 + src/args.cpp | 4 +++- src/main.cpp | 37 ++++++++++++++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/include/args.hpp b/include/args.hpp index 8f4be06..76ce4e7 100644 --- a/include/args.hpp +++ b/include/args.hpp @@ -29,6 +29,7 @@ enum { OP_SYNC, OP_REM, OP_QUERY, + OP_UPGRADE, OP_PACMAN, // when it's different from -S,R,Q we gonna use pacman }; diff --git a/src/args.cpp b/src/args.cpp index 5fd81c3..a6af628 100644 --- a/src/args.cpp +++ b/src/args.cpp @@ -62,7 +62,9 @@ int parsearg_op(int opt, int dryrun) { op.op = (op.op != OP_MAIN ? 0 : OP_PACMAN); break; case 'U': if(dryrun) break; - op.op = (op.op != OP_MAIN ? 0 : OP_PACMAN); break; + op.op = (op.op != OP_MAIN ? 0 : OP_UPGRADE); + op.requires_root = true; + break; case 'V': if(dryrun) break; op.version = 1; break; diff --git a/src/main.cpp b/src/main.cpp index ff108b8..1569260 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,6 +17,7 @@ * along with this program. If not, see . */ +#include #include #pragma GCC diagnostic ignored "-Wvla" @@ -143,6 +144,9 @@ void execPacman(int argc, char *argv[]) { } int installPkg(alpm_list_t *pkgNames) { + if (!pkgNames) + return false; + bool useGit = config->useGit; path cacheDir = config->cacheDir; @@ -534,6 +538,35 @@ bool queryPkgs(alpm_list_t *pkgNames) { return true; } +bool upgradePkgs(alpm_list_t *pkgNames) { + if (!pkgNames) { + return false; + } + + if (alpm_trans_init(config->handle, config->flags)) { + log_println(ERROR, _("Failed to initialize transaction ({})"), alpm_strerror(alpm_errno(config->handle))); + return false; + } + + for (; pkgNames; pkgNames = pkgNames->next) { + alpm_pkg_t *pkg; + alpm_pkg_load(config->handle, (const char *)pkgNames->data, 1, alpm_option_get_local_file_siglevel(config->handle), &pkg); + + if (!pkg) { + log_println(ERROR, _("Failed to load package {}! ({})"), (const char *)(pkgNames->data), alpm_strerror(alpm_errno(config->handle))); + return false; // Yes, I am ignoring the transaction we just created. + } + + if (alpm_add_pkg(config->handle, pkg)) { + log_println(ERROR, _("Failed to add package {} to transaction! ({})"), (const char *)(pkgNames->data), alpm_strerror(alpm_errno(config->handle))); + return false; + } + + } + + return commitTransactionAndRelease(true); +} + /** Sets up gettext localization. Safe to call multiple times. */ /* Inspired by the monotone function localize_monotone. */ @@ -774,7 +807,9 @@ int main(int argc, char *argv[]) { log_println(WARN, _("Watch out when using the -R operation in TabAUR, it has been tested pretty well, but you should always watch out for any errors, Please use pacman or be careful")); return removePkg(taur_targets.get()) ? 0 : 1; case OP_QUERY: - return (queryPkgs(taur_targets.get())) ? 0 : 1; + return queryPkgs(taur_targets.get()) ? 0 : 1; + case OP_UPGRADE: + return upgradePkgs(taur_targets.get()) ? 0 : 1; case OP_SYSUPGRADE: return (updateAll()) ? 0 : 1; default: From 88d735e23806bd49ff5586b5db85605583af791e Mon Sep 17 00:00:00 2001 From: BurntRanch <69512353+BurntRanch@users.noreply.github.com> Date: Mon, 1 Jul 2024 23:52:08 +0300 Subject: [PATCH 13/14] Bump version to 0.6.8 (Watch out for the next patch.) --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index fc69974..a4f555c 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ else CXXFLAGS := -O2 $(CXXFLAGS) endif -VERSION = 0.6.7 +VERSION = 0.6.8 BRANCH = dev SRC = $(sort $(wildcard src/*.cpp)) OBJ = $(SRC:.cpp=.o) From 7ab073eaec8e289b50dda027a1e719869484d7b9 Mon Sep 17 00:00:00 2001 From: BurntRanch <69512353+BurntRanch@users.noreply.github.com> Date: Mon, 1 Jul 2024 23:57:19 +0300 Subject: [PATCH 14/14] hope its happy now --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index a4f555c..efc23ec 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ else endif VERSION = 0.6.8 -BRANCH = dev +BRANCH = main SRC = $(sort $(wildcard src/*.cpp)) OBJ = $(SRC:.cpp=.o) CURL_LIBS ?= -lcurl -lnghttp3 -lnghttp2 -lidn2 -lssh2 -lssl -lcrypto -lpsl -lgssapi_krb5 -lzstd -lbrotlidec -lz # pkg-config --static --libs libcurl (but fixed)