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/>.
25 #include <sys/types.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>
46 #include <dpkg/parsedump.h>
47 #include <dpkg/fdio.h>
48 #include <dpkg/buffer.h>
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
},
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
{
107 struct pkgbin
*pkgbin
;
111 * Parse the field and value into the package being constructed.
114 pkg_parse_field(struct parsedb_state
*ps
, struct field_state
*fs
,
117 struct pkg_parse_object
*pkg_obj
= parse_obj
;
118 const struct fieldinfo
*fip
;
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)
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
);
134 struct arbitraryfield
*arp
, **larpp
;
136 if (fs
->fieldlen
< 2)
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
)
145 _("duplicate value for user-defined field '%.*s'"),
146 fs
->fieldlen
, fs
->fieldstart
);
149 arp
= nfmalloc(sizeof(*arp
));
150 arp
->name
= nfstrnsave(fs
->fieldstart
, fs
->fieldlen
);
151 arp
->value
= nfstrnsave(fs
->valuestart
, fs
->valuelen
);
158 * Verify and fixup the package structure being constructed.
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"),
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
)
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
)
231 _("'%s' field present for package with inappropriate '%s' field"),
232 "Config-Version", "Status");
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
))
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
)
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
))
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
)
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
&&
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
);
314 parse_count_pkg_instance(struct pkgcount
*count
,
315 struct pkginfo
*pkg
, struct pkgbin
*pkgbin
)
317 if (pkg
->status
== PKG_STAT_NOTINSTALLED
)
320 if (pkgbin
->multiarch
== PKG_MULTIARCH_SAME
)
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 };
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
);
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",
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
);
406 return pkg_hash_get_pkg(db_set
, new_pkgbin
->arch
);
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
)
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
,
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
);
442 return pkg_hash_get_pkg(db_set
, new_pkgbin
->arch
);
448 * Copy into the in-core database the package being constructed.
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
)) {
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
));
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
;
510 return pdb_file_available
;
512 if (flags
& pdb_single_stanza
)
513 return pdb_file_update
;
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
);
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
;
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"),
562 ps
= parsedb_new(filename
, fd
, flags
| pdb_close_fd
);
564 push_cleanup(cu_closefd
, ~ehflag_normaltidy
, 1, &ps
->fd
);
570 * Load data for package deb822 style parsing.
573 parsedb_load(struct parsedb_state
*ps
)
577 if (ps
->fd
< 0 && (ps
->flags
& pdb_allow_empty
))
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
;
588 size
= fd_vbuf_copy(ps
->fd
, &buf
, -1, &err
);
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) {
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
);
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
);
606 ps
->endptr
= ps
->dataptr
+ st
.st_size
;
608 ps
->dataptr
= ps
->endptr
= NULL
;
610 ps
->data
= ps
->dataptr
;
614 * Parse an RFC-822 style stanza.
617 parse_stanza(struct parsedb_state
*ps
, struct field_state
*fs
,
618 parse_field_func
*parse_field
, void *parse_obj
)
622 /* Skip adjacent new lines. */
623 while (!parse_at_eof(ps
)) {
625 if (c
!= '\n' && c
!= MSDOS_EOF_CHAR
)
630 /* Nothing relevant parsed, bail out. */
631 if (parse_at_eof(ps
))
634 /* Loop per field. */
638 /* Scan field name. */
639 fs
->fieldstart
= ps
->dataptr
- 1;
640 while (!parse_at_eof(ps
) && !c_isspace(c
) && c
!= ':' && c
!= MSDOS_EOF_CHAR
)
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
))
654 if (parse_at_eof(ps
))
655 parse_error(ps
, _("end of file after field name '%.*s'"),
656 fs
->fieldlen
, fs
->fieldstart
);
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
);
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
)) {
671 if (c
== '\n' || !c_isspace(c
))
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
);
683 /* Scan field value. */
684 fs
->valuestart
= ps
->dataptr
- 1;
686 if (c
== '\n' || c
== MSDOS_EOF_CHAR
) {
688 parse_lax_problem(ps
, pdb_lax_stanza_parser
,
689 _("blank line in value of field '%.*s'"),
690 fs
->fieldlen
, fs
->fieldstart
);
693 if (parse_at_eof(ps
))
697 /* Found double EOL, or start of new field. */
698 if (parse_at_eof(ps
) || c
== '\n' || !c_isspace(c
))
703 } else if (blank_line
&& !c_isspace(c
)) {
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
);
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)))
719 parse_field(ps
, fs
, parse_obj
);
721 if (parse_at_eof(ps
) || c
== '\n' || c
== MSDOS_EOF_CHAR
)
723 } /* Loop per field. */
732 * Teardown a package deb822 parser context.
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
) {
746 munmap(ps
->data
, ps
->endptr
- ps
->data
);
751 dpkg_error_destroy(&ps
->err
);
752 varbuf_destroy(&ps
->errmsg
);
757 * Parse deb822 style package data from a buffer.
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
)];
771 struct field_state fs
;
773 if (ps
->data
== NULL
&& (ps
->flags
& pdb_allow_empty
))
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
;
783 new_pkgbin
= &new_pkg
->installed
;
786 ps
->pkgbin
= new_pkgbin
;
788 pkg_obj
.pkg
= new_pkg
;
789 pkg_obj
.pkgbin
= new_pkgbin
;
793 /* Loop per package. */
795 memset(fieldencountered
, 0, sizeof(fieldencountered
));
796 pkgset_blank(&tmp_set
);
798 if (!parse_stanza(ps
, &fs
, pkg_parse_field
, &pkg_obj
))
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
;
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)
818 pkg_parse_copy(ps
, db_pkg
, db_pkgbin
, new_pkg
, new_pkgbin
);
823 if (parse_at_eof(ps
))
827 varbuf_destroy(&fs
.value
);
829 ohshit(_("no package information in '%.255s'"), ps
->filename
);
835 * Parse a deb822 style file.
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
;
846 ps
= parsedb_open(filename
, flags
);
848 count
= parsedb_parse(ps
, pkgp
);
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
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
,
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
890 for (dyp
= *updateme
; dyp
; dyp
= dyp
->next
) {
891 for (dop
= dyp
->list
; dop
; dop
= dop
->next
) {
893 dop
->rev_prev
->rev_next
= dop
->rev_next
;
895 dop
->ed
->depended
.available
= dop
->rev_next
;
897 dop
->ed
->depended
.installed
= 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
) {
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
;
913 (*revdeps
)->rev_prev
= dop
;
918 /* Finally, we fill in the new value. */
919 *updateme
= newdepends
;