quilt.pm 15 KB

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