Package.pm 9.3 KB

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