Quilt.pm 9.3 KB

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