Quilt.pm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 File::Spec;
  20. use File::Copy;
  21. use File::Find;
  22. use File::Path qw(make_path);
  23. use File::Basename;
  24. use Dpkg::Gettext;
  25. use Dpkg::ErrorHandling;
  26. use Dpkg::Util qw(:list);
  27. use Dpkg::Source::Patch;
  28. use Dpkg::Source::Functions qw(erasedir fs_time);
  29. use Dpkg::Vendor qw(get_current_vendor);
  30. sub new {
  31. my ($this, $dir, %opts) = @_;
  32. my $class = ref($this) || $this;
  33. my $self = {
  34. dir => $dir,
  35. };
  36. bless $self, $class;
  37. $self->load_series();
  38. $self->load_db();
  39. return $self;
  40. }
  41. sub setup_db {
  42. my $self = shift;
  43. my $db_dir = $self->get_db_file();
  44. if (not -d $db_dir) {
  45. mkdir $db_dir or syserr(g_('cannot mkdir %s'), $db_dir);
  46. }
  47. my $file = $self->get_db_file('.version');
  48. if (not -e $file) {
  49. open(my $version_fh, '>', $file) or syserr(g_('cannot write %s'), $file);
  50. print { $version_fh } "2\n";
  51. close($version_fh);
  52. }
  53. # The files below are used by quilt to know where patches are stored
  54. # and what file contains the patch list (supported by quilt >= 0.48-5
  55. # in Debian).
  56. $file = $self->get_db_file('.quilt_patches');
  57. if (not -e $file) {
  58. open(my $qpatch_fh, '>', $file) or syserr(g_('cannot write %s'), $file);
  59. print { $qpatch_fh } "debian/patches\n";
  60. close($qpatch_fh);
  61. }
  62. $file = $self->get_db_file('.quilt_series');
  63. if (not -e $file) {
  64. open(my $qseries_fh, '>', $file) or syserr(g_('cannot write %s'), $file);
  65. my $series = $self->get_series_file();
  66. $series = (File::Spec->splitpath($series))[2];
  67. print { $qseries_fh } "$series\n";
  68. close($qseries_fh);
  69. }
  70. }
  71. sub load_db {
  72. my $self = shift;
  73. my $pc_applied = $self->get_db_file('applied-patches');
  74. $self->{applied_patches} = [ $self->read_patch_list($pc_applied) ];
  75. }
  76. sub save_db {
  77. my $self = shift;
  78. $self->setup_db();
  79. my $pc_applied = $self->get_db_file('applied-patches');
  80. $self->write_patch_list($pc_applied, $self->{applied_patches});
  81. }
  82. sub load_series {
  83. my ($self, %opts) = @_;
  84. my $series = $self->get_series_file();
  85. $self->{series} = [ $self->read_patch_list($series, %opts) ];
  86. }
  87. sub series {
  88. my $self = shift;
  89. return @{$self->{series}};
  90. }
  91. sub applied {
  92. my $self = shift;
  93. return @{$self->{applied_patches}};
  94. }
  95. sub top {
  96. my $self = shift;
  97. my $count = scalar @{$self->{applied_patches}};
  98. return $self->{applied_patches}[$count - 1] if $count;
  99. return;
  100. }
  101. sub register {
  102. my ($self, $patch_name) = @_;
  103. return if any { $_ eq $patch_name } @{$self->{series}};
  104. # Add patch to series files.
  105. $self->setup_db();
  106. $self->_file_add_line($self->get_series_file(), $patch_name);
  107. $self->_file_add_line($self->get_db_file('applied-patches'), $patch_name);
  108. $self->load_db();
  109. $self->load_series();
  110. # Ensure quilt meta-data is created and in sync with some trickery:
  111. # Reverse-apply the patch, drop .pc/$patch, and re-apply it with the
  112. # correct options to recreate the backup files.
  113. $self->pop(reverse_apply => 1);
  114. $self->push();
  115. }
  116. sub unregister {
  117. my ($self, $patch_name) = @_;
  118. return if none { $_ eq $patch_name } @{$self->{series}};
  119. my $series = $self->get_series_file();
  120. $self->_file_drop_line($series, $patch_name);
  121. $self->_file_drop_line($self->get_db_file('applied-patches'), $patch_name);
  122. erasedir($self->get_db_file($patch_name));
  123. $self->load_db();
  124. $self->load_series();
  125. # Clean up empty series.
  126. unlink $series if -z $series;
  127. }
  128. sub next {
  129. my $self = shift;
  130. my $count_applied = scalar @{$self->{applied_patches}};
  131. my $count_series = scalar @{$self->{series}};
  132. return $self->{series}[$count_applied] if ($count_series > $count_applied);
  133. return;
  134. }
  135. sub push {
  136. my ($self, %opts) = @_;
  137. $opts{verbose} //= 0;
  138. $opts{timestamp} //= fs_time($self->{dir});
  139. my $patch = $self->next();
  140. return unless defined $patch;
  141. my $path = $self->get_patch_file($patch);
  142. my $obj = Dpkg::Source::Patch->new(filename => $path);
  143. info(g_('applying %s'), $patch) if $opts{verbose};
  144. eval {
  145. $obj->apply($self->{dir}, timestamp => $opts{timestamp},
  146. verbose => $opts{verbose},
  147. force_timestamp => 1, create_dirs => 1, remove_backup => 0,
  148. options => [ '-t', '-F', '0', '-N', '-p1', '-u',
  149. '-V', 'never', '-E', '-b',
  150. '-B', ".pc/$patch/", '--reject-file=-' ]);
  151. };
  152. if ($@) {
  153. info(g_('the patch has fuzz which is not allowed, or is malformed'));
  154. info(g_("if patch '%s' is correctly applied by quilt, use '%s' to update it"),
  155. $patch, 'quilt refresh');
  156. $self->restore_quilt_backup_files($patch, %opts);
  157. erasedir($self->get_db_file($patch));
  158. die $@;
  159. }
  160. CORE::push @{$self->{applied_patches}}, $patch;
  161. $self->save_db();
  162. }
  163. sub pop {
  164. my ($self, %opts) = @_;
  165. $opts{verbose} //= 0;
  166. $opts{timestamp} //= fs_time($self->{dir});
  167. $opts{reverse_apply} //= 0;
  168. my $patch = $self->top();
  169. return unless defined $patch;
  170. info(g_('unapplying %s'), $patch) if $opts{verbose};
  171. my $backup_dir = $self->get_db_file($patch);
  172. if (-d $backup_dir and not $opts{reverse_apply}) {
  173. # Use the backup copies to restore
  174. $self->restore_quilt_backup_files($patch);
  175. } else {
  176. # Otherwise reverse-apply the patch
  177. my $path = $self->get_patch_file($patch);
  178. my $obj = Dpkg::Source::Patch->new(filename => $path);
  179. $obj->apply($self->{dir}, timestamp => $opts{timestamp},
  180. verbose => 0, force_timestamp => 1, remove_backup => 0,
  181. options => [ '-R', '-t', '-N', '-p1',
  182. '-u', '-V', 'never', '-E',
  183. '--no-backup-if-mismatch' ]);
  184. }
  185. erasedir($backup_dir);
  186. pop @{$self->{applied_patches}};
  187. $self->save_db();
  188. }
  189. sub get_db_version {
  190. my $self = shift;
  191. my $pc_ver = $self->get_db_file('.version');
  192. if (-f $pc_ver) {
  193. open(my $ver_fh, '<', $pc_ver) or syserr(g_('cannot read %s'), $pc_ver);
  194. my $version = <$ver_fh>;
  195. chomp $version;
  196. close($ver_fh);
  197. return $version;
  198. }
  199. return;
  200. }
  201. sub find_problems {
  202. my $self = shift;
  203. my $patch_dir = $self->get_patch_file();
  204. if (-e $patch_dir and not -d _) {
  205. return sprintf(g_('%s should be a directory or non-existing'), $patch_dir);
  206. }
  207. my $series = $self->get_series_file();
  208. if (-e $series and not -f _) {
  209. return sprintf(g_('%s should be a file or non-existing'), $series);
  210. }
  211. return;
  212. }
  213. sub get_series_file {
  214. my $self = shift;
  215. my $vendor = lc(get_current_vendor() || 'debian');
  216. # Series files are stored alongside patches
  217. my $default_series = $self->get_patch_file('series');
  218. my $vendor_series = $self->get_patch_file("$vendor.series");
  219. return $vendor_series if -e $vendor_series;
  220. return $default_series;
  221. }
  222. sub get_db_file {
  223. my $self = shift;
  224. return File::Spec->catfile($self->{dir}, '.pc', @_);
  225. }
  226. sub get_db_dir {
  227. my $self = shift;
  228. return $self->get_db_file();
  229. }
  230. sub get_patch_file {
  231. my $self = shift;
  232. return File::Spec->catfile($self->{dir}, 'debian', 'patches', @_);
  233. }
  234. sub get_patch_dir {
  235. my $self = shift;
  236. return $self->get_patch_file();
  237. }
  238. ## METHODS BELOW ARE INTERNAL ##
  239. sub _file_load {
  240. my ($self, $file) = @_;
  241. open my $file_fh, '<', $file or syserr(g_('cannot read %s'), $file);
  242. my @lines = <$file_fh>;
  243. close $file_fh;
  244. return @lines;
  245. }
  246. sub _file_add_line {
  247. my ($self, $file, $line) = @_;
  248. my @lines;
  249. @lines = $self->_file_load($file) if -f $file;
  250. CORE::push @lines, $line;
  251. chomp @lines;
  252. open my $file_fh, '>', $file or syserr(g_('cannot write %s'), $file);
  253. print { $file_fh } "$_\n" foreach @lines;
  254. close $file_fh;
  255. }
  256. sub _file_drop_line {
  257. my ($self, $file, $re) = @_;
  258. my @lines = $self->_file_load($file);
  259. open my $file_fh, '>', $file or syserr(g_('cannot write %s'), $file);
  260. print { $file_fh } $_ foreach grep { not /^\Q$re\E\s*$/ } @lines;
  261. close $file_fh;
  262. }
  263. sub read_patch_list {
  264. my ($self, $file, %opts) = @_;
  265. return () if not defined $file or not -f $file;
  266. $opts{warn_options} //= 0;
  267. my @patches;
  268. open(my $series_fh, '<' , $file) or syserr(g_('cannot read %s'), $file);
  269. while (defined(my $line = <$series_fh>)) {
  270. chomp $line;
  271. # Strip leading/trailing spaces
  272. $line =~ s/^\s+//;
  273. $line =~ s/\s+$//;
  274. # Strip comment
  275. $line =~ s/(?:^|\s+)#.*$//;
  276. next unless $line;
  277. if ($line =~ /^(\S+)\s+(.*)$/) {
  278. $line = $1;
  279. if ($2 ne '-p1') {
  280. warning(g_('the series file (%s) contains unsupported ' .
  281. "options ('%s', line %s); dpkg-source might " .
  282. 'fail when applying patches'),
  283. $file, $2, $.) if $opts{warn_options};
  284. }
  285. }
  286. if ($line =~ m{(^|/)\.\./}) {
  287. error(g_('%s contains an insecure path: %s'), $file, $line);
  288. }
  289. CORE::push @patches, $line;
  290. }
  291. close($series_fh);
  292. return @patches;
  293. }
  294. sub write_patch_list {
  295. my ($self, $series, $patches) = @_;
  296. open my $series_fh, '>', $series or syserr(g_('cannot write %s'), $series);
  297. foreach my $patch (@{$patches}) {
  298. print { $series_fh } "$patch\n";
  299. }
  300. close $series_fh;
  301. }
  302. sub restore_quilt_backup_files {
  303. my ($self, $patch, %opts) = @_;
  304. my $patch_dir = $self->get_db_file($patch);
  305. return unless -d $patch_dir;
  306. info(g_('restoring quilt backup files for %s'), $patch) if $opts{verbose};
  307. find({
  308. no_chdir => 1,
  309. wanted => sub {
  310. return if -d;
  311. my $relpath_in_srcpkg = File::Spec->abs2rel($_, $patch_dir);
  312. my $target = File::Spec->catfile($self->{dir}, $relpath_in_srcpkg);
  313. if (-s) {
  314. unlink($target);
  315. make_path(dirname($target));
  316. unless (link($_, $target)) {
  317. copy($_, $target)
  318. or syserr(g_('failed to copy %s to %s'), $_, $target);
  319. chmod((stat(_))[2], $target)
  320. or syserr(g_("unable to change permission of '%s'"), $target);
  321. }
  322. } else {
  323. # empty files are "backups" for new files that patch created
  324. unlink($target);
  325. }
  326. }
  327. }, $patch_dir);
  328. }
  329. 1;