Werner's Miscellanea
Sign in or create your account | Project List | Help
Werner's Miscellanea Git Source Tree
Root/
Source at commit b89bcdfd93a60417649340a18ca3bb573b1d9691 created 13 years 4 months ago. By Werner Almesberger, qpkg: also consider conflicts introduced by package about to be added | |
---|---|
1 | /* |
2 | * gobble.c - Read a package database file in a hurry |
3 | * |
4 | * Written 2010 by Werner Almesberger |
5 | * Copyright 2010 Werner Almesberger |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by |
9 | * the Free Software Foundation; either version 2 of the License, or |
10 | * (at your option) any later version. |
11 | */ |
12 | |
13 | |
14 | #include <stdlib.h> |
15 | #include <stdio.h> |
16 | #include <string.h> |
17 | #include <ctype.h> |
18 | #include <fcntl.h> |
19 | #include <sys/stat.h> |
20 | #include <sys/mman.h> |
21 | |
22 | #include "util.h" |
23 | #include "id.h" |
24 | #include "qpkg.h" |
25 | #include "gobble.h" |
26 | |
27 | |
28 | #define CHARS_AFTER_ERROR 20 |
29 | |
30 | |
31 | #ifdef BREAKNECK_SPEED |
32 | |
33 | #define EXPECT(s) do { buf += sizeof(s)-1; } while (0) |
34 | |
35 | #else /* !BREAKNECK_SPEED */ |
36 | |
37 | #define EXPECT(s) \ |
38 | do { \ |
39 | if (end-buf < sizeof(s)-1) \ |
40 | FAIL; \ |
41 | if (memcmp(buf, s, sizeof(s)-1)) \ |
42 | FAIL; \ |
43 | buf += sizeof(s)-1; \ |
44 | } \ |
45 | while (0) |
46 | |
47 | #endif |
48 | |
49 | |
50 | #define NEXT (buf == end ? '?' : *buf++) |
51 | |
52 | |
53 | #define WHITESPACE \ |
54 | do { \ |
55 | if (buf == end) \ |
56 | FAIL; \ |
57 | if (*buf == '\n') \ |
58 | break; \ |
59 | if (!isspace(*buf)) \ |
60 | break; \ |
61 | buf++; \ |
62 | } \ |
63 | while (0) |
64 | |
65 | |
66 | #define ISTERM(c) \ |
67 | ((c) == ' ' || (c) == '\t' || (c) == '\n' || \ |
68 | (c) == ',' || (c) == ')') |
69 | |
70 | |
71 | #define ID(tree) \ |
72 | ({ \ |
73 | const char *start; \ |
74 | \ |
75 | if (buf == end) \ |
76 | FAIL; \ |
77 | start = buf; \ |
78 | while (buf != end && !ISTERM(*buf)) \ |
79 | buf++; \ |
80 | make_id(tree, start, buf-start); \ |
81 | }) |
82 | |
83 | |
84 | #define FAIL \ |
85 | do { \ |
86 | failed_at = __LINE__; \ |
87 | goto fail; \ |
88 | } \ |
89 | while (0) |
90 | |
91 | |
92 | #define DONE goto done |
93 | |
94 | |
95 | static void finish_pkg(struct pkg *new, struct jrb *jrb) |
96 | { |
97 | struct pkg *old; |
98 | |
99 | if (!new->version) { |
100 | fprintf(stderr, "package %.*s has no version\n", |
101 | ID2PF(new->id)); |
102 | exit(1); |
103 | } |
104 | if (!new->arch) { |
105 | fprintf(stderr, |
106 | "package %.*s version %.*s has no architecture\n", |
107 | ID2PF(new->id), ID2PF(new->version)); |
108 | exit(1); |
109 | } |
110 | if (!new->filename && !(new->flags & QPKG_INSTALLED)) { |
111 | fprintf(stderr, |
112 | "package %.*s version %.*s has no file name " |
113 | "(nor is it installed)\n", |
114 | ID2PF(new->id), ID2PF(new->version)); |
115 | exit(1); |
116 | } |
117 | |
118 | for (old = new->more; old; old = old->more) |
119 | if (old->version == new->version) |
120 | goto compact; |
121 | return; |
122 | |
123 | compact: |
124 | jrb->val = new->more; |
125 | old->flags |= new->flags; |
126 | /* @@@ we may leak a little */ |
127 | free(new); |
128 | } |
129 | |
130 | |
131 | static void gobble_buf(const char *name, const char *buf, size_t len) |
132 | { |
133 | const char *end = buf+len; |
134 | int lineno = 1; |
135 | struct pkg *pkg = NULL; /* current package */ |
136 | struct jrb *jrb = NULL; /* RB tree node of current package */ |
137 | struct ref **anchor = NULL; |
138 | int i, failed_at = 0; |
139 | |
140 | initial: |
141 | if (buf == end) |
142 | DONE; |
143 | if (*buf == '\n') { |
144 | lineno++; |
145 | buf++; |
146 | goto initial; |
147 | } |
148 | |
149 | /* decode the tag */ |
150 | |
151 | switch (*buf++) { |
152 | case 'A': /* Architecture // Auto-Installed */ |
153 | switch (NEXT) { |
154 | case 'r': |
155 | EXPECT("chitecture:"); |
156 | goto architecture; |
157 | case 'u': |
158 | EXPECT("to-Installed:"); |
159 | goto skip_data; |
160 | default: |
161 | FAIL; |
162 | } |
163 | |
164 | case 'C': /* Conflicts // Conffiles */ |
165 | EXPECT("onf"); |
166 | switch (NEXT) { |
167 | case 'l': |
168 | EXPECT("icts:"); |
169 | goto conflicts; |
170 | case 'f': |
171 | EXPECT("iles:"); |
172 | goto skip_data; |
173 | default: |
174 | FAIL; |
175 | } |
176 | |
177 | case 'D': /* Depends, Description */ |
178 | EXPECT("e"); |
179 | switch (NEXT) { |
180 | case 'p': |
181 | EXPECT("ends:"); |
182 | goto depends; |
183 | case 's': |
184 | EXPECT("cription:"); |
185 | goto skip_data; |
186 | default: |
187 | FAIL; |
188 | } |
189 | |
190 | case 'F': /* Filename */ |
191 | EXPECT("ilename:"); |
192 | goto filename; |
193 | |
194 | case 'H': /* HomePage, Homepage */ |
195 | EXPECT("ome"); |
196 | switch (NEXT) { |
197 | case 'P': |
198 | case 'p': |
199 | EXPECT("age:"); |
200 | goto skip_data; |
201 | default: |
202 | FAIL; |
203 | } |
204 | |
205 | case 'I': /* Installed-Time */ |
206 | EXPECT("nstalled-Time:"); |
207 | goto skip_data; |
208 | |
209 | case 'L': /* License */ |
210 | EXPECT("icense:"); |
211 | goto skip_data; |
212 | |
213 | case 'M': /* Maintainer, MD5Sum */ |
214 | switch (NEXT) { |
215 | case 'a': |
216 | EXPECT("intainer:"); |
217 | goto skip_data; |
218 | case 'D': |
219 | EXPECT("5Sum:"); |
220 | goto skip_data; |
221 | default: |
222 | FAIL; |
223 | } |
224 | |
225 | case 'O': /* OE */ |
226 | EXPECT("E:"); |
227 | goto skip_data; |
228 | |
229 | case 'P': /* Package, Priority, Provides */ |
230 | switch (NEXT) { |
231 | case 'a': |
232 | EXPECT("ckage:"); |
233 | goto package; |
234 | case 'r': |
235 | break; |
236 | default: |
237 | FAIL; |
238 | } |
239 | switch (NEXT) { |
240 | case 'i': |
241 | EXPECT("ority:"); |
242 | goto skip_data; |
243 | case 'o': |
244 | EXPECT("vides:"); |
245 | goto provides; |
246 | default: |
247 | FAIL; |
248 | } |
249 | |
250 | case 'R': /* Recommends, Replaces */ |
251 | EXPECT("e"); |
252 | switch (NEXT) { |
253 | case 'c': |
254 | EXPECT("ommends:"); |
255 | goto skip_data; |
256 | case 'p': |
257 | EXPECT("laces:"); |
258 | goto skip_data; |
259 | default: |
260 | FAIL; |
261 | } |
262 | |
263 | case 'S': /* Section, Size, Source, Suggests // Status */ |
264 | switch (NEXT) { |
265 | case 'e': |
266 | EXPECT("ction:"); |
267 | goto skip_data; |
268 | case 'i': |
269 | EXPECT("ze:"); |
270 | goto skip_data; |
271 | case 'o': |
272 | EXPECT("urce:"); |
273 | goto skip_data; |
274 | case 'u': |
275 | EXPECT("ggests:"); |
276 | goto skip_data; |
277 | case 't': |
278 | EXPECT("atus:"); |
279 | goto status; |
280 | default: |
281 | FAIL; |
282 | } |
283 | |
284 | case 'V': /* Version */ |
285 | EXPECT("ersion:"); |
286 | goto version; |
287 | |
288 | default: |
289 | FAIL; |
290 | } |
291 | |
292 | conflicts: |
293 | anchor = &pkg->conflicts; |
294 | goto list_with_version; |
295 | |
296 | depends: |
297 | anchor = &pkg->depends; |
298 | goto list_with_version; |
299 | |
300 | package: |
301 | if (pkg) |
302 | finish_pkg(pkg, jrb); |
303 | |
304 | WHITESPACE; |
305 | pkg = alloc_type(struct pkg); |
306 | jrb = ID(packages); |
307 | pkg->id = jrb->key; |
308 | pkg->more = jrb->val; |
309 | jrb->val = pkg; |
310 | pkg->version = NULL; |
311 | pkg->arch = NULL; |
312 | pkg->conflicts = pkg->depends = NULL; |
313 | pkg->filename = NULL; |
314 | pkg->flags = 0; |
315 | pkg->mark = 0; |
316 | goto eol; |
317 | |
318 | version: |
319 | WHITESPACE; |
320 | if (pkg->version) |
321 | FAIL; |
322 | pkg->version = ID(versions)->key; |
323 | goto eol; |
324 | |
325 | architecture: |
326 | WHITESPACE; |
327 | if (pkg->arch) |
328 | FAIL; |
329 | pkg->arch = buf; |
330 | goto skip_data; |
331 | |
332 | provides: |
333 | /* @@@ later */ |
334 | goto skip_data; |
335 | |
336 | status: |
337 | pkg->flags |= QPKG_INSTALLED; |
338 | /* @@@ later */ |
339 | goto skip_data; |
340 | |
341 | filename: |
342 | WHITESPACE; |
343 | if (pkg->filename) |
344 | FAIL; |
345 | pkg->filename = buf; |
346 | goto skip_data; |
347 | |
348 | eol: |
349 | while (buf != end) { |
350 | if (*buf == ' ' || *buf == '\t') { |
351 | buf++; |
352 | continue; |
353 | } |
354 | if (*buf++ != '\n') |
355 | FAIL; |
356 | lineno++; |
357 | if (buf == end) |
358 | DONE; |
359 | if (*buf == ' ' || *buf == '\t') |
360 | FAIL; |
361 | goto initial; |
362 | } |
363 | DONE; |
364 | |
365 | skip_data: |
366 | while (buf != end) { |
367 | if (*buf++ != '\n') |
368 | continue; |
369 | lineno++; |
370 | if (buf == end) |
371 | DONE; |
372 | if (*buf != ' ' && *buf != '\t') |
373 | goto initial; |
374 | } |
375 | DONE; |
376 | |
377 | list_with_version: |
378 | while (1) { |
379 | struct ref *ref; |
380 | |
381 | WHITESPACE; |
382 | ref = alloc_type(struct ref); |
383 | ref->pkg = ID(packages)->key; |
384 | |
385 | /* |
386 | * Work around the Wireshark Anomaly |
387 | */ |
388 | if (buf != end && *buf == ')') |
389 | buf++; |
390 | |
391 | WHITESPACE; |
392 | if (buf == end || *buf != '(') |
393 | ref->version = NULL; |
394 | else { |
395 | buf++; |
396 | if (buf == end) |
397 | FAIL; |
398 | switch (*buf++) { |
399 | case '=': |
400 | ref->relop = rel_eq; |
401 | break; |
402 | case '<': |
403 | ref->relop = rel_lt; |
404 | break; |
405 | case '>': |
406 | EXPECT("="); |
407 | ref->relop = rel_ge; |
408 | break; |
409 | default: |
410 | FAIL; |
411 | } |
412 | WHITESPACE; |
413 | ref->version = ID(versions)->key; |
414 | EXPECT(")"); |
415 | } |
416 | *anchor = ref; |
417 | ref->next = NULL; |
418 | anchor = &ref->next; |
419 | if (buf == end) |
420 | DONE; |
421 | if (*buf != ',') |
422 | break; |
423 | buf++; |
424 | } |
425 | anchor = NULL; |
426 | goto eol; |
427 | |
428 | done: |
429 | if (pkg) |
430 | finish_pkg(pkg, jrb); |
431 | return; |
432 | |
433 | fail: |
434 | fprintf(stderr, "syntax derailment #%d at %s line %d: ", |
435 | failed_at, name, lineno); |
436 | for (i = 0; i != CHARS_AFTER_ERROR && buf != end; i++) { |
437 | if (*buf == '\n') |
438 | fprintf(stderr, "\\n"); |
439 | else if (isspace(*buf)) |
440 | fputc(' ', stderr); |
441 | else if (isprint(*buf)) |
442 | fputc(*buf, stderr); |
443 | buf++; |
444 | } |
445 | fprintf(stderr, "%s\n", buf == end ? "": "..."); |
446 | exit(1); |
447 | } |
448 | |
449 | |
450 | /* |
451 | * We should be able to test for __UCLIBC_HAS_ADVANCED_REALTIME__ specifically, |
452 | * but that doesn't work for some reason. So let's just omit posix_madvise on |
453 | * __UCLIBC__ in general. |
454 | */ |
455 | |
456 | |
457 | #if !defined(__UCLIBC__) |
458 | |
459 | static int do_madvise(void *addr, size_t len, int advice) |
460 | { |
461 | return posix_madvise(addr, len, advice); |
462 | } |
463 | |
464 | #else /* __UCLIBC__ */ |
465 | |
466 | #define do_madvise(addr, len, advice) 0 |
467 | |
468 | #endif /* __UCLIBC__ */ |
469 | |
470 | |
471 | void gobble(const char *name) |
472 | { |
473 | int fd; |
474 | struct stat st; |
475 | void *map; |
476 | |
477 | fd = open(name, O_RDONLY); |
478 | if (fd < 0) { |
479 | perror(name); |
480 | exit(1); |
481 | } |
482 | if (fstat(fd, &st) < 0) { |
483 | perror("fstat"); |
484 | exit(1); |
485 | } |
486 | map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
487 | if (map == MAP_FAILED) { |
488 | perror("mmap"); |
489 | exit(1); |
490 | } |
491 | if (do_madvise(map, st.st_size, POSIX_MADV_WILLNEED) < 0) { |
492 | perror("posix_madvise(POSIX_MADV_WILLNEED)"); |
493 | exit(1); |
494 | } |
495 | gobble_buf(name, map, st.st_size); |
496 | if (do_madvise(map, st.st_size, POSIX_MADV_RANDOM) < 0) { |
497 | perror("posix_madvise(POSIX_MADV_RANDOM)"); |
498 | exit(1); |
499 | } |
500 | } |
501 |
Branches:
master