Various tweaks to support building with excessive GCC warning flags
[pacman-ng.git] / lib / libalpm / remove.c
blob469ce9bb6606a2ff4f0ee91066db74641d91ac85
1 /*
2 * remove.c
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/>.
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <limits.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
32 /* libalpm */
33 #include "remove.h"
34 #include "alpm_list.h"
35 #include "alpm.h"
36 #include "trans.h"
37 #include "util.h"
38 #include "log.h"
39 #include "backup.h"
40 #include "package.h"
41 #include "db.h"
42 #include "deps.h"
43 #include "handle.h"
44 #include "conflict.h"
46 int SYMEXPORT alpm_remove_pkg(alpm_handle_t *handle, alpm_pkg_t *pkg)
48 const char *pkgname;
49 alpm_trans_t *trans;
50 alpm_pkg_t *copy;
52 /* Sanity checks */
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));
61 pkgname = pkg->name;
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",
68 pkgname);
69 if(_alpm_pkg_dup(pkg, &copy) == -1) {
70 return -1;
72 trans->remove = alpm_list_add(trans->remove, copy);
73 return 0;
76 static int remove_prepare_cascade(alpm_handle_t *handle, alpm_list_t *lp)
78 alpm_trans_t *trans = handle->trans;
80 while(lp) {
81 alpm_list_t *i;
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);
85 if(info) {
86 alpm_pkg_t *copy;
87 if(!_alpm_pkg_find(trans->remove, info->name)) {
88 _alpm_log(handle, ALPM_LOG_DEBUG, "pulling %s in target list\n",
89 info->name);
90 if(_alpm_pkg_dup(info, &copy) == -1) {
91 return -1;
93 trans->remove = alpm_list_add(trans->remove, copy);
95 } else {
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);
101 alpm_list_free(lp);
102 lp = alpm_checkdeps(handle, _alpm_db_get_pkgcache(handle->db_local),
103 trans->remove, NULL, 1);
105 return 0;
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 */
113 while(lp != NULL) {
114 alpm_list_t *i;
115 for(i = lp; i; i = i->next) {
116 alpm_depmissing_t *miss = i->data;
117 void *vpkg;
118 alpm_pkg_t *pkg = _alpm_pkg_find(trans->remove, miss->causingpkg);
119 if(pkg == NULL) {
120 continue;
122 trans->remove = alpm_list_remove(trans->remove, pkg, _alpm_pkg_cmp,
123 &vpkg);
124 pkg = vpkg;
125 if(pkg) {
126 _alpm_log(handle, ALPM_LOG_WARNING, _("removing %s from target list\n"),
127 pkg->name);
128 _alpm_pkg_free(pkg);
131 alpm_list_free_inner(lp, (alpm_list_fn_free)_alpm_depmiss_free);
132 alpm_list_free(lp);
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)
148 alpm_list_t *lp;
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)) {
157 return -1;
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);
166 if(lp != NULL) {
168 if(trans->flags & ALPM_TRANS_FLAG_CASCADE) {
169 if(remove_prepare_cascade(handle, lp)) {
170 return -1;
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);
176 } else {
177 if(data) {
178 *data = lp;
179 } else {
180 alpm_list_free_inner(lp, (alpm_list_fn_free)_alpm_depmiss_free);
181 alpm_list_free(lp);
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);
193 trans->remove = lp;
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)) {
201 return -1;
205 if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
206 EVENT(handle, ALPM_EVENT_CHECKDEPS_DONE, NULL, NULL);
209 return 0;
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 */
219 return 1;
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));
231 return 0;
235 return 1;
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,
242 int nosave)
244 struct stat buf;
245 char file[PATH_MAX];
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
251 * explanation. */
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);
255 return 1;
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
261 * actual symlink */
262 if(lstat(file, &buf)) {
263 _alpm_log(handle, ALPM_LOG_DEBUG, "file %s does not exist\n", file);
264 return 1;
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 */
270 if(files > 0) {
271 _alpm_log(handle, ALPM_LOG_DEBUG, "keeping directory %s (contains files)\n",
272 file);
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),
277 fileobj->name)) {
278 _alpm_log(handle, ALPM_LOG_DEBUG,
279 "keeping directory %s (in new package)\n", file);
280 } else {
281 /* one last check- does any other package own this file? */
282 alpm_list_t *local, *local_pkgs;
283 int found = 0;
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) {
293 continue;
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);
299 found = 1;
302 if(!found) {
303 if(rmdir(file)) {
304 _alpm_log(handle, ALPM_LOG_DEBUG,
305 "directory removal of %s failed: %s\n", file, strerror(errno));
306 return -1;
307 } else {
308 _alpm_log(handle, ALPM_LOG_DEBUG,
309 "removed directory %s (no remaining owners)\n", file);
313 } else {
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);
316 if(backup) {
317 if(nosave) {
318 _alpm_log(handle, ALPM_LOG_DEBUG, "transaction is set to NOSAVE, not backing up '%s'\n", file);
319 } else {
320 char *filehash = alpm_compute_md5sum(file);
321 int cmp = filehash ? strcmp(filehash, backup->hash) : 0;
322 FREE(filehash);
323 if(cmp != 0) {
324 char *newpath;
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));
333 free(newpath);
334 return -1;
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);
338 free(newpath);
339 return 0;
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));
351 return -1;
354 return 0;
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;
363 size_t i;
364 int err = 0;
365 int nosave = handle->trans->flags & ALPM_TRANS_FLAG_NOSAVE;
367 if(newpkg) {
368 alpm_filelist_t *newfiles;
369 alpm_list_t *b;
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)) {
381 continue;
383 _alpm_log(handle, ALPM_LOG_DEBUG, "adding %s to the skip_remove array\n",
384 backup->name);
385 skip_remove = alpm_list_add(skip_remove, strdup(backup->name));
387 } else {
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",
397 oldpkg->name);
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);
405 if(!newpkg) {
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) {
415 err++;
418 if(!newpkg) {
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);
427 if(!newpkg) {
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);
433 return err;
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;
443 if(newpkg) {
444 _alpm_log(handle, ALPM_LOG_DEBUG, "removing old package first (%s-%s)\n",
445 pkgname, pkgver);
446 } else {
447 EVENT(handle, ALPM_EVENT_REMOVE_START, oldpkg, NULL);
448 _alpm_log(handle, ALPM_LOG_DEBUG, "removing package %s-%s\n",
449 pkgname, pkgver);
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,
455 oldpkg, "install");
456 _alpm_runscriptlet(handle, scriptlet, "pre_remove", pkgver, NULL, 0);
457 free(scriptlet);
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,
470 oldpkg, "install");
471 _alpm_runscriptlet(handle, scriptlet, "post_remove", pkgver, NULL, 0);
472 free(scriptlet);
475 if(!newpkg) {
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"),
483 pkgname, pkgver);
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"),
488 pkgname);
491 return 0;
494 int _alpm_remove_packages(alpm_handle_t *handle, int run_ldconfig)
496 alpm_list_t *targ;
497 size_t pkg_count, targ_count;
498 alpm_trans_t *trans = handle->trans;
499 int ret = 0;
501 pkg_count = alpm_list_count(trans->remove);
502 targ_count = 1;
504 for(targ = trans->remove; targ; targ = targ->next) {
505 alpm_pkg_t *pkg = targ->data;
507 if(trans->state == STATE_INTERRUPTED) {
508 return ret;
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 */
515 run_ldconfig = 0;
516 ret = -1;
519 targ_count++;
522 if(run_ldconfig) {
523 /* run ldconfig if it exists */
524 _alpm_ldconfig(handle);
527 return ret;
530 /* vim: set ts=2 sw=2 noet: */