V2.pm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. # Copyright © 2008 Raphaël Hertzog <hertzog@debian.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package Dpkg::Source::Package::V2;
  16. use strict;
  17. use warnings;
  18. use base 'Dpkg::Source::Package';
  19. use Dpkg;
  20. use Dpkg::Gettext;
  21. use Dpkg::ErrorHandling;
  22. use Dpkg::Compression;
  23. use Dpkg::Source::Archive;
  24. use Dpkg::Source::Patch;
  25. use Dpkg::Exit;
  26. use Dpkg::Source::Functions qw(erasedir is_binary);
  27. use POSIX;
  28. use File::Basename;
  29. use File::Temp qw(tempfile tempdir);
  30. use File::Path;
  31. use File::Spec;
  32. use File::Find;
  33. our $CURRENT_MINOR_VERSION = "0";
  34. sub init_options {
  35. my ($self) = @_;
  36. $self->SUPER::init_options();
  37. $self->{'options'}{'include_removal'} = 0
  38. unless exists $self->{'options'}{'include_removal'};
  39. $self->{'options'}{'include_timestamp'} = 0
  40. unless exists $self->{'options'}{'include_timestamp'};
  41. $self->{'options'}{'include_binaries'} = 0
  42. unless exists $self->{'options'}{'include_binaries'};
  43. $self->{'options'}{'preparation'} = 1
  44. unless exists $self->{'options'}{'preparation'};
  45. $self->{'options'}{'skip_patches'} = 0
  46. unless exists $self->{'options'}{'skip_patches'};
  47. $self->{'options'}{'skip_debianization'} = 0
  48. unless exists $self->{'options'}{'skip_debianization'};
  49. }
  50. sub parse_cmdline_option {
  51. my ($self, $opt) = @_;
  52. if ($opt =~ /^--include-removal$/) {
  53. $self->{'options'}{'include_removal'} = 1;
  54. return 1;
  55. } elsif ($opt =~ /^--include-timestamp$/) {
  56. $self->{'options'}{'include_timestamp'} = 1;
  57. return 1;
  58. } elsif ($opt =~ /^--include-binaries$/) {
  59. $self->{'options'}{'include_binaries'} = 1;
  60. return 1;
  61. } elsif ($opt =~ /^--no-preparation$/) {
  62. $self->{'options'}{'preparation'} = 0;
  63. return 1;
  64. } elsif ($opt =~ /^--skip-patches$/) {
  65. $self->{'options'}{'skip_patches'} = 1;
  66. return 1;
  67. } elsif ($opt =~ /^--skip-debianization$/) {
  68. $self->{'options'}{'skip_debianization'} = 1;
  69. return 1;
  70. }
  71. return 0;
  72. }
  73. sub do_extract {
  74. my ($self, $newdirectory) = @_;
  75. my $fields = $self->{'fields'};
  76. my $dscdir = $self->{'basedir'};
  77. my $basename = $self->get_basename();
  78. my $basenamerev = $self->get_basename(1);
  79. my ($tarfile, $debianfile, %origtar, %seen);
  80. foreach my $file ($self->get_files()) {
  81. (my $uncompressed = $file) =~ s/\.$comp_regex$//;
  82. error(_g("duplicate files in %s source package: %s.*"), "v2.0",
  83. $uncompressed) if $seen{$uncompressed};
  84. $seen{$uncompressed} = 1;
  85. if ($file =~ /^\Q$basename\E\.orig\.tar\.$comp_regex$/) {
  86. $tarfile = $file;
  87. } elsif ($file =~ /^\Q$basename\E\.orig-([\w-]+)\.tar\.$comp_regex$/) {
  88. $origtar{$1} = $file;
  89. } elsif ($file =~ /^\Q$basenamerev\E\.debian\.tar\.$comp_regex$/) {
  90. $debianfile = $file;
  91. } else {
  92. error(_g("unrecognized file for a %s source package: %s"),
  93. "v2.0", $file);
  94. }
  95. }
  96. unless ($tarfile and $debianfile) {
  97. error(_g("missing orig.tar or debian.tar file in v2.0 source package"));
  98. }
  99. erasedir($newdirectory);
  100. # Extract main tarball
  101. info(_g("unpacking %s"), $tarfile);
  102. my $tar = Dpkg::Source::Archive->new(filename => "$dscdir$tarfile");
  103. $tar->extract($newdirectory, no_fixperms => 1);
  104. # Extract additional orig tarballs
  105. foreach my $subdir (keys %origtar) {
  106. my $file = $origtar{$subdir};
  107. info(_g("unpacking %s"), $file);
  108. if (-e "$newdirectory/$subdir") {
  109. warning(_g("required removal of `%s' installed by original tarball"), $subdir);
  110. erasedir("$newdirectory/$subdir");
  111. }
  112. $tar = Dpkg::Source::Archive->new(filename => "$dscdir$file");
  113. $tar->extract("$newdirectory/$subdir", no_fixperms => 1);
  114. }
  115. # Stop here if debianization is not wanted
  116. return if $self->{'options'}{'skip_debianization'};
  117. # Extract debian tarball after removing the debian directory
  118. info(_g("unpacking %s"), $debianfile);
  119. erasedir("$newdirectory/debian");
  120. # Exclude existing symlinks from extraction of debian.tar.gz as we
  121. # don't want to overwrite something outside of $newdirectory due to a
  122. # symlink
  123. my @exclude_symlinks;
  124. my $wanted = sub {
  125. return if not -l $_;
  126. my $fn = File::Spec->abs2rel($_, $newdirectory);
  127. push @exclude_symlinks, "--exclude", $fn;
  128. };
  129. find({ wanted => $wanted, no_chdir => 1 }, $newdirectory);
  130. $tar = Dpkg::Source::Archive->new(filename => "$dscdir$debianfile");
  131. $tar->extract($newdirectory, in_place => 1,
  132. options => [ '--anchored', '--no-wildcards',
  133. @exclude_symlinks ]);
  134. # Apply patches (in a separate method as it might be overriden)
  135. $self->apply_patches($newdirectory, usage => 'unpack')
  136. unless $self->{'options'}{'skip_patches'};
  137. }
  138. sub get_autopatch_name {
  139. return "zz_debian-diff-auto";
  140. }
  141. sub get_patches {
  142. my ($self, $dir, %opts) = @_;
  143. $opts{"skip_auto"} = 0 unless defined($opts{"skip_auto"});
  144. my @patches;
  145. my $pd = "$dir/debian/patches";
  146. my $auto_patch = $self->get_autopatch_name();
  147. if (-d $pd) {
  148. opendir(DIR, $pd) || syserr(_g("cannot opendir %s"), $pd);
  149. foreach my $patch (sort readdir(DIR)) {
  150. # patches match same rules as run-parts
  151. next unless $patch =~ /^[\w-]+$/ and -f "$pd/$patch";
  152. next if $opts{"skip_auto"} and $patch eq $auto_patch;
  153. push @patches, $patch;
  154. }
  155. closedir(DIR);
  156. }
  157. return @patches;
  158. }
  159. sub apply_patches {
  160. my ($self, $dir, %opts) = @_;
  161. $opts{"skip_auto"} = 0 unless defined($opts{"skip_auto"});
  162. my @patches = $self->get_patches($dir, %opts);
  163. return unless scalar(@patches);
  164. my $timestamp = time();
  165. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  166. open(APPLIED, '>', $applied) || syserr(_g("cannot write %s"), $applied);
  167. foreach my $patch ($self->get_patches($dir, %opts)) {
  168. my $path = File::Spec->catfile($dir, "debian", "patches", $patch);
  169. info(_g("applying %s"), $patch) unless $opts{"skip_auto"};
  170. my $patch_obj = Dpkg::Source::Patch->new(filename => $path);
  171. $patch_obj->apply($dir, force_timestamp => 1,
  172. timestamp => $timestamp,
  173. add_options => [ '-E' ]);
  174. print APPLIED "$patch\n";
  175. }
  176. close(APPLIED);
  177. }
  178. sub can_build {
  179. my ($self, $dir) = @_;
  180. foreach ($self->find_original_tarballs()) {
  181. return 1 if /\.orig\.tar\.$comp_regex$/;
  182. }
  183. return (0, _g("no orig.tar file found"));
  184. }
  185. sub prepare_build {
  186. my ($self, $dir) = @_;
  187. $self->{'diff_options'} = {
  188. diff_ignore_regexp => $self->{'options'}{'diff_ignore_regexp'},
  189. include_removal => $self->{'options'}{'include_removal'},
  190. include_timestamp => $self->{'options'}{'include_timestamp'},
  191. use_dev_null => 1,
  192. };
  193. push @{$self->{'options'}{'tar_ignore'}}, "debian/patches/.dpkg-source-applied";
  194. $self->check_patches_applied($dir) if $self->{'options'}{'preparation'};
  195. }
  196. sub check_patches_applied {
  197. my ($self, $dir) = @_;
  198. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  199. unless (-e $applied) {
  200. warning(_g("patches have not been applied, applying them now (use --no-preparation to override)"));
  201. $self->apply_patches($dir, usage => 'preparation');
  202. }
  203. }
  204. sub do_build {
  205. my ($self, $dir) = @_;
  206. my ($dirname, $updir) = fileparse($dir);
  207. my @argv = @{$self->{'options'}{'ARGV'}};
  208. if (scalar(@argv)) {
  209. usageerr(_g("-b takes only one parameter with format `%s'"),
  210. $self->{'fields'}{'Format'});
  211. }
  212. $self->prepare_build($dir);
  213. my $include_binaries = $self->{'options'}{'include_binaries'};
  214. my @tar_ignore = map { "--exclude=$_" } @{$self->{'options'}{'tar_ignore'}};
  215. my $sourcepackage = $self->{'fields'}{'Source'};
  216. my $basenamerev = $self->get_basename(1);
  217. my $basename = $self->get_basename();
  218. my $basedirname = $basename;
  219. $basedirname =~ s/_/-/;
  220. # Identify original tarballs
  221. my ($tarfile, %origtar);
  222. my @origtarballs;
  223. foreach (sort $self->find_original_tarballs()) {
  224. if (/\.orig\.tar\.$comp_regex$/) {
  225. if (defined($tarfile)) {
  226. error(_g("several orig.tar files found (%s and %s) but only " .
  227. "one is allowed"), $tarfile, $_);
  228. }
  229. $tarfile = $_;
  230. push @origtarballs, $_;
  231. $self->add_file($_);
  232. } elsif (/\.orig-([\w-]+)\.tar\.$comp_regex$/) {
  233. $origtar{$1} = $_;
  234. push @origtarballs, $_;
  235. $self->add_file($_);
  236. }
  237. }
  238. error(_g("no orig.tar file found")) unless $tarfile;
  239. info(_g("building %s using existing %s"),
  240. $sourcepackage, "@origtarballs");
  241. # Unpack a second copy for comparison
  242. my $tmp = tempdir("$dirname.orig.XXXXXX", DIR => $updir);
  243. push @Dpkg::Exit::handlers, sub { erasedir($tmp) };
  244. # Extract main tarball
  245. my $tar = Dpkg::Source::Archive->new(filename => $tarfile);
  246. $tar->extract($tmp);
  247. # Extract additional orig tarballs
  248. foreach my $subdir (keys %origtar) {
  249. my $file = $origtar{$subdir};
  250. $tar = Dpkg::Source::Archive->new(filename => $file);
  251. $tar->extract("$tmp/$subdir");
  252. }
  253. # Copy over the debian directory
  254. system("cp", "-a", "--", "$dir/debian", "$tmp/");
  255. subprocerr(_g("copy of the debian directory")) if $?;
  256. # Apply all patches except the last automatic one
  257. $self->apply_patches($tmp, skip_auto => 1, usage => 'build');
  258. # Prepare handling of binary files
  259. my %auth_bin_files;
  260. my $incbin_file = File::Spec->catfile($dir, "debian", "source", "include-binaries");
  261. if (-f $incbin_file) {
  262. open(INC, "<", $incbin_file) || syserr(_g("cannot read %s"), $incbin_file);
  263. while(defined($_ = <INC>)) {
  264. chomp; s/^\s*//; s/\s*$//;
  265. next if /^#/ or /^$/;
  266. $auth_bin_files{$_} = 1;
  267. }
  268. close(INC);
  269. }
  270. my @binary_files;
  271. my $handle_binary = sub {
  272. my ($self, $old, $new) = @_;
  273. my $relfn = File::Spec->abs2rel($new, $dir);
  274. # Include binaries if they are whitelisted or if
  275. # --include-binaries has been given
  276. if ($include_binaries or $auth_bin_files{$relfn}) {
  277. push @binary_files, $relfn;
  278. } else {
  279. errormsg(_g("cannot represent change to %s: %s"), $new,
  280. _g("binary file contents changed"));
  281. errormsg(_g("add %s in debian/source/include-binaries if you want" .
  282. " to store the modified binary in the debian tarball"),
  283. $relfn);
  284. $self->register_error();
  285. }
  286. };
  287. # Check if the debian directory contains unwanted binary files
  288. my $unwanted_binaries = 0;
  289. my $check_binary = sub {
  290. my $fn = File::Spec->abs2rel($_, $dir);
  291. if (-f $_ and is_binary($_)) {
  292. if ($include_binaries or $auth_bin_files{$fn}) {
  293. push @binary_files, $fn;
  294. } else {
  295. errormsg(_g("unwanted binary file: %s"), $fn);
  296. $unwanted_binaries++;
  297. }
  298. }
  299. };
  300. my $tar_ignore_glob = "{" . join(",",
  301. map {
  302. my $copy = $_;
  303. $copy =~ s/,/\\,/g;
  304. $copy;
  305. } @{$self->{'options'}{'tar_ignore'}}) . "}";
  306. my $filter_ignore = sub {
  307. # Filter out files that are not going to be included in the debian
  308. # tarball due to ignores.
  309. my %exclude;
  310. my $reldir = File::Spec->abs2rel($File::Find::dir, $dir);
  311. foreach my $fn (glob($tar_ignore_glob)) {
  312. $exclude{$fn} = 1;
  313. }
  314. my @result;
  315. foreach my $fn (@_) {
  316. unless (exists $exclude{$fn} or exists $exclude{"$reldir/$fn"}) {
  317. push @result, $fn;
  318. }
  319. }
  320. return @result;
  321. };
  322. find({ wanted => $check_binary, preprocess => $filter_ignore,
  323. no_chdir => 1 }, File::Spec->catdir($dir, "debian"));
  324. error(_g("detected %d unwanted binary file(s) " .
  325. "(add them in debian/source/include-binaries to allow their " .
  326. "inclusion)."), $unwanted_binaries) if $unwanted_binaries;
  327. # Create a patch
  328. my $autopatch = File::Spec->catfile($dir, "debian", "patches",
  329. $self->get_autopatch_name());
  330. my ($difffh, $tmpdiff) = tempfile("$basenamerev.diff.XXXXXX",
  331. DIR => $updir, UNLINK => 0);
  332. push @Dpkg::Exit::handlers, sub { unlink($tmpdiff) };
  333. my $diff = Dpkg::Source::Patch->new(filename => $tmpdiff,
  334. compression => "none");
  335. $diff->create();
  336. $diff->set_header($self->get_patch_header($dir, $autopatch));
  337. $diff->add_diff_directory($tmp, $dir, basedirname => $basedirname,
  338. %{$self->{'diff_options'}}, handle_binary_func => $handle_binary);
  339. error(_g("unrepresentable changes to source")) if not $diff->finish();
  340. # The previous auto-patch must be removed, it has not been used and it
  341. # will be recreated if it's still needed
  342. if (-e $autopatch) {
  343. unlink($autopatch) || syserr(_g("cannot remove %s"), $autopatch);
  344. }
  345. # Install the diff as the new autopatch
  346. if (not -s $tmpdiff) {
  347. unlink($tmpdiff) || syserr(_g("cannot remove %s"), $tmpdiff);
  348. } else {
  349. mkpath(File::Spec->catdir($dir, "debian", "patches"));
  350. info(_g("local changes stored in %s, the modified files are:"), $autopatch);
  351. my $analysis = $diff->analyze($dir);
  352. foreach my $fn (sort keys %{$analysis->{'filepatched'}}) {
  353. print " $fn\n";
  354. }
  355. rename($tmpdiff, $autopatch) ||
  356. syserr(_g("cannot rename %s to %s"), $tmpdiff, $autopatch);
  357. chmod(0666 & ~ umask(), $autopatch) ||
  358. syserr(_g("unable to change permission of `%s'"), $autopatch);
  359. }
  360. $self->register_autopatch($dir);
  361. rmdir(File::Spec->catdir($dir, "debian", "patches")); # No check on purpose
  362. pop @Dpkg::Exit::handlers;
  363. # Remove the temporary directory
  364. erasedir($tmp);
  365. pop @Dpkg::Exit::handlers;
  366. # Update debian/source/include-binaries if needed
  367. if (scalar(@binary_files) and $include_binaries) {
  368. mkpath(File::Spec->catdir($dir, "debian", "source"));
  369. open(INC, ">>", $incbin_file) || syserr(_g("cannot write %s"), $incbin_file);
  370. foreach my $binary (@binary_files) {
  371. unless ($auth_bin_files{$binary}) {
  372. print INC "$binary\n";
  373. info(_g("adding %s to %s"), $binary, "debian/source/include-binaries");
  374. }
  375. }
  376. close(INC);
  377. }
  378. # Create the debian.tar
  379. my $debianfile = "$basenamerev.debian.tar." . $self->{'options'}{'comp_ext'};
  380. info(_g("building %s in %s"), $sourcepackage, $debianfile);
  381. $tar = Dpkg::Source::Archive->new(filename => $debianfile);
  382. $tar->create(options => \@tar_ignore, 'chdir' => $dir);
  383. $tar->add_directory("debian");
  384. foreach my $binary (@binary_files) {
  385. $tar->add_file($binary) unless $binary =~ m{^debian/};
  386. }
  387. $tar->finish();
  388. $self->add_file($debianfile);
  389. }
  390. sub get_patch_header {
  391. my ($self, $dir, $previous) = @_;
  392. my $ph = File::Spec->catfile($dir, "debian", "source", "patch-header");
  393. my $text;
  394. if (-f $ph) {
  395. open(PH, "<", $ph) || syserr(_g("cannot read %s"), $ph);
  396. $text = join("", <PH>);
  397. close(PH);
  398. return $text;
  399. }
  400. return "Description: Undocumented upstream changes
  401. This patch has been created by dpkg-source during the package build
  402. but it might have accumulated changes from several uploads. Please
  403. check the changelog to (hopefully) learn more on those changes.\n\n";
  404. }
  405. sub register_autopatch {
  406. my ($self, $dir) = @_;
  407. my $autopatch = File::Spec->catfile($dir, "debian", "patches",
  408. $self->get_autopatch_name());
  409. if (-e $autopatch) {
  410. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  411. open(APPLIED, '>>', $applied) || syserr(_g("cannot write %s"), $applied);
  412. print APPLIED ($self->get_autopatch_name() . "\n");
  413. close(APPLIED);
  414. }
  415. }
  416. # vim:et:sw=4:ts=8
  417. 1;