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(pmhandle_t
*handle
, pmpkg_t
*pkg
)
53 CHECK_HANDLE(handle
, return -1);
54 ASSERT(pkg
!= NULL
, RET_ERR(handle
, PM_ERR_WRONG_ARGS
, -1));
55 ASSERT(handle
== pkg
->handle
, RET_ERR(handle
, PM_ERR_WRONG_ARGS
, -1));
56 trans
= handle
->trans
;
57 ASSERT(trans
!= NULL
, RET_ERR(handle
, PM_ERR_TRANS_NULL
, -1));
58 ASSERT(trans
->state
== STATE_INITIALIZED
,
59 RET_ERR(handle
, PM_ERR_TRANS_NOT_INITIALIZED
, -1));
63 if(_alpm_pkg_find(trans
->remove
, pkgname
)) {
64 RET_ERR(handle
, PM_ERR_TRANS_DUP_TARGET
, -1);
67 _alpm_log(handle
, PM_LOG_DEBUG
, "adding package %s to the transaction remove list\n",
69 trans
->remove
= alpm_list_add(trans
->remove
, _alpm_pkg_dup(pkg
));
73 static void remove_prepare_cascade(pmhandle_t
*handle
, alpm_list_t
*lp
)
75 pmtrans_t
*trans
= handle
->trans
;
79 for(i
= lp
; i
; i
= i
->next
) {
80 pmdepmissing_t
*miss
= (pmdepmissing_t
*)i
->data
;
81 pmpkg_t
*info
= _alpm_db_get_pkgfromcache(handle
->db_local
, miss
->target
);
83 if(!_alpm_pkg_find(trans
->remove
, alpm_pkg_get_name(info
))) {
84 _alpm_log(handle
, PM_LOG_DEBUG
, "pulling %s in target list\n",
85 alpm_pkg_get_name(info
));
86 trans
->remove
= alpm_list_add(trans
->remove
, _alpm_pkg_dup(info
));
89 _alpm_log(handle
, PM_LOG_ERROR
, _("could not find %s in database -- skipping\n"),
93 alpm_list_free_inner(lp
, (alpm_list_fn_free
)_alpm_depmiss_free
);
95 lp
= alpm_checkdeps(handle
, _alpm_db_get_pkgcache(handle
->db_local
),
96 trans
->remove
, NULL
, 1);
100 static void remove_prepare_keep_needed(pmhandle_t
*handle
, alpm_list_t
*lp
)
102 pmtrans_t
*trans
= handle
->trans
;
104 /* Remove needed packages (which break dependencies) from target list */
107 for(i
= lp
; i
; i
= i
->next
) {
108 pmdepmissing_t
*miss
= (pmdepmissing_t
*)i
->data
;
110 pmpkg_t
*pkg
= _alpm_pkg_find(trans
->remove
, miss
->causingpkg
);
114 trans
->remove
= alpm_list_remove(trans
->remove
, pkg
, _alpm_pkg_cmp
,
118 _alpm_log(handle
, PM_LOG_WARNING
, _("removing %s from target list\n"),
119 alpm_pkg_get_name(pkg
));
123 alpm_list_free_inner(lp
, (alpm_list_fn_free
)_alpm_depmiss_free
);
125 lp
= alpm_checkdeps(handle
, _alpm_db_get_pkgcache(handle
->db_local
),
126 trans
->remove
, NULL
, 1);
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 handle the context handle
135 * @param data a pointer to an alpm_list_t* to fill
137 int _alpm_remove_prepare(pmhandle_t
*handle
, alpm_list_t
**data
)
140 pmtrans_t
*trans
= handle
->trans
;
141 pmdb_t
*db
= handle
->db_local
;
143 if((trans
->flags
& PM_TRANS_FLAG_RECURSE
) && !(trans
->flags
& PM_TRANS_FLAG_CASCADE
)) {
144 _alpm_log(handle
, PM_LOG_DEBUG
, "finding removable dependencies\n");
145 _alpm_recursedeps(db
, trans
->remove
,
146 trans
->flags
& PM_TRANS_FLAG_RECURSEALL
);
149 if(!(trans
->flags
& PM_TRANS_FLAG_NODEPS
)) {
150 EVENT(trans
, PM_TRANS_EVT_CHECKDEPS_START
, NULL
, NULL
);
152 _alpm_log(handle
, PM_LOG_DEBUG
, "looking for unsatisfied dependencies\n");
153 lp
= alpm_checkdeps(handle
, _alpm_db_get_pkgcache(db
), trans
->remove
, NULL
, 1);
156 if(trans
->flags
& PM_TRANS_FLAG_CASCADE
) {
157 remove_prepare_cascade(handle
, lp
);
158 } else if(trans
->flags
& PM_TRANS_FLAG_UNNEEDED
) {
159 /* Remove needed packages (which would break dependencies)
160 * from target list */
161 remove_prepare_keep_needed(handle
, lp
);
166 alpm_list_free_inner(lp
, (alpm_list_fn_free
)_alpm_depmiss_free
);
169 RET_ERR(handle
, PM_ERR_UNSATISFIED_DEPS
, -1);
174 /* re-order w.r.t. dependencies */
175 _alpm_log(handle
, PM_LOG_DEBUG
, "sorting by dependencies\n");
176 lp
= _alpm_sortbydeps(handle
, trans
->remove
, 1);
177 /* free the old alltargs */
178 alpm_list_free(trans
->remove
);
181 /* -Rcs == -Rc then -Rs */
182 if((trans
->flags
& PM_TRANS_FLAG_CASCADE
) && (trans
->flags
& PM_TRANS_FLAG_RECURSE
)) {
183 _alpm_log(handle
, PM_LOG_DEBUG
, "finding removable dependencies\n");
184 _alpm_recursedeps(db
, trans
->remove
, trans
->flags
& PM_TRANS_FLAG_RECURSEALL
);
187 if(!(trans
->flags
& PM_TRANS_FLAG_NODEPS
)) {
188 EVENT(trans
, PM_TRANS_EVT_CHECKDEPS_DONE
, NULL
, NULL
);
194 static int can_remove_file(pmhandle_t
*handle
, const char *path
,
195 alpm_list_t
*skip_remove
)
199 snprintf(file
, PATH_MAX
, "%s%s", handle
->root
, path
);
201 if(alpm_list_find_str(skip_remove
, file
)) {
202 /* return success because we will never actually remove this file */
205 /* If we fail write permissions due to a read-only filesystem, abort.
206 * Assume all other possible failures are covered somewhere else */
207 if(access(file
, W_OK
) == -1) {
208 if(errno
!= EACCES
&& errno
!= ETXTBSY
&& access(file
, F_OK
) == 0) {
209 /* only return failure if the file ACTUALLY exists and we can't write to
210 * it - ignore "chmod -w" simple permission failures */
211 _alpm_log(handle
, PM_LOG_ERROR
, _("cannot remove file '%s': %s\n"),
212 file
, strerror(errno
));
220 /* Helper function for iterating through a package's file and deleting them
221 * Used by _alpm_remove_commit. */
222 static void unlink_file(pmhandle_t
*handle
, pmpkg_t
*info
, const char *filename
,
223 alpm_list_t
*skip_remove
, int nosave
)
228 snprintf(file
, PATH_MAX
, "%s%s", handle
->root
, filename
);
230 /* check the remove skip list before removing the file.
231 * see the big comment block in db_find_fileconflicts() for an
233 if(alpm_list_find_str(skip_remove
, filename
)) {
234 _alpm_log(handle
, PM_LOG_DEBUG
, "%s is in skip_remove, skipping removal\n",
239 /* we want to do a lstat here, and not a _alpm_lstat.
240 * if a directory in the package is actually a directory symlink on the
241 * filesystem, we want to work with the linked directory instead of the
243 if(lstat(file
, &buf
)) {
244 _alpm_log(handle
, PM_LOG_DEBUG
, "file %s does not exist\n", file
);
248 if(S_ISDIR(buf
.st_mode
)) {
250 /* this is okay, other packages are probably using it (like /usr) */
251 _alpm_log(handle
, PM_LOG_DEBUG
, "keeping directory %s\n", file
);
253 _alpm_log(handle
, PM_LOG_DEBUG
, "removing directory %s\n", file
);
256 /* if the file needs backup and has been modified, back it up to .pacsave */
257 pmbackup_t
*backup
= _alpm_needbackup(filename
, alpm_pkg_get_backup(info
));
260 _alpm_log(handle
, PM_LOG_DEBUG
, "transaction is set to NOSAVE, not backing up '%s'\n", file
);
262 char *filehash
= alpm_compute_md5sum(file
);
263 int cmp
= filehash
? strcmp(filehash
, backup
->hash
) : 0;
266 char newpath
[PATH_MAX
];
267 snprintf(newpath
, PATH_MAX
, "%s.pacsave", file
);
268 rename(file
, newpath
);
269 _alpm_log(handle
, PM_LOG_WARNING
, _("%s saved as %s\n"), file
, newpath
);
270 alpm_logaction(handle
, "warning: %s saved as %s\n", file
, newpath
);
276 _alpm_log(handle
, PM_LOG_DEBUG
, "unlinking %s\n", file
);
278 if(unlink(file
) == -1) {
279 _alpm_log(handle
, PM_LOG_ERROR
, _("cannot remove file '%s': %s\n"),
280 filename
, strerror(errno
));
285 int _alpm_upgraderemove_package(pmhandle_t
*handle
,
286 pmpkg_t
*oldpkg
, pmpkg_t
*newpkg
)
288 alpm_list_t
*skip_remove
, *b
;
289 alpm_list_t
*newfiles
, *lp
;
291 alpm_list_t
*files
= alpm_pkg_get_files(oldpkg
);
292 const char *pkgname
= alpm_pkg_get_name(oldpkg
);
294 _alpm_log(handle
, PM_LOG_DEBUG
, "removing old package first (%s-%s)\n",
295 oldpkg
->name
, oldpkg
->version
);
297 if(handle
->trans
->flags
& PM_TRANS_FLAG_DBONLY
) {
301 /* copy the remove skiplist over */
302 skip_remove
= alpm_list_join(
303 alpm_list_strdup(handle
->trans
->skip_remove
),
304 alpm_list_strdup(handle
->noupgrade
));
305 /* Add files in the NEW backup array to the skip_remove array
306 * so this removal operation doesn't kill them */
307 /* old package backup list */
308 alpm_list_t
*filelist
= alpm_pkg_get_files(newpkg
);
309 for(b
= alpm_pkg_get_backup(newpkg
); b
; b
= b
->next
) {
310 const pmbackup_t
*backup
= b
->data
;
311 /* safety check (fix the upgrade026 pactest) */
312 if(!alpm_list_find_str(filelist
, backup
->name
)) {
315 _alpm_log(handle
, PM_LOG_DEBUG
, "adding %s to the skip_remove array\n",
317 skip_remove
= alpm_list_add(skip_remove
, strdup(backup
->name
));
320 for(lp
= files
; lp
; lp
= lp
->next
) {
321 if(!can_remove_file(handle
, lp
->data
, skip_remove
)) {
322 _alpm_log(handle
, PM_LOG_DEBUG
,
323 "not removing package '%s', can't remove all files\n", pkgname
);
324 RET_ERR(handle
, PM_ERR_PKG_CANT_REMOVE
, -1);
329 _alpm_log(handle
, PM_LOG_DEBUG
, "removing %ld files\n", (unsigned long)filenum
);
331 /* iterate through the list backwards, unlinking files */
332 newfiles
= alpm_list_reverse(files
);
333 for(lp
= newfiles
; lp
; lp
= alpm_list_next(lp
)) {
334 unlink_file(handle
, oldpkg
, lp
->data
, skip_remove
, 0);
336 alpm_list_free(newfiles
);
337 FREELIST(skip_remove
);
340 /* remove the package from the database */
341 _alpm_log(handle
, PM_LOG_DEBUG
, "updating database\n");
342 _alpm_log(handle
, PM_LOG_DEBUG
, "removing database entry '%s'\n", pkgname
);
343 if(_alpm_local_db_remove(handle
->db_local
, oldpkg
) == -1) {
344 _alpm_log(handle
, PM_LOG_ERROR
, _("could not remove database entry %s-%s\n"),
345 pkgname
, alpm_pkg_get_version(oldpkg
));
347 /* remove the package from the cache */
348 if(_alpm_db_remove_pkgfromcache(handle
->db_local
, oldpkg
) == -1) {
349 _alpm_log(handle
, PM_LOG_ERROR
, _("could not remove entry '%s' from cache\n"),
356 int _alpm_remove_packages(pmhandle_t
*handle
)
359 alpm_list_t
*targ
, *lp
;
361 pmtrans_t
*trans
= handle
->trans
;
363 pkg_count
= alpm_list_count(trans
->remove
);
365 for(targ
= trans
->remove
; targ
; targ
= targ
->next
) {
367 char scriptlet
[PATH_MAX
];
368 info
= (pmpkg_t
*)targ
->data
;
369 const char *pkgname
= NULL
;
370 size_t targcount
= alpm_list_count(targ
);
372 if(trans
->state
== STATE_INTERRUPTED
) {
376 /* get the name now so we can use it after package is removed */
377 pkgname
= alpm_pkg_get_name(info
);
378 snprintf(scriptlet
, PATH_MAX
, "%s%s-%s/install",
379 _alpm_db_path(handle
->db_local
), pkgname
, alpm_pkg_get_version(info
));
381 EVENT(trans
, PM_TRANS_EVT_REMOVE_START
, info
, NULL
);
382 _alpm_log(handle
, PM_LOG_DEBUG
, "removing package %s-%s\n",
383 pkgname
, alpm_pkg_get_version(info
));
385 /* run the pre-remove scriptlet if it exists */
386 if(alpm_pkg_has_scriptlet(info
) && !(trans
->flags
& PM_TRANS_FLAG_NOSCRIPTLET
)) {
387 _alpm_runscriptlet(handle
, scriptlet
, "pre_remove",
388 alpm_pkg_get_version(info
), NULL
);
391 if(!(trans
->flags
& PM_TRANS_FLAG_DBONLY
)) {
392 alpm_list_t
*files
= alpm_pkg_get_files(info
);
393 alpm_list_t
*newfiles
;
396 for(lp
= files
; lp
; lp
= lp
->next
) {
397 if(!can_remove_file(handle
, lp
->data
, NULL
)) {
398 _alpm_log(handle
, PM_LOG_DEBUG
, "not removing package '%s', can't remove all files\n",
400 RET_ERR(handle
, PM_ERR_PKG_CANT_REMOVE
, -1);
405 _alpm_log(handle
, PM_LOG_DEBUG
, "removing %ld files\n", (unsigned long)filenum
);
407 /* init progress bar */
408 PROGRESS(trans
, PM_TRANS_PROGRESS_REMOVE_START
, info
->name
, 0,
409 pkg_count
, (pkg_count
- targcount
+ 1));
411 /* iterate through the list backwards, unlinking files */
412 newfiles
= alpm_list_reverse(files
);
413 for(lp
= newfiles
; lp
; lp
= alpm_list_next(lp
)) {
415 unlink_file(handle
, info
, lp
->data
, NULL
, trans
->flags
& PM_TRANS_FLAG_NOSAVE
);
417 /* update progress bar after each file */
418 percent
= (position
* 100) / filenum
;
419 PROGRESS(trans
, PM_TRANS_PROGRESS_REMOVE_START
, info
->name
,
420 percent
, pkg_count
, (pkg_count
- targcount
+ 1));
423 alpm_list_free(newfiles
);
426 /* set progress to 100% after we finish unlinking files */
427 PROGRESS(trans
, PM_TRANS_PROGRESS_REMOVE_START
, pkgname
, 100,
428 pkg_count
, (pkg_count
- targcount
+ 1));
430 /* run the post-remove script if it exists */
431 if(alpm_pkg_has_scriptlet(info
) && !(trans
->flags
& PM_TRANS_FLAG_NOSCRIPTLET
)) {
432 _alpm_runscriptlet(handle
, scriptlet
, "post_remove",
433 alpm_pkg_get_version(info
), NULL
);
436 /* remove the package from the database */
437 _alpm_log(handle
, PM_LOG_DEBUG
, "updating database\n");
438 _alpm_log(handle
, PM_LOG_DEBUG
, "removing database entry '%s'\n", pkgname
);
439 if(_alpm_local_db_remove(handle
->db_local
, info
) == -1) {
440 _alpm_log(handle
, PM_LOG_ERROR
, _("could not remove database entry %s-%s\n"),
441 pkgname
, alpm_pkg_get_version(info
));
443 /* remove the package from the cache */
444 if(_alpm_db_remove_pkgfromcache(handle
->db_local
, info
) == -1) {
445 _alpm_log(handle
, PM_LOG_ERROR
, _("could not remove entry '%s' from cache\n"),
449 EVENT(trans
, PM_TRANS_EVT_REMOVE_DONE
, info
, NULL
);
452 /* run ldconfig if it exists */
453 _alpm_ldconfig(handle
);
458 /* vim: set ts=2 sw=2 noet: */