Move important information up in -Si output
[pacman-ng.git] / src / pacman / sync.c
blob532a66725b0697ec278bcedae1edbaa3cd71e031
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 if(!config->cleanmethod) {
176 /* default to KeepInstalled if user did not specify */
177 config->cleanmethod = PM_CLEAN_KEEPINST;
180 if(level == 1) {
181 printf(_("Packages to keep:\n"));
182 if(config->cleanmethod & PM_CLEAN_KEEPINST) {
183 printf(_(" All locally installed packages\n"));
185 if(config->cleanmethod & PM_CLEAN_KEEPCUR) {
186 printf(_(" All current sync database packages\n"));
189 printf("\n");
191 for(i = cachedirs; i; i = alpm_list_next(i)) {
192 const char *cachedir = i->data;
193 DIR *dir;
194 struct dirent *ent;
196 printf(_("Cache directory: %s\n"), (const char *)i->data);
198 if(level == 1) {
199 if(!yesno(_("Do you want to remove all other packages from cache?"))) {
200 printf("\n");
201 continue;
203 printf(_("removing old packages from cache...\n"));
204 } else {
205 if(!noyes(_("Do you want to remove ALL files from cache?"))) {
206 printf("\n");
207 continue;
209 printf(_("removing all files from cache...\n"));
212 dir = opendir(cachedir);
213 if(dir == NULL) {
214 pm_printf(ALPM_LOG_ERROR,
215 _("could not access cache directory %s\n"), cachedir);
216 ret++;
217 continue;
220 rewinddir(dir);
221 /* step through the directory one file at a time */
222 while((ent = readdir(dir)) != NULL) {
223 char path[PATH_MAX];
224 int delete = 1;
225 alpm_pkg_t *localpkg = NULL, *pkg = NULL;
226 const char *local_name, *local_version;
228 if(strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
229 continue;
232 if (level <= 1) {
233 static const char * const glob_skips[] = {
234 /* skip signature files - they are removed with their package file */
235 "*.sig",
236 /* skip package database within the cache directory */
237 "*.db*",
238 /* skip source packages within the cache directory */
239 "*.src.tar.*",
240 /* skip package deltas, we aren't smart enough to clean these yet */
241 "*.delta",
242 /* skip any partial downloads */
243 "*.part"
245 size_t j;
247 for(j = 0; j < sizeof(glob_skips) / sizeof(glob_skips[0]); j++) {
248 if(fnmatch(glob_skips[j], ent->d_name, 0) == 0) {
249 delete = 0;
250 break;
253 if(delete == 0) {
254 continue;
258 /* build the full filepath */
259 snprintf(path, PATH_MAX, "%s%s", cachedir, ent->d_name);
261 /* short circuit for removing all files from cache */
262 if(level > 1) {
263 ret += unlink_verbose(path, 0);
264 continue;
267 /* attempt to load the file as a package. if we cannot load the file,
268 * simply skip it and move on. we don't need a full load of the package,
269 * just the metadata. */
270 if(alpm_pkg_load(config->handle, path, 0, 0, &localpkg) != 0) {
271 pm_printf(ALPM_LOG_DEBUG, "skipping %s, could not load as package\n",
272 path);
273 continue;
275 local_name = alpm_pkg_get_name(localpkg);
276 local_version = alpm_pkg_get_version(localpkg);
278 if(config->cleanmethod & PM_CLEAN_KEEPINST) {
279 /* check if this package is in the local DB */
280 pkg = alpm_db_get_pkg(db_local, local_name);
281 if(pkg != NULL && alpm_pkg_vercmp(local_version,
282 alpm_pkg_get_version(pkg)) == 0) {
283 /* package was found in local DB and version matches, keep it */
284 pm_printf(ALPM_LOG_DEBUG, "package %s-%s found in local db\n",
285 local_name, local_version);
286 delete = 0;
289 if(config->cleanmethod & PM_CLEAN_KEEPCUR) {
290 alpm_list_t *j;
291 /* check if this package is in a sync DB */
292 for(j = sync_dbs; j && delete; j = alpm_list_next(j)) {
293 alpm_db_t *db = j->data;
294 pkg = alpm_db_get_pkg(db, local_name);
295 if(pkg != NULL && alpm_pkg_vercmp(local_version,
296 alpm_pkg_get_version(pkg)) == 0) {
297 /* package was found in a sync DB and version matches, keep it */
298 pm_printf(ALPM_LOG_DEBUG, "package %s-%s found in sync db\n",
299 local_name, local_version);
300 delete = 0;
304 /* free the local file package */
305 alpm_pkg_free(localpkg);
307 if(delete) {
308 size_t pathlen = strlen(path);
309 ret += unlink_verbose(path, 0);
310 /* unlink a signature file if present too */
311 if(PATH_MAX - 5 >= pathlen) {
312 strcpy(path + pathlen, ".sig");
313 ret += unlink_verbose(path, 1);
317 closedir(dir);
318 printf("\n");
321 return ret;
324 static int sync_synctree(int level, alpm_list_t *syncs)
326 alpm_list_t *i;
327 unsigned int success = 0;
329 for(i = syncs; i; i = alpm_list_next(i)) {
330 alpm_db_t *db = i->data;
332 int ret = alpm_db_update((level < 2 ? 0 : 1), db);
333 if(ret < 0) {
334 pm_printf(ALPM_LOG_ERROR, _("failed to update %s (%s)\n"),
335 alpm_db_get_name(db), alpm_strerror(alpm_errno(config->handle)));
336 } else if(ret == 1) {
337 printf(_(" %s is up to date\n"), alpm_db_get_name(db));
338 success++;
339 } else {
340 success++;
344 /* We should always succeed if at least one DB was upgraded - we may possibly
345 * fail later with unresolved deps, but that should be rare, and would be
346 * expected
348 if(!success) {
349 pm_printf(ALPM_LOG_ERROR, _("failed to synchronize any databases\n"));
350 trans_init_error();
352 return (success > 0);
355 static void print_installed(alpm_db_t *db_local, alpm_pkg_t *pkg)
357 const char *pkgname = alpm_pkg_get_name(pkg);
358 const char *pkgver = alpm_pkg_get_version(pkg);
359 alpm_pkg_t *lpkg = alpm_db_get_pkg(db_local, pkgname);
360 if(lpkg) {
361 const char *lpkgver = alpm_pkg_get_version(lpkg);
362 if(strcmp(lpkgver,pkgver) == 0) {
363 printf(" [%s]", _("installed"));
364 } else {
365 printf(" [%s: %s]", _("installed"), lpkgver);
370 /* search the sync dbs for a matching package */
371 static int sync_search(alpm_list_t *syncs, alpm_list_t *targets)
373 alpm_list_t *i, *j, *ret;
374 int freelist;
375 int found = 0;
376 alpm_db_t *db_local = alpm_get_localdb(config->handle);
378 for(i = syncs; i; i = alpm_list_next(i)) {
379 alpm_db_t *db = i->data;
380 unsigned short cols;
381 /* if we have a targets list, search for packages matching it */
382 if(targets) {
383 ret = alpm_db_search(db, targets);
384 freelist = 1;
385 } else {
386 ret = alpm_db_get_pkgcache(db);
387 freelist = 0;
389 if(ret == NULL) {
390 continue;
391 } else {
392 found = 1;
394 cols = getcols(fileno(stdout));
395 for(j = ret; j; j = alpm_list_next(j)) {
396 alpm_list_t *grp;
397 alpm_pkg_t *pkg = j->data;
399 if(!config->quiet) {
400 printf("%s/%s %s", alpm_db_get_name(db), alpm_pkg_get_name(pkg),
401 alpm_pkg_get_version(pkg));
402 } else {
403 fputs(alpm_pkg_get_name(pkg), stdout);
406 if(!config->quiet) {
407 if((grp = alpm_pkg_get_groups(pkg)) != NULL) {
408 alpm_list_t *k;
409 fputs(" (", stdout);
410 for(k = grp; k; k = alpm_list_next(k)) {
411 const char *group = k->data;
412 fputs(group, stdout);
413 if(alpm_list_next(k)) {
414 /* only print a spacer if there are more groups */
415 putchar(' ');
418 putchar(')');
421 print_installed(db_local, pkg);
423 /* we need a newline and initial indent first */
424 fputs("\n ", stdout);
425 indentprint(alpm_pkg_get_desc(pkg), 4, cols);
427 fputc('\n', stdout);
429 /* we only want to free if the list was a search list */
430 if(freelist) {
431 alpm_list_free(ret);
435 return !found;
438 static int sync_group(int level, alpm_list_t *syncs, alpm_list_t *targets)
440 alpm_list_t *i, *j, *k, *s = NULL;
442 if(targets) {
443 for(i = targets; i; i = alpm_list_next(i)) {
444 const char *grpname = i->data;
445 for(j = syncs; j; j = alpm_list_next(j)) {
446 alpm_db_t *db = j->data;
447 alpm_group_t *grp = alpm_db_get_group(db, grpname);
449 if(grp) {
450 /* get names of packages in group */
451 for(k = grp->packages; k; k = alpm_list_next(k)) {
452 if(!config->quiet) {
453 printf("%s %s\n", grpname,
454 alpm_pkg_get_name(k->data));
455 } else {
456 printf("%s\n", alpm_pkg_get_name(k->data));
462 } else {
463 for(i = syncs; i; i = alpm_list_next(i)) {
464 alpm_db_t *db = i->data;
466 for(j = alpm_db_get_groupcache(db); j; j = alpm_list_next(j)) {
467 alpm_group_t *grp = j->data;
469 if(level > 1) {
470 for(k = grp->packages; k; k = alpm_list_next(k)) {
471 printf("%s %s\n", grp->name,
472 alpm_pkg_get_name(k->data));
474 } else {
475 /* print grp names only, no package names */
476 if(!alpm_list_find_str (s, grp->name)) {
477 s = alpm_list_add (s, grp->name);
478 printf("%s\n", grp->name);
483 alpm_list_free(s);
486 return 0;
489 static int sync_info(alpm_list_t *syncs, alpm_list_t *targets)
491 alpm_list_t *i, *j, *k;
492 int ret = 0;
494 if(targets) {
495 for(i = targets; i; i = alpm_list_next(i)) {
496 const char *target = i->data;
497 char *name = strdup(target);
498 char *repo, *pkgstr;
499 int foundpkg = 0, founddb = 0;
501 pkgstr = strchr(name, '/');
502 if(pkgstr) {
503 repo = name;
504 *pkgstr = '\0';
505 ++pkgstr;
506 } else {
507 repo = NULL;
508 pkgstr = name;
511 for(j = syncs; j; j = alpm_list_next(j)) {
512 alpm_db_t *db = j->data;
513 if(repo && strcmp(repo, alpm_db_get_name(db)) != 0) {
514 continue;
516 founddb = 1;
518 for(k = alpm_db_get_pkgcache(db); k; k = alpm_list_next(k)) {
519 alpm_pkg_t *pkg = k->data;
521 if(strcmp(alpm_pkg_get_name(pkg), pkgstr) == 0) {
522 dump_pkg_full(pkg, config->op_s_info > 1);
523 foundpkg = 1;
524 break;
529 if(!founddb) {
530 pm_printf(ALPM_LOG_ERROR,
531 _("repository '%s' does not exist\n"), repo);
532 ret++;
534 if(!foundpkg) {
535 pm_printf(ALPM_LOG_ERROR,
536 _("package '%s' was not found\n"), target);
537 ret++;
539 free(name);
541 } else {
542 for(i = syncs; i; i = alpm_list_next(i)) {
543 alpm_db_t *db = i->data;
545 for(j = alpm_db_get_pkgcache(db); j; j = alpm_list_next(j)) {
546 alpm_pkg_t *pkg = j->data;
547 dump_pkg_full(pkg, config->op_s_info > 1);
552 return ret;
555 static int sync_list(alpm_list_t *syncs, alpm_list_t *targets)
557 alpm_list_t *i, *j, *ls = NULL;
558 alpm_db_t *db_local = alpm_get_localdb(config->handle);
560 if(targets) {
561 for(i = targets; i; i = alpm_list_next(i)) {
562 const char *repo = i->data;
563 alpm_db_t *db = NULL;
565 for(j = syncs; j; j = alpm_list_next(j)) {
566 alpm_db_t *d = j->data;
568 if(strcmp(repo, alpm_db_get_name(d)) == 0) {
569 db = d;
570 break;
574 if(db == NULL) {
575 pm_printf(ALPM_LOG_ERROR,
576 _("repository \"%s\" was not found.\n"),repo);
577 alpm_list_free(ls);
578 return 1;
581 ls = alpm_list_add(ls, db);
583 } else {
584 ls = syncs;
587 for(i = ls; i; i = alpm_list_next(i)) {
588 alpm_db_t *db = i->data;
590 for(j = alpm_db_get_pkgcache(db); j; j = alpm_list_next(j)) {
591 alpm_pkg_t *pkg = j->data;
593 if(!config->quiet) {
594 printf("%s %s %s", alpm_db_get_name(db), alpm_pkg_get_name(pkg),
595 alpm_pkg_get_version(pkg));
596 print_installed(db_local, pkg);
597 printf("\n");
598 } else {
599 printf("%s\n", alpm_pkg_get_name(pkg));
604 if(targets) {
605 alpm_list_free(ls);
608 return 0;
611 static alpm_db_t *get_db(const char *dbname)
613 alpm_list_t *i;
614 for(i = alpm_get_syncdbs(config->handle); i; i = i->next) {
615 alpm_db_t *db = i->data;
616 if(strcmp(alpm_db_get_name(db), dbname) == 0) {
617 return db;
620 return NULL;
623 static int process_pkg(alpm_pkg_t *pkg)
625 int ret = alpm_add_pkg(config->handle, pkg);
627 if(ret == -1) {
628 alpm_errno_t err = alpm_errno(config->handle);
629 if(err == ALPM_ERR_TRANS_DUP_TARGET
630 || err == ALPM_ERR_PKG_IGNORED) {
631 /* just skip duplicate or ignored targets */
632 pm_printf(ALPM_LOG_WARNING, _("skipping target: %s\n"), alpm_pkg_get_name(pkg));
633 return 0;
634 } else {
635 pm_printf(ALPM_LOG_ERROR, "'%s': %s\n", alpm_pkg_get_name(pkg),
636 alpm_strerror(err));
637 return 1;
640 config->explicit_adds = alpm_list_add(config->explicit_adds, pkg);
641 return 0;
644 static int process_group(alpm_list_t *dbs, const char *group, int error)
646 int ret = 0;
647 alpm_list_t *i;
648 alpm_list_t *pkgs = alpm_find_group_pkgs(dbs, group);
649 int count = alpm_list_count(pkgs);
651 if(!count) {
652 pm_printf(ALPM_LOG_ERROR, _("target not found: %s\n"), group);
653 return 1;
656 if(error) {
657 /* we already know another target errored. there is no reason to prompt the
658 * user here; we already validated the group name so just move on since we
659 * won't actually be installing anything anyway. */
660 goto cleanup;
663 if(config->print == 0) {
664 printf(_(":: There are %d members in group %s:\n"), count,
665 group);
666 select_display(pkgs);
667 char *array = malloc(count);
668 if(!array) {
669 ret = 1;
670 goto cleanup;
672 if(multiselect_question(array, count)) {
673 ret = 1;
674 free(array);
675 goto cleanup;
677 int n = 0;
678 for(i = pkgs; i; i = alpm_list_next(i)) {
679 if(array[n++] == 0)
680 continue;
681 alpm_pkg_t *pkg = i->data;
683 if(process_pkg(pkg) == 1) {
684 ret = 1;
685 free(array);
686 goto cleanup;
689 free(array);
690 } else {
691 for(i = pkgs; i; i = alpm_list_next(i)) {
692 alpm_pkg_t *pkg = i->data;
694 if(process_pkg(pkg) == 1) {
695 ret = 1;
696 goto cleanup;
701 cleanup:
702 alpm_list_free(pkgs);
703 return ret;
706 static int process_targname(alpm_list_t *dblist, const char *targname,
707 int error)
709 alpm_pkg_t *pkg = alpm_find_dbs_satisfier(config->handle, dblist, targname);
711 /* #FS#23342 - skip ignored packages when user says no */
712 if(alpm_errno(config->handle) == ALPM_ERR_PKG_IGNORED) {
713 pm_printf(ALPM_LOG_WARNING, _("skipping target: %s\n"), targname);
714 return 0;
717 if(pkg) {
718 return process_pkg(pkg);
720 /* fallback on group */
721 return process_group(dblist, targname, error);
724 static int process_target(const char *target, int error)
726 /* process targets */
727 char *targstring = strdup(target);
728 char *targname = strchr(targstring, '/');
729 int ret = 0;
730 alpm_list_t *dblist;
732 if(targname && targname != targstring) {
733 alpm_db_t *db;
734 const char *dbname;
736 *targname = '\0';
737 targname++;
738 dbname = targstring;
739 db = get_db(dbname);
740 if(!db) {
741 pm_printf(ALPM_LOG_ERROR, _("database not found: %s\n"),
742 dbname);
743 ret = 1;
744 goto cleanup;
746 dblist = alpm_list_add(NULL, db);
747 ret = process_targname(dblist, targname, error);
748 alpm_list_free(dblist);
749 } else {
750 targname = targstring;
751 dblist = alpm_get_syncdbs(config->handle);
752 ret = process_targname(dblist, targname, error);
755 cleanup:
756 free(targstring);
757 if(ret && access(target, R_OK) == 0) {
758 pm_printf(ALPM_LOG_WARNING,
759 _("'%s' is a file, did you mean %s instead of %s?\n"),
760 target, "-U/--upgrade", "-S/--sync");
762 return ret;
765 static int sync_trans(alpm_list_t *targets)
767 int retval = 0;
768 alpm_list_t *i;
770 /* Step 1: create a new transaction... */
771 if(trans_init(config->flags, 1) == -1) {
772 return 1;
775 /* process targets */
776 for(i = targets; i; i = alpm_list_next(i)) {
777 const char *targ = i->data;
778 if(process_target(targ, retval) == 1) {
779 retval = 1;
783 if(retval) {
784 trans_release();
785 return retval;
788 if(config->op_s_upgrade) {
789 printf(_(":: Starting full system upgrade...\n"));
790 alpm_logaction(config->handle, "starting full system upgrade\n");
791 if(alpm_sync_sysupgrade(config->handle, config->op_s_upgrade >= 2) == -1) {
792 pm_printf(ALPM_LOG_ERROR, "%s\n", alpm_strerror(alpm_errno(config->handle)));
793 trans_release();
794 return 1;
798 return sync_prepare_execute();
801 int sync_prepare_execute(void)
803 alpm_list_t *i, *packages, *data = NULL;
804 int retval = 0;
806 /* Step 2: "compute" the transaction based on targets and flags */
807 if(alpm_trans_prepare(config->handle, &data) == -1) {
808 alpm_errno_t err = alpm_errno(config->handle);
809 pm_printf(ALPM_LOG_ERROR, _("failed to prepare transaction (%s)\n"),
810 alpm_strerror(err));
811 switch(err) {
812 case ALPM_ERR_PKG_INVALID_ARCH:
813 for(i = data; i; i = alpm_list_next(i)) {
814 const char *pkg = i->data;
815 printf(_(":: package %s does not have a valid architecture\n"), pkg);
817 break;
818 case ALPM_ERR_UNSATISFIED_DEPS:
819 for(i = data; i; i = alpm_list_next(i)) {
820 alpm_depmissing_t *miss = i->data;
821 char *depstring = alpm_dep_compute_string(miss->depend);
822 printf(_(":: %s: requires %s\n"), miss->target, depstring);
823 free(depstring);
825 break;
826 case ALPM_ERR_CONFLICTING_DEPS:
827 for(i = data; i; i = alpm_list_next(i)) {
828 alpm_conflict_t *conflict = i->data;
829 /* only print reason if it contains new information */
830 if(conflict->reason->mod == ALPM_DEP_MOD_ANY) {
831 printf(_(":: %s and %s are in conflict\n"),
832 conflict->package1, conflict->package2);
833 } else {
834 char *reason = alpm_dep_compute_string(conflict->reason);
835 printf(_(":: %s and %s are in conflict (%s)\n"),
836 conflict->package1, conflict->package2, reason);
837 free(reason);
840 break;
841 default:
842 break;
844 retval = 1;
845 goto cleanup;
848 packages = alpm_trans_get_add(config->handle);
849 if(packages == NULL) {
850 /* nothing to do: just exit without complaining */
851 if(!config->print) {
852 printf(_(" there is nothing to do\n"));
854 goto cleanup;
857 /* Step 3: actually perform the operation */
858 if(config->print) {
859 print_packages(packages);
860 goto cleanup;
863 display_targets();
864 printf("\n");
866 int confirm;
867 if(config->op_s_downloadonly) {
868 confirm = yesno(_("Proceed with download?"));
869 } else {
870 confirm = yesno(_("Proceed with installation?"));
872 if(!confirm) {
873 goto cleanup;
876 if(alpm_trans_commit(config->handle, &data) == -1) {
877 alpm_errno_t err = alpm_errno(config->handle);
878 pm_printf(ALPM_LOG_ERROR, _("failed to commit transaction (%s)\n"),
879 alpm_strerror(err));
880 switch(err) {
881 case ALPM_ERR_FILE_CONFLICTS:
882 for(i = data; i; i = alpm_list_next(i)) {
883 alpm_fileconflict_t *conflict = i->data;
884 switch(conflict->type) {
885 case ALPM_FILECONFLICT_TARGET:
886 printf(_("%s exists in both '%s' and '%s'\n"),
887 conflict->file, conflict->target, conflict->ctarget);
888 break;
889 case ALPM_FILECONFLICT_FILESYSTEM:
890 printf(_("%s: %s exists in filesystem\n"),
891 conflict->target, conflict->file);
892 break;
895 break;
896 case ALPM_ERR_PKG_INVALID:
897 case ALPM_ERR_PKG_INVALID_CHECKSUM:
898 case ALPM_ERR_PKG_INVALID_SIG:
899 case ALPM_ERR_DLT_INVALID:
900 for(i = data; i; i = alpm_list_next(i)) {
901 const char *filename = i->data;
902 printf(_("%s is invalid or corrupted\n"), filename);
904 break;
905 default:
906 break;
908 /* TODO: stderr? */
909 printf(_("Errors occurred, no packages were upgraded.\n"));
910 retval = 1;
911 goto cleanup;
914 /* Step 4: release transaction resources */
915 cleanup:
916 if(data) {
917 FREELIST(data);
919 if(trans_release() == -1) {
920 retval = 1;
923 return retval;
926 int pacman_sync(alpm_list_t *targets)
928 alpm_list_t *sync_dbs = NULL;
930 /* clean the cache */
931 if(config->op_s_clean) {
932 int ret = 0;
934 if(trans_init(0, 0) == -1) {
935 return 1;
938 ret += sync_cleancache(config->op_s_clean);
939 ret += sync_cleandb_all();
941 if(trans_release() == -1) {
942 ret++;
945 return ret;
948 if(check_syncdbs(1, 0)) {
949 return 1;
952 sync_dbs = alpm_get_syncdbs(config->handle);
954 if(config->op_s_sync) {
955 /* grab a fresh package list */
956 printf(_(":: Synchronizing package databases...\n"));
957 alpm_logaction(config->handle, "synchronizing package lists\n");
958 if(!sync_synctree(config->op_s_sync, sync_dbs)) {
959 return 1;
963 if(check_syncdbs(1, 1)) {
964 return 1;
967 /* search for a package */
968 if(config->op_s_search) {
969 return sync_search(sync_dbs, targets);
972 /* look for groups */
973 if(config->group) {
974 return sync_group(config->group, sync_dbs, targets);
977 /* get package info */
978 if(config->op_s_info) {
979 return sync_info(sync_dbs, targets);
982 /* get a listing of files in sync DBs */
983 if(config->op_q_list) {
984 return sync_list(sync_dbs, targets);
987 if(targets == NULL) {
988 if(config->op_s_upgrade) {
989 /* proceed */
990 } else if(config->op_s_sync) {
991 return 0;
992 } else {
993 /* don't proceed here unless we have an operation that doesn't require a
994 * target list */
995 pm_printf(ALPM_LOG_ERROR, _("no targets specified (use -h for help)\n"));
996 return 1;
1000 return sync_trans(targets);
1003 /* vim: set ts=2 sw=2 noet: */