File Coverage

blib/lib/Dpkg/Control/FieldsCore.pm
Criterion Covered Total %
statement 77 105 73.3
branch 22 42 52.3
condition 10 30 33.3
subroutine 17 21 80.9
pod 13 13 100.0
total 139 211 65.8


line stmt bran cond sub pod time code
1             # Copyright © 2007-2009 Raphaël Hertzog
2             #
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.
7             #
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.
12             #
13             # You should have received a copy of the GNU General Public License
14             # along with this program. If not, see .
15              
16             package Dpkg::Control::FieldsCore;
17              
18 14     14   1256 use strict;
  14         30  
  14         402  
19 14     14   68 use warnings;
  14         35  
  14         950  
20              
21             our $VERSION = '1.00';
22             our @EXPORT = qw(
23             field_capitalize
24             field_is_official
25             field_is_allowed_in
26             field_transfer_single
27             field_transfer_all
28             field_list_src_dep
29             field_list_pkg_dep
30             field_get_dep_type
31             field_get_sep_type
32             field_ordered_list
33             field_register
34             field_insert_after
35             field_insert_before
36             FIELD_SEP_UNKNOWN
37             FIELD_SEP_SPACE
38             FIELD_SEP_COMMA
39             FIELD_SEP_LINE
40             );
41              
42 14     14   110 use Exporter qw(import);
  14         32  
  14         509  
43              
44 14     14   517 use Dpkg::Gettext;
  14         37  
  14         919  
45 14     14   533 use Dpkg::ErrorHandling;
  14         76  
  14         1187  
46 14     14   3628 use Dpkg::Control::Types;
  14         32  
  14         1637  
47              
48             use constant {
49 14         1351 ALL_PKG => CTRL_INFO_PKG | CTRL_INDEX_PKG | CTRL_PKG_DEB | CTRL_FILE_STATUS,
50             ALL_SRC => CTRL_INFO_SRC | CTRL_INDEX_SRC | CTRL_PKG_SRC,
51             ALL_CHANGES => CTRL_FILE_CHANGES | CTRL_CHANGELOG,
52             ALL_COPYRIGHT => CTRL_COPYRIGHT_HEADER | CTRL_COPYRIGHT_FILES | CTRL_COPYRIGHT_LICENSE,
53 14     14   97 };
  14         98  
54              
55             use constant {
56 14         42948 FIELD_SEP_UNKNOWN => 0,
57             FIELD_SEP_SPACE => 1,
58             FIELD_SEP_COMMA => 2,
59             FIELD_SEP_LINE => 4,
60 14     14   106 };
  14         45  
61              
62             # The canonical list of fields
63              
64             # Note that fields used only in dpkg's available file are not listed
65             # Deprecated fields of dpkg's status file are also not listed
66             our %FIELDS = (
67             'architecture' => {
68             name => 'Architecture',
69             allowed => (ALL_PKG | ALL_SRC | CTRL_FILE_BUILDINFO | CTRL_FILE_CHANGES) & (~CTRL_INFO_SRC),
70             separator => FIELD_SEP_SPACE,
71             },
72             'architectures' => {
73             name => 'Architectures',
74             allowed => CTRL_REPO_RELEASE,
75             separator => FIELD_SEP_SPACE,
76             },
77             'auto-built-package' => {
78             name => 'Auto-Built-Package',
79             allowed => ALL_PKG & ~CTRL_INFO_PKG,
80             separator => FIELD_SEP_SPACE,
81             },
82             'binary' => {
83             name => 'Binary',
84             allowed => CTRL_PKG_SRC | CTRL_INDEX_SRC | CTRL_FILE_BUILDINFO | CTRL_FILE_CHANGES,
85             # XXX: This field values are separated either by space or comma
86             # depending on the context.
87             separator => FIELD_SEP_SPACE | FIELD_SEP_COMMA,
88             },
89             'binary-only' => {
90             name => 'Binary-Only',
91             allowed => ALL_CHANGES,
92             },
93             'binary-only-changes' => {
94             name => 'Binary-Only-Changes',
95             allowed => CTRL_FILE_BUILDINFO,
96             },
97             'breaks' => {
98             name => 'Breaks',
99             allowed => ALL_PKG,
100             separator => FIELD_SEP_COMMA,
101             dependency => 'union',
102             dep_order => 7,
103             },
104             'bugs' => {
105             name => 'Bugs',
106             allowed => (ALL_PKG | CTRL_INFO_SRC | CTRL_FILE_VENDOR) & (~CTRL_INFO_PKG),
107             },
108             'build-architecture' => {
109             name => 'Build-Architecture',
110             allowed => CTRL_FILE_BUILDINFO,
111             },
112             'build-conflicts' => {
113             name => 'Build-Conflicts',
114             allowed => ALL_SRC,
115             separator => FIELD_SEP_COMMA,
116             dependency => 'union',
117             dep_order => 4,
118             },
119             'build-conflicts-arch' => {
120             name => 'Build-Conflicts-Arch',
121             allowed => ALL_SRC,
122             separator => FIELD_SEP_COMMA,
123             dependency => 'union',
124             dep_order => 5,
125             },
126             'build-conflicts-indep' => {
127             name => 'Build-Conflicts-Indep',
128             allowed => ALL_SRC,
129             separator => FIELD_SEP_COMMA,
130             dependency => 'union',
131             dep_order => 6,
132             },
133             'build-date' => {
134             name => 'Build-Date',
135             allowed => CTRL_FILE_BUILDINFO,
136             },
137             'build-depends' => {
138             name => 'Build-Depends',
139             allowed => ALL_SRC,
140             separator => FIELD_SEP_COMMA,
141             dependency => 'normal',
142             dep_order => 1,
143             },
144             'build-depends-arch' => {
145             name => 'Build-Depends-Arch',
146             allowed => ALL_SRC,
147             separator => FIELD_SEP_COMMA,
148             dependency => 'normal',
149             dep_order => 2,
150             },
151             'build-depends-indep' => {
152             name => 'Build-Depends-Indep',
153             allowed => ALL_SRC,
154             separator => FIELD_SEP_COMMA,
155             dependency => 'normal',
156             dep_order => 3,
157             },
158             'build-essential' => {
159             name => 'Build-Essential',
160             allowed => ALL_PKG,
161             },
162             'build-kernel-version' => {
163             name => 'Build-Kernel-Version',
164             allowed => CTRL_FILE_BUILDINFO,
165             },
166             'build-origin' => {
167             name => 'Build-Origin',
168             allowed => CTRL_FILE_BUILDINFO,
169             },
170             'build-path' => {
171             name => 'Build-Path',
172             allowed => CTRL_FILE_BUILDINFO,
173             },
174             'build-profiles' => {
175             name => 'Build-Profiles',
176             allowed => CTRL_INFO_PKG,
177             separator => FIELD_SEP_SPACE,
178             },
179             'build-tainted-by' => {
180             name => 'Build-Tainted-By',
181             allowed => CTRL_FILE_BUILDINFO,
182             separator => FIELD_SEP_SPACE,
183             },
184             'built-for-profiles' => {
185             name => 'Built-For-Profiles',
186             allowed => ALL_PKG | CTRL_FILE_CHANGES,
187             separator => FIELD_SEP_SPACE,
188             },
189             'built-using' => {
190             name => 'Built-Using',
191             allowed => ALL_PKG,
192             separator => FIELD_SEP_COMMA,
193             dependency => 'union',
194             dep_order => 10,
195             },
196             'changed-by' => {
197             name => 'Changed-By',
198             allowed => CTRL_FILE_CHANGES,
199             },
200             'changelogs' => {
201             name => 'Changelogs',
202             allowed => CTRL_REPO_RELEASE,
203             },
204             'changes' => {
205             name => 'Changes',
206             allowed => ALL_CHANGES,
207             },
208             'checksums-md5' => {
209             name => 'Checksums-Md5',
210             allowed => CTRL_PKG_SRC | CTRL_INDEX_SRC | CTRL_FILE_CHANGES | CTRL_FILE_BUILDINFO,
211             },
212             'checksums-sha1' => {
213             name => 'Checksums-Sha1',
214             allowed => CTRL_PKG_SRC | CTRL_INDEX_SRC | CTRL_FILE_CHANGES | CTRL_FILE_BUILDINFO,
215             },
216             'checksums-sha256' => {
217             name => 'Checksums-Sha256',
218             allowed => CTRL_PKG_SRC | CTRL_INDEX_SRC | CTRL_FILE_CHANGES | CTRL_FILE_BUILDINFO,
219             },
220             'classes' => {
221             name => 'Classes',
222             allowed => CTRL_TESTS,
223             separator => FIELD_SEP_COMMA,
224             },
225             'closes' => {
226             name => 'Closes',
227             allowed => ALL_CHANGES,
228             separator => FIELD_SEP_SPACE,
229             },
230             'codename' => {
231             name => 'Codename',
232             allowed => CTRL_REPO_RELEASE,
233             },
234             'comment' => {
235             name => 'Comment',
236             allowed => ALL_COPYRIGHT,
237             },
238             'components' => {
239             name => 'Components',
240             allowed => CTRL_REPO_RELEASE,
241             separator => FIELD_SEP_SPACE,
242             },
243             'conffiles' => {
244             name => 'Conffiles',
245             allowed => CTRL_FILE_STATUS,
246             separator => FIELD_SEP_LINE | FIELD_SEP_SPACE,
247             },
248             'config-version' => {
249             name => 'Config-Version',
250             allowed => CTRL_FILE_STATUS,
251             },
252             'conflicts' => {
253             name => 'Conflicts',
254             allowed => ALL_PKG,
255             separator => FIELD_SEP_COMMA,
256             dependency => 'union',
257             dep_order => 6,
258             },
259             'copyright' => {
260             name => 'Copyright',
261             allowed => CTRL_COPYRIGHT_HEADER | CTRL_COPYRIGHT_FILES,
262             },
263             'date' => {
264             name => 'Date',
265             allowed => ALL_CHANGES | CTRL_REPO_RELEASE,
266             },
267             'depends' => {
268             name => 'Depends',
269             allowed => ALL_PKG | CTRL_TESTS,
270             separator => FIELD_SEP_COMMA,
271             dependency => 'normal',
272             dep_order => 2,
273             },
274             'description' => {
275             name => 'Description',
276             allowed => ALL_SRC | ALL_PKG | CTRL_FILE_CHANGES | CTRL_REPO_RELEASE,
277             },
278             'disclaimer' => {
279             name => 'Disclaimer',
280             allowed => CTRL_COPYRIGHT_HEADER,
281             },
282             'directory' => {
283             name => 'Directory',
284             allowed => CTRL_INDEX_SRC,
285             },
286             'distribution' => {
287             name => 'Distribution',
288             allowed => ALL_CHANGES,
289             },
290             'enhances' => {
291             name => 'Enhances',
292             allowed => ALL_PKG,
293             separator => FIELD_SEP_COMMA,
294             dependency => 'union',
295             dep_order => 5,
296             },
297             'environment' => {
298             name => 'Environment',
299             allowed => CTRL_FILE_BUILDINFO,
300             separator => FIELD_SEP_LINE,
301             },
302             'essential' => {
303             name => 'Essential',
304             allowed => ALL_PKG,
305             },
306             'features' => {
307             name => 'Features',
308             allowed => CTRL_TESTS,
309             separator => FIELD_SEP_SPACE,
310             },
311             'filename' => {
312             name => 'Filename',
313             allowed => CTRL_INDEX_PKG,
314             separator => FIELD_SEP_LINE | FIELD_SEP_SPACE,
315             },
316             'files' => {
317             name => 'Files',
318             allowed => CTRL_PKG_SRC | CTRL_INDEX_SRC | CTRL_FILE_CHANGES | CTRL_COPYRIGHT_FILES,
319             separator => FIELD_SEP_LINE | FIELD_SEP_SPACE,
320             },
321             'format' => {
322             name => 'Format',
323             allowed => CTRL_PKG_SRC | CTRL_INDEX_SRC | CTRL_FILE_CHANGES | CTRL_COPYRIGHT_HEADER | CTRL_FILE_BUILDINFO,
324             },
325             'homepage' => {
326             name => 'Homepage',
327             allowed => ALL_SRC | ALL_PKG,
328             },
329             'installed-build-depends' => {
330             name => 'Installed-Build-Depends',
331             allowed => CTRL_FILE_BUILDINFO,
332             separator => FIELD_SEP_COMMA,
333             dependency => 'union',
334             dep_order => 11,
335             },
336             'installed-size' => {
337             name => 'Installed-Size',
338             allowed => ALL_PKG & ~CTRL_INFO_PKG,
339             },
340             'installer-menu-item' => {
341             name => 'Installer-Menu-Item',
342             allowed => ALL_PKG,
343             },
344             'kernel-version' => {
345             name => 'Kernel-Version',
346             allowed => ALL_PKG,
347             },
348             'label' => {
349             name => 'Label',
350             allowed => CTRL_REPO_RELEASE,
351             },
352             'license' => {
353             name => 'License',
354             allowed => ALL_COPYRIGHT,
355             },
356             'origin' => {
357             name => 'Origin',
358             allowed => (ALL_PKG | ALL_SRC | CTRL_REPO_RELEASE) & (~CTRL_INFO_PKG),
359             },
360             'maintainer' => {
361             name => 'Maintainer',
362             allowed => CTRL_PKG_DEB| CTRL_INDEX_PKG | CTRL_FILE_STATUS | ALL_SRC | ALL_CHANGES,
363             },
364             'md5sum' => {
365             # XXX: Wrong capitalization due to historical reasons.
366             name => 'MD5sum',
367             allowed => CTRL_INDEX_PKG | CTRL_REPO_RELEASE,
368             separator => FIELD_SEP_LINE | FIELD_SEP_SPACE,
369             },
370             'multi-arch' => {
371             name => 'Multi-Arch',
372             allowed => ALL_PKG,
373             },
374             'package' => {
375             name => 'Package',
376             allowed => ALL_PKG | CTRL_INDEX_SRC,
377             },
378             'package-list' => {
379             name => 'Package-List',
380             allowed => ALL_SRC & ~CTRL_INFO_SRC,
381             separator => FIELD_SEP_LINE | FIELD_SEP_SPACE,
382             },
383             'package-type' => {
384             name => 'Package-Type',
385             allowed => ALL_PKG,
386             },
387             'parent' => {
388             name => 'Parent',
389             allowed => CTRL_FILE_VENDOR,
390             },
391             'pre-depends' => {
392             name => 'Pre-Depends',
393             allowed => ALL_PKG,
394             separator => FIELD_SEP_COMMA,
395             dependency => 'normal',
396             dep_order => 1,
397             },
398             'priority' => {
399             name => 'Priority',
400             allowed => CTRL_INFO_SRC | CTRL_INDEX_SRC | ALL_PKG,
401             },
402             'protected' => {
403             name => 'Protected',
404             allowed => ALL_PKG,
405             },
406             'provides' => {
407             name => 'Provides',
408             allowed => ALL_PKG,
409             separator => FIELD_SEP_COMMA,
410             dependency => 'union',
411             dep_order => 9,
412             },
413             'recommends' => {
414             name => 'Recommends',
415             allowed => ALL_PKG,
416             separator => FIELD_SEP_COMMA,
417             dependency => 'normal',
418             dep_order => 3,
419             },
420             'replaces' => {
421             name => 'Replaces',
422             allowed => ALL_PKG,
423             separator => FIELD_SEP_COMMA,
424             dependency => 'union',
425             dep_order => 8,
426             },
427             'restrictions' => {
428             name => 'Restrictions',
429             allowed => CTRL_TESTS,
430             separator => FIELD_SEP_SPACE,
431             },
432             'rules-requires-root' => {
433             name => 'Rules-Requires-Root',
434             allowed => CTRL_INFO_SRC,
435             separator => FIELD_SEP_SPACE,
436             },
437             'section' => {
438             name => 'Section',
439             allowed => CTRL_INFO_SRC | CTRL_INDEX_SRC | ALL_PKG,
440             },
441             'sha1' => {
442             # XXX: Wrong capitalization due to historical reasons.
443             name => 'SHA1',
444             allowed => CTRL_INDEX_PKG | CTRL_REPO_RELEASE,
445             separator => FIELD_SEP_LINE | FIELD_SEP_SPACE,
446             },
447             'sha256' => {
448             # XXX: Wrong capitalization due to historical reasons.
449             name => 'SHA256',
450             allowed => CTRL_INDEX_PKG | CTRL_REPO_RELEASE,
451             separator => FIELD_SEP_LINE | FIELD_SEP_SPACE,
452             },
453             'size' => {
454             name => 'Size',
455             allowed => CTRL_INDEX_PKG,
456             separator => FIELD_SEP_LINE | FIELD_SEP_SPACE,
457             },
458             'source' => {
459             name => 'Source',
460             allowed => (ALL_PKG | ALL_SRC | ALL_CHANGES | CTRL_COPYRIGHT_HEADER | CTRL_FILE_BUILDINFO) &
461             (~(CTRL_INDEX_SRC | CTRL_INFO_PKG)),
462             },
463             'standards-version' => {
464             name => 'Standards-Version',
465             allowed => ALL_SRC,
466             },
467             'status' => {
468             name => 'Status',
469             allowed => CTRL_FILE_STATUS,
470             separator => FIELD_SEP_SPACE,
471             },
472             'subarchitecture' => {
473             name => 'Subarchitecture',
474             allowed => ALL_PKG,
475             },
476             'suite' => {
477             name => 'Suite',
478             allowed => CTRL_REPO_RELEASE,
479             },
480             'suggests' => {
481             name => 'Suggests',
482             allowed => ALL_PKG,
483             separator => FIELD_SEP_COMMA,
484             dependency => 'normal',
485             dep_order => 4,
486             },
487             'tag' => {
488             name => 'Tag',
489             allowed => ALL_PKG,
490             separator => FIELD_SEP_COMMA,
491             },
492             'task' => {
493             name => 'Task',
494             allowed => ALL_PKG,
495             },
496             'test-command' => {
497             name => 'Test-Command',
498             allowed => CTRL_TESTS,
499             },
500             'tests' => {
501             name => 'Tests',
502             allowed => CTRL_TESTS,
503             separator => FIELD_SEP_SPACE,
504             },
505             'tests-directory' => {
506             name => 'Tests-Directory',
507             allowed => CTRL_TESTS,
508             },
509             'testsuite' => {
510             name => 'Testsuite',
511             allowed => ALL_SRC,
512             separator => FIELD_SEP_COMMA,
513             },
514             'testsuite-triggers' => {
515             name => 'Testsuite-Triggers',
516             allowed => ALL_SRC,
517             separator => FIELD_SEP_COMMA,
518             },
519             'timestamp' => {
520             name => 'Timestamp',
521             allowed => CTRL_CHANGELOG,
522             },
523             'triggers-awaited' => {
524             name => 'Triggers-Awaited',
525             allowed => CTRL_FILE_STATUS,
526             separator => FIELD_SEP_SPACE,
527             },
528             'triggers-pending' => {
529             name => 'Triggers-Pending',
530             allowed => CTRL_FILE_STATUS,
531             separator => FIELD_SEP_SPACE,
532             },
533             'uploaders' => {
534             name => 'Uploaders',
535             allowed => ALL_SRC,
536             separator => FIELD_SEP_COMMA,
537             },
538             'upstream-name' => {
539             name => 'Upstream-Name',
540             allowed => CTRL_COPYRIGHT_HEADER,
541             },
542             'upstream-contact' => {
543             name => 'Upstream-Contact',
544             allowed => CTRL_COPYRIGHT_HEADER,
545             },
546             'urgency' => {
547             name => 'Urgency',
548             allowed => ALL_CHANGES,
549             },
550             'valid-until' => {
551             name => 'Valid-Until',
552             allowed => CTRL_REPO_RELEASE,
553             },
554             'vcs-browser' => {
555             name => 'Vcs-Browser',
556             allowed => ALL_SRC,
557             },
558             'vcs-arch' => {
559             name => 'Vcs-Arch',
560             allowed => ALL_SRC,
561             },
562             'vcs-bzr' => {
563             name => 'Vcs-Bzr',
564             allowed => ALL_SRC,
565             },
566             'vcs-cvs' => {
567             name => 'Vcs-Cvs',
568             allowed => ALL_SRC,
569             },
570             'vcs-darcs' => {
571             name => 'Vcs-Darcs',
572             allowed => ALL_SRC,
573             },
574             'vcs-git' => {
575             name => 'Vcs-Git',
576             allowed => ALL_SRC,
577             },
578             'vcs-hg' => {
579             name => 'Vcs-Hg',
580             allowed => ALL_SRC,
581             },
582             'vcs-mtn' => {
583             name => 'Vcs-Mtn',
584             allowed => ALL_SRC,
585             },
586             'vcs-svn' => {
587             name => 'Vcs-Svn',
588             allowed => ALL_SRC,
589             },
590             'vendor' => {
591             name => 'Vendor',
592             allowed => CTRL_FILE_VENDOR,
593             },
594             'vendor-url' => {
595             name => 'Vendor-Url',
596             allowed => CTRL_FILE_VENDOR,
597             },
598             'version' => {
599             name => 'Version',
600             allowed => (ALL_PKG | ALL_SRC | CTRL_FILE_BUILDINFO | ALL_CHANGES) &
601             (~(CTRL_INFO_SRC | CTRL_INFO_PKG)),
602             },
603             );
604              
605             my @src_dep_fields = qw(build-depends build-depends-arch build-depends-indep
606             build-conflicts build-conflicts-arch build-conflicts-indep);
607             my @bin_dep_fields = qw(pre-depends depends recommends suggests enhances
608             conflicts breaks replaces provides built-using);
609             my @src_checksums_fields = qw(checksums-md5 checksums-sha1 checksums-sha256);
610             my @bin_checksums_fields = qw(md5sum sha1 sha256);
611              
612             our %FIELD_ORDER = (
613             CTRL_PKG_DEB() => [
614             qw(package package-type source version built-using kernel-version
615             built-for-profiles auto-built-package architecture subarchitecture
616             installer-menu-item build-essential essential protected origin bugs
617             maintainer installed-size), @bin_dep_fields,
618             qw(section priority multi-arch homepage description tag task)
619             ],
620             CTRL_INDEX_PKG() => [
621             qw(package package-type source version built-using kernel-version
622             built-for-profiles auto-built-package architecture subarchitecture
623             installer-menu-item build-essential essential protected origin bugs
624             maintainer installed-size), @bin_dep_fields,
625             qw(filename size), @bin_checksums_fields,
626             qw(section priority multi-arch homepage description tag task)
627             ],
628             CTRL_PKG_SRC() => [
629             qw(format source binary architecture version origin maintainer
630             uploaders homepage description standards-version vcs-browser
631             vcs-arch vcs-bzr vcs-cvs vcs-darcs vcs-git vcs-hg vcs-mtn
632             vcs-svn testsuite testsuite-triggers), @src_dep_fields,
633             qw(package-list), @src_checksums_fields, qw(files)
634             ],
635             CTRL_INDEX_SRC() => [
636             qw(format package binary architecture version priority section origin
637             maintainer uploaders homepage description standards-version vcs-browser
638             vcs-arch vcs-bzr vcs-cvs vcs-darcs vcs-git vcs-hg vcs-mtn vcs-svn
639             testsuite testsuite-triggers), @src_dep_fields,
640             qw(package-list directory), @src_checksums_fields, qw(files)
641             ],
642             CTRL_FILE_BUILDINFO() => [
643             qw(format source binary architecture version binary-only-changes),
644             @src_checksums_fields,
645             qw(build-origin build-architecture build-kernel-version build-date
646             build-path build-tainted-by installed-build-depends environment),
647             ],
648             CTRL_FILE_CHANGES() => [
649             qw(format date source binary binary-only built-for-profiles architecture
650             version distribution urgency maintainer changed-by description
651             closes changes), @src_checksums_fields, qw(files)
652             ],
653             CTRL_CHANGELOG() => [
654             qw(source binary-only version distribution urgency maintainer
655             timestamp date closes changes)
656             ],
657             CTRL_FILE_STATUS() => [
658             # Same as fieldinfos in lib/dpkg/parse.c
659             qw(package essential protected status priority section installed-size
660             origin
661             maintainer bugs architecture multi-arch source version config-version
662             replaces provides depends pre-depends recommends suggests breaks
663             conflicts enhances conffiles description triggers-pending
664             triggers-awaited),
665             # These are allowed here, but not tracked by lib/dpkg/parse.c.
666             qw(auto-built-package build-essential built-for-profiles built-using
667             homepage installer-menu-item kernel-version package-type
668             subarchitecture tag task)
669             ],
670             CTRL_REPO_RELEASE() => [
671             qw(origin label suite codename changelogs date valid-until
672             architectures components description), @bin_checksums_fields
673             ],
674             CTRL_COPYRIGHT_HEADER() => [
675             qw(format upstream-name upstream-contact source disclaimer comment
676             license copyright)
677             ],
678             CTRL_COPYRIGHT_FILES() => [
679             qw(files copyright license comment)
680             ],
681             CTRL_COPYRIGHT_LICENSE() => [
682             qw(license comment)
683             ],
684             );
685              
686             =encoding utf8
687              
688             =head1 NAME
689              
690             Dpkg::Control::FieldsCore - manage (list of official) control fields
691              
692             =head1 DESCRIPTION
693              
694             The modules contains a list of fieldnames with associated meta-data explaining
695             in which type of control information they are allowed. The types are the
696             CTRL_* constants exported by Dpkg::Control.
697              
698             =head1 FUNCTIONS
699              
700             =over 4
701              
702             =item $f = field_capitalize($field_name)
703              
704             Returns the field name properly capitalized. All characters are lowercase,
705             except the first of each word (words are separated by a hyphen in field names).
706              
707             =cut
708              
709             sub field_capitalize($) {
710 5835     5835 1 41244 my $field = lc(shift);
711              
712             # Use known fields first.
713 5835 100       20902 return $FIELDS{$field}{name} if exists $FIELDS{$field};
714              
715             # Generic case
716 64         217 return join '-', map { ucfirst } split /-/, $field;
  136         485  
717             }
718              
719             =item field_is_official($fname)
720              
721             Returns true if the field is official and known.
722              
723             =cut
724              
725             sub field_is_official($) {
726 334     334 1 746 my $field = lc shift;
727              
728 334         1394 return exists $FIELDS{$field};
729             }
730              
731             =item field_is_allowed_in($fname, @types)
732              
733             Returns true (1) if the field $fname is allowed in all the types listed in
734             the list. Note that you can use type sets instead of individual types (ex:
735             CTRL_FILE_CHANGES | CTRL_CHANGELOG).
736              
737             field_allowed_in(A|B, C) returns true only if the field is allowed in C
738             and either A or B.
739              
740             Undef is returned for non-official fields.
741              
742             =cut
743              
744             sub field_is_allowed_in($@) {
745 2360     2360 1 513906 my ($field, @types) = @_;
746 2360         4906 $field = lc $field;
747              
748 2360 100       5963 return unless exists $FIELDS{$field};
749              
750 2338 50       4563 return 0 if not scalar(@types);
751 2338         3898 foreach my $type (@types) {
752 2676 50       6189 next if $type == CTRL_UNKNOWN; # Always allowed
753 2676 100       10685 return 0 unless $FIELDS{$field}{allowed} & $type;
754             }
755 725         2289 return 1;
756             }
757              
758             =item field_transfer_single($from, $to, $field)
759              
760             If appropriate, copy the value of the field named $field taken from the
761             $from Dpkg::Control object to the $to Dpkg::Control object.
762              
763             Official fields are copied only if the field is allowed in both types of
764             objects. Custom fields are treated in a specific manner. When the target
765             is not among CTRL_PKG_SRC, CTRL_PKG_DEB or CTRL_FILE_CHANGES, then they
766             are always copied as is (the X- prefix is kept). Otherwise they are not
767             copied except if the target object matches the target destination encoded
768             in the field name. The initial X denoting custom fields can be followed by
769             one or more letters among "S" (Source: corresponds to CTRL_PKG_SRC), "B"
770             (Binary: corresponds to CTRL_PKG_DEB) or "C" (Changes: corresponds to
771             CTRL_FILE_CHANGES).
772              
773             Returns undef if nothing has been copied or the name of the new field
774             added to $to otherwise.
775              
776             =cut
777              
778             sub field_transfer_single($$;$) {
779 360     360 1 809 my ($from, $to, $field) = @_;
780 360   33     1515 $field //= $_;
781 360         984 my ($from_type, $to_type) = ($from->get_type(), $to->get_type());
782 360         754 $field = field_capitalize($field);
783              
784 360 100       858 if (field_is_allowed_in($field, $from_type, $to_type)) {
    50          
    0          
785 338         757 $to->{$field} = $from->{$field};
786 338         1295 return $field;
787             } elsif ($field =~ /^X([SBC]*)-/i) {
788 22         64 my $dest = $1;
789 22 50 66     268 if (($dest =~ /B/i and $to_type == CTRL_PKG_DEB) or
    50 33        
      33        
      66        
      33        
      33        
      33        
790             ($dest =~ /S/i and $to_type == CTRL_PKG_SRC) or
791             ($dest =~ /C/i and $to_type == CTRL_FILE_CHANGES))
792             {
793 0         0 my $new = $field;
794 0         0 $new =~ s/^X([SBC]*)-//i;
795 0         0 $to->{$new} = $from->{$field};
796 0         0 return $new;
797             } elsif ($to_type != CTRL_PKG_DEB and
798             $to_type != CTRL_PKG_SRC and
799             $to_type != CTRL_FILE_CHANGES)
800             {
801 22         63 $to->{$field} = $from->{$field};
802 22         102 return $field;
803             }
804             } elsif (not field_is_allowed_in($field, $from_type)) {
805 0   0     0 warning(g_("unknown information field '%s' in input data in %s"),
806             $field, $from->get_option('name') || g_('control information'));
807             }
808 0         0 return;
809             }
810              
811             =item field_transfer_all($from, $to)
812              
813             Transfer all appropriate fields from $from to $to. Calls
814             field_transfer_single() on all fields available in $from.
815              
816             Returns the list of fields that have been added to $to.
817              
818             =cut
819              
820             sub field_transfer_all($$) {
821 0     0 1 0 my ($from, $to) = @_;
822 0         0 my (@res, $res);
823 0         0 foreach my $k (keys %$from) {
824 0         0 $res = field_transfer_single($from, $to, $k);
825 0 0 0     0 push @res, $res if $res and defined wantarray;
826             }
827 0         0 return @res;
828             }
829              
830             =item field_ordered_list($type)
831              
832             Returns an ordered list of fields for a given type of control information.
833             This list can be used to output the fields in a predictable order.
834             The list might be empty for types where the order does not matter much.
835              
836             =cut
837              
838             sub field_ordered_list($) {
839 787     787 1 9542 my $type = shift;
840              
841 787 100       1807 if (exists $FIELD_ORDER{$type}) {
842 770         1104 return map { $FIELDS{$_}{name} } @{$FIELD_ORDER{$type}};
  8395         17145  
  770         2073  
843             }
844 17         56 return ();
845             }
846              
847             =item field_list_src_dep()
848              
849             List of fields that contains dependencies-like information in a source
850             Debian package.
851              
852             =cut
853              
854             sub field_list_src_dep() {
855             my @list = map {
856             $FIELDS{$_}{name}
857 6         12 } sort {
858             $FIELDS{$a}{dep_order} <=> $FIELDS{$b}{dep_order}
859 10         18 } grep {
860 1     1 1 233 field_is_allowed_in($_, CTRL_PKG_SRC) and
861             exists $FIELDS{$_}{dependency}
862 112 100       164 } keys %FIELDS;
863 1         12 return @list;
864             }
865              
866             =item field_list_pkg_dep()
867              
868             List of fields that contains dependencies-like information in a binary
869             Debian package. The fields that express real dependencies are sorted from
870             the stronger to the weaker.
871              
872             =cut
873              
874             sub field_list_pkg_dep() {
875             my @list = map {
876             $FIELDS{$_}{name}
877 10         19 } sort {
878             $FIELDS{$a}{dep_order} <=> $FIELDS{$b}{dep_order}
879 21         33 } grep {
880 1     1 1 14 field_is_allowed_in($_, CTRL_PKG_DEB) and
881             exists $FIELDS{$_}{dependency}
882 112 100       174 } keys %FIELDS;
883 1         14 return @list;
884             }
885              
886             =item field_get_dep_type($field)
887              
888             Return the type of the dependency expressed by the given field. Can
889             either be "normal" for a real dependency field (Pre-Depends, Depends, ...)
890             or "union" for other relation fields sharing the same syntax (Conflicts,
891             Breaks, ...). Returns undef for fields which are not dependencies.
892              
893             =cut
894              
895             sub field_get_dep_type($) {
896 0     0 1 0 my $field = lc shift;
897              
898 0 0       0 return unless exists $FIELDS{$field};
899 0 0       0 return $FIELDS{$field}{dependency} if exists $FIELDS{$field}{dependency};
900 0         0 return;
901             }
902              
903             =item field_get_sep_type($field)
904              
905             Return the type of the field value separator. Can be one of FIELD_SEP_UNKNOWN,
906             FIELD_SEP_SPACE, FIELD_SEP_COMMA or FIELD_SEP_LINE.
907              
908             =cut
909              
910             sub field_get_sep_type($) {
911 0     0 1 0 my $field = lc shift;
912              
913 0 0       0 return $FIELDS{$field}{separator} if exists $FIELDS{$field}{separator};
914 0         0 return FIELD_SEP_UNKNOWN;
915             }
916              
917             =item field_register($field, $allowed_types, %opts)
918              
919             Register a new field as being allowed in control information of specified
920             types. %opts is optional
921              
922             =cut
923              
924             sub field_register($$;@) {
925 1     1 1 2 my ($field, $types, %opts) = @_;
926 1         2 $field = lc $field;
927 1         3 $FIELDS{$field} = {
928             name => field_capitalize($field),
929             allowed => $types,
930             %opts
931             };
932             }
933              
934             =item field_insert_after($type, $ref, @fields)
935              
936             Place field after another one ($ref) in output of control information of
937             type $type.
938              
939             =cut
940             sub field_insert_after($$@) {
941 2     2 1 5 my ($type, $field, @fields) = @_;
942 2 50       6 return 0 if not exists $FIELD_ORDER{$type};
943 2         3 ($field, @fields) = map { lc } ($field, @fields);
  4         11  
944 2         6 @{$FIELD_ORDER{$type}} = map {
945 29 100       49 ($_ eq $field) ? ($_, @fields) : $_
946 2         3 } @{$FIELD_ORDER{$type}};
  2         5  
947 2         7 return 1;
948             }
949              
950             =item field_insert_before($type, $ref, @fields)
951              
952             Place field before another one ($ref) in output of control information of
953             type $type.
954              
955             =cut
956             sub field_insert_before($$@) {
957 0     0 1   my ($type, $field, @fields) = @_;
958 0 0         return 0 if not exists $FIELD_ORDER{$type};
959 0           ($field, @fields) = map { lc } ($field, @fields);
  0            
960 0           @{$FIELD_ORDER{$type}} = map {
961 0 0         ($_ eq $field) ? (@fields, $_) : $_
962 0           } @{$FIELD_ORDER{$type}};
  0            
963 0           return 1;
964             }
965              
966             =back
967              
968             =head1 CHANGES
969              
970             =head2 Version 1.00 (dpkg 1.17.0)
971              
972             Mark the module as public.
973              
974             =cut
975              
976             1;