FieldsCore.pm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. # Copyright © 2007-2009 Raphaël Hertzog <hertzog@debian.org>
  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 <https://www.gnu.org/licenses/>.
  15. package Dpkg::Control::FieldsCore;
  16. use strict;
  17. use warnings;
  18. our $VERSION = '1.00';
  19. our @EXPORT = qw(
  20. field_capitalize
  21. field_is_official
  22. field_is_allowed_in
  23. field_transfer_single
  24. field_transfer_all
  25. field_list_src_dep
  26. field_list_pkg_dep
  27. field_get_dep_type
  28. field_get_sep_type
  29. field_ordered_list
  30. field_register
  31. field_insert_after
  32. field_insert_before
  33. FIELD_SEP_UNKNOWN
  34. FIELD_SEP_SPACE
  35. FIELD_SEP_COMMA
  36. FIELD_SEP_LINE
  37. );
  38. use Exporter qw(import);
  39. use Dpkg::Gettext;
  40. use Dpkg::ErrorHandling;
  41. use Dpkg::Control::Types;
  42. use Dpkg::Checksums;
  43. use constant {
  44. ALL_PKG => CTRL_INFO_PKG | CTRL_INDEX_PKG | CTRL_PKG_DEB | CTRL_FILE_STATUS,
  45. ALL_SRC => CTRL_INFO_SRC | CTRL_INDEX_SRC | CTRL_PKG_SRC,
  46. ALL_CHANGES => CTRL_FILE_CHANGES | CTRL_CHANGELOG,
  47. ALL_COPYRIGHT => CTRL_COPYRIGHT_HEADER | CTRL_COPYRIGHT_FILES | CTRL_COPYRIGHT_LICENSE,
  48. };
  49. use constant {
  50. FIELD_SEP_UNKNOWN => 0,
  51. FIELD_SEP_SPACE => 1,
  52. FIELD_SEP_COMMA => 2,
  53. FIELD_SEP_LINE => 4,
  54. };
  55. # The canonical list of fields
  56. # Note that fields used only in dpkg's available file are not listed
  57. # Deprecated fields of dpkg's status file are also not listed
  58. our %FIELDS = (
  59. 'Architecture' => {
  60. allowed => (ALL_PKG | ALL_SRC | CTRL_FILE_CHANGES) & (~CTRL_INFO_SRC),
  61. separator => FIELD_SEP_SPACE,
  62. },
  63. 'Architectures' => {
  64. allowed => CTRL_REPO_RELEASE,
  65. separator => FIELD_SEP_SPACE,
  66. },
  67. 'Binary' => {
  68. allowed => CTRL_PKG_SRC | CTRL_FILE_CHANGES,
  69. # XXX: This field values are separated either by space or comma
  70. # depending on the context.
  71. separator => FIELD_SEP_SPACE | FIELD_SEP_COMMA,
  72. },
  73. 'Binary-Only' => {
  74. allowed => ALL_CHANGES,
  75. },
  76. 'Breaks' => {
  77. allowed => ALL_PKG,
  78. separator => FIELD_SEP_COMMA,
  79. dependency => 'union',
  80. dep_order => 7,
  81. },
  82. 'Bugs' => {
  83. allowed => (ALL_PKG | CTRL_INFO_SRC | CTRL_FILE_VENDOR) & (~CTRL_INFO_PKG),
  84. },
  85. 'Build-Conflicts' => {
  86. allowed => ALL_SRC,
  87. separator => FIELD_SEP_COMMA,
  88. dependency => 'union',
  89. dep_order => 4,
  90. },
  91. 'Build-Conflicts-Arch' => {
  92. allowed => ALL_SRC,
  93. separator => FIELD_SEP_COMMA,
  94. dependency => 'union',
  95. dep_order => 5,
  96. },
  97. 'Build-Conflicts-Indep' => {
  98. allowed => ALL_SRC,
  99. separator => FIELD_SEP_COMMA,
  100. dependency => 'union',
  101. dep_order => 6,
  102. },
  103. 'Build-Depends' => {
  104. allowed => ALL_SRC,
  105. separator => FIELD_SEP_COMMA,
  106. dependency => 'normal',
  107. dep_order => 1,
  108. },
  109. 'Build-Depends-Arch' => {
  110. allowed => ALL_SRC,
  111. separator => FIELD_SEP_COMMA,
  112. dependency => 'normal',
  113. dep_order => 2,
  114. },
  115. 'Build-Depends-Indep' => {
  116. allowed => ALL_SRC,
  117. separator => FIELD_SEP_COMMA,
  118. dependency => 'normal',
  119. dep_order => 3,
  120. },
  121. 'Build-Essential' => {
  122. allowed => ALL_PKG,
  123. },
  124. 'Build-Profiles' => {
  125. allowed => CTRL_INFO_PKG,
  126. separator => FIELD_SEP_SPACE,
  127. },
  128. 'Built-For-Profiles' => {
  129. allowed => ALL_PKG | CTRL_FILE_CHANGES,
  130. separator => FIELD_SEP_SPACE,
  131. },
  132. 'Built-Using' => {
  133. allowed => ALL_PKG,
  134. separator => FIELD_SEP_COMMA,
  135. dependency => 'union',
  136. dep_order => 10,
  137. },
  138. 'Changed-By' => {
  139. allowed => CTRL_FILE_CHANGES,
  140. },
  141. 'Changelogs' => {
  142. allowed => CTRL_REPO_RELEASE,
  143. },
  144. 'Changes' => {
  145. allowed => ALL_CHANGES,
  146. },
  147. 'Closes' => {
  148. allowed => ALL_CHANGES,
  149. separator => FIELD_SEP_SPACE,
  150. },
  151. 'Codename' => {
  152. allowed => CTRL_REPO_RELEASE,
  153. },
  154. 'Comment' => {
  155. allowed => ALL_COPYRIGHT,
  156. },
  157. 'Components' => {
  158. allowed => CTRL_REPO_RELEASE,
  159. separator => FIELD_SEP_SPACE,
  160. },
  161. 'Conffiles' => {
  162. allowed => CTRL_FILE_STATUS,
  163. separator => FIELD_SEP_LINE | FIELD_SEP_SPACE,
  164. },
  165. 'Config-Version' => {
  166. allowed => CTRL_FILE_STATUS,
  167. },
  168. 'Conflicts' => {
  169. allowed => ALL_PKG,
  170. separator => FIELD_SEP_COMMA,
  171. dependency => 'union',
  172. dep_order => 6,
  173. },
  174. 'Copyright' => {
  175. allowed => CTRL_COPYRIGHT_HEADER | CTRL_COPYRIGHT_FILES,
  176. },
  177. 'Date' => {
  178. allowed => ALL_CHANGES | CTRL_REPO_RELEASE,
  179. },
  180. 'Depends' => {
  181. allowed => ALL_PKG,
  182. separator => FIELD_SEP_COMMA,
  183. dependency => 'normal',
  184. dep_order => 2,
  185. },
  186. 'Description' => {
  187. allowed => ALL_PKG | CTRL_FILE_CHANGES | CTRL_REPO_RELEASE,
  188. },
  189. 'Disclaimer' => {
  190. allowed => CTRL_COPYRIGHT_HEADER,
  191. },
  192. 'Directory' => {
  193. allowed => CTRL_INDEX_SRC,
  194. },
  195. 'Distribution' => {
  196. allowed => ALL_CHANGES,
  197. },
  198. 'Enhances' => {
  199. allowed => ALL_PKG,
  200. separator => FIELD_SEP_COMMA,
  201. dependency => 'union',
  202. dep_order => 5,
  203. },
  204. 'Essential' => {
  205. allowed => ALL_PKG,
  206. },
  207. 'Filename' => {
  208. allowed => CTRL_INDEX_PKG,
  209. separator => FIELD_SEP_LINE | FIELD_SEP_SPACE,
  210. },
  211. 'Files' => {
  212. allowed => CTRL_PKG_SRC | CTRL_FILE_CHANGES | CTRL_COPYRIGHT_FILES,
  213. separator => FIELD_SEP_LINE | FIELD_SEP_SPACE,
  214. },
  215. 'Format' => {
  216. allowed => CTRL_PKG_SRC | CTRL_FILE_CHANGES | CTRL_COPYRIGHT_HEADER,
  217. },
  218. 'Homepage' => {
  219. allowed => ALL_SRC | ALL_PKG,
  220. },
  221. 'Installed-Size' => {
  222. allowed => ALL_PKG & ~CTRL_INFO_PKG,
  223. },
  224. 'Installer-Menu-Item' => {
  225. allowed => ALL_PKG,
  226. },
  227. 'Kernel-Version' => {
  228. allowed => ALL_PKG,
  229. },
  230. 'Label' => {
  231. allowed => CTRL_REPO_RELEASE,
  232. },
  233. 'License' => {
  234. allowed => ALL_COPYRIGHT,
  235. },
  236. 'Origin' => {
  237. allowed => (ALL_PKG | ALL_SRC | CTRL_REPO_RELEASE) & (~CTRL_INFO_PKG),
  238. },
  239. 'Maintainer' => {
  240. allowed => CTRL_PKG_DEB| CTRL_FILE_STATUS | ALL_SRC | ALL_CHANGES,
  241. },
  242. 'Multi-Arch' => {
  243. allowed => ALL_PKG,
  244. },
  245. 'Package' => {
  246. allowed => ALL_PKG,
  247. },
  248. 'Package-List' => {
  249. allowed => ALL_SRC & ~CTRL_INFO_SRC,
  250. separator => FIELD_SEP_LINE | FIELD_SEP_SPACE,
  251. },
  252. 'Package-Type' => {
  253. allowed => ALL_PKG,
  254. },
  255. 'Parent' => {
  256. allowed => CTRL_FILE_VENDOR,
  257. },
  258. 'Pre-Depends' => {
  259. allowed => ALL_PKG,
  260. separator => FIELD_SEP_COMMA,
  261. dependency => 'normal',
  262. dep_order => 1,
  263. },
  264. 'Priority' => {
  265. allowed => CTRL_INFO_SRC | CTRL_INDEX_SRC | ALL_PKG,
  266. },
  267. 'Provides' => {
  268. allowed => ALL_PKG,
  269. separator => FIELD_SEP_COMMA,
  270. dependency => 'union',
  271. dep_order => 9,
  272. },
  273. 'Recommends' => {
  274. allowed => ALL_PKG,
  275. separator => FIELD_SEP_COMMA,
  276. dependency => 'normal',
  277. dep_order => 3,
  278. },
  279. 'Replaces' => {
  280. allowed => ALL_PKG,
  281. separator => FIELD_SEP_COMMA,
  282. dependency => 'union',
  283. dep_order => 8,
  284. },
  285. 'Section' => {
  286. allowed => CTRL_INFO_SRC | CTRL_INDEX_SRC | ALL_PKG,
  287. },
  288. 'Size' => {
  289. allowed => CTRL_INDEX_PKG,
  290. separator => FIELD_SEP_LINE | FIELD_SEP_SPACE,
  291. },
  292. 'Source' => {
  293. allowed => (ALL_PKG | ALL_SRC | ALL_CHANGES | CTRL_COPYRIGHT_HEADER) &
  294. (~(CTRL_INDEX_SRC | CTRL_INFO_PKG)),
  295. },
  296. 'Standards-Version' => {
  297. allowed => ALL_SRC,
  298. },
  299. 'Status' => {
  300. allowed => CTRL_FILE_STATUS,
  301. separator => FIELD_SEP_SPACE,
  302. },
  303. 'Subarchitecture' => {
  304. allowed => ALL_PKG,
  305. },
  306. 'Suite' => {
  307. allowed => CTRL_REPO_RELEASE,
  308. },
  309. 'Suggests' => {
  310. allowed => ALL_PKG,
  311. separator => FIELD_SEP_COMMA,
  312. dependency => 'normal',
  313. dep_order => 4,
  314. },
  315. 'Tag' => {
  316. allowed => ALL_PKG,
  317. separator => FIELD_SEP_COMMA,
  318. },
  319. 'Task' => {
  320. allowed => ALL_PKG,
  321. },
  322. 'Testsuite' => {
  323. allowed => ALL_SRC,
  324. separator => FIELD_SEP_COMMA,
  325. },
  326. 'Triggers-Awaited' => {
  327. allowed => CTRL_FILE_STATUS,
  328. separator => FIELD_SEP_SPACE,
  329. },
  330. 'Triggers-Pending' => {
  331. allowed => CTRL_FILE_STATUS,
  332. separator => FIELD_SEP_SPACE,
  333. },
  334. 'Uploaders' => {
  335. allowed => ALL_SRC,
  336. separator => FIELD_SEP_COMMA,
  337. },
  338. 'Upstream-Name' => {
  339. allowed => CTRL_COPYRIGHT_HEADER,
  340. },
  341. 'Upstream-Contact' => {
  342. allowed => CTRL_COPYRIGHT_HEADER,
  343. },
  344. 'Urgency' => {
  345. allowed => ALL_CHANGES,
  346. },
  347. 'Valid-Until' => {
  348. allowed => CTRL_REPO_RELEASE,
  349. },
  350. 'Vcs-Browser' => {
  351. allowed => ALL_SRC,
  352. },
  353. 'Vcs-Arch' => {
  354. allowed => ALL_SRC,
  355. },
  356. 'Vcs-Bzr' => {
  357. allowed => ALL_SRC,
  358. },
  359. 'Vcs-Cvs' => {
  360. allowed => ALL_SRC,
  361. },
  362. 'Vcs-Darcs' => {
  363. allowed => ALL_SRC,
  364. },
  365. 'Vcs-Git' => {
  366. allowed => ALL_SRC,
  367. },
  368. 'Vcs-Hg' => {
  369. allowed => ALL_SRC,
  370. },
  371. 'Vcs-Mtn' => {
  372. allowed => ALL_SRC,
  373. },
  374. 'Vcs-Svn' => {
  375. allowed => ALL_SRC,
  376. },
  377. 'Vendor' => {
  378. allowed => CTRL_FILE_VENDOR,
  379. },
  380. 'Vendor-Url' => {
  381. allowed => CTRL_FILE_VENDOR,
  382. },
  383. 'Version' => {
  384. allowed => (ALL_PKG | ALL_SRC | ALL_CHANGES) &
  385. (~(CTRL_INFO_SRC | CTRL_INFO_PKG)),
  386. },
  387. );
  388. my @checksum_fields = map { &field_capitalize("Checksums-$_") } checksums_get_list();
  389. my @sum_fields = map { $_ eq 'md5' ? 'MD5sum' : &field_capitalize($_) }
  390. checksums_get_list();
  391. &field_register($_, CTRL_PKG_SRC | CTRL_FILE_CHANGES) foreach @checksum_fields;
  392. &field_register($_, CTRL_INDEX_PKG | CTRL_REPO_RELEASE,
  393. separator => FIELD_SEP_LINE | FIELD_SEP_SPACE) foreach @sum_fields;
  394. our %FIELD_ORDER = (
  395. CTRL_PKG_DEB() => [
  396. qw(Package Package-Type Source Version Built-Using Kernel-Version
  397. Built-For-Profiles Architecture Subarchitecture
  398. Installer-Menu-Item Essential Origin Bugs
  399. Maintainer Installed-Size), &field_list_pkg_dep(),
  400. qw(Section Priority Multi-Arch Homepage Description Tag Task)
  401. ],
  402. CTRL_PKG_SRC() => [
  403. qw(Format Source Binary Architecture Version Origin Maintainer
  404. Uploaders Homepage Standards-Version Vcs-Browser
  405. Vcs-Arch Vcs-Bzr Vcs-Cvs Vcs-Darcs Vcs-Git Vcs-Hg Vcs-Mtn
  406. Vcs-Svn Testsuite), &field_list_src_dep(), qw(Package-List),
  407. @checksum_fields, qw(Files)
  408. ],
  409. CTRL_FILE_CHANGES() => [
  410. qw(Format Date Source Binary Binary-Only Built-For-Profiles Architecture
  411. Version Distribution Urgency Maintainer Changed-By Description
  412. Closes Changes),
  413. @checksum_fields, qw(Files)
  414. ],
  415. CTRL_CHANGELOG() => [
  416. qw(Source Binary-Only Version Distribution Urgency Maintainer
  417. Date Closes Changes)
  418. ],
  419. CTRL_FILE_STATUS() => [ # Same as fieldinfos in lib/dpkg/parse.c
  420. qw(Package Essential Status Priority Section Installed-Size Origin
  421. Maintainer Bugs Architecture Multi-Arch Source Version Config-Version
  422. Replaces Provides Depends Pre-Depends Recommends Suggests Breaks
  423. Conflicts Enhances Conffiles Description Triggers-Pending
  424. Triggers-Awaited)
  425. ],
  426. CTRL_REPO_RELEASE() => [
  427. qw(Origin Label Suite Codename Changelogs Date Valid-Until
  428. Architectures Components Description), @sum_fields
  429. ],
  430. CTRL_COPYRIGHT_HEADER() => [
  431. qw(Format Upstream-Name Upstream-Contact Source Disclaimer Comment
  432. License Copyright)
  433. ],
  434. CTRL_COPYRIGHT_FILES() => [
  435. qw(Files Copyright License Comment)
  436. ],
  437. CTRL_COPYRIGHT_LICENSE() => [
  438. qw(License Comment)
  439. ],
  440. );
  441. # Order for CTRL_INDEX_PKG is derived from CTRL_PKG_DEB
  442. $FIELD_ORDER{CTRL_INDEX_PKG()} = [ @{$FIELD_ORDER{CTRL_PKG_DEB()}} ];
  443. &field_insert_before(CTRL_INDEX_PKG, 'Section', 'Filename', 'Size', @sum_fields);
  444. # Order for CTRL_INDEX_SRC is derived from CTRL_PKG_SRC
  445. $FIELD_ORDER{CTRL_INDEX_SRC()} = [ @{$FIELD_ORDER{CTRL_PKG_SRC()}} ];
  446. @{$FIELD_ORDER{CTRL_INDEX_SRC()}} = map { $_ eq 'Source' ? 'Package' : $_ }
  447. @{$FIELD_ORDER{CTRL_PKG_SRC()}};
  448. &field_insert_after(CTRL_INDEX_SRC, 'Version', 'Priority', 'Section');
  449. &field_insert_before(CTRL_INDEX_SRC, 'Checksums-Md5', 'Directory');
  450. =encoding utf8
  451. =head1 NAME
  452. Dpkg::Control::FieldsCore - manage (list of official) control fields
  453. =head1 DESCRIPTION
  454. The modules contains a list of fieldnames with associated meta-data explaining
  455. in which type of control information they are allowed. The types are the
  456. CTRL_* constants exported by Dpkg::Control.
  457. =head1 FUNCTIONS
  458. =over 4
  459. =item $f = field_capitalize($field_name)
  460. Returns the field name properly capitalized. All characters are lowercase,
  461. except the first of each word (words are separated by a hyphen in field names).
  462. =cut
  463. sub field_capitalize($) {
  464. my $field = lc(shift);
  465. # Some special cases due to history
  466. return 'MD5sum' if $field eq 'md5sum';
  467. return uc($field) if checksums_is_supported($field);
  468. # Generic case
  469. return join '-', map { ucfirst } split /-/, $field;
  470. }
  471. =item field_is_official($fname)
  472. Returns true if the field is official and known.
  473. =cut
  474. sub field_is_official($) {
  475. my $field = field_capitalize(shift);
  476. return exists $FIELDS{$field};
  477. }
  478. =item field_is_allowed_in($fname, @types)
  479. Returns true (1) if the field $fname is allowed in all the types listed in
  480. the list. Note that you can use type sets instead of individual types (ex:
  481. CTRL_FILE_CHANGES | CTRL_CHANGELOG).
  482. field_allowed_in(A|B, C) returns true only if the field is allowed in C
  483. and either A or B.
  484. Undef is returned for non-official fields.
  485. =cut
  486. sub field_is_allowed_in($@) {
  487. my ($field, @types) = @_;
  488. $field = field_capitalize($field);
  489. return unless field_is_official($field);
  490. return 0 if not scalar(@types);
  491. foreach my $type (@types) {
  492. next if $type == CTRL_UNKNOWN; # Always allowed
  493. return 0 unless $FIELDS{$field}{allowed} & $type;
  494. }
  495. return 1;
  496. }
  497. =item field_transfer_single($from, $to, $field)
  498. If appropriate, copy the value of the field named $field taken from the
  499. $from Dpkg::Control object to the $to Dpkg::Control object.
  500. Official fields are copied only if the field is allowed in both types of
  501. objects. Custom fields are treated in a specific manner. When the target
  502. is not among CTRL_PKG_SRC, CTRL_PKG_DEB or CTRL_FILE_CHANGES, then they
  503. are alway copied as is (the X- prefix is kept). Otherwise they are not
  504. copied except if the target object matches the target destination encoded
  505. in the field name. The initial X denoting custom fields can be followed by
  506. one or more letters among "S" (Source: corresponds to CTRL_PKG_SRC), "B"
  507. (Binary: corresponds to CTRL_PKG_DEB) or "C" (Changes: corresponds to
  508. CTRL_FILE_CHANGES).
  509. Returns undef if nothing has been copied or the name of the new field
  510. added to $to otherwise.
  511. =cut
  512. sub field_transfer_single($$;$) {
  513. my ($from, $to, $field) = @_;
  514. $field //= $_;
  515. my ($from_type, $to_type) = ($from->get_type(), $to->get_type());
  516. $field = field_capitalize($field);
  517. if (field_is_allowed_in($field, $from_type, $to_type)) {
  518. $to->{$field} = $from->{$field};
  519. return $field;
  520. } elsif ($field =~ /^X([SBC]*)-/i) {
  521. my $dest = $1;
  522. if (($dest =~ /B/i and $to_type == CTRL_PKG_DEB) or
  523. ($dest =~ /S/i and $to_type == CTRL_PKG_SRC) or
  524. ($dest =~ /C/i and $to_type == CTRL_FILE_CHANGES))
  525. {
  526. my $new = $field;
  527. $new =~ s/^X([SBC]*)-//i;
  528. $to->{$new} = $from->{$field};
  529. return $new;
  530. } elsif ($to_type != CTRL_PKG_DEB and
  531. $to_type != CTRL_PKG_SRC and
  532. $to_type != CTRL_FILE_CHANGES)
  533. {
  534. $to->{$field} = $from->{$field};
  535. return $field;
  536. }
  537. } elsif (not field_is_allowed_in($field, $from_type)) {
  538. warning(g_("unknown information field '%s' in input data in %s"),
  539. $field, $from->get_option('name') || g_('control information'));
  540. }
  541. return;
  542. }
  543. =item field_transfer_all($from, $to)
  544. Transfer all appropriate fields from $from to $to. Calls
  545. field_transfer_single() on all fields available in $from.
  546. Returns the list of fields that have been added to $to.
  547. =cut
  548. sub field_transfer_all($$) {
  549. my ($from, $to) = @_;
  550. my (@res, $res);
  551. foreach my $k (keys %$from) {
  552. $res = field_transfer_single($from, $to, $k);
  553. push @res, $res if $res and defined wantarray;
  554. }
  555. return @res;
  556. }
  557. =item field_ordered_list($type)
  558. Returns an ordered list of fields for a given type of control information.
  559. This list can be used to output the fields in a predictable order.
  560. The list might be empty for types where the order does not matter much.
  561. =cut
  562. sub field_ordered_list($) {
  563. my $type = shift;
  564. return @{$FIELD_ORDER{$type}} if exists $FIELD_ORDER{$type};
  565. return ();
  566. }
  567. =item field_list_src_dep()
  568. List of fields that contains dependencies-like information in a source
  569. Debian package.
  570. =cut
  571. sub field_list_src_dep() {
  572. my @list = sort {
  573. $FIELDS{$a}{dep_order} <=> $FIELDS{$b}{dep_order}
  574. } grep {
  575. field_is_allowed_in($_, CTRL_PKG_SRC) and
  576. exists $FIELDS{$_}{dependency}
  577. } keys %FIELDS;
  578. return @list;
  579. }
  580. =item field_list_pkg_dep()
  581. List of fields that contains dependencies-like information in a binary
  582. Debian package. The fields that express real dependencies are sorted from
  583. the stronger to the weaker.
  584. =cut
  585. sub field_list_pkg_dep() {
  586. my @keys = keys %FIELDS;
  587. my @list = sort {
  588. $FIELDS{$a}{dep_order} <=> $FIELDS{$b}{dep_order}
  589. } grep {
  590. field_is_allowed_in($_, CTRL_PKG_DEB) and
  591. exists $FIELDS{$_}{dependency}
  592. } @keys;
  593. return @list;
  594. }
  595. =item field_get_dep_type($field)
  596. Return the type of the dependency expressed by the given field. Can
  597. either be "normal" for a real dependency field (Pre-Depends, Depends, ...)
  598. or "union" for other relation fields sharing the same syntax (Conflicts,
  599. Breaks, ...). Returns undef for fields which are not dependencies.
  600. =cut
  601. sub field_get_dep_type($) {
  602. my $field = field_capitalize(shift);
  603. return unless field_is_official($field);
  604. return $FIELDS{$field}{dependency} if exists $FIELDS{$field}{dependency};
  605. return;
  606. }
  607. =item field_get_sep_type($field)
  608. Return the type of the field value separator. Can be one of FIELD_SEP_UNKNOWN,
  609. FIELD_SEP_SPACE, FIELD_SEP_COMMA or FIELD_SEP_LINE.
  610. =cut
  611. sub field_get_sep_type($) {
  612. my $field = field_capitalize(shift);
  613. return $FIELDS{$field}{separator} if exists $FIELDS{$field}{separator};
  614. return FIELD_SEP_UNKNOWN;
  615. }
  616. =item field_register($field, $allowed_types, %opts)
  617. Register a new field as being allowed in control information of specified
  618. types. %opts is optional
  619. =cut
  620. sub field_register($$;@) {
  621. my ($field, $types, %opts) = @_;
  622. $field = field_capitalize($field);
  623. $FIELDS{$field} = {
  624. allowed => $types,
  625. %opts
  626. };
  627. }
  628. =item field_insert_after($type, $ref, @fields)
  629. Place field after another one ($ref) in output of control information of
  630. type $type.
  631. =cut
  632. sub field_insert_after($$@) {
  633. my ($type, $field, @fields) = @_;
  634. return 0 if not exists $FIELD_ORDER{$type};
  635. ($field, @fields) = map { field_capitalize($_) } ($field, @fields);
  636. @{$FIELD_ORDER{$type}} = map {
  637. ($_ eq $field) ? ($_, @fields) : $_
  638. } @{$FIELD_ORDER{$type}};
  639. return 1;
  640. }
  641. =item field_insert_before($type, $ref, @fields)
  642. Place field before another one ($ref) in output of control information of
  643. type $type.
  644. =cut
  645. sub field_insert_before($$@) {
  646. my ($type, $field, @fields) = @_;
  647. return 0 if not exists $FIELD_ORDER{$type};
  648. ($field, @fields) = map { field_capitalize($_) } ($field, @fields);
  649. @{$FIELD_ORDER{$type}} = map {
  650. ($_ eq $field) ? (@fields, $_) : $_
  651. } @{$FIELD_ORDER{$type}};
  652. return 1;
  653. }
  654. =back
  655. =head1 CHANGES
  656. =head2 Version 1.00 (dpkg 1.17.0)
  657. Mark the module as public.
  658. =head1 AUTHOR
  659. Raphaël Hertzog <hertzog@debian.org>.
  660. =cut
  661. 1;