new: remove and update operation

This commit is contained in:
ngn
2024-07-17 22:58:13 +03:00
parent 91f513132b
commit ba0583fdf4
7 changed files with 346 additions and 71 deletions

View File

@@ -1,6 +1,4 @@
#include <libmp/all.h>
#include <libmp/ctx.h>
#include <libmp/error.h>
#include <stdio.h>
#include "../util.h"
@@ -19,15 +17,22 @@ bool cmd_download_callback(lm_ctx_t *ctx, lm_pkg_t *pkg, bool is_archive, size_t
}
bool cmd_install(lm_ctx_t *ctx, config_t *config, args_t *args){
if(args_get_bool(args, "help")){
info(_("Listing options for the install command:"));
printf(_(" "FG_BOLD"--continue"FG_RESET":\tcontinue even if package is not found\n"));
printf(_(" "FG_BOLD"--yes"FG_RESET":\tdon't ask for confirmation\n"));
return true;
}
lm_ctx_resolve_list_t *list = NULL;
ssize_t current = 1, size = 0;
bool ret = false, r = false;
ssize_t current = 0, size = 0;
char ssize[LONGSTR_MAX+3];
lm_pkg_t *pkg = NULL;
char *name = NULL;
bool ret = false;
if(lm_ctx_sync(ctx, false, NULL, NULL) <= 0){
error(_("There are no avaliable pools"));
error(_("There are no avaliable pools, have you synced yet?"));
goto end;
}
@@ -41,12 +46,14 @@ bool cmd_install(lm_ctx_t *ctx, config_t *config, args_t *args){
// find the package
if((pkg = lm_ctx_pool_find(ctx, name, NULL)) == NULL){
error(_("Failed to find "FG_BOLD"%s"FG_RESET": %s"), name, lm_strerror());
goto end;
if(args_get_bool(args, "continue"))
continue;
else
goto end;
}
// WEIRD CRASH HERE
if(lm_ctx_database_is_installed(ctx, pkg, true)){
error(_("Package "FG_BOLD"%s"FG_RESET" ("FG_BLUE"%s"FG_RESET") is already installed"));
error(_("Package "FG_BOLD"%s"FG_RESET" ("FG_BLUE"%s"FG_RESET") is already installed"), name, pkg->version);
goto end;
}
@@ -58,7 +65,7 @@ bool cmd_install(lm_ctx_t *ctx, config_t *config, args_t *args){
}
if(NULL == list){
error(_("Please specify at least one package to install"));
error(_("Nothing to do (no packages to install)"));
goto end;
}
@@ -66,46 +73,49 @@ bool cmd_install(lm_ctx_t *ctx, config_t *config, args_t *args){
size += pkg->size;
size_to_human(ssize, size);
info(_("Following packages will be "FG_BOLD"INSTALLED:"FG_RESET));
if(list->count == 1)
info(_("Following %d package will be "FG_BOLD"INSTALLED:"FG_RESET), list->count);
else
info(_("Following %d packages will be "FG_BOLD"INSTALLED:"FG_RESET), list->count);
// list all the packages
info(_("Total of "FG_BOLD"%s"FG_RESET" disk space will be used"), ssize);
if(!yesno("Continue?")){
error("Operation cancelled");
if(!args_get_bool(args, "yes") && !yesno(_("Continue?"))){
error(_("Operation cancelled"));
goto end;
}
// download resolved packages
while((pkg = lm_ctx_resolve_next(list)) != NULL){
info(_("(%d/%d) Downloading "FG_BOLD"%s"FG_RESET" ("FG_BOLD FG_BLUE"%s"FG_RESET")"), current, list->count, pkg->name, pkg->version);
info(_("(%d/%d) Downloading "FG_BOLD"%s"FG_RESET" ("FG_BOLD FG_BLUE"%s"FG_RESET")"), ++current, list->count, pkg->name, pkg->version);
if(!lm_ctx_download(ctx, pkg, cmd_download_callback, NULL)){
r = lm_ctx_download(ctx, pkg, cmd_download_callback, NULL);
bar_free();
if(!r){
error(_("Failed to download "FG_BOLD"%s"FG_RESET": %s"), pkg->name, lm_strerror());
goto end;
}
current++;
bar_free();
}
current = 1;
current = 0;
// install resolved packages
while((pkg = lm_ctx_resolve_next(list)) != NULL){
info(_("(%d/%d) Installing "FG_BOLD"%s"FG_RESET" ("FG_BOLD FG_BLUE"%s"FG_RESET")"), current, list->count, pkg->name, pkg->version);
info(_("(%d/%d) Installing "FG_BOLD"%s"FG_RESET" ("FG_BOLD FG_BLUE"%s"FG_RESET")"), ++current, list->count, pkg->name, pkg->version);
if(!lm_ctx_install(ctx, pkg, cmd_install_callback, NULL)){
r = lm_ctx_install(ctx, pkg, cmd_install_callback, NULL);
bar_free();
if(!r){
error(_("Failed to install "FG_BOLD"%s"FG_RESET": %s"), pkg->name, lm_strerror());
goto end;
}
current++;
bar_free();
}
if(current == list->count){
success("Installed all of the %d packages", list->count);
success(list->count == 1 ? _("Installed %d package") : _("Installed all of the %d packages"), list->count);
ret = true;
}

View File

@@ -9,7 +9,7 @@ bool cmd_list(lm_ctx_t *ctx, config_t *config, args_t *args){
lm_pkg_t pkg;
if(args_get_bool(args, "help")){
info(_("Listing options for the install command:"));
info(_("Listing options for the list command:"));
printf(_(" "FG_BOLD"--grep"FG_RESET":\tmakes the output \"grepable\"\n"));
printf(_(" "FG_BOLD"--desc"FG_RESET":\tshow package descriptions\n"));
return true;
@@ -25,7 +25,7 @@ bool cmd_list(lm_ctx_t *ctx, config_t *config, args_t *args){
}
if(!args_get_bool(args, "grep"))
info(count > 1 ? _("Listing %d packages") : _("Listing %d package"));
info(count > 1 ? _("Listing "FG_BOLD"%d"FG_RESET" packages") : _("Listing "FG_BOLD"%d"FG_RESET" package"), count);
while(lm_ctx_database_next(ctx, &pkg)){
if(args_get_bool(args, "grep")){
@@ -33,12 +33,13 @@ bool cmd_list(lm_ctx_t *ctx, config_t *config, args_t *args){
printf("%s:%s:%s\n", pkg.name, pkg.version, pkg.desc);
else
printf("%s:%s\n", pkg.name, pkg.version);
continue;
}
if(args_get_bool(args, "desc"))
printf(FG_BOLD"%s ("FG_BLUE"%s"FG_RESET"): %s\n", pkg.name, pkg.version, pkg.desc);
printf(FG_BOLD"%s"FG_RESET" ("FG_BOLD FG_BLUE"%s"FG_RESET"): %s\n", pkg.name, pkg.version, pkg.desc);
else
printf(FG_BOLD"%s ("FG_BLUE"%s"FG_RESET")\n", pkg.name, pkg.version);
printf(FG_BOLD"%s"FG_RESET" ("FG_BOLD FG_BLUE"%s"FG_RESET")\n", pkg.name, pkg.version);
}
lm_ctx_database_next_free(ctx, &pkg);

102
src/cmd/remove.c Normal file
View File

@@ -0,0 +1,102 @@
#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){
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;
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
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;
}

View File

@@ -1,5 +1,4 @@
#include <libmp/all.h>
#include <libmp/error.h>
#include <stdio.h>
#include "../cmd.h"
@@ -43,12 +42,15 @@ bool cmd_sync(lm_ctx_t *ctx, config_t *config, args_t *args){
size_t sycned = 0;
bool ret = false;
if(0 == config->pool_count){
if(config->pool_count == 0){
error(_("There are no pools specified in the configuration"));
goto end;
}
if((sycned = lm_ctx_sync(ctx, true, cmd_sync_callback, NULL) < 0)){
sycned = lm_ctx_sync(ctx, true, cmd_sync_callback, NULL);
bar_free();
if(sycned < 0){
error(_("Failed to sync pools: %s"), lm_strerror());
goto end;
}

88
src/cmd/update.c Normal file
View File

@@ -0,0 +1,88 @@
#include <libmp/all.h>
#include <libmp/ctx.h>
#include <libmp/error.h>
#include <stdio.h>
#include "../cmd.h"
#include "../log.h"
struct cmd_update_data {
ssize_t total;
ssize_t current;
};
bool cmd_update_callback(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_update_state_t state, char *file, size_t current, size_t total, void *data){
struct cmd_update_data *cbdata = data;
switch (state) {
case UPDATE_REMOVE:
if(current == 0)
info(_("(%d/%d) Removing "FG_BOLD"%s"FG_RESET" ("FG_BOLD FG_BLUE"%s"FG_RESET")"), cbdata->current, cbdata->total, pkg->name, pkg->version);
bar(current, total);
break;
case UPDATE_INSTALL:
if(current == 0){
bar_free();
info(_("(%d/%d) Installing "FG_BOLD"%s"FG_RESET" ("FG_BOLD FG_BLUE"%s"FG_RESET")"), cbdata->current, cbdata->total, pkg->name, pkg->version);
}
bar(current, total);
break;
}
return true;
}
bool cmd_update(lm_ctx_t *ctx, config_t *config, args_t *args){
if(args_get_bool(args, "help")){
info(_("Listing options for the update command:"));
printf(_(" "FG_BOLD"--yes"FG_RESET":\tdon't ask for confirmation\n"));
return true;
}
lm_ctx_update_list_t *list = NULL;
struct cmd_update_data cbdata;
bool ret = false, r = false;
lm_pkg_t *pkg = NULL;
ssize_t current = 0;
if((list = lm_ctx_update_list(ctx)) == NULL){
error(_("Failed to get the package update list: %s"), lm_strerror());
goto end;
}
if(list->count == 0){
error(_("All packages are up-to-date"));
goto end;
}
if(list->count == 1)
info(_("Following %d package will be "FG_BOLD"UPDATED:"FG_RESET), list->count);
else
info(_("Following %d packages will be "FG_BOLD"UPDATED:"FG_RESET), list->count);
// list all the packages
if(!args_get_bool(args, "yes") && !yesno(_("Continue?"))){
error(_("Operation cancelled"));
goto end;
}
while((pkg = lm_ctx_update_list_next(list)) != NULL){
cbdata.current = ++current;
cbdata.total = list->count;
r = lm_ctx_update(ctx, pkg, cmd_update_callback, &cbdata);
bar_free();
if(!r){
error(_("Failed to update "FG_BOLD"%s"FG_RESET": %s"), pkg->name, lm_strerror());
goto end;
}
}
ret = true;
end:
lm_ctx_update_list_free(list);
return ret;
}

View File

@@ -40,8 +40,8 @@ int main(int argc, char *argv[]) {
{.name = "list", .desc = "list all the installed packages", .func = cmd_list },
{.name = "sync", .desc = "update the pool info and package lists", .func = cmd_sync },
{.name = "install", .desc = "install package(s) from remote pools", .func = cmd_install},
{.name = "remove", .desc = "remove installed package(s)", .func = NULL },
{.name = "update", .desc = "update installed package(s)", .func = NULL },
{.name = "remove", .desc = "remove installed package(s)", .func = cmd_remove },
{.name = "update", .desc = "update installed package(s)", .func = cmd_update },
};
char *config_file = NULL, *root_dir = NULL;