V2.pm 16 KB

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