Prefix alpm_transflag_t members with ALPM
[pacman-ng.git] / lib / libalpm / remove.c
blobed9f276773469c964a6a7c44ddf915dbfce05b90
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"
47 int SYMEXPORT alpm_remove_pkg(alpm_handle_t *handle, alpm_pkg_t *pkg)
49 const char *pkgname;
50 alpm_trans_t *trans;
52 /* Sanity checks */
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));
61 pkgname = pkg->name;
63 if(_alpm_pkg_find(trans->remove, pkgname)) {
64 RET_ERR(handle, PM_ERR_TRANS_DUP_TARGET, -1);
67 _alpm_log(handle, ALPM_LOG_DEBUG, "adding package %s to the transaction remove list\n",
68 pkgname);
69 trans->remove = alpm_list_add(trans->remove, _alpm_pkg_dup(pkg));
70 return 0;
73 static void remove_prepare_cascade(alpm_handle_t *handle, alpm_list_t *lp)
75 alpm_trans_t *trans = handle->trans;
77 while(lp) {
78 alpm_list_t *i;
79 for(i = lp; i; i = i->next) {
80 alpm_depmissing_t *miss = (alpm_depmissing_t *)i->data;
81 alpm_pkg_t *info = _alpm_db_get_pkgfromcache(handle->db_local, miss->target);
82 if(info) {
83 if(!_alpm_pkg_find(trans->remove, alpm_pkg_get_name(info))) {
84 _alpm_log(handle, ALPM_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));
88 } else {
89 _alpm_log(handle, ALPM_LOG_ERROR, _("could not find %s in database -- skipping\n"),
90 miss->target);
93 alpm_list_free_inner(lp, (alpm_list_fn_free)_alpm_depmiss_free);
94 alpm_list_free(lp);
95 lp = alpm_checkdeps(handle, _alpm_db_get_pkgcache(handle->db_local),
96 trans->remove, NULL, 1);
100 static void remove_prepare_keep_needed(alpm_handle_t *handle, alpm_list_t *lp)
102 alpm_trans_t *trans = handle->trans;
104 /* Remove needed packages (which break dependencies) from target list */
105 while(lp != NULL) {
106 alpm_list_t *i;
107 for(i = lp; i; i = i->next) {
108 alpm_depmissing_t *miss = (alpm_depmissing_t *)i->data;
109 void *vpkg;
110 alpm_pkg_t *pkg = _alpm_pkg_find(trans->remove, miss->causingpkg);
111 if(pkg == NULL) {
112 continue;
114 trans->remove = alpm_list_remove(trans->remove, pkg, _alpm_pkg_cmp,
115 &vpkg);
116 pkg = vpkg;
117 if(pkg) {
118 _alpm_log(handle, ALPM_LOG_WARNING, _("removing %s from target list\n"),
119 alpm_pkg_get_name(pkg));
120 _alpm_pkg_free(pkg);
123 alpm_list_free_inner(lp, (alpm_list_fn_free)_alpm_depmiss_free);
124 alpm_list_free(lp);
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 alpm_depmissing_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(alpm_handle_t *handle, alpm_list_t **data)
139 alpm_list_t *lp;
140 alpm_trans_t *trans = handle->trans;
141 alpm_db_t *db = handle->db_local;
143 if((trans->flags & ALPM_TRANS_FLAG_RECURSE) && !(trans->flags & ALPM_TRANS_FLAG_CASCADE)) {
144 _alpm_log(handle, ALPM_LOG_DEBUG, "finding removable dependencies\n");
145 _alpm_recursedeps(db, trans->remove,
146 trans->flags & ALPM_TRANS_FLAG_RECURSEALL);
149 if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
150 EVENT(trans, PM_TRANS_EVT_CHECKDEPS_START, NULL, NULL);
152 _alpm_log(handle, ALPM_LOG_DEBUG, "looking for unsatisfied dependencies\n");
153 lp = alpm_checkdeps(handle, _alpm_db_get_pkgcache(db), trans->remove, NULL, 1);
154 if(lp != NULL) {
156 if(trans->flags & ALPM_TRANS_FLAG_CASCADE) {
157 remove_prepare_cascade(handle, lp);
158 } else if(trans->flags & ALPM_TRANS_FLAG_UNNEEDED) {
159 /* Remove needed packages (which would break dependencies)
160 * from target list */
161 remove_prepare_keep_needed(handle, lp);
162 } else {
163 if(data) {
164 *data = lp;
165 } else {
166 alpm_list_free_inner(lp, (alpm_list_fn_free)_alpm_depmiss_free);
167 alpm_list_free(lp);
169 RET_ERR(handle, PM_ERR_UNSATISFIED_DEPS, -1);
174 /* re-order w.r.t. dependencies */
175 _alpm_log(handle, ALPM_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);
179 trans->remove = lp;
181 /* -Rcs == -Rc then -Rs */
182 if((trans->flags & ALPM_TRANS_FLAG_CASCADE) && (trans->flags & ALPM_TRANS_FLAG_RECURSE)) {
183 _alpm_log(handle, ALPM_LOG_DEBUG, "finding removable dependencies\n");
184 _alpm_recursedeps(db, trans->remove, trans->flags & ALPM_TRANS_FLAG_RECURSEALL);
187 if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
188 EVENT(trans, PM_TRANS_EVT_CHECKDEPS_DONE, NULL, NULL);
191 return 0;
194 static int can_remove_file(alpm_handle_t *handle, const char *path,
195 alpm_list_t *skip_remove)
197 char file[PATH_MAX];
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 */
203 return 1;
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, ALPM_LOG_ERROR, _("cannot remove file '%s': %s\n"),
212 file, strerror(errno));
213 return 0;
217 return 1;
220 /* Helper function for iterating through a package's file and deleting them
221 * Used by _alpm_remove_commit. */
222 static void unlink_file(alpm_handle_t *handle, alpm_pkg_t *info, const char *filename,
223 alpm_list_t *skip_remove, int nosave)
225 struct stat buf;
226 char file[PATH_MAX];
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
232 * explanation. */
233 if(alpm_list_find_str(skip_remove, filename)) {
234 _alpm_log(handle, ALPM_LOG_DEBUG, "%s is in skip_remove, skipping removal\n",
235 file);
236 return;
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
242 * actual symlink */
243 if(lstat(file, &buf)) {
244 _alpm_log(handle, ALPM_LOG_DEBUG, "file %s does not exist\n", file);
245 return;
248 if(S_ISDIR(buf.st_mode)) {
249 if(rmdir(file)) {
250 /* this is okay, other packages are probably using it (like /usr) */
251 _alpm_log(handle, ALPM_LOG_DEBUG, "keeping directory %s\n", file);
252 } else {
253 _alpm_log(handle, ALPM_LOG_DEBUG, "removing directory %s\n", file);
255 } else {
256 /* if the file needs backup and has been modified, back it up to .pacsave */
257 alpm_backup_t *backup = _alpm_needbackup(filename, alpm_pkg_get_backup(info));
258 if(backup) {
259 if(nosave) {
260 _alpm_log(handle, ALPM_LOG_DEBUG, "transaction is set to NOSAVE, not backing up '%s'\n", file);
261 } else {
262 char *filehash = alpm_compute_md5sum(file);
263 int cmp = filehash ? strcmp(filehash, backup->hash) : 0;
264 FREE(filehash);
265 if(cmp != 0) {
266 char newpath[PATH_MAX];
267 snprintf(newpath, PATH_MAX, "%s.pacsave", file);
268 rename(file, newpath);
269 _alpm_log(handle, ALPM_LOG_WARNING, _("%s saved as %s\n"), file, newpath);
270 alpm_logaction(handle, "warning: %s saved as %s\n", file, newpath);
271 return;
276 _alpm_log(handle, ALPM_LOG_DEBUG, "unlinking %s\n", file);
278 if(unlink(file) == -1) {
279 _alpm_log(handle, ALPM_LOG_ERROR, _("cannot remove file '%s': %s\n"),
280 filename, strerror(errno));
285 int _alpm_upgraderemove_package(alpm_handle_t *handle,
286 alpm_pkg_t *oldpkg, alpm_pkg_t *newpkg)
288 alpm_list_t *skip_remove, *b;
289 alpm_list_t *newfiles, *lp;
290 size_t filenum = 0;
291 alpm_list_t *files = alpm_pkg_get_files(oldpkg);
292 const char *pkgname = alpm_pkg_get_name(oldpkg);
294 _alpm_log(handle, ALPM_LOG_DEBUG, "removing old package first (%s-%s)\n",
295 oldpkg->name, oldpkg->version);
297 if(handle->trans->flags & ALPM_TRANS_FLAG_DBONLY) {
298 goto db;
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 alpm_backup_t *backup = b->data;
311 /* safety check (fix the upgrade026 pactest) */
312 if(!alpm_list_find_str(filelist, backup->name)) {
313 continue;
315 _alpm_log(handle, ALPM_LOG_DEBUG, "adding %s to the skip_remove array\n",
316 backup->name);
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, ALPM_LOG_DEBUG,
323 "not removing package '%s', can't remove all files\n", pkgname);
324 RET_ERR(handle, PM_ERR_PKG_CANT_REMOVE, -1);
326 filenum++;
329 _alpm_log(handle, ALPM_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, ALPM_LOG_DEBUG, "updating database\n");
342 _alpm_log(handle, ALPM_LOG_DEBUG, "removing database entry '%s'\n", pkgname);
343 if(_alpm_local_db_remove(handle->db_local, oldpkg) == -1) {
344 _alpm_log(handle, ALPM_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, ALPM_LOG_ERROR, _("could not remove entry '%s' from cache\n"),
350 pkgname);
353 return 0;
356 int _alpm_remove_packages(alpm_handle_t *handle)
358 alpm_pkg_t *info;
359 alpm_list_t *targ, *lp;
360 size_t pkg_count;
361 alpm_trans_t *trans = handle->trans;
363 pkg_count = alpm_list_count(trans->remove);
365 for(targ = trans->remove; targ; targ = targ->next) {
366 int position = 0;
367 char scriptlet[PATH_MAX];
368 info = (alpm_pkg_t *)targ->data;
369 const char *pkgname = NULL;
370 size_t targcount = alpm_list_count(targ);
372 if(trans->state == STATE_INTERRUPTED) {
373 return 0;
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, ALPM_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 & ALPM_TRANS_FLAG_NOSCRIPTLET)) {
387 _alpm_runscriptlet(handle, scriptlet, "pre_remove",
388 alpm_pkg_get_version(info), NULL);
391 if(!(trans->flags & ALPM_TRANS_FLAG_DBONLY)) {
392 alpm_list_t *files = alpm_pkg_get_files(info);
393 alpm_list_t *newfiles;
394 size_t filenum = 0;
396 for(lp = files; lp; lp = lp->next) {
397 if(!can_remove_file(handle, lp->data, NULL)) {
398 _alpm_log(handle, ALPM_LOG_DEBUG, "not removing package '%s', can't remove all files\n",
399 pkgname);
400 RET_ERR(handle, PM_ERR_PKG_CANT_REMOVE, -1);
402 filenum++;
405 _alpm_log(handle, ALPM_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)) {
414 int percent;
415 unlink_file(handle, info, lp->data, NULL, trans->flags & ALPM_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));
421 position++;
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 & ALPM_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, ALPM_LOG_DEBUG, "updating database\n");
438 _alpm_log(handle, ALPM_LOG_DEBUG, "removing database entry '%s'\n", pkgname);
439 if(_alpm_local_db_remove(handle->db_local, info) == -1) {
440 _alpm_log(handle, ALPM_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, ALPM_LOG_ERROR, _("could not remove entry '%s' from cache\n"),
446 pkgname);
449 EVENT(trans, PM_TRANS_EVT_REMOVE_DONE, info, NULL);
452 /* run ldconfig if it exists */
453 _alpm_ldconfig(handle);
455 return 0;
458 /* vim: set ts=2 sw=2 noet: */