remove.c: refactor into functions
[pacman-ng.git] / lib / libalpm / remove.c
blobf2e84ef5ebf8c9ec5c743286e9f73254c15ba0d4
1 /*
2 * remove.c
4 * Copyright (c) 2002-2007 by Judd Vinet <jvinet@zeroflux.org>
5 * Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
6 * Copyright (c) 2005 by Christian Hamar <krics@linuxforum.hu>
7 * Copyright (c) 2006 by David Kimpe <dnaku@frugalware.org>
8 * Copyright (c) 2005, 2006 by Miklos Vajna <vmiklos@frugalware.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "config.h"
26 #include <stdlib.h>
27 #include <errno.h>
28 #include <time.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <unistd.h>
33 #include <sys/stat.h>
35 /* libalpm */
36 #include "remove.h"
37 #include "alpm_list.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 "cache.h"
45 #include "deps.h"
46 #include "handle.h"
47 #include "alpm.h"
49 int _alpm_remove_loadtarget(pmtrans_t *trans, pmdb_t *db, char *name)
51 pmpkg_t *info;
53 ALPM_LOG_FUNC;
55 ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
56 ASSERT(trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
57 ASSERT(name != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1));
59 if(_alpm_pkg_find(name, trans->packages)) {
60 RET_ERR(PM_ERR_TRANS_DUP_TARGET, -1);
63 if((info = _alpm_db_get_pkgfromcache(db, name)) == NULL) {
64 _alpm_log(PM_LOG_DEBUG, "could not find %s in database\n", name);
65 RET_ERR(PM_ERR_PKG_NOT_FOUND, -1);
68 /* ignore holdpkgs on upgrade */
69 if((trans == handle->trans)
70 && alpm_list_find_str(handle->holdpkg, info->name)) {
71 int resp = 0;
72 QUESTION(trans, PM_TRANS_CONV_REMOVE_HOLDPKG, info, NULL, NULL, &resp);
73 if(!resp) {
74 RET_ERR(PM_ERR_PKG_HOLD, -1);
78 _alpm_log(PM_LOG_DEBUG, "adding %s in the targets list\n", info->name);
79 trans->packages = alpm_list_add(trans->packages, _alpm_pkg_dup(info));
81 return(0);
84 static void remove_prepare_cascade(pmtrans_t *trans, pmdb_t *db,
85 alpm_list_t *lp)
87 ALPM_LOG_FUNC;
89 while(lp) {
90 alpm_list_t *i;
91 for(i = lp; i; i = i->next) {
92 pmdepmissing_t *miss = (pmdepmissing_t *)i->data;
93 pmpkg_t *info = _alpm_db_get_pkgfromcache(db, miss->target);
94 if(info) {
95 if(!_alpm_pkg_find(alpm_pkg_get_name(info), trans->packages)) {
96 _alpm_log(PM_LOG_DEBUG, "pulling %s in the targets list\n",
97 alpm_pkg_get_name(info));
98 trans->packages = alpm_list_add(trans->packages, _alpm_pkg_dup(info));
100 } else {
101 _alpm_log(PM_LOG_ERROR, _("could not find %s in database -- skipping\n"),
102 miss->target);
105 alpm_list_free_inner(lp, (alpm_list_fn_free)_alpm_depmiss_free);
106 alpm_list_free(lp);
107 lp = alpm_checkdeps(db, 1, trans->packages, NULL);
111 static void remove_prepare_keep_needed(pmtrans_t *trans, pmdb_t *db,
112 alpm_list_t *lp)
114 ALPM_LOG_FUNC;
116 /* Remove needed packages (which break dependencies) from the target list */
117 while(lp != NULL) {
118 alpm_list_t *i;
119 for(i = lp; i; i = i->next) {
120 pmdepmissing_t *miss = (pmdepmissing_t *)i->data;
121 void *vpkg;
122 pmpkg_t *pkg;
123 trans->packages = alpm_list_remove(trans->packages, miss->causingpkg,
124 _alpm_pkgname_pkg_cmp, &vpkg);
125 pkg = vpkg;
126 if(pkg) {
127 _alpm_log(PM_LOG_WARNING, "removing %s from the target-list\n",
128 alpm_pkg_get_name(pkg));
129 _alpm_pkg_free(pkg);
132 alpm_list_free_inner(lp, (alpm_list_fn_free)_alpm_depmiss_free);
133 alpm_list_free(lp);
134 lp = alpm_checkdeps(db, 1, trans->packages, NULL);
138 int _alpm_remove_prepare(pmtrans_t *trans, pmdb_t *db, alpm_list_t **data)
140 alpm_list_t *lp;
142 ALPM_LOG_FUNC;
144 ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
145 ASSERT(trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
147 /* skip all checks if we are doing this removal as part of an upgrade */
148 if(trans->type == PM_TRANS_TYPE_REMOVEUPGRADE) {
149 return(0);
152 if(!(trans->flags & PM_TRANS_FLAG_NODEPS)) {
153 EVENT(trans, PM_TRANS_EVT_CHECKDEPS_START, NULL, NULL);
155 _alpm_log(PM_LOG_DEBUG, "looking for unsatisfied dependencies\n");
156 lp = alpm_checkdeps(db, 1, trans->packages, NULL);
157 if(lp != NULL) {
159 if(trans->flags & PM_TRANS_FLAG_CASCADE) {
160 remove_prepare_cascade(trans, db, lp);
161 } else if (trans->flags & PM_TRANS_FLAG_UNNEEDED) {
162 /* Remove needed packages (which would break dependencies)
163 * from the target list */
164 remove_prepare_keep_needed(trans, db, lp);
165 } else {
166 if(data) {
167 *data = lp;
168 } else {
169 alpm_list_free_inner(lp, (alpm_list_fn_free)_alpm_depmiss_free);
170 alpm_list_free(lp);
172 RET_ERR(PM_ERR_UNSATISFIED_DEPS, -1);
177 /* re-order w.r.t. dependencies */
178 _alpm_log(PM_LOG_DEBUG, "sorting by dependencies\n");
179 lp = _alpm_sortbydeps(trans->packages, 1);
180 /* free the old alltargs */
181 alpm_list_free(trans->packages);
182 trans->packages = lp;
184 if(trans->flags & PM_TRANS_FLAG_RECURSE) {
185 _alpm_log(PM_LOG_DEBUG, "finding removable dependencies\n");
186 _alpm_recursedeps(db, trans->packages, trans->flags & PM_TRANS_FLAG_RECURSEALL);
189 if(!(trans->flags & PM_TRANS_FLAG_NODEPS)) {
190 EVENT(trans, PM_TRANS_EVT_CHECKDEPS_DONE, NULL, NULL);
193 return(0);
196 static int can_remove_file(pmtrans_t *trans, const char *path)
198 char file[PATH_MAX+1];
200 snprintf(file, PATH_MAX, "%s%s", handle->root, path);
202 if(alpm_list_find_str(trans->skip_remove, file)) {
203 /* return success because we will never actually remove this file */
204 return(1);
206 /* If we fail write permissions due to a read-only filesystem, abort.
207 * Assume all other possible failures are covered somewhere else */
208 if(access(file, W_OK) == -1) {
209 if(errno != EACCES && errno != ETXTBSY && access(file, F_OK) == 0) {
210 /* only return failure if the file ACTUALLY exists and we can't write to
211 * it - ignore "chmod -w" simple permission failures */
212 _alpm_log(PM_LOG_ERROR, _("cannot remove file '%s': %s\n"),
213 file, strerror(errno));
214 return(0);
218 return(1);
221 /* Helper function for iterating through a package's file and deleting them
222 * Used by _alpm_remove_commit. */
223 static void unlink_file(pmpkg_t *info, alpm_list_t *lp, pmtrans_t *trans)
225 struct stat buf;
226 int needbackup = 0;
227 char file[PATH_MAX+1];
229 ALPM_LOG_FUNC;
231 char *hash = _alpm_needbackup(lp->data, alpm_pkg_get_backup(info));
232 if(hash) {
233 needbackup = 1;
234 FREE(hash);
237 snprintf(file, PATH_MAX, "%s%s", handle->root, (char *)lp->data);
239 if(trans->type == PM_TRANS_TYPE_REMOVEUPGRADE) {
240 /* check noupgrade */
241 if(alpm_list_find_str(handle->noupgrade, lp->data)) {
242 _alpm_log(PM_LOG_DEBUG, "Skipping removal of '%s' due to NoUpgrade\n",
243 file);
244 return;
248 /* we want to do a lstat here, and not a _alpm_lstat.
249 * if a directory in the package is actually a directory symlink on the
250 * filesystem, we want to work with the linked directory instead of the
251 * actual symlink */
252 if(lstat(file, &buf)) {
253 _alpm_log(PM_LOG_DEBUG, "file %s does not exist\n", file);
254 return;
257 if(S_ISDIR(buf.st_mode)) {
258 if(rmdir(file)) {
259 /* this is okay, other packages are probably using it (like /usr) */
260 _alpm_log(PM_LOG_DEBUG, "keeping directory %s\n", file);
261 } else {
262 _alpm_log(PM_LOG_DEBUG, "removing directory %s\n", file);
264 } else {
265 /* check the remove skip list before removing the file.
266 * see the big comment block in db_find_fileconflicts() for an
267 * explanation. */
268 if(alpm_list_find_str(trans->skip_remove, file)) {
269 _alpm_log(PM_LOG_DEBUG, "%s is in trans->skip_remove, skipping removal\n",
270 file);
271 return;
272 } else if(needbackup) {
273 /* if the file is flagged, back it up to .pacsave */
274 if(!(trans->flags & PM_TRANS_FLAG_NOSAVE)) {
275 char newpath[PATH_MAX];
276 snprintf(newpath, PATH_MAX, "%s.pacsave", file);
277 rename(file, newpath);
278 _alpm_log(PM_LOG_WARNING, _("%s saved as %s\n"), file, newpath);
279 return;
280 } else {
281 _alpm_log(PM_LOG_DEBUG, "transaction is set to NOSAVE, not backing up '%s'\n", file);
284 _alpm_log(PM_LOG_DEBUG, "unlinking %s\n", file);
286 if(unlink(file) == -1) {
287 _alpm_log(PM_LOG_ERROR, _("cannot remove file '%s': %s\n"),
288 (char *)lp->data, strerror(errno));
293 int _alpm_remove_commit(pmtrans_t *trans, pmdb_t *db)
295 pmpkg_t *info;
296 alpm_list_t *targ, *lp;
297 int pkg_count;
299 ALPM_LOG_FUNC;
301 ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
302 ASSERT(trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
304 pkg_count = alpm_list_count(trans->packages);
306 for(targ = trans->packages; targ; targ = targ->next) {
307 int position = 0;
308 char scriptlet[PATH_MAX];
309 alpm_list_t *files;
310 info = (pmpkg_t*)targ->data;
311 const char *pkgname = NULL;
313 if(handle->trans->state == STATE_INTERRUPTED) {
314 return(0);
317 /* get the name now so we can use it after package is removed */
318 pkgname = alpm_pkg_get_name(info);
319 snprintf(scriptlet, PATH_MAX, "%s%s-%s/install", db->path,
320 pkgname, alpm_pkg_get_version(info));
322 if(trans->type != PM_TRANS_TYPE_REMOVEUPGRADE) {
323 EVENT(trans, PM_TRANS_EVT_REMOVE_START, info, NULL);
324 _alpm_log(PM_LOG_DEBUG, "removing package %s-%s\n",
325 pkgname, alpm_pkg_get_version(info));
327 /* run the pre-remove scriptlet if it exists */
328 if(alpm_pkg_has_scriptlet(info) && !(trans->flags & PM_TRANS_FLAG_NOSCRIPTLET)) {
329 _alpm_runscriptlet(handle->root, scriptlet, "pre_remove",
330 alpm_pkg_get_version(info), NULL, trans);
334 files = alpm_pkg_get_files(info);
336 if(!(trans->flags & PM_TRANS_FLAG_DBONLY)) {
337 for(lp = files; lp; lp = lp->next) {
338 if(!can_remove_file(trans, lp->data)) {
339 _alpm_log(PM_LOG_DEBUG, "not removing package '%s', can't remove all files\n",
340 pkgname);
341 RET_ERR(PM_ERR_PKG_CANT_REMOVE, -1);
345 int filenum = alpm_list_count(files);
346 double percent = 0.0;
347 alpm_list_t *newfiles;
348 _alpm_log(PM_LOG_DEBUG, "removing %d files\n", filenum);
350 /* iterate through the list backwards, unlinking files */
351 newfiles = alpm_list_reverse(files);
352 for(lp = newfiles; lp; lp = alpm_list_next(lp)) {
353 unlink_file(info, lp, trans);
355 /* update progress bar after each file */
356 percent = (double)position / (double)filenum;
357 PROGRESS(trans, PM_TRANS_PROGRESS_REMOVE_START, info->name,
358 (double)(percent * 100), pkg_count,
359 (pkg_count - alpm_list_count(targ) + 1));
360 position++;
362 alpm_list_free(newfiles);
365 /* set progress to 100% after we finish unlinking files */
366 PROGRESS(trans, PM_TRANS_PROGRESS_REMOVE_START, pkgname, 100,
367 pkg_count, (pkg_count - alpm_list_count(targ) + 1));
369 if(trans->type != PM_TRANS_TYPE_REMOVEUPGRADE) {
370 /* run the post-remove script if it exists */
371 if(alpm_pkg_has_scriptlet(info) && !(trans->flags & PM_TRANS_FLAG_NOSCRIPTLET)) {
372 _alpm_runscriptlet(handle->root, scriptlet, "post_remove",
373 alpm_pkg_get_version(info), NULL, trans);
377 /* remove the package from the database */
378 _alpm_log(PM_LOG_DEBUG, "updating database\n");
379 _alpm_log(PM_LOG_DEBUG, "removing database entry '%s'\n", pkgname);
380 if(_alpm_db_remove(db, info) == -1) {
381 _alpm_log(PM_LOG_ERROR, _("could not remove database entry %s-%s\n"),
382 pkgname, alpm_pkg_get_version(info));
384 /* remove the package from the cache */
385 if(_alpm_db_remove_pkgfromcache(db, info) == -1) {
386 _alpm_log(PM_LOG_ERROR, _("could not remove entry '%s' from cache\n"),
387 pkgname);
390 /* call a done event if this isn't an upgrade */
391 if(trans->type != PM_TRANS_TYPE_REMOVEUPGRADE) {
392 EVENT(trans, PM_TRANS_EVT_REMOVE_DONE, info, NULL);
396 /* run ldconfig if it exists */
397 if(trans->type != PM_TRANS_TYPE_REMOVEUPGRADE) {
398 _alpm_log(PM_LOG_DEBUG, "running \"ldconfig -r %s\"\n", handle->root);
399 _alpm_ldconfig(handle->root);
402 return(0);
405 /* vim: set ts=2 sw=2 noet: */