4 * Copyright (c) 2006-2012 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/>.
34 #include "alpm_list.h"
46 int SYMEXPORT
alpm_remove_pkg(alpm_handle_t
*handle
, alpm_pkg_t
*pkg
)
53 CHECK_HANDLE(handle
, return -1);
54 ASSERT(pkg
!= NULL
, RET_ERR(handle
, ALPM_ERR_WRONG_ARGS
, -1));
55 ASSERT(handle
== pkg
->handle
, RET_ERR(handle
, ALPM_ERR_WRONG_ARGS
, -1));
56 trans
= handle
->trans
;
57 ASSERT(trans
!= NULL
, RET_ERR(handle
, ALPM_ERR_TRANS_NULL
, -1));
58 ASSERT(trans
->state
== STATE_INITIALIZED
,
59 RET_ERR(handle
, ALPM_ERR_TRANS_NOT_INITIALIZED
, -1));
63 if(_alpm_pkg_find(trans
->remove
, pkgname
)) {
64 RET_ERR(handle
, ALPM_ERR_TRANS_DUP_TARGET
, -1);
67 _alpm_log(handle
, ALPM_LOG_DEBUG
, "adding package %s to the transaction remove list\n",
69 if(_alpm_pkg_dup(pkg
, ©
) == -1) {
72 trans
->remove
= alpm_list_add(trans
->remove
, copy
);
76 static int remove_prepare_cascade(alpm_handle_t
*handle
, alpm_list_t
*lp
)
78 alpm_trans_t
*trans
= handle
->trans
;
82 for(i
= lp
; i
; i
= i
->next
) {
83 alpm_depmissing_t
*miss
= i
->data
;
84 alpm_pkg_t
*info
= _alpm_db_get_pkgfromcache(handle
->db_local
, miss
->target
);
87 if(!_alpm_pkg_find(trans
->remove
, info
->name
)) {
88 _alpm_log(handle
, ALPM_LOG_DEBUG
, "pulling %s in target list\n",
90 if(_alpm_pkg_dup(info
, ©
) == -1) {
93 trans
->remove
= alpm_list_add(trans
->remove
, copy
);
96 _alpm_log(handle
, ALPM_LOG_ERROR
,
97 _("could not find %s in database -- skipping\n"), miss
->target
);
100 alpm_list_free_inner(lp
, (alpm_list_fn_free
)_alpm_depmiss_free
);
102 lp
= alpm_checkdeps(handle
, _alpm_db_get_pkgcache(handle
->db_local
),
103 trans
->remove
, NULL
, 1);
108 static void remove_prepare_keep_needed(alpm_handle_t
*handle
, alpm_list_t
*lp
)
110 alpm_trans_t
*trans
= handle
->trans
;
112 /* Remove needed packages (which break dependencies) from target list */
115 for(i
= lp
; i
; i
= i
->next
) {
116 alpm_depmissing_t
*miss
= i
->data
;
118 alpm_pkg_t
*pkg
= _alpm_pkg_find(trans
->remove
, miss
->causingpkg
);
122 trans
->remove
= alpm_list_remove(trans
->remove
, pkg
, _alpm_pkg_cmp
,
126 _alpm_log(handle
, ALPM_LOG_WARNING
, _("removing %s from target list\n"),
131 alpm_list_free_inner(lp
, (alpm_list_fn_free
)_alpm_depmiss_free
);
133 lp
= alpm_checkdeps(handle
, _alpm_db_get_pkgcache(handle
->db_local
),
134 trans
->remove
, NULL
, 1);
138 /** Transaction preparation for remove actions.
139 * This functions takes a pointer to a alpm_list_t which will be
140 * filled with a list of alpm_depmissing_t* objects representing
141 * the packages blocking the transaction.
142 * @param handle the context handle
143 * @param data a pointer to an alpm_list_t* to fill
144 * @return 0 on success, -1 on error
146 int _alpm_remove_prepare(alpm_handle_t
*handle
, alpm_list_t
**data
)
149 alpm_trans_t
*trans
= handle
->trans
;
150 alpm_db_t
*db
= handle
->db_local
;
152 if((trans
->flags
& ALPM_TRANS_FLAG_RECURSE
)
153 && !(trans
->flags
& ALPM_TRANS_FLAG_CASCADE
)) {
154 _alpm_log(handle
, ALPM_LOG_DEBUG
, "finding removable dependencies\n");
155 if(_alpm_recursedeps(db
, trans
->remove
,
156 trans
->flags
& ALPM_TRANS_FLAG_RECURSEALL
)) {
161 if(!(trans
->flags
& ALPM_TRANS_FLAG_NODEPS
)) {
162 EVENT(handle
, ALPM_EVENT_CHECKDEPS_START
, NULL
, NULL
);
164 _alpm_log(handle
, ALPM_LOG_DEBUG
, "looking for unsatisfied dependencies\n");
165 lp
= alpm_checkdeps(handle
, _alpm_db_get_pkgcache(db
), trans
->remove
, NULL
, 1);
168 if(trans
->flags
& ALPM_TRANS_FLAG_CASCADE
) {
169 if(remove_prepare_cascade(handle
, lp
)) {
172 } else if(trans
->flags
& ALPM_TRANS_FLAG_UNNEEDED
) {
173 /* Remove needed packages (which would break dependencies)
174 * from target list */
175 remove_prepare_keep_needed(handle
, lp
);
180 alpm_list_free_inner(lp
, (alpm_list_fn_free
)_alpm_depmiss_free
);
183 RET_ERR(handle
, ALPM_ERR_UNSATISFIED_DEPS
, -1);
188 /* re-order w.r.t. dependencies */
189 _alpm_log(handle
, ALPM_LOG_DEBUG
, "sorting by dependencies\n");
190 lp
= _alpm_sortbydeps(handle
, trans
->remove
, 1);
191 /* free the old alltargs */
192 alpm_list_free(trans
->remove
);
195 /* -Rcs == -Rc then -Rs */
196 if((trans
->flags
& ALPM_TRANS_FLAG_CASCADE
)
197 && (trans
->flags
& ALPM_TRANS_FLAG_RECURSE
)) {
198 _alpm_log(handle
, ALPM_LOG_DEBUG
, "finding removable dependencies\n");
199 if(_alpm_recursedeps(db
, trans
->remove
,
200 trans
->flags
& ALPM_TRANS_FLAG_RECURSEALL
)) {
205 if(!(trans
->flags
& ALPM_TRANS_FLAG_NODEPS
)) {
206 EVENT(handle
, ALPM_EVENT_CHECKDEPS_DONE
, NULL
, NULL
);
212 static int can_remove_file(alpm_handle_t
*handle
, const alpm_file_t
*file
,
213 alpm_list_t
*skip_remove
)
215 char filepath
[PATH_MAX
];
217 if(alpm_list_find(skip_remove
, file
->name
, _alpm_fnmatch
)) {
218 /* return success because we will never actually remove this file */
222 snprintf(filepath
, PATH_MAX
, "%s%s", handle
->root
, file
->name
);
223 /* If we fail write permissions due to a read-only filesystem, abort.
224 * Assume all other possible failures are covered somewhere else */
225 if(_alpm_access(handle
, NULL
, filepath
, W_OK
) == -1) {
226 if(errno
!= EACCES
&& errno
!= ETXTBSY
&& access(filepath
, F_OK
) == 0) {
227 /* only return failure if the file ACTUALLY exists and we can't write to
228 * it - ignore "chmod -w" simple permission failures */
229 _alpm_log(handle
, ALPM_LOG_ERROR
, _("cannot remove file '%s': %s\n"),
230 filepath
, strerror(errno
));
238 /* Helper function for iterating through a package's file and deleting them
239 * Used by _alpm_remove_commit. */
240 static int unlink_file(alpm_handle_t
*handle
, alpm_pkg_t
*oldpkg
,
241 alpm_pkg_t
*newpkg
, const alpm_file_t
*fileobj
, alpm_list_t
*skip_remove
,
247 snprintf(file
, PATH_MAX
, "%s%s", handle
->root
, fileobj
->name
);
249 /* check the remove skip list before removing the file.
250 * see the big comment block in db_find_fileconflicts() for an
252 if(alpm_list_find(skip_remove
, fileobj
->name
, _alpm_fnmatch
)) {
253 _alpm_log(handle
, ALPM_LOG_DEBUG
,
254 "%s is in skip_remove, skipping removal\n", file
);
258 /* we want to do a lstat here, and not a _alpm_lstat.
259 * if a directory in the package is actually a directory symlink on the
260 * filesystem, we want to work with the linked directory instead of the
262 if(lstat(file
, &buf
)) {
263 _alpm_log(handle
, ALPM_LOG_DEBUG
, "file %s does not exist\n", file
);
267 if(S_ISDIR(buf
.st_mode
)) {
268 ssize_t files
= _alpm_files_in_directory(handle
, file
, 0);
269 /* if we have files, no need to remove the directory */
271 _alpm_log(handle
, ALPM_LOG_DEBUG
, "keeping directory %s (contains files)\n",
273 } else if(files
< 0) {
274 _alpm_log(handle
, ALPM_LOG_DEBUG
,
275 "keeping directory %s (could not count files)\n", file
);
276 } else if(newpkg
&& _alpm_filelist_contains(alpm_pkg_get_files(newpkg
),
278 _alpm_log(handle
, ALPM_LOG_DEBUG
,
279 "keeping directory %s (in new package)\n", file
);
281 /* one last check- does any other package own this file? */
282 alpm_list_t
*local
, *local_pkgs
;
284 local_pkgs
= _alpm_db_get_pkgcache(handle
->db_local
);
285 for(local
= local_pkgs
; local
&& !found
; local
= local
->next
) {
286 alpm_pkg_t
*local_pkg
= local
->data
;
287 alpm_filelist_t
*filelist
;
289 /* we duplicated the package when we put it in the removal list, so we
290 * so we can't use direct pointer comparison here. */
291 if(oldpkg
->name_hash
== local_pkg
->name_hash
292 && strcmp(oldpkg
->name
, local_pkg
->name
) == 0) {
295 filelist
= alpm_pkg_get_files(local_pkg
);
296 if(_alpm_filelist_contains(filelist
, fileobj
->name
)) {
297 _alpm_log(handle
, ALPM_LOG_DEBUG
,
298 "keeping directory %s (owned by %s)\n", file
, local_pkg
->name
);
304 _alpm_log(handle
, ALPM_LOG_DEBUG
,
305 "directory removal of %s failed: %s\n", file
, strerror(errno
));
308 _alpm_log(handle
, ALPM_LOG_DEBUG
,
309 "removed directory %s (no remaining owners)\n", file
);
314 /* if the file needs backup and has been modified, back it up to .pacsave */
315 alpm_backup_t
*backup
= _alpm_needbackup(fileobj
->name
, oldpkg
);
318 _alpm_log(handle
, ALPM_LOG_DEBUG
, "transaction is set to NOSAVE, not backing up '%s'\n", file
);
320 char *filehash
= alpm_compute_md5sum(file
);
321 int cmp
= filehash
? strcmp(filehash
, backup
->hash
) : 0;
325 size_t len
= strlen(file
) + 8 + 1;
326 MALLOC(newpath
, len
, RET_ERR(handle
, ALPM_ERR_MEMORY
, -1));
327 snprintf(newpath
, len
, "%s.pacsave", file
);
328 if(rename(file
, newpath
)) {
329 _alpm_log(handle
, ALPM_LOG_ERROR
, _("could not rename %s to %s (%s)\n"),
330 file
, newpath
, strerror(errno
));
331 alpm_logaction(handle
, "error: could not rename %s to %s (%s)\n",
332 file
, newpath
, strerror(errno
));
336 _alpm_log(handle
, ALPM_LOG_WARNING
, _("%s saved as %s\n"), file
, newpath
);
337 alpm_logaction(handle
, "warning: %s saved as %s\n", file
, newpath
);
344 _alpm_log(handle
, ALPM_LOG_DEBUG
, "unlinking %s\n", file
);
346 if(unlink(file
) == -1) {
347 _alpm_log(handle
, ALPM_LOG_ERROR
, _("cannot remove %s (%s)\n"),
348 file
, strerror(errno
));
349 alpm_logaction(handle
, "error: cannot remove %s (%s)\n",
350 file
, strerror(errno
));
357 static int remove_package_files(alpm_handle_t
*handle
,
358 alpm_pkg_t
*oldpkg
, alpm_pkg_t
*newpkg
,
359 size_t targ_count
, size_t pkg_count
)
361 alpm_list_t
*skip_remove
;
362 alpm_filelist_t
*filelist
;
365 int nosave
= handle
->trans
->flags
& ALPM_TRANS_FLAG_NOSAVE
;
368 alpm_filelist_t
*newfiles
;
370 skip_remove
= alpm_list_join(
371 alpm_list_strdup(handle
->trans
->skip_remove
),
372 alpm_list_strdup(handle
->noupgrade
));
373 /* Add files in the NEW backup array to the skip_remove array
374 * so this removal operation doesn't kill them */
375 /* old package backup list */
376 newfiles
= alpm_pkg_get_files(newpkg
);
377 for(b
= alpm_pkg_get_backup(newpkg
); b
; b
= b
->next
) {
378 const alpm_backup_t
*backup
= b
->data
;
379 /* safety check (fix the upgrade026 pactest) */
380 if(!_alpm_filelist_contains(newfiles
, backup
->name
)) {
383 _alpm_log(handle
, ALPM_LOG_DEBUG
, "adding %s to the skip_remove array\n",
385 skip_remove
= alpm_list_add(skip_remove
, strdup(backup
->name
));
388 skip_remove
= alpm_list_strdup(handle
->trans
->skip_remove
);
391 filelist
= alpm_pkg_get_files(oldpkg
);
392 for(i
= 0; i
< filelist
->count
; i
++) {
393 alpm_file_t
*file
= filelist
->files
+ i
;
394 if(!can_remove_file(handle
, file
, skip_remove
)) {
395 _alpm_log(handle
, ALPM_LOG_DEBUG
,
396 "not removing package '%s', can't remove all files\n",
398 FREELIST(skip_remove
);
399 RET_ERR(handle
, ALPM_ERR_PKG_CANT_REMOVE
, -1);
403 _alpm_log(handle
, ALPM_LOG_DEBUG
, "removing %zd files\n", filelist
->count
);
406 /* init progress bar, but only on true remove transactions */
407 PROGRESS(handle
, ALPM_PROGRESS_REMOVE_START
, oldpkg
->name
, 0,
408 pkg_count
, targ_count
);
411 /* iterate through the list backwards, unlinking files */
412 for(i
= filelist
->count
; i
> 0; i
--) {
413 alpm_file_t
*file
= filelist
->files
+ i
- 1;
414 if(unlink_file(handle
, oldpkg
, newpkg
, file
, skip_remove
, nosave
) < 0) {
419 /* update progress bar after each file */
420 int percent
= ((filelist
->count
- i
) * 100) / filelist
->count
;
421 PROGRESS(handle
, ALPM_PROGRESS_REMOVE_START
, oldpkg
->name
,
422 percent
, pkg_count
, targ_count
);
425 FREELIST(skip_remove
);
428 /* set progress to 100% after we finish unlinking files */
429 PROGRESS(handle
, ALPM_PROGRESS_REMOVE_START
, oldpkg
->name
, 100,
430 pkg_count
, targ_count
);
436 int _alpm_remove_single_package(alpm_handle_t
*handle
,
437 alpm_pkg_t
*oldpkg
, alpm_pkg_t
*newpkg
,
438 size_t targ_count
, size_t pkg_count
)
440 const char *pkgname
= oldpkg
->name
;
441 const char *pkgver
= oldpkg
->version
;
444 _alpm_log(handle
, ALPM_LOG_DEBUG
, "removing old package first (%s-%s)\n",
447 EVENT(handle
, ALPM_EVENT_REMOVE_START
, oldpkg
, NULL
);
448 _alpm_log(handle
, ALPM_LOG_DEBUG
, "removing package %s-%s\n",
451 /* run the pre-remove scriptlet if it exists */
452 if(alpm_pkg_has_scriptlet(oldpkg
) &&
453 !(handle
->trans
->flags
& ALPM_TRANS_FLAG_NOSCRIPTLET
)) {
454 char *scriptlet
= _alpm_local_db_pkgpath(handle
->db_local
,
456 _alpm_runscriptlet(handle
, scriptlet
, "pre_remove", pkgver
, NULL
, 0);
461 if(!(handle
->trans
->flags
& ALPM_TRANS_FLAG_DBONLY
)) {
462 /* TODO check returned errors if any */
463 remove_package_files(handle
, oldpkg
, newpkg
, targ_count
, pkg_count
);
466 /* run the post-remove script if it exists */
467 if(!newpkg
&& alpm_pkg_has_scriptlet(oldpkg
) &&
468 !(handle
->trans
->flags
& ALPM_TRANS_FLAG_NOSCRIPTLET
)) {
469 char *scriptlet
= _alpm_local_db_pkgpath(handle
->db_local
,
471 _alpm_runscriptlet(handle
, scriptlet
, "post_remove", pkgver
, NULL
, 0);
476 EVENT(handle
, ALPM_EVENT_REMOVE_DONE
, oldpkg
, NULL
);
479 /* remove the package from the database */
480 _alpm_log(handle
, ALPM_LOG_DEBUG
, "removing database entry '%s'\n", pkgname
);
481 if(_alpm_local_db_remove(handle
->db_local
, oldpkg
) == -1) {
482 _alpm_log(handle
, ALPM_LOG_ERROR
, _("could not remove database entry %s-%s\n"),
485 /* remove the package from the cache */
486 if(_alpm_db_remove_pkgfromcache(handle
->db_local
, oldpkg
) == -1) {
487 _alpm_log(handle
, ALPM_LOG_ERROR
, _("could not remove entry '%s' from cache\n"),
494 int _alpm_remove_packages(alpm_handle_t
*handle
, int run_ldconfig
)
497 size_t pkg_count
, targ_count
;
498 alpm_trans_t
*trans
= handle
->trans
;
501 pkg_count
= alpm_list_count(trans
->remove
);
504 for(targ
= trans
->remove
; targ
; targ
= targ
->next
) {
505 alpm_pkg_t
*pkg
= targ
->data
;
507 if(trans
->state
== STATE_INTERRUPTED
) {
511 if(_alpm_remove_single_package(handle
, pkg
, NULL
,
512 targ_count
, pkg_count
) == -1) {
513 handle
->pm_errno
= ALPM_ERR_TRANS_ABORT
;
514 /* running ldconfig at this point could possibly screw system */
523 /* run ldconfig if it exists */
524 _alpm_ldconfig(handle
);
530 /* vim: set ts=2 sw=2 noet: */