-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprovides.c
583 lines (493 loc) · 14.2 KB
/
provides.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*-
* Copyright (c) 2017 Rodrigo Osorio <[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.
*/
#define PCRE2_CODE_UNIT_WIDTH 8
#include <sysexits.h>
#include <unistd.h>
#include <pkg.h>
#include <fetch.h>
#include <errno.h>
#include <strings.h>
#include <archive.h>
#include <fcntl.h>
#include <string.h>
#include <archive_entry.h>
#include <pcre2.h>
#include <libgen.h>
#include <sys/sysctl.h>
#include <sys/queue.h>
static char myname[] = "provides";
static char myversion[] = "0.7.4";
static char dbversion[] = "v3";
static char mydescription[] = "A plugin for querying which package provides a particular file";
static struct pkg_plugin *self;
bool force_flag = false;
bool fetch_on_update = true;
typedef struct file_t {
char *name;
SLIST_ENTRY (file_t) next;
} file_t;
SLIST_HEAD (file_head_t, file_t);
typedef struct fpkg_t {
char *pkg_name;
struct file_head_t files;
SLIST_ENTRY (fpkg_t) next;
} fpkg_t;
SLIST_HEAD (pkg_head_t, fpkg_t);
struct search_t {
struct pkg_head_t head;
pcre2_code *regex;
fpkg_t *pnode;
char * pattern;
pcre2_match_data *match_data;
};
void provides_progressbar_start(const char *pmsg);
void provides_progressbar_stop(void);
void provides_progressbar_tick(int64_t current, int64_t total);
int mkpath(char *path);
int bigram_expand(FILE *fp, void (*match_cb)(const char *,struct search_t *), void *extra);
int config_fetch_on_update();
char *config_get_remote_srv();
char *config_get_filepath();
#define BUFLEN 4096
#define MAX_FN_SIZE 255
#define PKG_DB_PATH "/var/db/pkg/provides/"
int
pkg_plugin_shutdown(struct pkg_plugin *p __unused)
{
/* nothing to be done here */
return (EPKG_OK);
}
void
plugin_provides_usage(void)
{
fprintf(stderr, "usage: pkg %s [-uf] [-r repo] pattern\n\n", myname);
fprintf(stderr, "%s\n", mydescription);
}
int get_filepath(char *filename, size_t size)
{
static char osver[1024];
static char arch[1024];
static char osname[1024];
int mib_arch[] = { (CTL_HW), (HW_MACHINE_ARCH) };
int mib_ver[] = { (KERN_OSTYPE), (KERN_OSRELEASE) };
int mib_os[] = { (KERN_OSTYPE), (KERN_OSTYPE) };
size_t len;
char * ptr;
#ifdef __FreeBSD__
char sep[] = ".";
#else
char sep[] = "-";
#endif
ptr = config_get_filepath();
if (ptr != NULL) {
strncpy(filename, ptr, size);
return 0;
}
len = sizeof osname;
if (sysctl(mib_os, 2, &osname, &len, NULL, 0) == -1) {
return -1;
}
len = sizeof osver;
if (sysctl(mib_ver, 2, &osver, &len, NULL, 0) == -1) {
return (-1);
}
ptr = strstr(osver, sep);
if (ptr == NULL) {
return (-1);
} else {
ptr[0] = '\0';
}
len = sizeof arch;
if (sysctl(mib_arch, 2, &arch, &len, NULL, 0) == -1) {
return -1;
}
#ifndef __FreeBSD__
if ((ptr = strstr(arch, "_")) != NULL) {
ptr[0] = ':';
}
#endif
if(snprintf(filename, size, "%s/%s/%s:%s", dbversion, osname, osver, arch) < 0) {
return -1;
}
return (0);
}
int
plugin_archive_extract(int fd, const char *out)
{
struct archive_entry *ae = NULL;
struct archive *ar = NULL;
int fd_out = -1;
fd_out = open(out, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if ( fd_out < 0) {
return -1;
}
ar = archive_read_new();
if (ar == NULL) {
goto error;
}
archive_read_support_filter_all(ar);
archive_read_support_format_raw(ar);
archive_read_open_fd(ar, fd, BUFLEN);
if (archive_read_next_header(ar, &ae) == ARCHIVE_OK) {
if (archive_read_data_into_fd(ar, fd_out) != 0) {
fprintf(stderr,"archive_read_data failed\n");
goto error;
}
}
archive_read_free(ar);
close(fd_out);
return (0);
error:
if (fd_out) {
close(fd);
unlink(out);
}
return -1;
}
int
plugin_fetch_file(void)
{
char buffer[BUFLEN];
FILE *fi;
int fo, ft;
int count;
struct url_stat us;
int64_t size = 0;
char tmpfile[] = "/var/tmp/pkg-provides-XXXX";
struct stat sb;
char path[] =PKG_DB_PATH;
char filepath[MAX_FN_SIZE + 1];
char url[MAXPATHLEN + 1];
if(get_filepath(filepath, MAX_FN_SIZE) != 0) {
fprintf(stderr,"Can't get the OS ABI\n");
return (-1);
}
snprintf(url, MAXPATHLEN , "%s/%s/provides.db.xz", config_get_remote_srv(), filepath);
ft = open( PKG_DB_PATH "provides.db", O_WRONLY);
if (ft < 0) {
if (errno == ENOENT) {
if (mkpath(path) == 0) {
ft = open(PKG_DB_PATH "provides.db", O_RDWR | O_CREAT);
}
}
if (ft < 0) {
fprintf(stderr,"Insufficient privileges to update the provides database.\n");
return (-1);
}
unlink(PKG_DB_PATH "provides.db");
} else {
if(fstat(ft, &sb) < 0) {
fprintf(stderr,"fstat error\n");
close(ft);
return (-1);
}
close(ft);
if(fetchStatURL(url, &us, "") != 0) {
fprintf(stderr,"fetchStatURL error : %s\n",url);
return -1;
}
if(us.mtime < sb.st_mtim.tv_sec && (!force_flag)) {
printf("The provides database is up-to-date.\n");
return (0);
}
}
close(ft);
fo = mkstemp(tmpfile);
if(fo < 0) {
fprintf(stderr, "mkstemp failed\n");
goto error;
}
unlink(tmpfile);
fi = fetchXGetURL(url, &us, "");
if (fi == NULL) {
fprintf(stderr, "fetchXGetURL error: %s\n", url);
goto error;
}
provides_progressbar_start("Fetching provides database");
provides_progressbar_tick(size,us.size);
while ((count = fread(buffer, 1, BUFLEN, fi)) > 0) {
if(write(fo, buffer, count) != count) {
fprintf(stderr, "Could not write to temporary file.\n");
goto error;
}
size += count;
provides_progressbar_tick(size,us.size);
}
if (!feof(fi)) {
fprintf(stderr, "Error reading from %s\n", url);
goto error;
}
printf("Extracting database....");
fflush(stdout);
lseek(fo, SEEK_SET, 0);
if (plugin_archive_extract(fo, PKG_DB_PATH "provides.db") != 0 ) {
printf("fail\n");
goto error;
}
lchmod(PKG_DB_PATH "provides.db",S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
printf("success\n");
fclose(fi);
close(fo);
return EPKG_OK;
error:
if (fi != NULL) {
fclose(fi);
provides_progressbar_stop();
}
if (fo >= 0) {
close(fo);
}
return (-1);
}
static void
free_list(struct pkg_head_t *head) {
fpkg_t *pnode, *prev_n;
file_t *pfile, *prev_f;
prev_n = NULL;
SLIST_FOREACH(pnode,head, next) {
if (prev_n) {
free(prev_n);
}
prev_f = NULL;
SLIST_FOREACH(pfile,&(pnode->files), next) {
if (prev_f) {
free(prev_f);
}
free(pfile->name);
prev_f = pfile;
}
free(pnode->pkg_name);
prev_n = pnode;
}
}
static int
display_per_repo(char *repo_name, struct pkg_head_t *head)
{
struct pkgdb_it *it;
struct pkg *pkg = NULL;
struct pkg_repo *r = NULL;
struct pkgdb *db = NULL;
fpkg_t *pnode;
file_t *pfile;
int ret ;
if (pkgdb_open_all(&db, PKGDB_REMOTE,repo_name) != EPKG_OK) {
fprintf(stderr, "Can't open %s database\n",repo_name);
return -1;
}
SLIST_FOREACH(pnode,head, next) {
it = pkgdb_repo_query(db,pnode->pkg_name,MATCH_EXACT,repo_name);
if (it == NULL) {
continue;
}
if ((ret = pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC)) != EPKG_OK) {
pkgdb_it_free(it);
continue;
}
printf("%-8s: ", "Name");
pkg_printf("%n-", pkg);
pkg_printf("%v\n", pkg);
printf("%-8s: ", "Comment");
pkg_printf("%c\n", pkg);
printf("%-8s: ", "Repo");
printf("%s\n", repo_name);
SLIST_FOREACH(pfile,&(pnode->files), next) {
if(SLIST_FIRST(&(pnode->files)) == pfile) {
printf("Filename: %s\n",pfile->name);
} else {
printf(" %s\n",pfile->name);
}
}
if(SLIST_NEXT(pnode, next) != NULL) {
printf("\n");
}
pkgdb_it_free(it);
}
pkgdb_close(db);
return (0);
}
void
match_cb(const char * line, struct search_t *search)
{
file_t *pfile;
fpkg_t *pnode;
char *separator = strstr(line,"*");
if(separator != NULL) {
char *exp;
char * fullpath = separator + 1;
if(strstr(search->pattern,"/")) {
exp = fullpath;
} else {
exp = basename(fullpath);
}
if (pcre2_match(search->regex, (PCRE2_SPTR)exp, strlen(exp), 0, 0, search->match_data, NULL) > 0) {
int found = 0;
char * name = strndup(line, (separator - line + 1));
name[separator - line] = '\0';
SLIST_FOREACH(pnode,&(search->head), next) {
if(strcmp(pnode->pkg_name, name)==0) {
found = 1;
break;
}
}
if (found == 0) {
pnode = malloc (sizeof(struct fpkg_t));
if (pnode == NULL) {
exit(ENOMEM);
} else {
pnode->pkg_name = name;
if(pnode->pkg_name == NULL) {
exit(ENOMEM);
}
SLIST_INIT (&(pnode->files));
}
SLIST_INSERT_HEAD(&(search->head),pnode,next);
} else {
free(name);
}
pfile = malloc(sizeof(struct file_t));
if(pfile == NULL) {
exit(ENOMEM);
}
pfile->name = strdup(fullpath + 1);
if(pfile->name == NULL) {
exit(ENOMEM);
}
SLIST_INSERT_HEAD(&pnode->files,pfile,next);
}
}
}
int
plugin_provides_search(char *repo, char *pattern)
{
FILE *fh;
PCRE2_SIZE pcreErrorOffset;
int pcreErrorNumber;
char *repo_name;
struct pkg_repo *r = NULL;
struct search_t search;
memset(&search, 0, sizeof(search));
search.pattern = pattern;
SLIST_INIT (&search.head);
fh = fopen(PKG_DB_PATH "provides.db","r");
if (fh == NULL) {
fprintf(stderr, "Provides database not found, please update first.\n");
return (-1);
}
search.regex = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, PCRE2_CASELESS, &pcreErrorNumber, &pcreErrorOffset, NULL);
if(search.regex == NULL) {
fprintf(stderr, "Invalid search pattern\n");
goto error_pcre;
}
search.match_data = pcre2_match_data_create_from_pattern(search.regex, NULL);
if (bigram_expand(fh, &match_cb, &search) == -1) {
fprintf(stderr, "Corrupted database\n");
}
while (pkg_repos(&r) == EPKG_OK) {
if (pkg_repo_enabled(r)) {
repo_name = (char *)pkg_repo_name(r);
if (repo == NULL || strcmp(repo, repo_name) == 0) {
display_per_repo(repo_name, &search.head);
}
}
}
pcre2_match_data_free(search.match_data);
fclose(fh);
pcre2_code_free(search.regex);
free_list(&search.head);
return (0);
error_pcre:
fclose(fh);
if (search.regex != NULL) {
pcre2_code_free(search.regex);
}
return (-1);
}
int cb_event(void *data, struct pkgdb *db) {
struct pkg_event *ev = data;
if (ev->type == PKG_EVENT_INCREMENTAL_UPDATE && fetch_on_update) {
plugin_fetch_file();
}
return (EPKG_OK);
}
int
plugin_provides_callback(int argc, char **argv)
{
int ch;
bool do_update = false;
char *repo = NULL;
while ((ch = getopt(argc, argv, "ufr:")) != -1) {
switch (ch) {
case 'u':
do_update = true;
break;
case 'f':
force_flag = true;
break;
case 'r':
repo = optarg;
break;
default:
plugin_provides_usage();
return (EX_USAGE);
break;
}
}
if (do_update) {
return plugin_fetch_file();
}
argc -= optind;
argv += optind;
if (argc <= 0) {
plugin_provides_usage();
return (EX_USAGE);
}
plugin_provides_search(repo, argv[0]);
return (EPKG_OK);
}
int
pkg_plugin_init(struct pkg_plugin *p)
{
self = p;
pkg_plugin_set(p, PKG_PLUGIN_NAME, myname);
pkg_plugin_set(p, PKG_PLUGIN_VERSION, myversion);
pkg_plugin_set(p, PKG_PLUGIN_DESC, mydescription);
pkg_plugin_hook_register(p, PKG_PLUGIN_HOOK_EVENT, cb_event);
fetch_on_update = config_fetch_on_update() ? true : false;
return (EPKG_OK);
}
int
pkg_register_cmd(int idx, const char **name, const char **desc, int (**exec)(int argc, char **argv))
{
*name = myname;
*desc = mydescription;
*exec = plugin_provides_callback;
return (EPKG_OK);
}
int
pkg_register_cmd_count (void)
{
return 1;
}