Package.pm 9.0 KB

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