XBPS Library API 20260225
The X Binary Package System
package_remove.c
1/*-
2 * Copyright (c) 2009-2015 Juan Romero Pardines.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include <sys/stat.h>
27
28#include <dirent.h>
29#include <errno.h>
30#include <fcntl.h>
31#include <libgen.h>
32#include <limits.h>
33#include <stdbool.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#include <unistd.h>
38
39#include "xbps_api_impl.h"
40
41static bool
42check_remove_pkg_files(struct xbps_handle *xhp,
43 xbps_array_t obsoletes, const char *pkgver, uid_t euid)
44{
45 struct stat st;
46 bool fail = false;
47
48 if (euid == 0)
49 return false;
50
51 for (unsigned int i = 0; i < xbps_array_count(obsoletes); i++) {
52 const char *file = NULL;
53 xbps_array_get_cstring_nocopy(obsoletes, i, &file);
54 /*
55 * Check if effective user ID owns the file; this is
56 * enough to ensure the user has write permissions
57 * on the directory.
58 * XXX: this isn't really correct, would need to check parent
59 * directory permissions...
60 */
61 if (lstat(file, &st) == 0) {
62 if (euid == st.st_uid)
63 continue;
64 errno = EPERM;
65 }
66 if (errno != ENOENT) {
67 /*
68 * only bail out if something else than ENOENT
69 * is returned.
70 */
71 fail = true;
72 xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_FILE_FAIL,
73 errno, pkgver, "%s: cannot remove `%s': %s", pkgver,
74 file, strerror(errno));
75 }
76 }
77 return fail;
78}
79
80static int
81remove_pkg_files(struct xbps_handle *xhp,
82 xbps_array_t obsoletes,
83 const char *pkgver)
84{
85 int rv = 0;
86
87 for (unsigned int i = 0; i < xbps_array_count(obsoletes); i++) {
88 const char *file = NULL;
89 xbps_array_get_cstring_nocopy(obsoletes, i, &file);
90 /*
91 * Remove the object if possible.
92 */
93 if (remove(file) == -1) {
94 xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_FILE_FAIL,
95 errno, pkgver,
96 "%s: failed to remove `%s': %s", pkgver,
97 file, strerror(errno));
98 } else {
99 /* success */
100 xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_FILE,
101 0, pkgver, "Removed `%s'", file);
102 }
103 }
104
105 return rv;
106}
107
108int HIDDEN
109xbps_remove_pkg(struct xbps_handle *xhp, const char *pkgver, bool update)
110{
111 xbps_dictionary_t pkgd = NULL, obsd = NULL;
112 xbps_array_t obsoletes = NULL;
113 char pkgname[XBPS_NAME_SIZE], metafile[PATH_MAX];
114 int rv = 0;
115 pkg_state_t state = 0;
116 uid_t euid;
117
118 assert(xhp);
119 assert(pkgver);
120
121 if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver)) {
122 abort();
123 }
124
125 euid = geteuid();
126
127 if ((pkgd = xbps_pkgdb_get_pkg(xhp, pkgname)) == NULL) {
128 rv = errno;
129 xbps_dbg_printf("[remove] cannot find %s in pkgdb: %s\n",
130 pkgver, strerror(rv));
131 goto out;
132 }
133 if ((rv = xbps_pkg_state_dictionary(pkgd, &state)) != 0) {
134 xbps_dbg_printf("[remove] cannot find %s in pkgdb: %s\n",
135 pkgver, strerror(rv));
136 goto out;
137 }
138 xbps_dbg_printf("attempting to remove %s state %d\n", pkgver, state);
139
140 if (!update)
141 xbps_set_cb_state(xhp, XBPS_STATE_REMOVE, 0, pkgver, NULL);
142
143 if (chdir(xhp->rootdir) == -1) {
144 rv = errno;
145 xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_FAIL,
146 rv, pkgver,
147 "%s: [remove] failed to chdir to rootdir `%s': %s",
148 pkgver, xhp->rootdir, strerror(rv));
149 goto out;
150 }
151
152 /* If package was "half-removed", remove it fully. */
153 if (state == XBPS_PKG_STATE_HALF_REMOVED)
154 goto purge;
155
156 /* unregister alternatives */
157 if (update)
158 xbps_dictionary_set_bool(pkgd, "alternatives-update", true);
159
160 if ((rv = xbps_alternatives_unregister(xhp, pkgd)) != 0)
161 goto out;
162
163 /*
164 * If updating a package, we just need to execute the current
165 * pre-remove action target and we are done. Its files will be
166 * overwritten later in unpack phase.
167 */
168 if (update) {
169 return 0;
170 }
171
172 if (xbps_dictionary_get_dict(xhp->transd, "obsolete_files", &obsd))
173 obsoletes = xbps_dictionary_get(obsd, pkgname);
174
175 if (xbps_array_count(obsoletes) > 0) {
176 /*
177 * Do the removal in 2 phases:
178 * 1- check if user has enough perms to remove all entries
179 * 2- perform removal
180 */
181 if (check_remove_pkg_files(xhp, obsoletes, pkgver, euid)) {
182 rv = EPERM;
183 goto out;
184 }
185 /* Remove links */
186 if ((rv = remove_pkg_files(xhp, obsoletes, pkgver)) != 0)
187 goto out;
188 }
189
190 /*
191 * Set package state to "half-removed".
192 */
194 XBPS_PKG_STATE_HALF_REMOVED);
195 if (rv != 0) {
196 xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_FAIL,
197 rv, pkgver,
198 "%s: [remove] failed to set state to half-removed: %s",
199 pkgver, strerror(rv));
200 goto out;
201 }
202 /* XXX: setting the state and then removing the package seems useless. */
203
204purge:
205 /*
206 * Remove package metadata plist.
207 */
208 snprintf(metafile, sizeof(metafile), "%s/.%s-files.plist", xhp->metadir, pkgname);
209 if (remove(metafile) == -1) {
210 if (errno != ENOENT) {
211 xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_FAIL,
212 rv, pkgver,
213 "%s: failed to remove metadata file: %s",
214 pkgver, strerror(errno));
215 }
216 }
217 /*
218 * Unregister package from pkgdb.
219 */
220 xbps_dbg_printf("[remove] unregister %s returned %d\n", pkgver, rv);
221 xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_DONE, 0, pkgver, NULL);
222 xbps_dictionary_remove(xhp->pkgdb, pkgname);
223out:
224 if (rv != 0) {
225 xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_FAIL, rv, pkgver,
226 "%s: failed to remove package: %s", pkgver, strerror(rv));
227 }
228
229 return rv;
230}
int xbps_alternatives_unregister(struct xbps_handle *xhp, xbps_dictionary_t pkgd)
xbps_dictionary_t pkgdb
Definition xbps.h:590
char rootdir[XBPS_MAXPATH]
Definition xbps.h:664
xbps_dictionary_t transd
Definition xbps.h:597
char metadir[XBPS_MAXPATH]
Definition xbps.h:678
Generic XBPS structure handler for initialization.
Definition xbps.h:560
void xbps_dbg_printf(const char *fmt,...)
Prints debug messages to stderr.
Definition log.c:67
xbps_dictionary_t xbps_pkgdb_get_pkg(struct xbps_handle *xhp, const char *pkg)
Definition pkgdb.c:389
int xbps_set_pkg_state_dictionary(xbps_dictionary_t dict, pkg_state_t state)
pkg_state_t
Definition xbps.h:1831
int xbps_pkg_state_dictionary(xbps_dictionary_t dict, pkg_state_t *state)
bool xbps_pkg_name(char *dst, size_t len, const char *pkg)
Definition util.c:249