Hash.pm 12 KB

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