Package.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 subprocerr);
  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 Dpkg::Path qw(check_files_are_the_same);
  26. use POSIX;
  27. use File::Basename;
  28. # Public variables
  29. our $diff_ignore_default_regexp = '
  30. # Ignore general backup files
  31. (?:^|/).*~$|
  32. # Ignore emacs recovery files
  33. (?:^|/)\.#.*$|
  34. # Ignore vi swap files
  35. (?:^|/)\..*\.swp$|
  36. # Ignore baz-style junk files or directories
  37. (?:^|/),,.*(?:$|/.*$)|
  38. # File-names that should be ignored (never directories)
  39. (?:^|/)(?:DEADJOE|\.cvsignore|\.arch-inventory|\.bzrignore|\.gitignore)$|
  40. # File or directory names that should be ignored
  41. (?:^|/)(?:CVS|RCS|\.deps|\{arch\}|\.arch-ids|\.svn|\.hg|_darcs|\.git|
  42. \.shelf|_MTN|\.bzr(?:\.backup|tags)?)(?:$|/.*$)
  43. ';
  44. # Take out comments and newlines
  45. $diff_ignore_default_regexp =~ s/^#.*$//mg;
  46. $diff_ignore_default_regexp =~ s/\n//sg;
  47. no warnings 'qw';
  48. our @tar_ignore_default_pattern = qw(
  49. *.a
  50. *.la
  51. *.o
  52. *.so
  53. *.swp
  54. *~
  55. ,,*
  56. .[#~]*
  57. .arch-ids
  58. .arch-inventory
  59. .bzr
  60. .bzr.backup
  61. .bzr.tags
  62. .bzrignore
  63. .cvsignore
  64. .deps
  65. .git
  66. .gitignore
  67. .hg
  68. .shelf
  69. .svn
  70. CVS
  71. DEADJOE
  72. RCS
  73. _MTN
  74. _darcs
  75. {arch}
  76. );
  77. # Private stuff
  78. my @dsc_fields = (qw(Format Source Binary Architecture Version Origin
  79. Maintainer Uploaders Dm-Upload-Allowed Homepage
  80. Standards-Version Vcs-Browser Vcs-Arch Vcs-Bzr
  81. Vcs-Cvs Vcs-Darcs Vcs-Git Vcs-Hg Vcs-Mtn Vcs-Svn),
  82. @src_dep_fields,
  83. qw(Checksums-Md5 Checksums-Sha1 Checksums-Sha256 Files));
  84. # Object methods
  85. sub new {
  86. my ($this, %args) = @_;
  87. my $class = ref($this) || $this;
  88. my $self = {
  89. 'fields' => Dpkg::Fields::Object->new(),
  90. 'options' => {},
  91. };
  92. bless $self, $class;
  93. if (exists $args{"filename"}) {
  94. $self->initialize($args{"filename"});
  95. }
  96. if (exists $args{"options"}) {
  97. $self->{'options'} = $args{'options'};
  98. }
  99. $self->init_options();
  100. return $self;
  101. }
  102. sub init_options {
  103. my ($self) = @_;
  104. # Use full ignore list by default
  105. $self->{'options'}{'diff_ignore_regexp'} ||= $diff_ignore_default_regexp;
  106. $self->{'options'}{'tar_ignore'} = [ @tar_ignore_default_pattern ]
  107. unless @{$self->{'options'}{'tar_ignore'}};
  108. }
  109. sub initialize {
  110. my ($self, $filename) = @_;
  111. my ($fn, $dir) = fileparse($filename);
  112. error(_g("%s is not the name of a file"), $filename) unless $fn;
  113. $self->{'basedir'} = $dir || "./";
  114. $self->{'filename'} = $fn;
  115. # Check if it contains a signature
  116. open(DSC, "<", $filename) || syserr(_g("cannot open %s"), $filename);
  117. $self->{'is_signed'} = 0;
  118. while (<DSC>) {
  119. next if /^\s*$/o;
  120. $self->{'is_signed'} = 1 if /^-----BEGIN PGP SIGNED MESSAGE-----$/o;
  121. last;
  122. }
  123. close(DSC);
  124. # Read the fields
  125. open(CDATA, "<", $filename) || syserr(_g("cannot open %s"), $filename);
  126. my $fields = parsecdata(\*CDATA,
  127. sprintf(_g("source control file %s"), $filename),
  128. allow_pgp => 1);
  129. close(CDATA);
  130. $self->{'fields'} = $fields;
  131. foreach my $f (qw(Source Format Version Files)) {
  132. unless (defined($fields->{$f})) {
  133. error(_g("missing critical source control field %s"), $f);
  134. }
  135. }
  136. $self->parse_files();
  137. $self->upgrade_object_type();
  138. }
  139. sub upgrade_object_type {
  140. my ($self) = @_;
  141. my $format = $self->{'fields'}{'Format'};
  142. if ($format =~ /^([\d\.]+)(?:\s+\((.*)\))?$/) {
  143. my ($version, $variant) = ($1, $2);
  144. $version =~ s/\./_/;
  145. my $module = "Dpkg::Source::Package::V$version";
  146. $module .= "::$variant" if defined $variant;
  147. eval "require $module";
  148. if ($@) {
  149. error(_g("source package format `%s' is not supported (Perl module %s is required)"), $format, $module);
  150. }
  151. bless $self, $module;
  152. } else {
  153. error(_g("invalid Format field `%s'"), $format);
  154. }
  155. }
  156. sub get_filename {
  157. my ($self) = @_;
  158. return $self->{'basedir'} . $self->{'filename'};
  159. }
  160. sub get_files {
  161. my ($self) = @_;
  162. return keys %{$self->{'files'}};
  163. }
  164. sub parse_files {
  165. my ($self) = @_;
  166. my $rx_fname = qr/[0-9a-zA-Z][-+:.,=0-9a-zA-Z_~]+/;
  167. my $files = $self->{'fields'}{'Files'};
  168. foreach my $file (split(/\n /, $files)) {
  169. next if $file eq '';
  170. $file =~ m/^($check_regex{md5}) # checksum
  171. [ \t]+(\d+) # size
  172. [ \t]+($rx_fname) # filename
  173. $/x
  174. || error(_g("Files field contains bad line `%s'"), $file);
  175. if (exists $self->{'files'}{$3}) {
  176. error(_g("file `%s' listed twice in Files field"), $3);
  177. } else {
  178. $self->{'files'}{$3} = $2;
  179. }
  180. }
  181. }
  182. sub check_checksums {
  183. my ($self) = @_;
  184. my ($fields, %checksum, %size) = $self->{'fields'};
  185. my $has_md5 = 1;
  186. if (not exists $fields->{'Checksums-Md5'}) {
  187. $fields->{'Checksums-Md5'} = $fields->{'Files'};
  188. $has_md5 = 0;
  189. }
  190. # extract the checksums from the fields in two hashes
  191. readallchecksums($self->{'fields'}, \%checksum, \%size);
  192. delete $fields->{'Checksums-Md5'} unless $has_md5;
  193. # getchecksums verify the checksums if they are pre-filled
  194. foreach my $file ($self->get_files()) {
  195. getchecksums($self->{'basedir'} . $file, $checksum{$file},
  196. \$size{$file});
  197. }
  198. }
  199. sub get_basename {
  200. my ($self, $with_revision) = @_;
  201. my $f = $self->{'fields'};
  202. unless (exists $f->{'Source'} and exists $f->{'Version'}) {
  203. error(_g("source and version are required to compute the source basename"));
  204. }
  205. my %v = parseversion($f->{'Version'});
  206. my $basename = $f->{'Source'} . "_" . $v{"version"};
  207. if ($with_revision and $f->{'Version'} =~ /-/) {
  208. $basename .= "-" . $v{'revision'};
  209. }
  210. return $basename;
  211. }
  212. sub find_original_tarballs {
  213. my ($self, $ext) = @_;
  214. $ext ||= $comp_regex;
  215. my $basename = $self->get_basename();
  216. my @tar;
  217. foreach my $dir (".", $self->{'basedir'}, $self->{'options'}{'origtardir'}) {
  218. next unless defined($dir) and -d $dir;
  219. opendir(DIR, $dir) || syserr(_g("cannot opendir %s"), $dir);
  220. push @tar, map { "$dir/$_" }
  221. grep { /^\Q$basename\E\.orig(-\w+)?\.tar\.$ext$/ }
  222. readdir(DIR);
  223. closedir(DIR);
  224. }
  225. return @tar;
  226. }
  227. sub is_signed {
  228. my $self = shift;
  229. return $self->{'is_signed'};
  230. }
  231. sub check_signature {
  232. my ($self) = @_;
  233. my $dsc = $self->get_filename();
  234. if (-x '/usr/bin/gpg') {
  235. my $gpg_command = 'gpg -q --verify ';
  236. if (-r '/usr/share/keyrings/debian-keyring.gpg') {
  237. $gpg_command = $gpg_command.'--keyring /usr/share/keyrings/debian-keyring.gpg ';
  238. }
  239. $gpg_command = $gpg_command.quotemeta($dsc).' 2>&1';
  240. #TODO: cleanup here
  241. my @gpg_output = `$gpg_command`;
  242. my $gpg_status = $? >> 8;
  243. if ($gpg_status) {
  244. print STDERR join("",@gpg_output);
  245. error(_g("failed to verify signature on %s"), $dsc)
  246. if ($gpg_status == 1);
  247. }
  248. } else {
  249. warning(_g("could not verify signature on %s since gpg isn't installed"),
  250. $dsc);
  251. }
  252. }
  253. sub parse_cmdline_options {
  254. my ($self, @opts) = @_;
  255. foreach (@opts) {
  256. if (not $self->parse_cmdline_option($_)) {
  257. warning(_g("%s is not a valid option for %s"), $_, ref($self));
  258. }
  259. }
  260. }
  261. sub parse_cmdline_option {
  262. return 0;
  263. }
  264. sub extract {
  265. my $self = shift;
  266. my $newdirectory = $_[0];
  267. # Copy orig tarballs
  268. if ($self->{'options'}{'copy_orig_tarballs'}) {
  269. my $basename = $self->get_basename();
  270. my ($dirname, $destdir) = fileparse($newdirectory);
  271. $destdir ||= "./";
  272. foreach my $orig (grep { /^\Q$basename\E\.orig(-\w+)?\.tar\.$comp_regex$/ }
  273. $self->get_files())
  274. {
  275. my $src = File::Spec->catfile($self->{'basedir'}, $orig);
  276. my $dst = File::Spec->catfile($destdir, $orig);
  277. if (not check_files_are_the_same($src, $dst, 1)) {
  278. system('cp', '--', $src, $dst);
  279. subprocerr("cp $src to $dst") if $?;
  280. }
  281. }
  282. }
  283. # Try extract
  284. eval { $self->do_extract(@_) };
  285. if ($@) {
  286. &$_() foreach reverse @Dpkg::Exit::handlers;
  287. die $@;
  288. }
  289. # Make sure debian/rules is executable
  290. my $rules = File::Spec->catfile($newdirectory, "debian", "rules");
  291. my @s = lstat($rules);
  292. if (not scalar(@s)) {
  293. unless ($! == ENOENT) {
  294. syserr(_g("cannot stat %s"), $rules);
  295. }
  296. warning(_g("%s does not exist"), $rules);
  297. } elsif (-f _) {
  298. chmod($s[2] | 0111, $rules) ||
  299. syserr(_g("cannot make %s executable"), $rules);
  300. } else {
  301. warning(_g("%s is not a plain file"), $rules);
  302. }
  303. }
  304. sub do_extract {
  305. error("Dpkg::Source::Package doesn't know how to unpack a source package. Use one of the subclasses.");
  306. }
  307. # Function used specifically during creation of a source package
  308. sub build {
  309. my $self = shift;
  310. eval { $self->do_build(@_) };
  311. if ($@) {
  312. &$_() foreach reverse @Dpkg::Exit::handlers;
  313. die $@;
  314. }
  315. }
  316. sub do_build {
  317. error("Dpkg::Source::Package doesn't know how to build a source package. Use one of the subclasses.");
  318. }
  319. sub can_build {
  320. my ($self, $dir) = @_;
  321. return (0, "can_build() has not been overriden");
  322. }
  323. sub add_file {
  324. my ($self, $filename) = @_;
  325. my ($fn, $dir) = fileparse($filename);
  326. if (exists $self->{'files'}{$fn}) {
  327. internerr(_g("tried to add file `%s' twice"), $fn);
  328. }
  329. my (%sums, $size);
  330. getchecksums($filename, \%sums, \$size);
  331. $self->{'files'}{$fn} = $size;
  332. foreach my $alg (sort keys %sums) {
  333. $self->{'fields'}{"Checksums-$alg"} .= "\n $sums{$alg} $size $fn";
  334. }
  335. $self->{'fields'}{'Files'}.= "\n $sums{md5} $size $fn";
  336. }
  337. sub write_dsc {
  338. my ($self, %opts) = @_;
  339. my $fields = $self->{'fields'};
  340. foreach my $f (keys %{$opts{'override'}}) {
  341. $fields->{$f} = $opts{'override'}{$f};
  342. }
  343. unless($opts{'nocheck'}) {
  344. foreach my $f (qw(Source Version)) {
  345. unless (defined($fields->{$f})) {
  346. error(_g("missing information for critical output field %s"), $f);
  347. }
  348. }
  349. foreach my $f (qw(Maintainer Architecture Standards-Version)) {
  350. unless (defined($fields->{$f})) {
  351. warning(_g("missing information for output field %s"), $f);
  352. }
  353. }
  354. }
  355. foreach my $f (keys %{$opts{'remove'}}) {
  356. delete $fields->{$f};
  357. }
  358. my $filename = $opts{'filename'};
  359. unless (defined $filename) {
  360. $filename = $self->get_basename(1) . ".dsc";
  361. }
  362. open(DSC, ">", $filename) || syserr(_g("cannot write %s"), $filename);
  363. delete $fields->{'Checksums-Md5'}; # identical with Files field
  364. tied(%{$fields})->set_field_importance(@dsc_fields);
  365. tied(%{$fields})->output(\*DSC, $opts{'substvars'});
  366. close(DSC);
  367. }
  368. # vim: set et sw=4 ts=8
  369. 1;