FieldsCore.pm 22 KB

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