4 * Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
6 * Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
7 * Copyright (c) 2005 by Christian Hamar <krics@linuxforum.hu>
8 * Copyright (c) 2006 by David Kimpe <dnaku@frugalware.org>
9 * Copyright (c) 2005, 2006 by Miklos Vajna <vmiklos@frugalware.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 #include "alpm_list.h"
47 int SYMEXPORT
alpm_remove_pkg(pmpkg_t
*pkg
)
55 ASSERT(pkg
!= NULL
, RET_ERR(PM_ERR_WRONG_ARGS
, -1));
56 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
57 trans
= handle
->trans
;
58 ASSERT(trans
!= NULL
, RET_ERR(PM_ERR_TRANS_NULL
, -1));
59 ASSERT(trans
->state
== STATE_INITIALIZED
,
60 RET_ERR(PM_ERR_TRANS_NOT_INITIALIZED
, -1));
62 pkgname
= alpm_pkg_get_name(pkg
);
64 if(_alpm_pkg_find(trans
->remove
, pkgname
)) {
65 RET_ERR(PM_ERR_TRANS_DUP_TARGET
, -1);
68 _alpm_log(PM_LOG_DEBUG
, "adding %s in the target list\n", pkgname
);
69 trans
->remove
= alpm_list_add(trans
->remove
, _alpm_pkg_dup(pkg
));
73 static void remove_prepare_cascade(pmtrans_t
*trans
, pmdb_t
*db
,
80 for(i
= lp
; i
; i
= i
->next
) {
81 pmdepmissing_t
*miss
= (pmdepmissing_t
*)i
->data
;
82 pmpkg_t
*info
= _alpm_db_get_pkgfromcache(db
, miss
->target
);
84 if(!_alpm_pkg_find(trans
->remove
, alpm_pkg_get_name(info
))) {
85 _alpm_log(PM_LOG_DEBUG
, "pulling %s in target list\n",
86 alpm_pkg_get_name(info
));
87 trans
->remove
= alpm_list_add(trans
->remove
, _alpm_pkg_dup(info
));
90 _alpm_log(PM_LOG_ERROR
, _("could not find %s in database -- skipping\n"),
94 alpm_list_free_inner(lp
, (alpm_list_fn_free
)_alpm_depmiss_free
);
96 lp
= alpm_checkdeps(_alpm_db_get_pkgcache(db
), 1, trans
->remove
, NULL
);
100 static void remove_prepare_keep_needed(pmtrans_t
*trans
, pmdb_t
*db
,
105 /* Remove needed packages (which break dependencies) from target list */
108 for(i
= lp
; i
; i
= i
->next
) {
109 pmdepmissing_t
*miss
= (pmdepmissing_t
*)i
->data
;
111 pmpkg_t
*pkg
= _alpm_pkg_find(trans
->remove
, miss
->causingpkg
);
115 trans
->remove
= alpm_list_remove(trans
->remove
, pkg
, _alpm_pkg_cmp
,
119 _alpm_log(PM_LOG_WARNING
, _("removing %s from target list\n"),
120 alpm_pkg_get_name(pkg
));
124 alpm_list_free_inner(lp
, (alpm_list_fn_free
)_alpm_depmiss_free
);
126 lp
= alpm_checkdeps(_alpm_db_get_pkgcache(db
), 1, trans
->remove
, NULL
);
130 /** Transaction preparation for remove actions.
131 * This functions takes a pointer to a alpm_list_t which will be
132 * filled with a list of pmdepmissing_t* objects representing
133 * the packages blocking the transaction.
134 * @param trans the transaction object
135 * @param db the database of local packages
136 * @param data a pointer to an alpm_list_t* to fill
138 int _alpm_remove_prepare(pmtrans_t
*trans
, pmdb_t
*db
, alpm_list_t
**data
)
144 ASSERT(db
!= NULL
, RET_ERR(PM_ERR_DB_NULL
, -1));
145 ASSERT(trans
!= NULL
, RET_ERR(PM_ERR_TRANS_NULL
, -1));
147 if((trans
->flags
& PM_TRANS_FLAG_RECURSE
) && !(trans
->flags
& PM_TRANS_FLAG_CASCADE
)) {
148 _alpm_log(PM_LOG_DEBUG
, "finding removable dependencies\n");
149 _alpm_recursedeps(db
, trans
->remove
, trans
->flags
& PM_TRANS_FLAG_RECURSEALL
);
152 if(!(trans
->flags
& PM_TRANS_FLAG_NODEPS
)) {
153 EVENT(trans
, PM_TRANS_EVT_CHECKDEPS_START
, NULL
, NULL
);
155 _alpm_log(PM_LOG_DEBUG
, "looking for unsatisfied dependencies\n");
156 lp
= alpm_checkdeps(_alpm_db_get_pkgcache(db
), 1, trans
->remove
, NULL
);
159 if(trans
->flags
& PM_TRANS_FLAG_CASCADE
) {
160 remove_prepare_cascade(trans
, db
, lp
);
161 } else if(trans
->flags
& PM_TRANS_FLAG_UNNEEDED
) {
162 /* Remove needed packages (which would break dependencies)
163 * from target list */
164 remove_prepare_keep_needed(trans
, db
, lp
);
169 alpm_list_free_inner(lp
, (alpm_list_fn_free
)_alpm_depmiss_free
);
172 RET_ERR(PM_ERR_UNSATISFIED_DEPS
, -1);
177 /* re-order w.r.t. dependencies */
178 _alpm_log(PM_LOG_DEBUG
, "sorting by dependencies\n");
179 lp
= _alpm_sortbydeps(trans
->remove
, 1);
180 /* free the old alltargs */
181 alpm_list_free(trans
->remove
);
184 /* -Rcs == -Rc then -Rs */
185 if((trans
->flags
& PM_TRANS_FLAG_CASCADE
) && (trans
->flags
& PM_TRANS_FLAG_RECURSE
)) {
186 _alpm_log(PM_LOG_DEBUG
, "finding removable dependencies\n");
187 _alpm_recursedeps(db
, trans
->remove
, trans
->flags
& PM_TRANS_FLAG_RECURSEALL
);
190 if(!(trans
->flags
& PM_TRANS_FLAG_NODEPS
)) {
191 EVENT(trans
, PM_TRANS_EVT_CHECKDEPS_DONE
, NULL
, NULL
);
197 static int can_remove_file(const char *path
, alpm_list_t
*skip
)
199 char file
[PATH_MAX
+1];
201 snprintf(file
, PATH_MAX
, "%s%s", handle
->root
, path
);
203 if(alpm_list_find_str(skip
, file
)) {
204 /* return success because we will never actually remove this file */
207 /* If we fail write permissions due to a read-only filesystem, abort.
208 * Assume all other possible failures are covered somewhere else */
209 if(access(file
, W_OK
) == -1) {
210 if(errno
!= EACCES
&& errno
!= ETXTBSY
&& access(file
, F_OK
) == 0) {
211 /* only return failure if the file ACTUALLY exists and we can't write to
212 * it - ignore "chmod -w" simple permission failures */
213 _alpm_log(PM_LOG_ERROR
, _("cannot remove file '%s': %s\n"),
214 file
, strerror(errno
));
222 /* Helper function for iterating through a package's file and deleting them
223 * Used by _alpm_remove_commit. */
224 static void unlink_file(pmpkg_t
*info
, char *filename
, alpm_list_t
*skip_remove
, int nosave
)
227 char file
[PATH_MAX
+1];
231 snprintf(file
, PATH_MAX
, "%s%s", handle
->root
, filename
);
233 /* check the remove skip list before removing the file.
234 * see the big comment block in db_find_fileconflicts() for an
236 if(alpm_list_find_str(skip_remove
, filename
)) {
237 _alpm_log(PM_LOG_DEBUG
, "%s is in skip_remove, skipping removal\n",
242 /* we want to do a lstat here, and not a _alpm_lstat.
243 * if a directory in the package is actually a directory symlink on the
244 * filesystem, we want to work with the linked directory instead of the
246 if(lstat(file
, &buf
)) {
247 _alpm_log(PM_LOG_DEBUG
, "file %s does not exist\n", file
);
251 if(S_ISDIR(buf
.st_mode
)) {
253 /* this is okay, other packages are probably using it (like /usr) */
254 _alpm_log(PM_LOG_DEBUG
, "keeping directory %s\n", file
);
256 _alpm_log(PM_LOG_DEBUG
, "removing directory %s\n", file
);
259 /* if the file needs backup and has been modified, back it up to .pacsave */
260 char *pkghash
= _alpm_needbackup(filename
, alpm_pkg_get_backup(info
));
263 _alpm_log(PM_LOG_DEBUG
, "transaction is set to NOSAVE, not backing up '%s'\n", file
);
266 char *filehash
= alpm_compute_md5sum(file
);
267 int cmp
= strcmp(filehash
,pkghash
);
271 char newpath
[PATH_MAX
];
272 snprintf(newpath
, PATH_MAX
, "%s.pacsave", file
);
273 rename(file
, newpath
);
274 _alpm_log(PM_LOG_WARNING
, _("%s saved as %s\n"), file
, newpath
);
275 alpm_logaction("warning: %s saved as %s\n", file
, newpath
);
281 _alpm_log(PM_LOG_DEBUG
, "unlinking %s\n", file
);
283 if(unlink(file
) == -1) {
284 _alpm_log(PM_LOG_ERROR
, _("cannot remove file '%s': %s\n"),
285 filename
, strerror(errno
));
290 int _alpm_upgraderemove_package(pmpkg_t
*oldpkg
, pmpkg_t
*newpkg
,
293 alpm_list_t
*skip_remove
, *b
;
294 alpm_list_t
*newfiles
, *lp
;
296 alpm_list_t
*files
= alpm_pkg_get_files(oldpkg
);
297 const char *pkgname
= alpm_pkg_get_name(oldpkg
);
301 _alpm_log(PM_LOG_DEBUG
, "removing old package first (%s-%s)\n",
302 oldpkg
->name
, oldpkg
->version
);
304 if(trans
->flags
& PM_TRANS_FLAG_DBONLY
) {
308 /* copy the remove skiplist over */
309 skip_remove
= alpm_list_join(
310 alpm_list_strdup(trans
->skip_remove
),
311 alpm_list_strdup(handle
->noupgrade
));
312 /* Add files in the NEW backup array to the skip_remove array
313 * so this removal operation doesn't kill them */
314 /* old package backup list */
315 alpm_list_t
*filelist
= alpm_pkg_get_files(newpkg
);
316 for(b
= alpm_pkg_get_backup(newpkg
); b
; b
= b
->next
) {
317 char *backup
= _alpm_backup_file(b
->data
);
318 /* safety check (fix the upgrade026 pactest) */
319 if(!alpm_list_find_str(filelist
, backup
)) {
323 _alpm_log(PM_LOG_DEBUG
, "adding %s to the skip_remove array\n", backup
);
324 skip_remove
= alpm_list_add(skip_remove
, backup
);
327 for(lp
= files
; lp
; lp
= lp
->next
) {
328 if(!can_remove_file(lp
->data
, skip_remove
)) {
329 _alpm_log(PM_LOG_DEBUG
, "not removing package '%s', can't remove all files\n",
331 RET_ERR(PM_ERR_PKG_CANT_REMOVE
, -1);
335 filenum
= alpm_list_count(files
);
336 _alpm_log(PM_LOG_DEBUG
, "removing %ld files\n", (unsigned long)filenum
);
338 /* iterate through the list backwards, unlinking files */
339 newfiles
= alpm_list_reverse(files
);
340 for(lp
= newfiles
; lp
; lp
= alpm_list_next(lp
)) {
341 unlink_file(oldpkg
, lp
->data
, skip_remove
, 0);
343 alpm_list_free(newfiles
);
344 FREELIST(skip_remove
);
347 /* remove the package from the database */
348 _alpm_log(PM_LOG_DEBUG
, "updating database\n");
349 _alpm_log(PM_LOG_DEBUG
, "removing database entry '%s'\n", pkgname
);
350 if(_alpm_local_db_remove(handle
->db_local
, oldpkg
) == -1) {
351 _alpm_log(PM_LOG_ERROR
, _("could not remove database entry %s-%s\n"),
352 pkgname
, alpm_pkg_get_version(oldpkg
));
354 /* remove the package from the cache */
355 if(_alpm_db_remove_pkgfromcache(handle
->db_local
, oldpkg
) == -1) {
356 _alpm_log(PM_LOG_ERROR
, _("could not remove entry '%s' from cache\n"),
363 int _alpm_remove_packages(pmtrans_t
*trans
, pmdb_t
*db
)
366 alpm_list_t
*targ
, *lp
;
371 ASSERT(db
!= NULL
, RET_ERR(PM_ERR_DB_NULL
, -1));
372 ASSERT(trans
!= NULL
, RET_ERR(PM_ERR_TRANS_NULL
, -1));
374 pkg_count
= alpm_list_count(trans
->remove
);
376 for(targ
= trans
->remove
; targ
; targ
= targ
->next
) {
378 char scriptlet
[PATH_MAX
];
379 info
= (pmpkg_t
*)targ
->data
;
380 const char *pkgname
= NULL
;
381 size_t targcount
= alpm_list_count(targ
);
383 if(handle
->trans
->state
== STATE_INTERRUPTED
) {
387 /* get the name now so we can use it after package is removed */
388 pkgname
= alpm_pkg_get_name(info
);
389 snprintf(scriptlet
, PATH_MAX
, "%s%s-%s/install",
390 _alpm_db_path(db
), pkgname
, alpm_pkg_get_version(info
));
392 EVENT(trans
, PM_TRANS_EVT_REMOVE_START
, info
, NULL
);
393 _alpm_log(PM_LOG_DEBUG
, "removing package %s-%s\n",
394 pkgname
, alpm_pkg_get_version(info
));
396 /* run the pre-remove scriptlet if it exists */
397 if(alpm_pkg_has_scriptlet(info
) && !(trans
->flags
& PM_TRANS_FLAG_NOSCRIPTLET
)) {
398 _alpm_runscriptlet(handle
->root
, scriptlet
, "pre_remove",
399 alpm_pkg_get_version(info
), NULL
, trans
);
402 if(!(trans
->flags
& PM_TRANS_FLAG_DBONLY
)) {
403 alpm_list_t
*files
= alpm_pkg_get_files(info
);
404 alpm_list_t
*newfiles
;
407 for(lp
= files
; lp
; lp
= lp
->next
) {
408 if(!can_remove_file(lp
->data
, NULL
)) {
409 _alpm_log(PM_LOG_DEBUG
, "not removing package '%s', can't remove all files\n",
411 RET_ERR(PM_ERR_PKG_CANT_REMOVE
, -1);
415 filenum
= alpm_list_count(files
);
416 _alpm_log(PM_LOG_DEBUG
, "removing %ld files\n", (unsigned long)filenum
);
418 /* init progress bar */
419 PROGRESS(trans
, PM_TRANS_PROGRESS_REMOVE_START
, info
->name
, 0,
420 pkg_count
, (pkg_count
- targcount
+ 1));
422 /* iterate through the list backwards, unlinking files */
423 newfiles
= alpm_list_reverse(files
);
424 for(lp
= newfiles
; lp
; lp
= alpm_list_next(lp
)) {
426 unlink_file(info
, lp
->data
, NULL
, trans
->flags
& PM_TRANS_FLAG_NOSAVE
);
428 /* update progress bar after each file */
429 percent
= (position
* 100) / filenum
;
430 PROGRESS(trans
, PM_TRANS_PROGRESS_REMOVE_START
, info
->name
,
431 percent
, pkg_count
, (pkg_count
- targcount
+ 1));
434 alpm_list_free(newfiles
);
437 /* set progress to 100% after we finish unlinking files */
438 PROGRESS(trans
, PM_TRANS_PROGRESS_REMOVE_START
, pkgname
, 100,
439 pkg_count
, (pkg_count
- targcount
+ 1));
441 /* run the post-remove script if it exists */
442 if(alpm_pkg_has_scriptlet(info
) && !(trans
->flags
& PM_TRANS_FLAG_NOSCRIPTLET
)) {
443 _alpm_runscriptlet(handle
->root
, scriptlet
, "post_remove",
444 alpm_pkg_get_version(info
), NULL
, trans
);
447 /* remove the package from the database */
448 _alpm_log(PM_LOG_DEBUG
, "updating database\n");
449 _alpm_log(PM_LOG_DEBUG
, "removing database entry '%s'\n", pkgname
);
450 if(_alpm_local_db_remove(db
, info
) == -1) {
451 _alpm_log(PM_LOG_ERROR
, _("could not remove database entry %s-%s\n"),
452 pkgname
, alpm_pkg_get_version(info
));
454 /* remove the package from the cache */
455 if(_alpm_db_remove_pkgfromcache(db
, info
) == -1) {
456 _alpm_log(PM_LOG_ERROR
, _("could not remove entry '%s' from cache\n"),
460 EVENT(trans
, PM_TRANS_EVT_REMOVE_DONE
, info
, NULL
);
463 /* run ldconfig if it exists */
464 _alpm_ldconfig(handle
->root
);
469 /* vim: set ts=2 sw=2 noet: */