quilt.pm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 Dpkg::Exit;
  29. use POSIX;
  30. use File::Basename;
  31. use File::Spec;
  32. use File::Path;
  33. use File::Copy;
  34. use File::Find;
  35. our $CURRENT_MINOR_VERSION = "0";
  36. sub init_options {
  37. my ($self) = @_;
  38. $self->{'options'}{'single-debian-patch'} = 0
  39. unless exists $self->{'options'}{'single-debian-patch'};
  40. $self->{'options'}{'allow-version-of-quilt-db'} = []
  41. unless exists $self->{'options'}{'allow-version-of-quilt-db'};
  42. $self->SUPER::init_options();
  43. }
  44. sub parse_cmdline_option {
  45. my ($self, $opt) = @_;
  46. return 1 if $self->SUPER::parse_cmdline_option($opt);
  47. if ($opt =~ /^--single-debian-patch$/) {
  48. $self->{'options'}{'single-debian-patch'} = 1;
  49. # For backwards compatibility.
  50. $self->{'options'}{'auto_commit'} = 1;
  51. return 1;
  52. } elsif ($opt =~ /^--allow-version-of-quilt-db=(.*)$/) {
  53. push @{$self->{'options'}{'allow-version-of-quilt-db'}}, $1;
  54. return 1;
  55. }
  56. return 0;
  57. }
  58. sub can_build {
  59. my ($self, $dir) = @_;
  60. my ($code, $msg) = $self->SUPER::can_build($dir);
  61. return ($code, $msg) if $code eq 0;
  62. my $pd = File::Spec->catdir($dir, "debian", "patches");
  63. if (-e $pd and not -d _) {
  64. return (0, sprintf(_g("%s should be a directory or non-existing"), $pd));
  65. }
  66. my $series_vendor = $self->get_series_file($dir);
  67. my $series_main = File::Spec->catfile($pd, "series");
  68. foreach my $series ($series_vendor, $series_main) {
  69. if (defined($series) and -e $series and not -f _) {
  70. return (0, sprintf(_g("%s should be a file or non-existing"), $series));
  71. }
  72. }
  73. return (1, "");
  74. }
  75. sub get_autopatch_name {
  76. my ($self) = @_;
  77. if ($self->{'options'}{'single-debian-patch'}) {
  78. return "debian-changes";
  79. } else {
  80. return "debian-changes-" . $self->{'fields'}{'Version'};
  81. }
  82. }
  83. sub get_series_file {
  84. my ($self, $dir) = @_;
  85. my $pd = File::Spec->catdir($dir, "debian", "patches");
  86. my $vendor = lc(get_current_vendor() || "debian");
  87. foreach (File::Spec->catfile($pd, "$vendor.series"),
  88. File::Spec->catfile($pd, "series")) {
  89. return $_ if -e $_;
  90. }
  91. return undef;
  92. }
  93. sub read_patch_list {
  94. my ($self, $file, %opts) = @_;
  95. return () if not defined $file or not -f $file;
  96. $opts{"warn_options"} = 0 unless defined($opts{"warn_options"});
  97. $opts{"skip_auto"} = 0 unless defined($opts{"skip_auto"});
  98. my @patches;
  99. my $auto_patch = $self->get_autopatch_name();
  100. open(SERIES, "<" , $file) || syserr(_g("cannot read %s"), $file);
  101. while(defined($_ = <SERIES>)) {
  102. chomp; s/^\s+//; s/\s+$//; # Strip leading/trailing spaces
  103. s/(^|\s+)#.*$//; # Strip comment
  104. next unless $_;
  105. if (/^(\S+)\s+(.*)$/) {
  106. $_ = $1;
  107. if ($2 ne '-p1') {
  108. warning(_g("the series file (%s) contains unsupported " .
  109. "options ('%s', line %s), dpkg-source might " .
  110. "fail when applying patches."),
  111. $file, $2, $.) if $opts{"warn_options"};
  112. }
  113. }
  114. next if $opts{"skip_auto"} and $_ eq $auto_patch;
  115. error(_g("%s contains an insecure path: %s"), $file, $_) if m{(^|/)\.\./};
  116. push @patches, $_;
  117. }
  118. close(SERIES);
  119. return @patches;
  120. }
  121. sub create_quilt_db {
  122. my ($self, $dir) = @_;
  123. my $db_dir = File::Spec->catdir($dir, ".pc");
  124. if (not -d $db_dir) {
  125. mkdir $db_dir or syserr(_g("cannot mkdir %s"), $db_dir);
  126. }
  127. my $file = File::Spec->catfile($db_dir, ".version");
  128. if (not -e $file) {
  129. open(VERSION, ">", $file) or syserr(_g("cannot write %s"), $file);
  130. print VERSION "2\n";
  131. close(VERSION);
  132. }
  133. # The files below are used by quilt to know where patches are stored
  134. # and what file contains the patch list (supported by quilt >= 0.48-5
  135. # in Debian).
  136. $file = File::Spec->catfile($db_dir, ".quilt_patches");
  137. if (not -e $file) {
  138. open(QPATCH, ">", $file) or syserr(_g("cannot write %s"), $file);
  139. print QPATCH "debian/patches\n";
  140. close(QPATCH);
  141. }
  142. $file = File::Spec->catfile($db_dir, ".quilt_series");
  143. if (not -e $file) {
  144. open(QSERIES, ">", $file) or syserr(_g("cannot write %s"), $file);
  145. my $series = $self->get_series_file($dir) || "series";
  146. $series = (File::Spec->splitpath($series))[2];
  147. print QSERIES "$series\n";
  148. close(QSERIES);
  149. }
  150. }
  151. sub apply_quilt_patch {
  152. my ($self, $dir, $patch, %opts) = @_;
  153. $opts{"verbose"} = 0 unless defined($opts{"verbose"});
  154. $opts{"timestamp"} = fs_time($dir) unless defined($opts{"timestamp"});
  155. my $path = File::Spec->catfile($dir, "debian", "patches", $patch);
  156. my $obj = Dpkg::Source::Patch->new(filename => $path);
  157. info(_g("applying %s"), $patch) if $opts{"verbose"};
  158. eval {
  159. $obj->apply($dir, timestamp => $opts{"timestamp"},
  160. verbose => $opts{"verbose"},
  161. force_timestamp => 1, create_dirs => 1, remove_backup => 0,
  162. options => [ '-t', '-F', '0', '-N', '-p1', '-u',
  163. '-V', 'never', '-g0', '-E', '-b',
  164. '-B', ".pc/$patch/", '--reject-file=-' ]);
  165. };
  166. if ($@) {
  167. info(_g("dpkg-source doesn't allow fuzz when applying patches"));
  168. info(_g("if patch '%s' is correctly applied by quilt, use '%s' to update it"),
  169. $patch, "quilt refresh");
  170. $self->restore_quilt_backup_files($dir, $patch, %opts);
  171. die $@;
  172. }
  173. }
  174. sub restore_quilt_backup_files {
  175. my ($self, $dir, $patch, %opts) = @_;
  176. my $patch_dir = File::Spec->catdir($dir, ".pc", $patch);
  177. return unless -d $patch_dir;
  178. info(_g("restoring quilt backup files for %s"), $patch) if $opts{'verbose'};
  179. find({
  180. no_chdir => 1,
  181. wanted => sub {
  182. return if -d $_;
  183. my $relpath_in_srcpkg = File::Spec->abs2rel($_, $patch_dir);
  184. my $target = File::Spec->catfile($dir, $relpath_in_srcpkg);
  185. if (-s $_) {
  186. unlink($target);
  187. unless (link($_, $target)) {
  188. copy($_, $target) ||
  189. syserr(_g("failed to copy %s to %s"), $_, $target);
  190. chmod($target, (stat(_))[2]) ||
  191. syserr(_g("unable to change permission of `%s'"), $target);
  192. }
  193. } else {
  194. # empty files are "backups" for new files that patch created
  195. unlink($target);
  196. }
  197. }
  198. }, $patch_dir);
  199. erasedir($patch_dir);
  200. }
  201. sub get_patches {
  202. my ($self, $dir, %opts) = @_;
  203. my $series = $self->get_series_file($dir);
  204. return $self->read_patch_list($series, %opts);
  205. }
  206. sub apply_patches {
  207. my ($self, $dir, %opts) = @_;
  208. if ($opts{'usage'} eq 'unpack') {
  209. $opts{'verbose'} = 1;
  210. } elsif ($opts{'usage'} eq 'build') {
  211. $opts{'warn_options'} = 1;
  212. $opts{'verbose'} = 0;
  213. }
  214. my $patches = $opts{"patches"};
  215. # Always create the quilt db so that if the maintainer calls quilt to
  216. # create a patch, it's stored in the right directory
  217. $self->create_quilt_db($dir);
  218. # Update debian/patches/series symlink if needed to allow quilt usage
  219. my $series = $self->get_series_file($dir);
  220. return unless $series; # No series, no patches
  221. my $basename = basename($series);
  222. if ($basename ne "series") {
  223. my $dest = File::Spec->catfile($dir, "debian", "patches", "series");
  224. unlink($dest) if -l $dest;
  225. unless (-f _) { # Don't overwrite real files
  226. symlink($basename, $dest) ||
  227. syserr(_g("can't create symlink %s"), $dest);
  228. }
  229. }
  230. unless (defined($patches)) {
  231. $patches = [ $self->get_patches($dir, %opts) ];
  232. }
  233. return unless scalar(@$patches);
  234. if ($opts{'usage'} eq "preparation") {
  235. # We're applying the patches in --before-build, remember to unapply
  236. # them afterwards in --after-build
  237. my $pc_unapply = File::Spec->catfile($dir, ".pc", ".dpkg-source-unapply");
  238. open(UNAPPLY, ">", $pc_unapply) ||
  239. syserr(_g("cannot write %s"), $pc_unapply);
  240. close(UNAPPLY);
  241. }
  242. # Apply patches
  243. my $pc_applied = File::Spec->catfile($dir, ".pc", "applied-patches");
  244. my @applied = $self->read_patch_list($pc_applied);
  245. my @patches = $self->read_patch_list($self->get_series_file($dir));
  246. open(APPLIED, '>>', $pc_applied) || syserr(_g("cannot write %s"), $pc_applied);
  247. $opts{"timestamp"} = fs_time($pc_applied);
  248. foreach my $patch (@$patches) {
  249. $self->apply_quilt_patch($dir, $patch, %opts);
  250. print APPLIED "$patch\n";
  251. }
  252. close(APPLIED);
  253. }
  254. sub unapply_patches {
  255. my ($self, $dir, %opts) = @_;
  256. $opts{'verbose'} = 1 unless defined $opts{'verbose'};
  257. my $pc_applied = File::Spec->catfile($dir, ".pc", "applied-patches");
  258. my @applied = $self->read_patch_list($pc_applied);
  259. $opts{"timestamp"} = fs_time($pc_applied) if @applied;
  260. foreach my $patch (reverse @applied) {
  261. my $path = File::Spec->catfile($dir, "debian", "patches", $patch);
  262. my $obj = Dpkg::Source::Patch->new(filename => $path);
  263. info(_g("unapplying %s"), $patch) if $opts{"verbose"};
  264. $obj->apply($dir, timestamp => $opts{"timestamp"},
  265. verbose => 0,
  266. force_timestamp => 1, remove_backup => 0,
  267. options => [ '-R', '-t', '-N', '-p1',
  268. '-u', '-V', 'never', '-g0', '-E',
  269. '--no-backup-if-mismatch' ]);
  270. erasedir(File::Spec->catdir($dir, ".pc", $patch));
  271. }
  272. erasedir(File::Spec->catdir($dir, ".pc"));
  273. }
  274. sub prepare_build {
  275. my ($self, $dir) = @_;
  276. $self->SUPER::prepare_build($dir);
  277. # Skip .pc directories of quilt by default and ignore difference
  278. # on debian/patches/series symlinks and d/p/.dpkg-source-applied
  279. # stamp file created by ourselves
  280. my $func = sub {
  281. return 1 if $_[0] =~ m{^debian/patches/series$} and -l $_[0];
  282. return 1 if $_[0] =~ /^\.pc(\/|$)/;
  283. return 1 if $_[0] =~ /$self->{'options'}{'diff_ignore_regexp'}/;
  284. return 0;
  285. };
  286. $self->{'diff_options'}{'diff_ignore_func'} = $func;
  287. }
  288. sub do_build {
  289. my ($self, $dir) = @_;
  290. my $pc_ver = File::Spec->catfile($dir, ".pc", ".version");
  291. if (-f $pc_ver) {
  292. open(VER, "<", $pc_ver) || syserr(_g("cannot read %s"), $pc_ver);
  293. my $version = <VER>;
  294. chomp $version;
  295. close(VER);
  296. if ($version != 2) {
  297. if (scalar grep { $version eq $_ }
  298. @{$self->{'options'}{'allow-version-of-quilt-db'}})
  299. {
  300. warning(_g("unsupported version of the quilt metadata: %s"),
  301. $version);
  302. } else {
  303. error(_g("unsupported version of the quilt metadata: %s"),
  304. $version);
  305. }
  306. }
  307. }
  308. $self->SUPER::do_build($dir);
  309. }
  310. sub after_build {
  311. my ($self, $dir) = @_;
  312. my $pc_unapply = File::Spec->catfile($dir, ".pc", ".dpkg-source-unapply");
  313. if (-e $pc_unapply or $self->{'options'}{'unapply_patches'}) {
  314. unlink($pc_unapply);
  315. $self->unapply_patches($dir);
  316. }
  317. }
  318. sub check_patches_applied {
  319. my ($self, $dir) = @_;
  320. my $pc_applied = File::Spec->catfile($dir, ".pc", "applied-patches");
  321. my @applied = $self->read_patch_list($pc_applied);
  322. my @patches = $self->read_patch_list($self->get_series_file($dir));
  323. my @to_apply;
  324. foreach my $patch (@patches) {
  325. next if scalar grep { $_ eq $patch } @applied;
  326. push @to_apply, $patch;
  327. }
  328. if (scalar(@to_apply)) {
  329. my $first_patch = File::Spec->catfile($dir, "debian", "patches",
  330. $to_apply[0]);
  331. my $patch_obj = Dpkg::Source::Patch->new(filename => $first_patch);
  332. if ($patch_obj->check_apply($dir)) {
  333. info(_g("patches are not applied, applying them now"));
  334. $self->apply_patches($dir, usage => 'preparation', verbose => 1,
  335. patches => \@to_apply);
  336. }
  337. }
  338. }
  339. sub register_patch {
  340. my ($self, $dir, $tmpdiff, $patch_name) = @_;
  341. sub add_line {
  342. my ($file, $line) = @_;
  343. open(FILE, ">>", $file) || syserr(_g("cannot write %s"), $file);
  344. print FILE "$line\n";
  345. close(FILE);
  346. }
  347. sub drop_line {
  348. my ($file, $re) = @_;
  349. open(FILE, "<", $file) || syserr(_g("cannot read %s"), $file);
  350. my @lines = <FILE>;
  351. close(FILE);
  352. open(FILE, ">", $file) || syserr(_g("cannot write %s"), $file);
  353. print(FILE $_) foreach grep { not /^\Q$re\E\s*$/ } @lines;
  354. close(FILE);
  355. }
  356. my @patches = $self->get_patches($dir);
  357. my $has_patch = (grep { $_ eq $patch_name } @patches) ? 1 : 0;
  358. my $series = $self->get_series_file($dir);
  359. $series ||= File::Spec->catfile($dir, "debian", "patches", "series");
  360. my $applied = File::Spec->catfile($dir, ".pc", "applied-patches");
  361. my $patch = File::Spec->catfile($dir, "debian", "patches", $patch_name);
  362. if (-s $tmpdiff) {
  363. copy($tmpdiff, $patch) ||
  364. syserr(_g("failed to copy %s to %s"), $tmpdiff, $patch);
  365. chmod(0666 & ~ umask(), $patch) ||
  366. syserr(_g("unable to change permission of `%s'"), $patch);
  367. } elsif (-e $patch) {
  368. unlink($patch) || syserr(_g("cannot remove %s"), $patch);
  369. }
  370. if (-e $patch) {
  371. $self->create_quilt_db($dir);
  372. # Add patch to series file
  373. if (not $has_patch) {
  374. add_line($series, $patch_name);
  375. add_line($applied, $patch_name);
  376. }
  377. # Ensure quilt meta-data are created and in sync with some trickery:
  378. # reverse-apply the patch, drop .pc/$patch, re-apply it
  379. # with the correct options to recreate the backup files
  380. my $patch_obj = Dpkg::Source::Patch->new(filename => $patch);
  381. $patch_obj->apply($dir, add_options => ['-R', '-E'], verbose => 0);
  382. erasedir(File::Spec->catdir($dir, ".pc", $patch_name));
  383. $self->apply_quilt_patch($dir, $patch_name);
  384. } else {
  385. # Remove auto_patch from series
  386. if ($has_patch) {
  387. drop_line($series, $patch_name);
  388. drop_line($applied, $patch_name);
  389. erasedir(File::Spec->catdir($dir, ".pc", $patch_name));
  390. }
  391. # Clean up empty series
  392. unlink($series) if -z $series;
  393. }
  394. return $patch;
  395. }
  396. # vim:et:sw=4:ts=8
  397. 1;