Skip to content

Commit

Permalink
Addressed memory leak, and fixed issue relating to tarballs.
Browse files Browse the repository at this point in the history
  • Loading branch information
BurntRanch committed Apr 21, 2024
1 parent ab7b59b commit 281b71b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
3 changes: 2 additions & 1 deletion include/taur.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <rapidjson/stringbuffer.h>
#include "cpr/cpr.h"
#include <sys/wait.h>
#include "util.hpp"

class Config;

Expand Down Expand Up @@ -44,7 +45,7 @@ class TaurBackend {
optional<TaurPkg_t> fetch_pkg(string pkg, bool returnGit);
vector<TaurPkg_t> fetch_pkgs(vector<string> pkgs, bool returnGit);
bool remove_pkg(alpm_pkg_t *pkgs, bool ownTransaction = true);
bool remove_pkgs(alpm_list_t *pkgs);
bool remove_pkgs(alpm_list_smart_pointer &pkgs);
bool handle_aur_depends(TaurPkg_t pkg, path out_path, vector<TaurPkg_t> localPkgs, bool useGit);
bool build_pkg(string pkg_name, string extracted_path, bool alreadyprepared);
bool update_all_aur_pkgs(string cacheDir, bool useGit);
Expand Down
29 changes: 21 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,17 +256,21 @@ bool removePkg(alpm_list_t *pkgNames) {
if (!pkgNames)
return false;

alpm_list_t *ret = nullptr;
alpm_list_t *temp_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)
alpm_list_free(regexQuery);

if (alpm_db_search(alpm_get_localdb(config->handle), regexQuery, &temp_ret) != 0)
return false;

size_t ret_length = alpm_list_count(ret);
alpm_list_smart_pointer ret(temp_ret, alpm_list_free);

size_t ret_length = alpm_list_count(ret.get());

if (ret_length == 0) {
log_println(LOG_ERROR, "No packages found!");
Expand All @@ -279,16 +283,16 @@ bool removePkg(alpm_list_t *pkgNames) {
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)));
fmt::println("[{}] {}", i, alpm_pkg_get_name((alpm_pkg_t *)(alpm_list_nth(ret.get(), i)->data)));
}

string included;
std::getline(std::cin, included);

vector<string> includedIndexes = split(included, ' ');

alpm_list_t *finalPackageList = nullptr;
alpm_list_t *finalPackageListStart = nullptr;
alpm_list_t *finalPackageList = nullptr;
alpm_list_t *finalPackageListStart = nullptr;

if (included == "*")
return backend->remove_pkgs(ret);
Expand All @@ -300,18 +304,27 @@ bool removePkg(alpm_list_t *pkgNames) {
if (includedIndex >= ret_length)
continue;

finalPackageList = alpm_list_add(finalPackageList, alpm_list_nth(ret, includedIndex)->data);
finalPackageList = alpm_list_add(finalPackageList, alpm_list_nth(ret.get(), includedIndex)->data);

if (finalPackageList != nullptr && finalPackageListStart == nullptr)
finalPackageListStart = finalPackageList;

} catch (std::invalid_argument const&) {
log_println(LOG_WARN, "Invalid argument! Assuming all.");

if (finalPackageListStart != nullptr)
alpm_list_free(finalPackageListStart);
else if (finalPackageList != nullptr)
alpm_list_free(finalPackageList);

return backend->remove_pkgs(ret);
}
}

return backend->remove_pkgs(finalPackageListStart);
// take control of the list and pass it to the smart pointer
alpm_list_smart_pointer finalList = make_list_smart_pointer(finalPackageListStart);

return backend->remove_pkgs(finalList);
}

bool updateAll() {
Expand Down
12 changes: 8 additions & 4 deletions src/taur.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "util.hpp"
#include "taur.hpp"
#include "config.hpp"
#include <filesystem>

namespace fs = std::filesystem;

Expand Down Expand Up @@ -158,7 +159,7 @@ bool TaurBackend::remove_pkg(alpm_pkg_t *pkg, bool ownTransaction) {
@param pkgs alpm list of alpm_pkg_t pointers to remove.
@return success.
*/
bool TaurBackend::remove_pkgs(alpm_list_t *pkgs) {
bool TaurBackend::remove_pkgs(alpm_list_smart_pointer &pkgs) {
if (!pkgs)
return false;

Expand All @@ -167,7 +168,7 @@ bool TaurBackend::remove_pkgs(alpm_list_t *pkgs) {
return false;
}

size_t pkgs_length = alpm_list_count(pkgs);
size_t pkgs_length = alpm_list_count(pkgs.get());

if (pkgs_length == 0) {
log_println(LOG_ERROR, "Couldn't find any packages!");
Expand All @@ -177,8 +178,8 @@ bool TaurBackend::remove_pkgs(alpm_list_t *pkgs) {
return this->remove_pkg((alpm_pkg_t *)(pkgs->data), false) && commitTransactionAndRelease();
}

for (; pkgs; pkgs = pkgs->next) {
bool success = this->remove_pkg((alpm_pkg_t *)(pkgs->data), false);
for (alpm_list_t *pkgsGet = pkgs.get(); pkgsGet; pkgsGet = pkgsGet->next) {
bool success = this->remove_pkg((alpm_pkg_t *)(pkgsGet->data), false);
if (!success) {
alpm_trans_release(this->config.handle);
return false;
Expand Down Expand Up @@ -379,6 +380,9 @@ bool TaurBackend::update_all_aur_pkgs(string cacheDir, bool useGit) {
string pkgFolder = cacheDir + '/' + onlinePkgs[i].name;
sanitizeStr(pkgFolder);

if (!useGit)
std::filesystem::remove_all(pkgFolder);

bool downloadSuccess = this->download_pkg(onlinePkgs[i].url, pkgFolder);

if (!downloadSuccess) {
Expand Down

0 comments on commit 281b71b

Please sign in to comment.