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"
48 int SYMEXPORT
alpm_remove_pkg(alpm_handle_t
*handle
, alpm_pkg_t
*pkg
)
55 CHECK_HANDLE(handle
, return -1);
56 ASSERT(pkg
!= NULL
, RET_ERR(handle
, ALPM_ERR_WRONG_ARGS
, -1));
57 ASSERT(handle
== pkg
->handle
, RET_ERR(handle
, ALPM_ERR_WRONG_ARGS
, -1));
58 trans
= handle
->trans
;
59 ASSERT(trans
!= NULL
, RET_ERR(handle
, ALPM_ERR_TRANS_NULL
, -1));
60 ASSERT(trans
->state
== STATE_INITIALIZED
,
61 RET_ERR(handle
, ALPM_ERR_TRANS_NOT_INITIALIZED
, -1));
65 if(_alpm_pkg_find(trans
->remove
, pkgname
)) {
66 RET_ERR(handle
, ALPM_ERR_TRANS_DUP_TARGET
, -1);
69 _alpm_log(handle
, ALPM_LOG_DEBUG
, "adding package %s to the transaction remove list\n",
71 if(_alpm_pkg_dup(pkg
, ©
) == -1) {
74 trans
->remove
= alpm_list_add(trans
->remove
, copy
);
78 static int remove_prepare_cascade(alpm_handle_t
*handle
, alpm_list_t
*lp
)
80 alpm_trans_t
*trans
= handle
->trans
;
84 for(i
= lp
; i
; i
= i
->next
) {
85 alpm_depmissing_t
*miss
= i
->data
;
86 alpm_pkg_t
*info
= _alpm_db_get_pkgfromcache(handle
->db_local
, miss
->target
);
89 if(!_alpm_pkg_find(trans
->remove
, info
->name
)) {
90 _alpm_log(handle
, ALPM_LOG_DEBUG
, "pulling %s in target list\n",
92 if(_alpm_pkg_dup(info
, ©
) == -1) {
95 trans
->remove
= alpm_list_add(trans
->remove
, copy
);
98 _alpm_log(handle
, ALPM_LOG_ERROR
,
99 _("could not find %s in database -- skipping\n"), miss
->target
);
102 alpm_list_free_inner(lp
, (alpm_list_fn_free
)_alpm_depmiss_free
);
104 lp
= alpm_checkdeps(handle
, _alpm_db_get_pkgcache(handle
->db_local
),
105 trans
->remove
, NULL
, 1);
110 static void remove_prepare_keep_needed(alpm_handle_t
*handle
, alpm_list_t
*lp
)
112 alpm_trans_t
*trans
= handle
->trans
;
114 /* Remove needed packages (which break dependencies) from target list */
117 for(i
= lp
; i
; i
= i
->next
) {
118 alpm_depmissing_t
*miss
= i
->data
;
120 alpm_pkg_t
*pkg
= _alpm_pkg_find(trans
->remove
, miss
->causingpkg
);
124 trans
->remove
= alpm_list_remove(trans
->remove
, pkg
, _alpm_pkg_cmp
,
128 _alpm_log(handle
, ALPM_LOG_WARNING
, _("removing %s from target list\n"),
133 alpm_list_free_inner(lp
, (alpm_list_fn_free
)_alpm_depmiss_free
);
135 lp
= alpm_checkdeps(handle
, _alpm_db_get_pkgcache(handle
->db_local
),
136 trans
->remove
, NULL
, 1);
140 /** Transaction preparation for remove actions.
141 * This functions takes a pointer to a alpm_list_t which will be
142 * filled with a list of alpm_depmissing_t* objects representing
143 * the packages blocking the transaction.
144 * @param handle the context handle
145 * @param data a pointer to an alpm_list_t* to fill
146 * @return 0 on success, -1 on error
148 int _alpm_remove_prepare(alpm_handle_t
*handle
, alpm_list_t
**data
)
151 alpm_trans_t
*trans
= handle
->trans
;
152 alpm_db_t
*db
= handle
->db_local
;
154 if((trans
->flags
& ALPM_TRANS_FLAG_RECURSE
)
155 && !(trans
->flags
& ALPM_TRANS_FLAG_CASCADE
)) {
156 _alpm_log(handle
, ALPM_LOG_DEBUG
, "finding removable dependencies\n");
157 if(_alpm_recursedeps(db
, trans
->remove
,
158 trans
->flags
& ALPM_TRANS_FLAG_RECURSEALL
)) {
163 if(!(trans
->flags
& ALPM_TRANS_FLAG_NODEPS
)) {
164 EVENT(handle
, ALPM_EVENT_CHECKDEPS_START
, NULL
, NULL
);
166 _alpm_log(handle
, ALPM_LOG_DEBUG
, "looking for unsatisfied dependencies\n");
167 lp
= alpm_checkdeps(handle
, _alpm_db_get_pkgcache(db
), trans
->remove
, NULL
, 1);
170 if(trans
->flags
& ALPM_TRANS_FLAG_CASCADE
) {
171 if(remove_prepare_cascade(handle
, lp
)) {
174 } else if(trans
->flags
& ALPM_TRANS_FLAG_UNNEEDED
) {
175 /* Remove needed packages (which would break dependencies)
176 * from target list */
177 remove_prepare_keep_needed(handle
, lp
);
182 alpm_list_free_inner(lp
, (alpm_list_fn_free
)_alpm_depmiss_free
);
185 RET_ERR(handle
, ALPM_ERR_UNSATISFIED_DEPS
, -1);
190 /* re-order w.r.t. dependencies */
191 _alpm_log(handle
, ALPM_LOG_DEBUG
, "sorting by dependencies\n");
192 lp
= _alpm_sortbydeps(handle
, trans
->remove
, 1);
193 /* free the old alltargs */
194 alpm_list_free(trans
->remove
);
197 /* -Rcs == -Rc then -Rs */
198 if((trans
->flags
& ALPM_TRANS_FLAG_CASCADE
)
199 && (trans
->flags
& ALPM_TRANS_FLAG_RECURSE
)) {
200 _alpm_log(handle
, ALPM_LOG_DEBUG
, "finding removable dependencies\n");
201 if(_alpm_recursedeps(db
, trans
->remove
,
202 trans
->flags
& ALPM_TRANS_FLAG_RECURSEALL
)) {
207 if(!(trans
->flags
& ALPM_TRANS_FLAG_NODEPS
)) {
208 EVENT(handle
, ALPM_EVENT_CHECKDEPS_DONE
, NULL
, NULL
);
214 static int can_remove_file(alpm_handle_t
*handle
, const alpm_file_t
*file
,
215 alpm_list_t
*skip_remove
)
217 char filepath
[PATH_MAX
];
219 if(alpm_list_find(skip_remove
, file
->name
, _alpm_fnmatch
)) {
220 /* return success because we will never actually remove this file */
224 snprintf(filepath
, PATH_MAX
, "%s%s", handle
->root
, file
->name
);
225 /* If we fail write permissions due to a read-only filesystem, abort.
226 * Assume all other possible failures are covered somewhere else */
227 if(_alpm_access(handle
, NULL
, filepath
, W_OK
) == -1) {
228 if(errno
!= EACCES
&& errno
!= ETXTBSY
&& access(filepath
, F_OK
) == 0) {
229 /* only return failure if the file ACTUALLY exists and we can't write to
230 * it - ignore "chmod -w" simple permission failures */
231 _alpm_log(handle
, ALPM_LOG_ERROR
, _("cannot remove file '%s': %s\n"),
232 filepath
, strerror(errno
));
240 /* Helper function for iterating through a package's file and deleting them
241 * Used by _alpm_remove_commit. */
242 static int unlink_file(alpm_handle_t
*handle
, alpm_pkg_t
*oldpkg
,
243 alpm_pkg_t
*newpkg
, const alpm_file_t
*fileobj
, alpm_list_t
*skip_remove
,
249 snprintf(file
, PATH_MAX
, "%s%s", handle
->root
, fileobj
->name
);
251 /* check the remove skip list before removing the file.
252 * see the big comment block in db_find_fileconflicts() for an
254 if(alpm_list_find(skip_remove
, fileobj
->name
, _alpm_fnmatch
)) {
255 _alpm_log(handle
, ALPM_LOG_DEBUG
,
256 "%s is in skip_remove, skipping removal\n", file
);
260 /* we want to do a lstat here, and not a _alpm_lstat.
261 * if a directory in the package is actually a directory symlink on the
262 * filesystem, we want to work with the linked directory instead of the
264 if(lstat(file
, &buf
)) {
265 _alpm_log(handle
, ALPM_LOG_DEBUG
, "file %s does not exist\n", file
);
269 if(S_ISDIR(buf
.st_mode
)) {
270 ssize_t files
= _alpm_files_in_directory(handle
, file
, 0);
271 /* if we have files, no need to remove the directory */
273 _alpm_log(handle
, ALPM_LOG_DEBUG
, "keeping directory %s (contains files)\n",
275 } else if(files
< 0) {
276 _alpm_log(handle
, ALPM_LOG_DEBUG
,
277 "keeping directory %s (could not count files)\n", file
);
278 } else if(newpkg
&& _alpm_filelist_contains(alpm_pkg_get_files(newpkg
),
280 _alpm_log(handle
, ALPM_LOG_DEBUG
,
281 "keeping directory %s (in new package)\n", file
);
283 /* one last check- does any other package own this file? */
284 alpm_list_t
*local
, *local_pkgs
;
286 local_pkgs
= _alpm_db_get_pkgcache(handle
->db_local
);
287 for(local
= local_pkgs
; local
&& !found
; local
= local
->next
) {
288 alpm_pkg_t
*local_pkg
= local
->data
;
289 alpm_filelist_t
*filelist
;
291 /* we duplicated the package when we put it in the removal list, so we
292 * so we can't use direct pointer comparison here. */
293 if(oldpkg
->name_hash
== local_pkg
->name_hash
294 && strcmp(oldpkg
->name
, local_pkg
->name
) == 0) {
297 filelist
= alpm_pkg_get_files(local_pkg
);
298 if(_alpm_filelist_contains(filelist
, fileobj
->name
)) {
299 _alpm_log(handle
, ALPM_LOG_DEBUG
,
300 "keeping directory %s (owned by %s)\n", file
, local_pkg
->name
);
306 _alpm_log(handle
, ALPM_LOG_DEBUG
,
307 "directory removal of %s failed: %s\n", file
, strerror(errno
));
310 _alpm_log(handle
, ALPM_LOG_DEBUG
,
311 "removed directory %s (no remaining owners)\n", file
);
316 /* if the file needs backup and has been modified, back it up to .pacsave */
317 alpm_backup_t
*backup
= _alpm_needbackup(fileobj
->name
, oldpkg
);
320 _alpm_log(handle
, ALPM_LOG_DEBUG
, "transaction is set to NOSAVE, not backing up '%s'\n", file
);
322 char *filehash
= alpm_compute_md5sum(file
);
323 int cmp
= filehash
? strcmp(filehash
, backup
->hash
) : 0;
327 size_t len
= strlen(file
) + 8 + 1;
328 MALLOC(newpath
, len
, RET_ERR(handle
, ALPM_ERR_MEMORY
, -1));
329 snprintf(newpath
, len
, "%s.pacsave", file
);
330 if(rename(file
, newpath
)) {
331 _alpm_log(handle
, ALPM_LOG_ERROR
, _("could not rename %s to %s (%s)\n"),
332 file
, newpath
, strerror(errno
));
333 alpm_logaction(handle
, "error: could not rename %s to %s (%s)\n",
334 file
, newpath
, strerror(errno
));
338 _alpm_log(handle
, ALPM_LOG_WARNING
, _("%s saved as %s\n"), file
, newpath
);
339 alpm_logaction(handle
, "warning: %s saved as %s\n", file
, newpath
);
346 _alpm_log(handle
, ALPM_LOG_DEBUG
, "unlinking %s\n", file
);
348 if(unlink(file
) == -1) {
349 _alpm_log(handle
, ALPM_LOG_ERROR
, _("cannot remove %s (%s)\n"),
350 file
, strerror(errno
));
351 alpm_logaction(handle
, "error: cannot remove %s (%s)\n",
352 file
, strerror(errno
));
359 int _alpm_remove_single_package(alpm_handle_t
*handle
,
360 alpm_pkg_t
*oldpkg
, alpm_pkg_t
*newpkg
,
361 size_t targ_count
, size_t pkg_count
)
363 alpm_list_t
*skip_remove
;
364 size_t filenum
= 0, position
= 0;
365 const char *pkgname
= oldpkg
->name
;
366 const char *pkgver
= oldpkg
->version
;
367 alpm_filelist_t
*filelist
;
371 _alpm_log(handle
, ALPM_LOG_DEBUG
, "removing old package first (%s-%s)\n",
374 EVENT(handle
, ALPM_EVENT_REMOVE_START
, oldpkg
, NULL
);
375 _alpm_log(handle
, ALPM_LOG_DEBUG
, "removing package %s-%s\n",
378 /* run the pre-remove scriptlet if it exists */
379 if(alpm_pkg_has_scriptlet(oldpkg
) &&
380 !(handle
->trans
->flags
& ALPM_TRANS_FLAG_NOSCRIPTLET
)) {
381 char *scriptlet
= _alpm_local_db_pkgpath(handle
->db_local
,
383 _alpm_runscriptlet(handle
, scriptlet
, "pre_remove", pkgver
, NULL
, 0);
388 if(handle
->trans
->flags
& ALPM_TRANS_FLAG_DBONLY
) {
393 alpm_filelist_t
*newfiles
;
395 skip_remove
= alpm_list_join(
396 alpm_list_strdup(handle
->trans
->skip_remove
),
397 alpm_list_strdup(handle
->noupgrade
));
398 /* Add files in the NEW backup array to the skip_remove array
399 * so this removal operation doesn't kill them */
400 /* old package backup list */
401 newfiles
= alpm_pkg_get_files(newpkg
);
402 for(b
= alpm_pkg_get_backup(newpkg
); b
; b
= b
->next
) {
403 const alpm_backup_t
*backup
= b
->data
;
404 /* safety check (fix the upgrade026 pactest) */
405 if(!_alpm_filelist_contains(newfiles
, backup
->name
)) {
408 _alpm_log(handle
, ALPM_LOG_DEBUG
, "adding %s to the skip_remove array\n",
410 skip_remove
= alpm_list_add(skip_remove
, strdup(backup
->name
));
413 skip_remove
= alpm_list_strdup(handle
->trans
->skip_remove
);
416 filelist
= alpm_pkg_get_files(oldpkg
);
417 for(i
= 0; i
< filelist
->count
; i
++) {
418 alpm_file_t
*file
= filelist
->files
+ i
;
419 if(!can_remove_file(handle
, file
, skip_remove
)) {
420 _alpm_log(handle
, ALPM_LOG_DEBUG
,
421 "not removing package '%s', can't remove all files\n", pkgname
);
422 RET_ERR(handle
, ALPM_ERR_PKG_CANT_REMOVE
, -1);
427 _alpm_log(handle
, ALPM_LOG_DEBUG
, "removing %ld files\n", (unsigned long)filenum
);
430 /* init progress bar, but only on true remove transactions */
431 PROGRESS(handle
, ALPM_PROGRESS_REMOVE_START
, pkgname
, 0,
432 pkg_count
, targ_count
);
435 /* iterate through the list backwards, unlinking files */
436 for(i
= filelist
->count
; i
> 0; i
--) {
437 alpm_file_t
*file
= filelist
->files
+ i
- 1;
439 /* TODO: check return code and handle accordingly */
440 unlink_file(handle
, oldpkg
, newpkg
, file
, skip_remove
,
441 handle
->trans
->flags
& ALPM_TRANS_FLAG_NOSAVE
);
444 /* update progress bar after each file */
445 percent
= (position
* 100) / filenum
;
446 PROGRESS(handle
, ALPM_PROGRESS_REMOVE_START
, pkgname
,
447 percent
, pkg_count
, targ_count
);
451 FREELIST(skip_remove
);
454 /* set progress to 100% after we finish unlinking files */
455 PROGRESS(handle
, ALPM_PROGRESS_REMOVE_START
, pkgname
, 100,
456 pkg_count
, targ_count
);
458 /* run the post-remove script if it exists */
459 if(alpm_pkg_has_scriptlet(oldpkg
) &&
460 !(handle
->trans
->flags
& ALPM_TRANS_FLAG_NOSCRIPTLET
)) {
461 char *scriptlet
= _alpm_local_db_pkgpath(handle
->db_local
,
463 _alpm_runscriptlet(handle
, scriptlet
, "post_remove", pkgver
, NULL
, 0);
469 /* remove the package from the database */
470 _alpm_log(handle
, ALPM_LOG_DEBUG
, "updating database\n");
471 _alpm_log(handle
, ALPM_LOG_DEBUG
, "removing database entry '%s'\n", pkgname
);
472 if(_alpm_local_db_remove(handle
->db_local
, oldpkg
) == -1) {
473 _alpm_log(handle
, ALPM_LOG_ERROR
, _("could not remove database entry %s-%s\n"),
476 /* remove the package from the cache */
477 if(_alpm_db_remove_pkgfromcache(handle
->db_local
, oldpkg
) == -1) {
478 _alpm_log(handle
, ALPM_LOG_ERROR
, _("could not remove entry '%s' from cache\n"),
483 /* TODO: awesome! we're passing invalid pointers. */
484 EVENT(handle
, ALPM_EVENT_REMOVE_DONE
, oldpkg
, NULL
);
490 int _alpm_remove_packages(alpm_handle_t
*handle
, int run_ldconfig
)
493 size_t pkg_count
, targ_count
;
494 alpm_trans_t
*trans
= handle
->trans
;
497 pkg_count
= alpm_list_count(trans
->remove
);
500 for(targ
= trans
->remove
; targ
; targ
= targ
->next
) {
501 alpm_pkg_t
*pkg
= targ
->data
;
503 if(trans
->state
== STATE_INTERRUPTED
) {
507 if(_alpm_remove_single_package(handle
, pkg
, NULL
,
508 targ_count
, pkg_count
) == -1) {
509 handle
->pm_errno
= ALPM_ERR_TRANS_ABORT
;
510 /* running ldconfig at this point could possibly screw system */
519 /* run ldconfig if it exists */
520 _alpm_ldconfig(handle
);
526 /* vim: set ts=2 sw=2 noet: */