pacman/sync: rewrite target handling
[pacman-ng.git] / src / pacman / sync.c
blobdeda77d4f3476d70cc1008a8d5d9cb1593ca83ee
1 /*
2 * sync.c
4 * Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <limits.h>
27 #include <unistd.h>
28 #include <dirent.h>
29 #include <sys/stat.h>
31 #include <alpm.h>
32 #include <alpm_list.h>
34 /* pacman */
35 #include "pacman.h"
36 #include "util.h"
37 #include "package.h"
38 #include "conf.h"
40 /* if keep_used != 0, then the db files which match an used syncdb
41 * will be kept */
42 static int sync_cleandb(const char *dbpath, int keep_used) {
43 DIR *dir;
44 struct dirent *ent;
46 dir = opendir(dbpath);
47 if(dir == NULL) {
48 pm_fprintf(stderr, PM_LOG_ERROR, _("could not access database directory\n"));
49 return(1);
52 rewinddir(dir);
53 /* step through the directory one file at a time */
54 while((ent = readdir(dir)) != NULL) {
55 char path[PATH_MAX];
56 struct stat buf;
57 alpm_list_t *syncdbs = NULL, *i;
58 int found = 0;
59 const char *dname = ent->d_name;
60 size_t len;
62 if(strcmp(dname, ".") == 0 || strcmp(dname, "..") == 0) {
63 continue;
65 /* skip the local and sync directories */
66 if(strcmp(dname, "sync") == 0 || strcmp(dname, "local") == 0) {
67 continue;
69 /* skip the db.lck file */
70 if(strcmp(dname, "db.lck") == 0) {
71 continue;
74 /* build the full path */
75 snprintf(path, PATH_MAX, "%s%s", dbpath, dname);
77 /* remove all non-skipped directories and non-database files */
78 stat(path, &buf);
79 len = strlen(path);
80 if(S_ISDIR(buf.st_mode) || strcmp(path+(len-3),".db") != 0) {
81 if(rmrf(path)) {
82 pm_fprintf(stderr, PM_LOG_ERROR,
83 _("could not remove %s\n"), path);
84 closedir(dir);
85 return(1);
87 continue;
90 if(keep_used) {
91 len = strlen(dname);
92 char *dbname = strndup(dname, len-3);
93 syncdbs = alpm_option_get_syncdbs();
94 for(i = syncdbs; i && !found; i = alpm_list_next(i)) {
95 pmdb_t *db = alpm_list_getdata(i);
96 found = !strcmp(dbname, alpm_db_get_name(db));
98 free(dbname);
100 /* We have a database that doesn't match any syncdb.
101 * Ask the user if he wants to remove it. */
102 if(!found) {
103 if(!yesno(_("Do you want to remove %s?"), path)) {
104 continue;
107 if(rmrf(path)) {
108 pm_fprintf(stderr, PM_LOG_ERROR,
109 _("could not remove %s\n"), path);
110 closedir(dir);
111 return(1);
115 closedir(dir);
116 return(0);
119 static int sync_cleandb_all(void) {
120 const char *dbpath;
121 char newdbpath[PATH_MAX];
122 int ret = 0;
124 dbpath = alpm_option_get_dbpath();
125 printf(_("Database directory: %s\n"), dbpath);
126 if(!yesno(_("Do you want to remove unused repositories?"))) {
127 return(0);
129 /* The sync dbs were previously put in dbpath/, but are now in dbpath/sync/,
130 * so we will clean everything in dbpath/ (except dbpath/local/ and dbpath/sync/
131 * and db.lck) and only the unused sync dbs in dbpath/sync/ */
132 ret += sync_cleandb(dbpath, 0);
134 sprintf(newdbpath, "%s%s", dbpath, "sync/");
135 ret += sync_cleandb(newdbpath, 1);
137 printf(_("Database directory cleaned up\n"));
138 return(ret);
141 static int sync_cleancache(int level)
143 alpm_list_t *i;
144 alpm_list_t *sync_dbs = alpm_option_get_syncdbs();
145 pmdb_t *db_local = alpm_option_get_localdb();
146 int ret = 0;
148 for(i = alpm_option_get_cachedirs(); i; i = alpm_list_next(i)) {
149 printf(_("Cache directory: %s\n"), (char*)alpm_list_getdata(i));
152 if(level == 1) {
153 switch(config->cleanmethod) {
154 case PM_CLEAN_KEEPINST:
155 if(!yesno(_("Do you want to remove uninstalled packages from cache?"))) {
156 return(0);
158 break;
159 case PM_CLEAN_KEEPCUR:
160 if(!yesno(_("Do you want to remove outdated packages from cache?"))) {
161 return(0);
163 break;
164 default:
165 /* this should not happen : the config parsing doesn't set any other value */
166 return(1);
168 printf(_("removing old packages from cache...\n"));
169 } else {
170 if(!noyes(_("Do you want to remove ALL files from cache?"))) {
171 return(0);
173 printf(_("removing all files from cache...\n"));
176 for(i = alpm_option_get_cachedirs(); i; i = alpm_list_next(i)) {
177 const char *cachedir = alpm_list_getdata(i);
178 DIR *dir = opendir(cachedir);
179 struct dirent *ent;
181 if(dir == NULL) {
182 pm_fprintf(stderr, PM_LOG_ERROR,
183 _("could not access cache directory %s\n"), cachedir);
184 ret++;
185 continue;
188 rewinddir(dir);
189 /* step through the directory one file at a time */
190 while((ent = readdir(dir)) != NULL) {
191 char path[PATH_MAX];
192 int delete = 1;
193 pmpkg_t *localpkg = NULL, *pkg = NULL;
194 alpm_list_t *j;
196 if(strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
197 continue;
199 /* build the full filepath */
200 snprintf(path, PATH_MAX, "%s%s", cachedir, ent->d_name);
202 /* short circuit for removing all packages from cache */
203 if(level > 1) {
204 unlink(path);
205 continue;
208 /* attempt to load the package, prompt removal on failures as we may have
209 * files here that aren't valid packages. we also don't need a full
210 * load of the package, just the metadata. */
211 if(alpm_pkg_load(path, 0, &localpkg) != 0 || localpkg == NULL) {
212 if(yesno(_("File %s does not seem to be a valid package, remove it?"), path)) {
213 if(localpkg) {
214 alpm_pkg_free(localpkg);
216 unlink(path);
218 continue;
220 switch(config->cleanmethod) {
221 case PM_CLEAN_KEEPINST:
222 /* check if this package is in the local DB */
223 pkg = alpm_db_get_pkg(db_local, alpm_pkg_get_name(localpkg));
224 if(pkg != NULL && alpm_pkg_vercmp(alpm_pkg_get_version(localpkg),
225 alpm_pkg_get_version(pkg)) == 0) {
226 /* package was found in local DB and version matches, keep it */
227 delete = 0;
229 break;
230 case PM_CLEAN_KEEPCUR:
231 /* check if this package is in a sync DB */
232 for(j = sync_dbs; j && delete; j = alpm_list_next(j)) {
233 pmdb_t *db = alpm_list_getdata(j);
234 pkg = alpm_db_get_pkg(db, alpm_pkg_get_name(localpkg));
235 if(pkg != NULL && alpm_pkg_vercmp(alpm_pkg_get_version(localpkg),
236 alpm_pkg_get_version(pkg)) == 0) {
237 /* package was found in a sync DB and version matches, keep it */
238 delete = 0;
241 break;
242 default:
243 /* this should not happen : the config parsing doesn't set any other value */
244 delete = 0;
245 break;
247 /* free the local file package */
248 alpm_pkg_free(localpkg);
250 if(delete) {
251 unlink(path);
254 closedir(dir);
257 return(ret);
260 static int sync_synctree(int level, alpm_list_t *syncs)
262 alpm_list_t *i;
263 int success = 0, ret;
265 if(trans_init(0) == -1) {
266 return(0);
269 for(i = syncs; i; i = alpm_list_next(i)) {
270 pmdb_t *db = alpm_list_getdata(i);
272 ret = alpm_db_update((level < 2 ? 0 : 1), db);
273 if(ret < 0) {
274 pm_fprintf(stderr, PM_LOG_ERROR, _("failed to update %s (%s)\n"),
275 alpm_db_get_name(db), alpm_strerrorlast());
276 } else if(ret == 1) {
277 printf(_(" %s is up to date\n"), alpm_db_get_name(db));
278 success++;
279 } else {
280 success++;
284 if(trans_release() == -1) {
285 return(0);
287 /* We should always succeed if at least one DB was upgraded - we may possibly
288 * fail later with unresolved deps, but that should be rare, and would be
289 * expected
291 if(!success) {
292 pm_fprintf(stderr, PM_LOG_ERROR, _("failed to synchronize any databases\n"));
294 return(success > 0);
297 static void print_installed(pmdb_t *db_local, pmpkg_t *pkg)
299 const char *pkgname = alpm_pkg_get_name(pkg);
300 const char *pkgver = alpm_pkg_get_version(pkg);
301 pmpkg_t *lpkg = alpm_db_get_pkg(db_local, pkgname);
302 if(lpkg) {
303 const char *lpkgver = alpm_pkg_get_version(lpkg);
304 if(strcmp(lpkgver,pkgver) == 0) {
305 printf(" [%s]", _("installed"));
306 } else {
307 printf(" [%s: %s]", _("installed"), lpkgver);
312 /* search the sync dbs for a matching package */
313 static int sync_search(alpm_list_t *syncs, alpm_list_t *targets)
315 alpm_list_t *i, *j, *ret;
316 int freelist;
317 int found = 0;
318 pmdb_t *db_local = alpm_option_get_localdb();
320 for(i = syncs; i; i = alpm_list_next(i)) {
321 pmdb_t *db = alpm_list_getdata(i);
322 /* if we have a targets list, search for packages matching it */
323 if(targets) {
324 ret = alpm_db_search(db, targets);
325 freelist = 1;
326 } else {
327 ret = alpm_db_get_pkgcache(db);
328 freelist = 0;
330 if(ret == NULL) {
331 continue;
332 } else {
333 found = 1;
335 for(j = ret; j; j = alpm_list_next(j)) {
336 alpm_list_t *grp;
337 pmpkg_t *pkg = alpm_list_getdata(j);
339 if (!config->quiet) {
340 printf("%s/%s %s", alpm_db_get_name(db), alpm_pkg_get_name(pkg),
341 alpm_pkg_get_version(pkg));
342 } else {
343 printf("%s", alpm_pkg_get_name(pkg));
346 /* print the package size with the output if ShowSize option set */
347 if(!config->quiet && config->showsize) {
348 /* Convert byte size to MB */
349 double mbsize = (double)alpm_pkg_get_size(pkg) / (1024.0 * 1024.0);
351 printf(" [%.2f MB]", mbsize);
354 if (!config->quiet) {
355 if((grp = alpm_pkg_get_groups(pkg)) != NULL) {
356 alpm_list_t *k;
357 printf(" (");
358 for(k = grp; k; k = alpm_list_next(k)) {
359 const char *group = alpm_list_getdata(k);
360 printf("%s", group);
361 if(alpm_list_next(k)) {
362 /* only print a spacer if there are more groups */
363 printf(" ");
366 printf(")");
369 print_installed(db_local, pkg);
371 /* we need a newline and initial indent first */
372 printf("\n ");
373 indentprint(alpm_pkg_get_desc(pkg), 4);
375 printf("\n");
377 /* we only want to free if the list was a search list */
378 if(freelist) {
379 alpm_list_free(ret);
383 return(!found);
386 static int sync_group(int level, alpm_list_t *syncs, alpm_list_t *targets)
388 alpm_list_t *i, *j, *k;
390 if(targets) {
391 for(i = targets; i; i = alpm_list_next(i)) {
392 const char *grpname = alpm_list_getdata(i);
393 for(j = syncs; j; j = alpm_list_next(j)) {
394 pmdb_t *db = alpm_list_getdata(j);
395 pmgrp_t *grp = alpm_db_readgrp(db, grpname);
397 if(grp) {
398 /* get names of packages in group */
399 for(k = alpm_grp_get_pkgs(grp); k; k = alpm_list_next(k)) {
400 if(!config->quiet) {
401 printf("%s %s\n", grpname,
402 alpm_pkg_get_name(alpm_list_getdata(k)));
403 } else {
404 printf("%s\n", alpm_pkg_get_name(alpm_list_getdata(k)));
410 } else {
411 for(i = syncs; i; i = alpm_list_next(i)) {
412 pmdb_t *db = alpm_list_getdata(i);
414 for(j = alpm_db_get_grpcache(db); j; j = alpm_list_next(j)) {
415 pmgrp_t *grp = alpm_list_getdata(j);
416 const char *grpname = alpm_grp_get_name(grp);
418 if(level > 1) {
419 for(k = alpm_grp_get_pkgs(grp); k; k = alpm_list_next(k)) {
420 printf("%s %s\n", grpname,
421 alpm_pkg_get_name(alpm_list_getdata(k)));
423 } else {
424 /* print grp names only, no package names */
425 printf("%s\n", grpname);
431 return(0);
434 static int sync_info(alpm_list_t *syncs, alpm_list_t *targets)
436 alpm_list_t *i, *j, *k;
437 int ret = 0;
439 if(targets) {
440 for(i = targets; i; i = alpm_list_next(i)) {
441 pmdb_t *db = NULL;
442 int foundpkg = 0;
444 char target[512]; /* TODO is this enough space? */
445 char *repo = NULL, *pkgstr = NULL;
447 strncpy(target, i->data, 512);
448 pkgstr = strchr(target, '/');
449 if(pkgstr) {
450 repo = target;
451 *pkgstr = '\0';
452 ++pkgstr;
454 for(j = syncs; j; j = alpm_list_next(j)) {
455 db = alpm_list_getdata(j);
456 if(strcmp(repo, alpm_db_get_name(db)) == 0) {
457 break;
459 db = NULL;
462 if(!db) {
463 pm_fprintf(stderr, PM_LOG_ERROR,
464 _("repository '%s' does not exist\n"), repo);
465 return(1);
468 for(k = alpm_db_get_pkgcache(db); k; k = alpm_list_next(k)) {
469 pmpkg_t *pkg = alpm_list_getdata(k);
471 if(strcmp(alpm_pkg_get_name(pkg), pkgstr) == 0) {
472 dump_pkg_sync(pkg, alpm_db_get_name(db), config->op_s_info);
473 foundpkg = 1;
474 break;
478 if(!foundpkg) {
479 pm_fprintf(stderr, PM_LOG_ERROR,
480 _("package '%s' was not found in repository '%s'\n"), pkgstr, repo);
481 ret++;
483 } else {
484 pkgstr = target;
486 for(j = syncs; j; j = alpm_list_next(j)) {
487 pmdb_t *db = alpm_list_getdata(j);
489 for(k = alpm_db_get_pkgcache(db); k; k = alpm_list_next(k)) {
490 pmpkg_t *pkg = alpm_list_getdata(k);
492 if(strcmp(alpm_pkg_get_name(pkg), pkgstr) == 0) {
493 dump_pkg_sync(pkg, alpm_db_get_name(db), config->op_s_info);
494 foundpkg = 1;
495 break;
499 if(!foundpkg) {
500 pm_fprintf(stderr, PM_LOG_ERROR,
501 _("package '%s' was not found\n"), pkgstr);
502 ret++;
506 } else {
507 for(i = syncs; i; i = alpm_list_next(i)) {
508 pmdb_t *db = alpm_list_getdata(i);
510 for(j = alpm_db_get_pkgcache(db); j; j = alpm_list_next(j)) {
511 dump_pkg_sync(alpm_list_getdata(j), alpm_db_get_name(db), config->op_s_info);
516 return(ret);
519 static int sync_list(alpm_list_t *syncs, alpm_list_t *targets)
521 alpm_list_t *i, *j, *ls = NULL;
522 pmdb_t *db_local = alpm_option_get_localdb();
524 if(targets) {
525 for(i = targets; i; i = alpm_list_next(i)) {
526 const char *repo = alpm_list_getdata(i);
527 pmdb_t *db = NULL;
529 for(j = syncs; j; j = alpm_list_next(j)) {
530 pmdb_t *d = alpm_list_getdata(j);
532 if(strcmp(repo, alpm_db_get_name(d)) == 0) {
533 db = d;
534 break;
538 if(db == NULL) {
539 pm_fprintf(stderr, PM_LOG_ERROR,
540 _("repository \"%s\" was not found.\n"),repo);
541 alpm_list_free(ls);
542 return(1);
545 ls = alpm_list_add(ls, db);
547 } else {
548 ls = syncs;
551 for(i = ls; i; i = alpm_list_next(i)) {
552 pmdb_t *db = alpm_list_getdata(i);
554 for(j = alpm_db_get_pkgcache(db); j; j = alpm_list_next(j)) {
555 pmpkg_t *pkg = alpm_list_getdata(j);
557 if (!config->quiet) {
558 printf("%s %s %s", alpm_db_get_name(db), alpm_pkg_get_name(pkg),
559 alpm_pkg_get_version(pkg));
560 print_installed(db_local, pkg);
561 printf("\n");
562 } else {
563 printf("%s\n", alpm_pkg_get_name(pkg));
568 if(targets) {
569 alpm_list_free(ls);
572 return(0);
575 static alpm_list_t *syncfirst(void) {
576 alpm_list_t *i, *res = NULL;
578 for(i = config->syncfirst; i; i = alpm_list_next(i)) {
579 char *pkgname = alpm_list_getdata(i);
580 pmpkg_t *pkg = alpm_db_get_pkg(alpm_option_get_localdb(), pkgname);
581 if(pkg == NULL) {
582 continue;
585 if(alpm_sync_newversion(pkg, alpm_option_get_syncdbs())) {
586 res = alpm_list_add(res, strdup(pkgname));
590 return(res);
593 static pmdb_t *get_db(const char *dbname)
595 alpm_list_t *i;
596 for(i = alpm_option_get_syncdbs(); i; i = i->next) {
597 pmdb_t *db = i->data;
598 if(strcmp(alpm_db_get_name(db), dbname) == 0) {
599 return(db);
602 return(NULL);
605 static int process_pkg(pmpkg_t *pkg)
607 int ret = alpm_add_pkg(pkg);
609 if(ret == -1) {
610 if(pm_errno == PM_ERR_TRANS_DUP_TARGET
611 || pm_errno == PM_ERR_PKG_IGNORED) {
612 /* just skip duplicate or ignored targets */
613 pm_printf(PM_LOG_WARNING, _("skipping target: %s\n"), alpm_pkg_get_name(pkg));
614 return(0);
615 } else {
616 pm_fprintf(stderr, PM_LOG_ERROR, "'%s': %s\n", alpm_pkg_get_name(pkg),
617 alpm_strerrorlast());
618 return(1);
621 return(0);
624 static int process_group(alpm_list_t *dbs, char *group)
626 int ret = 0;
627 alpm_list_t *i;
628 alpm_list_t *pkgs = alpm_find_grp_pkgs(dbs, group);
629 int count = alpm_list_count(pkgs);
631 if(!count) {
632 pm_fprintf(stderr, PM_LOG_ERROR, _("target not found: %s\n"), group);
633 return(1);
636 printf(_(":: There are %d members in group %s:\n"), count,
637 group);
638 select_display(pkgs);
639 select_question(count,
640 _("Which ones do you want to install?"));
641 char *array = malloc(count);
642 memset(array, 1, count);
643 int n = 0;
644 for(i = pkgs; i; i = alpm_list_next(i)) {
645 if(array[n++] == 0)
646 continue;
647 pmpkg_t *pkg = alpm_list_getdata(i);
649 if(process_pkg(pkg) == 1) {
650 ret = 1;
651 goto cleanup;
654 cleanup:
655 alpm_list_free(pkgs);
656 free(array);
657 return(ret);
660 static int process_targname(alpm_list_t *dblist, char *targname)
662 pmpkg_t *pkg = alpm_find_dbs_satisfier(dblist, targname);
664 if(pkg) {
665 return(process_pkg(pkg));
667 /* fallback on group */
668 return(process_group(dblist, targname));
671 static int process_target(char *target)
673 /* process targets */
674 char *targstring = strdup(target);
675 char *targname = strchr(targstring, '/');
676 char *dbname = NULL;
677 int ret = 0;
678 alpm_list_t *dblist = NULL;
680 if(targname) {
681 pmdb_t *db = NULL;
683 *targname = '\0';
684 targname++;
685 dbname = targstring;
686 db = get_db(dbname);
687 if(!db) {
688 pm_fprintf(stderr, PM_LOG_ERROR, _("database not found: %s\n"),
689 dbname);
690 ret = 1;
691 goto cleanup;
693 dblist = alpm_list_add(dblist, db);
694 ret = process_targname(dblist, targname);
695 alpm_list_free(dblist);
696 } else {
697 targname = targstring;
698 dblist = alpm_option_get_syncdbs();
699 ret = process_targname(dblist, targname);
701 cleanup:
702 free(targstring);
703 return(ret);
706 static int sync_trans(alpm_list_t *targets)
708 int retval = 0;
709 alpm_list_t *data = NULL;
710 alpm_list_t *packages = NULL;
711 alpm_list_t *i;
713 /* Step 1: create a new transaction... */
714 if(trans_init(config->flags) == -1) {
715 return(1);
718 /* process targets */
719 for(i = targets; i; i = alpm_list_next(i)) {
720 char *targ = alpm_list_getdata(i);
721 if(process_target(targ) == 1) {
722 retval = 1;
723 goto cleanup;
727 if(config->op_s_upgrade) {
728 printf(_(":: Starting full system upgrade...\n"));
729 alpm_logaction("starting full system upgrade\n");
730 if(alpm_sync_sysupgrade(config->op_s_upgrade >= 2) == -1) {
731 pm_fprintf(stderr, PM_LOG_ERROR, "%s\n", alpm_strerrorlast());
732 retval = 1;
733 goto cleanup;
737 /* Step 2: "compute" the transaction based on targets and flags */
738 if(alpm_trans_prepare(&data) == -1) {
739 pm_fprintf(stderr, PM_LOG_ERROR, _("failed to prepare transaction (%s)\n"),
740 alpm_strerrorlast());
741 switch(pm_errno) {
742 alpm_list_t *i;
743 case PM_ERR_PKG_INVALID_ARCH:
744 for(i = data; i; i = alpm_list_next(i)) {
745 char *pkg = alpm_list_getdata(i);
746 printf(_(":: package %s does not have a valid architecture\n"), pkg);
748 break;
749 case PM_ERR_UNSATISFIED_DEPS:
750 for(i = data; i; i = alpm_list_next(i)) {
751 pmdepmissing_t *miss = alpm_list_getdata(i);
752 pmdepend_t *dep = alpm_miss_get_dep(miss);
753 char *depstring = alpm_dep_compute_string(dep);
754 printf(_(":: %s: requires %s\n"), alpm_miss_get_target(miss),
755 depstring);
756 free(depstring);
758 break;
759 case PM_ERR_CONFLICTING_DEPS:
760 for(i = data; i; i = alpm_list_next(i)) {
761 pmconflict_t *conflict = alpm_list_getdata(i);
762 const char *package1 = alpm_conflict_get_package1(conflict);
763 const char *package2 = alpm_conflict_get_package2(conflict);
764 const char *reason = alpm_conflict_get_reason(conflict);
765 /* only print reason if it contains new information */
766 if(strcmp(package1, reason) == 0 || strcmp(package2, reason) == 0) {
767 printf(_(":: %s and %s are in conflict\n"), package1, package2);
768 } else {
769 printf(_(":: %s and %s are in conflict (%s)\n"), package1, package2, reason);
772 break;
773 default:
774 break;
776 retval = 1;
777 goto cleanup;
780 packages = alpm_trans_get_add();
781 if(packages == NULL) {
782 /* nothing to do: just exit without complaining */
783 printf(_(" there is nothing to do\n"));
784 goto cleanup;
787 /* Step 3: actually perform the operation */
788 if(config->print) {
789 print_packages(packages);
790 goto cleanup;
793 display_targets(alpm_trans_get_remove(), 0);
794 display_targets(alpm_trans_get_add(), 1);
795 printf("\n");
797 int confirm;
798 if(config->op_s_downloadonly) {
799 confirm = yesno(_("Proceed with download?"));
800 } else {
801 confirm = yesno(_("Proceed with installation?"));
803 if(!confirm) {
804 goto cleanup;
807 if(alpm_trans_commit(&data) == -1) {
808 pm_fprintf(stderr, PM_LOG_ERROR, _("failed to commit transaction (%s)\n"),
809 alpm_strerrorlast());
810 switch(pm_errno) {
811 alpm_list_t *i;
812 case PM_ERR_FILE_CONFLICTS:
813 for(i = data; i; i = alpm_list_next(i)) {
814 pmfileconflict_t *conflict = alpm_list_getdata(i);
815 switch(alpm_fileconflict_get_type(conflict)) {
816 case PM_FILECONFLICT_TARGET:
817 printf(_("%s exists in both '%s' and '%s'\n"),
818 alpm_fileconflict_get_file(conflict),
819 alpm_fileconflict_get_target(conflict),
820 alpm_fileconflict_get_ctarget(conflict));
821 break;
822 case PM_FILECONFLICT_FILESYSTEM:
823 printf(_("%s: %s exists in filesystem\n"),
824 alpm_fileconflict_get_target(conflict),
825 alpm_fileconflict_get_file(conflict));
826 break;
829 break;
830 case PM_ERR_PKG_INVALID:
831 case PM_ERR_DLT_INVALID:
832 for(i = data; i; i = alpm_list_next(i)) {
833 char *filename = alpm_list_getdata(i);
834 printf(_("%s is invalid or corrupted\n"), filename);
836 break;
837 default:
838 break;
840 /* TODO: stderr? */
841 printf(_("Errors occurred, no packages were upgraded.\n"));
842 retval = 1;
843 goto cleanup;
846 /* Step 4: release transaction resources */
847 cleanup:
848 if(data) {
849 FREELIST(data);
851 if(trans_release() == -1) {
852 retval = 1;
855 return(retval);
858 int pacman_sync(alpm_list_t *targets)
860 alpm_list_t *sync_dbs = NULL;
862 /* clean the cache */
863 if(config->op_s_clean) {
864 int ret = 0;
866 if(trans_init(0) == -1) {
867 return(1);
870 ret += sync_cleancache(config->op_s_clean);
871 printf("\n");
872 ret += sync_cleandb_all();
874 if(trans_release() == -1) {
875 ret++;
878 return(ret);
881 /* ensure we have at least one valid sync db set up */
882 sync_dbs = alpm_option_get_syncdbs();
883 if(sync_dbs == NULL || alpm_list_count(sync_dbs) == 0) {
884 pm_printf(PM_LOG_ERROR, _("no usable package repositories configured.\n"));
885 return(1);
888 if(config->op_s_sync) {
889 /* grab a fresh package list */
890 printf(_(":: Synchronizing package databases...\n"));
891 alpm_logaction("synchronizing package lists\n");
892 if(!sync_synctree(config->op_s_sync, sync_dbs)) {
893 return(1);
897 /* search for a package */
898 if(config->op_s_search) {
899 return(sync_search(sync_dbs, targets));
902 /* look for groups */
903 if(config->group) {
904 return(sync_group(config->group, sync_dbs, targets));
907 /* get package info */
908 if(config->op_s_info) {
909 return(sync_info(sync_dbs, targets));
912 /* get a listing of files in sync DBs */
913 if(config->op_q_list) {
914 return(sync_list(sync_dbs, targets));
917 if(targets == NULL) {
918 if(config->op_s_upgrade) {
919 /* proceed */
920 } else if(config->op_s_sync) {
921 return(0);
922 } else {
923 /* don't proceed here unless we have an operation that doesn't require a
924 * target list */
925 pm_printf(PM_LOG_ERROR, _("no targets specified (use -h for help)\n"));
926 return(1);
930 alpm_list_t *targs = alpm_list_strdup(targets);
931 if(!(config->flags & PM_TRANS_FLAG_DOWNLOADONLY) && !config->print) {
932 /* check for newer versions of packages to be upgraded first */
933 alpm_list_t *packages = syncfirst();
934 if(packages) {
935 /* Do not ask user if all the -S targets are SyncFirst packages, see FS#15810 */
936 alpm_list_t *tmp = NULL;
937 if(config->op_s_upgrade || (tmp = alpm_list_diff(targets, packages, (alpm_list_fn_cmp)strcmp))) {
938 alpm_list_free(tmp);
939 printf(_(":: The following packages should be upgraded first :\n"));
940 list_display(" ", packages);
941 if(yesno(_(":: Do you want to cancel the current operation\n"
942 ":: and upgrade these packages now?"))) {
943 FREELIST(targs);
944 targs = packages;
945 config->flags = 0;
946 config->op_s_upgrade = 0;
947 } else {
948 FREELIST(packages);
950 printf("\n");
951 } else {
952 pm_printf(PM_LOG_DEBUG, "skipping SyncFirst dialog\n");
953 FREELIST(packages);
958 int ret = sync_trans(targs);
959 FREELIST(targs);
961 return(ret);
964 /* vim: set ts=2 sw=2 noet: */