1 # Copyright © 2007-2009 Raphaël Hertzog <hertzog@debian.org>
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16 package Dpkg::Control::FieldsCore;
21 our $VERSION = '1.00';
42 use Exporter qw(import);
45 use Dpkg::ErrorHandling;
46 use Dpkg::Control::Types;
50 ALL_PKG => CTRL_INFO_PKG | CTRL_INDEX_PKG | CTRL_PKG_DEB | CTRL_FILE_STATUS,
51 ALL_SRC => CTRL_INFO_SRC | CTRL_INDEX_SRC | CTRL_PKG_SRC,
52 ALL_CHANGES => CTRL_FILE_CHANGES | CTRL_CHANGELOG,
53 ALL_COPYRIGHT => CTRL_COPYRIGHT_HEADER | CTRL_COPYRIGHT_FILES | CTRL_COPYRIGHT_LICENSE,
57 FIELD_SEP_UNKNOWN => 0,
63 # The canonical list of fields
65 # Note that fields used only in dpkg's available file are not listed
66 # Deprecated fields of dpkg's status file are also not listed
69 allowed => (ALL_PKG | ALL_SRC | CTRL_FILE_BUILDINFO | CTRL_FILE_CHANGES) & (~CTRL_INFO_SRC),
70 separator => FIELD_SEP_SPACE,
73 allowed => CTRL_REPO_RELEASE,
74 separator => FIELD_SEP_SPACE,
76 'Auto-Built-Package' => {
77 allowed => ALL_PKG & ~CTRL_INFO_PKG,
78 separator => FIELD_SEP_SPACE,
81 allowed => CTRL_PKG_SRC | CTRL_FILE_BUILDINFO | CTRL_FILE_CHANGES,
82 # XXX: This field values are separated either by space or comma
83 # depending on the context.
84 separator => FIELD_SEP_SPACE | FIELD_SEP_COMMA,
87 allowed => ALL_CHANGES,
89 'Binary-Only-Changes' => {
90 allowed => CTRL_FILE_BUILDINFO,
94 separator => FIELD_SEP_COMMA,
95 dependency => 'union',
99 allowed => (ALL_PKG | CTRL_INFO_SRC | CTRL_FILE_VENDOR) & (~CTRL_INFO_PKG),
101 'Build-Architecture' => {
102 allowed => CTRL_FILE_BUILDINFO,
104 'Build-Conflicts' => {
106 separator => FIELD_SEP_COMMA,
107 dependency => 'union',
110 'Build-Conflicts-Arch' => {
112 separator => FIELD_SEP_COMMA,
113 dependency => 'union',
116 'Build-Conflicts-Indep' => {
118 separator => FIELD_SEP_COMMA,
119 dependency => 'union',
123 allowed => CTRL_FILE_BUILDINFO,
127 separator => FIELD_SEP_COMMA,
128 dependency => 'normal',
131 'Build-Depends-Arch' => {
133 separator => FIELD_SEP_COMMA,
134 dependency => 'normal',
137 'Build-Depends-Indep' => {
139 separator => FIELD_SEP_COMMA,
140 dependency => 'normal',
143 'Build-Essential' => {
147 allowed => CTRL_FILE_BUILDINFO,
150 allowed => CTRL_FILE_BUILDINFO,
152 'Build-Profiles' => {
153 allowed => CTRL_INFO_PKG,
154 separator => FIELD_SEP_SPACE,
156 'Built-For-Profiles' => {
157 allowed => ALL_PKG | CTRL_FILE_CHANGES,
158 separator => FIELD_SEP_SPACE,
162 separator => FIELD_SEP_COMMA,
163 dependency => 'union',
167 allowed => CTRL_FILE_CHANGES,
170 allowed => CTRL_REPO_RELEASE,
173 allowed => ALL_CHANGES,
176 allowed => CTRL_TESTS,
177 separator => FIELD_SEP_COMMA,
180 allowed => ALL_CHANGES,
181 separator => FIELD_SEP_SPACE,
184 allowed => CTRL_REPO_RELEASE,
187 allowed => ALL_COPYRIGHT,
190 allowed => CTRL_REPO_RELEASE,
191 separator => FIELD_SEP_SPACE,
194 allowed => CTRL_FILE_STATUS,
195 separator => FIELD_SEP_LINE | FIELD_SEP_SPACE,
197 'Config-Version' => {
198 allowed => CTRL_FILE_STATUS,
202 separator => FIELD_SEP_COMMA,
203 dependency => 'union',
207 allowed => CTRL_COPYRIGHT_HEADER | CTRL_COPYRIGHT_FILES,
210 allowed => ALL_CHANGES | CTRL_REPO_RELEASE,
213 allowed => ALL_PKG | CTRL_TESTS,
214 separator => FIELD_SEP_COMMA,
215 dependency => 'normal',
219 allowed => ALL_PKG | CTRL_FILE_CHANGES | CTRL_REPO_RELEASE,
222 allowed => CTRL_COPYRIGHT_HEADER,
225 allowed => CTRL_INDEX_SRC,
228 allowed => ALL_CHANGES,
232 separator => FIELD_SEP_COMMA,
233 dependency => 'union',
237 allowed => CTRL_FILE_BUILDINFO,
238 separator => FIELD_SEP_LINE,
244 allowed => CTRL_TESTS,
245 separator => FIELD_SEP_SPACE,
248 allowed => CTRL_INDEX_PKG,
249 separator => FIELD_SEP_LINE | FIELD_SEP_SPACE,
252 allowed => CTRL_PKG_SRC | CTRL_FILE_CHANGES | CTRL_COPYRIGHT_FILES,
253 separator => FIELD_SEP_LINE | FIELD_SEP_SPACE,
256 allowed => CTRL_PKG_SRC | CTRL_FILE_CHANGES | CTRL_COPYRIGHT_HEADER | CTRL_FILE_BUILDINFO,
259 allowed => ALL_SRC | ALL_PKG,
261 'Installed-Build-Depends' => {
262 allowed => CTRL_FILE_BUILDINFO,
263 separator => FIELD_SEP_COMMA,
264 dependency => 'union',
267 'Installed-Size' => {
268 allowed => ALL_PKG & ~CTRL_INFO_PKG,
270 'Installer-Menu-Item' => {
273 'Kernel-Version' => {
277 allowed => CTRL_REPO_RELEASE,
280 allowed => ALL_COPYRIGHT,
283 allowed => (ALL_PKG | ALL_SRC | CTRL_REPO_RELEASE) & (~CTRL_INFO_PKG),
286 allowed => CTRL_PKG_DEB| CTRL_FILE_STATUS | ALL_SRC | ALL_CHANGES,
295 allowed => ALL_SRC & ~CTRL_INFO_SRC,
296 separator => FIELD_SEP_LINE | FIELD_SEP_SPACE,
302 allowed => CTRL_FILE_VENDOR,
306 separator => FIELD_SEP_COMMA,
307 dependency => 'normal',
311 allowed => CTRL_INFO_SRC | CTRL_INDEX_SRC | ALL_PKG,
315 separator => FIELD_SEP_COMMA,
316 dependency => 'union',
321 separator => FIELD_SEP_COMMA,
322 dependency => 'normal',
327 separator => FIELD_SEP_COMMA,
328 dependency => 'union',
332 allowed => CTRL_TESTS,
333 separator => FIELD_SEP_SPACE,
336 allowed => CTRL_INFO_SRC | CTRL_INDEX_SRC | ALL_PKG,
339 allowed => CTRL_INDEX_PKG,
340 separator => FIELD_SEP_LINE | FIELD_SEP_SPACE,
343 allowed => (ALL_PKG | ALL_SRC | ALL_CHANGES | CTRL_COPYRIGHT_HEADER | CTRL_FILE_BUILDINFO) &
344 (~(CTRL_INDEX_SRC | CTRL_INFO_PKG)),
346 'Standards-Version' => {
350 allowed => CTRL_FILE_STATUS,
351 separator => FIELD_SEP_SPACE,
353 'Subarchitecture' => {
357 allowed => CTRL_REPO_RELEASE,
361 separator => FIELD_SEP_COMMA,
362 dependency => 'normal',
367 separator => FIELD_SEP_COMMA,
373 allowed => CTRL_TESTS,
376 allowed => CTRL_TESTS,
377 separator => FIELD_SEP_SPACE,
379 'Tests-Directory' => {
380 allowed => CTRL_TESTS,
384 separator => FIELD_SEP_COMMA,
386 'Testsuite-Triggers' => {
388 separator => FIELD_SEP_COMMA,
391 allowed => CTRL_CHANGELOG,
393 'Triggers-Awaited' => {
394 allowed => CTRL_FILE_STATUS,
395 separator => FIELD_SEP_SPACE,
397 'Triggers-Pending' => {
398 allowed => CTRL_FILE_STATUS,
399 separator => FIELD_SEP_SPACE,
403 separator => FIELD_SEP_COMMA,
406 allowed => CTRL_COPYRIGHT_HEADER,
408 'Upstream-Contact' => {
409 allowed => CTRL_COPYRIGHT_HEADER,
412 allowed => ALL_CHANGES,
415 allowed => CTRL_REPO_RELEASE,
445 allowed => CTRL_FILE_VENDOR,
448 allowed => CTRL_FILE_VENDOR,
451 allowed => (ALL_PKG | ALL_SRC | CTRL_FILE_BUILDINFO | ALL_CHANGES) &
452 (~(CTRL_INFO_SRC | CTRL_INFO_PKG)),
456 my @checksum_fields = map { &field_capitalize("Checksums-$_") } checksums_get_list();
457 my @sum_fields = map { $_ eq 'md5' ? 'MD5sum' : &field_capitalize($_) }
458 checksums_get_list();
459 &field_register($_, CTRL_PKG_SRC | CTRL_FILE_CHANGES | CTRL_FILE_BUILDINFO) foreach @checksum_fields;
460 &field_register($_, CTRL_INDEX_PKG | CTRL_REPO_RELEASE,
461 separator => FIELD_SEP_LINE | FIELD_SEP_SPACE) foreach @sum_fields;
465 qw(Package Package-Type Source Version Built-Using Kernel-Version
466 Built-For-Profiles Auto-Built-Package Architecture Subarchitecture
467 Installer-Menu-Item Essential Origin Bugs
468 Maintainer Installed-Size), &field_list_pkg_dep(),
469 qw(Section Priority Multi-Arch Homepage Description Tag Task)
472 qw(Format Source Binary Architecture Version Origin Maintainer
473 Uploaders Homepage Standards-Version Vcs-Browser
474 Vcs-Arch Vcs-Bzr Vcs-Cvs Vcs-Darcs Vcs-Git Vcs-Hg Vcs-Mtn
475 Vcs-Svn Testsuite Testsuite-Triggers), &field_list_src_dep(),
476 qw(Package-List), @checksum_fields, qw(Files)
478 CTRL_FILE_BUILDINFO() => [
479 qw(Format Source Binary Architecture Version
480 Binary-Only-Changes),
482 qw(Build-Origin Build-Architecture Build-Date Build-Path
483 Installed-Build-Depends Environment),
485 CTRL_FILE_CHANGES() => [
486 qw(Format Date Source Binary Binary-Only Built-For-Profiles Architecture
487 Version Distribution Urgency Maintainer Changed-By Description
489 @checksum_fields, qw(Files)
491 CTRL_CHANGELOG() => [
492 qw(Source Binary-Only Version Distribution Urgency Maintainer
493 Timestamp Date Closes Changes)
495 CTRL_FILE_STATUS() => [ # Same as fieldinfos in lib/dpkg/parse.c
496 qw(Package Essential Status Priority Section Installed-Size Origin
497 Maintainer Bugs Architecture Multi-Arch Source Version Config-Version
498 Replaces Provides Depends Pre-Depends Recommends Suggests Breaks
499 Conflicts Enhances Conffiles Description Triggers-Pending
502 CTRL_REPO_RELEASE() => [
503 qw(Origin Label Suite Codename Changelogs Date Valid-Until
504 Architectures Components Description), @sum_fields
506 CTRL_COPYRIGHT_HEADER() => [
507 qw(Format Upstream-Name Upstream-Contact Source Disclaimer Comment
510 CTRL_COPYRIGHT_FILES() => [
511 qw(Files Copyright License Comment)
513 CTRL_COPYRIGHT_LICENSE() => [
517 # Order for CTRL_INDEX_PKG is derived from CTRL_PKG_DEB
518 $FIELD_ORDER{CTRL_INDEX_PKG()} = [ @{$FIELD_ORDER{CTRL_PKG_DEB()}} ];
519 &field_insert_before(CTRL_INDEX_PKG, 'Section', 'Filename', 'Size', @sum_fields);
520 # Order for CTRL_INDEX_SRC is derived from CTRL_PKG_SRC
521 $FIELD_ORDER{CTRL_INDEX_SRC()} = [ @{$FIELD_ORDER{CTRL_PKG_SRC()}} ];
522 @{$FIELD_ORDER{CTRL_INDEX_SRC()}} = map { $_ eq 'Source' ? 'Package' : $_ }
523 @{$FIELD_ORDER{CTRL_PKG_SRC()}};
524 &field_insert_after(CTRL_INDEX_SRC, 'Version', 'Priority', 'Section');
525 &field_insert_before(CTRL_INDEX_SRC, 'Checksums-Md5', 'Directory');
531 Dpkg::Control::FieldsCore - manage (list of official) control fields
535 The modules contains a list of fieldnames with associated meta-data explaining
536 in which type of control information they are allowed. The types are the
537 CTRL_* constants exported by Dpkg::Control.
543 =item $f = field_capitalize($field_name)
545 Returns the field name properly capitalized. All characters are lowercase,
546 except the first of each word (words are separated by a hyphen in field names).
550 sub field_capitalize($) {
551 my $field = lc(shift);
552 # Some special cases due to history
553 return 'MD5sum' if $field eq 'md5sum';
554 return uc($field) if checksums_is_supported($field);
556 return join '-', map { ucfirst } split /-/, $field;
559 =item field_is_official($fname)
561 Returns true if the field is official and known.
565 sub field_is_official($) {
566 my $field = field_capitalize(shift);
568 return exists $FIELDS{$field};
571 =item field_is_allowed_in($fname, @types)
573 Returns true (1) if the field $fname is allowed in all the types listed in
574 the list. Note that you can use type sets instead of individual types (ex:
575 CTRL_FILE_CHANGES | CTRL_CHANGELOG).
577 field_allowed_in(A|B, C) returns true only if the field is allowed in C
580 Undef is returned for non-official fields.
584 sub field_is_allowed_in($@) {
585 my ($field, @types) = @_;
586 $field = field_capitalize($field);
587 return unless field_is_official($field);
589 return 0 if not scalar(@types);
590 foreach my $type (@types) {
591 next if $type == CTRL_UNKNOWN; # Always allowed
592 return 0 unless $FIELDS{$field}{allowed} & $type;
597 =item field_transfer_single($from, $to, $field)
599 If appropriate, copy the value of the field named $field taken from the
600 $from Dpkg::Control object to the $to Dpkg::Control object.
602 Official fields are copied only if the field is allowed in both types of
603 objects. Custom fields are treated in a specific manner. When the target
604 is not among CTRL_PKG_SRC, CTRL_PKG_DEB or CTRL_FILE_CHANGES, then they
605 are always copied as is (the X- prefix is kept). Otherwise they are not
606 copied except if the target object matches the target destination encoded
607 in the field name. The initial X denoting custom fields can be followed by
608 one or more letters among "S" (Source: corresponds to CTRL_PKG_SRC), "B"
609 (Binary: corresponds to CTRL_PKG_DEB) or "C" (Changes: corresponds to
612 Returns undef if nothing has been copied or the name of the new field
613 added to $to otherwise.
617 sub field_transfer_single($$;$) {
618 my ($from, $to, $field) = @_;
620 my ($from_type, $to_type) = ($from->get_type(), $to->get_type());
621 $field = field_capitalize($field);
623 if (field_is_allowed_in($field, $from_type, $to_type)) {
624 $to->{$field} = $from->{$field};
626 } elsif ($field =~ /^X([SBC]*)-/i) {
628 if (($dest =~ /B/i and $to_type == CTRL_PKG_DEB) or
629 ($dest =~ /S/i and $to_type == CTRL_PKG_SRC) or
630 ($dest =~ /C/i and $to_type == CTRL_FILE_CHANGES))
633 $new =~ s/^X([SBC]*)-//i;
634 $to->{$new} = $from->{$field};
636 } elsif ($to_type != CTRL_PKG_DEB and
637 $to_type != CTRL_PKG_SRC and
638 $to_type != CTRL_FILE_CHANGES)
640 $to->{$field} = $from->{$field};
643 } elsif (not field_is_allowed_in($field, $from_type)) {
644 warning(g_("unknown information field '%s' in input data in %s"),
645 $field, $from->get_option('name') || g_('control information'));
650 =item field_transfer_all($from, $to)
652 Transfer all appropriate fields from $from to $to. Calls
653 field_transfer_single() on all fields available in $from.
655 Returns the list of fields that have been added to $to.
659 sub field_transfer_all($$) {
660 my ($from, $to) = @_;
662 foreach my $k (keys %$from) {
663 $res = field_transfer_single($from, $to, $k);
664 push @res, $res if $res and defined wantarray;
669 =item field_ordered_list($type)
671 Returns an ordered list of fields for a given type of control information.
672 This list can be used to output the fields in a predictable order.
673 The list might be empty for types where the order does not matter much.
677 sub field_ordered_list($) {
679 return @{$FIELD_ORDER{$type}} if exists $FIELD_ORDER{$type};
683 =item field_list_src_dep()
685 List of fields that contains dependencies-like information in a source
690 sub field_list_src_dep() {
692 $FIELDS{$a}{dep_order} <=> $FIELDS{$b}{dep_order}
694 field_is_allowed_in($_, CTRL_PKG_SRC) and
695 exists $FIELDS{$_}{dependency}
700 =item field_list_pkg_dep()
702 List of fields that contains dependencies-like information in a binary
703 Debian package. The fields that express real dependencies are sorted from
704 the stronger to the weaker.
708 sub field_list_pkg_dep() {
709 my @keys = keys %FIELDS;
711 $FIELDS{$a}{dep_order} <=> $FIELDS{$b}{dep_order}
713 field_is_allowed_in($_, CTRL_PKG_DEB) and
714 exists $FIELDS{$_}{dependency}
719 =item field_get_dep_type($field)
721 Return the type of the dependency expressed by the given field. Can
722 either be "normal" for a real dependency field (Pre-Depends, Depends, ...)
723 or "union" for other relation fields sharing the same syntax (Conflicts,
724 Breaks, ...). Returns undef for fields which are not dependencies.
728 sub field_get_dep_type($) {
729 my $field = field_capitalize(shift);
731 return unless field_is_official($field);
732 return $FIELDS{$field}{dependency} if exists $FIELDS{$field}{dependency};
736 =item field_get_sep_type($field)
738 Return the type of the field value separator. Can be one of FIELD_SEP_UNKNOWN,
739 FIELD_SEP_SPACE, FIELD_SEP_COMMA or FIELD_SEP_LINE.
743 sub field_get_sep_type($) {
744 my $field = field_capitalize(shift);
746 return $FIELDS{$field}{separator} if exists $FIELDS{$field}{separator};
747 return FIELD_SEP_UNKNOWN;
750 =item field_register($field, $allowed_types, %opts)
752 Register a new field as being allowed in control information of specified
753 types. %opts is optional
757 sub field_register($$;@) {
758 my ($field, $types, %opts) = @_;
759 $field = field_capitalize($field);
766 =item field_insert_after($type, $ref, @fields)
768 Place field after another one ($ref) in output of control information of
772 sub field_insert_after($$@) {
773 my ($type, $field, @fields) = @_;
774 return 0 if not exists $FIELD_ORDER{$type};
775 ($field, @fields) = map { field_capitalize($_) } ($field, @fields);
776 @{$FIELD_ORDER{$type}} = map {
777 ($_ eq $field) ? ($_, @fields) : $_
778 } @{$FIELD_ORDER{$type}};
782 =item field_insert_before($type, $ref, @fields)
784 Place field before another one ($ref) in output of control information of
788 sub field_insert_before($$@) {
789 my ($type, $field, @fields) = @_;
790 return 0 if not exists $FIELD_ORDER{$type};
791 ($field, @fields) = map { field_capitalize($_) } ($field, @fields);
792 @{$FIELD_ORDER{$type}} = map {
793 ($_ eq $field) ? (@fields, $_) : $_
794 } @{$FIELD_ORDER{$type}};
802 =head2 Version 1.00 (dpkg 1.17.0)
804 Mark the module as public.