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/>.
38 #include "alpm_list.h"
49 int SYMEXPORT
alpm_remove_pkg(pmpkg_t
*pkg
)
57 ASSERT(pkg
!= NULL
, RET_ERR(PM_ERR_WRONG_ARGS
, -1));
58 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
59 trans
= handle
->trans
;
60 ASSERT(trans
!= NULL
, RET_ERR(PM_ERR_TRANS_NULL
, -1));
61 ASSERT(trans
->state
== STATE_INITIALIZED
,
62 RET_ERR(PM_ERR_TRANS_NOT_INITIALIZED
, -1));
64 pkgname
= alpm_pkg_get_name(pkg
);
66 if(_alpm_pkg_find(trans
->remove
, pkgname
)) {
67 RET_ERR(PM_ERR_TRANS_DUP_TARGET
, -1);
70 _alpm_log(PM_LOG_DEBUG
, "adding %s in the target list\n", pkgname
);
71 trans
->remove
= alpm_list_add(trans
->remove
, _alpm_pkg_dup(pkg
));
75 static void remove_prepare_cascade(pmtrans_t
*trans
, pmdb_t
*db
,
82 for(i
= lp
; i
; i
= i
->next
) {
83 pmdepmissing_t
*miss
= (pmdepmissing_t
*)i
->data
;
84 pmpkg_t
*info
= _alpm_db_get_pkgfromcache(db
, miss
->target
);
86 if(!_alpm_pkg_find(trans
->remove
, alpm_pkg_get_name(info
))) {
87 _alpm_log(PM_LOG_DEBUG
, "pulling %s in target list\n",
88 alpm_pkg_get_name(info
));
89 trans
->remove
= alpm_list_add(trans
->remove
, _alpm_pkg_dup(info
));
92 _alpm_log(PM_LOG_ERROR
, _("could not find %s in database -- skipping\n"),
96 alpm_list_free_inner(lp
, (alpm_list_fn_free
)_alpm_depmiss_free
);
98 lp
= alpm_checkdeps(_alpm_db_get_pkgcache_list(db
), 1, trans
->remove
, NULL
);
102 static void remove_prepare_keep_needed(pmtrans_t
*trans
, pmdb_t
*db
,
107 /* Remove needed packages (which break dependencies) from target list */
110 for(i
= lp
; i
; i
= i
->next
) {
111 pmdepmissing_t
*miss
= (pmdepmissing_t
*)i
->data
;
113 pmpkg_t
*pkg
= _alpm_pkg_find(trans
->remove
, miss
->causingpkg
);
117 trans
->remove
= alpm_list_remove(trans
->remove
, pkg
, _alpm_pkg_cmp
,
121 _alpm_log(PM_LOG_WARNING
, _("removing %s from target list\n"),
122 alpm_pkg_get_name(pkg
));
126 alpm_list_free_inner(lp
, (alpm_list_fn_free
)_alpm_depmiss_free
);
128 lp
= alpm_checkdeps(_alpm_db_get_pkgcache_list(db
), 1, trans
->remove
, NULL
);
132 int _alpm_remove_prepare(pmtrans_t
*trans
, pmdb_t
*db
, alpm_list_t
**data
)
138 ASSERT(db
!= NULL
, RET_ERR(PM_ERR_DB_NULL
, -1));
139 ASSERT(trans
!= NULL
, RET_ERR(PM_ERR_TRANS_NULL
, -1));
141 if((trans
->flags
& PM_TRANS_FLAG_RECURSE
) && !(trans
->flags
& PM_TRANS_FLAG_CASCADE
)) {
142 _alpm_log(PM_LOG_DEBUG
, "finding removable dependencies\n");
143 _alpm_recursedeps(db
, trans
->remove
, trans
->flags
& PM_TRANS_FLAG_RECURSEALL
);
146 if(!(trans
->flags
& PM_TRANS_FLAG_NODEPS
)) {
147 EVENT(trans
, PM_TRANS_EVT_CHECKDEPS_START
, NULL
, NULL
);
149 _alpm_log(PM_LOG_DEBUG
, "looking for unsatisfied dependencies\n");
150 lp
= alpm_checkdeps(_alpm_db_get_pkgcache_list(db
), 1, trans
->remove
, NULL
);
153 if(trans
->flags
& PM_TRANS_FLAG_CASCADE
) {
154 remove_prepare_cascade(trans
, db
, lp
);
155 } else if (trans
->flags
& PM_TRANS_FLAG_UNNEEDED
) {
156 /* Remove needed packages (which would break dependencies)
157 * from target list */
158 remove_prepare_keep_needed(trans
, db
, lp
);
163 alpm_list_free_inner(lp
, (alpm_list_fn_free
)_alpm_depmiss_free
);
166 RET_ERR(PM_ERR_UNSATISFIED_DEPS
, -1);
171 /* re-order w.r.t. dependencies */
172 _alpm_log(PM_LOG_DEBUG
, "sorting by dependencies\n");
173 lp
= _alpm_sortbydeps(trans
->remove
, 1);
174 /* free the old alltargs */
175 alpm_list_free(trans
->remove
);
178 /* -Rcs == -Rc then -Rs */
179 if((trans
->flags
& PM_TRANS_FLAG_CASCADE
) && (trans
->flags
& PM_TRANS_FLAG_RECURSE
)) {
180 _alpm_log(PM_LOG_DEBUG
, "finding removable dependencies\n");
181 _alpm_recursedeps(db
, trans
->remove
, trans
->flags
& PM_TRANS_FLAG_RECURSEALL
);
184 if(!(trans
->flags
& PM_TRANS_FLAG_NODEPS
)) {
185 EVENT(trans
, PM_TRANS_EVT_CHECKDEPS_DONE
, NULL
, NULL
);
191 static int can_remove_file(const char *path
, alpm_list_t
*skip
)
193 char file
[PATH_MAX
+1];
195 snprintf(file
, PATH_MAX
, "%s%s", handle
->root
, path
);
197 if(alpm_list_find_str(skip
, file
)) {
198 /* return success because we will never actually remove this file */
201 /* If we fail write permissions due to a read-only filesystem, abort.
202 * Assume all other possible failures are covered somewhere else */
203 if(access(file
, W_OK
) == -1) {
204 if(errno
!= EACCES
&& errno
!= ETXTBSY
&& access(file
, F_OK
) == 0) {
205 /* only return failure if the file ACTUALLY exists and we can't write to
206 * it - ignore "chmod -w" simple permission failures */
207 _alpm_log(PM_LOG_ERROR
, _("cannot remove file '%s': %s\n"),
208 file
, strerror(errno
));
216 /* Helper function for iterating through a package's file and deleting them
217 * Used by _alpm_remove_commit. */
218 static void unlink_file(pmpkg_t
*info
, char *filename
, alpm_list_t
*skip_remove
, int nosave
)
221 char file
[PATH_MAX
+1];
225 snprintf(file
, PATH_MAX
, "%s%s", handle
->root
, filename
);
227 /* check the remove skip list before removing the file.
228 * see the big comment block in db_find_fileconflicts() for an
230 if(alpm_list_find_str(skip_remove
, filename
)) {
231 _alpm_log(PM_LOG_DEBUG
, "%s is in skip_remove, skipping removal\n",
236 /* we want to do a lstat here, and not a _alpm_lstat.
237 * if a directory in the package is actually a directory symlink on the
238 * filesystem, we want to work with the linked directory instead of the
240 if(lstat(file
, &buf
)) {
241 _alpm_log(PM_LOG_DEBUG
, "file %s does not exist\n", file
);
245 if(S_ISDIR(buf
.st_mode
)) {
247 /* this is okay, other packages are probably using it (like /usr) */
248 _alpm_log(PM_LOG_DEBUG
, "keeping directory %s\n", file
);
250 _alpm_log(PM_LOG_DEBUG
, "removing directory %s\n", file
);
253 /* if the file needs backup and has been modified, back it up to .pacsave */
254 char *pkghash
= _alpm_needbackup(filename
, alpm_pkg_get_backup(info
));
257 _alpm_log(PM_LOG_DEBUG
, "transaction is set to NOSAVE, not backing up '%s'\n", file
);
260 char *filehash
= alpm_compute_md5sum(file
);
261 int cmp
= strcmp(filehash
,pkghash
);
265 char newpath
[PATH_MAX
];
266 snprintf(newpath
, PATH_MAX
, "%s.pacsave", file
);
267 rename(file
, newpath
);
268 _alpm_log(PM_LOG_WARNING
, _("%s saved as %s\n"), file
, newpath
);
269 alpm_logaction("warning: %s saved as %s\n", file
, newpath
);
275 _alpm_log(PM_LOG_DEBUG
, "unlinking %s\n", file
);
277 if(unlink(file
) == -1) {
278 _alpm_log(PM_LOG_ERROR
, _("cannot remove file '%s': %s\n"),
279 filename
, strerror(errno
));
284 int _alpm_upgraderemove_package(pmpkg_t
*oldpkg
, pmpkg_t
*newpkg
,
287 alpm_list_t
*skip_remove
, *b
;
288 alpm_list_t
*newfiles
, *lp
;
290 alpm_list_t
*files
= alpm_pkg_get_files(oldpkg
);
291 const char *pkgname
= alpm_pkg_get_name(oldpkg
);
295 _alpm_log(PM_LOG_DEBUG
, "removing old package first (%s-%s)\n",
296 oldpkg
->name
, oldpkg
->version
);
298 if(trans
->flags
& PM_TRANS_FLAG_DBONLY
) {
302 /* copy the remove skiplist over */
303 skip_remove
= alpm_list_join(
304 alpm_list_strdup(trans
->skip_remove
),
305 alpm_list_strdup(handle
->noupgrade
));
306 /* Add files in the NEW backup array to the skip_remove array
307 * so this removal operation doesn't kill them */
308 /* old package backup list */
309 alpm_list_t
*filelist
= alpm_pkg_get_files(newpkg
);
310 for(b
= alpm_pkg_get_backup(newpkg
); b
; b
= b
->next
) {
311 char *backup
= _alpm_backup_file(b
->data
);
312 /* safety check (fix the upgrade026 pactest) */
313 if(!alpm_list_find_str(filelist
, backup
)) {
317 _alpm_log(PM_LOG_DEBUG
, "adding %s to the skip_remove array\n", backup
);
318 skip_remove
= alpm_list_add(skip_remove
, backup
);
321 for(lp
= files
; lp
; lp
= lp
->next
) {
322 if(!can_remove_file(lp
->data
, skip_remove
)) {
323 _alpm_log(PM_LOG_DEBUG
, "not removing package '%s', can't remove all files\n",
325 RET_ERR(PM_ERR_PKG_CANT_REMOVE
, -1);
329 filenum
= alpm_list_count(files
);
330 _alpm_log(PM_LOG_DEBUG
, "removing %ld files\n", (unsigned long)filenum
);
332 /* iterate through the list backwards, unlinking files */
333 newfiles
= alpm_list_reverse(files
);
334 for(lp
= newfiles
; lp
; lp
= alpm_list_next(lp
)) {
335 unlink_file(oldpkg
, lp
->data
, skip_remove
, 0);
337 alpm_list_free(newfiles
);
338 FREELIST(skip_remove
);
341 /* remove the package from the database */
342 _alpm_log(PM_LOG_DEBUG
, "updating database\n");
343 _alpm_log(PM_LOG_DEBUG
, "removing database entry '%s'\n", pkgname
);
344 if(_alpm_local_db_remove(handle
->db_local
, oldpkg
) == -1) {
345 _alpm_log(PM_LOG_ERROR
, _("could not remove database entry %s-%s\n"),
346 pkgname
, alpm_pkg_get_version(oldpkg
));
348 /* remove the package from the cache */
349 if(_alpm_db_remove_pkgfromcache(handle
->db_local
, oldpkg
) == -1) {
350 _alpm_log(PM_LOG_ERROR
, _("could not remove entry '%s' from cache\n"),
357 int _alpm_remove_packages(pmtrans_t
*trans
, pmdb_t
*db
)
360 alpm_list_t
*targ
, *lp
;
365 ASSERT(db
!= NULL
, RET_ERR(PM_ERR_DB_NULL
, -1));
366 ASSERT(trans
!= NULL
, RET_ERR(PM_ERR_TRANS_NULL
, -1));
368 pkg_count
= alpm_list_count(trans
->remove
);
370 for(targ
= trans
->remove
; targ
; targ
= targ
->next
) {
372 char scriptlet
[PATH_MAX
];
373 info
= (pmpkg_t
*)targ
->data
;
374 const char *pkgname
= NULL
;
375 size_t targcount
= alpm_list_count(targ
);
377 if(handle
->trans
->state
== STATE_INTERRUPTED
) {
381 /* get the name now so we can use it after package is removed */
382 pkgname
= alpm_pkg_get_name(info
);
383 snprintf(scriptlet
, PATH_MAX
, "%s%s-%s/install",
384 _alpm_db_path(db
), pkgname
, alpm_pkg_get_version(info
));
386 EVENT(trans
, PM_TRANS_EVT_REMOVE_START
, info
, NULL
);
387 _alpm_log(PM_LOG_DEBUG
, "removing package %s-%s\n",
388 pkgname
, alpm_pkg_get_version(info
));
390 /* run the pre-remove scriptlet if it exists */
391 if(alpm_pkg_has_scriptlet(info
) && !(trans
->flags
& PM_TRANS_FLAG_NOSCRIPTLET
)) {
392 _alpm_runscriptlet(handle
->root
, scriptlet
, "pre_remove",
393 alpm_pkg_get_version(info
), NULL
, trans
);
396 if(!(trans
->flags
& PM_TRANS_FLAG_DBONLY
)) {
397 alpm_list_t
*files
= alpm_pkg_get_files(info
);
398 alpm_list_t
*newfiles
;
401 for(lp
= files
; lp
; lp
= lp
->next
) {
402 if(!can_remove_file(lp
->data
, NULL
)) {
403 _alpm_log(PM_LOG_DEBUG
, "not removing package '%s', can't remove all files\n",
405 RET_ERR(PM_ERR_PKG_CANT_REMOVE
, -1);
409 filenum
= alpm_list_count(files
);
410 _alpm_log(PM_LOG_DEBUG
, "removing %ld files\n", (unsigned long)filenum
);
412 /* init progress bar */
413 PROGRESS(trans
, PM_TRANS_PROGRESS_REMOVE_START
, info
->name
, 0,
414 pkg_count
, (pkg_count
- targcount
+ 1));
416 /* iterate through the list backwards, unlinking files */
417 newfiles
= alpm_list_reverse(files
);
418 for(lp
= newfiles
; lp
; lp
= alpm_list_next(lp
)) {
420 unlink_file(info
, lp
->data
, NULL
, trans
->flags
& PM_TRANS_FLAG_NOSAVE
);
422 /* update progress bar after each file */
423 percent
= (position
* 100) / filenum
;
424 PROGRESS(trans
, PM_TRANS_PROGRESS_REMOVE_START
, info
->name
,
425 percent
, pkg_count
, (pkg_count
- targcount
+ 1));
428 alpm_list_free(newfiles
);
431 /* set progress to 100% after we finish unlinking files */
432 PROGRESS(trans
, PM_TRANS_PROGRESS_REMOVE_START
, pkgname
, 100,
433 pkg_count
, (pkg_count
- targcount
+ 1));
435 /* run the post-remove script if it exists */
436 if(alpm_pkg_has_scriptlet(info
) && !(trans
->flags
& PM_TRANS_FLAG_NOSCRIPTLET
)) {
437 _alpm_runscriptlet(handle
->root
, scriptlet
, "post_remove",
438 alpm_pkg_get_version(info
), NULL
, trans
);
441 /* remove the package from the database */
442 _alpm_log(PM_LOG_DEBUG
, "updating database\n");
443 _alpm_log(PM_LOG_DEBUG
, "removing database entry '%s'\n", pkgname
);
444 if(_alpm_local_db_remove(db
, info
) == -1) {
445 _alpm_log(PM_LOG_ERROR
, _("could not remove database entry %s-%s\n"),
446 pkgname
, alpm_pkg_get_version(info
));
448 /* remove the package from the cache */
449 if(_alpm_db_remove_pkgfromcache(db
, info
) == -1) {
450 _alpm_log(PM_LOG_ERROR
, _("could not remove entry '%s' from cache\n"),
454 EVENT(trans
, PM_TRANS_EVT_REMOVE_DONE
, info
, NULL
);
457 /* run ldconfig if it exists */
458 _alpm_ldconfig(handle
->root
);
463 /* vim: set ts=2 sw=2 noet: */