V2.pm 14 KB

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