V2.pm 16 KB

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