Read pkgcache into hash
[pacman-ng.git] / src / pacman / sync.c
blob7af1667aa3b424307dbd49ad3eb3f6464f745bf2
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_list(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_list(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_list(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_list(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_list(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 if(pkg) {
667 return(process_pkg(pkg));
669 /* fallback on group */
670 return(process_group(dblist, targname));
673 static int process_target(char *target)
675 /* process targets */
676 char *targstring = strdup(target);
677 char *targname = strchr(targstring, '/');
678 char *dbname = NULL;
679 int ret = 0;
680 alpm_list_t *dblist = NULL;
682 if(targname) {
683 pmdb_t *db = NULL;
685 *targname = '\0';
686 targname++;
687 dbname = targstring;
688 db = get_db(dbname);
689 if(!db) {
690 pm_fprintf(stderr, PM_LOG_ERROR, _("database not found: %s\n"),
691 dbname);
692 ret = 1;
693 goto cleanup;
695 dblist = alpm_list_add(dblist, db);
696 ret = process_targname(dblist, targname);
697 alpm_list_free(dblist);
698 } else {
699 targname = targstring;
700 dblist = alpm_option_get_syncdbs();
701 ret = process_targname(dblist, targname);
703 cleanup:
704 free(targstring);
705 return(ret);
708 static int sync_trans(alpm_list_t *targets)
710 int retval = 0;
711 alpm_list_t *data = NULL;
712 alpm_list_t *packages = NULL;
713 alpm_list_t *i;
715 /* Step 1: create a new transaction... */
716 if(trans_init(config->flags) == -1) {
717 return(1);
720 /* process targets */
721 for(i = targets; i; i = alpm_list_next(i)) {
722 char *targ = alpm_list_getdata(i);
723 if(process_target(targ) == 1) {
724 retval = 1;
725 goto cleanup;
729 if(config->op_s_upgrade) {
730 printf(_(":: Starting full system upgrade...\n"));
731 alpm_logaction("starting full system upgrade\n");
732 if(alpm_sync_sysupgrade(config->op_s_upgrade >= 2) == -1) {
733 pm_fprintf(stderr, PM_LOG_ERROR, "%s\n", alpm_strerrorlast());
734 retval = 1;
735 goto cleanup;
739 /* Step 2: "compute" the transaction based on targets and flags */
740 if(alpm_trans_prepare(&data) == -1) {
741 pm_fprintf(stderr, PM_LOG_ERROR, _("failed to prepare transaction (%s)\n"),
742 alpm_strerrorlast());
743 switch(pm_errno) {
744 alpm_list_t *i;
745 case PM_ERR_PKG_INVALID_ARCH:
746 for(i = data; i; i = alpm_list_next(i)) {
747 char *pkg = alpm_list_getdata(i);
748 printf(_(":: package %s does not have a valid architecture\n"), pkg);
750 break;
751 case PM_ERR_UNSATISFIED_DEPS:
752 for(i = data; i; i = alpm_list_next(i)) {
753 pmdepmissing_t *miss = alpm_list_getdata(i);
754 pmdepend_t *dep = alpm_miss_get_dep(miss);
755 char *depstring = alpm_dep_compute_string(dep);
756 printf(_(":: %s: requires %s\n"), alpm_miss_get_target(miss),
757 depstring);
758 free(depstring);
760 break;
761 case PM_ERR_CONFLICTING_DEPS:
762 for(i = data; i; i = alpm_list_next(i)) {
763 pmconflict_t *conflict = alpm_list_getdata(i);
764 const char *package1 = alpm_conflict_get_package1(conflict);
765 const char *package2 = alpm_conflict_get_package2(conflict);
766 const char *reason = alpm_conflict_get_reason(conflict);
767 /* only print reason if it contains new information */
768 if(strcmp(package1, reason) == 0 || strcmp(package2, reason) == 0) {
769 printf(_(":: %s and %s are in conflict\n"), package1, package2);
770 } else {
771 printf(_(":: %s and %s are in conflict (%s)\n"), package1, package2, reason);
774 break;
775 default:
776 break;
778 retval = 1;
779 goto cleanup;
782 packages = alpm_trans_get_add();
783 if(packages == NULL) {
784 /* nothing to do: just exit without complaining */
785 printf(_(" there is nothing to do\n"));
786 goto cleanup;
789 /* Step 3: actually perform the operation */
790 if(config->print) {
791 print_packages(packages);
792 goto cleanup;
795 display_targets(alpm_trans_get_remove(), 0);
796 display_targets(alpm_trans_get_add(), 1);
797 printf("\n");
799 int confirm;
800 if(config->op_s_downloadonly) {
801 confirm = yesno(_("Proceed with download?"));
802 } else {
803 confirm = yesno(_("Proceed with installation?"));
805 if(!confirm) {
806 goto cleanup;
809 if(alpm_trans_commit(&data) == -1) {
810 pm_fprintf(stderr, PM_LOG_ERROR, _("failed to commit transaction (%s)\n"),
811 alpm_strerrorlast());
812 switch(pm_errno) {
813 alpm_list_t *i;
814 case PM_ERR_FILE_CONFLICTS:
815 for(i = data; i; i = alpm_list_next(i)) {
816 pmfileconflict_t *conflict = alpm_list_getdata(i);
817 switch(alpm_fileconflict_get_type(conflict)) {
818 case PM_FILECONFLICT_TARGET:
819 printf(_("%s exists in both '%s' and '%s'\n"),
820 alpm_fileconflict_get_file(conflict),
821 alpm_fileconflict_get_target(conflict),
822 alpm_fileconflict_get_ctarget(conflict));
823 break;
824 case PM_FILECONFLICT_FILESYSTEM:
825 printf(_("%s: %s exists in filesystem\n"),
826 alpm_fileconflict_get_target(conflict),
827 alpm_fileconflict_get_file(conflict));
828 break;
831 break;
832 case PM_ERR_PKG_INVALID:
833 case PM_ERR_DLT_INVALID:
834 for(i = data; i; i = alpm_list_next(i)) {
835 char *filename = alpm_list_getdata(i);
836 printf(_("%s is invalid or corrupted\n"), filename);
838 break;
839 default:
840 break;
842 /* TODO: stderr? */
843 printf(_("Errors occurred, no packages were upgraded.\n"));
844 retval = 1;
845 goto cleanup;
848 /* Step 4: release transaction resources */
849 cleanup:
850 if(data) {
851 FREELIST(data);
853 if(trans_release() == -1) {
854 retval = 1;
857 return(retval);
860 int pacman_sync(alpm_list_t *targets)
862 alpm_list_t *sync_dbs = NULL;
864 /* clean the cache */
865 if(config->op_s_clean) {
866 int ret = 0;
868 if(trans_init(0) == -1) {
869 return(1);
872 ret += sync_cleancache(config->op_s_clean);
873 printf("\n");
874 ret += sync_cleandb_all();
876 if(trans_release() == -1) {
877 ret++;
880 return(ret);
883 /* ensure we have at least one valid sync db set up */
884 sync_dbs = alpm_option_get_syncdbs();
885 if(sync_dbs == NULL || alpm_list_count(sync_dbs) == 0) {
886 pm_printf(PM_LOG_ERROR, _("no usable package repositories configured.\n"));
887 return(1);
890 if(config->op_s_sync) {
891 /* grab a fresh package list */
892 printf(_(":: Synchronizing package databases...\n"));
893 alpm_logaction("synchronizing package lists\n");
894 if(!sync_synctree(config->op_s_sync, sync_dbs)) {
895 return(1);
899 /* search for a package */
900 if(config->op_s_search) {
901 return(sync_search(sync_dbs, targets));
904 /* look for groups */
905 if(config->group) {
906 return(sync_group(config->group, sync_dbs, targets));
909 /* get package info */
910 if(config->op_s_info) {
911 return(sync_info(sync_dbs, targets));
914 /* get a listing of files in sync DBs */
915 if(config->op_q_list) {
916 return(sync_list(sync_dbs, targets));
919 if(targets == NULL) {
920 if(config->op_s_upgrade) {
921 /* proceed */
922 } else if(config->op_s_sync) {
923 return(0);
924 } else {
925 /* don't proceed here unless we have an operation that doesn't require a
926 * target list */
927 pm_printf(PM_LOG_ERROR, _("no targets specified (use -h for help)\n"));
928 return(1);
932 alpm_list_t *targs = alpm_list_strdup(targets);
933 if(!(config->flags & PM_TRANS_FLAG_DOWNLOADONLY) && !config->print) {
934 /* check for newer versions of packages to be upgraded first */
935 alpm_list_t *packages = syncfirst();
936 if(packages) {
937 /* Do not ask user if all the -S targets are SyncFirst packages, see FS#15810 */
938 alpm_list_t *tmp = NULL;
939 if(config->op_s_upgrade || (tmp = alpm_list_diff(targets, packages, (alpm_list_fn_cmp)strcmp))) {
940 alpm_list_free(tmp);
941 printf(_(":: The following packages should be upgraded first :\n"));
942 list_display(" ", packages);
943 if(yesno(_(":: Do you want to cancel the current operation\n"
944 ":: and upgrade these packages now?"))) {
945 FREELIST(targs);
946 targs = packages;
947 config->flags = 0;
948 config->op_s_upgrade = 0;
949 } else {
950 FREELIST(packages);
952 printf("\n");
953 } else {
954 pm_printf(PM_LOG_DEBUG, "skipping SyncFirst dialog\n");
955 FREELIST(packages);
960 int ret = sync_trans(targs);
961 FREELIST(targs);
963 return(ret);
966 /* vim: set ts=2 sw=2 noet: */