Skip to content

Commit ee770cb

Browse files
committed
bin/xbps-remove: allow removing uninstalled packages from the cache
Change "obsolete packages" to "outdated packages" when describing the old behaviour.
1 parent 6d940e6 commit ee770cb

File tree

5 files changed

+77
-23
lines changed

5 files changed

+77
-23
lines changed

bin/xbps-remove/clean-cache.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,23 @@ binpkg_parse(char *buf, size_t bufsz, const char *path, const char **pkgver, con
6464
return 0;
6565
}
6666

67+
struct cleaner_data {
68+
bool dry;
69+
bool uninstalled;
70+
};
71+
6772
static int
6873
cleaner_cb(struct xbps_handle *xhp, xbps_object_t obj,
6974
const char *key UNUSED, void *arg,
7075
bool *done UNUSED)
7176
{
7277
char buf[PATH_MAX];
73-
xbps_dictionary_t repo_pkgd;
78+
xbps_dictionary_t pkgd;
7479
const char *binpkg, *rsha256;
7580
const char *binpkgver, *binpkgarch;
76-
bool drun = false;
81+
struct cleaner_data *data = arg;
7782
int r;
7883

79-
/* Extract drun (dry-run) flag from arg*/
80-
if (arg != NULL)
81-
drun = *(bool*)arg;
82-
8384
binpkg = xbps_string_cstring_nocopy(obj);
8485
r = binpkg_parse(buf, sizeof(buf), binpkg, &binpkgver, &binpkgarch);
8586
if (r < 0) {
@@ -96,9 +97,13 @@ cleaner_cb(struct xbps_handle *xhp, xbps_object_t obj,
9697
* Remove binary pkg if it's not registered in any repository
9798
* or if hash doesn't match.
9899
*/
99-
repo_pkgd = xbps_rpool_get_pkg(xhp, binpkgver);
100-
if (repo_pkgd) {
101-
xbps_dictionary_get_cstring_nocopy(repo_pkgd,
100+
if (data->uninstalled) {
101+
pkgd = xbps_pkgdb_get_pkg(xhp, binpkgver);
102+
} else {
103+
pkgd = xbps_rpool_get_pkg(xhp, binpkgver);
104+
}
105+
if (pkgd) {
106+
xbps_dictionary_get_cstring_nocopy(pkgd,
102107
"filename-sha256", &rsha256);
103108
r = xbps_file_sha256_check(binpkg, rsha256);
104109
if (r == 0) {
@@ -111,13 +116,13 @@ cleaner_cb(struct xbps_handle *xhp, xbps_object_t obj,
111116
}
112117
}
113118
snprintf(buf, sizeof(buf), "%s.sig", binpkg);
114-
if (!drun && unlink(binpkg) == -1) {
119+
if (!data->dry && unlink(binpkg) == -1) {
115120
xbps_error_printf("Failed to remove `%s': %s\n",
116121
binpkg, strerror(errno));
117122
} else {
118123
printf("Removed %s from cachedir (obsolete)\n", binpkg);
119124
}
120-
if (!drun && unlink(buf) == -1 && errno != ENOENT) {
125+
if (!data->dry && unlink(buf) == -1 && errno != ENOENT) {
121126
xbps_error_printf("Failed to remove `%s': %s\n",
122127
buf, strerror(errno));
123128
}
@@ -126,7 +131,7 @@ cleaner_cb(struct xbps_handle *xhp, xbps_object_t obj,
126131
}
127132

128133
int
129-
clean_cachedir(struct xbps_handle *xhp, bool drun)
134+
clean_cachedir(struct xbps_handle *xhp, bool uninstalled, bool drun)
130135
{
131136
xbps_array_t array = NULL;
132137
DIR *dirp;
@@ -158,7 +163,11 @@ clean_cachedir(struct xbps_handle *xhp, bool drun)
158163
(void)closedir(dirp);
159164

160165
if (xbps_array_count(array)) {
161-
rv = xbps_array_foreach_cb_multi(xhp, array, NULL, cleaner_cb, (void*)&drun);
166+
struct cleaner_data data = {
167+
.dry = drun,
168+
.uninstalled = uninstalled,
169+
};
170+
rv = xbps_array_foreach_cb_multi(xhp, array, NULL, cleaner_cb, (void*)&data);
162171
xbps_object_release(array);
163172
}
164173
return rv;

bin/xbps-remove/defs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
#define _XBPS_REMOVE_DEFS_H_
2828

2929
/* From clean-cache.c */
30-
int clean_cachedir(struct xbps_handle *, bool drun);
30+
int clean_cachedir(struct xbps_handle *, bool uninstalled, bool drun);
3131

3232
#endif /* !_XBPS_REMOVE_DEFS_H_ */

bin/xbps-remove/main.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ usage(bool fail)
5353
" -f, --force Force package files removal\n"
5454
" -h, --help Show usage\n"
5555
" -n, --dry-run Dry-run mode\n"
56-
" -O, --clean-cache Remove obsolete packages in cachedir\n"
56+
" -O, --clean-cache Remove outdated packages from the cache\n"
57+
" If specified twice, also remove uninstalled packages\n"
5758
" -o, --remove-orphans Remove package orphans\n"
5859
" -R, --recursive Recursively remove dependencies\n"
5960
" -r, --rootdir <dir> Full path to rootdir\n"
@@ -179,12 +180,13 @@ main(int argc, char **argv)
179180
struct xbps_handle xh;
180181
const char *rootdir, *cachedir, *confdir;
181182
int c, flags, rv;
182-
bool yes, drun, recursive, clean_cache, orphans;
183+
bool yes, drun, recursive, orphans;
183184
int maxcols, missing;
185+
int clean_cache = 0;
184186

185187
rootdir = cachedir = confdir = NULL;
186188
flags = rv = 0;
187-
drun = recursive = clean_cache = yes = orphans = false;
189+
drun = recursive = yes = orphans = false;
188190

189191
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
190192
switch (c) {
@@ -210,7 +212,7 @@ main(int argc, char **argv)
210212
drun = true;
211213
break;
212214
case 'O':
213-
clean_cache = true;
215+
clean_cache++;
214216
break;
215217
case 'o':
216218
orphans = true;
@@ -236,7 +238,7 @@ main(int argc, char **argv)
236238
/* NOTREACHED */
237239
}
238240
}
239-
if (!clean_cache && !orphans && (argc == optind)) {
241+
if (clean_cache == 0 && !orphans && (argc == optind)) {
240242
usage(true);
241243
/* NOTREACHED */
242244
}
@@ -263,8 +265,8 @@ main(int argc, char **argv)
263265

264266
maxcols = get_maxcols();
265267

266-
if (clean_cache) {
267-
rv = clean_cachedir(&xh, drun);
268+
if (clean_cache > 0) {
269+
rv = clean_cachedir(&xh, clean_cache > 1, drun);
268270
if (!orphans || rv)
269271
exit(rv);;
270272
}

bin/xbps-remove/xbps-remove.1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ Show the help message.
7979
Dry-run mode. Show what actions would be done but don't do anything. The current output
8080
prints 6 arguments: "<pkgver> <action> <arch> <repository> <installedsize> <downloadsize>".
8181
.It Fl O, Fl -clean-cache
82-
Cleans cache directory removing obsolete binary packages.
82+
Cleans cache directory removing outdated binary packages.
83+
If specified twice,
84+
also remove packages that are not installed from the cache.
8385
.It Fl o, Fl -remove-orphans
8486
Removes installed package orphans that were installed automatically
8587
(as dependencies) and are not currently dependencies of any installed package.

tests/xbps/xbps-remove/basic_test.sh

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,15 @@ clean_cache_head() {
5353
}
5454

5555
clean_cache_body() {
56-
mkdir -p repo pkg_A/B/C
56+
mkdir -p repo pkg_A/B/C pkg_B
5757
touch pkg_A/
5858
cd repo
5959
xbps-create -A noarch -n A-1.0_1 -s "A pkg" ../pkg_A
6060
atf_check_equal $? 0
6161
xbps-create -A noarch -n A-1.0_2 -s "A pkg" ../pkg_A
6262
atf_check_equal $? 0
63+
xbps-create -A noarch -n B-1.0_1 -s "B pkg" ../pkg_B
64+
atf_check_equal $? 0
6365
xbps-rindex -d -a $PWD/*.xbps
6466
atf_check_equal $? 0
6567
cd ..
@@ -69,12 +71,15 @@ clean_cache_body() {
6971
cp repo/*.xbps root/var/cache/xbps
7072
atf_check_equal $? 0
7173
echo "repository=https://localhost/" >root/etc/xbps.d/localrepo.conf
74+
xbps-install -r root -C etc/xbps.d -R repo -dvy B
7275
xbps-remove -r root -C etc/xbps.d -dvO
7376
atf_check_equal $? 0
7477
test -f root/var/cache/xbps/A-1.0_2.noarch.xbps
7578
atf_check_equal $? 0
7679
test -f root/var/cache/xbps/A-1.0_1.noarch.xbps
7780
atf_check_equal $? 1
81+
test -f root/var/cache/xbps/B-1.0_1.noarch.xbps
82+
atf_check_equal $? 0
7883
}
7984

8085
atf_test_case clean_cache_dry_run
@@ -136,10 +141,46 @@ clean_cache_dry_run_perm_body() {
136141
atf_check_equal "$out" "Removed A-1.0_1.noarch.xbps from cachedir (obsolete)"
137142
}
138143

144+
clean_cache_uninstalled_head() {
145+
atf_set "descr" "xbps-remove(1): clean uninstalled package from cache"
146+
}
147+
148+
clean_cache_uninstalled_body() {
149+
mkdir -p repo pkg_A/B/C pkg_B
150+
touch pkg_A/
151+
cd repo
152+
xbps-create -A noarch -n A-1.0_1 -s "A pkg" ../pkg_A
153+
atf_check_equal $? 0
154+
xbps-create -A noarch -n A-1.0_2 -s "A pkg" ../pkg_A
155+
atf_check_equal $? 0
156+
xbps-create -A noarch -n B-1.0_1 -s "B pkg" ../pkg_B
157+
atf_check_equal $? 0
158+
xbps-rindex -d -a $PWD/*.xbps
159+
atf_check_equal $? 0
160+
cd ..
161+
mkdir -p root/etc/xbps.d root/var/db/xbps/https___localhost_ root/var/cache/xbps
162+
cp repo/*-repodata root/var/db/xbps/https___localhost_
163+
atf_check_equal $? 0
164+
cp repo/*.xbps root/var/cache/xbps
165+
atf_check_equal $? 0
166+
echo "repository=https://localhost/" >root/etc/xbps.d/localrepo.conf
167+
xbps-install -r root -C etc/xbps.d -R repo -dvy B
168+
atf_check_equal $? 0
169+
xbps-remove -r root -C etc/xbps.d -dvOO
170+
atf_check_equal $? 0
171+
test -f root/var/cache/xbps/A-1.0_2.noarch.xbps
172+
atf_check_equal $? 1
173+
test -f root/var/cache/xbps/A-1.0_1.noarch.xbps
174+
atf_check_equal $? 1
175+
test -f root/var/cache/xbps/B-1.0_1.noarch.xbps
176+
atf_check_equal $? 0
177+
}
178+
139179
atf_init_test_cases() {
140180
atf_add_test_case remove_directory
141181
atf_add_test_case remove_orphans
142182
atf_add_test_case clean_cache
143183
atf_add_test_case clean_cache_dry_run
144184
atf_add_test_case clean_cache_dry_run_perm
185+
atf_add_test_case clean_cache_uninstalled
145186
}

0 commit comments

Comments
 (0)