Package.pm 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 Dpkg::Compression;
  24. use File::Basename;
  25. my @dsc_fields = (qw(Format Source Binary Architecture Version Origin
  26. Maintainer Uploaders Dm-Upload-Allowed Homepage
  27. Standards-Version Vcs-Browser Vcs-Arch Vcs-Bzr
  28. Vcs-Cvs Vcs-Darcs Vcs-Git Vcs-Hg Vcs-Mtn Vcs-Svn),
  29. @src_dep_fields,
  30. qw(Files));
  31. # Object methods
  32. sub new {
  33. my ($this, %args) = @_;
  34. my $class = ref($this) || $this;
  35. my $self = {
  36. 'fields' => Dpkg::Fields::Object->new(),
  37. 'options' => {},
  38. };
  39. bless $self, $class;
  40. if (exists $args{"filename"}) {
  41. $self->initialize($args{"filename"});
  42. }
  43. if (exists $args{"options"}) {
  44. $self->{'options'} = $args{'options'};
  45. }
  46. return $self;
  47. }
  48. sub initialize {
  49. my ($self, $filename) = @_;
  50. my ($fn, $dir) = fileparse($filename);
  51. error(_g("%s is not the name of a file"), $filename) unless $fn;
  52. $self->{'basedir'} = $dir || "./";
  53. $self->{'filename'} = $fn;
  54. # Check if it contains a signature
  55. open(DSC, "<", $filename) || syserr(_g("cannot open %s"), $filename);
  56. $self->{'is_signed'} = 0;
  57. while (<DSC>) {
  58. next if /^\s*$/o;
  59. $self->{'is_signed'} = 1 if /^-----BEGIN PGP SIGNED MESSAGE-----$/o;
  60. last;
  61. }
  62. close(DSC);
  63. # Read the fields
  64. open(CDATA, "<", $filename) || syserr(_g("cannot open %s"), $filename);
  65. my $fields = parsecdata(\*CDATA,
  66. sprintf(_g("source control file %s"), $filename),
  67. allow_pgp => 1);
  68. close(CDATA);
  69. $self->{'fields'} = $fields;
  70. foreach my $f (qw(Source Format Version Files)) {
  71. unless (defined($fields->{$f})) {
  72. error(_g("missing critical source control field %s"), $f);
  73. }
  74. }
  75. $self->parse_files();
  76. $self->upgrade_object_type();
  77. }
  78. sub upgrade_object_type {
  79. my ($self) = @_;
  80. my $format = $self->{'fields'}{'Format'};
  81. if ($format =~ /^([\d\.]+)(?:\s+\((.*)\))?$/) {
  82. my ($version, $variant) = ($1, $2);
  83. $version =~ s/\./_/;
  84. my $module = "Dpkg::Source::Package::V$version";
  85. $module .= "::$variant" if defined $variant;
  86. eval "require $module";
  87. if ($@) {
  88. error(_g("source package format `%s' is not supported (perl module %s is required)"), $format, $module);
  89. }
  90. bless $self, $module;
  91. } else {
  92. error(_g("invalid Format field `%s'"), $format);
  93. }
  94. }
  95. sub get_filename {
  96. my ($self) = @_;
  97. return $self->{'basedir'} . $self->{'filename'};
  98. }
  99. sub get_files {
  100. my ($self) = @_;
  101. return keys %{$self->{'files'}};
  102. }
  103. sub parse_files {
  104. my ($self) = @_;
  105. my $rx_fname = qr/[0-9a-zA-Z][-+:.,=0-9a-zA-Z_~]+/;
  106. my $files = $self->{'fields'}{'Files'};
  107. foreach my $file (split(/\n /, $files)) {
  108. next if $file eq '';
  109. $file =~ m/^($check_regex{md5}) # checksum
  110. [ \t]+(\d+) # size
  111. [ \t]+($rx_fname) # filename
  112. $/x
  113. || error(_g("Files field contains bad line `%s'"), $file);
  114. if (exists $self->{'files'}{$3}) {
  115. error(_g("file `%s' listed twice in Files field"), $3);
  116. } else {
  117. $self->{'files'}{$3} = $2;
  118. }
  119. }
  120. }
  121. sub check_checksums {
  122. my ($self) = @_;
  123. my ($fields, %checksum, %size) = $self->{'fields'};
  124. my $has_md5 = 1;
  125. if (not exists $fields->{'Checksums-Md5'}) {
  126. $fields->{'Checksums-Md5'} = $fields->{'Files'};
  127. $has_md5 = 0;
  128. }
  129. # extract the checksums from the fields in two hashes
  130. readallchecksums($self->{'fields'}, \%checksum, \%size);
  131. delete $fields->{'Checksums-Md5'} unless $has_md5;
  132. # getchecksums verify the checksums if they are pre-filled
  133. foreach my $file ($self->get_files()) {
  134. getchecksums($self->{'basedir'} . $file, $checksum{$file},
  135. \$size{$file});
  136. }
  137. }
  138. sub get_basename {
  139. my ($self, $with_revision) = @_;
  140. my $f = $self->{'fields'};
  141. unless (exists $f->{'Source'} and exists $f->{'Version'}) {
  142. error(_g("source and version are required to compute the source basename"));
  143. }
  144. my %v = parseversion($f->{'Version'});
  145. my $basename = $f->{'Source'} . "_" . $v{"version"};
  146. if ($with_revision and $f->{'Version'} =~ /-/) {
  147. $basename .= "-" . $v{'revision'};
  148. }
  149. return $basename;
  150. }
  151. sub find_original_tarballs {
  152. my ($self, $ext) = @_;
  153. $ext ||= $comp_regex;
  154. my $basename = $self->get_basename();
  155. my @tar;
  156. foreach my $dir (".", $self->{'basedir'}, $self->{'options'}{'origtardir'}) {
  157. next unless defined($dir) and -d $dir;
  158. opendir(DIR, $dir) || syserr(_g("cannot opendir %s"), $dir);
  159. push @tar, map { "$dir/$_" }
  160. grep { /^\Q$basename\E\.orig(-\w+)?\.tar\.$ext$/ }
  161. readdir(DIR);
  162. closedir(DIR);
  163. }
  164. return @tar;
  165. }
  166. sub is_signed {
  167. my $self = shift;
  168. return $self->{'is_signed'};
  169. }
  170. sub check_signature {
  171. my ($self) = @_;
  172. my $dsc = $self->get_filename();
  173. if (-x '/usr/bin/gpg') {
  174. my $gpg_command = 'gpg -q --verify ';
  175. if (-r '/usr/share/keyrings/debian-keyring.gpg') {
  176. $gpg_command = $gpg_command.'--keyring /usr/share/keyrings/debian-keyring.gpg ';
  177. }
  178. $gpg_command = $gpg_command.quotemeta($dsc).' 2>&1';
  179. #TODO: cleanup here
  180. my @gpg_output = `$gpg_command`;
  181. my $gpg_status = $? >> 8;
  182. if ($gpg_status) {
  183. print STDERR join("",@gpg_output);
  184. error(_g("failed to verify signature on %s"), $dsc)
  185. if ($gpg_status == 1);
  186. }
  187. } else {
  188. warning(_g("could not verify signature on %s since gpg isn't installed"),
  189. $dsc);
  190. }
  191. }
  192. sub extract {
  193. error("Dpkg::Source::Package doesn't know how to unpack a source package. Use one of the subclass.");
  194. }
  195. # Function used specifically during creation of a source package
  196. sub build {
  197. error("Dpkg::Source::Package doesn't know how to build a source package. Use one of the subclass.");
  198. }
  199. sub can_build {
  200. my ($self, $dir) = @_;
  201. return 0;
  202. }
  203. sub add_file {
  204. my ($self, $filename) = @_;
  205. my ($fn, $dir) = fileparse($filename);
  206. if (exists $self->{'files'}{$fn}) {
  207. internerr(_g("tried to add file `%s' twice"), $fn);
  208. }
  209. my (%sums, $size);
  210. getchecksums($filename, \%sums, \$size);
  211. $self->{'files'}{$fn} = $size;
  212. foreach my $alg (sort keys %sums) {
  213. $self->{'fields'}{"Checksums-$alg"} .= "\n $sums{$alg} $size $fn";
  214. }
  215. $self->{'fields'}{'Files'}.= "\n $sums{md5} $size $fn";
  216. }
  217. sub write_dsc {
  218. my ($self, %opts) = @_;
  219. my $fields = $self->{'fields'};
  220. foreach my $f (keys %{$opts{'override'}}) {
  221. $fields->{$f} = $opts{'override'}{$f};
  222. }
  223. unless($opts{'nocheck'}) {
  224. foreach my $f (qw(Source Version)) {
  225. unless (defined($fields->{$f})) {
  226. error(_g("missing information for critical output field %s"), $f);
  227. }
  228. }
  229. foreach my $f (qw(Maintainer Architecture Standards-Version)) {
  230. unless (defined($fields->{$f})) {
  231. warning(_g("missing information for output field %s"), $f);
  232. }
  233. }
  234. }
  235. foreach my $f (keys %{$opts{'remove'}}) {
  236. delete $fields->{$f};
  237. }
  238. my $filename = $opts{'filename'};
  239. unless (defined $filename) {
  240. $filename = $self->get_basename(1) . ".dsc";
  241. }
  242. open(DSC, ">", $filename) || syserr(_g("cannot write %s"), $filename);
  243. delete $fields->{'Checksums-Md5'}; # identical with Files field
  244. tied(%{$fields})->set_field_importance(@dsc_fields);
  245. tied(%{$fields})->output(\*DSC, $opts{'substvars'});
  246. close(DSC);
  247. }
  248. # vim: set et sw=4 ts=8
  249. 1;