Fields.pm 17 KB

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