Skip to content

Commit 8cb883d

Browse files
committed
bin/xbps-remove: improve error handling and error messages
1 parent e88ad64 commit 8cb883d

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

bin/xbps-remove/clean-cache.c

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,20 +143,34 @@ clean_cachedir(struct xbps_handle *xhp, bool uninstalled, bool drun)
143143
DIR *dirp;
144144
struct dirent *dp;
145145
char *ext;
146-
int rv = 0;
146+
int r;
147147

148148
// XXX: there is no public api to load the pkgdb so force it before
149149
// its done potentially concurrently by threads through the
150150
// xbps_array_foreach_cb_multi call later.
151151
(void)xbps_pkgdb_get_pkg(xhp, "foo");
152152

153-
if (chdir(xhp->cachedir) == -1)
154-
return -1;
153+
if (chdir(xhp->cachedir) == -1) {
154+
if (errno == ENOENT)
155+
return 0;
156+
r = -errno;
157+
xbps_error_printf("failed to change to cache directory: %s: %s\n",
158+
xhp->cachedir, strerror(-r));
159+
return r;
160+
}
155161

156-
if ((dirp = opendir(xhp->cachedir)) == NULL)
157-
return 0;
162+
dirp = opendir(".");
163+
if (!dirp) {
164+
r = -errno;
165+
xbps_error_printf("failed to open cache directory: %s: %s\n",
166+
xhp->cachedir, strerror(-r));
167+
return r;
168+
}
158169

159170
array = xbps_array_create();
171+
if (!array)
172+
return xbps_error_oom();
173+
160174
while ((dp = readdir(dirp)) != NULL) {
161175
if ((strcmp(dp->d_name, ".") == 0) ||
162176
(strcmp(dp->d_name, "..") == 0))
@@ -169,7 +183,10 @@ clean_cachedir(struct xbps_handle *xhp, bool uninstalled, bool drun)
169183
xbps_dbg_printf("ignoring unknown file: %s\n", dp->d_name);
170184
continue;
171185
}
172-
xbps_array_add_cstring(array, dp->d_name);
186+
if (!xbps_array_add_cstring(array, dp->d_name)) {
187+
xbps_object_release(array);
188+
return xbps_error_oom();
189+
}
173190
}
174191
(void)closedir(dirp);
175192

@@ -178,9 +195,11 @@ clean_cachedir(struct xbps_handle *xhp, bool uninstalled, bool drun)
178195
.dry = drun,
179196
.uninstalled = uninstalled,
180197
};
181-
rv = xbps_array_foreach_cb_multi(xhp, array, NULL, cleaner_cb, (void*)&data);
198+
r = xbps_array_foreach_cb_multi(xhp, array, NULL, cleaner_cb, (void*)&data);
199+
} else {
200+
r = 0;
182201
}
183202

184203
xbps_object_release(array);
185-
return rv;
204+
return r;
186205
}

0 commit comments

Comments
 (0)