V2.pm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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::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. $self->{'options'}{'skip_debianization'} = 0
  47. unless exists $self->{'options'}{'skip_debianization'};
  48. }
  49. sub parse_cmdline_option {
  50. my ($self, $opt) = @_;
  51. if ($opt =~ /^--include-removal$/) {
  52. $self->{'options'}{'include_removal'} = 1;
  53. return 1;
  54. } elsif ($opt =~ /^--include-timestamp$/) {
  55. $self->{'options'}{'include_timestamp'} = 1;
  56. return 1;
  57. } elsif ($opt =~ /^--include-binaries$/) {
  58. $self->{'options'}{'include_binaries'} = 1;
  59. return 1;
  60. } elsif ($opt =~ /^--no-preparation$/) {
  61. $self->{'options'}{'preparation'} = 0;
  62. return 1;
  63. } elsif ($opt =~ /^--skip-patches$/) {
  64. $self->{'options'}{'skip_patches'} = 1;
  65. return 1;
  66. } elsif ($opt =~ /^--skip-debianization$/) {
  67. $self->{'options'}{'skip_debianization'} = 1;
  68. return 1;
  69. }
  70. return 0;
  71. }
  72. sub do_extract {
  73. my ($self, $newdirectory) = @_;
  74. my $fields = $self->{'fields'};
  75. my $dscdir = $self->{'basedir'};
  76. check_version($fields->{'Version'});
  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) unless $self->{'options'}{'skip_patches'};
  136. }
  137. sub get_autopatch_name {
  138. return "zz_debian-diff-auto";
  139. }
  140. sub get_patches {
  141. my ($self, $dir, $skip_auto) = @_;
  142. my @patches;
  143. my $pd = "$dir/debian/patches";
  144. my $auto_patch = $self->get_autopatch_name();
  145. if (-d $pd) {
  146. opendir(DIR, $pd) || syserr(_g("cannot opendir %s"), $pd);
  147. foreach my $patch (sort readdir(DIR)) {
  148. # patches match same rules as run-parts
  149. next unless $patch =~ /^[\w-]+$/ and -f "$pd/$patch";
  150. next if $skip_auto and $patch eq $auto_patch;
  151. push @patches, $patch;
  152. }
  153. closedir(DIR);
  154. }
  155. return @patches;
  156. }
  157. sub apply_patches {
  158. my ($self, $dir, $skip_auto) = @_;
  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("can't 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. find({ wanted => $check_binary, no_chdir => 1 }, File::Spec->catdir($dir, "debian"));
  292. error(_g("detected %d unwanted binary file(s) " .
  293. "(add them in debian/source/include-binaries to allow their " .
  294. "inclusion)."), $unwanted_binaries) if $unwanted_binaries;
  295. # Create a patch
  296. my ($difffh, $tmpdiff) = tempfile("$basenamerev.diff.XXXXXX",
  297. DIR => $updir, UNLINK => 0);
  298. push @Dpkg::Exit::handlers, sub { unlink($tmpdiff) };
  299. my $diff = Dpkg::Source::Patch->new(filename => $tmpdiff,
  300. compression => "none");
  301. $diff->create();
  302. $diff->add_diff_directory($tmp, $dir, basedirname => $basedirname,
  303. %{$self->{'diff_options'}}, handle_binary_func => $handle_binary);
  304. error(_g("unrepresentable changes to source")) if not $diff->finish();
  305. # The previous auto-patch must be removed, it has not been used and it
  306. # will be recreated if it's still needed
  307. my $autopatch = File::Spec->catfile($dir, "debian", "patches",
  308. $self->get_autopatch_name());
  309. if (-e $autopatch) {
  310. unlink($autopatch) || syserr(_g("cannot remove %s"), $autopatch);
  311. }
  312. # Install the diff as the new autopatch
  313. if (not -s $tmpdiff) {
  314. unlink($tmpdiff) || syserr(_g("cannot remove %s"), $tmpdiff);
  315. } else {
  316. mkpath(File::Spec->catdir($dir, "debian", "patches"));
  317. info(_g("local changes stored in %s, the modified files are:"), $autopatch);
  318. my $analysis = $diff->analyze($dir);
  319. foreach my $fn (sort keys %{$analysis->{'filepatched'}}) {
  320. print " $fn\n";
  321. }
  322. rename($tmpdiff, $autopatch) ||
  323. syserr(_g("cannot rename %s to %s"), $tmpdiff, $autopatch);
  324. chmod(0666 & ~ umask(), $autopatch) ||
  325. syserr(_g("unable to change permission of `%s'"), $autopatch);
  326. }
  327. $self->register_autopatch($dir);
  328. pop @Dpkg::Exit::handlers;
  329. # Remove the temporary directory
  330. erasedir($tmp);
  331. pop @Dpkg::Exit::handlers;
  332. # Update debian/source/include-binaries if needed
  333. if (scalar(@binary_files) and $include_binaries) {
  334. mkpath(File::Spec->catdir($dir, "debian", "source"));
  335. open(INC, ">>", $incbin_file) || syserr(_g("can't write %s"), $incbin_file);
  336. foreach my $binary (@binary_files) {
  337. unless ($auth_bin_files{$binary}) {
  338. print INC "$binary\n";
  339. info(_g("adding %s to %s"), $binary, "debian/source/include-binaries");
  340. }
  341. }
  342. close(INC);
  343. }
  344. # Create the debian.tar
  345. my $debianfile = "$basenamerev.debian.tar." . $self->{'options'}{'comp_ext'};
  346. info(_g("building %s in %s"), $sourcepackage, $debianfile);
  347. $tar = Dpkg::Source::Archive->new(filename => $debianfile);
  348. $tar->create(options => \@tar_ignore, 'chdir' => $dir);
  349. $tar->add_directory("debian");
  350. foreach my $binary (@binary_files) {
  351. $tar->add_file($binary) unless $binary =~ m{^debian/};
  352. }
  353. $tar->finish();
  354. $self->add_file($debianfile);
  355. }
  356. sub register_autopatch {
  357. my ($self, $dir) = @_;
  358. my $autopatch = File::Spec->catfile($dir, "debian", "patches",
  359. $self->get_autopatch_name());
  360. if (-e $autopatch) {
  361. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  362. open(APPLIED, '>>', $applied) || syserr(_g("cannot write %s"), $applied);
  363. print APPLIED ($self->get_autopatch_name() . "\n");
  364. close(APPLIED);
  365. }
  366. }
  367. # vim:et:sw=4:ts=8
  368. 1;