forked from falconindy/pkgfile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnosr.c
607 lines (516 loc) · 13.8 KB
/
nosr.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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#define _GNU_SOURCE
#include <errno.h>
#include <fnmatch.h>
#include <getopt.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include "nosr.h"
#include "match.h"
#include "result.h"
#include "update.h"
#include "util.h"
static struct config_t config;
static int archive_fgets(struct archive *a, struct archive_read_buffer *b)
{
char *i = NULL;
#if ARCHIVE_VERSION_NUMBER < 3000000
off_t offset;
#else
int64_t offset;
#endif
int done = 0;
/* ensure we start populating our line buffer at the beginning */
b->line_offset = b->line;
while(1) {
/* have we processed this entire block? */
if(b->block + b->block_size == b->block_offset) {
if(b->ret == ARCHIVE_EOF) {
/* reached end of archive on the last read, now we are out of data */
goto cleanup;
}
/* zero-copy - this is the entire next block of data. */
b->ret = archive_read_data_block(a, (void *)&b->block,
&b->block_size, &offset);
b->block_offset = b->block;
/* error, cleanup */
if(b->ret < ARCHIVE_OK) {
goto cleanup;
}
}
/* loop through the block looking for EOL characters */
for(i = b->block_offset; i < (b->block + b->block_size); i++) {
/* check if read value was null or newline */
if(*i == '\0' || *i == '\n') {
done = 1;
break;
}
}
/* allocate our buffer, or ensure our existing one is big enough */
if(!b->line) {
/* set the initial buffer to the read block_size */
CALLOC(b->line, b->block_size + 1, sizeof(char), b->ret = -ENOMEM; goto cleanup);
b->line_size = b->block_size + 1;
b->line_offset = b->line;
} else {
size_t needed = (size_t)((b->line_offset - b->line)
+ (i - b->block_offset) + 1);
if(needed > b->max_line_size) {
b->ret = -ERANGE;
goto cleanup;
}
if(needed > b->line_size) {
/* need to realloc + copy data to fit total length */
char *new;
CALLOC(new, needed, sizeof(char), b->ret = -ENOMEM; goto cleanup);
memcpy(new, b->line, b->line_size);
b->line_size = needed;
b->line_offset = new + (b->line_offset - b->line);
free(b->line);
b->line = new;
}
}
if(done) {
size_t len = (size_t)(i - b->block_offset);
memcpy(b->line_offset, b->block_offset, len);
b->line_offset[len] = '\0';
b->block_offset = ++i;
/* this is the main return point; from here you can read b->line */
return ARCHIVE_OK;
} else {
/* we've looked through the whole block but no newline, copy it */
size_t len = (size_t)(b->block + b->block_size - b->block_offset);
memcpy(b->line_offset, b->block_offset, len);
b->line_offset += len;
b->block_offset = i;
/* there was no new data, return what is left; saved ARCHIVE_EOF will be
* returned on next call */
if(len == 0) {
b->line_offset[0] = '\0';
return ARCHIVE_OK;
}
}
}
cleanup:
{
int ret = b->ret;
FREE(b->line);
memset(b, 0, sizeof(b));
return ret;
}
}
static size_t strip_newline(char *str)
{
size_t len;
if(str == '\0') {
return 0;
}
len = strlen(str);
while(len > 0 && str[len - 1] == '\n') {
len--;
}
str[len] = '\0';
return len;
}
static int is_binary(const char *line, size_t len)
{
const char *ptr;
/* directories aren't binaries */
if(line[len - 1] == '/') {
return 0;
}
ptr = memmem(line, len, "bin/", 4);
/* toss out the obvious non-matches */
if(!ptr) {
return 0;
}
/* match bin/... */
if(ptr == line) {
return 1;
}
/* match sbin/... */
if(line == ptr - 1 && *(ptr - 1) == 's') {
return 1;
}
/* match .../bin/ */
if(*(ptr - 1) == '/') {
return 1;
}
/* match .../sbin/ */
if(ptr >= line + 2 && *(ptr - 2) == '/' && *(ptr - 1) == 's') {
return 1;
}
return 0;
}
static int search_metafile(const char *repo, struct pkg_t *pkg,
struct archive *a, struct result_t *result) {
int found = 0;
const char * const files = "%FILES%";
struct archive_read_buffer buf;
memset(&buf, 0, sizeof(buf));
buf.max_line_size = 512 * 1024;
while(archive_fgets(a, &buf) == ARCHIVE_OK) {
size_t len = strip_newline(buf.line);
char *line;
if(!len || buf.line[len-1] == '/' || strcmp(buf.line, files) == 0 ||
(config.binaries && !is_binary(buf.line, len))) {
continue;
}
if(!found && config.filterfunc(&config.filter, buf.line, config.icase) == 0) {
if(config.verbose) {
if(asprintf(&line, "%s/%s %s\t/%s", repo, pkg->name, pkg->version, buf.line) == -1) {
fprintf(stderr, "error: failed to allocate memory\n");
return -1;
};
} else {
found = 1;
if(asprintf(&line, "%s/%s", repo, pkg->name) == -1) {
fprintf(stderr, "error: failed to allocate memory\n");
return -1;
};
}
result_add(result, line);
}
}
return 1;
}
static int list_metafile(const char *repo, struct pkg_t *pkg,
struct archive *a, struct result_t *result) {
int ret;
const char * const files = "%FILES%";
struct archive_read_buffer buf;
if(config.filterfunc(&config.filter, pkg->name, config.icase) != 0) {
return 1;
}
memset(&buf, 0, sizeof(buf));
buf.max_line_size = 512 * 1024;
while((ret = archive_fgets(a, &buf)) == ARCHIVE_OK) {
size_t len = strip_newline(buf.line);
char *line;
if(!len || strcmp(buf.line, files) == 0) {
continue;
}
if(config.binaries && !is_binary(buf.line, len)) {
continue;
}
if(asprintf(&line, "%s/%s /%s", repo, pkg->name, buf.line) == -1) {
fprintf(stderr, "error: failed to allocate memory\n");
return 1;
}
result_add(result, line);
}
return 1;
}
static int parse_pkgname(struct pkg_t *pkg, const char *entryname)
{
const char *slash, *ptr = strrchr(entryname, '-');
if(ptr) {
slash = ptr;
while(--ptr && ptr > entryname && *ptr != '-');
while(*++slash && *slash != '/');
if(*slash == '/' && *ptr == '-') {
pkg->name = strndup(entryname, ptr - entryname);
pkg->version = strndup(ptr + 1, slash - ptr - 1);
return 0;
}
}
return 1;
}
static void *load_repo(void *repo_obj)
{
int ret;
const char *entryname, *slash;
char repofile[1024];
struct archive *a;
struct archive_entry *e;
struct pkg_t *pkg;
struct repo_t *repo;
struct result_t *result;
repo = (struct repo_t *)repo_obj;
snprintf(repofile, 1024, "%s.files.tar.gz", repo->name);
result = result_new((char *)repo->name, 50);
CALLOC(pkg, 1, sizeof(struct pkg_t), return (void *)result);
a = archive_read_new();
archive_read_support_compression_all(a);
archive_read_support_format_all(a);
ret = archive_read_open_filename(a, repofile, ARCHIVE_DEFAULT_BYTES_PER_BLOCK);
if(ret != ARCHIVE_OK) {
/* fail silently if the file doesn't exist */
if(access(repofile, F_OK) == 0) {
fprintf(stderr, "error: failed to load repo: %s: %s\n", repofile,
archive_error_string(a));
}
goto cleanup;
}
repo->filefound = 1;
while(archive_read_next_header(a, &e) == ARCHIVE_OK) {
entryname = archive_entry_pathname(e);
slash = strrchr(entryname, '/');
if(!slash || strcmp(slash, "/files") != 0) {
continue;
}
ret = parse_pkgname(pkg, entryname);
if(ret != 0) {
fprintf(stderr, "error parsing pkgname from: %s\n", entryname);
continue;
}
ret = config.filefunc(repo->name, pkg, a, result);
/* clean out the struct, but don't get rid of it entirely */
free(pkg->name);
free(pkg->version);
switch(ret) {
case -1:
/* error */
/* FALLTHROUGH */
case 0:
/* done */
goto done;
case 1:
/* continue */
break;
}
}
done:
archive_read_close(a);
cleanup:
free(pkg);
archive_read_finish(a);
return (void *)result;
}
static int compile_pcre_expr(struct pcre_data *re, const char *preg, int flags)
{
const char *err;
char *anchored = NULL;
int err_offset;
/* did the user try to anchor this at BOL? */
if(preg[0] == '^') {
/* goddamnit, they did. cut off the first character. Conditionally also
* drop the second character if its a slash. This is ugly and hackish, but
* it's also a fairly odd edge case. --glob is a much better choice here,
* as its self-anchoring. */
preg++;
if(preg[0] == '/') {
preg++;
}
anchored = strdup(preg);
preg = anchored;
flags |= PCRE_ANCHORED;
}
re->re = pcre_compile(preg, flags, &err, &err_offset, NULL);
free(anchored);
if(!re->re) {
fprintf(stderr, "error: failed to compile regex at char %d: %s\n", err_offset, err);
return 1;
}
re->re_extra = pcre_study(re->re, 0, &err);
if(err) {
fprintf(stderr, "error: failed to study regex: %s\n", err);
pcre_free(re->re);
return 1;
}
return 0;
}
static void usage(void)
{
fprintf(stderr, "nosr " VERSION "\nUsage: nosr [options] target\n\n");
fprintf(stderr,
" Operations:\n"
" -l, --list list contents of a package\n"
" -s, --search search for packages containing the target (default)\n"
" -u, --update update repo files lists\n\n"
" Filtering:\n"
" -b, --binaries return only files contained in a bin dir\n"
" -g, --glob enable matching with glob characters\n"
" -i, --ignorecase use case insensitive matching\n"
" -R, --repo REPO search a specific repo\n"
" -r, --regex enable matching with pcre\n\n"
" -h, --help display this help and exit\n"
" -v, --verbose output more\n\n");
}
static int parse_opts(int argc, char **argv)
{
int opt, opt_idx;
static struct option opts[] = {
{"binaries", no_argument, 0, 'b'},
{"glob", no_argument, 0, 'g'},
{"help", no_argument, 0, 'h'},
{"ignorecase", no_argument, 0, 'i'},
{"list", no_argument, 0, 'l'},
{"repo", required_argument, 0, 'R'},
{"regex", no_argument, 0, 'r'},
{"search", no_argument, 0, 's'},
{"update", no_argument, 0, 'u'},
{"verbose", no_argument, 0, 'v'},
{0,0,0,0}
};
/* defaults */
config.filefunc = search_metafile;
while((opt = getopt_long(argc, argv, "bghilR:ruv", opts, &opt_idx)) != -1) {
switch(opt) {
case 'b':
config.binaries = 1;
break;
case 'g':
config.filterby = FILTER_GLOB;
break;
case 'h':
usage();
return 1;
case 'i':
config.icase = 1;
break;
case 'l':
config.filefunc = list_metafile;
break;
case 'R':
config.targetrepo = optarg;
break;
case 'r':
config.filterby = FILTER_REGEX;
break;
case 's':
config.filefunc = search_metafile;
break;
case 'u':
config.doupdate = 1;
break;
case 'v':
config.verbose = 1;
break;
default:
return 1;
}
}
return 0;
}
static int search_single_repo(struct repo_t **repos, int repocount, char *searchstring)
{
char *targetrepo = NULL, *slash;
int i, ret = 1;
if(config.targetrepo) {
targetrepo = config.targetrepo;
} else {
slash = strchr(searchstring, '/');
targetrepo = strdup(searchstring);
targetrepo[slash - searchstring] = '\0';
config.filter.glob = &slash[1];
}
config.filterby = FILTER_EXACT;
for(i = 0; i < repocount; i++) {
if(strcmp(repos[i]->name, targetrepo) == 0) {
struct result_t *result = load_repo((void *)repos[i]);
ret = (result->count == 0);
result_print(result);
result_free(result);
goto finish;
}
}
/* repo not found */
fprintf(stderr, "error: repo not available: %s\n", targetrepo);
finish:
if(!config.targetrepo) {
free(targetrepo);
}
return ret;
}
static struct result_t **search_all_repos(struct repo_t **repos, int repocount)
{
struct result_t **results;
pthread_t *t = NULL;
int i;
CALLOC(t, repocount, sizeof(pthread_t *), return NULL);
CALLOC(results, repocount, sizeof(struct result_t *), return NULL);
/* load and process DBs */
for(i = 0; i < repocount; i++) {
pthread_create(&t[i], NULL, load_repo, (void *)repos[i]);
}
/* gather results */
for(i = 0; i < repocount; i++) {
pthread_join(t[i], (void **)&results[i]);
}
free(t);
return results;
}
static int filter_setup(char *arg)
{
switch(config.filterby) {
case FILTER_EXACT:
config.filter.glob = arg;
config.filterfunc = match_exact;
break;
case FILTER_GLOB:
config.icase *= FNM_CASEFOLD;
config.filter.glob = arg;
config.filterfunc = match_glob;
break;
case FILTER_REGEX:
config.icase *= PCRE_CASELESS;
config.filterfunc = match_regex;
config.filterfree = free_regex;
if(compile_pcre_expr(&config.filter.re, arg, config.icase) != 0) {
return 1;
}
break;
}
return 0;
}
int main(int argc, char *argv[])
{
int i, repocount, reposfound = 0, ret = 1;
struct repo_t **repos = NULL;
struct result_t **results = NULL;
if(parse_opts(argc, argv) != 0) {
return 2;
}
if(mkdir(DBPATH, 0755) != 0 && errno != EEXIST) {
fprintf(stderr, "error: failed to create cachedir: " DBPATH ": %s\n", strerror(errno));
return 2;
}
if(chdir(DBPATH)) {
fprintf(stderr, "error: failed to chdir to " DBPATH ": %s\n", strerror(errno));
return 2;
}
repos = find_active_repos(PACMANCONFIG, &repocount);
if(!repocount) {
fprintf(stderr, "error: no repos found in " PACMANCONFIG "\n");
return 1;
}
if(config.doupdate) {
ret = !!nosr_update(repos, repocount);
goto cleanup;
}
if(optind == argc) {
fprintf(stderr, "error: no target specified (use -h for help)\n");
goto cleanup;
}
if(filter_setup(argv[optind]) != 0) {
goto cleanup;
}
/* override behavior on $repo/$pkg syntax or --repo */
if((config.filefunc == list_metafile && strchr(argv[optind], '/')) ||
config.targetrepo) {
ret = search_single_repo(repos, repocount, argv[optind]);
} else {
results = search_all_repos(repos, repocount);
for(ret = i = 0; i < repocount; i++) {
reposfound += repos[i]->filefound;
ret += result_print(results[i]);
result_free(results[i]);
}
if(!reposfound) {
fprintf(stderr, "error: No repo files found. Please run `nosr --update'.\n");
}
ret = ret > 0 ? 0 : 1;
}
if(config.filterfree) {
config.filterfree(&config.filter);
}
cleanup:
for(i = 0; i < repocount; i++) {
repo_free(repos[i]);
}
free(repos);
free(results);
return ret;
}
/* vim: set ts=2 sw=2 noet: */