1 /*
2  *  remove.c
3  *
4  *  Copyright (c) 2006-2018 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>
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <limits.h>
29 #include <dirent.h>
30 #include <regex.h>
31 #include <unistd.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 
35 /* libalpm */
36 #include "remove.h"
37 #include "alpm_list.h"
38 #include "alpm.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 "filelist.h"
48 
49 /**
50  * @brief Add a package removal action to the transaction.
51  *
52  * @param handle the context handle
53  * @param pkg the package to uninstall
54  *
55  * @return 0 on success, -1 on error
56  */
alpm_remove_pkg(alpm_handle_t * handle,alpm_pkg_t * pkg)57 int SYMEXPORT alpm_remove_pkg(alpm_handle_t *handle, alpm_pkg_t *pkg)
58 {
59 	const char *pkgname;
60 	alpm_trans_t *trans;
61 	alpm_pkg_t *copy;
62 
63 	/* Sanity checks */
64 	CHECK_HANDLE(handle, return -1);
65 	ASSERT(pkg != NULL, RET_ERR(handle, ALPM_ERR_WRONG_ARGS, -1));
66 	ASSERT(pkg->origin == ALPM_PKG_FROM_LOCALDB,
67 			RET_ERR(handle, ALPM_ERR_WRONG_ARGS, -1));
68 	ASSERT(handle == pkg->handle, RET_ERR(handle, ALPM_ERR_WRONG_ARGS, -1));
69 	trans = handle->trans;
70 	ASSERT(trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, -1));
71 	ASSERT(trans->state == STATE_INITIALIZED,
72 			RET_ERR(handle, ALPM_ERR_TRANS_NOT_INITIALIZED, -1));
73 
74 	pkgname = pkg->name;
75 
76 	if(alpm_pkg_find(trans->remove, pkgname)) {
77 		RET_ERR(handle, ALPM_ERR_TRANS_DUP_TARGET, -1);
78 	}
79 
80 	_alpm_log(handle, ALPM_LOG_DEBUG, "adding package %s to the transaction remove list\n",
81 			pkgname);
82 	if(_alpm_pkg_dup(pkg, &copy) == -1) {
83 		return -1;
84 	}
85 	trans->remove = alpm_list_add(trans->remove, copy);
86 	return 0;
87 }
88 
89 /**
90  * @brief Add dependencies to the removal transaction for cascading.
91  *
92  * @param handle the context handle
93  * @param lp list of missing dependencies caused by the removal transaction
94  *
95  * @return 0 on success, -1 on error
96  */
remove_prepare_cascade(alpm_handle_t * handle,alpm_list_t * lp)97 static int remove_prepare_cascade(alpm_handle_t *handle, alpm_list_t *lp)
98 {
99 	alpm_trans_t *trans = handle->trans;
100 
101 	while(lp) {
102 		alpm_list_t *i;
103 		for(i = lp; i; i = i->next) {
104 			alpm_depmissing_t *miss = i->data;
105 			alpm_pkg_t *info = _alpm_db_get_pkgfromcache(handle->db_local, miss->target);
106 			if(info) {
107 				alpm_pkg_t *copy;
108 				if(!alpm_pkg_find(trans->remove, info->name)) {
109 					_alpm_log(handle, ALPM_LOG_DEBUG, "pulling %s in target list\n",
110 							info->name);
111 					if(_alpm_pkg_dup(info, &copy) == -1) {
112 						return -1;
113 					}
114 					trans->remove = alpm_list_add(trans->remove, copy);
115 				}
116 			} else {
117 				_alpm_log(handle, ALPM_LOG_ERROR,
118 						_("could not find %s in database -- skipping\n"), miss->target);
119 			}
120 		}
121 		alpm_list_free_inner(lp, (alpm_list_fn_free)alpm_depmissing_free);
122 		alpm_list_free(lp);
123 		lp = alpm_checkdeps(handle, _alpm_db_get_pkgcache(handle->db_local),
124 				trans->remove, NULL, 1);
125 	}
126 	return 0;
127 }
128 
129 /**
130  * @brief Remove needed packages from the removal transaction.
131  *
132  * @param handle the context handle
133  * @param lp list of missing dependencies caused by the removal transaction
134  */
remove_prepare_keep_needed(alpm_handle_t * handle,alpm_list_t * lp)135 static void remove_prepare_keep_needed(alpm_handle_t *handle, alpm_list_t *lp)
136 {
137 	alpm_trans_t *trans = handle->trans;
138 
139 	/* Remove needed packages (which break dependencies) from target list */
140 	while(lp != NULL) {
141 		alpm_list_t *i;
142 		for(i = lp; i; i = i->next) {
143 			alpm_depmissing_t *miss = i->data;
144 			void *vpkg;
145 			alpm_pkg_t *pkg = alpm_pkg_find(trans->remove, miss->causingpkg);
146 			if(pkg == NULL) {
147 				continue;
148 			}
149 			trans->remove = alpm_list_remove(trans->remove, pkg, _alpm_pkg_cmp,
150 					&vpkg);
151 			pkg = vpkg;
152 			if(pkg) {
153 				_alpm_log(handle, ALPM_LOG_WARNING, _("removing %s from target list\n"),
154 						pkg->name);
155 				_alpm_pkg_free(pkg);
156 			}
157 		}
158 		alpm_list_free_inner(lp, (alpm_list_fn_free)alpm_depmissing_free);
159 		alpm_list_free(lp);
160 		lp = alpm_checkdeps(handle, _alpm_db_get_pkgcache(handle->db_local),
161 				trans->remove, NULL, 1);
162 	}
163 }
164 
165 /**
166  * @brief Send a callback for any optdepend being removed.
167  *
168  * @param handle the context handle
169  * @param lp list of packages to be removed
170  */
remove_notify_needed_optdepends(alpm_handle_t * handle,alpm_list_t * lp)171 static void remove_notify_needed_optdepends(alpm_handle_t *handle, alpm_list_t *lp)
172 {
173 	alpm_list_t *i;
174 
175 	for(i = _alpm_db_get_pkgcache(handle->db_local); i; i = alpm_list_next(i)) {
176 		alpm_pkg_t *pkg = i->data;
177 		alpm_list_t *optdeps = alpm_pkg_get_optdepends(pkg);
178 
179 		if(optdeps && !alpm_pkg_find(lp, pkg->name)) {
180 			alpm_list_t *j;
181 			for(j = optdeps; j; j = alpm_list_next(j)) {
182 				alpm_depend_t *optdep = j->data;
183 				char *optstring = alpm_dep_compute_string(optdep);
184 				if(alpm_find_satisfier(lp, optstring)) {
185 					alpm_event_optdep_removal_t event = {
186 						.type = ALPM_EVENT_OPTDEP_REMOVAL,
187 						.pkg = pkg,
188 						.optdep = optdep
189 					};
190 					EVENT(handle, &event);
191 				}
192 				free(optstring);
193 			}
194 		}
195 	}
196 }
197 
198 /**
199  * @brief Transaction preparation for remove actions.
200  *
201  * This functions takes a pointer to a alpm_list_t which will be
202  * filled with a list of alpm_depmissing_t* objects representing
203  * the packages blocking the transaction.
204  *
205  * @param handle the context handle
206  * @param data a pointer to an alpm_list_t* to fill
207  *
208  * @return 0 on success, -1 on error
209  */
_alpm_remove_prepare(alpm_handle_t * handle,alpm_list_t ** data)210 int _alpm_remove_prepare(alpm_handle_t *handle, alpm_list_t **data)
211 {
212 	alpm_list_t *lp;
213 	alpm_trans_t *trans = handle->trans;
214 	alpm_db_t *db = handle->db_local;
215 	alpm_event_t event;
216 
217 	if((trans->flags & ALPM_TRANS_FLAG_RECURSE)
218 			&& !(trans->flags & ALPM_TRANS_FLAG_CASCADE)) {
219 		_alpm_log(handle, ALPM_LOG_DEBUG, "finding removable dependencies\n");
220 		if(_alpm_recursedeps(db, &trans->remove,
221 				trans->flags & ALPM_TRANS_FLAG_RECURSEALL)) {
222 			return -1;
223 		}
224 	}
225 
226 	if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
227 		event.type = ALPM_EVENT_CHECKDEPS_START;
228 		EVENT(handle, &event);
229 
230 		_alpm_log(handle, ALPM_LOG_DEBUG, "looking for unsatisfied dependencies\n");
231 		lp = alpm_checkdeps(handle, _alpm_db_get_pkgcache(db), trans->remove, NULL, 1);
232 		if(lp != NULL) {
233 
234 			if(trans->flags & ALPM_TRANS_FLAG_CASCADE) {
235 				if(remove_prepare_cascade(handle, lp)) {
236 					return -1;
237 				}
238 			} else if(trans->flags & ALPM_TRANS_FLAG_UNNEEDED) {
239 				/* Remove needed packages (which would break dependencies)
240 				 * from target list */
241 				remove_prepare_keep_needed(handle, lp);
242 			} else {
243 				if(data) {
244 					*data = lp;
245 				} else {
246 					alpm_list_free_inner(lp,
247 							(alpm_list_fn_free)alpm_depmissing_free);
248 					alpm_list_free(lp);
249 				}
250 				RET_ERR(handle, ALPM_ERR_UNSATISFIED_DEPS, -1);
251 			}
252 		}
253 	}
254 
255 	/* -Rcs == -Rc then -Rs */
256 	if((trans->flags & ALPM_TRANS_FLAG_CASCADE)
257 			&& (trans->flags & ALPM_TRANS_FLAG_RECURSE)) {
258 		_alpm_log(handle, ALPM_LOG_DEBUG, "finding removable dependencies\n");
259 		if(_alpm_recursedeps(db, &trans->remove,
260 					trans->flags & ALPM_TRANS_FLAG_RECURSEALL)) {
261 			return -1;
262 		}
263 	}
264 
265 	/* Note packages being removed that are optdepends for installed packages */
266 	if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
267 		remove_notify_needed_optdepends(handle, trans->remove);
268 	}
269 
270 	if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
271 		event.type = ALPM_EVENT_CHECKDEPS_DONE;
272 		EVENT(handle, &event);
273 	}
274 
275 	return 0;
276 }
277 
278 /**
279  * @brief Test if a directory is being used as a mountpoint.
280  *
281  * @param handle context handle
282  * @param directory path to test, must be absolute and include trailing '/'
283  * @param stbuf stat result for @a directory, may be NULL
284  *
285  * @return 0 if @a directory is not a mountpoint or on error, 1 if @a directory
286  * is a mountpoint
287  */
dir_is_mountpoint(alpm_handle_t * handle,const char * directory,const struct stat * stbuf)288 static int dir_is_mountpoint(alpm_handle_t *handle, const char *directory,
289 		const struct stat *stbuf)
290 {
291 	char parent_dir[PATH_MAX];
292 	struct stat parent_stbuf;
293 	dev_t dir_st_dev;
294 
295 	if(stbuf == NULL) {
296 		struct stat dir_stbuf;
297 		if(stat(directory, &dir_stbuf) < 0) {
298 			_alpm_log(handle, ALPM_LOG_DEBUG,
299 					"failed to stat directory %s: %s\n",
300 					directory, strerror(errno));
301 			return 0;
302 		}
303 		dir_st_dev = dir_stbuf.st_dev;
304 	} else {
305 		dir_st_dev = stbuf->st_dev;
306 	}
307 
308 	snprintf(parent_dir, PATH_MAX, "%s..", directory);
309 	if(stat(parent_dir, &parent_stbuf) < 0) {
310 		_alpm_log(handle, ALPM_LOG_DEBUG,
311 				"failed to stat parent of %s: %s: %s\n",
312 				directory, parent_dir, strerror(errno));
313 		return 0;
314 	}
315 
316 	return dir_st_dev != parent_stbuf.st_dev;
317 }
318 
319 /**
320  * @brief Check if alpm can delete a file.
321  *
322  * @param handle the context handle
323  * @param file file to be removed
324  *
325  * @return 1 if the file can be deleted, 0 if it cannot be deleted
326  */
can_remove_file(alpm_handle_t * handle,const alpm_file_t * file)327 static int can_remove_file(alpm_handle_t *handle, const alpm_file_t *file)
328 {
329 	char filepath[PATH_MAX];
330 
331 	snprintf(filepath, PATH_MAX, "%s%s", handle->root, file->name);
332 
333 	if(file->name[strlen(file->name) - 1] == '/' &&
334 			dir_is_mountpoint(handle, filepath, NULL)) {
335 		/* we do not remove mountpoints */
336 		return 1;
337 	}
338 
339 	/* If we fail write permissions due to a read-only filesystem, abort.
340 	 * Assume all other possible failures are covered somewhere else */
341 	if(_alpm_access(handle, NULL, filepath, W_OK) == -1) {
342 		if(errno != EACCES && errno != ETXTBSY && access(filepath, F_OK) == 0) {
343 			/* only return failure if the file ACTUALLY exists and we can't write to
344 			 * it - ignore "chmod -w" simple permission failures */
345 			_alpm_log(handle, ALPM_LOG_ERROR, _("cannot remove file '%s': %s\n"),
346 					filepath, strerror(errno));
347 			return 0;
348 		}
349 	}
350 
351 	return 1;
352 }
353 
shift_pacsave(alpm_handle_t * handle,const char * file)354 static void shift_pacsave(alpm_handle_t *handle, const char *file)
355 {
356 	DIR *dir = NULL;
357 	struct dirent *ent;
358 	struct stat st;
359 	regex_t reg;
360 
361 	const char *basename;
362 	char *dirname;
363 	char oldfile[PATH_MAX];
364 	char newfile[PATH_MAX];
365 	char regstr[PATH_MAX];
366 
367 	unsigned long log_max = 0;
368 	size_t basename_len;
369 
370 	dirname = mdirname(file);
371 	if(!dirname) {
372 		return;
373 	}
374 
375 	basename = mbasename(file);
376 	basename_len = strlen(basename);
377 
378 	snprintf(regstr, PATH_MAX, "^%s\\.pacsave\\.([[:digit:]]+)$", basename);
379 	if(regcomp(&reg, regstr, REG_EXTENDED | REG_NEWLINE) != 0) {
380 		goto cleanup;
381 	}
382 
383 	dir = opendir(dirname);
384 	if(dir == NULL) {
385 		_alpm_log(handle, ALPM_LOG_ERROR, _("could not open directory: %s: %s\n"),
386 							dirname, strerror(errno));
387 		goto cleanup;
388 	}
389 
390 	while((ent = readdir(dir)) != NULL) {
391 		if(strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
392 			continue;
393 		}
394 
395 		if(regexec(&reg, ent->d_name, 0, 0, 0) == 0) {
396 			unsigned long cur_log;
397 			cur_log = strtoul(ent->d_name + basename_len + strlen(".pacsave."), NULL, 10);
398 			if(cur_log > log_max) {
399 				log_max = cur_log;
400 			}
401 		}
402 	}
403 
404 	/* Shift pacsaves */
405 	unsigned long i;
406 	for(i = log_max + 1; i > 1; i--) {
407 		if(snprintf(oldfile, PATH_MAX, "%s.pacsave.%lu", file, i-1) >= PATH_MAX
408 				|| snprintf(newfile, PATH_MAX, "%s.pacsave.%lu", file, i) >= PATH_MAX) {
409 			_alpm_log(handle, ALPM_LOG_ERROR,
410 					_("could not backup %s due to PATH_MAX overflow\n"), file);
411 			goto cleanup;
412 		}
413 		rename(oldfile, newfile);
414 	}
415 
416 	if(snprintf(oldfile, PATH_MAX, "%s.pacsave", file) >= PATH_MAX
417 			|| snprintf(newfile, PATH_MAX, "%s.1", oldfile) >= PATH_MAX) {
418 		_alpm_log(handle, ALPM_LOG_ERROR,
419 				_("could not backup %s due to PATH_MAX overflow\n"), file);
420 		goto cleanup;
421 	}
422 	if(stat(oldfile, &st) == 0) {
423 		rename(oldfile, newfile);
424 	}
425 
426 	regfree(&reg);
427 
428 cleanup:
429 	free(dirname);
430 	closedir(dir);
431 }
432 
433 
434 /**
435  * @brief Unlink a package file, backing it up if necessary.
436  *
437  * @param handle the context handle
438  * @param oldpkg the package being removed
439  * @param newpkg the package replacing \a oldpkg
440  * @param fileobj file to remove
441  * @param nosave whether files should be backed up
442  *
443  * @return 0 on success, -1 if there was an error unlinking the file, 1 if the
444  * file was skipped or did not exist
445  */
unlink_file(alpm_handle_t * handle,alpm_pkg_t * oldpkg,alpm_pkg_t * newpkg,const alpm_file_t * fileobj,int nosave)446 static int unlink_file(alpm_handle_t *handle, alpm_pkg_t *oldpkg,
447 		alpm_pkg_t *newpkg, const alpm_file_t *fileobj, int nosave)
448 {
449 	struct stat buf;
450 	char file[PATH_MAX];
451 	int file_len;
452 
453 	file_len = snprintf(file, PATH_MAX, "%s%s", handle->root, fileobj->name);
454 	if(file_len <= 0 || file_len >= PATH_MAX) {
455 		/* 0 is a valid value from snprintf, but should be impossible here */
456 		_alpm_log(handle, ALPM_LOG_DEBUG, "path too long to unlink %s%s\n",
457 				handle->root, fileobj->name);
458 		return -1;
459 	} else if(file[file_len-1] == '/') {
460 		/* trailing slashes cause errors and confusing messages if the user has
461 		 * replaced a directory with a symlink */
462 		file[--file_len] = '\0';
463 	}
464 
465 	if(llstat(file, &buf)) {
466 		_alpm_log(handle, ALPM_LOG_DEBUG, "file %s does not exist\n", file);
467 		return 1;
468 	}
469 
470 	if(S_ISDIR(buf.st_mode)) {
471 		ssize_t files = _alpm_files_in_directory(handle, file, 0);
472 		/* if we have files, no need to remove the directory */
473 		if(files > 0) {
474 			_alpm_log(handle, ALPM_LOG_DEBUG, "keeping directory %s (contains files)\n",
475 					file);
476 		} else if(files < 0) {
477 			_alpm_log(handle, ALPM_LOG_DEBUG,
478 					"keeping directory %s (could not count files)\n", file);
479 		} else if(newpkg && alpm_filelist_contains(alpm_pkg_get_files(newpkg),
480 					fileobj->name)) {
481 			_alpm_log(handle, ALPM_LOG_DEBUG,
482 					"keeping directory %s (in new package)\n", file);
483 		} else if(dir_is_mountpoint(handle, file, &buf)) {
484 			_alpm_log(handle, ALPM_LOG_DEBUG,
485 					"keeping directory %s (mountpoint)\n", file);
486 		} else {
487 			/* one last check- does any other package own this file? */
488 			alpm_list_t *local, *local_pkgs;
489 			int found = 0;
490 			local_pkgs = _alpm_db_get_pkgcache(handle->db_local);
491 			for(local = local_pkgs; local && !found; local = local->next) {
492 				alpm_pkg_t *local_pkg = local->data;
493 				alpm_filelist_t *filelist;
494 
495 				/* we duplicated the package when we put it in the removal list, so we
496 				 * so we can't use direct pointer comparison here. */
497 				if(oldpkg->name_hash == local_pkg->name_hash
498 						&& strcmp(oldpkg->name, local_pkg->name) == 0) {
499 					continue;
500 				}
501 				filelist = alpm_pkg_get_files(local_pkg);
502 				if(alpm_filelist_contains(filelist, fileobj->name)) {
503 					_alpm_log(handle, ALPM_LOG_DEBUG,
504 							"keeping directory %s (owned by %s)\n", file, local_pkg->name);
505 					found = 1;
506 				}
507 			}
508 			if(!found) {
509 				if(rmdir(file)) {
510 					_alpm_log(handle, ALPM_LOG_DEBUG,
511 							"directory removal of %s failed: %s\n", file, strerror(errno));
512 					return -1;
513 				} else {
514 					_alpm_log(handle, ALPM_LOG_DEBUG,
515 							"removed directory %s (no remaining owners)\n", file);
516 				}
517 			}
518 		}
519 	} else {
520 		/* if the file needs backup and has been modified, back it up to .pacsave */
521 		alpm_backup_t *backup = _alpm_needbackup(fileobj->name, oldpkg);
522 		if(backup) {
523 			if(nosave) {
524 				_alpm_log(handle, ALPM_LOG_DEBUG, "transaction is set to NOSAVE, not backing up '%s'\n", file);
525 			} else {
526 				char *filehash = alpm_compute_md5sum(file);
527 				int cmp = filehash ? strcmp(filehash, backup->hash) : 0;
528 				FREE(filehash);
529 				if(cmp != 0) {
530 					alpm_event_pacsave_created_t event = {
531 						.type = ALPM_EVENT_PACSAVE_CREATED,
532 						.oldpkg = oldpkg,
533 						.file = file
534 					};
535 					char *newpath;
536 					size_t len = strlen(file) + 8 + 1;
537 					MALLOC(newpath, len, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
538 					shift_pacsave(handle, file);
539 					snprintf(newpath, len, "%s.pacsave", file);
540 					if(rename(file, newpath)) {
541 						_alpm_log(handle, ALPM_LOG_ERROR, _("could not rename %s to %s (%s)\n"),
542 								file, newpath, strerror(errno));
543 						alpm_logaction(handle, ALPM_CALLER_PREFIX,
544 								"error: could not rename %s to %s (%s)\n",
545 								file, newpath, strerror(errno));
546 						free(newpath);
547 						return -1;
548 					}
549 					EVENT(handle, &event);
550 					alpm_logaction(handle, ALPM_CALLER_PREFIX,
551 							"warning: %s saved as %s\n", file, newpath);
552 					free(newpath);
553 					return 0;
554 				}
555 			}
556 		}
557 
558 		_alpm_log(handle, ALPM_LOG_DEBUG, "unlinking %s\n", file);
559 
560 		if(unlink(file) == -1) {
561 			_alpm_log(handle, ALPM_LOG_ERROR, _("cannot remove %s (%s)\n"),
562 					file, strerror(errno));
563 			alpm_logaction(handle, ALPM_CALLER_PREFIX,
564 					"error: cannot remove %s (%s)\n", file, strerror(errno));
565 			return -1;
566 		}
567 	}
568 	return 0;
569 }
570 
571 /**
572  * @brief Check if a package file should be removed.
573  *
574  * @param handle the context handle
575  * @param newpkg the package replacing the current owner of \a path
576  * @param path file to be removed
577  *
578  * @return 1 if the file should be skipped, 0 if it should be removed
579  */
should_skip_file(alpm_handle_t * handle,alpm_pkg_t * newpkg,const char * path)580 static int should_skip_file(alpm_handle_t *handle,
581 		alpm_pkg_t *newpkg, const char *path)
582 {
583 	return _alpm_fnmatch_patterns(handle->noupgrade, path) == 0
584 		|| alpm_list_find_str(handle->trans->skip_remove, path)
585 		|| (newpkg && _alpm_needbackup(path, newpkg)
586 				&& alpm_filelist_contains(alpm_pkg_get_files(newpkg), path));
587 }
588 
589 /**
590  * @brief Remove a package's files, optionally skipping its replacement's
591  * files.
592  *
593  * @param handle the context handle
594  * @param oldpkg package to remove
595  * @param newpkg package to replace \a oldpkg (optional)
596  * @param targ_count current index within the transaction (1-based)
597  * @param pkg_count the number of packages affected by the transaction
598  *
599  * @return 0 on success, -1 if alpm lacks permission to delete some of the
600  * files, >0 the number of files alpm was unable to delete
601  */
remove_package_files(alpm_handle_t * handle,alpm_pkg_t * oldpkg,alpm_pkg_t * newpkg,size_t targ_count,size_t pkg_count)602 static int remove_package_files(alpm_handle_t *handle,
603 		alpm_pkg_t *oldpkg, alpm_pkg_t *newpkg,
604 		size_t targ_count, size_t pkg_count)
605 {
606 	alpm_filelist_t *filelist;
607 	size_t i;
608 	int err = 0;
609 	int nosave = handle->trans->flags & ALPM_TRANS_FLAG_NOSAVE;
610 
611 	filelist = alpm_pkg_get_files(oldpkg);
612 	for(i = 0; i < filelist->count; i++) {
613 		alpm_file_t *file = filelist->files + i;
614 		if(!should_skip_file(handle, newpkg, file->name)
615 				&& !can_remove_file(handle, file)) {
616 			_alpm_log(handle, ALPM_LOG_DEBUG,
617 					"not removing package '%s', can't remove all files\n",
618 					oldpkg->name);
619 			RET_ERR(handle, ALPM_ERR_PKG_CANT_REMOVE, -1);
620 		}
621 	}
622 
623 	_alpm_log(handle, ALPM_LOG_DEBUG, "removing %zu files\n", filelist->count);
624 
625 	if(!newpkg) {
626 		/* init progress bar, but only on true remove transactions */
627 		PROGRESS(handle, ALPM_PROGRESS_REMOVE_START, oldpkg->name, 0,
628 				pkg_count, targ_count);
629 	}
630 
631 	/* iterate through the list backwards, unlinking files */
632 	for(i = filelist->count; i > 0; i--) {
633 		alpm_file_t *file = filelist->files + i - 1;
634 
635 		/* check the remove skip list before removing the file.
636 		 * see the big comment block in db_find_fileconflicts() for an
637 		 * explanation. */
638 		if(should_skip_file(handle, newpkg, file->name)) {
639 			_alpm_log(handle, ALPM_LOG_DEBUG,
640 					"%s is in skip_remove, skipping removal\n", file->name);
641 			continue;
642 		}
643 
644 		if(unlink_file(handle, oldpkg, newpkg, file, nosave) < 0) {
645 			err++;
646 		}
647 
648 		if(!newpkg) {
649 			/* update progress bar after each file */
650 			int percent = ((filelist->count - i) * 100) / filelist->count;
651 			PROGRESS(handle, ALPM_PROGRESS_REMOVE_START, oldpkg->name,
652 					percent, pkg_count, targ_count);
653 		}
654 	}
655 
656 	if(!newpkg) {
657 		/* set progress to 100% after we finish unlinking files */
658 		PROGRESS(handle, ALPM_PROGRESS_REMOVE_START, oldpkg->name, 100,
659 				pkg_count, targ_count);
660 	}
661 
662 	return err;
663 }
664 
665 /**
666  * @brief Remove a package from the filesystem.
667  *
668  * @param handle the context handle
669  * @param oldpkg package to remove
670  * @param newpkg package to replace \a oldpkg (optional)
671  * @param targ_count current index within the transaction (1-based)
672  * @param pkg_count the number of packages affected by the transaction
673  *
674  * @return 0
675  */
_alpm_remove_single_package(alpm_handle_t * handle,alpm_pkg_t * oldpkg,alpm_pkg_t * newpkg,size_t targ_count,size_t pkg_count)676 int _alpm_remove_single_package(alpm_handle_t *handle,
677 		alpm_pkg_t *oldpkg, alpm_pkg_t *newpkg,
678 		size_t targ_count, size_t pkg_count)
679 {
680 	const char *pkgname = oldpkg->name;
681 	const char *pkgver = oldpkg->version;
682 	alpm_event_package_operation_t event = {
683 		.type = ALPM_EVENT_PACKAGE_OPERATION_START,
684 		.operation = ALPM_PACKAGE_REMOVE,
685 		.oldpkg = oldpkg,
686 		.newpkg = NULL
687 	};
688 
689 	if(newpkg) {
690 		_alpm_log(handle, ALPM_LOG_DEBUG, "removing old package first (%s-%s)\n",
691 				pkgname, pkgver);
692 	} else {
693 		EVENT(handle, &event);
694 		_alpm_log(handle, ALPM_LOG_DEBUG, "removing package %s-%s\n",
695 				pkgname, pkgver);
696 
697 		/* run the pre-remove scriptlet if it exists */
698 		if(alpm_pkg_has_scriptlet(oldpkg) &&
699 				!(handle->trans->flags & ALPM_TRANS_FLAG_NOSCRIPTLET)) {
700 			char *scriptlet = _alpm_local_db_pkgpath(handle->db_local,
701 					oldpkg, "install");
702 			_alpm_runscriptlet(handle, scriptlet, "pre_remove", pkgver, NULL, 0);
703 			free(scriptlet);
704 		}
705 	}
706 
707 	if(!(handle->trans->flags & ALPM_TRANS_FLAG_DBONLY)) {
708 		/* TODO check returned errors if any */
709 		remove_package_files(handle, oldpkg, newpkg, targ_count, pkg_count);
710 	}
711 
712 	if(!newpkg) {
713 		alpm_logaction(handle, ALPM_CALLER_PREFIX, "removed %s (%s)\n",
714 					oldpkg->name, oldpkg->version);
715 	}
716 
717 	/* run the post-remove script if it exists */
718 	if(!newpkg && alpm_pkg_has_scriptlet(oldpkg) &&
719 			!(handle->trans->flags & ALPM_TRANS_FLAG_NOSCRIPTLET)) {
720 		char *scriptlet = _alpm_local_db_pkgpath(handle->db_local,
721 				oldpkg, "install");
722 		_alpm_runscriptlet(handle, scriptlet, "post_remove", pkgver, NULL, 0);
723 		free(scriptlet);
724 	}
725 
726 	if(!newpkg) {
727 		event.type = ALPM_EVENT_PACKAGE_OPERATION_DONE;
728 		EVENT(handle, &event);
729 	}
730 
731 	/* remove the package from the database */
732 	_alpm_log(handle, ALPM_LOG_DEBUG, "removing database entry '%s'\n", pkgname);
733 	if(_alpm_local_db_remove(handle->db_local, oldpkg) == -1) {
734 		_alpm_log(handle, ALPM_LOG_ERROR, _("could not remove database entry %s-%s\n"),
735 				pkgname, pkgver);
736 	}
737 	/* remove the package from the cache */
738 	if(_alpm_db_remove_pkgfromcache(handle->db_local, oldpkg) == -1) {
739 		_alpm_log(handle, ALPM_LOG_ERROR, _("could not remove entry '%s' from cache\n"),
740 				pkgname);
741 	}
742 
743 	/* TODO: useful return values */
744 	return 0;
745 }
746 
747 /**
748  * @brief Remove packages in the current transaction.
749  *
750  * @param handle the context handle
751  * @param run_ldconfig whether to run ld_config after removing the packages
752  *
753  * @return 0 on success, -1 if errors occurred while removing files
754  */
_alpm_remove_packages(alpm_handle_t * handle,int run_ldconfig)755 int _alpm_remove_packages(alpm_handle_t *handle, int run_ldconfig)
756 {
757 	alpm_list_t *targ;
758 	size_t pkg_count, targ_count;
759 	alpm_trans_t *trans = handle->trans;
760 	int ret = 0;
761 
762 	pkg_count = alpm_list_count(trans->remove);
763 	targ_count = 1;
764 
765 	for(targ = trans->remove; targ; targ = targ->next) {
766 		alpm_pkg_t *pkg = targ->data;
767 
768 		if(trans->state == STATE_INTERRUPTED) {
769 			return ret;
770 		}
771 
772 		if(_alpm_remove_single_package(handle, pkg, NULL,
773 					targ_count, pkg_count) == -1) {
774 			handle->pm_errno = ALPM_ERR_TRANS_ABORT;
775 			/* running ldconfig at this point could possibly screw system */
776 			run_ldconfig = 0;
777 			ret = -1;
778 		}
779 
780 		targ_count++;
781 	}
782 
783 	if(run_ldconfig) {
784 		/* run ldconfig if it exists */
785 		_alpm_ldconfig(handle);
786 	}
787 
788 	return ret;
789 }
790