Various tweaks to support building with excessive GCC warning flags
[pacman-ng.git] / src / pacman / sync.c
blob9c17f45f213b7514b61353b85b163ad84525f69b
1 /*
2 * sync.c
4 * Copyright (c) 2006-2012 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 <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <limits.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <dirent.h>
28 #include <sys/stat.h>
29 #include <fnmatch.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 static int unlink_verbose(const char *pathname, int ignore_missing)
42 int ret = unlink(pathname);
43 if(ret) {
44 if(ignore_missing && errno == ENOENT) {
45 ret = 0;
46 } else {
47 pm_printf(ALPM_LOG_ERROR, _("could not remove %s: %s\n"),
48 pathname, strerror(errno));
51 return ret;
54 /* if keep_used != 0, then the db files which match an used syncdb
55 * will be kept */
56 static int sync_cleandb(const char *dbpath, int keep_used)
58 DIR *dir;
59 struct dirent *ent;
60 alpm_list_t *syncdbs;
61 int ret = 0;
63 dir = opendir(dbpath);
64 if(dir == NULL) {
65 pm_printf(ALPM_LOG_ERROR, _("could not access database directory\n"));
66 return 1;
69 syncdbs = alpm_get_syncdbs(config->handle);
71 rewinddir(dir);
72 /* step through the directory one file at a time */
73 while((ent = readdir(dir)) != NULL) {
74 char path[PATH_MAX];
75 struct stat buf;
76 int found = 0;
77 const char *dname = ent->d_name;
78 char *dbname;
79 size_t len;
81 if(strcmp(dname, ".") == 0 || strcmp(dname, "..") == 0) {
82 continue;
84 /* skip the local and sync directories */
85 if(strcmp(dname, "sync") == 0 || strcmp(dname, "local") == 0) {
86 continue;
88 /* skip the db.lck file */
89 if(strcmp(dname, "db.lck") == 0) {
90 continue;
93 /* build the full path */
94 snprintf(path, PATH_MAX, "%s%s", dbpath, dname);
96 /* remove all non-skipped directories and non-database files */
97 stat(path, &buf);
98 if(S_ISDIR(buf.st_mode)) {
99 if(rmrf(path)) {
100 pm_printf(ALPM_LOG_ERROR, _("could not remove %s: %s\n"),
101 path, strerror(errno));
103 continue;
106 len = strlen(dname);
107 if(len > 3 && strcmp(dname + len - 3, ".db") == 0) {
108 dbname = strndup(dname, len - 3);
109 } else if(len > 7 && strcmp(dname + len - 7, ".db.sig") == 0) {
110 dbname = strndup(dname, len - 7);
111 } else {
112 ret += unlink_verbose(path, 0);
113 continue;
116 if(keep_used) {
117 alpm_list_t *i;
118 for(i = syncdbs; i && !found; i = alpm_list_next(i)) {
119 alpm_db_t *db = i->data;
120 found = !strcmp(dbname, alpm_db_get_name(db));
124 /* We have a database that doesn't match any syncdb. */
125 if(!found) {
126 /* ENOENT check is because the signature and database could come in any
127 * order in our readdir() call, so either file may already be gone. */
128 snprintf(path, PATH_MAX, "%s%s.db", dbpath, dbname);
129 ret += unlink_verbose(path, 1);
130 /* unlink a signature file if present too */
131 snprintf(path, PATH_MAX, "%s%s.db.sig", dbpath, dbname);
132 ret += unlink_verbose(path, 1);
134 free(dbname);
136 closedir(dir);
137 return ret;
140 static int sync_cleandb_all(void)
142 const char *dbpath;
143 char *newdbpath;
144 int ret = 0;
146 dbpath = alpm_option_get_dbpath(config->handle);
147 printf(_("Database directory: %s\n"), dbpath);
148 if(!yesno(_("Do you want to remove unused repositories?"))) {
149 return 0;
151 printf(_("removing unused sync repositories...\n"));
152 /* The sync dbs were previously put in dbpath/ but are now in dbpath/sync/.
153 * We will clean everything in dbpath/ except local/, sync/ and db.lck, and
154 * only the unused sync dbs in dbpath/sync/ */
155 ret += sync_cleandb(dbpath, 0);
157 if(asprintf(&newdbpath, "%s%s", dbpath, "sync/") < 0) {
158 ret += 1;
159 return ret;
161 ret += sync_cleandb(newdbpath, 1);
162 free(newdbpath);
164 return ret;
167 static int sync_cleancache(int level)
169 alpm_list_t *i;
170 alpm_list_t *sync_dbs = alpm_get_syncdbs(config->handle);
171 alpm_db_t *db_local = alpm_get_localdb(config->handle);
172 alpm_list_t *cachedirs = alpm_option_get_cachedirs(config->handle);
173 int ret = 0;
175 for(i = cachedirs; i; i = alpm_list_next(i)) {
176 printf(_("Cache directory: %s\n"), (const char *)i->data);
179 if(!config->cleanmethod) {
180 /* default to KeepInstalled if user did not specify */
181 config->cleanmethod = PM_CLEAN_KEEPINST;
184 if(level == 1) {
185 printf(_("Packages to keep:\n"));
186 if(config->cleanmethod & PM_CLEAN_KEEPINST) {
187 printf(_(" All locally installed packages\n"));
189 if(config->cleanmethod & PM_CLEAN_KEEPCUR) {
190 printf(_(" All current sync database packages\n"));
192 if(!yesno(_("Do you want to remove all other packages from cache?"))) {
193 return 0;
195 printf(_("removing old packages from cache...\n"));
196 } else {
197 if(!noyes(_("Do you want to remove ALL files from cache?"))) {
198 return 0;
200 printf(_("removing all files from cache...\n"));
203 for(i = cachedirs; i; i = alpm_list_next(i)) {
204 const char *cachedir = i->data;
205 DIR *dir = opendir(cachedir);
206 struct dirent *ent;
208 if(dir == NULL) {
209 pm_printf(ALPM_LOG_ERROR,
210 _("could not access cache directory %s\n"), cachedir);
211 ret++;
212 continue;
215 rewinddir(dir);
216 /* step through the directory one file at a time */
217 while((ent = readdir(dir)) != NULL) {
218 char path[PATH_MAX];
219 int delete = 1;
220 alpm_pkg_t *localpkg = NULL, *pkg = NULL;
221 const char *local_name, *local_version;
223 if(strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
224 continue;
227 /* skip signature files - they are removed with their package file */
228 if(fnmatch("*.sig", ent->d_name, 0) == 0) {
229 continue;
232 /* skip package database within the cache directory */
233 if(fnmatch("*.db*", ent->d_name, 0) == 0) {
234 continue;
237 /* skip source packages within the cache directory */
238 if(fnmatch("*.src.tar*", ent->d_name, 0) == 0) {
239 continue;
242 /* build the full filepath */
243 snprintf(path, PATH_MAX, "%s%s", cachedir, ent->d_name);
245 /* short circuit for removing all files from cache */
246 if(level > 1) {
247 ret += unlink_verbose(path, 0);
248 continue;
251 /* attempt to load the file as a package. if we cannot load the file,
252 * simply skip it and move on. we don't need a full load of the package,
253 * just the metadata. */
254 if(alpm_pkg_load(config->handle, path, 0, 0, &localpkg) != 0) {
255 pm_printf(ALPM_LOG_DEBUG, "skipping %s, could not load as package\n",
256 path);
257 continue;
259 local_name = alpm_pkg_get_name(localpkg);
260 local_version = alpm_pkg_get_version(localpkg);
262 if(config->cleanmethod & PM_CLEAN_KEEPINST) {
263 /* check if this package is in the local DB */
264 pkg = alpm_db_get_pkg(db_local, local_name);
265 if(pkg != NULL && alpm_pkg_vercmp(local_version,
266 alpm_pkg_get_version(pkg)) == 0) {
267 /* package was found in local DB and version matches, keep it */
268 pm_printf(ALPM_LOG_DEBUG, "package %s-%s found in local db\n",
269 local_name, local_version);
270 delete = 0;
273 if(config->cleanmethod & PM_CLEAN_KEEPCUR) {
274 alpm_list_t *j;
275 /* check if this package is in a sync DB */
276 for(j = sync_dbs; j && delete; j = alpm_list_next(j)) {
277 alpm_db_t *db = j->data;
278 pkg = alpm_db_get_pkg(db, local_name);
279 if(pkg != NULL && alpm_pkg_vercmp(local_version,
280 alpm_pkg_get_version(pkg)) == 0) {
281 /* package was found in a sync DB and version matches, keep it */
282 pm_printf(ALPM_LOG_DEBUG, "package %s-%s found in sync db\n",
283 local_name, local_version);
284 delete = 0;
288 /* free the local file package */
289 alpm_pkg_free(localpkg);
291 if(delete) {
292 size_t pathlen = strlen(path);
293 ret += unlink_verbose(path, 0);
294 /* unlink a signature file if present too */
295 if(PATH_MAX - 5 >= pathlen) {
296 strcpy(path + pathlen, ".sig");
297 ret += unlink_verbose(path, 1);
301 closedir(dir);
304 return ret;
307 static int sync_synctree(int level, alpm_list_t *syncs)
309 alpm_list_t *i;
310 unsigned int success = 0;
312 for(i = syncs; i; i = alpm_list_next(i)) {
313 alpm_db_t *db = i->data;
315 int ret = alpm_db_update((level < 2 ? 0 : 1), db);
316 if(ret < 0) {
317 pm_printf(ALPM_LOG_ERROR, _("failed to update %s (%s)\n"),
318 alpm_db_get_name(db), alpm_strerror(alpm_errno(config->handle)));
319 } else if(ret == 1) {
320 printf(_(" %s is up to date\n"), alpm_db_get_name(db));
321 success++;
322 } else {
323 success++;
327 /* We should always succeed if at least one DB was upgraded - we may possibly
328 * fail later with unresolved deps, but that should be rare, and would be
329 * expected
331 if(!success) {
332 pm_printf(ALPM_LOG_ERROR, _("failed to synchronize any databases\n"));
333 trans_init_error();
335 return (success > 0);
338 static void print_installed(alpm_db_t *db_local, alpm_pkg_t *pkg)
340 const char *pkgname = alpm_pkg_get_name(pkg);
341 const char *pkgver = alpm_pkg_get_version(pkg);
342 alpm_pkg_t *lpkg = alpm_db_get_pkg(db_local, pkgname);
343 if(lpkg) {
344 const char *lpkgver = alpm_pkg_get_version(lpkg);
345 if(strcmp(lpkgver,pkgver) == 0) {
346 printf(" [%s]", _("installed"));
347 } else {
348 printf(" [%s: %s]", _("installed"), lpkgver);
353 /* search the sync dbs for a matching package */
354 static int sync_search(alpm_list_t *syncs, alpm_list_t *targets)
356 alpm_list_t *i, *j, *ret;
357 int freelist;
358 int found = 0;
359 alpm_db_t *db_local = alpm_get_localdb(config->handle);
361 for(i = syncs; i; i = alpm_list_next(i)) {
362 alpm_db_t *db = i->data;
363 unsigned short cols;
364 /* if we have a targets list, search for packages matching it */
365 if(targets) {
366 ret = alpm_db_search(db, targets);
367 freelist = 1;
368 } else {
369 ret = alpm_db_get_pkgcache(db);
370 freelist = 0;
372 if(ret == NULL) {
373 continue;
374 } else {
375 found = 1;
377 cols = getcols(fileno(stdout));
378 for(j = ret; j; j = alpm_list_next(j)) {
379 alpm_list_t *grp;
380 alpm_pkg_t *pkg = j->data;
382 if(!config->quiet) {
383 printf("%s/%s %s", alpm_db_get_name(db), alpm_pkg_get_name(pkg),
384 alpm_pkg_get_version(pkg));
385 } else {
386 fputs(alpm_pkg_get_name(pkg), stdout);
389 if(!config->quiet) {
390 if((grp = alpm_pkg_get_groups(pkg)) != NULL) {
391 alpm_list_t *k;
392 fputs(" (", stdout);
393 for(k = grp; k; k = alpm_list_next(k)) {
394 const char *group = k->data;
395 fputs(group, stdout);
396 if(alpm_list_next(k)) {
397 /* only print a spacer if there are more groups */
398 putchar(' ');
401 putchar(')');
404 print_installed(db_local, pkg);
406 /* we need a newline and initial indent first */
407 fputs("\n ", stdout);
408 indentprint(alpm_pkg_get_desc(pkg), 4, cols);
410 fputc('\n', stdout);
412 /* we only want to free if the list was a search list */
413 if(freelist) {
414 alpm_list_free(ret);
418 return !found;
421 static int sync_group(int level, alpm_list_t *syncs, alpm_list_t *targets)
423 alpm_list_t *i, *j, *k, *s = NULL;
425 if(targets) {
426 for(i = targets; i; i = alpm_list_next(i)) {
427 const char *grpname = i->data;
428 for(j = syncs; j; j = alpm_list_next(j)) {
429 alpm_db_t *db = j->data;
430 alpm_group_t *grp = alpm_db_get_group(db, grpname);
432 if(grp) {
433 /* get names of packages in group */
434 for(k = grp->packages; k; k = alpm_list_next(k)) {
435 if(!config->quiet) {
436 printf("%s %s\n", grpname,
437 alpm_pkg_get_name(k->data));
438 } else {
439 printf("%s\n", alpm_pkg_get_name(k->data));
445 } else {
446 for(i = syncs; i; i = alpm_list_next(i)) {
447 alpm_db_t *db = i->data;
449 for(j = alpm_db_get_groupcache(db); j; j = alpm_list_next(j)) {
450 alpm_group_t *grp = j->data;
452 if(level > 1) {
453 for(k = grp->packages; k; k = alpm_list_next(k)) {
454 printf("%s %s\n", grp->name,
455 alpm_pkg_get_name(k->data));
457 } else {
458 /* print grp names only, no package names */
459 if(!alpm_list_find_str (s, grp->name)) {
460 s = alpm_list_add (s, grp->name);
461 printf("%s\n", grp->name);
466 alpm_list_free(s);
469 return 0;
472 static int sync_info(alpm_list_t *syncs, alpm_list_t *targets)
474 alpm_list_t *i, *j, *k;
475 int ret = 0;
477 if(targets) {
478 for(i = targets; i; i = alpm_list_next(i)) {
479 const char *target = i->data;
480 char *name = strdup(target);
481 char *repo, *pkgstr;
482 int foundpkg = 0, founddb = 0;
484 pkgstr = strchr(name, '/');
485 if(pkgstr) {
486 repo = name;
487 *pkgstr = '\0';
488 ++pkgstr;
489 } else {
490 repo = NULL;
491 pkgstr = name;
494 for(j = syncs; j; j = alpm_list_next(j)) {
495 alpm_db_t *db = j->data;
496 if(repo && strcmp(repo, alpm_db_get_name(db)) != 0) {
497 continue;
499 founddb = 1;
501 for(k = alpm_db_get_pkgcache(db); k; k = alpm_list_next(k)) {
502 alpm_pkg_t *pkg = k->data;
504 if(strcmp(alpm_pkg_get_name(pkg), pkgstr) == 0) {
505 dump_pkg_full(pkg, config->op_s_info > 1);
506 foundpkg = 1;
507 break;
512 if(!founddb) {
513 pm_printf(ALPM_LOG_ERROR,
514 _("repository '%s' does not exist\n"), repo);
515 ret++;
517 if(!foundpkg) {
518 pm_printf(ALPM_LOG_ERROR,
519 _("package '%s' was not found\n"), target);
520 ret++;
522 free(name);
524 } else {
525 for(i = syncs; i; i = alpm_list_next(i)) {
526 alpm_db_t *db = i->data;
528 for(j = alpm_db_get_pkgcache(db); j; j = alpm_list_next(j)) {
529 alpm_pkg_t *pkg = j->data;
530 dump_pkg_full(pkg, config->op_s_info > 1);
535 return ret;
538 static int sync_list(alpm_list_t *syncs, alpm_list_t *targets)
540 alpm_list_t *i, *j, *ls = NULL;
541 alpm_db_t *db_local = alpm_get_localdb(config->handle);
543 if(targets) {
544 for(i = targets; i; i = alpm_list_next(i)) {
545 const char *repo = i->data;
546 alpm_db_t *db = NULL;
548 for(j = syncs; j; j = alpm_list_next(j)) {
549 alpm_db_t *d = j->data;
551 if(strcmp(repo, alpm_db_get_name(d)) == 0) {
552 db = d;
553 break;
557 if(db == NULL) {
558 pm_printf(ALPM_LOG_ERROR,
559 _("repository \"%s\" was not found.\n"),repo);
560 alpm_list_free(ls);
561 return 1;
564 ls = alpm_list_add(ls, db);
566 } else {
567 ls = syncs;
570 for(i = ls; i; i = alpm_list_next(i)) {
571 alpm_db_t *db = i->data;
573 for(j = alpm_db_get_pkgcache(db); j; j = alpm_list_next(j)) {
574 alpm_pkg_t *pkg = j->data;
576 if(!config->quiet) {
577 printf("%s %s %s", alpm_db_get_name(db), alpm_pkg_get_name(pkg),
578 alpm_pkg_get_version(pkg));
579 print_installed(db_local, pkg);
580 printf("\n");
581 } else {
582 printf("%s\n", alpm_pkg_get_name(pkg));
587 if(targets) {
588 alpm_list_free(ls);
591 return 0;
594 static alpm_list_t *syncfirst(void) {
595 alpm_list_t *i, *res = NULL;
596 alpm_db_t *db_local = alpm_get_localdb(config->handle);
597 alpm_list_t *syncdbs = alpm_get_syncdbs(config->handle);
599 for(i = config->syncfirst; i; i = alpm_list_next(i)) {
600 const char *pkgname = i->data;
601 alpm_pkg_t *pkg = alpm_db_get_pkg(db_local, pkgname);
602 if(pkg == NULL) {
603 continue;
606 if(alpm_sync_newversion(pkg, syncdbs)) {
607 res = alpm_list_add(res, strdup(pkgname));
611 return res;
614 static alpm_db_t *get_db(const char *dbname)
616 alpm_list_t *i;
617 for(i = alpm_get_syncdbs(config->handle); i; i = i->next) {
618 alpm_db_t *db = i->data;
619 if(strcmp(alpm_db_get_name(db), dbname) == 0) {
620 return db;
623 return NULL;
626 static int process_pkg(alpm_pkg_t *pkg)
628 int ret = alpm_add_pkg(config->handle, pkg);
630 if(ret == -1) {
631 alpm_errno_t err = alpm_errno(config->handle);
632 if(err == ALPM_ERR_TRANS_DUP_TARGET
633 || err == ALPM_ERR_PKG_IGNORED) {
634 /* just skip duplicate or ignored targets */
635 pm_printf(ALPM_LOG_WARNING, _("skipping target: %s\n"), alpm_pkg_get_name(pkg));
636 return 0;
637 } else {
638 pm_printf(ALPM_LOG_ERROR, "'%s': %s\n", alpm_pkg_get_name(pkg),
639 alpm_strerror(err));
640 return 1;
643 config->explicit_adds = alpm_list_add(config->explicit_adds, pkg);
644 return 0;
647 static int process_group(alpm_list_t *dbs, const char *group, int error)
649 int ret = 0;
650 alpm_list_t *i;
651 alpm_list_t *pkgs = alpm_find_group_pkgs(dbs, group);
652 int count = alpm_list_count(pkgs);
654 if(!count) {
655 pm_printf(ALPM_LOG_ERROR, _("target not found: %s\n"), group);
656 return 1;
659 if(error) {
660 /* we already know another target errored. there is no reason to prompt the
661 * user here; we already validated the group name so just move on since we
662 * won't actually be installing anything anyway. */
663 goto cleanup;
666 if(config->print == 0) {
667 printf(_(":: There are %d members in group %s:\n"), count,
668 group);
669 select_display(pkgs);
670 char *array = malloc(count);
671 if(!array) {
672 ret = 1;
673 goto cleanup;
675 if(multiselect_question(array, count)) {
676 ret = 1;
677 free(array);
678 goto cleanup;
680 int n = 0;
681 for(i = pkgs; i; i = alpm_list_next(i)) {
682 if(array[n++] == 0)
683 continue;
684 alpm_pkg_t *pkg = i->data;
686 if(process_pkg(pkg) == 1) {
687 ret = 1;
688 free(array);
689 goto cleanup;
692 free(array);
693 } else {
694 for(i = pkgs; i; i = alpm_list_next(i)) {
695 alpm_pkg_t *pkg = i->data;
697 if(process_pkg(pkg) == 1) {
698 ret = 1;
699 goto cleanup;
704 cleanup:
705 alpm_list_free(pkgs);
706 return ret;
709 static int process_targname(alpm_list_t *dblist, const char *targname,
710 int error)
712 alpm_pkg_t *pkg = alpm_find_dbs_satisfier(config->handle, dblist, targname);
714 /* #FS#23342 - skip ignored packages when user says no */
715 if(alpm_errno(config->handle) == ALPM_ERR_PKG_IGNORED) {
716 pm_printf(ALPM_LOG_WARNING, _("skipping target: %s\n"), targname);
717 return 0;
720 if(pkg) {
721 return process_pkg(pkg);
723 /* fallback on group */
724 return process_group(dblist, targname, error);
727 static int process_target(const char *target, int error)
729 /* process targets */
730 char *targstring = strdup(target);
731 char *targname = strchr(targstring, '/');
732 int ret = 0;
733 alpm_list_t *dblist;
735 if(targname && targname != targstring) {
736 alpm_db_t *db;
737 const char *dbname;
739 *targname = '\0';
740 targname++;
741 dbname = targstring;
742 db = get_db(dbname);
743 if(!db) {
744 pm_printf(ALPM_LOG_ERROR, _("database not found: %s\n"),
745 dbname);
746 ret = 1;
747 goto cleanup;
749 dblist = alpm_list_add(NULL, db);
750 ret = process_targname(dblist, targname, error);
751 alpm_list_free(dblist);
752 } else {
753 targname = targstring;
754 dblist = alpm_get_syncdbs(config->handle);
755 ret = process_targname(dblist, targname, error);
758 cleanup:
759 free(targstring);
760 if(ret && access(target, R_OK) == 0) {
761 pm_printf(ALPM_LOG_WARNING,
762 _("'%s' is a file, did you mean %s instead of %s?\n"),
763 target, "-U/--upgrade", "-S/--sync");
765 return ret;
768 static int sync_trans(alpm_list_t *targets)
770 int retval = 0;
771 alpm_list_t *i;
773 /* Step 1: create a new transaction... */
774 if(trans_init(config->flags, 1) == -1) {
775 return 1;
778 /* process targets */
779 for(i = targets; i; i = alpm_list_next(i)) {
780 const char *targ = i->data;
781 if(process_target(targ, retval) == 1) {
782 retval = 1;
786 if(retval) {
787 trans_release();
788 return retval;
791 if(config->op_s_upgrade) {
792 printf(_(":: Starting full system upgrade...\n"));
793 alpm_logaction(config->handle, "starting full system upgrade\n");
794 if(alpm_sync_sysupgrade(config->handle, config->op_s_upgrade >= 2) == -1) {
795 pm_printf(ALPM_LOG_ERROR, "%s\n", alpm_strerror(alpm_errno(config->handle)));
796 trans_release();
797 return 1;
801 return sync_prepare_execute();
804 int sync_prepare_execute(void)
806 alpm_list_t *i, *packages, *data = NULL;
807 int retval = 0;
809 /* Step 2: "compute" the transaction based on targets and flags */
810 if(alpm_trans_prepare(config->handle, &data) == -1) {
811 alpm_errno_t err = alpm_errno(config->handle);
812 pm_printf(ALPM_LOG_ERROR, _("failed to prepare transaction (%s)\n"),
813 alpm_strerror(err));
814 switch(err) {
815 case ALPM_ERR_PKG_INVALID_ARCH:
816 for(i = data; i; i = alpm_list_next(i)) {
817 const char *pkg = i->data;
818 printf(_(":: package %s does not have a valid architecture\n"), pkg);
820 break;
821 case ALPM_ERR_UNSATISFIED_DEPS:
822 for(i = data; i; i = alpm_list_next(i)) {
823 alpm_depmissing_t *miss = i->data;
824 char *depstring = alpm_dep_compute_string(miss->depend);
825 printf(_(":: %s: requires %s\n"), miss->target, depstring);
826 free(depstring);
828 break;
829 case ALPM_ERR_CONFLICTING_DEPS:
830 for(i = data; i; i = alpm_list_next(i)) {
831 alpm_conflict_t *conflict = i->data;
832 /* only print reason if it contains new information */
833 if(conflict->reason->mod == ALPM_DEP_MOD_ANY) {
834 printf(_(":: %s and %s are in conflict\n"),
835 conflict->package1, conflict->package2);
836 } else {
837 char *reason = alpm_dep_compute_string(conflict->reason);
838 printf(_(":: %s and %s are in conflict (%s)\n"),
839 conflict->package1, conflict->package2, reason);
840 free(reason);
843 break;
844 default:
845 break;
847 retval = 1;
848 goto cleanup;
851 packages = alpm_trans_get_add(config->handle);
852 if(packages == NULL) {
853 /* nothing to do: just exit without complaining */
854 if(!config->print) {
855 printf(_(" there is nothing to do\n"));
857 goto cleanup;
860 /* Step 3: actually perform the operation */
861 if(config->print) {
862 print_packages(packages);
863 goto cleanup;
866 display_targets();
867 printf("\n");
869 int confirm;
870 if(config->op_s_downloadonly) {
871 confirm = yesno(_("Proceed with download?"));
872 } else {
873 confirm = yesno(_("Proceed with installation?"));
875 if(!confirm) {
876 goto cleanup;
879 if(alpm_trans_commit(config->handle, &data) == -1) {
880 alpm_errno_t err = alpm_errno(config->handle);
881 pm_printf(ALPM_LOG_ERROR, _("failed to commit transaction (%s)\n"),
882 alpm_strerror(err));
883 switch(err) {
884 case ALPM_ERR_FILE_CONFLICTS:
885 for(i = data; i; i = alpm_list_next(i)) {
886 alpm_fileconflict_t *conflict = i->data;
887 switch(conflict->type) {
888 case ALPM_FILECONFLICT_TARGET:
889 printf(_("%s exists in both '%s' and '%s'\n"),
890 conflict->file, conflict->target, conflict->ctarget);
891 break;
892 case ALPM_FILECONFLICT_FILESYSTEM:
893 printf(_("%s: %s exists in filesystem\n"),
894 conflict->target, conflict->file);
895 break;
898 break;
899 case ALPM_ERR_PKG_INVALID:
900 case ALPM_ERR_PKG_INVALID_CHECKSUM:
901 case ALPM_ERR_PKG_INVALID_SIG:
902 case ALPM_ERR_DLT_INVALID:
903 for(i = data; i; i = alpm_list_next(i)) {
904 const char *filename = i->data;
905 printf(_("%s is invalid or corrupted\n"), filename);
907 break;
908 default:
909 break;
911 /* TODO: stderr? */
912 printf(_("Errors occurred, no packages were upgraded.\n"));
913 retval = 1;
914 goto cleanup;
917 /* Step 4: release transaction resources */
918 cleanup:
919 if(data) {
920 FREELIST(data);
922 if(trans_release() == -1) {
923 retval = 1;
926 return retval;
929 int pacman_sync(alpm_list_t *targets)
931 alpm_list_t *sync_dbs = NULL;
933 /* clean the cache */
934 if(config->op_s_clean) {
935 int ret = 0;
937 if(trans_init(0, 0) == -1) {
938 return 1;
941 ret += sync_cleancache(config->op_s_clean);
942 printf("\n");
943 ret += sync_cleandb_all();
945 if(trans_release() == -1) {
946 ret++;
949 return ret;
952 if(check_syncdbs(1, 0)) {
953 return 1;
956 sync_dbs = alpm_get_syncdbs(config->handle);
958 if(config->op_s_sync) {
959 /* grab a fresh package list */
960 printf(_(":: Synchronizing package databases...\n"));
961 alpm_logaction(config->handle, "synchronizing package lists\n");
962 if(!sync_synctree(config->op_s_sync, sync_dbs)) {
963 return 1;
967 if(check_syncdbs(1, 1)) {
968 return 1;
971 /* search for a package */
972 if(config->op_s_search) {
973 return sync_search(sync_dbs, targets);
976 /* look for groups */
977 if(config->group) {
978 return sync_group(config->group, sync_dbs, targets);
981 /* get package info */
982 if(config->op_s_info) {
983 return sync_info(sync_dbs, targets);
986 /* get a listing of files in sync DBs */
987 if(config->op_q_list) {
988 return sync_list(sync_dbs, targets);
991 if(targets == NULL) {
992 if(config->op_s_upgrade) {
993 /* proceed */
994 } else if(config->op_s_sync) {
995 return 0;
996 } else {
997 /* don't proceed here unless we have an operation that doesn't require a
998 * target list */
999 pm_printf(ALPM_LOG_ERROR, _("no targets specified (use -h for help)\n"));
1000 return 1;
1004 alpm_list_t *targs = alpm_list_strdup(targets);
1005 if(!config->op_s_downloadonly && !config->print) {
1006 /* check for newer versions of packages to be upgraded first */
1007 alpm_list_t *packages = syncfirst();
1008 if(packages) {
1009 /* Do not ask user if all the -S targets are SyncFirst packages, see FS#15810 */
1010 alpm_list_t *tmp = NULL;
1011 if(config->op_s_upgrade || (tmp = alpm_list_diff(targets, packages, (alpm_list_fn_cmp)strcmp))) {
1012 alpm_list_free(tmp);
1013 printf(_(":: The following packages should be upgraded first :\n"));
1014 list_display(" ", packages, getcols(fileno(stdout)));
1015 if(yesno(_(":: Do you want to cancel the current operation\n"
1016 ":: and upgrade these packages now?"))) {
1017 FREELIST(targs);
1018 targs = packages;
1019 config->flags = 0;
1020 config->op_s_upgrade = 0;
1021 } else {
1022 FREELIST(packages);
1024 printf("\n");
1025 } else {
1026 pm_printf(ALPM_LOG_DEBUG, "skipping SyncFirst dialog\n");
1027 FREELIST(packages);
1032 int ret = sync_trans(targs);
1033 FREELIST(targs);
1035 return ret;
1038 /* vim: set ts=2 sw=2 noet: */