FieldsCore.pm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. 'Timestamp' => {
  327. allowed => CTRL_CHANGELOG,
  328. },
  329. 'Triggers-Awaited' => {
  330. allowed => CTRL_FILE_STATUS,
  331. separator => FIELD_SEP_SPACE,
  332. },
  333. 'Triggers-Pending' => {
  334. allowed => CTRL_FILE_STATUS,
  335. separator => FIELD_SEP_SPACE,
  336. },
  337. 'Uploaders' => {
  338. allowed => ALL_SRC,
  339. separator => FIELD_SEP_COMMA,
  340. },
  341. 'Upstream-Name' => {
  342. allowed => CTRL_COPYRIGHT_HEADER,
  343. },
  344. 'Upstream-Contact' => {
  345. allowed => CTRL_COPYRIGHT_HEADER,
  346. },
  347. 'Urgency' => {
  348. allowed => ALL_CHANGES,
  349. },
  350. 'Valid-Until' => {
  351. allowed => CTRL_REPO_RELEASE,
  352. },
  353. 'Vcs-Browser' => {
  354. allowed => ALL_SRC,
  355. },
  356. 'Vcs-Arch' => {
  357. allowed => ALL_SRC,
  358. },
  359. 'Vcs-Bzr' => {
  360. allowed => ALL_SRC,
  361. },
  362. 'Vcs-Cvs' => {
  363. allowed => ALL_SRC,
  364. },
  365. 'Vcs-Darcs' => {
  366. allowed => ALL_SRC,
  367. },
  368. 'Vcs-Git' => {
  369. allowed => ALL_SRC,
  370. },
  371. 'Vcs-Hg' => {
  372. allowed => ALL_SRC,
  373. },
  374. 'Vcs-Mtn' => {
  375. allowed => ALL_SRC,
  376. },
  377. 'Vcs-Svn' => {
  378. allowed => ALL_SRC,
  379. },
  380. 'Vendor' => {
  381. allowed => CTRL_FILE_VENDOR,
  382. },
  383. 'Vendor-Url' => {
  384. allowed => CTRL_FILE_VENDOR,
  385. },
  386. 'Version' => {
  387. allowed => (ALL_PKG | ALL_SRC | ALL_CHANGES) &
  388. (~(CTRL_INFO_SRC | CTRL_INFO_PKG)),
  389. },
  390. );
  391. my @checksum_fields = map { &field_capitalize("Checksums-$_") } checksums_get_list();
  392. my @sum_fields = map { $_ eq 'md5' ? 'MD5sum' : &field_capitalize($_) }
  393. checksums_get_list();
  394. &field_register($_, CTRL_PKG_SRC | CTRL_FILE_CHANGES) foreach @checksum_fields;
  395. &field_register($_, CTRL_INDEX_PKG | CTRL_REPO_RELEASE,
  396. separator => FIELD_SEP_LINE | FIELD_SEP_SPACE) foreach @sum_fields;
  397. our %FIELD_ORDER = (
  398. CTRL_PKG_DEB() => [
  399. qw(Package Package-Type Source Version Built-Using Kernel-Version
  400. Built-For-Profiles Architecture Subarchitecture
  401. Installer-Menu-Item Essential Origin Bugs
  402. Maintainer Installed-Size), &field_list_pkg_dep(),
  403. qw(Section Priority Multi-Arch Homepage Description Tag Task)
  404. ],
  405. CTRL_PKG_SRC() => [
  406. qw(Format Source Binary Architecture Version Origin Maintainer
  407. Uploaders Homepage Standards-Version Vcs-Browser
  408. Vcs-Arch Vcs-Bzr Vcs-Cvs Vcs-Darcs Vcs-Git Vcs-Hg Vcs-Mtn
  409. Vcs-Svn Testsuite), &field_list_src_dep(), qw(Package-List),
  410. @checksum_fields, qw(Files)
  411. ],
  412. CTRL_FILE_CHANGES() => [
  413. qw(Format Date Source Binary Binary-Only Built-For-Profiles Architecture
  414. Version Distribution Urgency Maintainer Changed-By Description
  415. Closes Changes),
  416. @checksum_fields, qw(Files)
  417. ],
  418. CTRL_CHANGELOG() => [
  419. qw(Source Binary-Only Version Distribution Urgency Maintainer
  420. Timestamp Date Closes Changes)
  421. ],
  422. CTRL_FILE_STATUS() => [ # Same as fieldinfos in lib/dpkg/parse.c
  423. qw(Package Essential Status Priority Section Installed-Size Origin
  424. Maintainer Bugs Architecture Multi-Arch Source Version Config-Version
  425. Replaces Provides Depends Pre-Depends Recommends Suggests Breaks
  426. Conflicts Enhances Conffiles Description Triggers-Pending
  427. Triggers-Awaited)
  428. ],
  429. CTRL_REPO_RELEASE() => [
  430. qw(Origin Label Suite Codename Changelogs Date Valid-Until
  431. Architectures Components Description), @sum_fields
  432. ],
  433. CTRL_COPYRIGHT_HEADER() => [
  434. qw(Format Upstream-Name Upstream-Contact Source Disclaimer Comment
  435. License Copyright)
  436. ],
  437. CTRL_COPYRIGHT_FILES() => [
  438. qw(Files Copyright License Comment)
  439. ],
  440. CTRL_COPYRIGHT_LICENSE() => [
  441. qw(License Comment)
  442. ],
  443. );
  444. # Order for CTRL_INDEX_PKG is derived from CTRL_PKG_DEB
  445. $FIELD_ORDER{CTRL_INDEX_PKG()} = [ @{$FIELD_ORDER{CTRL_PKG_DEB()}} ];
  446. &field_insert_before(CTRL_INDEX_PKG, 'Section', 'Filename', 'Size', @sum_fields);
  447. # Order for CTRL_INDEX_SRC is derived from CTRL_PKG_SRC
  448. $FIELD_ORDER{CTRL_INDEX_SRC()} = [ @{$FIELD_ORDER{CTRL_PKG_SRC()}} ];
  449. @{$FIELD_ORDER{CTRL_INDEX_SRC()}} = map { $_ eq 'Source' ? 'Package' : $_ }
  450. @{$FIELD_ORDER{CTRL_PKG_SRC()}};
  451. &field_insert_after(CTRL_INDEX_SRC, 'Version', 'Priority', 'Section');
  452. &field_insert_before(CTRL_INDEX_SRC, 'Checksums-Md5', 'Directory');
  453. =encoding utf8
  454. =head1 NAME
  455. Dpkg::Control::FieldsCore - manage (list of official) control fields
  456. =head1 DESCRIPTION
  457. The modules contains a list of fieldnames with associated meta-data explaining
  458. in which type of control information they are allowed. The types are the
  459. CTRL_* constants exported by Dpkg::Control.
  460. =head1 FUNCTIONS
  461. =over 4
  462. =item $f = field_capitalize($field_name)
  463. Returns the field name properly capitalized. All characters are lowercase,
  464. except the first of each word (words are separated by a hyphen in field names).
  465. =cut
  466. sub field_capitalize($) {
  467. my $field = lc(shift);
  468. # Some special cases due to history
  469. return 'MD5sum' if $field eq 'md5sum';
  470. return uc($field) if checksums_is_supported($field);
  471. # Generic case
  472. return join '-', map { ucfirst } split /-/, $field;
  473. }
  474. =item field_is_official($fname)
  475. Returns true if the field is official and known.
  476. =cut
  477. sub field_is_official($) {
  478. my $field = field_capitalize(shift);
  479. return exists $FIELDS{$field};
  480. }
  481. =item field_is_allowed_in($fname, @types)
  482. Returns true (1) if the field $fname is allowed in all the types listed in
  483. the list. Note that you can use type sets instead of individual types (ex:
  484. CTRL_FILE_CHANGES | CTRL_CHANGELOG).
  485. field_allowed_in(A|B, C) returns true only if the field is allowed in C
  486. and either A or B.
  487. Undef is returned for non-official fields.
  488. =cut
  489. sub field_is_allowed_in($@) {
  490. my ($field, @types) = @_;
  491. $field = field_capitalize($field);
  492. return unless field_is_official($field);
  493. return 0 if not scalar(@types);
  494. foreach my $type (@types) {
  495. next if $type == CTRL_UNKNOWN; # Always allowed
  496. return 0 unless $FIELDS{$field}{allowed} & $type;
  497. }
  498. return 1;
  499. }
  500. =item field_transfer_single($from, $to, $field)
  501. If appropriate, copy the value of the field named $field taken from the
  502. $from Dpkg::Control object to the $to Dpkg::Control object.
  503. Official fields are copied only if the field is allowed in both types of
  504. objects. Custom fields are treated in a specific manner. When the target
  505. is not among CTRL_PKG_SRC, CTRL_PKG_DEB or CTRL_FILE_CHANGES, then they
  506. are always copied as is (the X- prefix is kept). Otherwise they are not
  507. copied except if the target object matches the target destination encoded
  508. in the field name. The initial X denoting custom fields can be followed by
  509. one or more letters among "S" (Source: corresponds to CTRL_PKG_SRC), "B"
  510. (Binary: corresponds to CTRL_PKG_DEB) or "C" (Changes: corresponds to
  511. CTRL_FILE_CHANGES).
  512. Returns undef if nothing has been copied or the name of the new field
  513. added to $to otherwise.
  514. =cut
  515. sub field_transfer_single($$;$) {
  516. my ($from, $to, $field) = @_;
  517. $field //= $_;
  518. my ($from_type, $to_type) = ($from->get_type(), $to->get_type());
  519. $field = field_capitalize($field);
  520. if (field_is_allowed_in($field, $from_type, $to_type)) {
  521. $to->{$field} = $from->{$field};
  522. return $field;
  523. } elsif ($field =~ /^X([SBC]*)-/i) {
  524. my $dest = $1;
  525. if (($dest =~ /B/i and $to_type == CTRL_PKG_DEB) or
  526. ($dest =~ /S/i and $to_type == CTRL_PKG_SRC) or
  527. ($dest =~ /C/i and $to_type == CTRL_FILE_CHANGES))
  528. {
  529. my $new = $field;
  530. $new =~ s/^X([SBC]*)-//i;
  531. $to->{$new} = $from->{$field};
  532. return $new;
  533. } elsif ($to_type != CTRL_PKG_DEB and
  534. $to_type != CTRL_PKG_SRC and
  535. $to_type != CTRL_FILE_CHANGES)
  536. {
  537. $to->{$field} = $from->{$field};
  538. return $field;
  539. }
  540. } elsif (not field_is_allowed_in($field, $from_type)) {
  541. warning(g_("unknown information field '%s' in input data in %s"),
  542. $field, $from->get_option('name') || g_('control information'));
  543. }
  544. return;
  545. }
  546. =item field_transfer_all($from, $to)
  547. Transfer all appropriate fields from $from to $to. Calls
  548. field_transfer_single() on all fields available in $from.
  549. Returns the list of fields that have been added to $to.
  550. =cut
  551. sub field_transfer_all($$) {
  552. my ($from, $to) = @_;
  553. my (@res, $res);
  554. foreach my $k (keys %$from) {
  555. $res = field_transfer_single($from, $to, $k);
  556. push @res, $res if $res and defined wantarray;
  557. }
  558. return @res;
  559. }
  560. =item field_ordered_list($type)
  561. Returns an ordered list of fields for a given type of control information.
  562. This list can be used to output the fields in a predictable order.
  563. The list might be empty for types where the order does not matter much.
  564. =cut
  565. sub field_ordered_list($) {
  566. my $type = shift;
  567. return @{$FIELD_ORDER{$type}} if exists $FIELD_ORDER{$type};
  568. return ();
  569. }
  570. =item field_list_src_dep()
  571. List of fields that contains dependencies-like information in a source
  572. Debian package.
  573. =cut
  574. sub field_list_src_dep() {
  575. my @list = sort {
  576. $FIELDS{$a}{dep_order} <=> $FIELDS{$b}{dep_order}
  577. } grep {
  578. field_is_allowed_in($_, CTRL_PKG_SRC) and
  579. exists $FIELDS{$_}{dependency}
  580. } keys %FIELDS;
  581. return @list;
  582. }
  583. =item field_list_pkg_dep()
  584. List of fields that contains dependencies-like information in a binary
  585. Debian package. The fields that express real dependencies are sorted from
  586. the stronger to the weaker.
  587. =cut
  588. sub field_list_pkg_dep() {
  589. my @keys = keys %FIELDS;
  590. my @list = sort {
  591. $FIELDS{$a}{dep_order} <=> $FIELDS{$b}{dep_order}
  592. } grep {
  593. field_is_allowed_in($_, CTRL_PKG_DEB) and
  594. exists $FIELDS{$_}{dependency}
  595. } @keys;
  596. return @list;
  597. }
  598. =item field_get_dep_type($field)
  599. Return the type of the dependency expressed by the given field. Can
  600. either be "normal" for a real dependency field (Pre-Depends, Depends, ...)
  601. or "union" for other relation fields sharing the same syntax (Conflicts,
  602. Breaks, ...). Returns undef for fields which are not dependencies.
  603. =cut
  604. sub field_get_dep_type($) {
  605. my $field = field_capitalize(shift);
  606. return unless field_is_official($field);
  607. return $FIELDS{$field}{dependency} if exists $FIELDS{$field}{dependency};
  608. return;
  609. }
  610. =item field_get_sep_type($field)
  611. Return the type of the field value separator. Can be one of FIELD_SEP_UNKNOWN,
  612. FIELD_SEP_SPACE, FIELD_SEP_COMMA or FIELD_SEP_LINE.
  613. =cut
  614. sub field_get_sep_type($) {
  615. my $field = field_capitalize(shift);
  616. return $FIELDS{$field}{separator} if exists $FIELDS{$field}{separator};
  617. return FIELD_SEP_UNKNOWN;
  618. }
  619. =item field_register($field, $allowed_types, %opts)
  620. Register a new field as being allowed in control information of specified
  621. types. %opts is optional
  622. =cut
  623. sub field_register($$;@) {
  624. my ($field, $types, %opts) = @_;
  625. $field = field_capitalize($field);
  626. $FIELDS{$field} = {
  627. allowed => $types,
  628. %opts
  629. };
  630. }
  631. =item field_insert_after($type, $ref, @fields)
  632. Place field after another one ($ref) in output of control information of
  633. type $type.
  634. =cut
  635. sub field_insert_after($$@) {
  636. my ($type, $field, @fields) = @_;
  637. return 0 if not exists $FIELD_ORDER{$type};
  638. ($field, @fields) = map { field_capitalize($_) } ($field, @fields);
  639. @{$FIELD_ORDER{$type}} = map {
  640. ($_ eq $field) ? ($_, @fields) : $_
  641. } @{$FIELD_ORDER{$type}};
  642. return 1;
  643. }
  644. =item field_insert_before($type, $ref, @fields)
  645. Place field before another one ($ref) in output of control information of
  646. type $type.
  647. =cut
  648. sub field_insert_before($$@) {
  649. my ($type, $field, @fields) = @_;
  650. return 0 if not exists $FIELD_ORDER{$type};
  651. ($field, @fields) = map { field_capitalize($_) } ($field, @fields);
  652. @{$FIELD_ORDER{$type}} = map {
  653. ($_ eq $field) ? (@fields, $_) : $_
  654. } @{$FIELD_ORDER{$type}};
  655. return 1;
  656. }
  657. =back
  658. =head1 CHANGES
  659. =head2 Version 1.00 (dpkg 1.17.0)
  660. Mark the module as public.
  661. =head1 AUTHOR
  662. Raphaël Hertzog <hertzog@debian.org>.
  663. =cut
  664. 1;