Repository navigation
Expand file tree
/
Copy pathautoremove.c
More file actions
225 lines (194 loc) · 6.03 KB
/
Copy pathautoremove.c
File metadata and controls
225 lines (194 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* Copyright (c) 2009-2015 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Emile "iMil" Heitor <imil@NetBSD.org> .
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/**
* \file autoremove.c
*
* Cleanup orphan dependencies, keep and unkeep packages
*/
#include <sqlite3.h>
#include "pkgain.h"
void
pkgain_autoremove(void)
{
Plistarray *depshead;
Plistnumbered *nokeephead, *keephead;
Plisthead *removehead, *orderedhead;
Pkglist *pkglist, *premove, *p;
size_t slot;
char *toremove = NULL, preserve[BUFSIZ];
int removenb = 0;
/*
* Record all keep and no-keep packages. If either are empty then
* we're done.
*/
if ((keephead = rec_pkglist(KEEP_LOCAL_PKGS)) == NULL)
errx(EXIT_FAILURE, "no packages have been marked as keepable");
if ((nokeephead = rec_pkglist(NOKEEP_LOCAL_PKGS)) == NULL) {
free_pkglist(&keephead->P_Plisthead);
free(keephead);
printf(MSG_ALL_KEEP_PKGS);
return;
}
/*
* Record all recursive dependencies for each keep package. This then
* contains a list of all packages that are required.
*/
depshead = init_array(DEPS_HASH_SIZE);
SLIST_FOREACH(p, keephead->P_Plisthead, next) {
get_depends_recursive(p->full, depshead, DEPENDS_LOCAL);
}
removehead = init_head();
/*
* For each non-keep package, check whether it is a recursive
* dependency of a keep package, in which case it is still required.
*
* Check the hash slot matching the package name, then slot 0 if not
* already checked, as that's where any dependencies recorded from a
* complicated pattern with no pkgbase are stored.
*/
SLIST_FOREACH(pkglist, nokeephead->P_Plisthead, next) {
slot = pkg_hash_entry(pkglist->name, depshead->size);
if (pkgname_in_local_pkglist(pkglist->full,
&depshead->head[slot], 1))
continue;
if (slot != 0 && pkgname_in_local_pkglist(pkglist->full,
&depshead->head[0], 1))
continue;
/*
* Also keep the package if it is a "preserve" package (one
* that is specifically built to not be uninstalled, for
* example important bootstrap packages.
*/
snprintf(preserve, BUFSIZ, "%s/%s/%s", pkgdb_get_dir(),
pkglist->full, PRESERVE_FNAME);
if (access(preserve, F_OK) != -1)
continue;
/*
* Package can be auto removed, find its lpkg entry and add to
* the list.
*/
premove = malloc_pkglist();
premove->action = ACTION_REMOVE;
premove->lpkg = find_local_pkg(pkglist->full, pkglist->name);
SLIST_INSERT_HEAD(removehead, premove, next);
removenb++;
}
free_pkglist(&keephead->P_Plisthead);
free(keephead);
free_pkglist(&nokeephead->P_Plisthead);
free(nokeephead);
free_array(depshead);
if (!removenb) {
printf(MSG_NO_ORPHAN_DEPS);
exit(EXIT_SUCCESS);
}
orderedhead = order_remove(removehead);
free_pkglist(&removehead);
if (SLIST_EMPTY(orderedhead)) {
free_pkglist(&orderedhead);
return;
}
toremove = action_list_sorted(orderedhead, ACTION_NONE);
printf(MSG_AUTOREMOVE_PKGS, removenb, toremove);
if (!noflag)
printf("\n");
if (check_yesno(DEFAULT_YES)) {
do_pkg_remove(orderedhead);
(void) update_db(LOCAL_SUMMARY, 1);
}
XFREE(toremove);
free_pkglist(&orderedhead);
}
static void
show_keep_list(const char *query, const char *emptymsg)
{
Plistnumbered *plisthead;
Pkglist *pkglist;
if ((plisthead = rec_pkglist(query)) == NULL) {
printf("%s\n", emptymsg);
return;
}
SLIST_FOREACH(pkglist, plisthead->P_Plisthead, next)
printf("%-20s %s\n", pkglist->full, pkglist->comment);
free_pkglist(&plisthead->P_Plisthead);
free(plisthead);
}
void
show_pkg_keep(void)
{
show_keep_list(KEEP_LOCAL_PKGS, MSG_EMPTY_KEEP_LIST);
}
void
show_pkg_nokeep(void)
{
show_keep_list(NOKEEP_LOCAL_PKGS, MSG_EMPTY_NOKEEP_LIST);
}
/*
* Mark packages as keep (non-autoremovable) or nokeep (autoremovable).
*/
int
pkg_keep(int type, char *pattern)
{
Pkglist *lpkg;
char query[BUFSIZ];
if (!have_privs(PRIVS_PKGDB|PRIVS_PKGAINDB))
errx(EXIT_FAILURE, MSG_DONT_HAVE_RIGHTS);
if (is_empty_local_pkglist())
return 1;
if ((lpkg = find_local_pkg(pattern, NULL)) == NULL) {
printf(MSG_PKG_NOT_INSTALLED, pattern);
return 1;
}
/*
* Only print a message if the state is being changed, and update the
* pkgdb as the source of truth.
*/
switch (type) {
case KEEP:
if (is_automatic_installed(lpkg->full)) {
printf(MSG_MARKING_PKG_KEEP, lpkg->full);
if (mark_as_automatic_installed(lpkg->full, 0) < 0)
exit(EXIT_FAILURE);
}
lpkg->keep = 1;
sqlite3_snprintf(BUFSIZ, query, KEEP_PKG, lpkg->name);
break;
case UNKEEP:
if (!is_automatic_installed(lpkg->full)) {
printf(MSG_UNMARKING_PKG_KEEP, lpkg->full);
if (mark_as_automatic_installed(lpkg->full, 1) < 0)
exit(EXIT_FAILURE);
}
lpkg->keep = 0;
sqlite3_snprintf(BUFSIZ, query, UNKEEP_PKG, lpkg->name);
break;
}
pkgaindb_doquery(query, NULL, NULL);
return 0;
}