pacman: list all unknown targets on removal operation
[pacman-ng.git] / lib / libalpm / remove.c
blobd7e06bc89206eb2e38a83c8d9ca41c69d13435b3
1 /*
2 * remove.c
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/>.
25 #include "config.h"
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <limits.h>
31 #include <unistd.h>
32 #include <sys/stat.h>
34 /* libalpm */
35 #include "remove.h"
36 #include "alpm_list.h"
37 #include "alpm.h"
38 #include "trans.h"
39 #include "util.h"
40 #include "log.h"
41 #include "backup.h"
42 #include "package.h"
43 #include "db.h"
44 #include "deps.h"
45 #include "handle.h"
46 #include "conflict.h"
48 int SYMEXPORT alpm_remove_pkg(alpm_handle_t *handle, alpm_pkg_t *pkg)
50 const char *pkgname;
51 alpm_trans_t *trans;
52 alpm_pkg_t *copy;
54 /* Sanity checks */
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));
63 pkgname = pkg->name;
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",
70 pkgname);
71 if(_alpm_pkg_dup(pkg, &copy) == -1) {
72 return -1;
74 trans->remove = alpm_list_add(trans->remove, copy);
75 return 0;
78 static int remove_prepare_cascade(alpm_handle_t *handle, alpm_list_t *lp)
80 alpm_trans_t *trans = handle->trans;
82 while(lp) {
83 alpm_list_t *i;
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);
87 if(info) {
88 alpm_pkg_t *copy;
89 if(!_alpm_pkg_find(trans->remove, info->name)) {
90 _alpm_log(handle, ALPM_LOG_DEBUG, "pulling %s in target list\n",
91 info->name);
92 if(_alpm_pkg_dup(info, &copy) == -1) {
93 return -1;
95 trans->remove = alpm_list_add(trans->remove, copy);
97 } else {
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);
103 alpm_list_free(lp);
104 lp = alpm_checkdeps(handle, _alpm_db_get_pkgcache(handle->db_local),
105 trans->remove, NULL, 1);
107 return 0;
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 */
115 while(lp != NULL) {
116 alpm_list_t *i;
117 for(i = lp; i; i = i->next) {
118 alpm_depmissing_t *miss = i->data;
119 void *vpkg;
120 alpm_pkg_t *pkg = _alpm_pkg_find(trans->remove, miss->causingpkg);
121 if(pkg == NULL) {
122 continue;
124 trans->remove = alpm_list_remove(trans->remove, pkg, _alpm_pkg_cmp,
125 &vpkg);
126 pkg = vpkg;
127 if(pkg) {
128 _alpm_log(handle, ALPM_LOG_WARNING, _("removing %s from target list\n"),
129 pkg->name);
130 _alpm_pkg_free(pkg);
133 alpm_list_free_inner(lp, (alpm_list_fn_free)_alpm_depmiss_free);
134 alpm_list_free(lp);
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)
150 alpm_list_t *lp;
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)) {
159 return -1;
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);
168 if(lp != NULL) {
170 if(trans->flags & ALPM_TRANS_FLAG_CASCADE) {
171 if(remove_prepare_cascade(handle, lp)) {
172 return -1;
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);
178 } else {
179 if(data) {
180 *data = lp;
181 } else {
182 alpm_list_free_inner(lp, (alpm_list_fn_free)_alpm_depmiss_free);
183 alpm_list_free(lp);
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);
195 trans->remove = lp;
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)) {
203 return -1;
207 if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
208 EVENT(handle, ALPM_EVENT_CHECKDEPS_DONE, NULL, NULL);
211 return 0;
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 */
221 return 1;
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));
233 return 0;
237 return 1;
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,
244 int nosave)
246 struct stat buf;
247 char file[PATH_MAX];
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
253 * explanation. */
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);
257 return 1;
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
263 * actual symlink */
264 if(lstat(file, &buf)) {
265 _alpm_log(handle, ALPM_LOG_DEBUG, "file %s does not exist\n", file);
266 return 1;
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 */
272 if(files > 0) {
273 _alpm_log(handle, ALPM_LOG_DEBUG, "keeping directory %s (contains files)\n",
274 file);
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),
279 fileobj->name)) {
280 _alpm_log(handle, ALPM_LOG_DEBUG,
281 "keeping directory %s (in new package)\n", file);
282 } else {
283 /* one last check- does any other package own this file? */
284 alpm_list_t *local, *local_pkgs;
285 int found = 0;
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) {
295 continue;
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);
301 found = 1;
304 if(!found) {
305 if(rmdir(file)) {
306 _alpm_log(handle, ALPM_LOG_DEBUG,
307 "directory removal of %s failed: %s\n", file, strerror(errno));
308 return -1;
309 } else {
310 _alpm_log(handle, ALPM_LOG_DEBUG,
311 "removed directory %s (no remaining owners)\n", file);
315 } else {
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);
318 if(backup) {
319 if(nosave) {
320 _alpm_log(handle, ALPM_LOG_DEBUG, "transaction is set to NOSAVE, not backing up '%s'\n", file);
321 } else {
322 char *filehash = alpm_compute_md5sum(file);
323 int cmp = filehash ? strcmp(filehash, backup->hash) : 0;
324 FREE(filehash);
325 if(cmp != 0) {
326 char *newpath;
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));
335 free(newpath);
336 return -1;
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);
340 free(newpath);
341 return 0;
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));
353 return -1;
356 return 0;
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;
368 size_t i;
370 if(newpkg) {
371 _alpm_log(handle, ALPM_LOG_DEBUG, "removing old package first (%s-%s)\n",
372 pkgname, pkgver);
373 } else {
374 EVENT(handle, ALPM_EVENT_REMOVE_START, oldpkg, NULL);
375 _alpm_log(handle, ALPM_LOG_DEBUG, "removing package %s-%s\n",
376 pkgname, pkgver);
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,
382 oldpkg, "install");
383 _alpm_runscriptlet(handle, scriptlet, "pre_remove", pkgver, NULL, 0);
384 free(scriptlet);
388 if(handle->trans->flags & ALPM_TRANS_FLAG_DBONLY) {
389 goto db;
392 if(newpkg) {
393 alpm_filelist_t *newfiles;
394 alpm_list_t *b;
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)) {
406 continue;
408 _alpm_log(handle, ALPM_LOG_DEBUG, "adding %s to the skip_remove array\n",
409 backup->name);
410 skip_remove = alpm_list_add(skip_remove, strdup(backup->name));
412 } else {
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);
424 filenum++;
427 _alpm_log(handle, ALPM_LOG_DEBUG, "removing %ld files\n", (unsigned long)filenum);
429 if(!newpkg) {
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;
438 int percent;
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);
443 if(!newpkg) {
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);
449 position++;
451 FREELIST(skip_remove);
453 if(!newpkg) {
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,
462 oldpkg, "install");
463 _alpm_runscriptlet(handle, scriptlet, "post_remove", pkgver, NULL, 0);
464 free(scriptlet);
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"),
474 pkgname, pkgver);
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"),
479 pkgname);
482 if(!newpkg) {
483 /* TODO: awesome! we're passing invalid pointers. */
484 EVENT(handle, ALPM_EVENT_REMOVE_DONE, oldpkg, NULL);
487 return 0;
490 int _alpm_remove_packages(alpm_handle_t *handle, int run_ldconfig)
492 alpm_list_t *targ;
493 size_t pkg_count, targ_count;
494 alpm_trans_t *trans = handle->trans;
495 int ret = 0;
497 pkg_count = alpm_list_count(trans->remove);
498 targ_count = 1;
500 for(targ = trans->remove; targ; targ = targ->next) {
501 alpm_pkg_t *pkg = targ->data;
503 if(trans->state == STATE_INTERRUPTED) {
504 return ret;
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 */
511 run_ldconfig = 0;
512 ret = -1;
515 targ_count++;
518 if(run_ldconfig) {
519 /* run ldconfig if it exists */
520 _alpm_ldconfig(handle);
523 return ret;
526 /* vim: set ts=2 sw=2 noet: */