po: Update German man pages translation
[dpkg.git] / lib / dpkg / parse.c
blobda99624291c327fa48956870d8f325c52b2e161c
1 /*
2 * libdpkg - Debian packaging suite library routines
3 * parse.c - database file parsing, main package/field loop
5 * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2006, 2008-2015 Guillem Jover <guillem@debian.org>
8 * This is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 #include <config.h>
23 #include <compat.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #ifdef USE_MMAP
28 #include <sys/mman.h>
29 #endif
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <stdarg.h>
36 #include <stdlib.h>
37 #include <stdio.h>
39 #include <dpkg/macros.h>
40 #include <dpkg/i18n.h>
41 #include <dpkg/c-ctype.h>
42 #include <dpkg/dpkg.h>
43 #include <dpkg/dpkg-db.h>
44 #include <dpkg/string.h>
45 #include <dpkg/pkg.h>
46 #include <dpkg/parsedump.h>
47 #include <dpkg/fdio.h>
48 #include <dpkg/buffer.h>
50 /**
51 * Fields information.
53 const struct fieldinfo fieldinfos[]= {
54 /* Note: Capitalization of field name strings is important. */
55 { FIELD("Package"), f_name, w_name },
56 { FIELD("Essential"), f_boolean, w_booleandefno, PKGIFPOFF(essential) },
57 { FIELD("Protected"), f_boolean, w_booleandefno, PKGIFPOFF(is_protected) },
58 { FIELD("Status"), f_status, w_status },
59 { FIELD("Priority"), f_priority, w_priority },
60 { FIELD("Section"), f_section, w_section },
61 { FIELD("Installed-Size"), f_charfield, w_charfield, PKGIFPOFF(installedsize) },
62 { FIELD("Origin"), f_charfield, w_charfield, PKGIFPOFF(origin) },
63 { FIELD("Maintainer"), f_charfield, w_charfield, PKGIFPOFF(maintainer) },
64 { FIELD("Bugs"), f_charfield, w_charfield, PKGIFPOFF(bugs) },
65 { FIELD("Architecture"), f_architecture, w_architecture },
66 { FIELD("Multi-Arch"), f_multiarch, w_multiarch, PKGIFPOFF(multiarch) },
67 { FIELD("Source"), f_charfield, w_charfield, PKGIFPOFF(source) },
68 { FIELD("Version"), f_version, w_version, PKGIFPOFF(version) },
69 { FIELD("Config-Version"), f_configversion, w_configversion },
70 { FIELD("Replaces"), f_dependency, w_dependency, dep_replaces },
71 { FIELD("Provides"), f_dependency, w_dependency, dep_provides },
72 { FIELD("Depends"), f_dependency, w_dependency, dep_depends },
73 { FIELD("Pre-Depends"), f_dependency, w_dependency, dep_predepends },
74 { FIELD("Recommends"), f_dependency, w_dependency, dep_recommends },
75 { FIELD("Suggests"), f_dependency, w_dependency, dep_suggests },
76 { FIELD("Breaks"), f_dependency, w_dependency, dep_breaks },
77 { FIELD("Conflicts"), f_dependency, w_dependency, dep_conflicts },
78 { FIELD("Enhances"), f_dependency, w_dependency, dep_enhances },
79 { FIELD("Conffiles"), f_conffiles, w_conffiles },
80 { FIELD("Filename"), f_archives, w_archives, ARCHIVEFOFF(name) },
81 { FIELD("Size"), f_archives, w_archives, ARCHIVEFOFF(size) },
82 { FIELD("MD5sum"), f_archives, w_archives, ARCHIVEFOFF(md5sum) },
83 { FIELD("MSDOS-Filename"), f_archives, w_archives, ARCHIVEFOFF(msdosname) },
84 { FIELD("Description"), f_charfield, w_charfield, PKGIFPOFF(description) },
85 { FIELD("Triggers-Pending"), f_trigpend, w_trigpend },
86 { FIELD("Triggers-Awaited"), f_trigaw, w_trigaw },
88 /* The following are the obsolete fields that get remapped to their
89 * modern forms, while emitting an obsolescence warning. */
90 { FIELD("Recommended"), f_obs_dependency, w_null, dep_recommends },
91 { FIELD("Optional"), f_obs_dependency, w_null, dep_suggests },
92 { FIELD("Class"), f_obs_class, w_null },
93 { FIELD("Revision"), f_obs_revision, w_null },
94 { FIELD("Package-Revision"), f_obs_revision, w_null },
95 { FIELD("Package_Revision"), f_obs_revision, w_null },
96 { NULL }
99 /**
100 * Package object being parsed.
102 * Structure used to hold the parsed data for the package being constructed,
103 * before it gets properly inserted into the package database.
105 struct pkg_parse_object {
106 struct pkginfo *pkg;
107 struct pkgbin *pkgbin;
111 * Parse the field and value into the package being constructed.
113 static void
114 pkg_parse_field(struct parsedb_state *ps, struct field_state *fs,
115 void *parse_obj)
117 struct pkg_parse_object *pkg_obj = parse_obj;
118 const struct fieldinfo *fip;
119 int *ip;
121 for (fip = fieldinfos, ip = fs->fieldencountered; fip->name; fip++, ip++)
122 if (fip->namelen == (size_t)fs->fieldlen &&
123 strncasecmp(fip->name, fs->fieldstart, fs->fieldlen) == 0)
124 break;
125 if (fip->name) {
126 if ((*ip)++)
127 parse_error(ps,
128 _("duplicate value for '%s' field"), fip->name);
130 varbuf_set_buf(&fs->value, fs->valuestart, fs->valuelen);
132 fip->rcall(pkg_obj->pkg, pkg_obj->pkgbin, ps, fs->value.buf, fip);
133 } else {
134 struct arbitraryfield *arp, **larpp;
136 if (fs->fieldlen < 2)
137 parse_error(ps,
138 _("user-defined field name '%.*s' too short"),
139 fs->fieldlen, fs->fieldstart);
140 larpp = &pkg_obj->pkgbin->arbs;
141 while ((arp = *larpp) != NULL) {
142 if (strncasecmp(arp->name, fs->fieldstart, fs->fieldlen) == 0 &&
143 strlen(arp->name) == (size_t)fs->fieldlen)
144 parse_error(ps,
145 _("duplicate value for user-defined field '%.*s'"),
146 fs->fieldlen, fs->fieldstart);
147 larpp = &arp->next;
149 arp = nfmalloc(sizeof(*arp));
150 arp->name = nfstrnsave(fs->fieldstart, fs->fieldlen);
151 arp->value = nfstrnsave(fs->valuestart, fs->valuelen);
152 arp->next = NULL;
153 *larpp = arp;
158 * Verify and fixup the package structure being constructed.
160 static void
161 pkg_parse_verify(struct parsedb_state *ps,
162 struct pkginfo *pkg, struct pkgbin *pkgbin)
164 struct dependency *dep;
165 struct deppossi *dop;
167 parse_must_have_field(ps, pkg->set->name, "Package");
169 /* XXX: We need to check for status != PKG_STAT_HALFINSTALLED as while
170 * unpacking an unselected package, it will not have yet all data in
171 * place. But we cannot check for > PKG_STAT_HALFINSTALLED as
172 * PKG_STAT_CONFIGFILES always should have those fields. */
173 if ((ps->flags & pdb_recordavailable) ||
174 (pkg->status != PKG_STAT_NOTINSTALLED &&
175 pkg->status != PKG_STAT_HALFINSTALLED)) {
176 parse_ensure_have_field(ps, &pkgbin->description, "Description");
177 parse_ensure_have_field(ps, &pkgbin->maintainer, "Maintainer");
178 parse_must_have_field(ps, pkgbin->version.version, "Version");
181 /* XXX: Versions before dpkg 1.10.19 did not preserve the Architecture
182 * field in the status file. So there's still live systems with packages
183 * in PKG_STAT_CONFIGFILES, ignore those too for now. */
184 if ((ps->flags & pdb_recordavailable) ||
185 pkg->status > PKG_STAT_HALFINSTALLED) {
186 /* We always want usable architecture information (as long as the package
187 * is in such a state that it makes sense), so that it can be used safely
188 * on string comparisons and the like. */
189 if (pkgbin->arch->type == DPKG_ARCH_NONE)
190 parse_warn(ps, _("missing '%s' field"), "Architecture");
191 else if (pkgbin->arch->type == DPKG_ARCH_EMPTY)
192 parse_warn(ps, _("empty value for '%s' field"), "Architecture");
194 /* Mark missing architectures as empty, to distinguish these from
195 * unused slots in the db. */
196 if (pkgbin->arch->type == DPKG_ARCH_NONE)
197 pkgbin->arch = dpkg_arch_get(DPKG_ARCH_EMPTY);
199 if (pkgbin->arch->type == DPKG_ARCH_EMPTY &&
200 pkgbin->multiarch == PKG_MULTIARCH_SAME)
201 parse_error(ps, _("package has '%s' field but is missing architecture"),
202 "Multi-Arch: same");
203 if (pkgbin->arch->type == DPKG_ARCH_ALL &&
204 pkgbin->multiarch == PKG_MULTIARCH_SAME)
205 parse_error(ps, _("package has '%s' field but is architecture '%s'"),
206 "Multi-Arch: same", "all");
208 /* Generate the cached fully qualified package name representation. */
209 pkgbin->pkgname_archqual = pkgbin_name_archqual(pkg, pkgbin);
211 /* Initialize deps to be arch-specific unless stated otherwise. */
212 for (dep = pkgbin->depends; dep; dep = dep->next)
213 for (dop = dep->list; dop; dop = dop->next)
214 if (!dop->arch)
215 dop->arch = pkgbin->arch;
218 * Check the Config-Version information:
220 * If there is a Config-Version it is definitely to be used, but there
221 * should not be one if the package is ‘installed’ or ‘triggers-pending’
222 * (in which case the Version will be copied) or if the package is
223 * ‘not-installed’ (in which case there is no Config-Version).
225 if (!(ps->flags & pdb_recordavailable)) {
226 if (pkg->configversion.version) {
227 if (pkg->status == PKG_STAT_INSTALLED ||
228 pkg->status == PKG_STAT_NOTINSTALLED ||
229 pkg->status == PKG_STAT_TRIGGERSPENDING)
230 parse_error(ps,
231 _("'%s' field present for package with inappropriate '%s' field"),
232 "Config-Version", "Status");
233 } else {
234 if (pkg->status == PKG_STAT_INSTALLED ||
235 pkg->status == PKG_STAT_TRIGGERSPENDING)
236 pkg->configversion = pkgbin->version;
240 if (pkg->trigaw.head &&
241 (pkg->status <= PKG_STAT_CONFIGFILES ||
242 pkg->status >= PKG_STAT_TRIGGERSPENDING))
243 parse_error(ps,
244 _("package has status %s but triggers are awaited"),
245 pkg_status_name(pkg));
246 else if (pkg->status == PKG_STAT_TRIGGERSAWAITED && !pkg->trigaw.head)
247 parse_error(ps,
248 _("package has status %s but no triggers awaited"),
249 pkg_status_name(pkg));
251 if (pkg->trigpend_head &&
252 !(pkg->status == PKG_STAT_TRIGGERSPENDING ||
253 pkg->status == PKG_STAT_TRIGGERSAWAITED))
254 parse_error(ps,
255 _("package has status %s but triggers are pending"),
256 pkg_status_name(pkg));
257 else if (pkg->status == PKG_STAT_TRIGGERSPENDING && !pkg->trigpend_head)
258 parse_error(ps,
259 _("package has status %s but no triggers pending"),
260 pkg_status_name(pkg));
262 /* Note: There was a bug that could make a not-installed package have
263 * conffiles, so we check for them here and remove them (rather than
264 * calling it an error, which will do at some point). */
265 if (!(ps->flags & pdb_recordavailable) &&
266 pkg->status == PKG_STAT_NOTINSTALLED &&
267 pkgbin->conffiles) {
268 parse_warn(ps,
269 _("package has status %s and has conffiles, forgetting them"),
270 pkg_status_name(pkg));
271 pkgbin->conffiles = NULL;
274 /* Note: Mark not-installed leftover packages for automatic removal on
275 * next database dump. */
276 if (!(ps->flags & pdb_recordavailable) &&
277 pkg->status == PKG_STAT_NOTINSTALLED &&
278 pkg->eflag == PKG_EFLAG_OK &&
279 (pkg->want == PKG_WANT_PURGE ||
280 pkg->want == PKG_WANT_DEINSTALL)) {
281 pkg_set_want(pkg, PKG_WANT_UNKNOWN);
284 /* Note: Mark not-installed non-arch-qualified selections for automatic
285 * removal, as they do not make sense in a multiarch enabled world, and
286 * might cause those selections to be unreferencable from command-line
287 * interfaces when there's other more specific selections. */
288 if (ps->type == pdb_file_status &&
289 pkg->status == PKG_STAT_NOTINSTALLED &&
290 pkg->eflag == PKG_EFLAG_OK &&
291 pkg->want == PKG_WANT_INSTALL &&
292 pkgbin->arch->type == DPKG_ARCH_EMPTY)
293 pkg_set_want(pkg, PKG_WANT_UNKNOWN);
295 /* Note: Versions before dpkg 1.13.10 did not blank the Origin and Bugs
296 * fields, so there can be packages that should be garbage collected but
297 * are lingering around. Blank them to make sure we will forget all about
298 * them on the next database dump. */
299 if (!(ps->flags & pdb_recordavailable) &&
300 pkg->status == PKG_STAT_NOTINSTALLED &&
301 pkg->eflag == PKG_EFLAG_OK &&
302 pkg->want == PKG_WANT_UNKNOWN) {
303 pkgbin_blank(pkgbin);
307 struct pkgcount {
308 int single;
309 int multi;
310 int total;
313 static void
314 parse_count_pkg_instance(struct pkgcount *count,
315 struct pkginfo *pkg, struct pkgbin *pkgbin)
317 if (pkg->status == PKG_STAT_NOTINSTALLED)
318 return;
320 if (pkgbin->multiarch == PKG_MULTIARCH_SAME)
321 count->multi++;
322 else
323 count->single++;
325 count->total++;
329 * Lookup the package set slot for the parsed package.
331 * Perform various checks, to make sure the database is always in a sane
332 * state, and to not allow breaking it.
334 static struct pkgset *
335 parse_find_set_slot(struct parsedb_state *ps,
336 struct pkginfo *new_pkg, struct pkgbin *new_pkgbin)
338 struct pkgcount count = { .single = 0, .multi = 0, .total = 0 };
339 struct pkgset *set;
340 struct pkginfo *pkg;
342 set = pkg_hash_find_set(new_pkg->set->name);
344 /* Sanity checks: verify that the db is in a consistent state. */
346 if (ps->type == pdb_file_status)
347 parse_count_pkg_instance(&count, new_pkg, new_pkgbin);
349 count.total = 0;
351 for (pkg = &set->pkg; pkg; pkg = pkg->arch_next)
352 parse_count_pkg_instance(&count, pkg, &pkg->installed);
354 if (count.single > 1)
355 parse_error(ps, _("multiple non-coinstallable package instances present; "
356 "most probably due to an upgrade from an unofficial dpkg"));
358 if (count.single > 0 && count.multi > 0)
359 parse_error(ps, _("mixed non-coinstallable and coinstallable package "
360 "instances present; most probably due to an upgrade "
361 "from an unofficial dpkg"));
363 if (pkgset_installed_instances(set) != count.total)
364 internerr("in-core pkgset '%s' with inconsistent number of instances",
365 set->name);
367 return set;
371 * Lookup the package slot for the parsed package.
373 * Cross-grading (i.e. switching arch) is only possible when parsing an
374 * update entry or when installing a new package.
376 * Most of the time each pkginfo in a pkgset has the same architecture for
377 * both the installed and available pkgbin members. But when cross-grading
378 * there's going to be a temporary discrepancy, because we reuse the single
379 * instance and fill the available pkgbin with the candidate pkgbin, until
380 * that is copied over the installed pkgbin.
382 * If there's 0 or > 1 package instances, then we match against the pkginfo
383 * slot architecture, because cross-grading is just not possible.
385 * If there's 1 instance, we are cross-grading and both installed and
386 * candidate are not PKG_MULTIARCH_SAME, we have to reuse the existing single
387 * slot regardless of the arch differing between the two. If we are not
388 * cross-grading, then we use the entry with the matching arch.
390 static struct pkginfo *
391 parse_find_pkg_slot(struct parsedb_state *ps,
392 struct pkginfo *new_pkg, struct pkgbin *new_pkgbin)
394 struct pkgset *db_set;
396 db_set = parse_find_set_slot(ps, new_pkg, new_pkgbin);
398 if (ps->type == pdb_file_available) {
399 /* If there's a single package installed and the new package is not
400 * “Multi-Arch: same”, then we preserve the previous behavior of
401 * possible architecture switch, for example from native to all. */
402 if (pkgset_installed_instances(db_set) == 1 &&
403 new_pkgbin->multiarch != PKG_MULTIARCH_SAME)
404 return pkg_hash_get_singleton(db_set);
405 else
406 return pkg_hash_get_pkg(db_set, new_pkgbin->arch);
407 } else {
408 bool selection = false;
410 /* If the package is part of the status file, and it's not installed
411 * then this means it's just a selection. */
412 if (ps->type == pdb_file_status && new_pkg->status == PKG_STAT_NOTINSTALLED)
413 selection = true;
415 /* Verify we don't allow something that will mess up the db. */
416 if (pkgset_installed_instances(db_set) > 1 &&
417 !selection && new_pkgbin->multiarch != PKG_MULTIARCH_SAME)
418 ohshit(_("package %s (%s) with field '%s: %s' is not co-installable "
419 "with %s which has multiple installed instances"),
420 pkgbin_name(new_pkg, new_pkgbin, pnaw_always),
421 versiondescribe(&new_pkgbin->version, vdew_nonambig),
422 "Multi-Arch", multiarchinfos[new_pkgbin->multiarch].name,
423 db_set->name);
425 /* If we are parsing the status file, use a slot per arch. */
426 if (ps->type == pdb_file_status)
427 return pkg_hash_get_pkg(db_set, new_pkgbin->arch);
429 /* If we are doing an update, from the log or a new package, then
430 * handle cross-grades. */
431 if (pkgset_installed_instances(db_set) == 1) {
432 struct pkginfo *db_pkg;
434 db_pkg = pkg_hash_get_singleton(db_set);
436 if (db_pkg->installed.multiarch == PKG_MULTIARCH_SAME &&
437 new_pkgbin->multiarch == PKG_MULTIARCH_SAME)
438 return pkg_hash_get_pkg(db_set, new_pkgbin->arch);
439 else
440 return db_pkg;
441 } else {
442 return pkg_hash_get_pkg(db_set, new_pkgbin->arch);
448 * Copy into the in-core database the package being constructed.
450 static void
451 pkg_parse_copy(struct parsedb_state *ps,
452 struct pkginfo *dst_pkg, struct pkgbin *dst_pkgbin,
453 struct pkginfo *src_pkg, struct pkgbin *src_pkgbin)
455 /* Copy the priority and section across, but don't overwrite existing
456 * values if the pdb_weakclassification flag is set. */
457 if (str_is_set(src_pkg->section) &&
458 !((ps->flags & pdb_weakclassification) &&
459 str_is_set(dst_pkg->section)))
460 dst_pkg->section = src_pkg->section;
461 if (src_pkg->priority != PKG_PRIO_UNKNOWN &&
462 !((ps->flags & pdb_weakclassification) &&
463 dst_pkg->priority != PKG_PRIO_UNKNOWN)) {
464 dst_pkg->priority = src_pkg->priority;
465 if (src_pkg->priority == PKG_PRIO_OTHER)
466 dst_pkg->otherpriority = src_pkg->otherpriority;
469 /* Sort out the dependency mess. */
470 copy_dependency_links(dst_pkg, &dst_pkgbin->depends, src_pkgbin->depends,
471 (ps->flags & pdb_recordavailable) ? true : false);
473 /* Copy across data. */
474 memcpy(dst_pkgbin, src_pkgbin, sizeof(struct pkgbin));
475 if (!(ps->flags & pdb_recordavailable)) {
476 struct trigaw *ta;
478 pkg_set_want(dst_pkg, src_pkg->want);
479 pkg_copy_eflags(dst_pkg, src_pkg);
480 pkg_set_status(dst_pkg, src_pkg->status);
481 dst_pkg->configversion = src_pkg->configversion;
482 dst_pkg->archives = NULL;
484 dst_pkg->trigpend_head = src_pkg->trigpend_head;
485 dst_pkg->trigaw = src_pkg->trigaw;
486 for (ta = dst_pkg->trigaw.head; ta; ta = ta->sameaw.next) {
487 if (ta->aw != src_pkg)
488 internerr("trigger awaited package %s and origin package %s not linked properly",
489 pkg_name(ta->aw, pnaw_always),
490 pkgbin_name(src_pkg, src_pkgbin, pnaw_always));
491 ta->aw = dst_pkg;
492 /* ->othertrigaw_head is updated by trig_note_aw in *(pkg_hash_find())
493 * rather than in dst_pkg. */
495 } else if (!(ps->flags & pdb_ignore_archives)) {
496 dst_pkg->archives = src_pkg->archives;
501 * Return a descriptive parser type.
503 static enum parsedbtype
504 parse_get_type(struct parsedb_state *ps, enum parsedbflags flags)
506 if (flags & pdb_recordavailable) {
507 if (flags & pdb_single_stanza)
508 return pdb_file_control;
509 else
510 return pdb_file_available;
511 } else {
512 if (flags & pdb_single_stanza)
513 return pdb_file_update;
514 else
515 return pdb_file_status;
520 * Create a new deb822 parser context.
522 struct parsedb_state *
523 parsedb_new(const char *filename, int fd, enum parsedbflags flags)
525 struct parsedb_state *ps;
527 ps = m_malloc(sizeof(*ps));
528 ps->err = DPKG_ERROR_OBJECT;
529 ps->errmsg = VARBUF_OBJECT;
530 ps->filename = filename;
531 ps->type = parse_get_type(ps, flags);
532 ps->flags = flags;
533 ps->fd = fd;
534 ps->lno = 0;
535 ps->data = NULL;
536 ps->dataptr = NULL;
537 ps->endptr = NULL;
538 ps->pkg = NULL;
539 ps->pkgbin = NULL;
541 return ps;
545 * Open a file for deb822 parsing.
547 struct parsedb_state *
548 parsedb_open(const char *filename, enum parsedbflags flags)
550 struct parsedb_state *ps;
551 int fd;
553 /* Special case stdin handling. */
554 if (flags & pdb_dash_is_stdin && strcmp(filename, "-") == 0)
555 return parsedb_new(filename, STDIN_FILENO, flags);
557 fd = open(filename, O_RDONLY);
558 if (fd < 0 && !(errno == ENOENT && (flags & pdb_allow_empty)))
559 ohshite(_("failed to open package info file '%.255s' for reading"),
560 filename);
562 ps = parsedb_new(filename, fd, flags | pdb_close_fd);
564 push_cleanup(cu_closefd, ~ehflag_normaltidy, 1, &ps->fd);
566 return ps;
570 * Load data for package deb822 style parsing.
572 void
573 parsedb_load(struct parsedb_state *ps)
575 struct stat st;
577 if (ps->fd < 0 && (ps->flags & pdb_allow_empty))
578 return;
580 if (fstat(ps->fd, &st) < 0)
581 ohshite(_("can't stat package info file '%.255s'"), ps->filename);
583 if (S_ISFIFO(st.st_mode)) {
584 struct varbuf buf = VARBUF_INIT;
585 struct dpkg_error err;
586 off_t size;
588 size = fd_vbuf_copy(ps->fd, &buf, -1, &err);
589 if (size < 0)
590 ohshit(_("reading package info file '%s': %s"), ps->filename, err.str);
593 ps->dataptr = varbuf_detach(&buf);
594 ps->endptr = ps->dataptr + size;
595 } else if (st.st_size > 0) {
596 #ifdef USE_MMAP
597 ps->dataptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, ps->fd, 0);
598 if (ps->dataptr == MAP_FAILED)
599 ohshite(_("can't mmap package info file '%.255s'"), ps->filename);
600 #else
601 ps->dataptr = m_malloc(st.st_size);
603 if (fd_read(ps->fd, ps->dataptr, st.st_size) < 0)
604 ohshite(_("reading package info file '%.255s'"), ps->filename);
605 #endif
606 ps->endptr = ps->dataptr + st.st_size;
607 } else {
608 ps->dataptr = ps->endptr = NULL;
610 ps->data = ps->dataptr;
614 * Parse an RFC-822 style stanza.
616 bool
617 parse_stanza(struct parsedb_state *ps, struct field_state *fs,
618 parse_field_func *parse_field, void *parse_obj)
620 int c = '\0';
622 /* Skip adjacent new lines. */
623 while (!parse_at_eof(ps)) {
624 c = parse_getc(ps);
625 if (c != '\n' && c != MSDOS_EOF_CHAR)
626 break;
627 ps->lno++;
630 /* Nothing relevant parsed, bail out. */
631 if (parse_at_eof(ps))
632 return false;
634 /* Loop per field. */
635 for (;;) {
636 bool blank_line;
638 /* Scan field name. */
639 fs->fieldstart = ps->dataptr - 1;
640 while (!parse_at_eof(ps) && !c_isspace(c) && c != ':' && c != MSDOS_EOF_CHAR)
641 c = parse_getc(ps);
642 fs->fieldlen = ps->dataptr - fs->fieldstart - 1;
643 if (fs->fieldlen == 0)
644 parse_error(ps, _("empty field name"));
645 if (fs->fieldstart[0] == '-')
646 parse_error(ps, _("field name '%.*s' cannot start with hyphen"),
647 fs->fieldlen, fs->fieldstart);
649 /* Skip spaces before ‘:’. */
650 while (!parse_at_eof(ps) && c != '\n' && c_isspace(c))
651 c = parse_getc(ps);
653 /* Validate ‘:’. */
654 if (parse_at_eof(ps))
655 parse_error(ps, _("end of file after field name '%.*s'"),
656 fs->fieldlen, fs->fieldstart);
657 if (c == '\n')
658 parse_error(ps,
659 _("newline in field name '%.*s'"), fs->fieldlen, fs->fieldstart);
660 if (c == MSDOS_EOF_CHAR)
661 parse_error(ps, _("MSDOS end of file (^Z) in field name '%.*s'"),
662 fs->fieldlen, fs->fieldstart);
663 if (c != ':')
664 parse_error(ps,
665 _("field name '%.*s' must be followed by colon"),
666 fs->fieldlen, fs->fieldstart);
668 /* Skip space after ‘:’ but before value and EOL. */
669 while (!parse_at_eof(ps)) {
670 c = parse_getc(ps);
671 if (c == '\n' || !c_isspace(c))
672 break;
674 if (parse_at_eof(ps))
675 parse_error(ps, _("end of file before value of field '%.*s' (missing final newline)"),
676 fs->fieldlen, fs->fieldstart);
677 if (c == MSDOS_EOF_CHAR)
678 parse_error(ps, _("MSDOS end of file (^Z) in value of field '%.*s' (missing newline?)"),
679 fs->fieldlen, fs->fieldstart);
681 blank_line = false;
683 /* Scan field value. */
684 fs->valuestart = ps->dataptr - 1;
685 for (;;) {
686 if (c == '\n' || c == MSDOS_EOF_CHAR) {
687 if (blank_line)
688 parse_lax_problem(ps, pdb_lax_stanza_parser,
689 _("blank line in value of field '%.*s'"),
690 fs->fieldlen, fs->fieldstart);
691 ps->lno++;
693 if (parse_at_eof(ps))
694 break;
695 c = parse_getc(ps);
697 /* Found double EOL, or start of new field. */
698 if (parse_at_eof(ps) || c == '\n' || !c_isspace(c))
699 break;
701 parse_ungetc(c, ps);
702 blank_line = true;
703 } else if (blank_line && !c_isspace(c)) {
704 blank_line = false;
707 if (parse_at_eof(ps))
708 parse_error(ps, _("end of file during value of field '%.*s' (missing final newline)"),
709 fs->fieldlen, fs->fieldstart);
711 c = parse_getc(ps);
713 fs->valuelen = ps->dataptr - fs->valuestart - 1;
715 /* Trim ending space on value. */
716 while (fs->valuelen && c_isspace(*(fs->valuestart + fs->valuelen - 1)))
717 fs->valuelen--;
719 parse_field(ps, fs, parse_obj);
721 if (parse_at_eof(ps) || c == '\n' || c == MSDOS_EOF_CHAR)
722 break;
723 } /* Loop per field. */
725 if (c == '\n')
726 ps->lno++;
728 return true;
732 * Teardown a package deb822 parser context.
734 void
735 parsedb_close(struct parsedb_state *ps)
737 if (ps->flags & pdb_close_fd) {
738 pop_cleanup(ehflag_normaltidy);
740 if (ps->fd >= 0 && close(ps->fd) < 0)
741 ohshite(_("failed to close after read: '%.255s'"), ps->filename);
744 if (ps->data != NULL) {
745 #ifdef USE_MMAP
746 munmap(ps->data, ps->endptr - ps->data);
747 #else
748 free(ps->data);
749 #endif
751 dpkg_error_destroy(&ps->err);
752 varbuf_destroy(&ps->errmsg);
753 free(ps);
757 * Parse deb822 style package data from a buffer.
759 * donep may be NULL.
760 * If donep is not NULL only one package's information is expected.
763 parsedb_parse(struct parsedb_state *ps, struct pkginfo **donep)
765 struct pkgset tmp_set;
766 struct pkginfo *new_pkg, *db_pkg;
767 struct pkgbin *new_pkgbin, *db_pkgbin;
768 struct pkg_parse_object pkg_obj;
769 int fieldencountered[array_count(fieldinfos)];
770 int pdone;
771 struct field_state fs;
773 if (ps->data == NULL && (ps->flags & pdb_allow_empty))
774 return 0;
776 memset(&fs, 0, sizeof(fs));
777 fs.fieldencountered = fieldencountered;
779 new_pkg = &tmp_set.pkg;
780 if (ps->flags & pdb_recordavailable)
781 new_pkgbin = &new_pkg->available;
782 else
783 new_pkgbin = &new_pkg->installed;
785 ps->pkg = new_pkg;
786 ps->pkgbin = new_pkgbin;
788 pkg_obj.pkg = new_pkg;
789 pkg_obj.pkgbin = new_pkgbin;
791 pdone= 0;
793 /* Loop per package. */
794 for (;;) {
795 memset(fieldencountered, 0, sizeof(fieldencountered));
796 pkgset_blank(&tmp_set);
798 if (!parse_stanza(ps, &fs, pkg_parse_field, &pkg_obj))
799 break;
801 if (pdone && donep)
802 parse_error(ps,
803 _("several package info entries found, only one allowed"));
805 pkg_parse_verify(ps, new_pkg, new_pkgbin);
807 db_pkg = parse_find_pkg_slot(ps, new_pkg, new_pkgbin);
808 if (ps->flags & pdb_recordavailable)
809 db_pkgbin = &db_pkg->available;
810 else
811 db_pkgbin = &db_pkg->installed;
813 if (((ps->flags & pdb_ignoreolder) || ps->type == pdb_file_available) &&
814 dpkg_version_is_informative(&db_pkgbin->version) &&
815 dpkg_version_compare(&new_pkgbin->version, &db_pkgbin->version) < 0)
816 continue;
818 pkg_parse_copy(ps, db_pkg, db_pkgbin, new_pkg, new_pkgbin);
820 if (donep)
821 *donep = db_pkg;
822 pdone++;
823 if (parse_at_eof(ps))
824 break;
827 varbuf_destroy(&fs.value);
828 if (donep && !pdone)
829 ohshit(_("no package information in '%.255s'"), ps->filename);
831 return pdone;
835 * Parse a deb822 style file.
837 * donep may be NULL.
838 * If donep is not NULL only one package's information is expected.
841 parsedb(const char *filename, enum parsedbflags flags, struct pkginfo **pkgp)
843 struct parsedb_state *ps;
844 int count;
846 ps = parsedb_open(filename, flags);
847 parsedb_load(ps);
848 count = parsedb_parse(ps, pkgp);
849 parsedb_close(ps);
851 return count;
855 * Copy dependency links structures.
857 * This routine is used to update the ‘reverse’ dependency pointers when
858 * new ‘forwards’ information has been constructed. It first removes all
859 * the links based on the old information. The old information starts in
860 * *updateme; after much brou-ha-ha the reverse structures are created
861 * and *updateme is set to the value from newdepends.
863 * @param pkg The package we're doing this for. This is used to construct
864 * correct uplinks.
865 * @param updateme The forwards dependency pointer that we are to update.
866 * This starts out containing the old forwards info, which we use to
867 * unthread the old reverse links. After we're done it is updated.
868 * @param newdepends The value that we ultimately want to have in updateme.
869 * @param available The pkgbin to modify, available or installed.
871 * It is likely that the backward pointer for the package in question
872 * (‘depended’) will be updated by this routine, but this will happen by
873 * the routine traversing the dependency data structures. It doesn't need
874 * to be told where to update that; just mentioned here as something that
875 * one should be cautious about.
877 void copy_dependency_links(struct pkginfo *pkg,
878 struct dependency **updateme,
879 struct dependency *newdepends,
880 bool available)
882 struct dependency *dyp;
883 struct deppossi *dop, **revdeps;
885 /* Delete ‘backward’ (‘depended’) links from other packages to
886 * dependencies listed in old version of this one. We do this by
887 * going through all the dependencies in the old version of this
888 * one and following them down to find which deppossi nodes to
889 * remove. */
890 for (dyp= *updateme; dyp; dyp= dyp->next) {
891 for (dop= dyp->list; dop; dop= dop->next) {
892 if (dop->rev_prev)
893 dop->rev_prev->rev_next = dop->rev_next;
894 else if (available)
895 dop->ed->depended.available = dop->rev_next;
896 else
897 dop->ed->depended.installed = dop->rev_next;
898 if (dop->rev_next)
899 dop->rev_next->rev_prev = dop->rev_prev;
903 /* Now fill in new ‘ed’ links from other packages to dependencies
904 * listed in new version of this one, and set our uplinks correctly. */
905 for (dyp= newdepends; dyp; dyp= dyp->next) {
906 dyp->up= pkg;
907 for (dop= dyp->list; dop; dop= dop->next) {
908 revdeps = available ? &dop->ed->depended.available :
909 &dop->ed->depended.installed;
910 dop->rev_next = *revdeps;
911 dop->rev_prev = NULL;
912 if (*revdeps)
913 (*revdeps)->rev_prev = dop;
914 *revdeps = dop;
918 /* Finally, we fill in the new value. */
919 *updateme= newdepends;