FieldsCore.pm 22 KB

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