-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkgfl.c
714 lines (548 loc) · 18 KB
/
pkgfl.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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
/*
Copyright (C) 2000 - 2008 Pawel A. Gajda <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2 as
published by the Free Software Foundation (see file COPYING for details).
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <fnmatch.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <trurl/trurl.h>
#include "compiler.h"
#include "i18n.h"
#include "log.h"
#include "pkg.h"
#include "pkgfl.h"
#include "depdirs.h"
struct dirname_h {
tn_hash *dnh;
tn_alloc *na;
int n;
};
static struct dirname_h dirname_h = { 0, 0, 0 };
static inline char *register_dn(char *dn)
{
char *dnn;
if (dirname_h.dnh == NULL) {
dirname_h.na = n_alloc_new(16, TN_ALLOC_OBSTACK);
dirname_h.dnh = n_hash_new_na(dirname_h.na, 4096, NULL);
n_hash_ctl(dirname_h.dnh, TN_HASH_NOCPKEY | TN_HASH_REHASH);
}
if ((dnn = n_hash_get(dirname_h.dnh, dn)) == NULL) {
int len = strlen(dn) + 1;
dnn = dirname_h.na->na_malloc(dirname_h.na, len);
memcpy(dnn, dn, len);
n_hash_insert(dirname_h.dnh, dnn, dnn);
} else {
dirname_h.n++;
//printf("hhhh %d %s\n", dirname_h.n, dnn);
}
return dnn;
}
struct flfile *flfile_new(tn_alloc *na, uint32_t size, uint16_t mode,
const char *basename, int blen,
const char *slinkto, int slen)
{
struct flfile *file;
char *p;
if (na)
file = na->na_malloc(na, sizeof(*file) + blen + 1 + slen + 1);
else
file = n_malloc(sizeof(*file) + blen + 1 + slen + 1);
file->mode = mode;
file->size = size;
memcpy(file->basename, basename, blen);
p = file->basename + blen;
*p++ = '\0';
*p = '\0';
if (slinkto && *slinkto) {
memcpy(p, slinkto, slen);
*(p + slen) = '\0';
}
return file;
}
struct flfile *flfile_clone(struct flfile *flfile)
{
int bnl = strlen(flfile->basename);
return flfile_new(NULL, flfile->size, flfile->mode,
flfile->basename, bnl,
S_ISLNK(flfile->mode) ? flfile->basename + bnl + 1 : NULL,
S_ISLNK(flfile->mode) ? strlen(flfile->basename + bnl + 1) : 0);
}
int flfile_cnfl2(const struct flfile *f1, uint32_t size, uint16_t mode,
const char *slinkto, int strict)
{
register int cmprc;
if ((cmprc = (f1->size - size)) == 0)
cmprc = f1->mode - mode;
if (cmprc == 0 || strict == 0) {
if (S_ISLNK(f1->mode)) {
if (!S_ISLNK(mode))
cmprc = 1;
else {
register char *l1;
l1 = strchr(f1->basename, '\0') + 1;
n_assert(slinkto);
cmprc = strcmp(l1, slinkto);
}
} else if (S_ISLNK(mode)) {
cmprc = -1;
}
}
if (cmprc && strict == 0 && S_ISDIR(f1->mode) && S_ISDIR(mode))
cmprc = 0;
return cmprc;
}
int flfile_cnfl(const struct flfile *f1, const struct flfile *f2, int strict)
{
register int cmprc;
if (f1->mode == 0 || f2->mode == 0) /* missing FILEMODES || FILESIZES */
return 0;
if ((cmprc = (f1->mode - f2->mode)) == 0 &&
!S_ISDIR(f1->mode) && !S_ISDIR(f1->mode))
cmprc = f1->size - f2->size;
if (cmprc == 0 || strict == 0) {
if (S_ISLNK(f1->mode)) {
if (!S_ISLNK(f2->mode))
cmprc = 1;
else {
register char *l1, *l2;
l1 = strchr(f1->basename, '\0') + 1;
l2 = strchr(f2->basename, '\0') + 1;
cmprc = strcmp(l1, l2);
}
} else if (S_ISLNK(f2->mode)) {
cmprc = -1;
}
}
if (cmprc && strict == 0 && S_ISDIR(f1->mode) && S_ISDIR(f2->mode))
cmprc = 0;
return cmprc;
}
int flfile_cmp(const struct flfile *f1, const struct flfile *f2)
{
register int cmprc;
//printf("cmp %s %s\n", f1->basename, f2->basename);
if ((cmprc = strcmp(f1->basename, f2->basename)))
return cmprc;
if ((cmprc = (f1->size - f2->size)) == 0)
cmprc = f1->mode - f2->mode;
if (cmprc == 0 && S_ISLNK(f1->mode)) {
register char *l1, *l2;
l1 = strchr(f1->basename, '\0') + 1;
l2 = strchr(f2->basename, '\0') + 1;
cmprc = strcmp(l1, l2);
}
return cmprc;
}
int flfile_cmp_qsort(const struct flfile **f1, const struct flfile **f2)
{
return flfile_cmp(*f1, *f2);
}
int pkgfl_ent_cmp(const void *a, const void *b)
{
const struct pkgfl_ent *aa = a;
const struct pkgfl_ent *bb = b;
return strcmp(aa->dirname, bb->dirname);
}
static
int pkgfl_ent_deep_cmp(const void *a, const void *b)
{
register int i, cmprc;
const struct pkgfl_ent *aa = a;
const struct pkgfl_ent *bb = b;
if ((cmprc = strcmp(aa->dirname, bb->dirname)))
return cmprc;
if ((cmprc = aa->items - bb->items))
return cmprc;
for (i = 0; i < aa->items; i++)
if ((cmprc = flfile_cmp(aa->files[i], bb->files[i])))
return cmprc;
logn(LOGERR | LOGDIE, "pkgfl_ent_deep_cmp: %p:%s eq %p:%s", aa, aa->dirname,
bb, bb->dirname);
n_assert(0); /* directories must be different */
return cmprc;
}
tn_array *pkgfl_array_new(int size)
{
tn_array *arr;
if ((arr = n_array_new(size, NULL, pkgfl_ent_cmp)) != NULL)
n_array_ctl(arr, TN_ARRAY_AUTOSORTED);
return arr;
}
tn_tuple *pkgfl_array_pdir_sort(tn_tuple *fl)
{
return n_tuple_isort_ex(fl, pkgfl_ent_deep_cmp);
}
/* trim slashes from dirname, update dirnamelen */
static
char *prepare_dirname(char *dirname, int *dirnamelen)
{
if (dirname[*dirnamelen - 1] == '/' && *dirnamelen > 1) {
(*dirnamelen)--;
dirname[*dirnamelen] = '\0';
}
if (*dirname == '/' && *dirnamelen > 1) {
dirname++;
(*dirnamelen)--;
}
return dirname;
}
struct pkgfl_ent *pkgfl_ent_new(tn_alloc *na,
char *dirname, int dirname_len, int nfiles)
{
struct pkgfl_ent *flent;
flent = na->na_malloc(na, sizeof(*flent)+(nfiles * sizeof(struct flfile*)));
dirname = prepare_dirname(dirname, &dirname_len);
flent->dirname = register_dn(dirname);
flent->items = 0;
DBGF("flent_new %s %d\n", flent->dirname, nfiles);
return flent;
}
static int strncmp_path(const char *p1, const char *p2)
{
int rc;
if ((rc = *p1 - *p2))
return rc;
rc = strncmp(p1, p2, strlen(p1));
//printf("cmp %s %s = %d\n", p1, p2, rc);
return rc;
}
/*
stores file list as binary data
*/
static
int pkgfl_store_buf(tn_tuple *fl, tn_buf *nbuf, tn_array *exclpath,
tn_array *depdirs, int which)
{
uint8_t *matches, *skipped, *lengths;
int i, j;
int ndirs = 0;
matches = alloca(n_tuple_size(fl) * sizeof(*matches));
memset(matches, 0, n_tuple_size(fl) * sizeof(*matches));
skipped = alloca(n_tuple_size(fl) * sizeof(*matches));
memset(skipped, 0, n_tuple_size(fl) * sizeof(*skipped));
lengths = alloca(n_tuple_size(fl) * sizeof(*lengths));
memset(lengths, 0, n_tuple_size(fl) * sizeof(*lengths));
if (which == PKGFL_ALL && exclpath == NULL) {
memset(matches, 1, n_tuple_size(fl) * sizeof(*matches));
ndirs = n_tuple_size(fl);
} else {
for (i=0; i<n_tuple_size(fl); i++) {
struct pkgfl_ent *flent = n_tuple_nth(fl, i);
int is_depdir = 0, dnl;
dnl = strlen(flent->dirname);
n_assert(dnl < UINT8_MAX - 1);
lengths[i] = dnl;
if (depdirs)
is_depdir = (n_array_bsearch(depdirs, flent->dirname) != NULL);
n_assert(which != PKGFL_ALL);
if (which == PKGFL_DEPDIRS && is_depdir) {
matches[i] = 1;
ndirs++;
} else if (which == PKGFL_NOTDEPDIRS && !is_depdir) {
if (exclpath && n_array_bsearch_ex(exclpath, flent->dirname,
(tn_fn_cmp)strncmp_path)) {
DBGF("%s skipped\n", flent->dirname);
skipped[i] = 1;
continue;
}
matches[i] = 1;
ndirs++;
}
}
}
n_buf_add_int32(nbuf, ndirs);
for (i=0; i < n_tuple_size(fl); i++) {
struct pkgfl_ent *flent = n_tuple_nth(fl, i);
uint8_t dnl;
if (matches[i] == 0)
continue;
dnl = strlen(flent->dirname) + 1;
n_buf_add_int8(nbuf, dnl);
n_buf_add(nbuf, flent->dirname, dnl);
n_buf_add_int32(nbuf, flent->items);
#if 0
if (strstr(flent->dirname, "SourceForgeXX"))
printf("\nDIR %s\n", flent->dirname);
#endif
for (j=0; j < flent->items; j++) {
struct flfile *file = flent->files[j];
uint8_t bnl = strlen(file->basename);
#if 0
if (strstr(flent->dirname, "SourceForgeXX"))
printf("STORE%d %s\n", which, file->basename);
#endif
n_buf_add_int8(nbuf, bnl);
n_buf_add(nbuf, file->basename, bnl);
n_buf_add_int16(nbuf, file->mode);
n_buf_add_int32(nbuf, file->size);
if (S_ISLNK(file->mode)) {
char *linkto = file->basename + bnl + 1;
bnl = strlen(linkto);
n_buf_add_int8(nbuf, bnl);
n_buf_add(nbuf, linkto, bnl);
#if 0
if (strstr(flent->dirname, "SourceForgeXX"))
printf("STORE%d linkto %s\n", which, linkto);
#endif
}
}
}
return ndirs;
}
#if 0 /* unused */
int pkgfl_store_st(tn_array *fl, tn_stream *st, tn_array *depdirs, int which)
{
tn_buf *nbuf;
int rc;
nbuf = n_buf_new(4096);
pkgfl_store_buf(fl, nbuf, NULL, depdirs, which);
rc = n_buf_store_buf(nbuf, st, TN_BUF_STORE_32B);
n_buf_free(nbuf);
n_stream_printf(st, "\n");
return rc;
}
#endif
int pkgfl_store(tn_tuple *fl, tn_buf *nbuf,
tn_array *exclpath, tn_array *depdirs, int which)
{
int sizeoffs, offs;
int32_t bsize;
sizeoffs = n_buf_tell(nbuf);
n_buf_seek(nbuf, sizeof(bsize), SEEK_CUR); /* place for buf size */
offs = n_buf_tell(nbuf);
pkgfl_store_buf(fl, nbuf, exclpath, depdirs, which);
bsize = n_buf_tell(nbuf) - offs;
n_assert(bsize > 0);
/* write buffer size */
n_buf_seek(nbuf, sizeoffs, SEEK_SET);
n_buf_write_int32(nbuf, bsize);
n_buf_seek(nbuf, 0, SEEK_END);
n_buf_puts(nbuf, "\n");
return bsize;
}
static int pkgfl_restore(tn_alloc *na, tn_tuple **fl,
tn_buf_it *nbufi, tn_array *dirs, int include)
{
struct pkgfl_ent **ents;
int32_t ndirs = 0, n;
int j, default_loadir;
*fl = NULL;
default_loadir = 1;
if (dirs)
default_loadir = include ? 0 : 1;
if (!n_buf_it_get_int32(nbufi, &ndirs))
return -1;
ents = alloca(ndirs * sizeof(*ents));
n = 0;
while (ndirs--) {
struct pkgfl_ent *flent = NULL;
char *dn = NULL;
uint8_t dnl = 0;
int32_t nfiles = 0;
int loadir;
n_buf_it_get_int8(nbufi, &dnl);
dn = n_buf_it_get(nbufi, dnl);
loadir = default_loadir;
if (dirs && n_array_bsearch(dirs, dn))
loadir = include;
#if 0
if (loadir)
printf("LOAD (%d) %s\n", include, dn);
#endif
dnl--;
n_buf_it_get_int32(nbufi, &nfiles);
if (loadir) {
flent = pkgfl_ent_new(na, dn, dnl, nfiles);
ents[n++] = flent;
}
for (j=0; j < nfiles; j++) {
char *bn, *linkto = NULL;
uint8_t bnl = 0, slen = 0;
uint16_t mode = 0;
uint32_t size = 0;
n_buf_it_get_int8(nbufi, &bnl);
bn = n_buf_it_get(nbufi, bnl);
n_buf_it_get_int16(nbufi, &mode);
n_buf_it_get_int32(nbufi, &size);
if (S_ISLNK(mode)) {
n_buf_it_get_int8(nbufi, &slen);
linkto = n_buf_it_get(nbufi, slen);
}
if (loadir) {
struct flfile *file;
file = flfile_new(na, size, mode, bn, bnl, linkto, slen);
flent->files[flent->items++] = file;
}
}
}
DBGF("n = %d\n", n);
if (n > 0)
*fl = n_tuple_new(na, n, (void **)ents);
return n;
}
int pkgfl_restore_st(tn_alloc *na, tn_tuple **fl,
tn_stream *st, tn_array *dirs, int include)
{
tn_buf *nbuf = NULL;
tn_buf_it nbufi;
int rc = 0;
*fl = NULL;
n_buf_restore(st, &nbuf, TN_BUF_STORE_32B);
if (nbuf == NULL)
return -1;
n_buf_it_init(&nbufi, nbuf);
rc = pkgfl_restore(na, fl, &nbufi, dirs, include);
n_buf_free(nbuf);
n_stream_seek(st, 1, SEEK_CUR); /* skip ending '\n' */
return rc;
}
int pkgfl_skip_st(tn_stream *st)
{
n_buf_restore_skip(st, TN_BUF_STORE_32B);
n_stream_seek(st, 1, SEEK_CUR); /* skip ending '\n' */
return 1;
}
void pkgfl_dump(tn_tuple *fl)
{
int i, j;
if (fl == NULL)
return;
for (i=0; i < n_tuple_size(fl); i++) {
struct pkgfl_ent *flent = n_tuple_nth(fl, i);
printf("DIR %s:", flent->dirname);
for (j=0; j<flent->items; j++) {
printf(" %s,", flent->files[j]->basename);
}
printf("\n");
}
return;
}
struct pkgfl_it *pkgfl_it_new(tn_tuple *fl)
{
struct pkgfl_it *it;
it = n_malloc(sizeof(*it));
pkgfl_it_init(it, fl);
return it;
}
void pkgfl_it_init(struct pkgfl_it *it, tn_tuple *fl)
{
memset(it, 0, sizeof(*it));
it->fl = fl;
it->flent = NULL;
it->i = it->j = 0;
it->endp = NULL;
it->path[0] = '\0';
if (fl)
it->flent = n_tuple_nth(fl, it->i++);
}
const char *pkgfl_it_get(struct pkgfl_it *it, struct flfile **flfile)
{
struct flfile *file;
int path_left_size;
if (it->flent == NULL)
return NULL;
if (it->j == it->flent->items) {
if (it->i == n_tuple_size(it->fl))
return NULL;
it->flent = n_tuple_nth(it->fl, it->i++);
it->j = 0;
it->endp = NULL;
}
if (it->endp == NULL) {
it->endp = it->path;
if (*it->flent->dirname != '/')
*it->endp++ = '/';
it->endp = n_strncpy(it->endp, it->flent->dirname,
sizeof(it->path) - 2);
if (*(it->endp - 1) != '/')
*it->endp++ = '/';
}
file = it->flent->files[it->j++];
path_left_size = sizeof(it->path) - (it->endp - it->path);
n_strncpy(it->endp, file->basename, path_left_size);
if (flfile)
*flfile = file;
return it->path;
}
/* to simplify python wrapper, i.e do not map flfile struct in it */
const char *pkgfl_it_get_rawargs(struct pkgfl_it *it, uint32_t *size, uint16_t *mode,
const char **basename)
{
const char *path;
struct flfile *file;
if ((path = pkgfl_it_get(it, &file))) {
*size = file->size;
*mode = file->mode;
*basename = file->basename;
}
return path;
}
int pkgfl_owned_and_required_dirs(tn_tuple *fl, tn_array **owned,
tn_array **required)
{
int i, j, n;
tn_array *od = NULL, *rd = NULL;
tn_hash *oh = n_hash_new(n_tuple_size(fl), free);
n_assert(owned);
for (i=0; i < n_tuple_size(fl); i++) {
struct pkgfl_ent *flent = n_tuple_nth(fl, i);
for (j=0; j < flent->items; j++) {
struct flfile *f = flent->files[j];
char dir[PATH_MAX];
if (S_ISDIR(f->mode)) {
if (*flent->dirname != '/')
n_snprintf(dir, sizeof(dir), "%s/%s", flent->dirname,
f->basename);
else if (*f->basename == '\0')
n_snprintf(dir, sizeof(dir), "%s", flent->dirname);
else
n_snprintf(dir, sizeof(dir), "%s", f->basename);
DBGF("dir (%s) (%s) -> %s\n", flent->dirname, f->basename, dir);
n_hash_insert(oh, dir, NULL);
}
}
}
n = 0;
od = n_hash_keys_cp(oh);
if (required) {
rd = n_array_clone(od);
for (i=0; i < n_tuple_size(fl); i++) {
struct pkgfl_ent *flent = n_tuple_nth(fl, i);
if (!n_hash_exists(oh, flent->dirname))
n_array_push(rd, n_strdup(flent->dirname));
}
if (n_array_size(rd) == 0)
n_array_cfree(&rd);
else
n += n_array_size(rd);
*required = rd;
}
n_hash_free(oh);
if (n_array_size(od) == 0)
n_array_cfree(&od);
else
n += n_array_size(od);
*owned = od;
#if DEVEL
if (od) {
for (i = 0; i < n_array_size(od); i++)
printf("O %s\n", n_array_nth(od, i));
}
if (rd) {
for (i = 0; i < n_array_size(rd); i++)
printf("R %s\n", n_array_nth(rd, i));
}
#endif
return n;
}