Hash.pm 12 KB

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