FieldsCore.pm 16 KB

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