HashCore.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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::HashCore;
  16. use strict;
  17. use warnings;
  18. our $VERSION = '1.00';
  19. use Dpkg::Gettext;
  20. use Dpkg::ErrorHandling;
  21. # This module cannot use Dpkg::Control::Fields, because that one makes use
  22. # of Dpkg::Vendor which at the same time uses this module, which would turn
  23. # into a compilation error. We can use Dpkg::Control::FieldsCore instead.
  24. use parent qw(Dpkg::Interface::Storable);
  25. use overload
  26. '%{}' => sub { ${$_[0]}->{fields} },
  27. 'eq' => sub { "$_[0]" eq "$_[1]" };
  28. =encoding utf8
  29. =head1 NAME
  30. Dpkg::Control::HashCore - parse and manipulate a block of RFC822-like fields
  31. =head1 DESCRIPTION
  32. The Dpkg::Control::Hash object is a hash-like representation of a set of
  33. RFC822-like fields. The fields names are case insensitive and are always
  34. capitalized the same when output (see field_capitalize function in
  35. Dpkg::Control::Fields).
  36. The order in which fields have been set is remembered and is used
  37. to be able to dump back the same content. The output order can also be
  38. overridden if needed.
  39. You can store arbitrary values in the hash, they will always be properly
  40. escaped in the output to conform to the syntax of control files. This is
  41. relevant mainly for multilines values: while the first line is always output
  42. unchanged directly after the field name, supplementary lines are
  43. modified. Empty lines and lines containing only dots are prefixed with
  44. " ." (space + dot) while other lines are prefixed with a single space.
  45. During parsing, trailing spaces are stripped on all lines while leading
  46. spaces are stripped only on the first line of each field.
  47. =head1 FUNCTIONS
  48. =over 4
  49. =item my $c = Dpkg::Control::Hash->new(%opts)
  50. Creates a new object with the indicated options. Supported options
  51. are:
  52. =over 8
  53. =item allow_pgp
  54. Configures the parser to accept PGP signatures around the control
  55. information. Value can be 0 (default) or 1.
  56. =item allow_duplicate
  57. Configures the parser to allow duplicate fields in the control
  58. information. Value can be 0 (default) or 1.
  59. =item drop_empty
  60. Defines if empty fields are dropped during the output. Value can be 0
  61. (default) or 1.
  62. =item name
  63. The user friendly name of the information stored in the object. It might
  64. be used in some error messages or warnings. A default name might be set
  65. depending on the type.
  66. =back
  67. =cut
  68. sub new {
  69. my ($this, %opts) = @_;
  70. my $class = ref($this) || $this;
  71. # Object is a scalar reference and not a hash ref to avoid
  72. # infinite recursion due to overloading hash-derefencing
  73. my $self = \{
  74. in_order => [],
  75. out_order => [],
  76. allow_pgp => 0,
  77. allow_duplicate => 0,
  78. drop_empty => 0,
  79. };
  80. bless $self, $class;
  81. $$self->{fields} = Dpkg::Control::HashCore::Tie->new($self);
  82. # Options set by the user override default values
  83. $$self->{$_} = $opts{$_} foreach keys %opts;
  84. return $self;
  85. }
  86. # There is naturally a circular reference between the tied hash and its
  87. # containing object. Happily, the extra layer of scalar reference can
  88. # be used to detect the destruction of the object and break the loop so
  89. # that everything gets garbage-collected.
  90. sub DESTROY {
  91. my ($self) = @_;
  92. delete $$self->{fields};
  93. }
  94. =item $c->set_options($option, %opts)
  95. Changes the value of one or more options.
  96. =cut
  97. sub set_options {
  98. my ($self, %opts) = @_;
  99. $$self->{$_} = $opts{$_} foreach keys %opts;
  100. }
  101. =item my $value = $c->get_option($option)
  102. Returns the value of the corresponding option.
  103. =cut
  104. sub get_option {
  105. my ($self, $k) = @_;
  106. return $$self->{$k};
  107. }
  108. =item $c->load($file)
  109. Parse the content of $file. Exits in case of errors. Returns true if some
  110. fields have been parsed.
  111. =item $c->parse($fh, $description)
  112. Parse a control file from the given filehandle. Exits in case of errors.
  113. $description is used to describe the filehandle, ideally it's a filename
  114. or a description of where the data comes from. It's used in error
  115. messages. Returns true if some fields have been parsed.
  116. =cut
  117. sub parse {
  118. my ($self, $fh, $desc) = @_;
  119. my $paraborder = 1;
  120. my $parabody = 0;
  121. my $cf; # Current field
  122. my $expect_pgp_sig = 0;
  123. my $pgp_signed = 0;
  124. while (<$fh>) {
  125. s/\s*\n$//;
  126. next if (m/^$/ and $paraborder);
  127. next if (m/^#/);
  128. $paraborder = 0;
  129. if (m/^(\S+?)\s*:\s*(.*)$/) {
  130. $parabody = 1;
  131. if (exists $self->{$1}) {
  132. unless ($$self->{allow_duplicate}) {
  133. syntaxerr($desc, sprintf(_g('duplicate field %s found'), $1));
  134. }
  135. }
  136. $self->{$1} = $2;
  137. $cf = $1;
  138. } elsif (m/^\s(\s*\S.*)$/) {
  139. my $line = $1;
  140. unless (defined($cf)) {
  141. syntaxerr($desc, _g('continued value line not in field'));
  142. }
  143. if ($line =~ /^\.+$/) {
  144. $line = substr $line, 1;
  145. }
  146. $self->{$cf} .= "\n$line";
  147. } elsif (m/^-----BEGIN PGP SIGNED MESSAGE-----$/) {
  148. $expect_pgp_sig = 1;
  149. if ($$self->{allow_pgp} and not $parabody) {
  150. # Skip PGP headers
  151. while (<$fh>) {
  152. last if m/^\s*$/;
  153. }
  154. } else {
  155. syntaxerr($desc, _g('PGP signature not allowed here'));
  156. }
  157. } elsif (m/^$/ || ($expect_pgp_sig && m/^-----BEGIN PGP SIGNATURE-----$/)) {
  158. if ($expect_pgp_sig) {
  159. # Skip empty lines
  160. $_ = <$fh> while defined($_) && $_ =~ /^\s*$/;
  161. length($_) ||
  162. syntaxerr($desc, _g('expected PGP signature, found EOF ' .
  163. 'after blank line'));
  164. s/\s*\n$//;
  165. unless (m/^-----BEGIN PGP SIGNATURE-----$/) {
  166. syntaxerr($desc, sprintf(_g('expected PGP signature, ' .
  167. "found something else \`%s'"), $_));
  168. }
  169. # Skip PGP signature
  170. while (<$fh>) {
  171. s/\s*\n$//;
  172. last if m/^-----END PGP SIGNATURE-----$/;
  173. }
  174. unless (defined($_)) {
  175. syntaxerr($desc, _g('unfinished PGP signature'));
  176. }
  177. # This does not mean the signature is correct, that needs to
  178. # be verified by gnupg.
  179. $pgp_signed = 1;
  180. }
  181. last; # Finished parsing one block
  182. } else {
  183. syntaxerr($desc,
  184. _g('line with unknown format (not field-colon-value)'));
  185. }
  186. }
  187. if ($expect_pgp_sig and not $pgp_signed) {
  188. syntaxerr($desc, _g('unfinished PGP signature'));
  189. }
  190. return defined($cf);
  191. }
  192. =item $c->find_custom_field($name)
  193. Scan the fields and look for a user specific field whose name matches the
  194. following regex: /X[SBC]*-$name/i. Return the name of the field found or
  195. undef if nothing has been found.
  196. =cut
  197. sub find_custom_field {
  198. my ($self, $name) = @_;
  199. foreach my $key (keys %$self) {
  200. return $key if $key =~ /^X[SBC]*-\Q$name\E$/i;
  201. }
  202. return;
  203. }
  204. =item $c->get_custom_field($name)
  205. Identify a user field and retrieve its value.
  206. =cut
  207. sub get_custom_field {
  208. my ($self, $name) = @_;
  209. my $key = $self->find_custom_field($name);
  210. return $self->{$key} if defined $key;
  211. return;
  212. }
  213. =item $c->save($filename)
  214. Write the string representation of the control information to a
  215. file.
  216. =item my $str = $c->output()
  217. =item "$c"
  218. Get a string representation of the control information. The fields
  219. are sorted in the order in which they have been read or set except
  220. if the order has been overridden with set_output_order().
  221. =item $c->output($fh)
  222. Print the string representation of the control information to a
  223. filehandle.
  224. =cut
  225. sub output {
  226. my ($self, $fh) = @_;
  227. my $str = '';
  228. my @keys;
  229. if (@{$$self->{out_order}}) {
  230. my $i = 1;
  231. my $imp = {};
  232. $imp->{$_} = $i++ foreach @{$$self->{out_order}};
  233. @keys = sort {
  234. if (defined $imp->{$a} && defined $imp->{$b}) {
  235. $imp->{$a} <=> $imp->{$b};
  236. } elsif (defined($imp->{$a})) {
  237. -1;
  238. } elsif (defined($imp->{$b})) {
  239. 1;
  240. } else {
  241. $a cmp $b;
  242. }
  243. } keys %$self;
  244. } else {
  245. @keys = @{$$self->{in_order}};
  246. }
  247. foreach my $key (@keys) {
  248. if (exists $self->{$key}) {
  249. my $value = $self->{$key};
  250. # Skip whitespace-only fields
  251. next if $$self->{drop_empty} and $value !~ m/\S/;
  252. # Escape data to follow control file syntax
  253. my @lines = split(/\n/, $value);
  254. $value = (scalar @lines) ? shift @lines : '';
  255. foreach (@lines) {
  256. s/\s+$//;
  257. if (/^$/ or /^\.+$/) {
  258. $value .= "\n .$_";
  259. } else {
  260. $value .= "\n $_";
  261. }
  262. }
  263. # Print it out
  264. if ($fh) {
  265. print $fh "$key: $value\n" ||
  266. syserr(_g('write error on control data'));
  267. }
  268. $str .= "$key: $value\n" if defined wantarray;
  269. }
  270. }
  271. return $str;
  272. }
  273. =item $c->set_output_order(@fields)
  274. Define the order in which fields will be displayed in the output() method.
  275. =cut
  276. sub set_output_order {
  277. my ($self, @fields) = @_;
  278. $$self->{out_order} = [@fields];
  279. }
  280. =item $c->apply_substvars($substvars)
  281. Update all fields by replacing the variables references with
  282. the corresponding value stored in the Dpkg::Substvars object.
  283. =cut
  284. sub apply_substvars {
  285. my ($self, $substvars, %opts) = @_;
  286. # Add substvars to refer to other fields
  287. foreach my $f (keys %$self) {
  288. $substvars->set_as_used("F:$f", $self->{$f});
  289. }
  290. foreach my $f (keys %$self) {
  291. my $v = $substvars->substvars($self->{$f}, %opts);
  292. if ($v ne $self->{$f}) {
  293. # If we replaced stuff, ensure we're not breaking
  294. # a dependency field by introducing empty lines, or multiple
  295. # commas
  296. $v =~ s/\n[ \t]*(\n|$)/$1/; # Drop empty/whitespace-only lines
  297. # TODO: do this only for dependency fields
  298. $v =~ s/,[\s,]*,/,/g;
  299. $v =~ s/^\s*,\s*//;
  300. $v =~ s/\s*,\s*$//;
  301. }
  302. $v =~ s/\$\{\}/\$/g; # XXX: what for?
  303. $self->{$f} = $v;
  304. }
  305. }
  306. package Dpkg::Control::HashCore::Tie;
  307. # This object is used to tie a hash. It implements hash-like functions by
  308. # normalizing the name of fields received in keys (using
  309. # Dpkg::Control::Fields::field_capitalize). It also stores the order in
  310. # which fields have been added in order to be able to dump them in the
  311. # same order. But the order information is stored in a parent object of
  312. # type Dpkg::Control.
  313. use Dpkg::Checksums;
  314. use Dpkg::Control::FieldsCore;
  315. use Tie::Hash;
  316. use parent -norequire, qw(Tie::ExtraHash);
  317. # $self->[0] is the real hash
  318. # $self->[1] is a reference to the hash contained by the parent object.
  319. # This reference bypasses the top-level scalar reference of a
  320. # Dpkg::Control::Hash, hence ensuring that that reference gets DESTROYed
  321. # properly.
  322. # Dpkg::Control::Hash->new($parent)
  323. #
  324. # Return a reference to a tied hash implementing storage of simple
  325. # "field: value" mapping as used in many Debian-specific files.
  326. sub new {
  327. my $class = shift;
  328. my $hash = {};
  329. tie %{$hash}, $class, @_;
  330. return $hash;
  331. }
  332. sub TIEHASH {
  333. my ($class, $parent) = @_;
  334. die 'Parent object must be Dpkg::Control::Hash'
  335. if not $parent->isa('Dpkg::Control::HashCore') and
  336. not $parent->isa('Dpkg::Control::Hash');
  337. return bless [ {}, $$parent ], $class;
  338. }
  339. sub FETCH {
  340. my ($self, $key) = @_;
  341. $key = lc($key);
  342. return $self->[0]->{$key} if exists $self->[0]->{$key};
  343. return;
  344. }
  345. sub STORE {
  346. my ($self, $key, $value) = @_;
  347. my $parent = $self->[1];
  348. $key = lc($key);
  349. if (not exists $self->[0]->{$key}) {
  350. push @{$parent->{in_order}}, field_capitalize($key);
  351. }
  352. $self->[0]->{$key} = $value;
  353. }
  354. sub EXISTS {
  355. my ($self, $key) = @_;
  356. $key = lc($key);
  357. return exists $self->[0]->{$key};
  358. }
  359. sub DELETE {
  360. my ($self, $key) = @_;
  361. my $parent = $self->[1];
  362. my $in_order = $parent->{in_order};
  363. $key = lc($key);
  364. if (exists $self->[0]->{$key}) {
  365. delete $self->[0]->{$key};
  366. @$in_order = grep { lc($_) ne $key } @$in_order;
  367. return 1;
  368. } else {
  369. return 0;
  370. }
  371. }
  372. sub FIRSTKEY {
  373. my $self = shift;
  374. my $parent = $self->[1];
  375. foreach (@{$parent->{in_order}}) {
  376. return $_ if exists $self->[0]->{lc($_)};
  377. }
  378. }
  379. sub NEXTKEY {
  380. my ($self, $last) = @_;
  381. my $parent = $self->[1];
  382. my $found = 0;
  383. foreach (@{$parent->{in_order}}) {
  384. if ($found) {
  385. return $_ if exists $self->[0]->{lc($_)};
  386. } else {
  387. $found = 1 if $_ eq $last;
  388. }
  389. }
  390. return;
  391. }
  392. 1;
  393. =back
  394. =head1 AUTHOR
  395. Raphaël Hertzog <hertzog@debian.org>.
  396. =cut
  397. 1;