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/>.
37 #include "alpm_list.h"
49 int _alpm_remove_loadtarget(pmtrans_t
*trans
, pmdb_t
*db
, char *name
)
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
)) {
72 QUESTION(trans
, PM_TRANS_CONV_REMOVE_HOLDPKG
, info
, NULL
, NULL
, &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
));
84 static void remove_prepare_cascade(pmtrans_t
*trans
, pmdb_t
*db
,
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
);
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
));
101 _alpm_log(PM_LOG_ERROR
, _("could not find %s in database -- skipping\n"),
105 alpm_list_free_inner(lp
, (alpm_list_fn_free
)_alpm_depmiss_free
);
107 lp
= alpm_checkdeps(db
, 1, trans
->packages
, NULL
);
111 static void remove_prepare_keep_needed(pmtrans_t
*trans
, pmdb_t
*db
,
116 /* Remove needed packages (which break dependencies) from the target list */
119 for(i
= lp
; i
; i
= i
->next
) {
120 pmdepmissing_t
*miss
= (pmdepmissing_t
*)i
->data
;
123 trans
->packages
= alpm_list_remove(trans
->packages
, miss
->causingpkg
,
124 _alpm_pkgname_pkg_cmp
, &vpkg
);
127 _alpm_log(PM_LOG_WARNING
, "removing %s from the target-list\n",
128 alpm_pkg_get_name(pkg
));
132 alpm_list_free_inner(lp
, (alpm_list_fn_free
)_alpm_depmiss_free
);
134 lp
= alpm_checkdeps(db
, 1, trans
->packages
, NULL
);
138 int _alpm_remove_prepare(pmtrans_t
*trans
, pmdb_t
*db
, alpm_list_t
**data
)
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
) {
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
);
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
);
169 alpm_list_free_inner(lp
, (alpm_list_fn_free
)_alpm_depmiss_free
);
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
);
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 */
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
));
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
)
227 char file
[PATH_MAX
+1];
231 char *hash
= _alpm_needbackup(lp
->data
, alpm_pkg_get_backup(info
));
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",
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
252 if(lstat(file
, &buf
)) {
253 _alpm_log(PM_LOG_DEBUG
, "file %s does not exist\n", file
);
257 if(S_ISDIR(buf
.st_mode
)) {
259 /* this is okay, other packages are probably using it (like /usr) */
260 _alpm_log(PM_LOG_DEBUG
, "keeping directory %s\n", file
);
262 _alpm_log(PM_LOG_DEBUG
, "removing directory %s\n", file
);
265 /* check the remove skip list before removing the file.
266 * see the big comment block in db_find_fileconflicts() for an
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",
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
);
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
)
296 alpm_list_t
*targ
, *lp
;
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
) {
308 char scriptlet
[PATH_MAX
];
310 info
= (pmpkg_t
*)targ
->data
;
311 const char *pkgname
= NULL
;
313 if(handle
->trans
->state
== STATE_INTERRUPTED
) {
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",
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));
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"),
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
);
405 /* vim: set ts=2 sw=2 noet: */