quilt.pm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. # Copyright © 2008-2011 Raphaël Hertzog <hertzog@debian.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package Dpkg::Source::Package::V3::quilt;
  16. use strict;
  17. use warnings;
  18. our $VERSION = "0.01";
  19. # Based on wig&pen implementation
  20. use base 'Dpkg::Source::Package::V2';
  21. use Dpkg;
  22. use Dpkg::Gettext;
  23. use Dpkg::ErrorHandling;
  24. use Dpkg::Source::Patch;
  25. use Dpkg::Source::Functions qw(erasedir fs_time);
  26. use Dpkg::IPC;
  27. use Dpkg::Vendor qw(get_current_vendor);
  28. use POSIX;
  29. use File::Basename;
  30. use File::Spec;
  31. use File::Path;
  32. use File::Copy;
  33. our $CURRENT_MINOR_VERSION = "0";
  34. sub init_options {
  35. my ($self) = @_;
  36. $self->{'options'}{'single-debian-patch'} = 0
  37. unless exists $self->{'options'}{'single-debian-patch'};
  38. $self->{'options'}{'allow-version-of-quilt-db'} = []
  39. unless exists $self->{'options'}{'allow-version-of-quilt-db'};
  40. $self->SUPER::init_options();
  41. }
  42. sub parse_cmdline_option {
  43. my ($self, $opt) = @_;
  44. return 1 if $self->SUPER::parse_cmdline_option($opt);
  45. if ($opt =~ /^--single-debian-patch$/) {
  46. $self->{'options'}{'single-debian-patch'} = 1;
  47. # For backwards compatibility.
  48. $self->{'options'}{'auto_commit'} = 1;
  49. return 1;
  50. } elsif ($opt =~ /^--allow-version-of-quilt-db=(.*)$/) {
  51. push @{$self->{'options'}{'allow-version-of-quilt-db'}}, $1;
  52. return 1;
  53. }
  54. return 0;
  55. }
  56. sub can_build {
  57. my ($self, $dir) = @_;
  58. my ($code, $msg) = $self->SUPER::can_build($dir);
  59. return ($code, $msg) if $code eq 0;
  60. my $pd = File::Spec->catdir($dir, "debian", "patches");
  61. if (-e $pd and not -d _) {
  62. return (0, sprintf(_g("%s should be a directory or non-existing"), $pd));
  63. }
  64. my $series_vendor = $self->get_series_file($dir);
  65. my $series_main = File::Spec->catfile($pd, "series");
  66. foreach my $series ($series_vendor, $series_main) {
  67. if (defined($series) and -e $series and not -f _) {
  68. return (0, sprintf(_g("%s should be a file or non-existing"), $series));
  69. }
  70. }
  71. return (1, "");
  72. }
  73. sub get_autopatch_name {
  74. my ($self) = @_;
  75. if ($self->{'options'}{'single-debian-patch'}) {
  76. return "debian-changes";
  77. } else {
  78. return "debian-changes-" . $self->{'fields'}{'Version'};
  79. }
  80. }
  81. sub get_series_file {
  82. my ($self, $dir) = @_;
  83. my $pd = File::Spec->catdir($dir, "debian", "patches");
  84. my $vendor = lc(get_current_vendor() || "debian");
  85. foreach (File::Spec->catfile($pd, "$vendor.series"),
  86. File::Spec->catfile($pd, "series")) {
  87. return $_ if -e $_;
  88. }
  89. return undef;
  90. }
  91. sub read_patch_list {
  92. my ($self, $file, %opts) = @_;
  93. return () if not defined $file or not -f $file;
  94. $opts{"warn_options"} = 0 unless defined($opts{"warn_options"});
  95. $opts{"skip_auto"} = 0 unless defined($opts{"skip_auto"});
  96. my @patches;
  97. my $auto_patch = $self->get_autopatch_name();
  98. open(SERIES, "<" , $file) || syserr(_g("cannot read %s"), $file);
  99. while(defined($_ = <SERIES>)) {
  100. chomp; s/^\s+//; s/\s+$//; # Strip leading/trailing spaces
  101. s/(^|\s+)#.*$//; # Strip comment
  102. next unless $_;
  103. if (/^(\S+)\s+(.*)$/) {
  104. $_ = $1;
  105. if ($2 ne '-p1') {
  106. warning(_g("the series file (%s) contains unsupported " .
  107. "options ('%s', line %s), dpkg-source might " .
  108. "fail when applying patches."),
  109. $file, $2, $.) if $opts{"warn_options"};
  110. }
  111. }
  112. next if $opts{"skip_auto"} and $_ eq $auto_patch;
  113. error(_g("%s contains an insecure path: %s"), $file, $_) if m{(^|/)\.\./};
  114. push @patches, $_;
  115. }
  116. close(SERIES);
  117. return @patches;
  118. }
  119. sub create_quilt_db {
  120. my ($self, $dir) = @_;
  121. my $db_dir = File::Spec->catdir($dir, ".pc");
  122. if (not -d $db_dir) {
  123. mkdir $db_dir or syserr(_g("cannot mkdir %s"), $db_dir);
  124. }
  125. my $file = File::Spec->catfile($db_dir, ".version");
  126. if (not -e $file) {
  127. open(VERSION, ">", $file) or syserr(_g("cannot write %s"), $file);
  128. print VERSION "2\n";
  129. close(VERSION);
  130. }
  131. # The files below are used by quilt to know where patches are stored
  132. # and what file contains the patch list (supported by quilt >= 0.48-5
  133. # in Debian).
  134. $file = File::Spec->catfile($db_dir, ".quilt_patches");
  135. if (not -e $file) {
  136. open(QPATCH, ">", $file) or syserr(_g("cannot write %s"), $file);
  137. print QPATCH "debian/patches\n";
  138. close(QPATCH);
  139. }
  140. $file = File::Spec->catfile($db_dir, ".quilt_series");
  141. if (not -e $file) {
  142. open(QSERIES, ">", $file) or syserr(_g("cannot write %s"), $file);
  143. my $series = $self->get_series_file($dir) || "series";
  144. $series = (File::Spec->splitpath($series))[2];
  145. print QSERIES "$series\n";
  146. close(QSERIES);
  147. }
  148. }
  149. sub apply_quilt_patch {
  150. my ($self, $dir, $patch, %opts) = @_;
  151. $opts{"verbose"} = 0 unless defined($opts{"verbose"});
  152. $opts{"timestamp"} = fs_time($dir) unless defined($opts{"timestamp"});
  153. my $path = File::Spec->catfile($dir, "debian", "patches", $patch);
  154. my $obj = Dpkg::Source::Patch->new(filename => $path);
  155. info(_g("applying %s"), $patch) if $opts{"verbose"};
  156. $obj->apply($dir, timestamp => $opts{"timestamp"},
  157. verbose => $opts{"verbose"},
  158. force_timestamp => 1, create_dirs => 1, remove_backup => 0,
  159. options => [ '-t', '-F', '0', '-N', '-p1', '-u',
  160. '-V', 'never', '-g0', '-E', '-b',
  161. '-B', ".pc/$patch/" ]);
  162. }
  163. sub get_patches {
  164. my ($self, $dir, %opts) = @_;
  165. my $series = $self->get_series_file($dir);
  166. return $self->read_patch_list($series, %opts);
  167. }
  168. sub apply_patches {
  169. my ($self, $dir, %opts) = @_;
  170. if ($opts{'usage'} eq 'unpack') {
  171. $opts{'verbose'} = 1;
  172. } elsif ($opts{'usage'} eq 'build') {
  173. $opts{'warn_options'} = 1;
  174. $opts{'verbose'} = 0;
  175. }
  176. my $patches = $opts{"patches"};
  177. # Always create the quilt db so that if the maintainer calls quilt to
  178. # create a patch, it's stored in the right directory
  179. $self->create_quilt_db($dir);
  180. # Update debian/patches/series symlink if needed to allow quilt usage
  181. my $series = $self->get_series_file($dir);
  182. return unless $series; # No series, no patches
  183. my $basename = basename($series);
  184. if ($basename ne "series") {
  185. my $dest = File::Spec->catfile($dir, "debian", "patches", "series");
  186. unlink($dest) if -l $dest;
  187. unless (-f _) { # Don't overwrite real files
  188. symlink($basename, $dest) ||
  189. syserr(_g("can't create symlink %s"), $dest);
  190. }
  191. }
  192. unless (defined($patches)) {
  193. $patches = [ $self->get_patches($dir, %opts) ];
  194. }
  195. return unless scalar(@$patches);
  196. if ($opts{'usage'} eq "preparation") {
  197. # We're applying the patches in --before-build, remember to unapply
  198. # them afterwards in --after-build
  199. my $pc_unapply = File::Spec->catfile($dir, ".pc", ".dpkg-source-unapply");
  200. open(UNAPPLY, ">", $pc_unapply) ||
  201. syserr(_g("cannot write %s"), $pc_unapply);
  202. close(UNAPPLY);
  203. }
  204. # Apply patches
  205. my $pc_applied = File::Spec->catfile($dir, ".pc", "applied-patches");
  206. my @applied = $self->read_patch_list($pc_applied);
  207. my @patches = $self->read_patch_list($self->get_series_file($dir));
  208. open(APPLIED, '>>', $pc_applied) || syserr(_g("cannot write %s"), $pc_applied);
  209. $opts{"timestamp"} = fs_time($pc_applied);
  210. foreach my $patch (@$patches) {
  211. $self->apply_quilt_patch($dir, $patch, %opts);
  212. print APPLIED "$patch\n";
  213. }
  214. close(APPLIED);
  215. }
  216. sub unapply_patches {
  217. my ($self, $dir, %opts) = @_;
  218. $opts{'verbose'} = 1 unless defined $opts{'verbose'};
  219. my $pc_applied = File::Spec->catfile($dir, ".pc", "applied-patches");
  220. my @applied = $self->read_patch_list($pc_applied);
  221. $opts{"timestamp"} = fs_time($pc_applied) if @applied;
  222. foreach my $patch (reverse @applied) {
  223. my $path = File::Spec->catfile($dir, "debian", "patches", $patch);
  224. my $obj = Dpkg::Source::Patch->new(filename => $path);
  225. info(_g("unapplying %s"), $patch) if $opts{"verbose"};
  226. $obj->apply($dir, timestamp => $opts{"timestamp"},
  227. verbose => 0,
  228. force_timestamp => 1, remove_backup => 0,
  229. options => [ '-R', '-t', '-N', '-p1',
  230. '-u', '-V', 'never', '-g0', '-E',
  231. '--no-backup-if-mismatch' ]);
  232. erasedir(File::Spec->catdir($dir, ".pc", $patch));
  233. }
  234. erasedir(File::Spec->catdir($dir, ".pc"));
  235. }
  236. sub prepare_build {
  237. my ($self, $dir) = @_;
  238. $self->SUPER::prepare_build($dir);
  239. # Skip .pc directories of quilt by default and ignore difference
  240. # on debian/patches/series symlinks and d/p/.dpkg-source-applied
  241. # stamp file created by ourselves
  242. my $func = sub {
  243. return 1 if $_[0] =~ m{^debian/patches/series$} and -l $_[0];
  244. return 1 if $_[0] =~ /^\.pc(\/|$)/;
  245. return 1 if $_[0] =~ /$self->{'options'}{'diff_ignore_regexp'}/;
  246. return 0;
  247. };
  248. $self->{'diff_options'}{'diff_ignore_func'} = $func;
  249. }
  250. sub do_build {
  251. my ($self, $dir) = @_;
  252. my $pc_ver = File::Spec->catfile($dir, ".pc", ".version");
  253. if (-f $pc_ver) {
  254. open(VER, "<", $pc_ver) || syserr(_g("cannot read %s"), $pc_ver);
  255. my $version = <VER>;
  256. chomp $version;
  257. close(VER);
  258. if ($version != 2) {
  259. if (scalar grep { $version eq $_ }
  260. @{$self->{'options'}{'allow-version-of-quilt-db'}})
  261. {
  262. warning(_g("unsupported version of the quilt metadata: %s"),
  263. $version);
  264. } else {
  265. error(_g("unsupported version of the quilt metadata: %s"),
  266. $version);
  267. }
  268. }
  269. }
  270. $self->SUPER::do_build($dir);
  271. }
  272. sub after_build {
  273. my ($self, $dir) = @_;
  274. my $pc_unapply = File::Spec->catfile($dir, ".pc", ".dpkg-source-unapply");
  275. if (-e $pc_unapply or $self->{'options'}{'unapply_patches'}) {
  276. unlink($pc_unapply);
  277. $self->unapply_patches($dir);
  278. }
  279. }
  280. sub check_patches_applied {
  281. my ($self, $dir) = @_;
  282. my $pc_applied = File::Spec->catfile($dir, ".pc", "applied-patches");
  283. my @applied = $self->read_patch_list($pc_applied);
  284. my @patches = $self->read_patch_list($self->get_series_file($dir));
  285. my @to_apply;
  286. foreach my $patch (@patches) {
  287. next if scalar grep { $_ eq $patch } @applied;
  288. push @to_apply, $patch;
  289. }
  290. if (scalar(@to_apply)) {
  291. my $first_patch = File::Spec->catfile($dir, "debian", "patches",
  292. $to_apply[0]);
  293. my $patch_obj = Dpkg::Source::Patch->new(filename => $first_patch);
  294. if ($patch_obj->check_apply($dir)) {
  295. info(_g("patches are not applied, applying them now"));
  296. $self->apply_patches($dir, usage => 'preparation', verbose => 1,
  297. patches => \@to_apply);
  298. }
  299. }
  300. }
  301. sub register_patch {
  302. my ($self, $dir, $tmpdiff, $patch_name) = @_;
  303. sub add_line {
  304. my ($file, $line) = @_;
  305. open(FILE, ">>", $file) || syserr(_g("cannot write %s"), $file);
  306. print FILE "$line\n";
  307. close(FILE);
  308. }
  309. sub drop_line {
  310. my ($file, $re) = @_;
  311. open(FILE, "<", $file) || syserr(_g("cannot read %s"), $file);
  312. my @lines = <FILE>;
  313. close(FILE);
  314. open(FILE, ">", $file) || syserr(_g("cannot write %s"), $file);
  315. print(FILE $_) foreach grep { not /^\Q$re\E\s*$/ } @lines;
  316. close(FILE);
  317. }
  318. my @patches = $self->get_patches($dir);
  319. my $has_patch = (grep { $_ eq $patch_name } @patches) ? 1 : 0;
  320. my $series = $self->get_series_file($dir);
  321. $series ||= File::Spec->catfile($dir, "debian", "patches", "series");
  322. my $applied = File::Spec->catfile($dir, ".pc", "applied-patches");
  323. my $patch = File::Spec->catfile($dir, "debian", "patches", $patch_name);
  324. if (-s $tmpdiff) {
  325. copy($tmpdiff, $patch) ||
  326. syserr(_g("failed to copy %s to %s"), $tmpdiff, $patch);
  327. chmod(0666 & ~ umask(), $patch) ||
  328. syserr(_g("unable to change permission of `%s'"), $patch);
  329. } elsif (-e $patch) {
  330. unlink($patch) || syserr(_g("cannot remove %s"), $patch);
  331. }
  332. if (-e $patch) {
  333. $self->create_quilt_db($dir);
  334. # Add patch to series file
  335. if (not $has_patch) {
  336. add_line($series, $patch_name);
  337. add_line($applied, $patch_name);
  338. }
  339. # Ensure quilt meta-data are created and in sync with some trickery:
  340. # reverse-apply the patch, drop .pc/$patch, re-apply it
  341. # with the correct options to recreate the backup files
  342. my $patch_obj = Dpkg::Source::Patch->new(filename => $patch);
  343. $patch_obj->apply($dir, add_options => ['-R', '-E'], verbose => 0);
  344. erasedir(File::Spec->catdir($dir, ".pc", $patch_name));
  345. $self->apply_quilt_patch($dir, $patch_name);
  346. } else {
  347. # Remove auto_patch from series
  348. if ($has_patch) {
  349. drop_line($series, $patch_name);
  350. drop_line($applied, $patch_name);
  351. erasedir(File::Spec->catdir($dir, ".pc", $patch_name));
  352. }
  353. # Clean up empty series
  354. unlink($series) if -z $series;
  355. }
  356. return $patch;
  357. }
  358. # vim:et:sw=4:ts=8
  359. 1;