forked from freebsd/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdating.c
390 lines (356 loc) · 8.38 KB
/
updating.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*-
* Copyright (c) 2011-2022 Baptiste Daroussin <[email protected]>
* Copyright (c) 2014 Matthew Seaman <[email protected]>
* All rights reserved.
*
* 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
* in this position and unchanged.
* 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(S) ``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(S) 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.
*/
#ifdef HAVE_CONFIG_H
#include "pkg_config.h"
#endif
#ifdef HAVE_CAPSICUM
#include <sys/capsicum.h>
#endif
#include <err.h>
#include <errno.h>
#include <getopt.h>
#include <pkg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <regex.h>
#include <tllist.h>
#include "pkgcli.h"
struct installed_ports {
char *origin;
};
struct regex_cache {
char *pattern;
regex_t reg;
};
void
usage_updating(void)
{
fprintf(stderr, "Usage: pkg updating [-i] [-d YYYYMMDD] [-f file] [portname ...]\n");
fprintf(stderr, "For more information see 'pkg help updating'.\n");
}
static char *
convert_re(const char *src)
{
const char *p;
char *q;
bool brace_flag = false;
size_t len = strlen(src);
char *buf = malloc(len*2+1);
if (buf == NULL)
return NULL;
for (p=src, q=buf; p < src+len; p++) {
switch (*p) {
case '*':
*q++ = '.';
*q++ = '*';
break;
case '?':
*q++ = '.';
break;
case '.':
*q++ = '\\';
*q++ = '.';
break;
case '{':
*q++='(';
brace_flag=true;
break;
case ',':
if (brace_flag)
*q++='|';
else
*q++=*p;
break;
case '}':
*q++=')';
brace_flag=false;
break;
default:
*q++ = *p;
}
}
*q ='\0';
return buf;
}
int
matcher(const char *affects, const char *origin, bool ignorecase)
{
int i, n, count, found, ret, rc;
bool was_spc;
size_t len;
char *re, *buf, *p, **words;
struct regex_cache *ent;
tll(struct regex_cache *) cache = tll_init();
len = strlen(affects);
buf = strdup(affects);
if (buf == NULL)
return 0;
for (count = 0, was_spc = true, p = buf; p < buf + len ; p++) {
if (isspace(*p)) {
if (!was_spc)
was_spc = true;
*p = '\0';
} else {
if (was_spc) {
count++;
was_spc = false;
}
}
}
words = malloc(sizeof(char*)*count);
if (words == NULL) {
free(buf);
return 0;
}
for (i = 0, was_spc = true, p = buf; p < buf + len ; p++) {
if (*p == '\0') {
if (!was_spc)
was_spc = true;
} else {
if (was_spc) {
words[i++] = p;
was_spc = false;
}
}
}
for(ret = 0, i = 0; i < count; i++) {
n = strlen(words[i]);
if (words[i][n-1] == ',') {
words[i][n-1] = '\0';
}
if (strpbrk(words[i],"^$*|?") == NULL &&
(strchr(words[i],'[') == NULL || strchr(words[i],']') == NULL) &&
(strchr(words[i],'{') == NULL || strchr(words[i],'}') == NULL) &&
(strchr(words[i],'(') == NULL || strchr(words[i],')') == NULL)) {
if (ignorecase) {
if (strcasecmp(words[i], origin) == 0) {
ret = 1;
break;
}
} else {
if (strcmp(words[i], origin) == 0) {
ret = 1;
break;
}
}
continue;
}
found = 0;
tll_foreach(cache, it) {
if (ignorecase)
rc = strcasecmp(words[i], it->item->pattern);
else
rc = strcmp(words[i], it->item->pattern);
if (rc == 0) {
found++;
if (regexec(&it->item->reg, origin, 0, NULL, 0) == 0) {
ret = 1;
break;
}
}
}
if (found == 0) {
ent = malloc(sizeof(struct regex_cache));
if (ent == NULL)
goto err;
if ((ent->pattern = strdup(words[i])) == NULL) {
free(ent);
goto err;
}
re = convert_re(words[i]);
if (re == NULL) {
free(ent->pattern);
free(ent);
goto err;
}
regcomp(&ent->reg, re, (ignorecase) ? REG_ICASE|REG_EXTENDED : REG_EXTENDED);
free(re);
tll_push_front(cache, ent);
if (regexec(&ent->reg, origin, 0, NULL, 0) == 0) {
ret = 1;
break;
}
}
}
free(words);
free(buf);
return ret;
err:
free(words);
free(buf);
return 0;
}
int
exec_updating(int argc, char **argv)
{
char *date = NULL;
char *dateline = NULL;
char *updatingfile = NULL;
bool caseinsensitive = false;
struct installed_ports *port;
tll(struct installed_ports *) origins = tll_init();
int ch;
char *line = NULL;
size_t linecap = 0;
char *tmp;
int head = 0;
int found = 0;
struct pkgdb *db = NULL;
struct pkg *pkg = NULL;
struct pkgdb_it *it = NULL;
FILE *fd;
int retcode = EXIT_SUCCESS;
#ifdef HAVE_CAPSICUM
cap_rights_t rights;
#endif
struct option longopts[] = {
{ "date", required_argument, NULL, 'd' },
{ "file", required_argument, NULL, 'f' },
{ "case-insensitive", no_argument, NULL, 'i' },
{ NULL, 0, NULL, 0 },
};
while ((ch = getopt_long(argc, argv, "+d:f:i", longopts, NULL)) != -1) {
switch (ch) {
case 'd':
date = optarg;
break;
case 'f':
updatingfile = optarg;
break;
case 'i':
caseinsensitive = true;
break;
default:
usage_updating();
return (EXIT_FAILURE);
}
}
argc -= optind;
argv += optind;
/* checking date format */
if (date != NULL)
if (strlen(date) != 8 || strspn(date, "0123456789") != 8)
err(EXIT_FAILURE, "Invalid date format");
if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK)
return (EXIT_FAILURE);
if (pkgdb_obtain_lock(db, PKGDB_LOCK_READONLY) != EPKG_OK) {
pkgdb_close(db);
warnx("Cannot get a read lock on a database, it is locked by another process");
return (EXIT_FAILURE);
}
if (updatingfile == NULL) {
const char *portsdir = pkg_object_string(pkg_config_get("PORTSDIR"));
if (portsdir == NULL) {
retcode = EXIT_FAILURE;
goto cleanup;
}
asprintf(&updatingfile, "%s/UPDATING", portsdir);
}
fd = fopen(updatingfile, "r");
if (fd == NULL) {
warnx("Unable to open: %s", updatingfile);
goto cleanup;
}
#ifdef HAVE_CAPSICUM
cap_rights_init(&rights, CAP_READ);
if (cap_rights_limit(fileno(fd), &rights) < 0 && errno != ENOSYS ) {
warn("cap_rights_limit() failed");
fclose(fd);
return (EXIT_FAILURE);
}
#ifndef PKG_COVERAGE
if (cap_enter() < 0 && errno != ENOSYS) {
warn("cap_enter() failed");
fclose(fd);
return (EXIT_FAILURE);
}
#endif
#endif
if (argc == 0) {
if ((it = pkgdb_query(db, NULL, MATCH_ALL)) == NULL) {
retcode = EXIT_FAILURE;
fclose(fd);
goto cleanup;
}
while (pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC) == EPKG_OK) {
port = malloc(sizeof(struct installed_ports));
pkg_asprintf(&port->origin, "%o", pkg);
tll_push_front(origins, port);
}
} else {
while (*argv) {
port = malloc(sizeof(struct installed_ports));
port->origin = strdup(*argv);
tll_push_front(origins, port);
argv++;
}
}
while (getline(&line, &linecap, fd) > 0) {
if (strspn(line, "0123456789:") == 9) {
dateline = strdup(line);
found = 0;
head = 1;
} else if (head == 0) {
continue;
}
tmp = NULL;
if (found == 0) {
if (strstr(line, "AFFECTS") != NULL) {
tll_foreach(origins, it) {
if (matcher(line, it->item->origin, caseinsensitive) != 0) {
tmp = "";
break;
}
}
if (tmp == NULL)
tmp = strcasestr(line, "all users\n");
if (tmp == NULL)
tmp = strcasestr(line, "all ports users\n");
if (tmp != NULL) {
if ((date != NULL) && strncmp(dateline, date, 8) < 0) {
continue;
}
printf("%s%s",dateline, line);
found = 1;
}
}
} else {
printf("%s",line);
}
}
fclose(fd);
cleanup:
pkgdb_it_free(it);
pkgdb_release_lock(db, PKGDB_LOCK_READONLY);
pkgdb_close(db);
pkg_free(pkg);
free(dateline);
return (retcode);
}