Skip to content

Commit e2a9b5b

Browse files
committed
bin/xbps-remove: improve error handling and error messages
1 parent 2ca4260 commit e2a9b5b

1 file changed

Lines changed: 27 additions & 8 deletions

File tree

bin/xbps-remove/clean-cache.c

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

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

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

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

161172
array = xbps_array_create();
173+
if (!array)
174+
return xbps_error_oom();
175+
162176
while ((dp = readdir(dirp)) != NULL) {
163177
if ((strcmp(dp->d_name, ".") == 0) ||
164178
(strcmp(dp->d_name, "..") == 0))
@@ -171,7 +185,10 @@ clean_cachedir(struct xbps_handle *xhp, bool uninstalled, bool drun)
171185
xbps_dbg_printf("ignoring unknown file: %s\n", dp->d_name);
172186
continue;
173187
}
174-
xbps_array_add_cstring(array, dp->d_name);
188+
if (!xbps_array_add_cstring(array, dp->d_name)) {
189+
xbps_object_release(array);
190+
return xbps_error_oom();
191+
}
175192
}
176193
(void)closedir(dirp);
177194

@@ -180,9 +197,11 @@ clean_cachedir(struct xbps_handle *xhp, bool uninstalled, bool drun)
180197
.dry = drun,
181198
.uninstalled = uninstalled,
182199
};
183-
rv = xbps_array_foreach_cb_multi(xhp, array, NULL, cleaner_cb, (void*)&data);
200+
r = xbps_array_foreach_cb_multi(xhp, array, NULL, cleaner_cb, (void*)&data);
201+
} else {
202+
r = 0;
184203
}
185204

186205
xbps_object_release(array);
187-
return rv;
206+
return r;
188207
}

0 commit comments

Comments
 (0)