Hash.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. while (<$fh>) {
  124. s/\s*\n$//;
  125. next if (m/^$/ and $paraborder);
  126. next if (m/^#/);
  127. $paraborder = 0;
  128. if (m/^(\S+?)\s*:\s*(.*)$/) {
  129. if (exists $self->{$1}) {
  130. unless ($$self->{'allow_duplicate'}) {
  131. syntaxerr($desc, sprintf(_g("duplicate field %s found"), $1));
  132. }
  133. }
  134. $self->{$1} = $2;
  135. $cf = $1;
  136. } elsif (m/^\s(\s*\S.*)$/) {
  137. my $line = $1;
  138. unless (defined($cf)) {
  139. syntaxerr($desc, _g("continued value line not in field"));
  140. }
  141. if ($line =~ /^\.+$/) {
  142. $line = substr $line, 1;
  143. }
  144. $self->{$cf} .= "\n$line";
  145. } elsif (m/^-----BEGIN PGP SIGNED MESSAGE/) {
  146. $expect_pgp_sig = 1;
  147. if ($$self->{'allow_pgp'}) {
  148. # Skip PGP headers
  149. while (<$fh>) {
  150. last if m/^$/;
  151. }
  152. } else {
  153. syntaxerr($desc, _g("PGP signature not allowed here"));
  154. }
  155. } elsif (m/^$/ || ($expect_pgp_sig && m/^-----BEGIN PGP SIGNATURE/)) {
  156. if ($expect_pgp_sig) {
  157. # Skip empty lines
  158. $_ = <$fh> while defined($_) && $_ =~ /^\s*$/;
  159. length($_) ||
  160. syntaxerr($desc, _g("expected PGP signature, found EOF " .
  161. "after blank line"));
  162. s/\n$//;
  163. unless (m/^-----BEGIN PGP SIGNATURE/) {
  164. syntaxerr($desc, sprintf(_g("expected PGP signature, " .
  165. "found something else \`%s'"), $_));
  166. }
  167. # Skip PGP signature
  168. while (<$fh>) {
  169. last if m/^-----END PGP SIGNATURE/;
  170. }
  171. unless (defined($_)) {
  172. syntaxerr($desc, _g("unfinished PGP signature"));
  173. }
  174. }
  175. last; # Finished parsing one block
  176. } else {
  177. syntaxerr($desc,
  178. _g("line with unknown format (not field-colon-value)"));
  179. }
  180. }
  181. return defined($cf);
  182. }
  183. =item $c->find_custom_field($name)
  184. Scan the fields and look for a user specific field whose name matches the
  185. following regex: /X[SBC]*-$name/i. Return the name of the field found or
  186. undef if nothing has been found.
  187. =cut
  188. sub find_custom_field {
  189. my ($self, $name) = @_;
  190. foreach my $key (keys %$self) {
  191. return $key if $key =~ /^X[SBC]*-\Q$name\E$/i;
  192. }
  193. return undef;
  194. }
  195. =item $c->get_custom_field($name)
  196. Identify a user field and retrieve its value.
  197. =cut
  198. sub get_custom_field {
  199. my ($self, $name) = @_;
  200. my $key = $self->find_custom_field($name);
  201. return $self->{$key} if defined $key;
  202. return undef;
  203. }
  204. =item $c->save($filename)
  205. Write the string representation of the control information to a
  206. file.
  207. =item my $str = $c->output()
  208. =item "$c"
  209. Get a string representation of the control information. The fields
  210. are sorted in the order in which they have been read or set except
  211. if the order has been overridden with set_output_order().
  212. =item $c->output($fh)
  213. Print the string representation of the control information to a
  214. filehandle.
  215. =cut
  216. sub output {
  217. my ($self, $fh) = @_;
  218. my $str = "";
  219. my @keys;
  220. if (@{$$self->{'out_order'}}) {
  221. my $i = 1;
  222. my $imp = {};
  223. $imp->{$_} = $i++ foreach @{$$self->{'out_order'}};
  224. @keys = sort {
  225. if (defined $imp->{$a} && defined $imp->{$b}) {
  226. $imp->{$a} <=> $imp->{$b};
  227. } elsif (defined($imp->{$a})) {
  228. -1;
  229. } elsif (defined($imp->{$b})) {
  230. 1;
  231. } else {
  232. $a cmp $b;
  233. }
  234. } keys %$self;
  235. } else {
  236. @keys = @{$$self->{'in_order'}};
  237. }
  238. foreach my $key (@keys) {
  239. if (exists $self->{$key}) {
  240. my $value = $self->{$key};
  241. # Skip whitespace-only fields
  242. next if $$self->{'drop_empty'} and $value !~ m/\S/;
  243. # Escape data to follow control file syntax
  244. my @lines = split(/\n/, $value);
  245. $value = (scalar @lines) ? shift @lines : "";
  246. foreach (@lines) {
  247. s/\s+$//;
  248. if (/^$/ or /^\.+$/) {
  249. $value .= "\n .$_";
  250. } else {
  251. $value .= "\n $_";
  252. }
  253. }
  254. # Print it out
  255. if ($fh) {
  256. print $fh "$key: $value\n" ||
  257. syserr(_g("write error on control data"));
  258. }
  259. $str .= "$key: $value\n" if defined wantarray;
  260. }
  261. }
  262. return $str;
  263. }
  264. =item $c->set_output_order(@fields)
  265. Define the order in which fields will be displayed in the output() method.
  266. =cut
  267. sub set_output_order {
  268. my ($self, @fields) = @_;
  269. $$self->{'out_order'} = [@fields];
  270. }
  271. =item $c->apply_substvars($substvars)
  272. Update all fields by replacing the variables references with
  273. the corresponding value stored in the Dpkg::Substvars object.
  274. =cut
  275. sub apply_substvars {
  276. my ($self, $substvars, %opts) = @_;
  277. # Add substvars to refer to other fields
  278. foreach my $f (keys %$self) {
  279. $substvars->set_as_used("F:$f", $self->{$f});
  280. }
  281. foreach my $f (keys %$self) {
  282. my $v = $substvars->substvars($self->{$f}, %opts);
  283. if ($v ne $self->{$f}) {
  284. # If we replaced stuff, ensure we're not breaking
  285. # a dependency field by introducing empty lines, or multiple
  286. # commas
  287. $v =~ s/\n[ \t]*(\n|$)/$1/; # Drop empty/whitespace-only lines
  288. # TODO: do this only for dependency fields
  289. $v =~ s/,[\s,]*,/,/g;
  290. $v =~ s/^\s*,\s*//;
  291. $v =~ s/\s*,\s*$//;
  292. }
  293. $v =~ s/\$\{\}/\$/g; # XXX: what for?
  294. $self->{$f} = $v;
  295. }
  296. }
  297. package Dpkg::Control::Hash::Tie;
  298. # This object is used to tie a hash. It implements hash-like functions by
  299. # normalizing the name of fields received in keys (using
  300. # Dpkg::Control::Fields::field_capitalize). It also stores the order in
  301. # which fields have been added in order to be able to dump them in the
  302. # same order. But the order information is stored in a parent object of
  303. # type Dpkg::Control.
  304. use Dpkg::ErrorHandling;
  305. use Dpkg::Checksums;
  306. use Tie::Hash;
  307. use base qw(Tie::ExtraHash);
  308. sub field_capitalize($) {
  309. my $field = lc(shift);
  310. # Some special cases due to history
  311. return "MD5sum" if $field eq "md5sum";
  312. return uc($field) if checksums_is_supported($field);
  313. # Generic case
  314. return join '-', map { ucfirst } split /-/, $field;
  315. }
  316. # $self->[0] is the real hash
  317. # $self->[1] is a reference to the hash contained by the parent object.
  318. # This reference bypasses the top-level scalar reference of a
  319. # Dpkg::Control::Hash, hence ensuring that that reference gets DESTROYed
  320. # properly.
  321. # Dpkg::Control::Hash->new($parent)
  322. #
  323. # Return a reference to a tied hash implementing storage of simple
  324. # "field: value" mapping as used in many Debian-specific files.
  325. sub new {
  326. my $class = shift;
  327. my $hash = {};
  328. tie %{$hash}, $class, @_;
  329. return $hash;
  330. }
  331. sub TIEHASH {
  332. my ($class, $parent) = @_;
  333. die "Parent object must be Dpkg::Control::Hash"
  334. if not $parent->isa("Dpkg::Control::Hash");
  335. return bless [ {}, $$parent ], $class;
  336. }
  337. sub FETCH {
  338. my ($self, $key) = @_;
  339. $key = lc($key);
  340. return $self->[0]->{$key} if exists $self->[0]->{$key};
  341. return undef;
  342. }
  343. sub STORE {
  344. my ($self, $key, $value) = @_;
  345. my $parent = $self->[1];
  346. $key = lc($key);
  347. if (not exists $self->[0]->{$key}) {
  348. push @{$parent->{'in_order'}}, field_capitalize($key);
  349. }
  350. $self->[0]->{$key} = $value;
  351. }
  352. sub EXISTS {
  353. my ($self, $key) = @_;
  354. $key = lc($key);
  355. return exists $self->[0]->{$key};
  356. }
  357. sub DELETE {
  358. my ($self, $key) = @_;
  359. my $parent = $self->[1];
  360. my $in_order = $parent->{'in_order'};
  361. $key = lc($key);
  362. if (exists $self->[0]->{$key}) {
  363. delete $self->[0]->{$key};
  364. @$in_order = grep { lc($_) ne $key } @$in_order;
  365. return 1;
  366. } else {
  367. return 0;
  368. }
  369. }
  370. sub FIRSTKEY {
  371. my $self = shift;
  372. my $parent = $self->[1];
  373. foreach (@{$parent->{'in_order'}}) {
  374. return $_ if exists $self->[0]->{lc($_)};
  375. }
  376. }
  377. sub NEXTKEY {
  378. my ($self, $last) = @_;
  379. my $parent = $self->[1];
  380. my $found = 0;
  381. foreach (@{$parent->{'in_order'}}) {
  382. if ($found) {
  383. return $_ if exists $self->[0]->{lc($_)};
  384. } else {
  385. $found = 1 if $_ eq $last;
  386. }
  387. }
  388. return undef;
  389. }
  390. 1;
  391. =back
  392. =head1 AUTHOR
  393. Raphaël Hertzog <hertzog@debian.org>.
  394. =cut
  395. 1;