Fields.pm 17 KB

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