Fields.pm 16 KB

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