Merge branch 'maint'
[pacman-ng.git] / lib / libalpm / remove.c
blobef9d40e115429c628748cd5e4c9da6cdc9cf032b
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 <time.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <limits.h>
33 #include <unistd.h>
34 #include <sys/stat.h>
36 /* libalpm */
37 #include "remove.h"
38 #include "alpm_list.h"
39 #include "trans.h"
40 #include "util.h"
41 #include "log.h"
42 #include "backup.h"
43 #include "package.h"
44 #include "db.h"
45 #include "deps.h"
46 #include "handle.h"
47 #include "alpm.h"
49 int SYMEXPORT alpm_remove_pkg(pmpkg_t *pkg)
51 pmtrans_t *trans;
52 const char *pkgname;
54 ALPM_LOG_FUNC;
56 /* Sanity checks */
57 ASSERT(pkg != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1));
58 ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
59 trans = handle->trans;
60 ASSERT(trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
61 ASSERT(trans->state == STATE_INITIALIZED,
62 RET_ERR(PM_ERR_TRANS_NOT_INITIALIZED, -1));
64 pkgname = alpm_pkg_get_name(pkg);
66 if(_alpm_pkg_find(trans->remove, pkgname)) {
67 RET_ERR(PM_ERR_TRANS_DUP_TARGET, -1);
70 _alpm_log(PM_LOG_DEBUG, "adding %s in the target list\n", pkgname);
71 trans->remove = alpm_list_add(trans->remove, _alpm_pkg_dup(pkg));
72 return 0;
75 static void remove_prepare_cascade(pmtrans_t *trans, pmdb_t *db,
76 alpm_list_t *lp)
78 ALPM_LOG_FUNC;
80 while(lp) {
81 alpm_list_t *i;
82 for(i = lp; i; i = i->next) {
83 pmdepmissing_t *miss = (pmdepmissing_t *)i->data;
84 pmpkg_t *info = _alpm_db_get_pkgfromcache(db, miss->target);
85 if(info) {
86 if(!_alpm_pkg_find(trans->remove, alpm_pkg_get_name(info))) {
87 _alpm_log(PM_LOG_DEBUG, "pulling %s in target list\n",
88 alpm_pkg_get_name(info));
89 trans->remove = alpm_list_add(trans->remove, _alpm_pkg_dup(info));
91 } else {
92 _alpm_log(PM_LOG_ERROR, _("could not find %s in database -- skipping\n"),
93 miss->target);
96 alpm_list_free_inner(lp, (alpm_list_fn_free)_alpm_depmiss_free);
97 alpm_list_free(lp);
98 lp = alpm_checkdeps(_alpm_db_get_pkgcache(db), 1, trans->remove, NULL);
102 static void remove_prepare_keep_needed(pmtrans_t *trans, pmdb_t *db,
103 alpm_list_t *lp)
105 ALPM_LOG_FUNC;
107 /* Remove needed packages (which break dependencies) from target list */
108 while(lp != NULL) {
109 alpm_list_t *i;
110 for(i = lp; i; i = i->next) {
111 pmdepmissing_t *miss = (pmdepmissing_t *)i->data;
112 void *vpkg;
113 pmpkg_t *pkg = _alpm_pkg_find(trans->remove, miss->causingpkg);
114 if(pkg == NULL) {
115 continue;
117 trans->remove = alpm_list_remove(trans->remove, pkg, _alpm_pkg_cmp,
118 &vpkg);
119 pkg = vpkg;
120 if(pkg) {
121 _alpm_log(PM_LOG_WARNING, _("removing %s from target list\n"),
122 alpm_pkg_get_name(pkg));
123 _alpm_pkg_free(pkg);
126 alpm_list_free_inner(lp, (alpm_list_fn_free)_alpm_depmiss_free);
127 alpm_list_free(lp);
128 lp = alpm_checkdeps(_alpm_db_get_pkgcache(db), 1, trans->remove, NULL);
132 /** Transaction preparation for remove actions.
133 * This functions takes a pointer to a alpm_list_t which will be
134 * filled with a list of pmdepmissing_t* objects representing
135 * the packages blocking the transaction.
136 * @param trans the transaction object
137 * @param db the database of local packages
138 * @param data a pointer to an alpm_list_t* to fill
140 int _alpm_remove_prepare(pmtrans_t *trans, pmdb_t *db, alpm_list_t **data)
142 alpm_list_t *lp;
144 ALPM_LOG_FUNC;
146 ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
147 ASSERT(trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
149 if((trans->flags & PM_TRANS_FLAG_RECURSE) && !(trans->flags & PM_TRANS_FLAG_CASCADE)) {
150 _alpm_log(PM_LOG_DEBUG, "finding removable dependencies\n");
151 _alpm_recursedeps(db, trans->remove, trans->flags & PM_TRANS_FLAG_RECURSEALL);
154 if(!(trans->flags & PM_TRANS_FLAG_NODEPS)) {
155 EVENT(trans, PM_TRANS_EVT_CHECKDEPS_START, NULL, NULL);
157 _alpm_log(PM_LOG_DEBUG, "looking for unsatisfied dependencies\n");
158 lp = alpm_checkdeps(_alpm_db_get_pkgcache(db), 1, trans->remove, NULL);
159 if(lp != NULL) {
161 if(trans->flags & PM_TRANS_FLAG_CASCADE) {
162 remove_prepare_cascade(trans, db, lp);
163 } else if (trans->flags & PM_TRANS_FLAG_UNNEEDED) {
164 /* Remove needed packages (which would break dependencies)
165 * from target list */
166 remove_prepare_keep_needed(trans, db, lp);
167 } else {
168 if(data) {
169 *data = lp;
170 } else {
171 alpm_list_free_inner(lp, (alpm_list_fn_free)_alpm_depmiss_free);
172 alpm_list_free(lp);
174 RET_ERR(PM_ERR_UNSATISFIED_DEPS, -1);
179 /* re-order w.r.t. dependencies */
180 _alpm_log(PM_LOG_DEBUG, "sorting by dependencies\n");
181 lp = _alpm_sortbydeps(trans->remove, 1);
182 /* free the old alltargs */
183 alpm_list_free(trans->remove);
184 trans->remove = lp;
186 /* -Rcs == -Rc then -Rs */
187 if((trans->flags & PM_TRANS_FLAG_CASCADE) && (trans->flags & PM_TRANS_FLAG_RECURSE)) {
188 _alpm_log(PM_LOG_DEBUG, "finding removable dependencies\n");
189 _alpm_recursedeps(db, trans->remove, trans->flags & PM_TRANS_FLAG_RECURSEALL);
192 if(!(trans->flags & PM_TRANS_FLAG_NODEPS)) {
193 EVENT(trans, PM_TRANS_EVT_CHECKDEPS_DONE, NULL, NULL);
196 return 0;
199 static int can_remove_file(const char *path, alpm_list_t *skip)
201 char file[PATH_MAX+1];
203 snprintf(file, PATH_MAX, "%s%s", handle->root, path);
205 if(alpm_list_find_str(skip, file)) {
206 /* return success because we will never actually remove this file */
207 return 1;
209 /* If we fail write permissions due to a read-only filesystem, abort.
210 * Assume all other possible failures are covered somewhere else */
211 if(access(file, W_OK) == -1) {
212 if(errno != EACCES && errno != ETXTBSY && access(file, F_OK) == 0) {
213 /* only return failure if the file ACTUALLY exists and we can't write to
214 * it - ignore "chmod -w" simple permission failures */
215 _alpm_log(PM_LOG_ERROR, _("cannot remove file '%s': %s\n"),
216 file, strerror(errno));
217 return 0;
221 return 1;
224 /* Helper function for iterating through a package's file and deleting them
225 * Used by _alpm_remove_commit. */
226 static void unlink_file(pmpkg_t *info, char *filename, alpm_list_t *skip_remove, int nosave)
228 struct stat buf;
229 char file[PATH_MAX+1];
231 ALPM_LOG_FUNC;
233 snprintf(file, PATH_MAX, "%s%s", handle->root, filename);
235 /* check the remove skip list before removing the file.
236 * see the big comment block in db_find_fileconflicts() for an
237 * explanation. */
238 if(alpm_list_find_str(skip_remove, filename)) {
239 _alpm_log(PM_LOG_DEBUG, "%s is in skip_remove, skipping removal\n",
240 file);
241 return;
244 /* we want to do a lstat here, and not a _alpm_lstat.
245 * if a directory in the package is actually a directory symlink on the
246 * filesystem, we want to work with the linked directory instead of the
247 * actual symlink */
248 if(lstat(file, &buf)) {
249 _alpm_log(PM_LOG_DEBUG, "file %s does not exist\n", file);
250 return;
253 if(S_ISDIR(buf.st_mode)) {
254 if(rmdir(file)) {
255 /* this is okay, other packages are probably using it (like /usr) */
256 _alpm_log(PM_LOG_DEBUG, "keeping directory %s\n", file);
257 } else {
258 _alpm_log(PM_LOG_DEBUG, "removing directory %s\n", file);
260 } else {
261 /* if the file needs backup and has been modified, back it up to .pacsave */
262 char *pkghash = _alpm_needbackup(filename, alpm_pkg_get_backup(info));
263 if(pkghash) {
264 if(nosave) {
265 _alpm_log(PM_LOG_DEBUG, "transaction is set to NOSAVE, not backing up '%s'\n", file);
266 FREE(pkghash);
267 } else {
268 char *filehash = alpm_compute_md5sum(file);
269 int cmp = strcmp(filehash,pkghash);
270 FREE(filehash);
271 FREE(pkghash);
272 if(cmp != 0) {
273 char newpath[PATH_MAX];
274 snprintf(newpath, PATH_MAX, "%s.pacsave", file);
275 rename(file, newpath);
276 _alpm_log(PM_LOG_WARNING, _("%s saved as %s\n"), file, newpath);
277 alpm_logaction("warning: %s saved as %s\n", file, newpath);
278 return;
283 _alpm_log(PM_LOG_DEBUG, "unlinking %s\n", file);
285 if(unlink(file) == -1) {
286 _alpm_log(PM_LOG_ERROR, _("cannot remove file '%s': %s\n"),
287 filename, strerror(errno));
292 int _alpm_upgraderemove_package(pmpkg_t *oldpkg, pmpkg_t *newpkg,
293 pmtrans_t *trans)
295 alpm_list_t *skip_remove, *b;
296 alpm_list_t *newfiles, *lp;
297 size_t filenum;
298 alpm_list_t *files = alpm_pkg_get_files(oldpkg);
299 const char *pkgname = alpm_pkg_get_name(oldpkg);
301 ALPM_LOG_FUNC;
303 _alpm_log(PM_LOG_DEBUG, "removing old package first (%s-%s)\n",
304 oldpkg->name, oldpkg->version);
306 if(trans->flags & PM_TRANS_FLAG_DBONLY) {
307 goto db;
310 /* copy the remove skiplist over */
311 skip_remove = alpm_list_join(
312 alpm_list_strdup(trans->skip_remove),
313 alpm_list_strdup(handle->noupgrade));
314 /* Add files in the NEW backup array to the skip_remove array
315 * so this removal operation doesn't kill them */
316 /* old package backup list */
317 alpm_list_t *filelist = alpm_pkg_get_files(newpkg);
318 for(b = alpm_pkg_get_backup(newpkg); b; b = b->next) {
319 char *backup = _alpm_backup_file(b->data);
320 /* safety check (fix the upgrade026 pactest) */
321 if(!alpm_list_find_str(filelist, backup)) {
322 FREE(backup);
323 continue;
325 _alpm_log(PM_LOG_DEBUG, "adding %s to the skip_remove array\n", backup);
326 skip_remove = alpm_list_add(skip_remove, backup);
329 for(lp = files; lp; lp = lp->next) {
330 if(!can_remove_file(lp->data, skip_remove)) {
331 _alpm_log(PM_LOG_DEBUG, "not removing package '%s', can't remove all files\n",
332 pkgname);
333 RET_ERR(PM_ERR_PKG_CANT_REMOVE, -1);
337 filenum = alpm_list_count(files);
338 _alpm_log(PM_LOG_DEBUG, "removing %ld files\n", (unsigned long)filenum);
340 /* iterate through the list backwards, unlinking files */
341 newfiles = alpm_list_reverse(files);
342 for(lp = newfiles; lp; lp = alpm_list_next(lp)) {
343 unlink_file(oldpkg, lp->data, skip_remove, 0);
345 alpm_list_free(newfiles);
346 FREELIST(skip_remove);
349 /* remove the package from the database */
350 _alpm_log(PM_LOG_DEBUG, "updating database\n");
351 _alpm_log(PM_LOG_DEBUG, "removing database entry '%s'\n", pkgname);
352 if(_alpm_local_db_remove(handle->db_local, oldpkg) == -1) {
353 _alpm_log(PM_LOG_ERROR, _("could not remove database entry %s-%s\n"),
354 pkgname, alpm_pkg_get_version(oldpkg));
356 /* remove the package from the cache */
357 if(_alpm_db_remove_pkgfromcache(handle->db_local, oldpkg) == -1) {
358 _alpm_log(PM_LOG_ERROR, _("could not remove entry '%s' from cache\n"),
359 pkgname);
362 return 0;
365 int _alpm_remove_packages(pmtrans_t *trans, pmdb_t *db)
367 pmpkg_t *info;
368 alpm_list_t *targ, *lp;
369 size_t pkg_count;
371 ALPM_LOG_FUNC;
373 ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
374 ASSERT(trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
376 pkg_count = alpm_list_count(trans->remove);
378 for(targ = trans->remove; targ; targ = targ->next) {
379 int position = 0;
380 char scriptlet[PATH_MAX];
381 info = (pmpkg_t*)targ->data;
382 const char *pkgname = NULL;
383 size_t targcount = alpm_list_count(targ);
385 if(handle->trans->state == STATE_INTERRUPTED) {
386 return 0;
389 /* get the name now so we can use it after package is removed */
390 pkgname = alpm_pkg_get_name(info);
391 snprintf(scriptlet, PATH_MAX, "%s%s-%s/install",
392 _alpm_db_path(db), pkgname, alpm_pkg_get_version(info));
394 EVENT(trans, PM_TRANS_EVT_REMOVE_START, info, NULL);
395 _alpm_log(PM_LOG_DEBUG, "removing package %s-%s\n",
396 pkgname, alpm_pkg_get_version(info));
398 /* run the pre-remove scriptlet if it exists */
399 if(alpm_pkg_has_scriptlet(info) && !(trans->flags & PM_TRANS_FLAG_NOSCRIPTLET)) {
400 _alpm_runscriptlet(handle->root, scriptlet, "pre_remove",
401 alpm_pkg_get_version(info), NULL, trans);
404 if(!(trans->flags & PM_TRANS_FLAG_DBONLY)) {
405 alpm_list_t *files = alpm_pkg_get_files(info);
406 alpm_list_t *newfiles;
407 size_t filenum;
409 for(lp = files; lp; lp = lp->next) {
410 if(!can_remove_file(lp->data, NULL)) {
411 _alpm_log(PM_LOG_DEBUG, "not removing package '%s', can't remove all files\n",
412 pkgname);
413 RET_ERR(PM_ERR_PKG_CANT_REMOVE, -1);
417 filenum = alpm_list_count(files);
418 _alpm_log(PM_LOG_DEBUG, "removing %ld files\n", (unsigned long)filenum);
420 /* init progress bar */
421 PROGRESS(trans, PM_TRANS_PROGRESS_REMOVE_START, info->name, 0,
422 pkg_count, (pkg_count - targcount + 1));
424 /* iterate through the list backwards, unlinking files */
425 newfiles = alpm_list_reverse(files);
426 for(lp = newfiles; lp; lp = alpm_list_next(lp)) {
427 int percent;
428 unlink_file(info, lp->data, NULL, trans->flags & PM_TRANS_FLAG_NOSAVE);
430 /* update progress bar after each file */
431 percent = (position * 100) / filenum;
432 PROGRESS(trans, PM_TRANS_PROGRESS_REMOVE_START, info->name,
433 percent, pkg_count, (pkg_count - targcount + 1));
434 position++;
436 alpm_list_free(newfiles);
439 /* set progress to 100% after we finish unlinking files */
440 PROGRESS(trans, PM_TRANS_PROGRESS_REMOVE_START, pkgname, 100,
441 pkg_count, (pkg_count - targcount + 1));
443 /* run the post-remove script if it exists */
444 if(alpm_pkg_has_scriptlet(info) && !(trans->flags & PM_TRANS_FLAG_NOSCRIPTLET)) {
445 _alpm_runscriptlet(handle->root, scriptlet, "post_remove",
446 alpm_pkg_get_version(info), NULL, trans);
449 /* remove the package from the database */
450 _alpm_log(PM_LOG_DEBUG, "updating database\n");
451 _alpm_log(PM_LOG_DEBUG, "removing database entry '%s'\n", pkgname);
452 if(_alpm_local_db_remove(db, info) == -1) {
453 _alpm_log(PM_LOG_ERROR, _("could not remove database entry %s-%s\n"),
454 pkgname, alpm_pkg_get_version(info));
456 /* remove the package from the cache */
457 if(_alpm_db_remove_pkgfromcache(db, info) == -1) {
458 _alpm_log(PM_LOG_ERROR, _("could not remove entry '%s' from cache\n"),
459 pkgname);
462 EVENT(trans, PM_TRANS_EVT_REMOVE_DONE, info, NULL);
465 /* run ldconfig if it exists */
466 _alpm_ldconfig(handle->root);
468 return 0;
471 /* vim: set ts=2 sw=2 noet: */