1 # Copyright © 2008-2012 Raphaël Hertzog <hertzog@debian.org>
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.
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.
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/>.
16 package Dpkg::Source::Quilt;
21 our $VERSION = '0.02';
26 use File::Path qw(make_path);
30 use Dpkg::ErrorHandling;
31 use Dpkg::Util qw(:list);
32 use Dpkg::Source::Patch;
33 use Dpkg::Source::Functions qw(erasedir fs_time);
34 use Dpkg::Vendor qw(get_current_vendor);
37 my ($this, $dir, %opts) = @_;
38 my $class = ref($this) || $this;
53 my $db_dir = $self->get_db_file();
55 mkdir $db_dir or syserr(g_('cannot mkdir %s'), $db_dir);
57 my $file = $self->get_db_file('.version');
59 open(my $version_fh, '>', $file) or syserr(g_('cannot write %s'), $file);
60 print { $version_fh } "2\n";
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
66 $file = $self->get_db_file('.quilt_patches');
68 open(my $qpatch_fh, '>', $file) or syserr(g_('cannot write %s'), $file);
69 print { $qpatch_fh } "debian/patches\n";
72 $file = $self->get_db_file('.quilt_series');
74 open(my $qseries_fh, '>', $file) or syserr(g_('cannot write %s'), $file);
75 my $series = $self->get_series_file();
76 $series = (File::Spec->splitpath($series))[2];
77 print { $qseries_fh } "$series\n";
85 my $pc_applied = $self->get_db_file('applied-patches');
86 $self->{applied_patches} = [ $self->read_patch_list($pc_applied) ];
93 my $pc_applied = $self->get_db_file('applied-patches');
94 $self->write_patch_list($pc_applied, $self->{applied_patches});
98 my ($self, %opts) = @_;
100 my $series = $self->get_series_file();
101 $self->{series} = [ $self->read_patch_list($series, %opts) ];
106 return @{$self->{series}};
111 return @{$self->{applied_patches}};
116 my $count = scalar @{$self->{applied_patches}};
117 return $self->{applied_patches}[$count - 1] if $count;
122 my ($self, $patch_name) = @_;
124 return if any { $_ eq $patch_name } @{$self->{series}};
126 # Add patch to series files.
128 $self->_file_add_line($self->get_series_file(), $patch_name);
129 $self->_file_add_line($self->get_db_file('applied-patches'), $patch_name);
131 $self->load_series();
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 $self->pop(reverse_apply => 1);
141 my ($self, $patch_name) = @_;
143 return if none { $_ eq $patch_name } @{$self->{series}};
145 my $series = $self->get_series_file();
147 $self->_file_drop_line($series, $patch_name);
148 $self->_file_drop_line($self->get_db_file('applied-patches'), $patch_name);
149 erasedir($self->get_db_file($patch_name));
151 $self->load_series();
153 # Clean up empty series.
154 unlink $series if -z $series;
159 my $count_applied = scalar @{$self->{applied_patches}};
160 my $count_series = scalar @{$self->{series}};
161 return $self->{series}[$count_applied] if ($count_series > $count_applied);
166 my ($self, %opts) = @_;
167 $opts{verbose} //= 0;
168 $opts{timestamp} //= fs_time($self->{dir});
170 my $patch = $self->next();
171 return unless defined $patch;
173 my $path = $self->get_patch_file($patch);
174 my $obj = Dpkg::Source::Patch->new(filename => $path);
176 info(g_('applying %s'), $patch) if $opts{verbose};
178 $obj->apply($self->{dir}, timestamp => $opts{timestamp},
179 verbose => $opts{verbose},
180 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=-' ]);
186 info(g_('the patch has fuzz which is not allowed, or is malformed'));
187 info(g_("if patch '%s' is correctly applied by quilt, use '%s' to update it"),
188 $patch, 'quilt refresh');
189 $self->restore_quilt_backup_files($patch, %opts);
190 erasedir($self->get_db_file($patch));
193 CORE::push @{$self->{applied_patches}}, $patch;
198 my ($self, %opts) = @_;
199 $opts{verbose} //= 0;
200 $opts{timestamp} //= fs_time($self->{dir});
201 $opts{reverse_apply} //= 0;
203 my $patch = $self->top();
204 return unless defined $patch;
206 info(g_('unapplying %s'), $patch) if $opts{verbose};
207 my $backup_dir = $self->get_db_file($patch);
208 if (-d $backup_dir and not $opts{reverse_apply}) {
209 # Use the backup copies to restore
210 $self->restore_quilt_backup_files($patch);
212 # Otherwise reverse-apply the patch
213 my $path = $self->get_patch_file($patch);
214 my $obj = Dpkg::Source::Patch->new(filename => $path);
216 $obj->apply($self->{dir}, timestamp => $opts{timestamp},
217 verbose => 0, force_timestamp => 1, remove_backup => 0,
218 options => [ '-R', '-t', '-N', '-p1',
219 '-u', '-V', 'never', '-E',
220 '--no-backup-if-mismatch' ]);
223 erasedir($backup_dir);
224 pop @{$self->{applied_patches}};
230 my $pc_ver = $self->get_db_file('.version');
232 open(my $ver_fh, '<', $pc_ver) or syserr(g_('cannot read %s'), $pc_ver);
233 my $version = <$ver_fh>;
243 my $patch_dir = $self->get_patch_file();
244 if (-e $patch_dir and not -d _) {
245 return sprintf(g_('%s should be a directory or non-existing'), $patch_dir);
247 my $series = $self->get_series_file();
248 if (-e $series and not -f _) {
249 return sprintf(g_('%s should be a file or non-existing'), $series);
254 sub get_series_file {
256 my $vendor = lc(get_current_vendor() || 'debian');
257 # Series files are stored alongside patches
258 my $default_series = $self->get_patch_file('series');
259 my $vendor_series = $self->get_patch_file("$vendor.series");
260 return $vendor_series if -e $vendor_series;
261 return $default_series;
266 return File::Spec->catfile($self->{dir}, '.pc', @_);
271 return $self->get_db_file();
276 return File::Spec->catfile($self->{dir}, 'debian', 'patches', @_);
281 return $self->get_patch_file();
284 ## METHODS BELOW ARE INTERNAL ##
287 my ($self, $file) = @_;
289 open my $file_fh, '<', $file or syserr(g_('cannot read %s'), $file);
290 my @lines = <$file_fh>;
297 my ($self, $file, $line) = @_;
300 @lines = $self->_file_load($file) if -f $file;
301 CORE::push @lines, $line;
304 open my $file_fh, '>', $file or syserr(g_('cannot write %s'), $file);
305 print { $file_fh } "$_\n" foreach @lines;
309 sub _file_drop_line {
310 my ($self, $file, $re) = @_;
312 my @lines = $self->_file_load($file);
313 open my $file_fh, '>', $file or syserr(g_('cannot write %s'), $file);
314 print { $file_fh } $_ foreach grep { not /^\Q$re\E\s*$/ } @lines;
318 sub read_patch_list {
319 my ($self, $file, %opts) = @_;
320 return () if not defined $file or not -f $file;
321 $opts{warn_options} //= 0;
323 open(my $series_fh, '<' , $file) or syserr(g_('cannot read %s'), $file);
324 while (defined(my $line = <$series_fh>)) {
326 # Strip leading/trailing spaces
330 $line =~ s/(?:^|\s+)#.*$//;
332 if ($line =~ /^(\S+)\s+(.*)$/) {
335 warning(g_('the series file (%s) contains unsupported ' .
336 "options ('%s', line %s); dpkg-source might " .
337 'fail when applying patches'),
338 $file, $2, $.) if $opts{warn_options};
341 if ($line =~ m{(^|/)\.\./}) {
342 error(g_('%s contains an insecure path: %s'), $file, $line);
344 CORE::push @patches, $line;
350 sub write_patch_list {
351 my ($self, $series, $patches) = @_;
353 open my $series_fh, '>', $series or syserr(g_('cannot write %s'), $series);
354 foreach my $patch (@{$patches}) {
355 print { $series_fh } "$patch\n";
360 sub restore_quilt_backup_files {
361 my ($self, $patch, %opts) = @_;
362 my $patch_dir = $self->get_db_file($patch);
363 return unless -d $patch_dir;
364 info(g_('restoring quilt backup files for %s'), $patch) if $opts{verbose};
369 my $relpath_in_srcpkg = File::Spec->abs2rel($_, $patch_dir);
370 my $target = File::Spec->catfile($self->{dir}, $relpath_in_srcpkg);
373 make_path(dirname($target));
374 unless (link($_, $target)) {
376 or syserr(g_('failed to copy %s to %s'), $_, $target);
377 chmod((stat(_))[2], $target)
378 or syserr(g_("unable to change permission of '%s'"), $target);
381 # empty files are "backups" for new files that patch created