102 lines
3.2 KiB
C
102 lines
3.2 KiB
C
#include <libmp/all.h>
|
|
#include <libmp/ctx.h>
|
|
#include <libmp/error.h>
|
|
#include <stdio.h>
|
|
|
|
#include "../cmd.h"
|
|
#include "../log.h"
|
|
|
|
bool cmd_update_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_update_download_callback(lm_ctx_t *ctx, lm_pkg_t *pkg, bool is_archive, size_t current, size_t total, void *data){
|
|
if(!is_archive)
|
|
bar(current, total);
|
|
return true;
|
|
}
|
|
|
|
bool cmd_update(lm_ctx_t *ctx, config_t *config, args_t *args){
|
|
args_split(args, "yes");
|
|
|
|
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;
|
|
}
|
|
|
|
if(lm_ctx_sync(ctx, false, NULL, NULL) <= 0){
|
|
error(_("There are no avaliable pools, have you synced yet?"));
|
|
return false;
|
|
}
|
|
|
|
lm_pkg_t *old = NULL, *new = NULL;
|
|
lm_ctx_update_list_t *list = NULL;
|
|
ssize_t current = 0, save = 0;
|
|
bool ret = false, r = false;
|
|
|
|
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 "FG_BOLD"%d"FG_RESET" package will be "FG_BOLD"UPDATED:"FG_RESET), list->count);
|
|
else
|
|
info(_("Following "FG_BOLD"%d"FG_RESET" packages will be "FG_BOLD"UPDATED:"FG_RESET), list->count);
|
|
|
|
while((old = lm_ctx_update_list_next(list)) != NULL)
|
|
package_list(old, &save, 0);
|
|
printf("\n");
|
|
|
|
if(!args_get_bool(args, "yes") && !yesno(_("Continue?"))){
|
|
error(_("Operation cancelled"));
|
|
goto end;
|
|
}
|
|
|
|
while((old = lm_ctx_update_list_next(list)) != NULL){
|
|
if((new = lm_ctx_update(ctx, old)) == NULL){
|
|
error(_("Failed to update "FG_BOLD"%s"FG_RESET"_"FG_BOLD FG_GREEN"%s"FG_RESET": %s"), old->name, old->version, lm_strerror());
|
|
goto end;
|
|
}
|
|
|
|
info(_("("FG_BOLD"%d"FG_RESET"/"FG_BOLD"%d"FG_RESET") Downloading "FG_BOLD"%s"FG_RESET"_"FG_BOLD FG_GREEN"%s"), ++current, list->count, new->name, new->version);
|
|
r = lm_ctx_download(ctx, new, cmd_update_download_callback, NULL);
|
|
bar_free();
|
|
|
|
if(!r){
|
|
error(_("Failed to download "FG_BOLD"%s"FG_RESET"_"FG_BOLD FG_GREEN"%s"FG_RESET": %s"), new->name, new->version, lm_strerror());
|
|
goto end;
|
|
}
|
|
|
|
info(_("("FG_BOLD"%d"FG_RESET"/"FG_BOLD"%d"FG_RESET") Removing "FG_BOLD"%s"FG_RESET"_"FG_BOLD FG_GREEN"%s"), current, list->count, old->name, old->version);
|
|
r = lm_ctx_remove(ctx, old, cmd_update_callback, NULL);
|
|
bar_free();
|
|
|
|
if(!r){
|
|
error(_("Failed to remove "FG_BOLD"%s"FG_RESET"_"FG_BOLD FG_GREEN"%s"FG_RESET": %s"), old->name, old->version, lm_strerror());
|
|
goto end;
|
|
}
|
|
|
|
info(_("("FG_BOLD"%d"FG_RESET"/"FG_BOLD"%d"FG_RESET") Installing "FG_BOLD"%s"FG_RESET"_"FG_BOLD FG_GREEN"%s"), current, list->count, new->name, new->version);
|
|
r = lm_ctx_install(ctx, new, cmd_update_callback, NULL);
|
|
bar_free();
|
|
|
|
if(!r){
|
|
error(_("Failed to install "FG_BOLD"%s"FG_RESET"_"FG_BOLD FG_GREEN"%s"FG_RESET": %s"), new->name, new->version, lm_strerror());
|
|
goto end;
|
|
}
|
|
}
|
|
|
|
ret = true;
|
|
end:
|
|
lm_ctx_update_list_free(list);
|
|
return ret;
|
|
}
|