Fields.pm 17 KB

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