Package.pm 14 KB

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