From cf58aee98947205b451db81de1101db7c6a5bfdc Mon Sep 17 00:00:00 2001 From: Xavier Chantry Date: Sun, 19 Jul 2009 11:15:11 +0200 Subject: [PATCH] Add new --print operation for all operations This implements FS#14208 -U/-S/-R --print : print package name and version -U/-S/-R --print -q : print package name Signed-off-by: Xavier Chantry Signed-off-by: Nagy Gabor --- contrib/bash_completion | 3 +++ contrib/zsh_completion | 1 + doc/pacman.8.txt | 4 ++++ src/pacman/conf.h | 2 +- src/pacman/pacman.c | 18 +++++++++++++++--- src/pacman/remove.c | 14 ++++++++++++++ src/pacman/sync.c | 33 +++++++++++++++++++++------------ src/pacman/upgrade.c | 16 +++++++++++++++- src/pacman/util.c | 28 +++++++++++++++++++--------- 9 files changed, 93 insertions(+), 26 deletions(-) diff --git a/contrib/bash_completion b/contrib/bash_completion index 11f021c..f0b743e 100644 --- a/contrib/bash_completion +++ b/contrib/bash_completion @@ -231,6 +231,7 @@ _pacman () -r --root \ -b --dbpath \ --cachedir \ + --print \ ' -- $cur ) ) return 0 ;; @@ -252,6 +253,7 @@ _pacman () -r --root \ -b --dbpath \ --cachedir \ + --print \ ' -- $cur ) ) return 0 ;; @@ -283,6 +285,7 @@ _pacman () -r --root \ -b --dbpath \ --cachedir \ + --print \ ' -- $cur ) ) return 0 ;; diff --git a/contrib/zsh_completion b/contrib/zsh_completion index e127318..7262917 100644 --- a/contrib/zsh_completion +++ b/contrib/zsh_completion @@ -26,6 +26,7 @@ _pacman_opts_common=( '--noconfirm[Do not ask for confirmation]' '--noprogressbar[Do not show a progress bar when downloading files]' '--noscriptlet[Do not execute the install scriptlet if one exists]' + '--print[Only print the targets instead of performing the operation]' ) # options for passing to _arguments: options for --upgrade commands diff --git a/doc/pacman.8.txt b/doc/pacman.8.txt index af85a15..7439933 100644 --- a/doc/pacman.8.txt +++ b/doc/pacman.8.txt @@ -166,6 +166,10 @@ Options If an install scriptlet exists, do not execute it. Do not use this unless you know what you are doing. +*\--print*:: + Only print the target names and versions instead of performing the actual + operation (sync, remove or upgrade). Use with '\--quiet' to only see the + target names. Query Options[[QO]] ------------------- diff --git a/src/pacman/conf.h b/src/pacman/conf.h index 39802ca..2c02ccb 100644 --- a/src/pacman/conf.h +++ b/src/pacman/conf.h @@ -58,7 +58,7 @@ typedef struct __config_t { unsigned short op_s_sync; unsigned short op_s_search; unsigned short op_s_upgrade; - unsigned short op_s_printuris; + unsigned short op_s_print; unsigned short group; pmtransflag_t flags; diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c index 7f86489..6fe628f 100644 --- a/src/pacman/pacman.c +++ b/src/pacman/pacman.c @@ -138,6 +138,7 @@ static void usage(int op, const char * const myname) " ignore a group upgrade (can be used more than once)\n")); printf(_(" -q, --quiet show less information for query and search\n")); } + printf(_(" --print only print the targets instead of performing the operation\n")); printf(_(" --config set an alternate configuration file\n")); printf(_(" --logfile set an alternate log file\n")); printf(_(" --noconfirm do not ask for any confirmation\n")); @@ -374,6 +375,7 @@ static int parseargs(int argc, char *argv[]) {"ignoregroup", required_argument, 0, 1010}, {"needed", no_argument, 0, 1011}, {"asexplicit", no_argument, 0, 1012}, + {"print", no_argument, 0, 1013}, {0, 0, 0, 0} }; @@ -448,6 +450,9 @@ static int parseargs(int argc, char *argv[]) case 1012: config->flags |= PM_TRANS_FLAG_ALLEXPLICIT; break; + case 1013: + config->op_s_print = 1; + break; case 'Q': config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_QUERY); break; case 'R': config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_REMOVE); break; case 'S': config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_SYNC); break; @@ -480,9 +485,7 @@ static int parseargs(int argc, char *argv[]) case 'o': config->op_q_owns = 1; break; case 'p': config->op_q_isfile = 1; - config->op_s_printuris = 1; - config->flags |= PM_TRANS_FLAG_NOCONFLICTS; - config->flags |= PM_TRANS_FLAG_NOLOCK; + config->op_s_print = 2; break; case 'q': config->quiet = 1; @@ -900,6 +903,15 @@ int main(int argc, char *argv[]) alpm_option_set_totaldlcb(cb_dl_total); } + /* set up the print operations */ + if(config->op_s_print) { + /* Display only errors */ + config->logmask &= ~PM_LOG_WARNING; + config->flags |= PM_TRANS_FLAG_NOCONFLICTS; + config->flags |= PM_TRANS_FLAG_NOLOCK; + } + + #if defined(HAVE_GETEUID) && !defined(CYGWIN) /* check if we have sufficient permission for the requested operation */ if(myuid > 0 && needs_root()) { diff --git a/src/pacman/remove.c b/src/pacman/remove.c index 0efbd94..08c5856 100644 --- a/src/pacman/remove.c +++ b/src/pacman/remove.c @@ -153,6 +153,20 @@ int pacman_remove(alpm_list_t *targets) } /* Step 3: actually perform the removal */ + if(config->op_s_print) { /* --print */ + alpm_list_t *i; + for(i = alpm_trans_get_pkgs(); i; i = alpm_list_next(i)) { + pmpkg_t *pkg = alpm_list_getdata(i); + const char *pkgname = alpm_pkg_get_name(pkg); + if(!config->quiet) { + printf("%s %s\n", pkgname, alpm_pkg_get_version(pkg)); + } else { + printf("%s\n", pkgname); + } + } + /* we are done */ + goto cleanup; + } if(alpm_trans_commit(NULL) == -1) { pm_fprintf(stderr, PM_LOG_ERROR, _("failed to commit transaction (%s)\n"), alpm_strerrorlast()); diff --git a/src/pacman/sync.c b/src/pacman/sync.c index cb0b8b1..5f97aa9 100644 --- a/src/pacman/sync.c +++ b/src/pacman/sync.c @@ -671,19 +671,28 @@ static int sync_trans(alpm_list_t *targets) } /* Step 3: actually perform the operation */ - if(config->op_s_printuris) { + if(config->op_s_print) { /* print uris */ alpm_list_t *i; for(i = packages; i; i = alpm_list_next(i)) { pmpkg_t *pkg = alpm_list_getdata(i); - pmdb_t *db = alpm_pkg_get_db(pkg); - const char *dburl = alpm_db_get_url(db); - if(dburl) { - printf("%s/%s\n", dburl, alpm_pkg_get_filename(pkg)); - } else { - /* can't use WARNING here, we don't show warnings in -Sp... */ - pm_fprintf(stderr, PM_LOG_ERROR, _("no database for package: %s\n"), - alpm_pkg_get_name(pkg)); + const char *pkgname = alpm_pkg_get_name(pkg); + if(config->op_s_print == 2) { /* --printuris */ + pmdb_t *db = alpm_pkg_get_db(pkg); + const char *dburl = alpm_db_get_url(db); + if(dburl) { + printf("%s/%s\n", dburl, alpm_pkg_get_filename(pkg)); + } else { + /* can't use WARNING here, we don't show warnings in -Sp... */ + pm_fprintf(stderr, PM_LOG_ERROR, _("no database for package: %s\n"), + pkgname); + } + } else { /* --print */ + if(!config->quiet) { + printf("%s %s\n", pkgname, alpm_pkg_get_version(pkg)); + } else { + printf("%s\n", pkgname); + } } } @@ -759,8 +768,8 @@ int pacman_sync(alpm_list_t *targets) { alpm_list_t *sync_dbs = NULL; - /* Display only errors with -Sp and -Sw operations */ - if((config->flags & PM_TRANS_FLAG_DOWNLOADONLY) || config->op_s_printuris) { + /* Display only errors with -Sw operations */ + if(config->flags & PM_TRANS_FLAG_DOWNLOADONLY) { config->logmask &= ~PM_LOG_WARNING; } @@ -833,7 +842,7 @@ int pacman_sync(alpm_list_t *targets) } alpm_list_t *targs = alpm_list_strdup(targets); - if(!(config->flags & PM_TRANS_FLAG_DOWNLOADONLY) && !config->op_s_printuris) { + if(!(config->flags & PM_TRANS_FLAG_DOWNLOADONLY) && !config->op_s_print) { /* check for newer versions of packages to be upgraded first */ alpm_list_t *packages = syncfirst(); if(packages) { diff --git a/src/pacman/upgrade.c b/src/pacman/upgrade.c index 1570f95..85f880c 100644 --- a/src/pacman/upgrade.c +++ b/src/pacman/upgrade.c @@ -70,7 +70,6 @@ int pacman_upgrade(alpm_list_t *targets) } /* add targets to the created transaction */ - printf(_("loading package data...\n")); for(i = targets; i; i = alpm_list_next(i)) { char *targ = alpm_list_getdata(i); if(alpm_trans_addtarget(targ) == -1) { @@ -137,6 +136,21 @@ int pacman_upgrade(alpm_list_t *targets) alpm_list_free(data); /* Step 3: perform the installation */ + if(config->op_s_print) { /* --print */ + alpm_list_t *i; + for(i = alpm_trans_get_pkgs(); i; i = alpm_list_next(i)) { + pmpkg_t *pkg = alpm_list_getdata(i); + const char *pkgname = alpm_pkg_get_name(pkg); + if(!config->quiet) { + printf("%s %s\n", pkgname, alpm_pkg_get_version(pkg)); + } else { + printf("%s\n", pkgname); + } + } + /* we are done */ + trans_release(); + return(0); + } if(alpm_trans_commit(NULL) == -1) { pm_fprintf(stderr, PM_LOG_ERROR, _("failed to commit transaction (%s)\n"), alpm_strerrorlast()); diff --git a/src/pacman/util.c b/src/pacman/util.c index a02b43c..9c4b797 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -47,8 +47,15 @@ int trans_init(pmtranstype_t type, pmtransflag_t flags) { - if(alpm_trans_init(type, flags, cb_trans_evt, - cb_trans_conv, cb_trans_progress) == -1) { + int ret; + if(config->op_s_print) { + ret = alpm_trans_init(type, flags, NULL, NULL, NULL); + } else { + ret = alpm_trans_init(type, flags, cb_trans_evt, cb_trans_conv, + cb_trans_progress); + } + + if(ret == -1) { pm_fprintf(stderr, PM_LOG_ERROR, _("failed to init transaction (%s)\n"), alpm_strerrorlast()); if(pm_errno == PM_ERR_HANDLE_LOCK) { @@ -72,13 +79,16 @@ int trans_release(void) int needs_root(void) { - if(config->op == PM_OP_UPGRADE || config->op == PM_OP_REMOVE || /* -U, -R */ - (config->op == PM_OP_SYNC && (config->op_s_clean || config->op_s_sync || /* -Sc, -Sy */ - (!config->group && !config->op_s_info && !config->op_q_list /* all other -S combinations, where */ - && !config->op_s_search && !config->op_s_printuris)))) { /* -g, -i, -l, -s, -p is not set */ - return(1); - } else { - return(0); + switch(config->op) { + case PM_OP_UPGRADE: + case PM_OP_REMOVE: + return(!config->op_s_print); + case PM_OP_SYNC: + return(config->op_s_clean || config->op_s_sync || + (!config->group && !config->op_s_info && !config->op_q_list && + !config->op_s_search && !config->op_s_print)); + default: + return(0); } } -- 1.6.3.3