quilt.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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::V3::quilt;
  13. use strict;
  14. use warnings;
  15. # Based on wig&pen implementation
  16. use base 'Dpkg::Source::Package::V2';
  17. use Dpkg;
  18. use Dpkg::Gettext;
  19. use Dpkg::ErrorHandling;
  20. use Dpkg::Source::Patch;
  21. use Dpkg::IPC;
  22. use Dpkg::Vendor qw(get_current_vendor);
  23. use POSIX;
  24. use File::Basename;
  25. use File::Spec;
  26. use File::Path;
  27. our $CURRENT_MINOR_VERSION = "0";
  28. sub init_options {
  29. my ($self) = @_;
  30. $self->SUPER::init_options();
  31. # By default use quilt, unless it's not available
  32. $self->{'options'}{'without_quilt'} = (-x "/usr/bin/quilt") ? 0 : 1
  33. unless exists $self->{'options'}{'without_quilt'};
  34. }
  35. sub parse_cmdline_option {
  36. my ($self, $opt) = @_;
  37. return 1 if $self->SUPER::parse_cmdline_option($opt);
  38. if ($opt =~ /^--without-quilt$/) {
  39. $self->{'options'}{'without_quilt'} = 1;
  40. return 1;
  41. }
  42. return 0;
  43. }
  44. sub get_autopatch_name {
  45. my ($self) = @_;
  46. return "debian-changes-" . $self->{'fields'}{'Version'};
  47. }
  48. sub get_series_file {
  49. my ($self, $dir) = @_;
  50. my $pd = File::Spec->catdir($dir, "debian", "patches");
  51. my $vendor = lc(get_current_vendor() || "debian");
  52. foreach (File::Spec->catfile($pd, "$vendor.series"),
  53. File::Spec->catfile($pd, "series")) {
  54. return $_ if -e $_;
  55. }
  56. return undef;
  57. }
  58. sub get_patches {
  59. my ($self, $dir, $skip_auto) = @_;
  60. my @patches;
  61. my $auto_patch = $self->get_autopatch_name();
  62. my $series = $self->get_series_file($dir);
  63. if (defined($series)) {
  64. open(SERIES, "<" , $series) || syserr(_g("cannot read %s"), $series);
  65. while(defined($_ = <SERIES>)) {
  66. chomp; s/^\s+//; s/\s+$//; # Strip leading/trailing spaces
  67. s/(^|\s+)#.*$//; # Strip comment
  68. next unless $_;
  69. if (/^(\S+)\s+(.*)$/) {
  70. $_ = $1;
  71. if ($2 ne '-p1') {
  72. warning(_g("the series file (%s) contains unsupported " .
  73. "options ('%s', line %s), dpkg-source might " .
  74. "fail when applying patches."),
  75. $series, $2, $.) unless $skip_auto;
  76. }
  77. }
  78. next if $skip_auto and $_ eq $auto_patch;
  79. push @patches, $_;
  80. }
  81. close(SERIES);
  82. }
  83. return @patches;
  84. }
  85. sub run_quilt {
  86. my ($self, $dir, $params, %more_opts) = @_;
  87. $params = [ $params ] unless ref($params) eq "ARRAY";
  88. my $absdir = $dir;
  89. unless (File::Spec->file_name_is_absolute($absdir)) {
  90. $absdir = File::Spec->rel2abs($dir);
  91. }
  92. my $series = $self->get_series_file($dir);
  93. # Use default name if no series files exist yet
  94. $series = "$absdir/debian/patches/series" unless defined $series;
  95. unless (File::Spec->file_name_is_absolute($series)) {
  96. $series = File::Spec->rel2abs($series);
  97. }
  98. my %opts = (
  99. env => { QUILT_PATCHES => "$absdir/debian/patches",
  100. QUILT_SERIES => $series },
  101. 'chdir' => $dir,
  102. 'exec' => [ 'quilt', '--quiltrc', '/dev/null', @$params ],
  103. %more_opts
  104. );
  105. my $pid = fork_and_exec(%opts);
  106. return $pid;
  107. }
  108. sub apply_patches {
  109. my ($self, $dir, $skip_auto) = @_;
  110. # Update debian/patches/series symlink if needed to allow quilt usage
  111. my $series = $self->get_series_file($dir);
  112. return unless $series; # No series, no patches
  113. my $basename = basename($series);
  114. if ($basename ne "series") {
  115. my $dest = File::Spec->catfile($dir, "debian", "patches", "series");
  116. unlink($dest) if -l $dest;
  117. unless (-f _) { # Don't overwrite real files
  118. symlink($basename, $dest) ||
  119. syserr(_g("can't create symlink %s"), $dest);
  120. }
  121. }
  122. my @patches = $self->get_patches($dir, $skip_auto);
  123. return unless scalar(@patches);
  124. # Apply patches
  125. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  126. open(APPLIED, '>', $applied) || syserr(_g("cannot write %s"), $applied);
  127. my $now = time();
  128. my $pobj = {};
  129. my $panalysis = {};
  130. foreach my $patch (@patches) {
  131. my $path = File::Spec->catfile($dir, "debian", "patches", $patch);
  132. $pobj->{$patch} = Dpkg::Source::Patch->new(filename => $path);
  133. if ($self->{'options'}{'without_quilt'}) {
  134. info(_g("applying %s"), $patch) unless $skip_auto;
  135. $pobj->{$patch}->apply($dir, timestamp => $now,
  136. force_timestamp => 1, create_dirs => 1,
  137. add_options => [ '-E' ]);
  138. print APPLIED "$patch\n";
  139. } else {
  140. $panalysis->{$patch} = $pobj->{$patch}->analyze($dir);
  141. foreach my $dir (keys %{$panalysis->{$patch}->{'dirtocreate'}}) {
  142. eval { mkpath($dir); };
  143. syserr(_g("cannot create directory %s"), $dir) if $@;
  144. }
  145. }
  146. }
  147. if (not $self->{'options'}{'without_quilt'}) {
  148. my %opts;
  149. $opts{"to_file"} = "/dev/null" if $skip_auto;
  150. info(_g("applying all patches with %s"), "quilt push -q " . $patches[-1]) unless $skip_auto;
  151. $self->run_quilt($dir, ['push', '-q', $patches[-1]],
  152. delete_env => ['QUILT_PATCH_OPTS'],
  153. wait_child => 1, %opts);
  154. foreach my $patch (@patches) {
  155. foreach my $fn (keys %{$panalysis->{$patch}->{'filepatched'}}) {
  156. utime($now, $now, $fn) || $! == ENOENT ||
  157. syserr(_g("cannot change timestamp for %s"), $fn);
  158. }
  159. print APPLIED "$patch\n";
  160. }
  161. }
  162. close(APPLIED);
  163. }
  164. sub prepare_build {
  165. my ($self, $dir) = @_;
  166. $self->SUPER::prepare_build($dir);
  167. # Skip .pc directories of quilt by default and ignore difference
  168. # on debian/patches/series symlinks and d/p/.dpkg-source-applied
  169. # stamp file created by ourselves
  170. my $func = sub {
  171. return 1 if $_[0] =~ m{^debian/patches/series$} and -l $_[0];
  172. return 1 if $_[0] =~ m{^debian/patches/.dpkg-source-applied$};
  173. return 1 if $_[0] =~ /^.pc(\/|$)/;
  174. return 1 if $_[0] =~ /$self->{'options'}{'diff_ignore_regexp'}/;
  175. return 0;
  176. };
  177. $self->{'diff_options'}{'diff_ignore_func'} = $func;
  178. }
  179. sub check_patches_applied {
  180. my ($self, $dir) = @_;
  181. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  182. my $auto_patch = $self->get_autopatch_name();
  183. my @patches ;
  184. # First we try to get a list of patches that are probably not napplied
  185. if (not $self->{'options'}{'without_quilt'}) {
  186. my $pipe;
  187. my $pid = $self->run_quilt($dir, ['unapplied'], error_to_file => '/dev/null',
  188. to_pipe => \$pipe);
  189. @patches = map { chomp; $_ } (<$pipe>);
  190. close ($pipe) || syserr("close on 'quilt unapplied' pipe");
  191. wait_child($pid, cmdline => "quilt unapplied", nocheck => 1);
  192. subprocerr("quilt unapplied") unless WIFEXITED($?);
  193. } else {
  194. @patches = $self->get_patches($dir);
  195. }
  196. # Then we check if it's applicable, and if yes, we make the
  197. # assumption that patches are not applied and need to be applied
  198. if (scalar(@patches)) {
  199. my $first_patch = File::Spec->catfile($dir, "debian", "patches", $patches[0]);
  200. my $patch_obj = Dpkg::Source::Patch->new(filename => $first_patch);
  201. if ($patch_obj->check_apply($dir)) {
  202. warning(_g("patches have not been applied, applying them now (use --no-preparation to override)"));
  203. $self->apply_patches($dir);
  204. }
  205. }
  206. }
  207. sub register_autopatch {
  208. my ($self, $dir) = @_;
  209. my $auto_patch = $self->get_autopatch_name();
  210. my @patches = $self->get_patches($dir);
  211. my $has_patch = (grep { $_ eq $auto_patch } @patches) ? 1 : 0;
  212. my $series = $self->get_series_file($dir);
  213. $series ||= File::Spec->catfile($dir, "debian", "patches", "series");
  214. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  215. my $patch = File::Spec->catfile($dir, "debian", "patches", $auto_patch);
  216. my $absdir = $dir;
  217. unless (File::Spec->file_name_is_absolute($absdir)) {
  218. $absdir = File::Spec->rel2abs($dir);
  219. }
  220. if (-e $patch) {
  221. # Add auto_patch to series file
  222. if (not $has_patch) {
  223. # Use quilt to register only if it's wanted/available AND :
  224. # - either we have patches and quilt has been used (.pc dir exists)
  225. # - or we don't have patches, hence quilt couldn't be used
  226. if ((-d "$dir/.pc" or not scalar(@patches)) and
  227. not $self->{'options'}{'without_quilt'})
  228. {
  229. # Registering the new patch with quilt requires some
  230. # trickery: reverse-apply the patch, create a new quilt patch,
  231. # fold the patch into the quilt-managed one
  232. my $patch_obj = Dpkg::Source::Patch->new(filename => $patch);
  233. $patch_obj->apply($dir, add_options => ['-R', '-E']);
  234. $self->run_quilt($dir, ['new', "$auto_patch"],
  235. wait_child => 1, to_file => '/dev/null');
  236. $self->run_quilt($dir, ['fold'],
  237. from_file => "$absdir/debian/patches/$auto_patch",
  238. wait_child => 1, to_file => '/dev/null');
  239. } else {
  240. open(SERIES, ">>", $series) || syserr(_g("cannot write %s"), $series);
  241. print SERIES "$auto_patch\n";
  242. close(SERIES);
  243. }
  244. } else {
  245. # If quilt was used, ensure its meta-information are
  246. # synchronized with the updated patch
  247. if (-d "$dir/.pc" and not $self->{'options'}{'without_quilt'}) {
  248. # Some trickery needed: reverse-apply the patch, fold the
  249. # new patch into the quilt-managed one
  250. my $patch_obj = Dpkg::Source::Patch->new(filename => $patch);
  251. $patch_obj->apply($dir, add_options => ['-R', '-E']);
  252. $self->run_quilt($dir, ['fold'],
  253. from_file => "$absdir/debian/patches/$auto_patch",
  254. wait_child => 1, to_file => '/dev/null');
  255. }
  256. }
  257. } else {
  258. # Remove auto_patch from series
  259. if ($has_patch) {
  260. if ($self->{'options'}{'without_quilt'}) {
  261. open(SERIES, "<", $series) || syserr(_g("cannot read %s"), $series);
  262. my @lines = <SERIES>;
  263. close(SERIES);
  264. open(SERIES, ">", $series) || syserr(_g("cannot write %s"), $series);
  265. print(SERIES $_) foreach grep { not /^\Q$auto_patch\E\s*$/ } @lines;
  266. close(SERIES);
  267. } else {
  268. $self->run_quilt($dir, ['delete', $auto_patch],
  269. wait_child => 1, to_file => '/dev/null');
  270. }
  271. }
  272. # Clean up empty series
  273. unlink($series) if not -s $series;
  274. }
  275. }
  276. # vim:et:sw=4:ts=8
  277. 1;