Hash.pm 13 KB

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