Quilt.pm 9.2 KB

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