File Coverage

blib/lib/Dpkg/Source/Quilt.pm
Criterion Covered Total %
statement 83 250 33.2
branch 10 86 11.6
condition 5 31 16.1
subroutine 21 42 50.0
pod 0 23 0.0
total 119 432 27.5


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