Checksums.pm 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. # Copyright © 2008 Frank Lichtenheld <djpig@debian.org>
  2. # Copyright © 2010 Raphaël Hertzog <hertzog@debian.org>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. package Dpkg::Checksums;
  17. use strict;
  18. use warnings;
  19. our $VERSION = "1.00";
  20. use Dpkg;
  21. use Dpkg::Gettext;
  22. use Dpkg::ErrorHandling;
  23. use Dpkg::IPC;
  24. use base qw(Exporter);
  25. our @EXPORT = qw(checksums_get_list checksums_is_supported
  26. checksums_get_property);
  27. =encoding utf8
  28. =head1 NAME
  29. Dpkg::Checksums - generate and manipulate file checksums
  30. =head1 DESCRIPTION
  31. This module provides an object that can generate and manipulate
  32. various file checksums as well as some methods to query information
  33. about supported checksums.
  34. =head1 EXPORTED FUNCTIONS
  35. =over 4
  36. =cut
  37. my $CHECKSUMS = {
  38. "md5" => {
  39. "program" => [ "md5sum" ],
  40. "regex" => qr/[0-9a-f]{32}/,
  41. },
  42. "sha1" => {
  43. "program" => [ "sha1sum" ],
  44. "regex" => qr/[0-9a-f]{40}/,
  45. },
  46. "sha256" => {
  47. "program" => [ "sha256sum" ],
  48. "regex" => qr/[0-9a-f]{64}/,
  49. },
  50. };
  51. =item @list = checksums_get_list()
  52. Returns the list of supported checksums algorithms.
  53. =cut
  54. sub checksums_get_list() {
  55. return sort keys %{$CHECKSUMS};
  56. }
  57. =item $bool = checksums_is_supported($alg)
  58. Returns a boolean indicating whether the given checksum algorithm is
  59. supported. The checksum algorithm is case-insensitive.
  60. =cut
  61. sub checksums_is_supported($) {
  62. my ($alg) = @_;
  63. return exists $CHECKSUMS->{lc($alg)};
  64. }
  65. =item $value = checksums_get_property($alg, $property)
  66. Returns the requested property of the checksum algorithm. Returns undef if
  67. either the property or the checksum algorithm doesn't exist. Valid
  68. properties currently include "program" (returns an array reference with
  69. a program name and parameters required to compute the checksum of the
  70. filename given as last parameter) and "regex" for the regular expression
  71. describing the common string representation of the checksum (as output
  72. by the program that generates it).
  73. =cut
  74. sub checksums_get_property($$) {
  75. my ($alg, $property) = @_;
  76. return undef unless checksums_is_supported($alg);
  77. return $CHECKSUMS->{lc($alg)}{$property};
  78. }
  79. =back
  80. =head1 OBJECT METHODS
  81. =over 4
  82. =item my $ck = Dpkg::Checksums->new()
  83. Create a new Dpkg::Checksums object. This object is able to store
  84. the checksums of several files to later export them or verify them.
  85. =cut
  86. sub new {
  87. my ($this, %opts) = @_;
  88. my $class = ref($this) || $this;
  89. my $self = {};
  90. bless $self, $class;
  91. $self->reset();
  92. return $self;
  93. }
  94. =item $ck->reset()
  95. Forget about all checksums stored. The object is again in the same state
  96. as if it was newly created.
  97. =cut
  98. sub reset {
  99. my ($self) = @_;
  100. $self->{files} = [];
  101. $self->{checksums} = {};
  102. $self->{size} = {};
  103. }
  104. =item $ck->add_from_file($filename, %opts)
  105. Add checksums information for the file $filename. The file must exists
  106. for the call to succeed. If you don't want the given filename to appear
  107. when you later export the checksums you might want to set the "key"
  108. option with the public name that you want to use. Also if you don't want
  109. to generate all the checksums, you can pass an array reference of the
  110. wanted checksums in the "checksums" option.
  111. It the object already contains checksums information associated the
  112. filename (or key), it will error out if the newly computed information
  113. does not match what's stored.
  114. =cut
  115. sub add_from_file {
  116. my ($self, $file, %opts) = @_;
  117. my $key = exists $opts{key} ? $opts{key} : $file;
  118. my @alg;
  119. if (exists $opts{checksums}) {
  120. push @alg, map { lc($_) } @{$opts{checksums}};
  121. } else {
  122. push @alg, checksums_get_list();
  123. }
  124. push @{$self->{files}}, $key unless exists $self->{size}{$key};
  125. (my @s = stat($file)) || syserr(_g("cannot fstat file %s"), $file);
  126. if (exists $self->{size}{$key} and $self->{size}{$key} != $s[7]) {
  127. error(_g("File %s has size %u instead of expected %u"),
  128. $file, $s[7], $self->{size}{$key});
  129. }
  130. $self->{size}{$key} = $s[7];
  131. foreach my $alg (@alg) {
  132. my @exec = (@{$CHECKSUMS->{$alg}{"program"}}, $file);
  133. my $regex = $CHECKSUMS->{$alg}{"regex"};
  134. my $output;
  135. spawn('exec' => \@exec, to_string => \$output);
  136. if ($output =~ /^($regex)(\s|$)/m) {
  137. my $newsum = $1;
  138. if (exists $self->{checksums}{$key}{$alg} and
  139. $self->{checksums}{$key}{$alg} ne $newsum) {
  140. error(_g("File %s has checksum %s instead of expected %s (algorithm %s)"),
  141. $file, $newsum, $self->{checksums}{$key}{$alg}, $alg);
  142. }
  143. $self->{checksums}{$key}{$alg} = $newsum;
  144. } else {
  145. error(_g("checksum program gave bogus output `%s'"), $output);
  146. }
  147. }
  148. }
  149. =item $ck->add_from_string($alg, $value)
  150. Add checksums of type $alg that are stored in the $value variable.
  151. $value can be multi-lines, each line should be a space separated list
  152. of checksum, file size and filename. Leading or trailing spaces are
  153. not allowed.
  154. It the object already contains checksums information associated to the
  155. filenames, it will error out if the newly read information does not match
  156. what's stored.
  157. =cut
  158. sub add_from_string {
  159. my ($self, $alg, $fieldtext) = @_;
  160. $alg = lc($alg);
  161. my $rx_fname = qr/[0-9a-zA-Z][-+:.,=0-9a-zA-Z_~]+/;
  162. my $regex = checksums_get_property($alg, "regex");
  163. my $checksums = $self->{checksums};
  164. for my $checksum (split /\n */, $fieldtext) {
  165. next if $checksum eq '';
  166. unless ($checksum =~ m/^($regex)\s+(\d+)\s+($rx_fname)$/) {
  167. error(_g("invalid line in %s checksums string: %s"),
  168. $alg, $checksum);
  169. }
  170. my ($sum, $size, $file) = ($1, $2, $3);
  171. if (exists($checksums->{$file}{$alg})
  172. and $checksums->{$file}{$alg} ne $sum) {
  173. error(_g("Conflicting checksums \`%s\' and \`%s' for file \`%s'"),
  174. $checksums->{$file}{$alg}, $sum, $file);
  175. }
  176. if (exists $self->{size}{$file} and $self->{size}{$file} != $size) {
  177. error(_g("Conflicting file sizes \`%u\' and \`%u' for file \`%s'"),
  178. $self->{size}{$file}, $size, $file);
  179. }
  180. push @{$self->{files}}, $file unless exists $self->{size}{$file};
  181. $checksums->{$file}{$alg} = $sum;
  182. $self->{size}{$file} = $size;
  183. }
  184. }
  185. =item $ck->add_from_control($control, %opts)
  186. Read checksums from Checksums-* fields stored in the Dpkg::Control object
  187. $control. It uses $self->add_from_string() on the field values to do the
  188. actual work.
  189. If the option "use_files_for_md5" evaluates to true, then the "Files"
  190. field is used in place of the "Checksums-Md5" field. By default the option
  191. is false.
  192. =cut
  193. sub add_from_control {
  194. my ($self, $control, %opts) = @_;
  195. $opts{use_files_for_md5} = 0 unless exists $opts{use_files_for_md5};
  196. foreach my $alg (checksums_get_list()) {
  197. my $key = "Checksums-$alg";
  198. $key = "Files" if ($opts{use_files_for_md5} and $alg eq "md5");
  199. if (exists $control->{$key}) {
  200. $self->add_from_string($alg, $control->{$key});
  201. }
  202. }
  203. }
  204. =item @files = $ck->get_files()
  205. Return the list of files whose checksums are stored in the object.
  206. =cut
  207. sub get_files {
  208. my ($self) = @_;
  209. return @{$self->{files}};
  210. }
  211. =item $bool = $ck->has_file($file)
  212. Return true if we have checksums for the given file. Returns false
  213. otherwise.
  214. =cut
  215. sub has_file {
  216. my ($self, $file) = @_;
  217. return exists $self->{size}{$file};
  218. }
  219. =item $ck->remove_file($file)
  220. Remove all checksums of the given file.
  221. =cut
  222. sub remove_file {
  223. my ($self, $file) = @_;
  224. return unless $self->has_file($file);
  225. delete $self->{'checksums'}{$file};
  226. delete $self->{'size'}{$file};
  227. @{$self->{'files'}} = grep { $_ ne $file } $self->get_files();
  228. }
  229. =item $checksum = $ck->get_checksum($file, $alg)
  230. Return the checksum of type $alg for the requested $file. This will not
  231. compute the checksum but only return the checksum stored in the object, if
  232. any.
  233. If $alg is not defined, it returns a reference to a hash: keys are
  234. the checksum algorithms and values are the checksums themselves. The
  235. hash returned must not be modified, it's internal to the object.
  236. =cut
  237. sub get_checksum {
  238. my ($self, $file, $alg) = @_;
  239. $alg = lc($alg) if defined $alg;
  240. if (exists $self->{checksums}{$file}) {
  241. return $self->{checksums}{$file} unless defined $alg;
  242. return $self->{checksums}{$file}{$alg};
  243. }
  244. return undef;
  245. }
  246. =item $size = $ck->get_size($file)
  247. Return the size of the requested file if it's available in the object.
  248. =cut
  249. sub get_size {
  250. my ($self, $file) = @_;
  251. return $self->{size}{$file};
  252. }
  253. =item $ck->export_to_string($alg, %opts)
  254. Return a multi-line string containing the checksums of type $alg. The
  255. string can be stored as-is in a Checksum-* field of a Dpkg::Control
  256. object.
  257. =cut
  258. sub export_to_string {
  259. my ($self, $alg, %opts) = @_;
  260. my $res = "";
  261. foreach my $file ($self->get_files()) {
  262. my $sum = $self->get_checksum($file, $alg);
  263. my $size = $self->get_size($file);
  264. next unless defined $sum and defined $size;
  265. $res .= "\n$sum $size $file";
  266. }
  267. return $res;
  268. }
  269. =item $ck->export_to_control($control, %opts)
  270. Export the checksums in the Checksums-* fields of the Dpkg::Control
  271. $control object.
  272. =cut
  273. sub export_to_control {
  274. my ($self, $control, %opts) = @_;
  275. $opts{use_files_for_md5} = 0 unless exists $opts{use_files_for_md5};
  276. foreach my $alg (checksums_get_list()) {
  277. my $key = "Checksums-$alg";
  278. $key = "Files" if ($opts{use_files_for_md5} and $alg eq "md5");
  279. $control->{$key} = $self->export_to_string($alg, %opts);
  280. }
  281. }
  282. =back
  283. =head1 AUTHOR
  284. Raphaël Hertzog <hertzog@debian.org>.
  285. =cut
  286. 1;