Skip to content

Commit

Permalink
several improvements to removePkg, and some modification to make code…
Browse files Browse the repository at this point in the history
… cleaner.
  • Loading branch information
BurntRanch committed Apr 18, 2024
1 parent a00feb5 commit 7810e1b
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 64 deletions.
4 changes: 3 additions & 1 deletion include/taur.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef GIT_HPP
#define GIT_HPP

#include <alpm.h>
#include <alpm_list.h>
#include <fcntl.h>
#include <unistd.h>
Expand Down Expand Up @@ -42,7 +43,8 @@ class TaurBackend {
bool download_pkg(string url, string out_path);
optional<TaurPkg_t> fetch_pkg(string pkg, bool returnGit);
vector<TaurPkg_t> fetch_pkgs(vector<string> 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<TaurPkg_t> localPkgs, bool useGit);
bool install_pkg(string pkg_name, string extracted_path, bool onlydownload);
bool update_all_aur_pkgs(string cacheDir, bool useGit);
Expand Down
54 changes: 53 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <alpm_list.h>
#include <iostream>
#pragma GCC diagnostic ignored "-Wvla"

#include "util.hpp"
Expand Down Expand Up @@ -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<string> 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() {
Expand Down
101 changes: 41 additions & 60 deletions src/taur.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "taur.hpp"
#include "config.hpp"
#include "args.hpp"
#include <iostream>

namespace fs = std::filesystem;

Expand Down Expand Up @@ -130,81 +129,63 @@ vector<TaurPkg_t> TaurBackend::fetch_pkgs(vector<string> 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<string> 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;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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] ");
Expand Down

0 comments on commit 7810e1b

Please sign in to comment.