XBPS Library API 20260501
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))
123
124 euid = geteuid();
125
126 if ((pkgd = xbps_pkgdb_get_pkg(xhp, pkgname)) == NULL) {
127 rv = errno;
128 xbps_dbg_printf("[remove] cannot find %s in pkgdb: %s\n",
129 pkgver, strerror(rv));
130 goto out;
131 }
132 if ((rv = xbps_pkg_state_dictionary(pkgd, &state)) != 0) {
133 xbps_dbg_printf("[remove] cannot find %s in pkgdb: %s\n",
134 pkgver, strerror(rv));
135 goto out;
136 }
137 xbps_dbg_printf("attempting to remove %s state %d\n", pkgver, state);
138
139 if (!update)
140 xbps_set_cb_state(xhp, XBPS_STATE_REMOVE, 0, pkgver, NULL);
141
142 if (chdir(xhp->rootdir) == -1) {
143 rv = errno;
144 xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_FAIL,
145 rv, pkgver,
146 "%s: [remove] failed to chdir to rootdir `%s': %s",
147 pkgver, xhp->rootdir, strerror(rv));
148 goto out;
149 }
150
151 /* If package was "half-removed", remove it fully. */
152 if (state == XBPS_PKG_STATE_HALF_REMOVED)
153 goto purge;
154
155 /* unregister alternatives */
156 if (update)
157 xbps_dictionary_set_bool(pkgd, "alternatives-update", true);
158
159 if ((rv = xbps_alternatives_unregister(xhp, pkgd)) != 0)
160 goto out;
161
162 /*
163 * If updating a package, we just need to execute the current
164 * pre-remove action target and we are done. Its files will be
165 * overwritten later in unpack phase.
166 */
167 if (update) {
168 return 0;
169 }
170
171 if (xbps_dictionary_get_dict(xhp->transd, "obsolete_files", &obsd))
172 obsoletes = xbps_dictionary_get(obsd, pkgname);
173
174 if (xbps_array_count(obsoletes) > 0) {
175 /*
176 * Do the removal in 2 phases:
177 * 1- check if user has enough perms to remove all entries
178 * 2- perform removal
179 */
180 if (check_remove_pkg_files(xhp, obsoletes, pkgver, euid)) {
181 rv = EPERM;
182 goto out;
183 }
184 /* Remove links */
185 if ((rv = remove_pkg_files(xhp, obsoletes, pkgver)) != 0)
186 goto out;
187 }
188
189 /*
190 * Set package state to "half-removed".
191 */
193 XBPS_PKG_STATE_HALF_REMOVED);
194 if (rv != 0) {
195 xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_FAIL,
196 rv, pkgver,
197 "%s: [remove] failed to set state to half-removed: %s",
198 pkgver, strerror(rv));
199 goto out;
200 }
201 /* XXX: setting the state and then removing the package seems useless. */
202
203purge:
204 /*
205 * Remove package metadata plist.
206 */
207 snprintf(metafile, sizeof(metafile), "%s/.%s-files.plist", xhp->metadir, pkgname);
208 if (remove(metafile) == -1) {
209 if (errno != ENOENT) {
210 xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_FAIL,
211 rv, pkgver,
212 "%s: failed to remove metadata file: %s",
213 pkgver, strerror(errno));
214 }
215 }
216 /*
217 * Unregister package from pkgdb.
218 */
219 xbps_dbg_printf("[remove] unregister %s returned %d\n", pkgver, rv);
220 xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_DONE, 0, pkgver, NULL);
221 xbps_dictionary_remove(xhp->pkgdb, pkgname);
222out:
223 if (rv != 0) {
224 xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_FAIL, rv, pkgver,
225 "%s: failed to remove package: %s", pkgver, strerror(rv));
226 }
227
228 return rv;
229}
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
#define xbps_unreachable()
Log and abort for code that should be unreachable.
Definition xbps.h:783
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