Read pkgcache into hash
[pacman-ng.git] / lib / libalpm / remove.c
blob823795be3ff78722ea34864bd58d8e31d79f2bbc
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_list(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_list(db), 1, trans->remove, NULL);
132 int _alpm_remove_prepare(pmtrans_t *trans, pmdb_t *db, alpm_list_t **data)
134 alpm_list_t *lp;
136 ALPM_LOG_FUNC;
138 ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
139 ASSERT(trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
141 if((trans->flags & PM_TRANS_FLAG_RECURSE) && !(trans->flags & PM_TRANS_FLAG_CASCADE)) {
142 _alpm_log(PM_LOG_DEBUG, "finding removable dependencies\n");
143 _alpm_recursedeps(db, trans->remove, trans->flags & PM_TRANS_FLAG_RECURSEALL);
146 if(!(trans->flags & PM_TRANS_FLAG_NODEPS)) {
147 EVENT(trans, PM_TRANS_EVT_CHECKDEPS_START, NULL, NULL);
149 _alpm_log(PM_LOG_DEBUG, "looking for unsatisfied dependencies\n");
150 lp = alpm_checkdeps(_alpm_db_get_pkgcache_list(db), 1, trans->remove, NULL);
151 if(lp != NULL) {
153 if(trans->flags & PM_TRANS_FLAG_CASCADE) {
154 remove_prepare_cascade(trans, db, lp);
155 } else if (trans->flags & PM_TRANS_FLAG_UNNEEDED) {
156 /* Remove needed packages (which would break dependencies)
157 * from target list */
158 remove_prepare_keep_needed(trans, db, lp);
159 } else {
160 if(data) {
161 *data = lp;
162 } else {
163 alpm_list_free_inner(lp, (alpm_list_fn_free)_alpm_depmiss_free);
164 alpm_list_free(lp);
166 RET_ERR(PM_ERR_UNSATISFIED_DEPS, -1);
171 /* re-order w.r.t. dependencies */
172 _alpm_log(PM_LOG_DEBUG, "sorting by dependencies\n");
173 lp = _alpm_sortbydeps(trans->remove, 1);
174 /* free the old alltargs */
175 alpm_list_free(trans->remove);
176 trans->remove = lp;
178 /* -Rcs == -Rc then -Rs */
179 if((trans->flags & PM_TRANS_FLAG_CASCADE) && (trans->flags & PM_TRANS_FLAG_RECURSE)) {
180 _alpm_log(PM_LOG_DEBUG, "finding removable dependencies\n");
181 _alpm_recursedeps(db, trans->remove, trans->flags & PM_TRANS_FLAG_RECURSEALL);
184 if(!(trans->flags & PM_TRANS_FLAG_NODEPS)) {
185 EVENT(trans, PM_TRANS_EVT_CHECKDEPS_DONE, NULL, NULL);
188 return(0);
191 static int can_remove_file(const char *path, alpm_list_t *skip)
193 char file[PATH_MAX+1];
195 snprintf(file, PATH_MAX, "%s%s", handle->root, path);
197 if(alpm_list_find_str(skip, file)) {
198 /* return success because we will never actually remove this file */
199 return(1);
201 /* If we fail write permissions due to a read-only filesystem, abort.
202 * Assume all other possible failures are covered somewhere else */
203 if(access(file, W_OK) == -1) {
204 if(errno != EACCES && errno != ETXTBSY && access(file, F_OK) == 0) {
205 /* only return failure if the file ACTUALLY exists and we can't write to
206 * it - ignore "chmod -w" simple permission failures */
207 _alpm_log(PM_LOG_ERROR, _("cannot remove file '%s': %s\n"),
208 file, strerror(errno));
209 return(0);
213 return(1);
216 /* Helper function for iterating through a package's file and deleting them
217 * Used by _alpm_remove_commit. */
218 static void unlink_file(pmpkg_t *info, char *filename, alpm_list_t *skip_remove, int nosave)
220 struct stat buf;
221 char file[PATH_MAX+1];
223 ALPM_LOG_FUNC;
225 snprintf(file, PATH_MAX, "%s%s", handle->root, filename);
227 /* check the remove skip list before removing the file.
228 * see the big comment block in db_find_fileconflicts() for an
229 * explanation. */
230 if(alpm_list_find_str(skip_remove, filename)) {
231 _alpm_log(PM_LOG_DEBUG, "%s is in skip_remove, skipping removal\n",
232 file);
233 return;
236 /* we want to do a lstat here, and not a _alpm_lstat.
237 * if a directory in the package is actually a directory symlink on the
238 * filesystem, we want to work with the linked directory instead of the
239 * actual symlink */
240 if(lstat(file, &buf)) {
241 _alpm_log(PM_LOG_DEBUG, "file %s does not exist\n", file);
242 return;
245 if(S_ISDIR(buf.st_mode)) {
246 if(rmdir(file)) {
247 /* this is okay, other packages are probably using it (like /usr) */
248 _alpm_log(PM_LOG_DEBUG, "keeping directory %s\n", file);
249 } else {
250 _alpm_log(PM_LOG_DEBUG, "removing directory %s\n", file);
252 } else {
253 /* if the file needs backup and has been modified, back it up to .pacsave */
254 char *pkghash = _alpm_needbackup(filename, alpm_pkg_get_backup(info));
255 if(pkghash) {
256 if(nosave) {
257 _alpm_log(PM_LOG_DEBUG, "transaction is set to NOSAVE, not backing up '%s'\n", file);
258 FREE(pkghash);
259 } else {
260 char *filehash = alpm_compute_md5sum(file);
261 int cmp = strcmp(filehash,pkghash);
262 FREE(filehash);
263 FREE(pkghash);
264 if(cmp != 0) {
265 char newpath[PATH_MAX];
266 snprintf(newpath, PATH_MAX, "%s.pacsave", file);
267 rename(file, newpath);
268 _alpm_log(PM_LOG_WARNING, _("%s saved as %s\n"), file, newpath);
269 alpm_logaction("warning: %s saved as %s\n", file, newpath);
270 return;
275 _alpm_log(PM_LOG_DEBUG, "unlinking %s\n", file);
277 if(unlink(file) == -1) {
278 _alpm_log(PM_LOG_ERROR, _("cannot remove file '%s': %s\n"),
279 filename, strerror(errno));
284 int _alpm_upgraderemove_package(pmpkg_t *oldpkg, pmpkg_t *newpkg,
285 pmtrans_t *trans)
287 alpm_list_t *skip_remove, *b;
288 alpm_list_t *newfiles, *lp;
289 size_t filenum;
290 alpm_list_t *files = alpm_pkg_get_files(oldpkg);
291 const char *pkgname = alpm_pkg_get_name(oldpkg);
293 ALPM_LOG_FUNC;
295 _alpm_log(PM_LOG_DEBUG, "removing old package first (%s-%s)\n",
296 oldpkg->name, oldpkg->version);
298 if(trans->flags & PM_TRANS_FLAG_DBONLY) {
299 goto db;
302 /* copy the remove skiplist over */
303 skip_remove = alpm_list_join(
304 alpm_list_strdup(trans->skip_remove),
305 alpm_list_strdup(handle->noupgrade));
306 /* Add files in the NEW backup array to the skip_remove array
307 * so this removal operation doesn't kill them */
308 /* old package backup list */
309 alpm_list_t *filelist = alpm_pkg_get_files(newpkg);
310 for(b = alpm_pkg_get_backup(newpkg); b; b = b->next) {
311 char *backup = _alpm_backup_file(b->data);
312 /* safety check (fix the upgrade026 pactest) */
313 if(!alpm_list_find_str(filelist, backup)) {
314 FREE(backup);
315 continue;
317 _alpm_log(PM_LOG_DEBUG, "adding %s to the skip_remove array\n", backup);
318 skip_remove = alpm_list_add(skip_remove, backup);
321 for(lp = files; lp; lp = lp->next) {
322 if(!can_remove_file(lp->data, skip_remove)) {
323 _alpm_log(PM_LOG_DEBUG, "not removing package '%s', can't remove all files\n",
324 pkgname);
325 RET_ERR(PM_ERR_PKG_CANT_REMOVE, -1);
329 filenum = alpm_list_count(files);
330 _alpm_log(PM_LOG_DEBUG, "removing %ld files\n", (unsigned long)filenum);
332 /* iterate through the list backwards, unlinking files */
333 newfiles = alpm_list_reverse(files);
334 for(lp = newfiles; lp; lp = alpm_list_next(lp)) {
335 unlink_file(oldpkg, lp->data, skip_remove, 0);
337 alpm_list_free(newfiles);
338 FREELIST(skip_remove);
341 /* remove the package from the database */
342 _alpm_log(PM_LOG_DEBUG, "updating database\n");
343 _alpm_log(PM_LOG_DEBUG, "removing database entry '%s'\n", pkgname);
344 if(_alpm_local_db_remove(handle->db_local, oldpkg) == -1) {
345 _alpm_log(PM_LOG_ERROR, _("could not remove database entry %s-%s\n"),
346 pkgname, alpm_pkg_get_version(oldpkg));
348 /* remove the package from the cache */
349 if(_alpm_db_remove_pkgfromcache(handle->db_local, oldpkg) == -1) {
350 _alpm_log(PM_LOG_ERROR, _("could not remove entry '%s' from cache\n"),
351 pkgname);
354 return(0);
357 int _alpm_remove_packages(pmtrans_t *trans, pmdb_t *db)
359 pmpkg_t *info;
360 alpm_list_t *targ, *lp;
361 size_t pkg_count;
363 ALPM_LOG_FUNC;
365 ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
366 ASSERT(trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
368 pkg_count = alpm_list_count(trans->remove);
370 for(targ = trans->remove; targ; targ = targ->next) {
371 int position = 0;
372 char scriptlet[PATH_MAX];
373 info = (pmpkg_t*)targ->data;
374 const char *pkgname = NULL;
375 size_t targcount = alpm_list_count(targ);
377 if(handle->trans->state == STATE_INTERRUPTED) {
378 return(0);
381 /* get the name now so we can use it after package is removed */
382 pkgname = alpm_pkg_get_name(info);
383 snprintf(scriptlet, PATH_MAX, "%s%s-%s/install",
384 _alpm_db_path(db), pkgname, alpm_pkg_get_version(info));
386 EVENT(trans, PM_TRANS_EVT_REMOVE_START, info, NULL);
387 _alpm_log(PM_LOG_DEBUG, "removing package %s-%s\n",
388 pkgname, alpm_pkg_get_version(info));
390 /* run the pre-remove scriptlet if it exists */
391 if(alpm_pkg_has_scriptlet(info) && !(trans->flags & PM_TRANS_FLAG_NOSCRIPTLET)) {
392 _alpm_runscriptlet(handle->root, scriptlet, "pre_remove",
393 alpm_pkg_get_version(info), NULL, trans);
396 if(!(trans->flags & PM_TRANS_FLAG_DBONLY)) {
397 alpm_list_t *files = alpm_pkg_get_files(info);
398 alpm_list_t *newfiles;
399 size_t filenum;
401 for(lp = files; lp; lp = lp->next) {
402 if(!can_remove_file(lp->data, NULL)) {
403 _alpm_log(PM_LOG_DEBUG, "not removing package '%s', can't remove all files\n",
404 pkgname);
405 RET_ERR(PM_ERR_PKG_CANT_REMOVE, -1);
409 filenum = alpm_list_count(files);
410 _alpm_log(PM_LOG_DEBUG, "removing %ld files\n", (unsigned long)filenum);
412 /* init progress bar */
413 PROGRESS(trans, PM_TRANS_PROGRESS_REMOVE_START, info->name, 0,
414 pkg_count, (pkg_count - targcount + 1));
416 /* iterate through the list backwards, unlinking files */
417 newfiles = alpm_list_reverse(files);
418 for(lp = newfiles; lp; lp = alpm_list_next(lp)) {
419 int percent;
420 unlink_file(info, lp->data, NULL, trans->flags & PM_TRANS_FLAG_NOSAVE);
422 /* update progress bar after each file */
423 percent = (position * 100) / filenum;
424 PROGRESS(trans, PM_TRANS_PROGRESS_REMOVE_START, info->name,
425 percent, pkg_count, (pkg_count - targcount + 1));
426 position++;
428 alpm_list_free(newfiles);
431 /* set progress to 100% after we finish unlinking files */
432 PROGRESS(trans, PM_TRANS_PROGRESS_REMOVE_START, pkgname, 100,
433 pkg_count, (pkg_count - targcount + 1));
435 /* run the post-remove script if it exists */
436 if(alpm_pkg_has_scriptlet(info) && !(trans->flags & PM_TRANS_FLAG_NOSCRIPTLET)) {
437 _alpm_runscriptlet(handle->root, scriptlet, "post_remove",
438 alpm_pkg_get_version(info), NULL, trans);
441 /* remove the package from the database */
442 _alpm_log(PM_LOG_DEBUG, "updating database\n");
443 _alpm_log(PM_LOG_DEBUG, "removing database entry '%s'\n", pkgname);
444 if(_alpm_local_db_remove(db, info) == -1) {
445 _alpm_log(PM_LOG_ERROR, _("could not remove database entry %s-%s\n"),
446 pkgname, alpm_pkg_get_version(info));
448 /* remove the package from the cache */
449 if(_alpm_db_remove_pkgfromcache(db, info) == -1) {
450 _alpm_log(PM_LOG_ERROR, _("could not remove entry '%s' from cache\n"),
451 pkgname);
454 EVENT(trans, PM_TRANS_EVT_REMOVE_DONE, info, NULL);
457 /* run ldconfig if it exists */
458 _alpm_ldconfig(handle->root);
460 return(0);
463 /* vim: set ts=2 sw=2 noet: */