File: pkgcdb2.c

package info (click to toggle)
auto-apt 0.3.24
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 296 kB
  • sloc: ansic: 3,309; sh: 543; perl: 337; makefile: 47
file content (574 lines) | stat: -rw-r--r-- 12,554 bytes parent folder | download | duplicates (6)
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
/*-
 * pkgcdb2.c
 * Copyright (c) 2000 Fumitoshi UKAI <ukai@debian.or.jp>
 * GPL
 *
 */

static char pkgcdb2_rcsid[] __attribute__((unused)) = "$Id: pkgcdb2.c,v 1.4 2000/07/13 16:34:30 ukai Exp $";

#include "pkgcdb2.h"
#include "pathnode.h"
#include "strtab.h"
#include "mempool.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/file.h>
#include <fcntl.h>
#include <limits.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <time.h>

#ifdef DEBUG
PKGCDB_VARDEF int debug = 0;
#endif
PKGCDB_VARDEF int verbose = 0;
PKGCDB_VARDEF int quiet = 0;

typedef PathNodeTree PkgCDB;

#ifndef PKGCDB_AUTOAPT
PKGCDB_API PathNodeTree
pkgcdb_alloc()
{
    PathNodeTree pnt;
    StrTable st;
    st = strtab_alloc();
    if (st == NULL) {
	return NULL;
    }
    pnt = pathnode_alloc(st);
    return pnt;
}
#endif

#ifndef PKGCDB_AUTOAPT
PKGCDB_API void
pkgcdb_release(PathNodeTree pnt)
{
    StrTable st;
    assert(pnt != NULL);
    st = pathnode_strtab(pnt);
    assert(st != NULL);
    pathnode_release(pnt);
    strtab_release(st);
}
#endif

PKGCDB_API PathNodeTree
pkgcdb_load(char *dbfile, int str_margin, int pathnode_margin)
{
    int fd;
    char buf[8];
    PathNodeTree pnt;
    StrTable st;

    if (dbfile == NULL)
	return NULL;
    fd = open(dbfile, O_RDONLY);
    if (fd < 0) {
	return NULL;
    }
    if (read(fd, buf, sizeof(buf)) < 0) {
	return NULL;
    }
    if (strncmp(buf, PKGCDB_VERSION_TAG, sizeof(buf)) != 0) {
	MSG(("magic mismatch: %8s\n", buf));
	return NULL;
    }
    DPRINT(("fd=%d magic: %8s\n", fd, buf));
    st = strtab_restore(fd, str_margin);
    if (st == NULL) {
	MSG(("strtab read error\n"));
	close(fd);
	return NULL;
    }
    pnt = pathnode_restore(fd, st, pathnode_margin);
    if (pnt == NULL) {
	MSG(("pathnode read error\n"));
	close(fd);
	return NULL;
    }
    close(fd);
    return pnt;
}

#ifndef PKGCDB_AUTOAPT
PKGCDB_API int 
pkgcdb_save(char *dbfile, PathNodeTree pnt, int shrink)
{
    int fd;
    char buf[8];

    if (dbfile == NULL)
	return -1;
    if (pnt == NULL)
	return -1;
    fd = open(dbfile, O_WRONLY|O_TRUNC|O_CREAT, 0644);
    if (fd < 0)
	return fd;
    strncpy(buf, PKGCDB_VERSION_TAG, sizeof(buf));
    if (write(fd, buf, sizeof(buf)) < 0) {
	goto error;
    }
    if (strtab_dump(fd, pathnode_strtab(pnt), shrink) < 0) {
	goto error;
    }
    if (pathnode_dump(fd, pnt, shrink) < 0) {
	goto error;
    }
error:
    close(fd);
    return 0;
}
#endif


PKGCDB_API struct path_node *
pkgcdb_get(PathNodeTree pnt, char *file, char **matchfile, char **ext)
{
    char *p;
    char *filename;
    static char matchpath[PATH_MAX];
    char path[PATH_MAX];
    struct path_node *pn;
    struct path_node *match;

    DPRINT(("pkgcdb_get: %s\n", file));
    if (file[0] == '/') {
	file++;
    }
    filename = file;
    p = file;
    pn = pathnode_top(pnt);
    match = NULL;
    matchpath[0] = '\0';
    while (*p != '\0') {
	if (*p != '/' || p - file <= 1) {
	    p++;
	    continue;
	}
	p++;
	
	assert(p - file < PATH_MAX);
	strncpy(path, file, p - file);
	path[p - file] = '\0';
	pn = pathnode_retrieve(pnt, pn, path);
	if (pn == NULL) {
	    if (matchfile != NULL) {
		assert(file - filename + 2 < PATH_MAX);
		*matchfile = matchpath;
		matchpath[0] = '/';
		strncpy(matchpath + 1, filename, file - filename);
		matchpath[1 + file - filename] = '\0';
	    }
	    return match;
	}
	file = p;
	match = pn;
	assert(pn != NULL);
	assert(match != NULL);
	DPRINT(("match:%s[%s] rest>%s\n", 
		pathnode_pathname(pnt, pn), 
		pathnode_packagename(pnt, match), file));
    }
    DPRINT(("last?%s (%d)\n", file, p - file));
    if (p - file >= 1) {
	pn = pathnode_retrieve(pnt, pn, file);
	if (pn == NULL) {
	    if (matchfile != NULL) {
		assert(file - filename + 2 < PATH_MAX);
		*matchfile = matchpath;
		matchpath[0] = '/';
		strncpy(matchpath + 1, filename, file - filename);
		matchpath[1 + file - filename] = '\0';
	    }
	    return match;
	}
    }
    if (matchfile != NULL) {
	*matchfile = matchpath;
	matchpath[0] = '/';
	strcpy(matchpath + 1, filename);
    }
    return pn;
}

#ifndef PKGCDB_AUTOAPT
struct pathlist {
    struct pathlist *next;
    char *path;
    int pathlen;
    char *pkg;
    struct path_node *n;
} *defpathlist = NULL;

PKGCDB_API int
pkgcdb_path_list_init(PathNodeTree pnt, char *file)
{
    FILE *fp;
    char buf[PATH_MAX];
    char *p, *pkg;
    struct pathlist *pl, *plp;
    int n = 0;

    if (file == NULL)
	return -1;
    VMSG(("paths.list loading from %s...", file));
    fp = fopen(file, "r");
    if (fp == NULL) {
	PERROR(("fopen paths.list"));
	return -1;
    }
    while (fgets(buf, sizeof(buf), fp) != NULL) {
	if (buf[0] == '#')
	    continue;
	if (buf[strlen(buf)-1] == '\n')
	    buf[strlen(buf)-1] = '\0';
	for (p = buf; *p != '\0'; p++) {
	    if (isspace(*p)) {
		*p++ = '\0';
		break;
	    }
	}
	for (; *p != '\0'; p++) {
	    if (!isspace(*p)) {
		break;
	    }
	}
	pkg = p;
	if (*p == '\0') {
	    pkg = DEFAULT_PATH_PACKAGE;
	} else {
	    for (; *p != '\0'; p++) {
		if (isspace(*p)) {
		    *p = '\0';
		    break;
		}
	    }
	}
	pl = (struct pathlist *)malloc(sizeof(struct pathlist));
	if (pl == NULL)
	    return -1;
	memset(pl, 0, sizeof(struct pathlist));
	pl->path = strdup(buf);
	pl->pathlen = strlen(pl->path);
	pl->pkg = strdup(pkg);
	pl->n = pkgcdb_put(pnt, pl->path, pl->pkg, NULL);

	if (defpathlist == NULL || pl->pathlen > defpathlist->pathlen) {
	    pl->next = defpathlist;
	    defpathlist = pl;
	} else {
	    for (plp = defpathlist; 
		 plp != NULL && plp->next != NULL; 
		 plp = plp->next) {
		if (pl->pathlen > plp->next->pathlen) {
		    pl->next = plp->next;
		    plp->next = pl;
		    break;
		}
	    }
	    if (plp != NULL && plp->next == NULL) {
		plp->next = pl;
	    }
	}
	n++;
    }
    VMSG(("%d done\n", n));
#ifdef DEBUG
    if (debug) {
	for (plp = defpathlist; plp != NULL; plp = plp->next) {
	    printf("%s %s\n", plp->path, plp->pkg);
	}
    }
#endif
    return 0;
}

static int
path_list_local_init(PathNodeTree pnt)
{
    struct pathlist *pl, *npl;
    struct stat st;
    char buf[PATH_MAX+2];
    for (pl = defpathlist; pl != NULL; pl = pl->next) {
	if (pl->path[pl->pathlen-1] == '/') {
	    snprintf(buf, PATH_MAX, "/%s", pl->path);
	    if (stat(buf, &st) == 0
		&& S_ISDIR(st.st_mode)) {
		npl = (struct pathlist *)malloc(sizeof(struct pathlist));
		if (npl == NULL)
		    continue;
		memset(npl, 0, sizeof(struct pathlist));
		npl->path = strdup(pl->path);
		npl->path[pl->pathlen-1] = '\0';
		npl->pathlen = pl->pathlen -1;
		npl->pkg = pl->pkg;
		npl->n = pkgcdb_put(pnt, npl->path, npl->pkg, NULL);
		npl->next = pl->next;
		pl->next = npl;
		pl = npl;
	    }
	}
    }
    return 0;
}

static struct pathlist *
path_list_check(char *file)
{
    struct pathlist *pl;
    int len;
    DPRINT(("path_list check? file=%s\n", file));
    len = strlen(file);
    for (pl = defpathlist; pl != NULL; pl = pl->next) {
	if (pl->n != NULL) {
	    if (len >= pl->pathlen &&
		strncmp(file, pl->path, pl->pathlen) == 0) {
		DPRINT(("path list match[%s]:%s <=> %s", 
			pl->pkg, pl->path, file));
		return pl;
	    }
	}
    }
    return NULL;
}
#endif

#ifndef PKGCDB_AUTOAPT
static char *
next_package(char *p, char **np)
{
    int i;
    static char package[PATH_MAX];
    
    *np = NULL;
    if (p == NULL || *p == '\0')
	return NULL;
    for (i = 0; p[i] != '\0'; i++) {
	if (p[i] == ',')
	    break;
	package[i] = tolower(p[i]);
    }
    if (p[i] == ',')
	*np = &p[i+1];
    package[i] = '\0';
    return package;
}
#endif

#ifndef PKGCDB_AUTOAPT
PKGCDB_API struct path_node *
pkgcdb_put(PathNodeTree pnt, char *file, char *pkg, int *nent)
{
    char *package, *dups;
    char *p;
    char path[PATH_MAX];
    struct path_node *pn, *npn;
    pkg_id pkgid, npkgid;
    static int inited = 0;
    static pkg_id pkg_defs;
#ifndef PKGCDB_AUTOAPT
    static int local_inited = 0;
    struct pathlist *pl;
#endif

    assert(pnt != NULL);
    if (!inited) {
	inited = 1;
	pkg_defs = pkg_intern(pathnode_strtab(pnt), DEFAULT_PATH_PACKAGE);
    }

    DPRINT(("put: <%s><%s>\n", file, pkg));
    package = next_package(pkg, &dups);
    pkgid = pkg_intern(pathnode_strtab(pnt), package);
    if (strncmp(file, "./", 2) == 0) {
	file += 2;
    }
    pn = pathnode_top(pnt);
    while (*file != '\0' && *file == '/')
	file++;

#ifndef PKGCDB_AUTOAPT
    if (strcmp(file, ".") == 0) {
	if (!local_inited) {
	    /* local mode */
	    local_inited = 1;
	    MSG(("local file list mode\n"));
	    path_list_local_init(pnt);
	}
	return NULL;
    }
    /* skip common dirs if available, it increase much performance */
    pl = path_list_check(file);
    if (pl != NULL) {
	file += pl->pathlen;
	pn = pl->n;
	if (strcmp(pl->pkg, DEFAULT_PATH_PACKAGE) == 0) {
	    DPRINT((" start=<%s> %p(%s)", 
		    file, pn, pathnode_pathname(pnt, pn)));
	} else {
	    /* all dir/files under local,man are ignored */
	    DPRINT((" ignore=<%s> %p(%s)",
		    file, pn, pathnode_pathname(pnt, pn)));
	    return pn;
	}
    }
#endif

    p = file;
    DPRINT((" file? %s\n", p));
    while (*p != '\0') {
	if (*p != '/' || p - file <= 1) {
	    p++;
	    continue;
	}
	p++;
	assert(p - file < PATH_MAX);
	strncpy(path, file, p - file);
	path[p - file] = '\0';
	DPRINT(("insert=<%s> %s|", path, pkg));
	npn = pathnode_insert(pnt, pn, path, pkgid);
	assert(npn != NULL);
	npkgid = pathnode_package(pnt, npn);
	if (nent != NULL)
	    (*nent)++;
	if (dups) {
	    for (package = next_package(dups, &dups);
		 package != NULL;
		 package = next_package(dups, &dups)) {
		pkgid = pkg_intern(pathnode_strtab(pnt), package);
		pathnode_chain(pnt, npn, pkgid);
	    }
	}
	if (pkg_cmp(&pkgid, &pkg_defs) != 0 /* default path? */
	    && pkg_cmp(&npkgid, &pkgid) == 0) {
	    return npn;
	}
	file = p;
	pn = npn;
	DPRINT(("next=<%s> %p(%s)", file, pn, pathnode_pathname(pnt, pn)));
    }
    if (p - file >= 1) {
	DPRINT(("insert=<%s> %s|", file, pkg));
	pn = pathnode_insert(pnt, pn, file, pkgid);
	if (dups) {
	    for (package = next_package(dups, &dups);
		 package != NULL;
		 package = next_package(dups, &dups)) {
		pkgid = pkg_intern(pathnode_strtab(pnt), package);
		pathnode_chain(pnt, pn, pkgid);
	    }
	}
    }
    return pn;
}
#endif

#ifndef PKGCDB_AUTOAPT
PKGCDB_API void
pkgcdb_del(PathNodeTree pnt, char *file, char *pkg, int *nent)
{
    char *package;
    char *p;
    int i;
    char path[PATH_MAX];
    struct path_node *pn, *npn;
    pkg_id pkgid, npkgid;
    struct pathlist *pl;

    assert(pnt != NULL);
    DPRINT(("put: <%s><%s>\n", file, pkg));
    package = malloc(strlen(pkg)+1);
    for (i = 0; pkg[i] != '\0'; i++) {
	if (pkg[i] == ',')
	    break;
	package[i] = tolower(pkg[i]);
    }
    package[i] = '\0';
    pkgid = pkg_intern(pathnode_strtab(pnt), package);
    free(package);
    if (strncmp(file, "./", 2) == 0) {
	file += 2;
    }
    pn = pathnode_top(pnt);
    while (*file != '\0' && *file == '/')
	file++;

    pl = path_list_check(file);
    if (pl != NULL) {
	file += pl->pathlen;
	pn = pl->n;
	if (strcmp(pl->pkg, DEFAULT_PATH_PACKAGE) == 0) {
	    DPRINT((" start=<%s> %p(%s)", 
		    file, pn, pathnode_pathname(pnt, pn)));
	} else {
	    /* all dir/files under local,man are ignored */
	    DPRINT((" ignore=<%s> %p(%s)",
		    file, pn, pathnode_pathname(pnt, pn)));
	    return;
	}
    }

    p = file;
    while (*p != '\0') {
	if (*p != '/' || p - file <= 1) {
	    p++;
	    continue;
	}
	p++;
	assert(p - file < PATH_MAX);
	strncpy(path, file, p - file);
	path[p - file] = '\0';
	npn = pathnode_retrieve(pnt, pn, path);
	if (npn == NULL)
	    return;
	for (pn = npn; pn != NULL; pn = pathnode_next(pnt, pn)) {
	    npkgid = pathnode_package(pnt, pn);
	    if (pkg_cmp(&npkgid, &pkgid) == 0) {
		pathnode_delete(pnt, pn);
		if (nent)
		    (*nent)--;
	    }
	}
	file = p;
	pn = npn;
	DPRINT(("next=<%s> %p(%s)", file, pn, pathnode_pathname(pnt, pn)));
    }
    if (p - file >= 1) {
	pn = pathnode_retrieve(pnt, pn, path);
	if (pn == NULL)
	    return;
	for (; pn != NULL; pn = pathnode_next(pnt, pn)) {
	    npkgid = pathnode_package(pnt, pn);
	    if (pkg_cmp(&npkgid, &pkgid) == 0) {
		pathnode_delete(pnt, pn);
		if (nent)
		    (*nent)--;
	    }
	}
    }
    return;
}
#endif

#ifndef PKGCDB_AUTOAPT

PKGCDB_API void
pkgcdb_traverse(PathNodeTree pnt, 
		void (*func)(PathNodeTree pnt, 
			     char *path, struct path_node *pn, void *arg),
		void *arg)
{
    struct path_node *pn;
    pn = pathnode_top(pnt);
    pathnode_traverse(pnt, NULL, pn, func, arg);
}
#endif