Quilt.pm 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. # Copyright © 2008-2012 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::Quilt;
  16. use strict;
  17. use warnings;
  18. our $VERSION = "0.01";
  19. use Dpkg::Gettext;
  20. use Dpkg::ErrorHandling;
  21. use Dpkg::Source::Patch;
  22. use Dpkg::Source::Functions qw(erasedir fs_time);
  23. use Dpkg::Vendor qw(get_current_vendor);
  24. use File::Spec;
  25. use File::Copy;
  26. use File::Find;
  27. sub new {
  28. my ($this, $dir, %opts) = @_;
  29. my $class = ref($this) || $this;
  30. my $self = {
  31. 'dir' => $dir,
  32. };
  33. bless $self, $class;
  34. $self->load_series();
  35. $self->load_db();
  36. return $self;
  37. }
  38. sub setup_db {
  39. my ($self) = @_;
  40. my $db_dir = $self->get_db_file();
  41. if (not -d $db_dir) {
  42. mkdir $db_dir or syserr(_g("cannot mkdir %s"), $db_dir);
  43. }
  44. my $file = $self->get_db_file(".version");
  45. if (not -e $file) {
  46. open(VERSION, ">", $file) or syserr(_g("cannot write %s"), $file);
  47. print VERSION "2\n";
  48. close(VERSION);
  49. }
  50. # The files below are used by quilt to know where patches are stored
  51. # and what file contains the patch list (supported by quilt >= 0.48-5
  52. # in Debian).
  53. $file = $self->get_db_file(".quilt_patches");
  54. if (not -e $file) {
  55. open(QPATCH, ">", $file) or syserr(_g("cannot write %s"), $file);
  56. print QPATCH "debian/patches\n";
  57. close(QPATCH);
  58. }
  59. $file = $self->get_db_file(".quilt_series");
  60. if (not -e $file) {
  61. open(QSERIES, ">", $file) or syserr(_g("cannot write %s"), $file);
  62. my $series = $self->get_series_file();
  63. $series = (File::Spec->splitpath($series))[2];
  64. print QSERIES "$series\n";
  65. close(QSERIES);
  66. }
  67. }
  68. sub load_db {
  69. my ($self) = @_;
  70. my $pc_applied = $self->get_db_file("applied-patches");
  71. $self->{'applied-patches'} = [ $self->read_patch_list($pc_applied) ];
  72. }
  73. sub write_db {
  74. my ($self) = @_;
  75. $self->setup_db();
  76. my $pc_applied = $self->get_db_file("applied-patches");
  77. open(APPLIED, ">", $pc_applied) or syserr(_g("cannot write %s"), $pc_applied);
  78. foreach my $patch (@{$self->{'applied-patches'}}) {
  79. print APPLIED "$patch\n";
  80. }
  81. close(APPLIED);
  82. }
  83. sub load_series {
  84. my ($self, %opts) = @_;
  85. my $series = $self->get_series_file();
  86. $self->{'series'} = [ $self->read_patch_list($series, %opts) ];
  87. }
  88. sub series {
  89. my ($self) = @_;
  90. return @{$self->{'series'}};
  91. }
  92. sub applied {
  93. my ($self) = @_;
  94. return @{$self->{'applied-patches'}};
  95. }
  96. sub top {
  97. my ($self) = @_;
  98. my $count = scalar @{$self->{'applied-patches'}};
  99. return $self->{'applied-patches'}[$count - 1] if $count;
  100. return undef;
  101. }
  102. sub next {
  103. my ($self) = @_;
  104. my $count_applied = scalar @{$self->{'applied-patches'}};
  105. my $count_series = scalar @{$self->{'series'}};
  106. return $self->{'series'}[$count_applied] if ($count_series > $count_applied);
  107. return undef;
  108. }
  109. sub push {
  110. my ($self, %opts) = @_;
  111. $opts{"verbose"} = 0 unless defined($opts{"verbose"});
  112. $opts{"timestamp"} = fs_time($self->{'dir'}) unless defined($opts{"timestamp"});
  113. my $patch = $self->next();
  114. return unless defined $patch;
  115. my $path = $self->get_patch_file($patch);
  116. my $obj = Dpkg::Source::Patch->new(filename => $path);
  117. info(_g("applying %s"), $patch) if $opts{"verbose"};
  118. eval {
  119. $obj->apply($self->{'dir'}, timestamp => $opts{"timestamp"},
  120. verbose => $opts{"verbose"},
  121. force_timestamp => 1, create_dirs => 1, remove_backup => 0,
  122. options => [ '-t', '-F', '0', '-N', '-p1', '-u',
  123. '-V', 'never', '-g0', '-E', '-b',
  124. '-B', ".pc/$patch/", '--reject-file=-' ]);
  125. };
  126. if ($@) {
  127. info(_g("fuzz is not allowed when applying patches"));
  128. info(_g("if patch '%s' is correctly applied by quilt, use '%s' to update it"),
  129. $patch, "quilt refresh");
  130. $self->restore_quilt_backup_files($patch, %opts);
  131. erasedir($self->get_db_file($patch));
  132. die $@;
  133. }
  134. CORE::push @{$self->{'applied-patches'}}, $patch;
  135. $self->write_db();
  136. }
  137. sub pop {
  138. my ($self, %opts) = @_;
  139. $opts{"verbose"} = 0 unless defined($opts{"verbose"});
  140. $opts{"timestamp"} = fs_time($self->{'dir'}) unless defined($opts{"timestamp"});
  141. $opts{"reverse_apply"} = 0 unless defined($opts{"reverse_apply"});
  142. my $patch = $self->top();
  143. return unless defined $patch;
  144. info(_g("unapplying %s"), $patch) if $opts{"verbose"};
  145. my $backup_dir = $self->get_db_file($patch);
  146. if (-d $backup_dir and not $opts{"reverse_apply"}) {
  147. # Use the backup copies to restore
  148. $self->restore_quilt_backup_files($patch);
  149. } else {
  150. # Otherwise reverse-apply the patch
  151. my $path = $self->get_patch_file($patch);
  152. my $obj = Dpkg::Source::Patch->new(filename => $path);
  153. $obj->apply($self->{'dir'}, timestamp => $opts{"timestamp"},
  154. verbose => 0, force_timestamp => 1, remove_backup => 0,
  155. options => [ '-R', '-t', '-N', '-p1',
  156. '-u', '-V', 'never', '-g0', '-E',
  157. '--no-backup-if-mismatch' ]);
  158. }
  159. erasedir($backup_dir);
  160. pop @{$self->{'applied-patches'}};
  161. $self->write_db();
  162. }
  163. sub get_db_version {
  164. my ($self) = @_;
  165. my $pc_ver = $self->get_db_file(".version");
  166. if (-f $pc_ver) {
  167. open(VER, "<", $pc_ver) || syserr(_g("cannot read %s"), $pc_ver);
  168. my $version = <VER>;
  169. chomp $version;
  170. close(VER);
  171. return $version;
  172. }
  173. return undef;
  174. }
  175. sub find_problems {
  176. my ($self) = @_;
  177. my $patch_dir = $self->get_patch_file();
  178. if (-e $patch_dir and not -d _) {
  179. return sprintf(_g("%s should be a directory or non-existing"), $patch_dir);
  180. }
  181. my $series = $self->get_series_file();
  182. if (-e $series and not -f _) {
  183. return sprintf(_g("%s should be a file or non-existing"), $series);
  184. }
  185. return undef;
  186. }
  187. sub get_series_file {
  188. my ($self) = @_;
  189. my $vendor = lc(get_current_vendor() || "debian");
  190. # Series files are stored alongside patches
  191. my $default_series = $self->get_patch_file("series");
  192. my $vendor_series = $self->get_patch_file("$vendor.series");
  193. return $vendor_series if -e $vendor_series;
  194. return $default_series;
  195. }
  196. sub get_db_file {
  197. my $self = shift;
  198. return File::Spec->catfile($self->{'dir'}, ".pc", @_);
  199. }
  200. sub get_db_dir {
  201. my ($self) = @_;
  202. return $self->get_db_file();
  203. }
  204. sub get_patch_file {
  205. my $self = shift;
  206. return File::Spec->catfile($self->{'dir'}, "debian", "patches", @_);
  207. }
  208. sub get_patch_dir {
  209. my ($self) = @_;
  210. return $self->get_patch_file();
  211. }
  212. ## METHODS BELOW ARE INTERNAL ##
  213. sub read_patch_list {
  214. my ($self, $file, %opts) = @_;
  215. return () if not defined $file or not -f $file;
  216. $opts{"warn_options"} = 0 unless defined($opts{"warn_options"});
  217. my @patches;
  218. open(SERIES, "<" , $file) || syserr(_g("cannot read %s"), $file);
  219. while(defined($_ = <SERIES>)) {
  220. chomp; s/^\s+//; s/\s+$//; # Strip leading/trailing spaces
  221. s/(^|\s+)#.*$//; # Strip comment
  222. next unless $_;
  223. if (/^(\S+)\s+(.*)$/) {
  224. $_ = $1;
  225. if ($2 ne '-p1') {
  226. warning(_g("the series file (%s) contains unsupported " .
  227. "options ('%s', line %s); dpkg-source might " .
  228. "fail when applying patches"),
  229. $file, $2, $.) if $opts{"warn_options"};
  230. }
  231. }
  232. error(_g("%s contains an insecure path: %s"), $file, $_) if m{(^|/)\.\./};
  233. CORE::push @patches, $_;
  234. }
  235. close(SERIES);
  236. return @patches;
  237. }
  238. sub restore_quilt_backup_files {
  239. my ($self, $patch, %opts) = @_;
  240. my $patch_dir = $self->get_db_file($patch);
  241. return unless -d $patch_dir;
  242. info(_g("restoring quilt backup files for %s"), $patch) if $opts{'verbose'};
  243. find({
  244. no_chdir => 1,
  245. wanted => sub {
  246. return if -d $_;
  247. my $relpath_in_srcpkg = File::Spec->abs2rel($_, $patch_dir);
  248. my $target = File::Spec->catfile($self->{'dir'}, $relpath_in_srcpkg);
  249. if (-s $_) {
  250. unlink($target);
  251. unless (link($_, $target)) {
  252. copy($_, $target) ||
  253. syserr(_g("failed to copy %s to %s"), $_, $target);
  254. chmod($target, (stat(_))[2]) ||
  255. syserr(_g("unable to change permission of `%s'"), $target);
  256. }
  257. } else {
  258. # empty files are "backups" for new files that patch created
  259. unlink($target);
  260. }
  261. }
  262. }, $patch_dir);
  263. }
  264. # vim:et:sw=4:ts=8
  265. 1;