Checksums.pm 10 KB

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