Fix handling of ignored packages
[pacman-ng.git] / src / pacman / sync.c
blob29bcf540d5c39aac1488f5aea53792775439bf8e
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;
45 alpm_list_t *syncdbs;
47 dir = opendir(dbpath);
48 if(dir == NULL) {
49 pm_fprintf(stderr, PM_LOG_ERROR, _("could not access database directory\n"));
50 return(1);
53 syncdbs = alpm_option_get_syncdbs();
55 rewinddir(dir);
56 /* step through the directory one file at a time */
57 while((ent = readdir(dir)) != NULL) {
58 char path[PATH_MAX];
59 struct stat buf;
60 int found = 0;
61 const char *dname = ent->d_name;
62 size_t len;
64 if(strcmp(dname, ".") == 0 || strcmp(dname, "..") == 0) {
65 continue;
67 /* skip the local and sync directories */
68 if(strcmp(dname, "sync") == 0 || strcmp(dname, "local") == 0) {
69 continue;
71 /* skip the db.lck file */
72 if(strcmp(dname, "db.lck") == 0) {
73 continue;
76 /* build the full path */
77 snprintf(path, PATH_MAX, "%s%s", dbpath, dname);
79 /* remove all non-skipped directories and non-database files */
80 stat(path, &buf);
81 len = strlen(path);
82 if(S_ISDIR(buf.st_mode) || strcmp(path + len - 3, ".db") != 0) {
83 if(rmrf(path)) {
84 pm_fprintf(stderr, PM_LOG_ERROR,
85 _("could not remove %s\n"), path);
86 closedir(dir);
87 return(1);
89 continue;
92 if(keep_used) {
93 alpm_list_t *i;
94 len = strlen(dname);
95 char *dbname = strndup(dname, len - 3);
96 for(i = syncdbs; i && !found; i = alpm_list_next(i)) {
97 pmdb_t *db = alpm_list_getdata(i);
98 found = !strcmp(dbname, alpm_db_get_name(db));
100 free(dbname);
102 /* We have a database that doesn't match any syncdb.
103 * Ask the user if he wants to remove it. */
104 if(!found) {
105 if(!yesno(_("Do you want to remove %s?"), path)) {
106 continue;
109 if(rmrf(path)) {
110 pm_fprintf(stderr, PM_LOG_ERROR,
111 _("could not remove %s\n"), path);
112 closedir(dir);
113 return(1);
117 closedir(dir);
118 return(0);
121 static int sync_cleandb_all(void) {
122 const char *dbpath;
123 char newdbpath[PATH_MAX];
124 int ret = 0;
126 dbpath = alpm_option_get_dbpath();
127 printf(_("Database directory: %s\n"), dbpath);
128 if(!yesno(_("Do you want to remove unused repositories?"))) {
129 return(0);
131 /* The sync dbs were previously put in dbpath/ but are now in dbpath/sync/.
132 * We will clean everything in dbpath/ except local/, sync/ and db.lck, and
133 * only the unused sync dbs in dbpath/sync/ */
134 ret += sync_cleandb(dbpath, 0);
136 sprintf(newdbpath, "%s%s", dbpath, "sync/");
137 ret += sync_cleandb(newdbpath, 1);
139 printf(_("Database directory cleaned up\n"));
140 return(ret);
143 static int sync_cleancache(int level)
145 alpm_list_t *i;
146 alpm_list_t *sync_dbs = alpm_option_get_syncdbs();
147 pmdb_t *db_local = alpm_option_get_localdb();
148 int ret = 0;
150 for(i = alpm_option_get_cachedirs(); i; i = alpm_list_next(i)) {
151 printf(_("Cache directory: %s\n"), (char*)alpm_list_getdata(i));
154 if(!config->cleanmethod) {
155 /* default to KeepInstalled if user did not specify */
156 config->cleanmethod = PM_CLEAN_KEEPINST;
159 if(level == 1) {
160 printf(_("Packages to keep:\n"));
161 if(config->cleanmethod & PM_CLEAN_KEEPINST) {
162 printf(_(" All locally installed packages\n"));
164 if(config->cleanmethod & PM_CLEAN_KEEPCUR) {
165 printf(_(" All current sync database packages\n"));
167 if(!yesno(_("Do you want to remove all other packages from cache?"))) {
168 return(0);
170 printf(_("removing old packages from cache...\n"));
171 } else {
172 if(!noyes(_("Do you want to remove ALL files from cache?"))) {
173 return(0);
175 printf(_("removing all files from cache...\n"));
178 for(i = alpm_option_get_cachedirs(); i; i = alpm_list_next(i)) {
179 const char *cachedir = alpm_list_getdata(i);
180 DIR *dir = opendir(cachedir);
181 struct dirent *ent;
183 if(dir == NULL) {
184 pm_fprintf(stderr, PM_LOG_ERROR,
185 _("could not access cache directory %s\n"), cachedir);
186 ret++;
187 continue;
190 rewinddir(dir);
191 /* step through the directory one file at a time */
192 while((ent = readdir(dir)) != NULL) {
193 char path[PATH_MAX];
194 int delete = 1;
195 pmpkg_t *localpkg = NULL, *pkg = NULL;
196 const char *local_name, *local_version;
197 alpm_list_t *j;
199 if(strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
200 continue;
202 /* build the full filepath */
203 snprintf(path, PATH_MAX, "%s%s", cachedir, ent->d_name);
205 /* short circuit for removing all packages from cache */
206 if(level > 1) {
207 unlink(path);
208 continue;
211 /* attempt to load the package, prompt removal on failures as we may have
212 * files here that aren't valid packages. we also don't need a full
213 * load of the package, just the metadata. */
214 if(alpm_pkg_load(path, 0, &localpkg) != 0 || localpkg == NULL) {
215 if(yesno(_("File %s does not seem to be a valid package, remove it?"), path)) {
216 if(localpkg) {
217 alpm_pkg_free(localpkg);
219 unlink(path);
221 continue;
223 local_name = alpm_pkg_get_name(localpkg);
224 local_version = alpm_pkg_get_version(localpkg);
226 if(config->cleanmethod & PM_CLEAN_KEEPINST) {
227 /* check if this package is in the local DB */
228 pkg = alpm_db_get_pkg(db_local, local_name);
229 if(pkg != NULL && alpm_pkg_vercmp(local_version,
230 alpm_pkg_get_version(pkg)) == 0) {
231 /* package was found in local DB and version matches, keep it */
232 pm_printf(PM_LOG_DEBUG, "pkg %s-%s found in local db\n",
233 local_name, local_version);
234 delete = 0;
237 if(config->cleanmethod & PM_CLEAN_KEEPCUR) {
238 /* check if this package is in a sync DB */
239 for(j = sync_dbs; j && delete; j = alpm_list_next(j)) {
240 pmdb_t *db = alpm_list_getdata(j);
241 pkg = alpm_db_get_pkg(db, local_name);
242 if(pkg != NULL && alpm_pkg_vercmp(local_version,
243 alpm_pkg_get_version(pkg)) == 0) {
244 /* package was found in a sync DB and version matches, keep it */
245 pm_printf(PM_LOG_DEBUG, "pkg %s-%s found in sync db\n",
246 local_name, local_version);
247 delete = 0;
251 /* free the local file package */
252 alpm_pkg_free(localpkg);
254 if(delete) {
255 unlink(path);
258 closedir(dir);
261 return(ret);
264 static int sync_synctree(int level, alpm_list_t *syncs)
266 alpm_list_t *i;
267 int success = 0, ret;
269 if(trans_init(0) == -1) {
270 return(0);
273 for(i = syncs; i; i = alpm_list_next(i)) {
274 pmdb_t *db = alpm_list_getdata(i);
276 ret = alpm_db_update((level < 2 ? 0 : 1), db);
277 if(ret < 0) {
278 pm_fprintf(stderr, PM_LOG_ERROR, _("failed to update %s (%s)\n"),
279 alpm_db_get_name(db), alpm_strerrorlast());
280 } else if(ret == 1) {
281 printf(_(" %s is up to date\n"), alpm_db_get_name(db));
282 success++;
283 } else {
284 success++;
288 if(trans_release() == -1) {
289 return(0);
291 /* We should always succeed if at least one DB was upgraded - we may possibly
292 * fail later with unresolved deps, but that should be rare, and would be
293 * expected
295 if(!success) {
296 pm_fprintf(stderr, PM_LOG_ERROR, _("failed to synchronize any databases\n"));
298 return(success > 0);
301 static void print_installed(pmdb_t *db_local, pmpkg_t *pkg)
303 const char *pkgname = alpm_pkg_get_name(pkg);
304 const char *pkgver = alpm_pkg_get_version(pkg);
305 pmpkg_t *lpkg = alpm_db_get_pkg(db_local, pkgname);
306 if(lpkg) {
307 const char *lpkgver = alpm_pkg_get_version(lpkg);
308 if(strcmp(lpkgver,pkgver) == 0) {
309 printf(" [%s]", _("installed"));
310 } else {
311 printf(" [%s: %s]", _("installed"), lpkgver);
316 /* search the sync dbs for a matching package */
317 static int sync_search(alpm_list_t *syncs, alpm_list_t *targets)
319 alpm_list_t *i, *j, *ret;
320 int freelist;
321 int found = 0;
322 pmdb_t *db_local = alpm_option_get_localdb();
324 for(i = syncs; i; i = alpm_list_next(i)) {
325 pmdb_t *db = alpm_list_getdata(i);
326 /* if we have a targets list, search for packages matching it */
327 if(targets) {
328 ret = alpm_db_search(db, targets);
329 freelist = 1;
330 } else {
331 ret = alpm_db_get_pkgcache(db);
332 freelist = 0;
334 if(ret == NULL) {
335 continue;
336 } else {
337 found = 1;
339 for(j = ret; j; j = alpm_list_next(j)) {
340 alpm_list_t *grp;
341 pmpkg_t *pkg = alpm_list_getdata(j);
343 if (!config->quiet) {
344 printf("%s/%s %s", alpm_db_get_name(db), alpm_pkg_get_name(pkg),
345 alpm_pkg_get_version(pkg));
346 } else {
347 printf("%s", alpm_pkg_get_name(pkg));
350 /* print the package size with the output if ShowSize option set */
351 if(!config->quiet && config->showsize) {
352 /* Convert byte size to MB */
353 double mbsize = (double)alpm_pkg_get_size(pkg) / (1024.0 * 1024.0);
355 printf(" [%.2f MB]", mbsize);
358 if (!config->quiet) {
359 if((grp = alpm_pkg_get_groups(pkg)) != NULL) {
360 alpm_list_t *k;
361 printf(" (");
362 for(k = grp; k; k = alpm_list_next(k)) {
363 const char *group = alpm_list_getdata(k);
364 printf("%s", group);
365 if(alpm_list_next(k)) {
366 /* only print a spacer if there are more groups */
367 printf(" ");
370 printf(")");
373 print_installed(db_local, pkg);
375 /* we need a newline and initial indent first */
376 printf("\n ");
377 indentprint(alpm_pkg_get_desc(pkg), 4);
379 printf("\n");
381 /* we only want to free if the list was a search list */
382 if(freelist) {
383 alpm_list_free(ret);
387 return(!found);
390 static int sync_group(int level, alpm_list_t *syncs, alpm_list_t *targets)
392 alpm_list_t *i, *j, *k;
394 if(targets) {
395 for(i = targets; i; i = alpm_list_next(i)) {
396 const char *grpname = alpm_list_getdata(i);
397 for(j = syncs; j; j = alpm_list_next(j)) {
398 pmdb_t *db = alpm_list_getdata(j);
399 pmgrp_t *grp = alpm_db_readgrp(db, grpname);
401 if(grp) {
402 /* get names of packages in group */
403 for(k = alpm_grp_get_pkgs(grp); k; k = alpm_list_next(k)) {
404 if(!config->quiet) {
405 printf("%s %s\n", grpname,
406 alpm_pkg_get_name(alpm_list_getdata(k)));
407 } else {
408 printf("%s\n", alpm_pkg_get_name(alpm_list_getdata(k)));
414 } else {
415 for(i = syncs; i; i = alpm_list_next(i)) {
416 pmdb_t *db = alpm_list_getdata(i);
418 for(j = alpm_db_get_grpcache(db); j; j = alpm_list_next(j)) {
419 pmgrp_t *grp = alpm_list_getdata(j);
420 const char *grpname = alpm_grp_get_name(grp);
422 if(level > 1) {
423 for(k = alpm_grp_get_pkgs(grp); k; k = alpm_list_next(k)) {
424 printf("%s %s\n", grpname,
425 alpm_pkg_get_name(alpm_list_getdata(k)));
427 } else {
428 /* print grp names only, no package names */
429 printf("%s\n", grpname);
435 return(0);
438 static int sync_info(alpm_list_t *syncs, alpm_list_t *targets)
440 alpm_list_t *i, *j, *k;
441 int ret = 0;
443 if(targets) {
444 for(i = targets; i; i = alpm_list_next(i)) {
445 pmdb_t *db = NULL;
446 int foundpkg = 0;
448 char target[512]; /* TODO is this enough space? */
449 char *repo = NULL, *pkgstr = NULL;
451 strncpy(target, i->data, 512);
452 pkgstr = strchr(target, '/');
453 if(pkgstr) {
454 repo = target;
455 *pkgstr = '\0';
456 ++pkgstr;
458 for(j = syncs; j; j = alpm_list_next(j)) {
459 db = alpm_list_getdata(j);
460 if(strcmp(repo, alpm_db_get_name(db)) == 0) {
461 break;
463 db = NULL;
466 if(!db) {
467 pm_fprintf(stderr, PM_LOG_ERROR,
468 _("repository '%s' does not exist\n"), repo);
469 return(1);
472 for(k = alpm_db_get_pkgcache(db); k; k = alpm_list_next(k)) {
473 pmpkg_t *pkg = alpm_list_getdata(k);
475 if(strcmp(alpm_pkg_get_name(pkg), pkgstr) == 0) {
476 dump_pkg_sync(pkg, alpm_db_get_name(db), config->op_s_info);
477 foundpkg = 1;
478 break;
482 if(!foundpkg) {
483 pm_fprintf(stderr, PM_LOG_ERROR,
484 _("package '%s' was not found in repository '%s'\n"), pkgstr, repo);
485 ret++;
487 } else {
488 pkgstr = target;
490 for(j = syncs; j; j = alpm_list_next(j)) {
491 pmdb_t *db = alpm_list_getdata(j);
493 for(k = alpm_db_get_pkgcache(db); k; k = alpm_list_next(k)) {
494 pmpkg_t *pkg = alpm_list_getdata(k);
496 if(strcmp(alpm_pkg_get_name(pkg), pkgstr) == 0) {
497 dump_pkg_sync(pkg, alpm_db_get_name(db), config->op_s_info);
498 foundpkg = 1;
499 break;
503 if(!foundpkg) {
504 pm_fprintf(stderr, PM_LOG_ERROR,
505 _("package '%s' was not found\n"), pkgstr);
506 ret++;
510 } else {
511 for(i = syncs; i; i = alpm_list_next(i)) {
512 pmdb_t *db = alpm_list_getdata(i);
514 for(j = alpm_db_get_pkgcache(db); j; j = alpm_list_next(j)) {
515 dump_pkg_sync(alpm_list_getdata(j), alpm_db_get_name(db), config->op_s_info);
520 return(ret);
523 static int sync_list(alpm_list_t *syncs, alpm_list_t *targets)
525 alpm_list_t *i, *j, *ls = NULL;
526 pmdb_t *db_local = alpm_option_get_localdb();
528 if(targets) {
529 for(i = targets; i; i = alpm_list_next(i)) {
530 const char *repo = alpm_list_getdata(i);
531 pmdb_t *db = NULL;
533 for(j = syncs; j; j = alpm_list_next(j)) {
534 pmdb_t *d = alpm_list_getdata(j);
536 if(strcmp(repo, alpm_db_get_name(d)) == 0) {
537 db = d;
538 break;
542 if(db == NULL) {
543 pm_fprintf(stderr, PM_LOG_ERROR,
544 _("repository \"%s\" was not found.\n"),repo);
545 alpm_list_free(ls);
546 return(1);
549 ls = alpm_list_add(ls, db);
551 } else {
552 ls = syncs;
555 for(i = ls; i; i = alpm_list_next(i)) {
556 pmdb_t *db = alpm_list_getdata(i);
558 for(j = alpm_db_get_pkgcache(db); j; j = alpm_list_next(j)) {
559 pmpkg_t *pkg = alpm_list_getdata(j);
561 if (!config->quiet) {
562 printf("%s %s %s", alpm_db_get_name(db), alpm_pkg_get_name(pkg),
563 alpm_pkg_get_version(pkg));
564 print_installed(db_local, pkg);
565 printf("\n");
566 } else {
567 printf("%s\n", alpm_pkg_get_name(pkg));
572 if(targets) {
573 alpm_list_free(ls);
576 return(0);
579 static alpm_list_t *syncfirst(void) {
580 alpm_list_t *i, *res = NULL;
582 for(i = config->syncfirst; i; i = alpm_list_next(i)) {
583 char *pkgname = alpm_list_getdata(i);
584 pmpkg_t *pkg = alpm_db_get_pkg(alpm_option_get_localdb(), pkgname);
585 if(pkg == NULL) {
586 continue;
589 if(alpm_sync_newversion(pkg, alpm_option_get_syncdbs())) {
590 res = alpm_list_add(res, strdup(pkgname));
594 return(res);
597 static pmdb_t *get_db(const char *dbname)
599 alpm_list_t *i;
600 for(i = alpm_option_get_syncdbs(); i; i = i->next) {
601 pmdb_t *db = i->data;
602 if(strcmp(alpm_db_get_name(db), dbname) == 0) {
603 return(db);
606 return(NULL);
609 static int process_pkg(pmpkg_t *pkg)
611 int ret = alpm_add_pkg(pkg);
613 if(ret == -1) {
614 if(pm_errno == PM_ERR_TRANS_DUP_TARGET
615 || pm_errno == PM_ERR_PKG_IGNORED) {
616 /* just skip duplicate or ignored targets */
617 pm_printf(PM_LOG_WARNING, _("skipping target: %s\n"), alpm_pkg_get_name(pkg));
618 return(0);
619 } else {
620 pm_fprintf(stderr, PM_LOG_ERROR, "'%s': %s\n", alpm_pkg_get_name(pkg),
621 alpm_strerrorlast());
622 return(1);
625 return(0);
628 static int process_group(alpm_list_t *dbs, char *group)
630 int ret = 0;
631 alpm_list_t *i;
632 alpm_list_t *pkgs = alpm_find_grp_pkgs(dbs, group);
633 int count = alpm_list_count(pkgs);
635 if(!count) {
636 pm_fprintf(stderr, PM_LOG_ERROR, _("target not found: %s\n"), group);
637 return(1);
640 printf(_(":: There are %d members in group %s:\n"), count,
641 group);
642 select_display(pkgs);
643 char *array = malloc(count);
644 multiselect_question(array, count);
645 int n = 0;
646 for(i = pkgs; i; i = alpm_list_next(i)) {
647 if(array[n++] == 0)
648 continue;
649 pmpkg_t *pkg = alpm_list_getdata(i);
651 if(process_pkg(pkg) == 1) {
652 ret = 1;
653 goto cleanup;
656 cleanup:
657 alpm_list_free(pkgs);
658 free(array);
659 return(ret);
662 static int process_targname(alpm_list_t *dblist, char *targname)
664 pmpkg_t *pkg = alpm_find_dbs_satisfier(dblist, targname);
666 /* #FS23342 - skip ignored packages when user says no */
667 if(pm_errno == PM_ERR_PKG_IGNORED) {
668 pm_printf(PM_LOG_WARNING, _("skipping target: %s\n"), targname);
669 pm_errno = 0;
670 return(0);
673 if(pkg) {
674 return(process_pkg(pkg));
676 /* fallback on group */
677 return(process_group(dblist, targname));
680 static int process_target(char *target)
682 /* process targets */
683 char *targstring = strdup(target);
684 char *targname = strchr(targstring, '/');
685 char *dbname = NULL;
686 int ret = 0;
687 alpm_list_t *dblist = NULL;
689 if(targname) {
690 pmdb_t *db = NULL;
692 *targname = '\0';
693 targname++;
694 dbname = targstring;
695 db = get_db(dbname);
696 if(!db) {
697 pm_fprintf(stderr, PM_LOG_ERROR, _("database not found: %s\n"),
698 dbname);
699 ret = 1;
700 goto cleanup;
702 dblist = alpm_list_add(dblist, db);
703 ret = process_targname(dblist, targname);
704 alpm_list_free(dblist);
705 } else {
706 targname = targstring;
707 dblist = alpm_option_get_syncdbs();
708 ret = process_targname(dblist, targname);
710 cleanup:
711 free(targstring);
712 return(ret);
715 static int sync_trans(alpm_list_t *targets)
717 int retval = 0;
718 alpm_list_t *data = NULL;
719 alpm_list_t *packages = NULL;
720 alpm_list_t *i;
722 /* Step 1: create a new transaction... */
723 if(trans_init(config->flags) == -1) {
724 return(1);
727 /* process targets */
728 for(i = targets; i; i = alpm_list_next(i)) {
729 char *targ = alpm_list_getdata(i);
730 if(process_target(targ) == 1) {
731 retval = 1;
732 goto cleanup;
736 if(config->op_s_upgrade) {
737 printf(_(":: Starting full system upgrade...\n"));
738 alpm_logaction("starting full system upgrade\n");
739 if(alpm_sync_sysupgrade(config->op_s_upgrade >= 2) == -1) {
740 pm_fprintf(stderr, PM_LOG_ERROR, "%s\n", alpm_strerrorlast());
741 retval = 1;
742 goto cleanup;
746 /* Step 2: "compute" the transaction based on targets and flags */
747 if(alpm_trans_prepare(&data) == -1) {
748 pm_fprintf(stderr, PM_LOG_ERROR, _("failed to prepare transaction (%s)\n"),
749 alpm_strerrorlast());
750 switch(pm_errno) {
751 alpm_list_t *i;
752 case PM_ERR_PKG_INVALID_ARCH:
753 for(i = data; i; i = alpm_list_next(i)) {
754 char *pkg = alpm_list_getdata(i);
755 printf(_(":: package %s does not have a valid architecture\n"), pkg);
757 break;
758 case PM_ERR_UNSATISFIED_DEPS:
759 for(i = data; i; i = alpm_list_next(i)) {
760 pmdepmissing_t *miss = alpm_list_getdata(i);
761 pmdepend_t *dep = alpm_miss_get_dep(miss);
762 char *depstring = alpm_dep_compute_string(dep);
763 printf(_(":: %s: requires %s\n"), alpm_miss_get_target(miss),
764 depstring);
765 free(depstring);
767 break;
768 case PM_ERR_CONFLICTING_DEPS:
769 for(i = data; i; i = alpm_list_next(i)) {
770 pmconflict_t *conflict = alpm_list_getdata(i);
771 const char *package1 = alpm_conflict_get_package1(conflict);
772 const char *package2 = alpm_conflict_get_package2(conflict);
773 const char *reason = alpm_conflict_get_reason(conflict);
774 /* only print reason if it contains new information */
775 if(strcmp(package1, reason) == 0 || strcmp(package2, reason) == 0) {
776 printf(_(":: %s and %s are in conflict\n"), package1, package2);
777 } else {
778 printf(_(":: %s and %s are in conflict (%s)\n"), package1, package2, reason);
781 break;
782 default:
783 break;
785 retval = 1;
786 goto cleanup;
789 packages = alpm_trans_get_add();
790 if(packages == NULL) {
791 /* nothing to do: just exit without complaining */
792 printf(_(" there is nothing to do\n"));
793 goto cleanup;
796 /* Step 3: actually perform the operation */
797 if(config->print) {
798 print_packages(packages);
799 goto cleanup;
802 display_targets(alpm_trans_get_remove(), 0);
803 display_targets(alpm_trans_get_add(), 1);
804 printf("\n");
806 int confirm;
807 if(config->op_s_downloadonly) {
808 confirm = yesno(_("Proceed with download?"));
809 } else {
810 confirm = yesno(_("Proceed with installation?"));
812 if(!confirm) {
813 goto cleanup;
816 if(alpm_trans_commit(&data) == -1) {
817 pm_fprintf(stderr, PM_LOG_ERROR, _("failed to commit transaction (%s)\n"),
818 alpm_strerrorlast());
819 switch(pm_errno) {
820 alpm_list_t *i;
821 case PM_ERR_FILE_CONFLICTS:
822 for(i = data; i; i = alpm_list_next(i)) {
823 pmfileconflict_t *conflict = alpm_list_getdata(i);
824 switch(alpm_fileconflict_get_type(conflict)) {
825 case PM_FILECONFLICT_TARGET:
826 printf(_("%s exists in both '%s' and '%s'\n"),
827 alpm_fileconflict_get_file(conflict),
828 alpm_fileconflict_get_target(conflict),
829 alpm_fileconflict_get_ctarget(conflict));
830 break;
831 case PM_FILECONFLICT_FILESYSTEM:
832 printf(_("%s: %s exists in filesystem\n"),
833 alpm_fileconflict_get_target(conflict),
834 alpm_fileconflict_get_file(conflict));
835 break;
838 break;
839 case PM_ERR_PKG_INVALID:
840 case PM_ERR_DLT_INVALID:
841 for(i = data; i; i = alpm_list_next(i)) {
842 char *filename = alpm_list_getdata(i);
843 printf(_("%s is invalid or corrupted\n"), filename);
845 break;
846 default:
847 break;
849 /* TODO: stderr? */
850 printf(_("Errors occurred, no packages were upgraded.\n"));
851 retval = 1;
852 goto cleanup;
855 /* Step 4: release transaction resources */
856 cleanup:
857 if(data) {
858 FREELIST(data);
860 if(trans_release() == -1) {
861 retval = 1;
864 return(retval);
867 int pacman_sync(alpm_list_t *targets)
869 alpm_list_t *sync_dbs = NULL;
871 /* clean the cache */
872 if(config->op_s_clean) {
873 int ret = 0;
875 if(trans_init(0) == -1) {
876 return(1);
879 ret += sync_cleancache(config->op_s_clean);
880 printf("\n");
881 ret += sync_cleandb_all();
883 if(trans_release() == -1) {
884 ret++;
887 return(ret);
890 /* ensure we have at least one valid sync db set up */
891 sync_dbs = alpm_option_get_syncdbs();
892 if(sync_dbs == NULL || alpm_list_count(sync_dbs) == 0) {
893 pm_printf(PM_LOG_ERROR, _("no usable package repositories configured.\n"));
894 return(1);
897 if(config->op_s_sync) {
898 /* grab a fresh package list */
899 printf(_(":: Synchronizing package databases...\n"));
900 alpm_logaction("synchronizing package lists\n");
901 if(!sync_synctree(config->op_s_sync, sync_dbs)) {
902 return(1);
906 /* search for a package */
907 if(config->op_s_search) {
908 return(sync_search(sync_dbs, targets));
911 /* look for groups */
912 if(config->group) {
913 return(sync_group(config->group, sync_dbs, targets));
916 /* get package info */
917 if(config->op_s_info) {
918 return(sync_info(sync_dbs, targets));
921 /* get a listing of files in sync DBs */
922 if(config->op_q_list) {
923 return(sync_list(sync_dbs, targets));
926 if(targets == NULL) {
927 if(config->op_s_upgrade) {
928 /* proceed */
929 } else if(config->op_s_sync) {
930 return(0);
931 } else {
932 /* don't proceed here unless we have an operation that doesn't require a
933 * target list */
934 pm_printf(PM_LOG_ERROR, _("no targets specified (use -h for help)\n"));
935 return(1);
939 alpm_list_t *targs = alpm_list_strdup(targets);
940 if(!(config->flags & PM_TRANS_FLAG_DOWNLOADONLY) && !config->print) {
941 /* check for newer versions of packages to be upgraded first */
942 alpm_list_t *packages = syncfirst();
943 if(packages) {
944 /* Do not ask user if all the -S targets are SyncFirst packages, see FS#15810 */
945 alpm_list_t *tmp = NULL;
946 if(config->op_s_upgrade || (tmp = alpm_list_diff(targets, packages, (alpm_list_fn_cmp)strcmp))) {
947 alpm_list_free(tmp);
948 printf(_(":: The following packages should be upgraded first :\n"));
949 list_display(" ", packages);
950 if(yesno(_(":: Do you want to cancel the current operation\n"
951 ":: and upgrade these packages now?"))) {
952 FREELIST(targs);
953 targs = packages;
954 config->flags = 0;
955 config->op_s_upgrade = 0;
956 } else {
957 FREELIST(packages);
959 printf("\n");
960 } else {
961 pm_printf(PM_LOG_DEBUG, "skipping SyncFirst dialog\n");
962 FREELIST(packages);
967 int ret = sync_trans(targs);
968 FREELIST(targs);
970 return(ret);
973 /* vim: set ts=2 sw=2 noet: */