1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-10-31 15:45:03 -04:00

src/pacman : rework the display_targets function.

We had a lot of duplicated code here. The code handling the showsize option
needed to be there three times :
1) for install part of -S
2) for remove part of -S (conflict removal)
3) for -R

This patch introduce a new display_targets(pkglist, install) function which
can handle the 3 cases above. We pass install == 1 for case 1), and install
== 0 for case 2) and 3).

Now we can finally get the benefit of an old patch which handled the
ShowSize option consistently in the 3 cases above, without an awful lot of
duplicated code :
http://www.archlinux.org/pipermail/pacman-dev/2008-January/011029.html

Signed-off-by: Xavier Chantry <shiningxc@gmail.com>
Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Xavier Chantry 2008-07-25 02:02:36 +02:00 committed by Dan McGee
parent e6fb229534
commit dfae7bdd52
5 changed files with 65 additions and 59 deletions

View File

@ -261,7 +261,7 @@ static int query_upgrades(void)
return(-1);
}
if(syncpkgs) {
display_targets(syncpkgs, db_local);
display_synctargets(syncpkgs);
return(0);
}

View File

@ -125,21 +125,16 @@ int pacman_remove(alpm_list_t *targets)
if(config->flags & PM_TRANS_FLAG_RECURSE ||
config->flags & PM_TRANS_FLAG_CASCADE) {
/* list transaction targets */
alpm_list_t *lst = NULL;
/* create a new list of package names only */
for(i = alpm_trans_get_pkgs(); i; i = alpm_list_next(i)) {
pmpkg_t *pkg = alpm_list_getdata(i);
lst = alpm_list_add(lst, strdup(alpm_pkg_get_name(pkg)));
}
alpm_list_t *pkglist = alpm_trans_get_pkgs();
display_targets(pkglist, 0);
printf("\n");
list_display(_("Targets:"), lst);
FREELIST(lst);
/* get confirmation */
if(yesno(1, _("\nDo you want to remove these packages?")) == 0) {
if(yesno(1, _("Do you want to remove these packages?")) == 0) {
retval = 1;
goto cleanup;
}
printf("\n");
}
/* Step 3: actually perform the removal */

View File

@ -669,7 +669,7 @@ static int sync_trans(alpm_list_t *targets)
if(!(alpm_trans_get_flags() & PM_TRANS_FLAG_PRINTURIS)) {
int confirm;
display_targets(packages, db_local);
display_synctargets(packages);
printf("\n");
if(config->op_s_downloadonly) {

View File

@ -480,49 +480,33 @@ void list_display(const char *title, const alpm_list_t *list)
}
}
/* Display a list of transaction targets.
* `pkgs` should be a list of pmsyncpkg_t's,
* retrieved from a transaction object
*/
/* TODO move to output.c? or just combine util and output */
void display_targets(const alpm_list_t *syncpkgs, pmdb_t *db_local)
/* prepare a list of pkgs to display */
void display_targets(const alpm_list_t *pkgs, int install)
{
char *str;
const alpm_list_t *i, *j;
alpm_list_t *targets = NULL, *to_remove = NULL;
/* TODO these are some messy variable names */
off_t isize = 0, rsize = 0, dispsize = 0, dlsize = 0;
double mbisize = 0.0, mbrsize = 0.0, mbdispsize = 0.0, mbdlsize = 0.0;
const alpm_list_t *i;
off_t isize = 0, dlsize = 0;
double mbisize = 0.0, mbdlsize = 0.0;
alpm_list_t *targets = NULL;
for(i = syncpkgs; i; i = alpm_list_next(i)) {
pmsyncpkg_t *sync = alpm_list_getdata(i);
pmpkg_t *pkg = alpm_sync_get_pkg(sync);
if(!pkgs) {
return;
}
/* The removes member contains a list of packages to be removed
* due to the package that is being installed. */
alpm_list_t *to_replace = alpm_sync_get_removes(sync);
printf("\n");
for(i = pkgs; i; i = alpm_list_next(i)) {
pmpkg_t *pkg = alpm_list_getdata(i);
for(j = to_replace; j; j = alpm_list_next(j)) {
pmpkg_t *rp = alpm_list_getdata(j);
const char *name = alpm_pkg_get_name(rp);
if(!alpm_list_find_str(to_remove, name)) {
rsize += alpm_pkg_get_isize(rp);
to_remove = alpm_list_add(to_remove, strdup(name));
}
}
dispsize = alpm_pkg_get_size(pkg);
dlsize += alpm_pkg_download_size(pkg);
isize += alpm_pkg_get_isize(pkg);
/* print the package size with the output if ShowSize option set */
if(config->showsize) {
/* Convert byte size to MB */
mbdispsize = dispsize / (1024.0 * 1024.0);
double mbsize = 0.0;
mbsize = alpm_pkg_get_size(pkg) / (1024.0 * 1024.0);
asprintf(&str, "%s-%s [%.2f MB]", alpm_pkg_get_name(pkg),
alpm_pkg_get_version(pkg), mbdispsize);
alpm_pkg_get_version(pkg), mbsize);
} else {
asprintf(&str, "%s-%s", alpm_pkg_get_name(pkg),
alpm_pkg_get_version(pkg));
@ -531,31 +515,57 @@ void display_targets(const alpm_list_t *syncpkgs, pmdb_t *db_local)
}
/* Convert byte sizes to MB */
mbisize = isize / (1024.0 * 1024.0);
mbrsize = rsize / (1024.0 * 1024.0);
mbdlsize = dlsize / (1024.0 * 1024.0);
mbisize = isize / (1024.0 * 1024.0);
/* start displaying information */
printf("\n");
if(to_remove) {
list_display(_("Remove:"), to_remove);
if(install) {
list_display(_("Targets:"), targets);
printf("\n");
FREELIST(to_remove);
printf(_("Total Removed Size: %.2f MB\n"), mbrsize);
printf(_("Total Download Size: %.2f MB\n"), mbdlsize);
printf(_("Total Installed Size: %.2f MB\n"), mbisize);
} else {
list_display(_("Remove:"), targets);
printf("\n");
printf(_("Total Removed Size: %.2f MB\n"), mbisize);
}
list_display(_("Targets:"), targets);
printf("\n");
printf(_("Total Download Size: %.2f MB\n"), mbdlsize);
printf(_("Total Installed Size: %.2f MB\n"), mbisize);
FREELIST(targets);
}
/* Display a list of transaction targets.
* `pkgs` should be a list of pmsyncpkg_t's,
* retrieved from a transaction object
*/
void display_synctargets(const alpm_list_t *syncpkgs)
{
const alpm_list_t *i, *j;
alpm_list_t *pkglist = NULL, *rpkglist = NULL;
for(i = syncpkgs; i; i = alpm_list_next(i)) {
pmsyncpkg_t *sync = alpm_list_getdata(i);
pmpkg_t *pkg = alpm_sync_get_pkg(sync);
pkglist = alpm_list_add(pkglist, pkg);
/* The removes member contains a list of packages to be removed
* due to the package that is being installed. */
alpm_list_t *to_replace = alpm_sync_get_removes(sync);
for(j = to_replace; j; j = alpm_list_next(j)) {
pmpkg_t *rp = alpm_list_getdata(j);
rpkglist = alpm_list_add(rpkglist, rp);
}
}
/* start displaying information */
display_targets(rpkglist, 0);
display_targets(pkglist, 1);
alpm_list_free(pkglist);
alpm_list_free(rpkglist);
}
/* presents a prompt and gets a Y/N answer */
int yesno(short preset, char *fmt, ...)
{

View File

@ -51,7 +51,8 @@ char *strreplace(const char *str, const char *needle, const char *replace);
alpm_list_t *strsplit(const char *str, const char splitchar);
void string_display(const char *title, const char *string);
void list_display(const char *title, const alpm_list_t *list);
void display_targets(const alpm_list_t *syncpkgs, pmdb_t *db_local);
void display_targets(const alpm_list_t *pkgs, int install);
void display_synctargets(const alpm_list_t *syncpkgs);
int yesno(short preset, char *fmt, ...);
int pm_printf(pmloglevel_t level, const char *format, ...) __attribute__((format(printf,2,3)));
int pm_fprintf(FILE *stream, pmloglevel_t level, const char *format, ...) __attribute__((format(printf,3,4)));