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/>.
20 Dpkg::Source::Quilt - represent a quilt patch queue
24 This module provides a class to handle quilt patch queues.
26 B<Note>: This is a private module, its API can change at any time.
30 package Dpkg
::Source
::Quilt
0.02;
35 use List
::Util
qw(any none);
39 use File
::Path
qw(make_path);
43 use Dpkg
::ErrorHandling
;
45 use Dpkg
::Source
::Patch
;
46 use Dpkg
::Source
::Functions
qw(erasedir chmod_if_needed fs_time);
47 use Dpkg
::Vendor
qw(get_current_vendor);
50 my ($this, $dir, %opts) = @_;
51 my $class = ref($this) || $this;
66 my $db_dir = $self->get_db_dir();
68 mkdir $db_dir or syserr
(g_
('cannot mkdir %s'), $db_dir);
70 my $file = $self->get_db_file('.version');
72 file_dump
($file, "2\n");
74 # The files below are used by quilt to know where patches are stored
75 # and what file contains the patch list (supported by quilt >= 0.48-5
77 $file = $self->get_db_file('.quilt_patches');
79 file_dump
($file, "debian/patches\n");
81 $file = $self->get_db_file('.quilt_series');
83 my $series = $self->get_series_file();
84 $series = (File
::Spec
->splitpath($series))[2];
85 file_dump
($file, "$series\n");
92 my $pc_applied = $self->get_db_file('applied-patches');
93 $self->{applied_patches
} = [ $self->read_patch_list($pc_applied) ];
100 my $pc_applied = $self->get_db_file('applied-patches');
101 $self->write_patch_list($pc_applied, $self->{applied_patches
});
105 my ($self, %opts) = @_;
107 my $series = $self->get_series_file();
108 $self->{series
} = [ $self->read_patch_list($series, %opts) ];
113 return @
{$self->{series
}};
118 return @
{$self->{applied_patches
}};
123 my $count = scalar @
{$self->{applied_patches
}};
124 return $self->{applied_patches
}[$count - 1] if $count;
129 my ($self, $patch_name) = @_;
131 return if any
{ $_ eq $patch_name } @
{$self->{series
}};
133 # Add patch to series files.
135 $self->_file_add_line($self->get_series_file(), $patch_name);
136 $self->_file_add_line($self->get_db_file('applied-patches'), $patch_name);
138 $self->load_series();
140 # Ensure quilt meta-data is created and in sync with some trickery:
141 # Reverse-apply the patch, drop .pc/$patch, and re-apply it with the
142 # correct options to recreate the backup files.
143 $self->pop(reverse_apply
=> 1);
148 my ($self, $patch_name) = @_;
150 return if none
{ $_ eq $patch_name } @
{$self->{series
}};
152 my $series = $self->get_series_file();
154 $self->_file_drop_line($series, $patch_name);
155 $self->_file_drop_line($self->get_db_file('applied-patches'), $patch_name);
156 erasedir
($self->get_db_file($patch_name));
158 $self->load_series();
160 # Clean up empty series.
161 unlink $series if -z
$series;
166 my $count_applied = scalar @
{$self->{applied_patches
}};
167 my $count_series = scalar @
{$self->{series
}};
168 return $self->{series
}[$count_applied] if ($count_series > $count_applied);
173 my ($self, %opts) = @_;
174 $opts{verbose
} //= 0;
175 $opts{timestamp
} //= fs_time
($self->{dir
});
177 my $patch = $self->next();
178 return unless defined $patch;
180 my $path = $self->get_patch_file($patch);
181 my $obj = Dpkg
::Source
::Patch
->new(filename
=> $path);
183 info
(g_
('applying %s'), $patch) if $opts{verbose
};
185 $obj->apply($self->{dir
}, timestamp
=> $opts{timestamp
},
186 verbose
=> $opts{verbose
},
187 force_timestamp
=> 1, create_dirs
=> 1, remove_backup
=> 0,
188 options
=> [ '-t', '-F', '0', '-N', '-p1', '-u',
189 '-V', 'never', '-E', '-b',
190 '-B', ".pc/$patch/", '--reject-file=-' ]);
193 info
(g_
('the patch has fuzz which is not allowed, or is malformed'));
194 info
(g_
("if patch '%s' is correctly applied by quilt, use '%s' to update it"),
195 $patch, 'quilt refresh');
196 info
(g_
('if the file is present in the unpacked source, make sure it ' .
197 'is also present in the orig tarball'));
198 $self->restore_quilt_backup_files($patch, %opts);
199 erasedir
($self->get_db_file($patch));
202 CORE
::push @
{$self->{applied_patches
}}, $patch;
207 my ($self, %opts) = @_;
208 $opts{verbose
} //= 0;
209 $opts{timestamp
} //= fs_time
($self->{dir
});
210 $opts{reverse_apply
} //= 0;
212 my $patch = $self->top();
213 return unless defined $patch;
215 info
(g_
('unapplying %s'), $patch) if $opts{verbose
};
216 my $backup_dir = $self->get_db_file($patch);
217 if (-d
$backup_dir and not $opts{reverse_apply
}) {
218 # Use the backup copies to restore
219 $self->restore_quilt_backup_files($patch);
221 # Otherwise reverse-apply the patch
222 my $path = $self->get_patch_file($patch);
223 my $obj = Dpkg
::Source
::Patch
->new(filename
=> $path);
225 $obj->apply($self->{dir
}, timestamp
=> $opts{timestamp
},
226 verbose
=> 0, force_timestamp
=> 1, remove_backup
=> 0,
227 options
=> [ '-R', '-t', '-N', '-p1',
228 '-u', '-V', 'never', '-E',
229 '--no-backup-if-mismatch' ]);
232 erasedir
($backup_dir);
233 pop @
{$self->{applied_patches
}};
239 my $pc_ver = $self->get_db_file('.version');
241 my $version = file_slurp
($pc_ver);
250 my $patch_dir = $self->get_patch_file();
251 if (-e
$patch_dir and not -d _
) {
252 return sprintf(g_
('%s should be a directory or non-existing'), $patch_dir);
254 my $series = $self->get_series_file();
255 if (-e
$series and not -f _
) {
256 return sprintf(g_
('%s should be a file or non-existing'), $series);
261 sub get_series_file
{
263 my $vendor = lc(get_current_vendor
() || 'debian');
264 # Series files are stored alongside patches
265 my $default_series = $self->get_patch_file('series');
266 my $vendor_series = $self->get_patch_file("$vendor.series");
267 return $vendor_series if -e
$vendor_series;
268 return $default_series;
272 my ($self, $file) = @_;
274 return File
::Spec
->catfile($self->get_db_dir(), $file);
280 return File
::Spec
->catfile($self->{dir
}, '.pc');
284 my ($self, $file) = @_;
286 return File
::Spec
->catfile($self->{dir
}, 'debian', 'patches', $file);
291 return $self->get_patch_file();
294 ## METHODS BELOW ARE INTERNAL ##
297 my ($self, $file) = @_;
299 open my $file_fh, '<', $file or syserr
(g_
('cannot read %s'), $file);
300 my @lines = <$file_fh>;
307 my ($self, $file, $line) = @_;
310 @lines = $self->_file_load($file) if -f
$file;
311 CORE
::push @lines, $line;
314 open my $file_fh, '>', $file or syserr
(g_
('cannot write %s'), $file);
315 print { $file_fh } "$_\n" foreach @lines;
319 sub _file_drop_line
{
320 my ($self, $file, $re) = @_;
322 my @lines = $self->_file_load($file);
323 open my $file_fh, '>', $file or syserr
(g_
('cannot write %s'), $file);
324 print { $file_fh } $_ foreach grep { not /^\Q$re\E\s*$/ } @lines;
328 sub read_patch_list
{
329 my ($self, $file, %opts) = @_;
330 return () if not defined $file or not -f
$file;
331 $opts{warn_options
} //= 0;
333 open(my $series_fh, '<' , $file) or syserr
(g_
('cannot read %s'), $file);
334 while (defined(my $line = <$series_fh>)) {
336 # Strip leading/trailing spaces
340 $line =~ s/(?:^|\s+)#.*$//;
342 if ($line =~ /^(\S+)\s+(.*)$/) {
345 warning
(g_
('the series file (%s) contains unsupported ' .
346 "options ('%s', line %s); dpkg-source might " .
347 'fail when applying patches'),
348 $file, $2, $.) if $opts{warn_options
};
351 if ($line =~ m{(^|/)\.\./}) {
352 error
(g_
('%s contains an insecure path: %s'), $file, $line);
354 CORE
::push @patches, $line;
360 sub write_patch_list
{
361 my ($self, $series, $patches) = @_;
363 open my $series_fh, '>', $series or syserr
(g_
('cannot write %s'), $series);
364 foreach my $patch (@
{$patches}) {
365 print { $series_fh } "$patch\n";
370 sub restore_quilt_backup_files
{
371 my ($self, $patch, %opts) = @_;
372 my $patch_dir = $self->get_db_file($patch);
373 return unless -d
$patch_dir;
374 info
(g_
('restoring quilt backup files for %s'), $patch) if $opts{verbose
};
379 my $relpath_in_srcpkg = File
::Spec
->abs2rel($_, $patch_dir);
380 my $target = File
::Spec
->catfile($self->{dir
}, $relpath_in_srcpkg);
383 make_path
(dirname
($target));
384 unless (link($_, $target)) {
386 or syserr
(g_
('failed to copy %s to %s'), $_, $target);
387 chmod_if_needed
((stat _
)[2], $target)
388 or syserr
(g_
("unable to change permission of '%s'"), $target);
391 # empty files are "backups" for new files that patch created
402 This is a private module.