4 * Copyright (c) 2006-2013 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/>.
32 #include <alpm_list.h>
40 static int unlink_verbose(const char *pathname
, int ignore_missing
)
42 int ret
= unlink(pathname
);
44 if(ignore_missing
&& errno
== ENOENT
) {
47 pm_printf(ALPM_LOG_ERROR
, _("could not remove %s: %s\n"),
48 pathname
, strerror(errno
));
54 /* if keep_used != 0, then the db files which match an used syncdb
56 static int sync_cleandb(const char *dbpath
, int keep_used
)
63 dir
= opendir(dbpath
);
65 pm_printf(ALPM_LOG_ERROR
, _("could not access database directory\n"));
69 syncdbs
= alpm_get_syncdbs(config
->handle
);
72 /* step through the directory one file at a time */
73 while((ent
= readdir(dir
)) != NULL
) {
77 const char *dname
= ent
->d_name
;
81 if(strcmp(dname
, ".") == 0 || strcmp(dname
, "..") == 0) {
84 /* skip the local and sync directories */
85 if(strcmp(dname
, "sync") == 0 || strcmp(dname
, "local") == 0) {
88 /* skip the db.lck file */
89 if(strcmp(dname
, "db.lck") == 0) {
93 /* build the full path */
94 snprintf(path
, PATH_MAX
, "%s%s", dbpath
, dname
);
96 /* remove all non-skipped directories and non-database files */
98 if(S_ISDIR(buf
.st_mode
)) {
100 pm_printf(ALPM_LOG_ERROR
, _("could not remove %s: %s\n"),
101 path
, strerror(errno
));
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);
112 ret
+= unlink_verbose(path
, 0);
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. */
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);
140 static int sync_cleandb_all(void)
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?"))) {
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) {
161 ret
+= sync_cleandb(newdbpath
, 1);
167 static int sync_cleancache(int level
)
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
);
175 if(!config
->cleanmethod
) {
176 /* default to KeepInstalled if user did not specify */
177 config
->cleanmethod
= PM_CLEAN_KEEPINST
;
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"));
191 for(i
= cachedirs
; i
; i
= alpm_list_next(i
)) {
192 const char *cachedir
= i
->data
;
196 printf(_("Cache directory: %s\n"), (const char *)i
->data
);
199 if(!yesno(_("Do you want to remove all other packages from cache?"))) {
203 printf(_("removing old packages from cache...\n"));
205 if(!noyes(_("Do you want to remove ALL files from cache?"))) {
209 printf(_("removing all files from cache...\n"));
212 dir
= opendir(cachedir
);
214 pm_printf(ALPM_LOG_ERROR
,
215 _("could not access cache directory %s\n"), cachedir
);
221 /* step through the directory one file at a time */
222 while((ent
= readdir(dir
)) != NULL
) {
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) {
233 static const char * const glob_skips
[] = {
234 /* skip signature files - they are removed with their package file */
236 /* skip package database within the cache directory */
238 /* skip source packages within the cache directory */
240 /* skip package deltas, we aren't smart enough to clean these yet */
245 for(j
= 0; j
< sizeof(glob_skips
) / sizeof(glob_skips
[0]); j
++) {
246 if(fnmatch(glob_skips
[j
], ent
->d_name
, 0) == 0) {
256 /* build the full filepath */
257 snprintf(path
, PATH_MAX
, "%s%s", cachedir
, ent
->d_name
);
259 /* short circuit for removing all files from cache */
261 ret
+= unlink_verbose(path
, 0);
265 /* attempt to load the file as a package. if we cannot load the file,
266 * simply skip it and move on. we don't need a full load of the package,
267 * just the metadata. */
268 if(alpm_pkg_load(config
->handle
, path
, 0, 0, &localpkg
) != 0) {
269 pm_printf(ALPM_LOG_DEBUG
, "skipping %s, could not load as package\n",
273 local_name
= alpm_pkg_get_name(localpkg
);
274 local_version
= alpm_pkg_get_version(localpkg
);
276 if(config
->cleanmethod
& PM_CLEAN_KEEPINST
) {
277 /* check if this package is in the local DB */
278 pkg
= alpm_db_get_pkg(db_local
, local_name
);
279 if(pkg
!= NULL
&& alpm_pkg_vercmp(local_version
,
280 alpm_pkg_get_version(pkg
)) == 0) {
281 /* package was found in local DB and version matches, keep it */
282 pm_printf(ALPM_LOG_DEBUG
, "package %s-%s found in local db\n",
283 local_name
, local_version
);
287 if(config
->cleanmethod
& PM_CLEAN_KEEPCUR
) {
289 /* check if this package is in a sync DB */
290 for(j
= sync_dbs
; j
&& delete; j
= alpm_list_next(j
)) {
291 alpm_db_t
*db
= j
->data
;
292 pkg
= alpm_db_get_pkg(db
, local_name
);
293 if(pkg
!= NULL
&& alpm_pkg_vercmp(local_version
,
294 alpm_pkg_get_version(pkg
)) == 0) {
295 /* package was found in a sync DB and version matches, keep it */
296 pm_printf(ALPM_LOG_DEBUG
, "package %s-%s found in sync db\n",
297 local_name
, local_version
);
302 /* free the local file package */
303 alpm_pkg_free(localpkg
);
306 size_t pathlen
= strlen(path
);
307 ret
+= unlink_verbose(path
, 0);
308 /* unlink a signature file if present too */
309 if(PATH_MAX
- 5 >= pathlen
) {
310 strcpy(path
+ pathlen
, ".sig");
311 ret
+= unlink_verbose(path
, 1);
322 static int sync_synctree(int level
, alpm_list_t
*syncs
)
325 unsigned int success
= 0;
327 for(i
= syncs
; i
; i
= alpm_list_next(i
)) {
328 alpm_db_t
*db
= i
->data
;
330 int ret
= alpm_db_update((level
< 2 ? 0 : 1), db
);
332 pm_printf(ALPM_LOG_ERROR
, _("failed to update %s (%s)\n"),
333 alpm_db_get_name(db
), alpm_strerror(alpm_errno(config
->handle
)));
334 } else if(ret
== 1) {
335 printf(_(" %s is up to date\n"), alpm_db_get_name(db
));
342 /* We should always succeed if at least one DB was upgraded - we may possibly
343 * fail later with unresolved deps, but that should be rare, and would be
347 pm_printf(ALPM_LOG_ERROR
, _("failed to synchronize any databases\n"));
350 return (success
> 0);
353 /* search the sync dbs for a matching package */
354 static int sync_search(alpm_list_t
*syncs
, alpm_list_t
*targets
)
359 for(i
= syncs
; i
; i
= alpm_list_next(i
)) {
360 alpm_db_t
*db
= i
->data
;
361 found
+= !dump_pkg_search(db
, targets
, 1);
367 static int sync_group(int level
, alpm_list_t
*syncs
, alpm_list_t
*targets
)
369 alpm_list_t
*i
, *j
, *k
, *s
= NULL
;
372 for(i
= targets
; i
; i
= alpm_list_next(i
)) {
373 const char *grpname
= i
->data
;
374 for(j
= syncs
; j
; j
= alpm_list_next(j
)) {
375 alpm_db_t
*db
= j
->data
;
376 alpm_group_t
*grp
= alpm_db_get_group(db
, grpname
);
379 /* get names of packages in group */
380 for(k
= grp
->packages
; k
; k
= alpm_list_next(k
)) {
382 printf("%s %s\n", grpname
,
383 alpm_pkg_get_name(k
->data
));
385 printf("%s\n", alpm_pkg_get_name(k
->data
));
392 for(i
= syncs
; i
; i
= alpm_list_next(i
)) {
393 alpm_db_t
*db
= i
->data
;
395 for(j
= alpm_db_get_groupcache(db
); j
; j
= alpm_list_next(j
)) {
396 alpm_group_t
*grp
= j
->data
;
399 for(k
= grp
->packages
; k
; k
= alpm_list_next(k
)) {
400 printf("%s %s\n", grp
->name
,
401 alpm_pkg_get_name(k
->data
));
404 /* print grp names only, no package names */
405 if(!alpm_list_find_str (s
, grp
->name
)) {
406 s
= alpm_list_add (s
, grp
->name
);
407 printf("%s\n", grp
->name
);
418 static int sync_info(alpm_list_t
*syncs
, alpm_list_t
*targets
)
420 alpm_list_t
*i
, *j
, *k
;
424 for(i
= targets
; i
; i
= alpm_list_next(i
)) {
425 const char *target
= i
->data
;
426 char *name
= strdup(target
);
428 int foundpkg
= 0, founddb
= 0;
430 pkgstr
= strchr(name
, '/');
440 for(j
= syncs
; j
; j
= alpm_list_next(j
)) {
441 alpm_db_t
*db
= j
->data
;
442 if(repo
&& strcmp(repo
, alpm_db_get_name(db
)) != 0) {
447 for(k
= alpm_db_get_pkgcache(db
); k
; k
= alpm_list_next(k
)) {
448 alpm_pkg_t
*pkg
= k
->data
;
450 if(strcmp(alpm_pkg_get_name(pkg
), pkgstr
) == 0) {
451 dump_pkg_full(pkg
, config
->op_s_info
> 1);
459 pm_printf(ALPM_LOG_ERROR
,
460 _("repository '%s' does not exist\n"), repo
);
464 pm_printf(ALPM_LOG_ERROR
,
465 _("package '%s' was not found\n"), target
);
471 for(i
= syncs
; i
; i
= alpm_list_next(i
)) {
472 alpm_db_t
*db
= i
->data
;
474 for(j
= alpm_db_get_pkgcache(db
); j
; j
= alpm_list_next(j
)) {
475 alpm_pkg_t
*pkg
= j
->data
;
476 dump_pkg_full(pkg
, config
->op_s_info
> 1);
484 static int sync_list(alpm_list_t
*syncs
, alpm_list_t
*targets
)
486 alpm_list_t
*i
, *j
, *ls
= NULL
;
487 alpm_db_t
*db_local
= alpm_get_localdb(config
->handle
);
491 for(i
= targets
; i
; i
= alpm_list_next(i
)) {
492 const char *repo
= i
->data
;
493 alpm_db_t
*db
= NULL
;
495 for(j
= syncs
; j
; j
= alpm_list_next(j
)) {
496 alpm_db_t
*d
= j
->data
;
498 if(strcmp(repo
, alpm_db_get_name(d
)) == 0) {
505 pm_printf(ALPM_LOG_ERROR
,
506 _("repository \"%s\" was not found.\n"), repo
);
510 ls
= alpm_list_add(ls
, db
);
516 for(i
= ls
; i
; i
= alpm_list_next(i
)) {
517 alpm_db_t
*db
= i
->data
;
519 for(j
= alpm_db_get_pkgcache(db
); j
; j
= alpm_list_next(j
)) {
520 alpm_pkg_t
*pkg
= j
->data
;
523 const colstr_t
*colstr
= &config
->colstr
;
524 printf("%s%s %s%s %s%s%s", colstr
->repo
, alpm_db_get_name(db
),
525 colstr
->title
, alpm_pkg_get_name(pkg
),
526 colstr
->version
, alpm_pkg_get_version(pkg
), colstr
->nocolor
);
527 print_installed(db_local
, pkg
);
530 printf("%s\n", alpm_pkg_get_name(pkg
));
542 static alpm_db_t
*get_db(const char *dbname
)
545 for(i
= alpm_get_syncdbs(config
->handle
); i
; i
= i
->next
) {
546 alpm_db_t
*db
= i
->data
;
547 if(strcmp(alpm_db_get_name(db
), dbname
) == 0) {
554 static int process_pkg(alpm_pkg_t
*pkg
)
556 int ret
= alpm_add_pkg(config
->handle
, pkg
);
559 alpm_errno_t err
= alpm_errno(config
->handle
);
560 if(err
== ALPM_ERR_TRANS_DUP_TARGET
561 || err
== ALPM_ERR_PKG_IGNORED
) {
562 /* just skip duplicate or ignored targets */
563 pm_printf(ALPM_LOG_WARNING
, _("skipping target: %s\n"), alpm_pkg_get_name(pkg
));
566 pm_printf(ALPM_LOG_ERROR
, "'%s': %s\n", alpm_pkg_get_name(pkg
),
571 config
->explicit_adds
= alpm_list_add(config
->explicit_adds
, pkg
);
575 static int process_group(alpm_list_t
*dbs
, const char *group
, int error
)
579 alpm_list_t
*pkgs
= alpm_find_group_pkgs(dbs
, group
);
580 int count
= alpm_list_count(pkgs
);
583 pm_printf(ALPM_LOG_ERROR
, _("target not found: %s\n"), group
);
588 /* we already know another target errored. there is no reason to prompt the
589 * user here; we already validated the group name so just move on since we
590 * won't actually be installing anything anyway. */
594 if(config
->print
== 0) {
595 colon_printf(_("There are %d members in group %s:\n"), count
,
597 select_display(pkgs
);
598 char *array
= malloc(count
);
603 if(multiselect_question(array
, count
)) {
609 for(i
= pkgs
; i
; i
= alpm_list_next(i
)) {
612 alpm_pkg_t
*pkg
= i
->data
;
614 if(process_pkg(pkg
) == 1) {
622 for(i
= pkgs
; i
; i
= alpm_list_next(i
)) {
623 alpm_pkg_t
*pkg
= i
->data
;
625 if(process_pkg(pkg
) == 1) {
633 alpm_list_free(pkgs
);
637 static int process_targname(alpm_list_t
*dblist
, const char *targname
,
640 alpm_pkg_t
*pkg
= alpm_find_dbs_satisfier(config
->handle
, dblist
, targname
);
642 /* #FS#23342 - skip ignored packages when user says no */
643 if(alpm_errno(config
->handle
) == ALPM_ERR_PKG_IGNORED
) {
644 pm_printf(ALPM_LOG_WARNING
, _("skipping target: %s\n"), targname
);
649 return process_pkg(pkg
);
651 /* fallback on group */
652 return process_group(dblist
, targname
, error
);
655 static int process_target(const char *target
, int error
)
657 /* process targets */
658 char *targstring
= strdup(target
);
659 char *targname
= strchr(targstring
, '/');
663 if(targname
&& targname
!= targstring
) {
672 pm_printf(ALPM_LOG_ERROR
, _("database not found: %s\n"),
677 dblist
= alpm_list_add(NULL
, db
);
678 ret
= process_targname(dblist
, targname
, error
);
679 alpm_list_free(dblist
);
681 targname
= targstring
;
682 dblist
= alpm_get_syncdbs(config
->handle
);
683 ret
= process_targname(dblist
, targname
, error
);
688 if(ret
&& access(target
, R_OK
) == 0) {
689 pm_printf(ALPM_LOG_WARNING
,
690 _("'%s' is a file, did you mean %s instead of %s?\n"),
691 target
, "-U/--upgrade", "-S/--sync");
696 static int sync_trans(alpm_list_t
*targets
)
701 /* Step 1: create a new transaction... */
702 if(trans_init(config
->flags
, 1) == -1) {
706 /* process targets */
707 for(i
= targets
; i
; i
= alpm_list_next(i
)) {
708 const char *targ
= i
->data
;
709 if(process_target(targ
, retval
) == 1) {
719 if(config
->op_s_upgrade
) {
720 colon_printf(_("Starting full system upgrade...\n"));
721 alpm_logaction(config
->handle
, PACMAN_CALLER_PREFIX
,
722 "starting full system upgrade\n");
723 if(alpm_sync_sysupgrade(config
->handle
, config
->op_s_upgrade
>= 2) == -1) {
724 pm_printf(ALPM_LOG_ERROR
, "%s\n", alpm_strerror(alpm_errno(config
->handle
)));
730 return sync_prepare_execute();
733 int sync_prepare_execute(void)
735 alpm_list_t
*i
, *packages
, *data
= NULL
;
738 /* Step 2: "compute" the transaction based on targets and flags */
739 if(alpm_trans_prepare(config
->handle
, &data
) == -1) {
740 alpm_errno_t err
= alpm_errno(config
->handle
);
741 pm_printf(ALPM_LOG_ERROR
, _("failed to prepare transaction (%s)\n"),
744 case ALPM_ERR_PKG_INVALID_ARCH
:
745 for(i
= data
; i
; i
= alpm_list_next(i
)) {
746 const char *pkg
= i
->data
;
747 colon_printf(_("package %s does not have a valid architecture\n"), pkg
);
750 case ALPM_ERR_UNSATISFIED_DEPS
:
751 for(i
= data
; i
; i
= alpm_list_next(i
)) {
752 alpm_depmissing_t
*miss
= i
->data
;
753 char *depstring
= alpm_dep_compute_string(miss
->depend
);
754 colon_printf(_("%s: requires %s\n"), miss
->target
, depstring
);
758 case ALPM_ERR_CONFLICTING_DEPS
:
759 for(i
= data
; i
; i
= alpm_list_next(i
)) {
760 alpm_conflict_t
*conflict
= i
->data
;
761 /* only print reason if it contains new information */
762 if(conflict
->reason
->mod
== ALPM_DEP_MOD_ANY
) {
763 colon_printf(_("%s and %s are in conflict\n"),
764 conflict
->package1
, conflict
->package2
);
766 char *reason
= alpm_dep_compute_string(conflict
->reason
);
767 colon_printf(_("%s and %s are in conflict (%s)\n"),
768 conflict
->package1
, conflict
->package2
, reason
);
780 packages
= alpm_trans_get_add(config
->handle
);
781 if(packages
== NULL
) {
782 /* nothing to do: just exit without complaining */
784 printf(_(" there is nothing to do\n"));
789 /* Step 3: actually perform the operation */
791 print_packages(packages
);
799 if(config
->op_s_downloadonly
) {
800 confirm
= yesno(_("Proceed with download?"));
802 confirm
= yesno(_("Proceed with installation?"));
809 if(alpm_trans_commit(config
->handle
, &data
) == -1) {
810 alpm_errno_t err
= alpm_errno(config
->handle
);
811 pm_printf(ALPM_LOG_ERROR
, _("failed to commit transaction (%s)\n"),
814 case ALPM_ERR_FILE_CONFLICTS
:
815 if(config
->flags
& ALPM_TRANS_FLAG_FORCE
) {
816 printf(_("unable to %s directory-file conflicts\n"), "--force");
818 for(i
= data
; i
; i
= alpm_list_next(i
)) {
819 alpm_fileconflict_t
*conflict
= i
->data
;
820 switch(conflict
->type
) {
821 case ALPM_FILECONFLICT_TARGET
:
822 printf(_("%s exists in both '%s' and '%s'\n"),
823 conflict
->file
, conflict
->target
, conflict
->ctarget
);
825 case ALPM_FILECONFLICT_FILESYSTEM
:
826 printf(_("%s: %s exists in filesystem\n"),
827 conflict
->target
, conflict
->file
);
832 case ALPM_ERR_PKG_INVALID
:
833 case ALPM_ERR_PKG_INVALID_CHECKSUM
:
834 case ALPM_ERR_PKG_INVALID_SIG
:
835 case ALPM_ERR_DLT_INVALID
:
836 for(i
= data
; i
; i
= alpm_list_next(i
)) {
837 const char *filename
= i
->data
;
838 printf(_("%s is invalid or corrupted\n"), filename
);
845 printf(_("Errors occurred, no packages were upgraded.\n"));
850 /* Step 4: release transaction resources */
855 if(trans_release() == -1) {
862 int pacman_sync(alpm_list_t
*targets
)
864 alpm_list_t
*sync_dbs
= NULL
;
866 /* clean the cache */
867 if(config
->op_s_clean
) {
870 if(trans_init(0, 0) == -1) {
874 ret
+= sync_cleancache(config
->op_s_clean
);
875 ret
+= sync_cleandb_all();
877 if(trans_release() == -1) {
884 if(check_syncdbs(1, 0)) {
888 sync_dbs
= alpm_get_syncdbs(config
->handle
);
890 if(config
->op_s_sync
) {
891 /* grab a fresh package list */
892 colon_printf(_("Synchronizing package databases...\n"));
893 alpm_logaction(config
->handle
, PACMAN_CALLER_PREFIX
,
894 "synchronizing package lists\n");
895 if(!sync_synctree(config
->op_s_sync
, sync_dbs
)) {
900 if(check_syncdbs(1, 1)) {
904 /* search for a package */
905 if(config
->op_s_search
) {
906 return sync_search(sync_dbs
, targets
);
909 /* look for groups */
911 return sync_group(config
->group
, sync_dbs
, targets
);
914 /* get package info */
915 if(config
->op_s_info
) {
916 return sync_info(sync_dbs
, targets
);
919 /* get a listing of files in sync DBs */
920 if(config
->op_q_list
) {
921 return sync_list(sync_dbs
, targets
);
924 if(targets
== NULL
) {
925 if(config
->op_s_upgrade
) {
927 } else if(config
->op_s_sync
) {
930 /* don't proceed here unless we have an operation that doesn't require a
932 pm_printf(ALPM_LOG_ERROR
, _("no targets specified (use -h for help)\n"));
937 return sync_trans(targets
);
940 /* vim: set ts=2 sw=2 noet: */