Quilt.pm 9.1 KB

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