109 lines
2.9 KiB
C
109 lines
2.9 KiB
C
#include <libmp/all.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
#include "../util.h"
|
|
#include "../cmd.h"
|
|
#include "../log.h"
|
|
|
|
bool cmd_remove_callback(lm_ctx_t *ctx, lm_pkg_t *pkg, char *file, size_t current, size_t total, void *data){
|
|
bar(current, total);
|
|
return true;
|
|
}
|
|
|
|
bool cmd_remove(lm_ctx_t *ctx, config_t *config, args_t *args){
|
|
args_split(args, "break");
|
|
args_split(args, "yes");
|
|
|
|
if(args_get_bool(args, "help")){
|
|
info(_("Listing options for the remove command:"));
|
|
printf(_(" "FG_BOLD"--break"FG_RESET":\tbreak other package depends\n"));
|
|
printf(_(" "FG_BOLD"--yes"FG_RESET":\tdon't ask for confirmation\n"));
|
|
return true;
|
|
}
|
|
|
|
ssize_t count = 0, size = 0, current = 0, save = 0;
|
|
bool ret = false, r = false;
|
|
char ssize[LONGSTR_MAX+3];
|
|
lm_pkg_t *pkgs = NULL;
|
|
char *name = NULL;
|
|
|
|
for(int i = 0; i < args->count; i++){
|
|
if(NULL != args->list[i].name)
|
|
continue;
|
|
|
|
if(eq((name = args->list[i].value), "remove"))
|
|
continue;
|
|
|
|
if(NULL == pkgs)
|
|
pkgs = malloc(sizeof(lm_pkg_t));
|
|
else
|
|
pkgs = realloc(pkgs, sizeof(lm_pkg_t)*(count+1));
|
|
|
|
if(!lm_ctx_database_find(ctx, &pkgs[count++], name, NULL)){
|
|
if(lm_error() == LM_ERR_DbSqlNotFound)
|
|
error(_("Package "FG_BOLD"%s"FG_RESET" is not installed"), name);
|
|
else
|
|
error(_("Failed to find "FG_BOLD"%s"FG_RESET": %s"), name, lm_strerror());
|
|
goto end;
|
|
}
|
|
|
|
if(!lm_ctx_removeable(ctx, &pkgs[count-1])){
|
|
error(_("Cannot remove "FG_BOLD"%s"FG_RESET" ("FG_BOLD FG_BLUE"%s"FG_RESET"): %s"), pkgs[count-1].name, pkgs[count-1].version, lm_strerror());
|
|
goto end;
|
|
}
|
|
|
|
size += pkgs[count].size;
|
|
}
|
|
|
|
if(NULL == pkgs){
|
|
error(_("Nothing to do (no packages to remove)"));
|
|
goto end;
|
|
}
|
|
|
|
size_to_human(ssize, size);
|
|
|
|
if(count == 1)
|
|
info(_("Following %d package will be "FG_BOLD"REMOVED:"FG_RESET), count);
|
|
else
|
|
info(_("Following %d packages will be "FG_BOLD"REMOVED:"FG_RESET), count);
|
|
|
|
// list all the packages
|
|
for(int i = 0; i < count; i++)
|
|
package_list(&pkgs[i], &save, 0);
|
|
printf("\n");
|
|
|
|
info(_("Total of "FG_BOLD"%s"FG_RESET" disk space will be freed"), ssize);
|
|
if(!args_get_bool(args, "yes") && !yesno(_("Continue?"))){
|
|
error(_("Operation cancelled"));
|
|
goto end;
|
|
}
|
|
|
|
for(int i = 0; i < count; i++){
|
|
lm_pkg_t *cur = &pkgs[i];
|
|
|
|
info(_("(%d/%d) Removing "FG_BOLD"%s"FG_RESET" ("FG_BOLD FG_BLUE"%s"FG_RESET")"), ++current, count, cur->name, cur->version);
|
|
|
|
r = lm_ctx_remove(ctx, cur, cmd_remove_callback, NULL);
|
|
bar_free();
|
|
|
|
if(!r){
|
|
error(_("Failed to remove "FG_BOLD"%s"FG_RESET": %s"), cur->name, lm_strerror());
|
|
goto end;
|
|
}
|
|
}
|
|
|
|
if(current == count){
|
|
success(count == 1 ? _("Removed %d package") : _("Removed all of the %d packages"), count);
|
|
ret = true;
|
|
}
|
|
|
|
ret = true;
|
|
end:
|
|
if(NULL != pkgs)
|
|
for(int i = 0; i < count; i++)
|
|
lm_package_free(&pkgs[i]);
|
|
bar_free();
|
|
return ret;
|
|
}
|