-
Notifications
You must be signed in to change notification settings - Fork 42
/
pkg_check.c
319 lines (271 loc) · 8.22 KB
/
pkg_check.c
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* 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.
*/
#include <sqlite3.h>
#include "pkgin.h"
/*
* SQLite callback for LOCAL_CONFLICTS etc, record a pattern and optional
* PKGBASE to a Plistarray.
*
* argv0: pattern
* argv1: pkgbase, may be NULL if it cannot be determined from pattern
*/
static int
record_pattern_to_array(void *param, int argc, char **argv, char **colname)
{
Plistarray *depends = (Plistarray *)param;
Pkglist *d;
int slot;
if (argv == NULL)
return PDB_ERR;
d = malloc_pkglist();
d->patterns = xmalloc(2 * sizeof(char *));
d->patterns[0] = xstrdup(argv[0]);
d->patterns[1] = NULL;
d->patcount = 1;
/*
* XXX: default slot if no pkgbase available, should we allocate one
* outside of the normal range for these?
*/
slot = 0;
if (argv[1]) {
d->name = xstrdup(argv[1]);
slot = pkg_hash_entry(d->name, depends->size);
}
SLIST_INSERT_HEAD(&depends->head[slot], d, next);
return PDB_OK;
}
void
get_conflicts(Plistarray *conflicts)
{
pkgindb_doquery(LOCAL_CONFLICTS, record_pattern_to_array, conflicts);
}
/* find required files (REQUIRES) from PROVIDES or filename */
int
pkg_met_reqs(Plisthead *impacthead)
{
int met_reqs = 1;
Pkglist *pimpact, *requires;
Plistnumbered *requireshead = NULL;
struct stat sb;
#ifdef CHECK_PROVIDES
int foundreq;
Pkglist *impactprov = NULL, *provides = NULL;
Plistnumbered *l_provideshead = NULL, *r_provideshead = NULL;
l_provideshead = rec_pkglist(LOCAL_PROVIDES);
#endif
/* first, parse impact list */
SLIST_FOREACH(pimpact, impacthead, next) {
/*
* Skip entries that only operate on local packages (e.g.
* ACTION_SUPERSEDED).
*/
if (pimpact->rpkg == NULL)
continue;
/* retreive requires list for package */
if ((requireshead = rec_pkglist(REMOTE_REQUIRES,
pimpact->rpkg->full)) == NULL)
/* empty requires list (very unlikely) */
continue;
/* parse requires list */
SLIST_FOREACH(requires, requireshead->P_Plisthead, next) {
#ifdef CHECK_PROVIDES
foundreq = 0;
#endif
/* for performance sake, first check basesys */
if ((strncmp(requires->full, PREFIX,
strlen(PREFIX) - 1)) != 0) {
if (stat(requires->full, &sb) < 0) {
printf(MSG_REQT_NOT_PRESENT,
requires->full, pimpact->rpkg->full);
/*
* mark as DONOTHING,
* requirement missing
*/
pimpact->action = ACTION_UNMET_REQ;
met_reqs = 0;
}
/* was a basysfile, no need to check PROVIDES */
continue;
}
/*
* FIXME: the code below actually works, but there's no
* point losing performances when some REQUIRES do not
* match PROVIDES in pkg_summary(5). This is a known
* issue and will hopefuly be fixed.
*/
#ifndef CHECK_PROVIDES
continue;
#else
/* search what local packages provide */
SLIST_FOREACH(provides, l_provideshead->P_Plisthead,
next) {
if (strncmp(provides->full,
requires->full,
strlen(requires->full)) == 0) {
foundreq = 1;
/* found, no need to go further*/
break;
} /* match */
} /* SLIST_FOREACH LOCAL_PROVIDES */
/*
* REQUIRES was not found on local packages,
* try impact list
*/
if (!foundreq) {
/* re-parse impact list to retreive PROVIDES */
SLIST_FOREACH(impactprov, impacthead, next) {
if ((r_provideshead =
rec_pkglist(REMOTE_PROVIDES,
impactprov->full)) == NULL)
continue;
/*
* then parse provides list for
* every package
*/
SLIST_FOREACH(provides,
r_provideshead->P_Plisthead, next) {
if (strncmp(provides->full,
requires->full,
strlen(requires->full)) == 0) {
foundreq = 1;
/*
* found, no need to
* go further return
* to impactprov list
*/
break;
} /* match */
}
free_pkglist(&r_provideshead->P_Plisthead);
free(r_provideshead);
if (foundreq)
/* exit impactprov list loop */
break;
} /* SLIST_NEXT impactprov */
} /* if (!foundreq) LOCAL_PROVIDES -> impact list */
/* FIXME: BIG FAT DISCLAIMER
* as of 04/2009, some packages described in pkg_summary
* have unmet REQUIRES. This is a known bug that makes
* the PROVIDES untrustable and some packages
* uninstallable. foundreq is forced to 1 for now for
* every REQUIRES matching PREFIX.
*/
if (!foundreq) {
printf(MSG_REQT_NOT_PRESENT_DEPS,
requires->full);
foundreq = 1;
}
#endif
} /* SLIST_FOREACH requires */
free_pkglist(&requireshead->P_Plisthead);
free(requireshead);
} /* 1st impact SLIST_FOREACH */
#ifdef CHECK_PROVIDES
free_pkglist(&l_provideshead->P_Plisthead);
free(l_provideshead);
#endif
return met_reqs;
}
/*
* A remote package has been identified as matching a local conflict. Retrieve
* the local package and print the conflict.
*/
static void
print_pkg_conflict(const char *pattern, const char *pkgname)
{
char query[BUFSIZ];
char *conflict;
conflict = xmalloc(BUFSIZ * sizeof(char));
sqlite3_snprintf(BUFSIZ, query, REMOTE_CONFLICTS, pattern);
if (pkgindb_doquery(query, pdb_get_value, conflict) == PDB_OK)
printf(MSG_CONFLICT_PKG, pkgname, conflict);
XFREE(conflict);
}
/*
* Check if an incoming remote package conflicts with any local packages.
*/
int
pkg_has_conflicts(Pkglist *pkg, Plistarray *conflicts)
{
Pkglist *p;
int i, slot;
if (is_empty_plistarray(conflicts))
return 0;
slot = pkg_hash_entry(pkg->rpkg->name, conflicts->size);
SLIST_FOREACH(p, &conflicts->head[slot], next) {
for (i = 0; i < p->patcount; i++) {
if (!pkg_match(p->patterns[i], pkg->rpkg->full))
continue;
/*
* Incoming remote package matches a local CONFLICTS
* entry, print the conflict message and return fail.
*/
print_pkg_conflict(p->patterns[i], pkg->rpkg->full);
return 1;
}
}
/*
* We also need to check slot 0 if not already checked as that's where
* any CONFLICTS that have a complicated pattern and no pkgbase are
* stored.
*/
if (slot == 0)
return 0;
SLIST_FOREACH(p, &conflicts->head[0], next) {
for (i = 0; i < p->patcount; i++) {
if (!pkg_match(p->patterns[i], pkg->rpkg->full))
continue;
print_pkg_conflict(p->patterns[i], pkg->rpkg->full);
return 1;
}
}
return 0;
}
void
show_prov_req(const char *query, const char *pkgname)
{
const char *out[] = { "provided", "required" };
const char *say;
char *fullpkgname;
Plistnumbered *plisthead;
Pkglist *plist;
if ((fullpkgname = unique_pkg(pkgname, REMOTE_PKG)) == NULL)
errx(EXIT_FAILURE, MSG_PKG_NOT_AVAIL, pkgname);
say = (query == REMOTE_PROVIDES) ? out[0] : out[1];
if ((plisthead = rec_pkglist(query, fullpkgname)) == NULL) {
printf(MSG_NO_PROV_REQ, say, fullpkgname);
exit(EXIT_SUCCESS);
}
printf(MSG_FILES_PROV_REQ, say, fullpkgname);
SLIST_FOREACH(plist, plisthead->P_Plisthead, next)
printf("\t%s\n", plist->full);
free_pkglist(&plisthead->P_Plisthead);
free(plisthead);
XFREE(fullpkgname);
}