V2.pm 17 KB

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