Package.pm 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. # Copyright 2008 Raphaël Hertzog <hertzog@debian.org>
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 2 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License along
  11. # with this program; if not, write to the Free Software Foundation, Inc.,
  12. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  13. package Dpkg::Source::Package;
  14. use strict;
  15. use warnings;
  16. use Dpkg::Gettext;
  17. use Dpkg::ErrorHandling qw(error syserr warning internerr);
  18. use Dpkg::Fields;
  19. use Dpkg::Cdata;
  20. use Dpkg::Checksums;
  21. use Dpkg::Version qw(parseversion);
  22. use Dpkg::Deps qw(@src_dep_fields);
  23. use File::Basename;
  24. my @dsc_fields = (qw(Format Source Binary Architecture Version Origin
  25. Maintainer Uploaders Dm-Upload-Allowed Homepage
  26. Standards-Version Vcs-Browser Vcs-Arch Vcs-Bzr
  27. Vcs-Cvs Vcs-Darcs Vcs-Git Vcs-Hg Vcs-Mtn Vcs-Svn),
  28. @src_dep_fields,
  29. qw(Files));
  30. # Object methods
  31. sub new {
  32. my ($this, %args) = @_;
  33. my $class = ref($this) || $this;
  34. my $self = {
  35. 'fields' => Dpkg::Fields::Object->new(),
  36. 'options' => {},
  37. };
  38. bless $self, $class;
  39. if (exists $args{"filename"}) {
  40. $self->initialize($args{"filename"});
  41. }
  42. if (exists $args{"options"}) {
  43. $self->{'options'} = $args{'options'};
  44. }
  45. return $self;
  46. }
  47. sub initialize {
  48. my ($self, $filename) = @_;
  49. my ($fn, $dir) = fileparse($filename);
  50. error(_g("%s is not the name of a file"), $filename) unless $fn;
  51. $self->{'basedir'} = $dir || "./";
  52. $self->{'filename'} = $fn;
  53. # Check if it contains a signature
  54. open(DSC, "<", $filename) || syserr(_g("cannot open %s"), $filename);
  55. $self->{'is_signed'} = 0;
  56. while (<DSC>) {
  57. next if /^\s*$/o;
  58. $self->{'is_signed'} = 1 if /^-----BEGIN PGP SIGNED MESSAGE-----$/o;
  59. last;
  60. }
  61. close(DSC);
  62. # Read the fields
  63. open(CDATA, "<", $filename) || syserr(_g("cannot open %s"), $filename);
  64. my $fields = parsecdata(\*CDATA,
  65. sprintf(_g("source control file %s"), $filename),
  66. allow_pgp => 1);
  67. close(CDATA);
  68. $self->{'fields'} = $fields;
  69. foreach my $f (qw(Source Format Version Files)) {
  70. unless (defined($fields->{$f})) {
  71. error(_g("missing critical source control field %s"), $f);
  72. }
  73. }
  74. $self->parse_files();
  75. $self->upgrade_object_type();
  76. }
  77. sub upgrade_object_type {
  78. my ($self) = @_;
  79. my $format = $self->{'fields'}{'Format'};
  80. if ($format =~ /^([\d\.]+)(?:\s+\((.*)\))?$/) {
  81. my ($version, $variant) = ($1, $2);
  82. $version =~ s/\./_/;
  83. my $module = "Dpkg::Source::Package::V$version";
  84. $module .= "::$variant" if defined $variant;
  85. eval "require $module";
  86. if ($@) {
  87. error(_g("source package format `%s' is not supported (perl module %s is required)"), $format, $module);
  88. }
  89. bless $self, $module;
  90. } else {
  91. error(_g("invalid Format field `%s'"), $format);
  92. }
  93. }
  94. sub get_filename {
  95. my ($self) = @_;
  96. return $self->{'basedir'} . $self->{'filename'};
  97. }
  98. sub get_files {
  99. my ($self) = @_;
  100. return keys %{$self->{'files'}};
  101. }
  102. sub parse_files {
  103. my ($self) = @_;
  104. my $rx_fname = qr/[0-9a-zA-Z][-+:.,=0-9a-zA-Z_~]+/;
  105. my $files = $self->{'fields'}{'Files'};
  106. foreach my $file (split(/\n /, $files)) {
  107. next if $file eq '';
  108. $file =~ m/^($check_regex{md5}) # checksum
  109. [ \t]+(\d+) # size
  110. [ \t]+($rx_fname) # filename
  111. $/x
  112. || error(_g("Files field contains bad line `%s'"), $file);
  113. if (exists $self->{'files'}{$3}) {
  114. error(_g("file `%s' listed twice in Files field"), $3);
  115. } else {
  116. $self->{'files'}{$3} = $2;
  117. }
  118. }
  119. }
  120. sub check_checksums {
  121. my ($self) = @_;
  122. my ($fields, %checksum, %size) = $self->{'fields'};
  123. my $has_md5 = 1;
  124. if (not exists $fields->{'Checksums-Md5'}) {
  125. $fields->{'Checksums-Md5'} = $fields->{'Files'};
  126. $has_md5 = 0;
  127. }
  128. # extract the checksums from the fields in two hashes
  129. readallchecksums($self->{'fields'}, \%checksum, \%size);
  130. delete $fields->{'Checksums-Md5'} unless $has_md5;
  131. # getchecksums verify the checksums if they are pre-filled
  132. foreach my $file ($self->get_files()) {
  133. getchecksums($self->{'basedir'} . $file, $checksum{$file},
  134. \$size{$file});
  135. }
  136. }
  137. sub get_basename {
  138. my ($self, $with_revision) = @_;
  139. my $f = $self->{'fields'};
  140. unless (exists $f->{'Source'} and exists $f->{'Version'}) {
  141. error(_g("source and version are required to compute the source basename"));
  142. }
  143. my %v = parseversion($f->{'Version'});
  144. my $basename = $f->{'Source'} . "_" . $v{"version"};
  145. if ($with_revision and $f->{'Version'} =~ /-/) {
  146. $basename .= "-" . $v{'revision'};
  147. }
  148. return $basename;
  149. }
  150. sub is_signed {
  151. my $self = shift;
  152. return $self->{'is_signed'};
  153. }
  154. sub check_signature {
  155. my ($self) = @_;
  156. my $dsc = $self->get_filename();
  157. if (-x '/usr/bin/gpg') {
  158. my $gpg_command = 'gpg -q --verify ';
  159. if (-r '/usr/share/keyrings/debian-keyring.gpg') {
  160. $gpg_command = $gpg_command.'--keyring /usr/share/keyrings/debian-keyring.gpg ';
  161. }
  162. $gpg_command = $gpg_command.quotemeta($dsc).' 2>&1';
  163. #TODO: cleanup here
  164. my @gpg_output = `$gpg_command`;
  165. my $gpg_status = $? >> 8;
  166. if ($gpg_status) {
  167. print STDERR join("",@gpg_output);
  168. error(_g("failed to verify signature on %s"), $dsc)
  169. if ($gpg_status == 1);
  170. }
  171. } else {
  172. warning(_g("could not verify signature on %s since gpg isn't installed"),
  173. $dsc);
  174. }
  175. }
  176. sub extract {
  177. error("Dpkg::Source::Package doesn't know how to unpack a source package. Use one of the subclass.");
  178. }
  179. # Function used specifically during creation of a source package
  180. sub build {
  181. error("Dpkg::Source::Package doesn't know how to build a source package. Use one of the subclass.");
  182. }
  183. sub add_file {
  184. my ($self, $filename) = @_;
  185. if (exists $self->{'files'}{$filename}) {
  186. internerr(_g("tried to add file `%s' twice"), $filename);
  187. }
  188. my (%sums, $size);
  189. getchecksums($filename, \%sums, \$size);
  190. $self->{'files'}{$filename} = $size;
  191. foreach my $alg (sort keys %sums) {
  192. $self->{'fields'}{"Checksums-$alg"} .= "\n $sums{$alg} $size $filename";
  193. }
  194. $self->{'fields'}{'Files'}.= "\n $sums{md5} $size $filename";
  195. }
  196. sub write_dsc {
  197. my ($self, %opts) = @_;
  198. my $fields = $self->{'fields'};
  199. foreach my $f (keys %{$opts{'override'}}) {
  200. $fields->{$f} = $opts{'override'}{$f};
  201. }
  202. unless($opts{'nocheck'}) {
  203. foreach my $f (qw(Source Version)) {
  204. unless (defined($fields->{$f})) {
  205. error(_g("missing information for critical output field %s"), $f);
  206. }
  207. }
  208. foreach my $f (qw(Maintainer Architecture Standards-Version)) {
  209. unless (defined($fields->{$f})) {
  210. warning(_g("missing information for output field %s"), $f);
  211. }
  212. }
  213. }
  214. foreach my $f (keys %{$opts{'remove'}}) {
  215. delete $fields->{$f};
  216. }
  217. my $filename = $opts{'filename'};
  218. unless (defined $filename) {
  219. $filename = $self->get_basename(1) . ".dsc";
  220. }
  221. open(DSC, ">", $filename) || syserr(_g("cannot write %s"), $filename);
  222. delete $fields->{'Checksums-Md5'}; # identical with Files field
  223. tied(%{$fields})->set_field_importance(@dsc_fields);
  224. tied(%{$fields})->output(\*DSC, $opts{'substvars'});
  225. close(DSC);
  226. }
  227. # vim: set et sw=4 ts=8
  228. 1;