1 # Copyright © 2008 Raphaël Hertzog <hertzog@debian.org>
2 # Copyright © 2008-2010, 2012-2015 Guillem Jover <guillem@debian.org>
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
21 Dpkg::Source::Patch - represent a patch file
25 This module provides a class to handle patch files.
27 B<Note>: This is a private module, its API can change at any time.
31 package Dpkg
::Source
::Patch
0.01;
36 use POSIX
qw(:errno_h :sys_wait_h);
40 use File
::Path
qw(make_path);
43 use Time
::HiRes
qw(stat);
47 use Dpkg
::ErrorHandling
;
49 use Dpkg
::Source
::Functions
qw(fs_time);
51 use parent
qw(Dpkg::Compression::FileHandle);
54 my ($self, %opts) = @_;
55 $self->ensure_open('w'); # Creates the file
58 if ($opts{old
} and $opts{new
} and $opts{filename
}) {
59 $opts{old
} = '/dev/null' unless -e
$opts{old
};
60 $opts{new
} = '/dev/null' unless -e
$opts{new
};
61 if (-d
$opts{old
} and -d
$opts{new
}) {
62 $self->add_diff_directory($opts{old
}, $opts{new
}, %opts);
63 } elsif (-f
$opts{old
} and -f
$opts{new
}) {
64 $self->add_diff_file($opts{old
}, $opts{new
}, %opts);
66 $self->_fail_not_same_type($opts{old
}, $opts{new
}, $opts{filename
});
68 $self->finish() unless $opts{nofinish
};
73 my ($self, $header) = @_;
74 *$self->{header
} = $header;
80 if (ref *$self->{header
} eq 'CODE') {
81 return *$self->{header
}->();
83 return *$self->{header
};
88 my ($self, $old, $new, %opts) = @_;
89 $opts{include_timestamp
} //= 0;
90 my $handle_binary = $opts{handle_binary_func
} // sub {
91 my ($self, $old, $new, %opts) = @_;
92 my $file = $opts{filename
};
93 $self->_fail_with_msg($file, g_
('binary file contents changed'));
95 # Optimization to avoid forking diff if unnecessary
96 return 1 if compare
($old, $new, 4096) == 0;
97 # Default diff options
100 push @options, @
{$opts{options
}};
105 if ($opts{label_old
} and $opts{label_new
}) {
106 if ($opts{include_timestamp
}) {
107 my $ts = (stat($old))[9];
108 my $t = POSIX
::strftime
('%Y-%m-%d %H:%M:%S', gmtime($ts));
109 $opts{label_old
} .= sprintf("\t%s.%09d +0000", $t,
110 ($ts - int($ts)) * 1_000_000_000
);
111 $ts = (stat($new))[9];
112 $t = POSIX
::strftime
('%Y-%m-%d %H:%M:%S', gmtime($ts));
113 $opts{label_new
} .= sprintf("\t%s.%09d +0000", $t,
114 ($ts - int($ts)) * 1_000_000_000
);
116 # Space in filenames need special treatment
117 $opts{label_old
} .= "\t" if $opts{label_old
} =~ / /;
118 $opts{label_new
} .= "\t" if $opts{label_new
} =~ / /;
120 push @options, '-L', $opts{label_old
},
121 '-L', $opts{label_new
};
125 my $diff_pid = spawn
(
126 exec => [ 'diff', '-u', @options, '--', $old, $new ],
127 env
=> { LC_ALL
=> 'C', TZ
=> 'UTC0' },
128 to_pipe
=> \
$diffgen,
130 # Check diff and write it in patch file
131 my $difflinefound = 0;
136 if (m/^(?:binary|[^-+\@ ].*\bdiffer\b)/i) {
138 $handle_binary->($self, $old, $new, %opts);
140 } elsif (m/^[-+\@ ]/) {
143 warning
(g_
('file %s has no final newline (either ' .
144 'original or modified version)'), $new);
147 error
(g_
("unknown line from diff -u on %s: '%s'"), $new, $_);
149 if (*$self->{empty
} and defined(*$self->{header
})) {
150 $self->print($self->get_header()) or syserr
(g_
('failed to write'));
153 print { $self } $_ or syserr
(g_
('failed to write'));
155 close($diffgen) or syserr
('close on diff pipe');
156 wait_child
($diff_pid, nocheck
=> 1,
157 cmdline
=> "diff -u @options -- $old $new");
158 # Verify diff process ended successfully
159 # Exit code of diff: 0 => no difference, 1 => diff ok, 2 => error
160 # Ignore error if binary content detected
161 my $exit = WEXITSTATUS
($?
);
162 unless (WIFEXITED
($?
) && ($exit == 0 || $exit == 1 || $binary)) {
163 subprocerr
(g_
('diff on %s'), $new);
165 return ($exit == 0 || $exit == 1);
168 sub add_diff_directory
{
169 my ($self, $old, $new, %opts) = @_;
170 # TODO: make this function more configurable
171 # - offer to disable some checks
172 my $basedir = $opts{basedirname
} || basename
($new);
174 if ($opts{diff_ignore_func
}) {
175 $diff_ignore = $opts{diff_ignore_func
};
176 } elsif ($opts{diff_ignore_regex
}) {
177 $diff_ignore = sub { return $_[0] =~ /$opts{diff_ignore_regex}/o };
179 $diff_ignore = sub { return 0 };
185 my $fn = (length > length($new)) ?
substr($_, length($new) + 1) : '.';
186 return if $diff_ignore->($fn);
187 $files_in_new{$fn} = 1;
188 lstat("$new/$fn") or syserr
(g_
('cannot stat file %s'), "$new/$fn");
189 my $mode = S_IMODE
((lstat(_
))[2]);
190 my $size = (lstat(_
))[7];
192 unless (-l
"$old/$fn") {
193 $self->_fail_not_same_type("$old/$fn", "$new/$fn", $fn);
196 my $n = readlink("$new/$fn");
197 unless (defined $n) {
198 syserr
(g_
('cannot read link %s'), "$new/$fn");
200 my $n2 = readlink("$old/$fn");
201 unless (defined $n2) {
202 syserr
(g_
('cannot read link %s'), "$old/$fn");
205 $self->_fail_not_same_type("$old/$fn", "$new/$fn", $fn);
208 my $old_file = "$old/$fn";
209 if (not lstat("$old/$fn")) {
211 syserr
(g_
('cannot stat file %s'), "$old/$fn");
213 $old_file = '/dev/null';
215 $self->_fail_not_same_type("$old/$fn", "$new/$fn", $fn);
219 my $label_old = "$basedir.orig/$fn";
220 if ($opts{use_dev_null
}) {
221 $label_old = $old_file if $old_file eq '/dev/null';
223 push @diff_files, [$fn, $mode, $size, $old_file, "$new/$fn",
224 $label_old, "$basedir/$fn"];
226 unless (-p
"$old/$fn") {
227 $self->_fail_not_same_type("$old/$fn", "$new/$fn", $fn);
229 } elsif (-b _
|| -c _
|| -S _
) {
230 $self->_fail_with_msg("$new/$fn",
231 g_
('device or socket is not allowed'));
233 if (not lstat("$old/$fn")) {
235 syserr
(g_
('cannot stat file %s'), "$old/$fn");
238 $self->_fail_not_same_type("$old/$fn", "$new/$fn", $fn);
241 $self->_fail_with_msg("$new/$fn", g_
('unknown file type'));
245 my $fn = (length > length($old)) ?
substr($_, length($old) + 1) : '.';
246 return if $diff_ignore->($fn);
247 return if $files_in_new{$fn};
248 lstat("$old/$fn") or syserr
(g_
('cannot stat file %s'), "$old/$fn");
250 if (not defined $opts{include_removal
}) {
251 warning
(g_
('ignoring deletion of file %s'), $fn);
252 } elsif (not $opts{include_removal
}) {
253 warning
(g_
('ignoring deletion of file %s, use --include-removal to override'), $fn);
255 push @diff_files, [$fn, 0, 0, "$old/$fn", '/dev/null',
256 "$basedir.orig/$fn", '/dev/null'];
259 warning
(g_
('ignoring deletion of directory %s'), $fn);
261 warning
(g_
('ignoring deletion of symlink %s'), $fn);
263 $self->_fail_not_same_type("$old/$fn", "$new/$fn", $fn);
267 find
({ wanted
=> $scan_new, no_chdir
=> 1 }, $new);
268 find
({ wanted
=> $scan_old, no_chdir
=> 1 }, $old);
270 if ($opts{order_from
} and -e
$opts{order_from
}) {
271 my $order_from = Dpkg
::Source
::Patch
->new(
272 filename
=> $opts{order_from
});
273 my $analysis = $order_from->analyze($basedir, verbose
=> 0);
276 foreach my $fn (@
{$analysis->{patchorder
}}) {
278 $patchorder{$fn} = $i++;
280 # 'quilt refresh' sorts files as follows:
281 # - Any files in the existing patch come first, in the order in
282 # which they appear in the existing patch.
283 # - New files follow, sorted lexicographically.
284 # This seems a reasonable policy to follow, and avoids autopatches
285 # being shuffled when they are regenerated.
286 foreach my $diff_file (sort { $a->[0] cmp $b->[0] } @diff_files) {
287 my $fn = $diff_file->[0];
288 $patchorder{$fn} //= $i++;
290 @diff_files = sort { $patchorder{$a->[0]} <=> $patchorder{$b->[0]} }
293 @diff_files = sort { $a->[0] cmp $b->[0] } @diff_files;
296 foreach my $diff_file (@diff_files) {
297 my ($fn, $mode, $size,
298 $old_file, $new_file, $label_old, $label_new) = @
$diff_file;
299 my $success = $self->add_diff_file($old_file, $new_file,
301 label_old
=> $label_old,
302 label_new
=> $label_new, %opts);
304 $old_file eq '/dev/null' and $new_file ne '/dev/null') {
306 warning
(g_
("newly created empty file '%s' will not " .
307 'be represented in diff'), $fn);
309 if ($mode & (S_IXUSR
| S_IXGRP
| S_IXOTH
)) {
310 warning
(g_
("executable mode %04o of '%s' will " .
311 'not be represented in diff'), $mode, $fn)
312 unless $fn eq 'debian/rules';
314 if ($mode & (S_ISUID
| S_ISGID
| S_ISVTX
)) {
315 warning
(g_
("special mode %04o of '%s' will not " .
316 'be represented in diff'), $mode, $fn);
325 close($self) or syserr
(g_
('cannot close %s'), $self->get_filename());
326 return not *$self->{errors
};
334 my ($self, $file, $msg) = @_;
335 errormsg
(g_
('cannot represent change to %s: %s'), $file, $msg);
336 $self->register_error();
338 sub _fail_not_same_type
{
339 my ($self, $old, $new, $file) = @_;
340 my $old_type = get_type
($old);
341 my $new_type = get_type
($new);
342 errormsg
(g_
('cannot represent change to %s:'), $file);
343 errormsg
(g_
(' new version is %s'), $new_type);
344 errormsg
(g_
(' old version is %s'), $old_type);
345 $self->register_error();
351 my $line = <$handle>;
353 # Strip end-of-line chars
360 # Fetch the header filename ignoring the optional timestamp
361 sub _fetch_filename
{
362 my ($diff, $header) = @_;
364 # Strip any leading spaces.
367 # Is it a C-style string?
368 if ($header =~ m/^"/) {
369 error
(g_
('diff %s patches file with C-style encoded filename'), $diff);
371 # Tab is the official separator, it's always used when
372 # filename contain spaces. Try it first, otherwise strip on space
374 $header =~ s/\s.*// unless $header =~ s/\t.*//;
380 sub _intuit_file_patched
{
381 my ($old, $new) = @_;
383 return $new unless defined $old;
384 return $old unless defined $new;
385 return $new if -e
$new and not -e
$old;
386 return $old if -e
$old and not -e
$new;
388 # We don't consider the case where both files are non-existent and
389 # where patch picks the one with the fewest directories to create
390 # since dpkg-source will pre-create the required directories
392 # Precalculate metrics used by patch
393 my ($tmp_o, $tmp_n) = ($old, $new);
394 my ($len_o, $len_n) = (length($old), length($new));
395 $tmp_o =~ s{[/\\]+}{/}g;
396 $tmp_n =~ s{[/\\]+}{/}g;
397 my $nb_comp_o = ($tmp_o =~ tr{/}{/});
398 my $nb_comp_n = ($tmp_n =~ tr{/}{/});
401 my ($blen_o, $blen_n) = (length($tmp_o), length($tmp_n));
403 # Decide like patch would
404 if ($nb_comp_o != $nb_comp_n) {
405 return ($nb_comp_o < $nb_comp_n) ?
$old : $new;
406 } elsif ($blen_o != $blen_n) {
407 return ($blen_o < $blen_n) ?
$old : $new;
408 } elsif ($len_o != $len_n) {
409 return ($len_o < $len_n) ?
$old : $new;
414 # check diff for sanity, find directories to create as a side effect
416 my ($self, $destdir, %opts) = @_;
418 $opts{verbose
} //= 1;
419 my $diff = $self->get_filename();
423 my $patch_header = '';
426 my $line = _getline
($self);
429 while (defined $line or not eof $self) {
432 # Skip comments leading up to the patch (if any). Although we do not
433 # look for an Index: pseudo-header in the comments, because we would
434 # not use it anyway, as we require both ---/+++ filename headers.
436 if ($line =~ /^(?:--- |\+\+\+ |@@ -)/) {
439 $patch_header .= "$line\n";
441 $line = _getline
($self);
442 last HUNK
if not defined $line;
445 # read file header (---/+++ pair)
446 unless ($line =~ s/^--- //) {
447 error
(g_
("expected ^--- in line %d of diff '%s'"), $., $diff);
449 $path{old
} = $line = _fetch_filename
($diff, $line);
450 if ($line ne '/dev/null' and $line =~ s{^[^/]*/+}{$destdir/}) {
453 if ($line =~ /\.dpkg-orig$/) {
454 error
(g_
("diff '%s' patches file with name ending in .dpkg-orig"),
458 $line = _getline
($self);
459 unless (defined $line) {
460 error
(g_
("diff '%s' finishes in middle of ---/+++ (line %d)"),
463 unless ($line =~ s/^\+\+\+ //) {
464 error
(g_
("line after --- isn't as expected in diff '%s' (line %d)"),
467 $path{new
} = $line = _fetch_filename
($diff, $line);
468 if ($line ne '/dev/null' and $line =~ s{^[^/]*/+}{$destdir/}) {
472 unless (defined $fn{old
} or defined $fn{new
}) {
473 error
(g_
("none of the filenames in ---/+++ are valid in diff '%s' (line %d)"),
477 # Safety checks on both filenames that patch could use
478 foreach my $key ('old', 'new') {
479 next unless defined $fn{$key};
480 if ($path{$key} =~ m{/\.\./}) {
481 error
(g_
('%s contains an insecure path: %s'), $diff, $path{$key});
483 my $path = $fn{$key};
486 error
(g_
('diff %s modifies file %s through a symlink: %s'),
487 $diff, $fn{$key}, $path);
489 last unless $path =~ s{/+[^/]*$}{};
490 last if length($path) <= length($destdir); # $destdir is assumed safe
494 if ($path{old
} eq '/dev/null' and $path{new
} eq '/dev/null') {
495 error
(g_
("original and modified files are /dev/null in diff '%s' (line %d)"),
497 } elsif ($path{new
} eq '/dev/null') {
498 error
(g_
("file removal without proper filename in diff '%s' (line %d)"),
499 $diff, $. - 1) unless defined $fn{old
};
500 if ($opts{verbose
}) {
501 warning
(g_
('diff %s removes a non-existing file %s (line %d)'),
502 $diff, $fn{old
}, $.) unless -e
$fn{old
};
505 my $fn = _intuit_file_patched
($fn{old
}, $fn{new
});
508 if ($dirname =~ s{/[^/]+$}{} and not -d
$dirname) {
509 $dirtocreate{$dirname} = 1;
512 if (-e
$fn and not -f _
) {
513 error
(g_
("diff '%s' patches something which is not a plain file"),
517 if ($filepatched{$fn}) {
520 if ($opts{fatal_dupes
}) {
521 error
(g_
("diff '%s' patches files multiple times; split the " .
522 'diff in multiple files or merge the hunks into a ' .
523 'single one'), $diff);
524 } elsif ($opts{verbose
} and $filepatched{$fn} == 2) {
525 warning
(g_
("diff '%s' patches file %s more than once"), $diff, $fn)
528 $filepatched{$fn} = 1;
529 push @patchorder, $fn;
534 while (defined($line = _getline
($self))) {
535 # read hunk header (@@)
536 next if $line =~ /^\\ /;
537 last unless $line =~ /^@@ -\d+(,(\d+))? \+\d+(,(\d+))? @\@(?: .*)?$/;
538 my ($olines, $nlines) = ($1 ?
$2 : 1, $3 ?
$4 : 1);
540 while ($olines || $nlines) {
541 unless (defined($line = _getline
($self))) {
542 if (($olines == $nlines) and ($olines < 3)) {
543 warning
(g_
("unexpected end of diff '%s'"), $diff)
547 error
(g_
("unexpected end of diff '%s'"), $diff);
550 next if $line =~ /^\\ /;
552 if ($line =~ /^ / or length $line == 0) {
555 } elsif ($line =~ /^-/) {
557 } elsif ($line =~ /^\+/) {
560 error
(g_
("expected [ +-] at start of line %d of diff '%s'"),
567 error
(g_
("expected ^\@\@ at line %d of diff '%s'"), $., $diff);
571 unless ($diff_count) {
572 warning
(g_
("diff '%s' doesn't contain any patch"), $diff)
575 *$self->{analysis
}{$destdir}{dirtocreate
} = \
%dirtocreate;
576 *$self->{analysis
}{$destdir}{filepatched
} = \
%filepatched;
577 *$self->{analysis
}{$destdir}{patchorder
} = \
@patchorder;
578 *$self->{analysis
}{$destdir}{patchheader
} = $patch_header;
579 return *$self->{analysis
}{$destdir};
583 my ($self, $analysis, %opts) = @_;
584 if ($opts{create_dirs
}) {
585 foreach my $dir (keys %{$analysis->{dirtocreate
}}) {
586 eval { make_path
($dir, { mode
=> 0777 }) };
587 syserr
(g_
('cannot create directory %s'), $dir) if $@
;
593 my ($self, $destdir, %opts) = @_;
594 # Set default values to options
595 $opts{force_timestamp
} //= 1;
596 $opts{remove_backup
} //= 1;
597 $opts{create_dirs
} //= 1;
608 $opts{add_options
} //= [];
609 push @
{$opts{options
}}, @
{$opts{add_options
}};
610 # Check the diff and create missing directories
611 my $analysis = $self->analyze($destdir, %opts);
612 $self->prepare_apply($analysis, %opts);
614 $self->ensure_open('r');
615 my ($stdout, $stderr) = ('', '');
617 exec => [ $Dpkg::PROGPATCH
, @
{$opts{options
}} ],
619 env
=> { LC_ALL
=> 'C', PATCH_GET
=> '0' },
620 delete_env
=> [ 'POSIXLY_CORRECT' ], # ensure expected patch behaviour
623 from_handle
=> $self->get_filehandle(),
624 to_string
=> \
$stdout,
625 error_to_string
=> \
$stderr,
628 print { *STDOUT
} $stdout;
629 print { *STDERR
} $stderr;
630 subprocerr
("LC_ALL=C $Dpkg::PROGPATCH " . join(' ', @
{$opts{options
}}) .
631 ' < ' . $self->get_filename());
634 # Reset the timestamp of all the patched files
635 # and remove .dpkg-orig files
636 my @files = keys %{$analysis->{filepatched
}};
637 my $now = $opts{timestamp
};
638 $now //= fs_time
($files[0]) if $opts{force_timestamp
} && scalar @files;
639 foreach my $fn (@files) {
640 if ($opts{force_timestamp
}) {
641 utime($now, $now, $fn) or $! == ENOENT
642 or syserr
(g_
('cannot change timestamp for %s'), $fn);
644 if ($opts{remove_backup
}) {
646 unlink($fn) or syserr
(g_
('remove patch backup file %s'), $fn);
652 # Verify if check will work...
654 my ($self, $destdir, %opts) = @_;
655 # Set default values to options
656 $opts{create_dirs
} //= 1;
669 $opts{add_options
} //= [];
670 push @
{$opts{options
}}, @
{$opts{add_options
}};
671 # Check the diff and create missing directories
672 my $analysis = $self->analyze($destdir, %opts);
673 $self->prepare_apply($analysis, %opts);
675 $self->ensure_open('r');
676 my $patch_pid = spawn
(
677 exec => [ $Dpkg::PROGPATCH
, @
{$opts{options
}} ],
679 env
=> { LC_ALL
=> 'C', PATCH_GET
=> '0' },
680 delete_env
=> [ 'POSIXLY_CORRECT' ], # ensure expected patch behaviour
681 from_handle
=> $self->get_filehandle(),
682 to_file
=> '/dev/null',
683 error_to_file
=> '/dev/null',
685 wait_child
($patch_pid, nocheck
=> 1);
686 my $exit = WEXITSTATUS
($?
);
687 subprocerr
("$Dpkg::PROGPATCH --dry-run") unless WIFEXITED
($?
);
695 if (not lstat($file)) {
696 return g_
('nonexistent') if $! == ENOENT
;
697 syserr
(g_
('cannot stat %s'), $file);
699 -f _
&& return g_
('plain file');
700 -d _
&& return g_
('directory');
701 -l _
&& return sprintf(g_
('symlink to %s'), readlink($file));
702 -b _
&& return g_
('block device');
703 -c _
&& return g_
('character device');
704 -p _
&& return g_
('named pipe');
705 -S _
&& return g_
('named socket');
713 This is a private module.