FieldsCore.pm 22 KB

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