Patch.pm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. # Copyright © 2008 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 <http://www.gnu.org/licenses/>.
  15. package Dpkg::Source::Patch;
  16. use strict;
  17. use warnings;
  18. our $VERSION = "0.01";
  19. use Dpkg;
  20. use Dpkg::Gettext;
  21. use Dpkg::IPC;
  22. use Dpkg::ErrorHandling;
  23. use POSIX;
  24. use File::Find;
  25. use File::Basename;
  26. use File::Spec;
  27. use File::Path;
  28. use File::Compare;
  29. use Fcntl ':mode';
  30. #XXX: Needed for sub-second timestamps, require recent perl
  31. #use Time::HiRes qw(stat);
  32. use base 'Dpkg::Compression::FileHandle';
  33. sub create {
  34. my ($self, %opts) = @_;
  35. $self->ensure_open("w"); # Creates the file
  36. *$self->{'errors'} = 0;
  37. *$self->{'empty'} = 1;
  38. if ($opts{'old'} and $opts{'new'}) {
  39. $opts{'old'} = "/dev/null" unless -e $opts{'old'};
  40. $opts{'new'} = "/dev/null" unless -e $opts{'new'};
  41. if (-d $opts{'old'} and -d $opts{'new'}) {
  42. $self->add_diff_directory($opts{'old'}, $opts{'new'}, %opts);
  43. } elsif (-f $opts{'old'} and -f $opts{'new'}) {
  44. $self->add_diff_file($opts{'old'}, $opts{'new'}, %opts);
  45. } else {
  46. $self->_fail_not_same_type($opts{'old'}, $opts{'new'});
  47. }
  48. $self->finish() unless $opts{"nofinish"};
  49. }
  50. }
  51. sub set_header {
  52. my ($self, $header) = @_;
  53. *$self->{'header'} = $header;
  54. }
  55. sub add_diff_file {
  56. my ($self, $old, $new, %opts) = @_;
  57. $opts{"include_timestamp"} = 0 unless exists $opts{"include_timestamp"};
  58. my $handle_binary = $opts{"handle_binary_func"} || sub {
  59. my ($self, $old, $new) = @_;
  60. $self->_fail_with_msg($new, _g("binary file contents changed"));
  61. };
  62. # Optimization to avoid forking diff if unnecessary
  63. return 1 if compare($old, $new, 4096) == 0;
  64. # Default diff options
  65. my @options;
  66. if ($opts{"options"}) {
  67. push @options, @{$opts{"options"}};
  68. } else {
  69. push @options, '-p';
  70. }
  71. # Add labels
  72. if ($opts{"label_old"} and $opts{"label_new"}) {
  73. if ($opts{"include_timestamp"}) {
  74. my $ts = (stat($old))[9];
  75. my $t = POSIX::strftime("%Y-%m-%d %H:%M:%S", gmtime($ts));
  76. $opts{"label_old"} .= sprintf("\t%s.%09d +0000", $t,
  77. ($ts-int($ts))*1000000000);
  78. $ts = (stat($new))[9];
  79. $t = POSIX::strftime("%Y-%m-%d %H:%M:%S", gmtime($ts));
  80. $opts{"label_new"} .= sprintf("\t%s.%09d +0000", $t,
  81. ($ts-int($ts))*1000000000);
  82. } else {
  83. # Space in filenames need special treatment
  84. $opts{"label_old"} .= "\t" if $opts{"label_old"} =~ / /;
  85. $opts{"label_new"} .= "\t" if $opts{"label_new"} =~ / /;
  86. }
  87. push @options, "-L", $opts{"label_old"},
  88. "-L", $opts{"label_new"};
  89. }
  90. # Generate diff
  91. my $diffgen;
  92. my $diff_pid = spawn(
  93. 'exec' => [ 'diff', '-u', @options, '--', $old, $new ],
  94. 'env' => { LC_ALL => 'C', LANG => 'C', TZ => 'UTC0' },
  95. 'to_pipe' => \$diffgen
  96. );
  97. # Check diff and write it in patch file
  98. my $difflinefound = 0;
  99. my $binary = 0;
  100. while (<$diffgen>) {
  101. if (m/^(?:binary|[^-+\@ ].*\bdiffer\b)/i) {
  102. $binary = 1;
  103. &$handle_binary($self, $old, $new);
  104. last;
  105. } elsif (m/^[-+\@ ]/) {
  106. $difflinefound++;
  107. } elsif (m/^\\ No newline at end of file$/) {
  108. warning(_g("file %s has no final newline (either " .
  109. "original or modified version)"), $new);
  110. } else {
  111. chomp;
  112. error(_g("unknown line from diff -u on %s: `%s'"), $new, $_);
  113. }
  114. if (*$self->{'empty'} and defined(*$self->{'header'})) {
  115. $self->print(*$self->{'header'}) or syserr(_g("failed to write"));
  116. *$self->{'empty'} = 0;
  117. }
  118. print $self $_ || syserr(_g("failed to write"));
  119. }
  120. close($diffgen) or syserr("close on diff pipe");
  121. wait_child($diff_pid, nocheck => 1,
  122. cmdline => "diff -u @options -- $old $new");
  123. # Verify diff process ended successfully
  124. # Exit code of diff: 0 => no difference, 1 => diff ok, 2 => error
  125. # Ignore error if binary content detected
  126. my $exit = WEXITSTATUS($?);
  127. unless (WIFEXITED($?) && ($exit == 0 || $exit == 1 || $binary)) {
  128. subprocerr(_g("diff on %s"), $new);
  129. }
  130. return ($exit == 0 || $exit == 1);
  131. }
  132. sub add_diff_directory {
  133. my ($self, $old, $new, %opts) = @_;
  134. # TODO: make this function more configurable
  135. # - offer to disable some checks
  136. my $basedir = $opts{"basedirname"} || basename($new);
  137. my $inc_removal = $opts{"include_removal"} || 0;
  138. my $diff_ignore;
  139. if ($opts{"diff_ignore_func"}) {
  140. $diff_ignore = $opts{"diff_ignore_func"};
  141. } elsif ($opts{"diff_ignore_regexp"}) {
  142. $diff_ignore = sub { return $_[0] =~ /$opts{"diff_ignore_regexp"}/o };
  143. } else {
  144. $diff_ignore = sub { return 0 };
  145. }
  146. my @diff_files;
  147. my %files_in_new;
  148. my $scan_new = sub {
  149. my $fn = (length > length($new)) ? substr($_, length($new) + 1) : '.';
  150. return if &$diff_ignore($fn);
  151. $files_in_new{$fn} = 1;
  152. lstat("$new/$fn") || syserr(_g("cannot stat file %s"), "$new/$fn");
  153. my $mode = S_IMODE((lstat(_))[2]);
  154. my $size = (lstat(_))[7];
  155. if (-l _) {
  156. unless (-l "$old/$fn") {
  157. $self->_fail_not_same_type("$old/$fn", "$new/$fn");
  158. return;
  159. }
  160. defined(my $n = readlink("$new/$fn")) ||
  161. syserr(_g("cannot read link %s"), "$new/$fn");
  162. defined(my $n2 = readlink("$old/$fn")) ||
  163. syserr(_g("cannot read link %s"), "$old/$fn");
  164. unless ($n eq $n2) {
  165. $self->_fail_not_same_type("$old/$fn", "$new/$fn");
  166. }
  167. } elsif (-f _) {
  168. my $old_file = "$old/$fn";
  169. if (not lstat("$old/$fn")) {
  170. $! == ENOENT ||
  171. syserr(_g("cannot stat file %s"), "$old/$fn");
  172. $old_file = '/dev/null';
  173. } elsif (not -f _) {
  174. $self->_fail_not_same_type("$old/$fn", "$new/$fn");
  175. return;
  176. }
  177. my $label_old = "$basedir.orig/$fn";
  178. if ($opts{'use_dev_null'}) {
  179. $label_old = $old_file if $old_file eq '/dev/null';
  180. }
  181. push @diff_files, [$fn, $mode, $size, $old_file, "$new/$fn",
  182. $label_old, "$basedir/$fn"];
  183. } elsif (-p _) {
  184. unless (-p "$old/$fn") {
  185. $self->_fail_not_same_type("$old/$fn", "$new/$fn");
  186. }
  187. } elsif (-b _ || -c _ || -S _) {
  188. $self->_fail_with_msg("$new/$fn",
  189. _g("device or socket is not allowed"));
  190. } elsif (-d _) {
  191. if (not lstat("$old/$fn")) {
  192. $! == ENOENT ||
  193. syserr(_g("cannot stat file %s"), "$old/$fn");
  194. } elsif (not -d _) {
  195. $self->_fail_not_same_type("$old/$fn", "$new/$fn");
  196. }
  197. } else {
  198. $self->_fail_with_msg("$new/$fn", _g("unknown file type"));
  199. }
  200. };
  201. my $scan_old = sub {
  202. my $fn = (length > length($old)) ? substr($_, length($old) + 1) : '.';
  203. return if &$diff_ignore($fn);
  204. return if $files_in_new{$fn};
  205. lstat("$old/$fn") || syserr(_g("cannot stat file %s"), "$old/$fn");
  206. if (-f _) {
  207. if ($inc_removal) {
  208. push @diff_files, [$fn, 0, 0, "$old/$fn", "/dev/null",
  209. "$basedir.orig/$fn", "/dev/null"];
  210. } else {
  211. warning(_g("ignoring deletion of file %s"), $fn);
  212. }
  213. } elsif (-d _) {
  214. warning(_g("ignoring deletion of directory %s"), $fn);
  215. } elsif (-l _) {
  216. warning(_g("ignoring deletion of symlink %s"), $fn);
  217. } else {
  218. $self->_fail_not_same_type("$old/$fn", "$new/$fn");
  219. }
  220. };
  221. find({ wanted => $scan_new, no_chdir => 1 }, $new);
  222. find({ wanted => $scan_old, no_chdir => 1 }, $old);
  223. if ($opts{"order_from"} and -e $opts{"order_from"}) {
  224. my $order_from = Dpkg::Source::Patch->new(
  225. filename => $opts{"order_from"});
  226. my $analysis = $order_from->analyze($basedir);
  227. my %patchorder;
  228. my $i = 0;
  229. foreach my $fn (@{$analysis->{"patchorder"}}) {
  230. $fn =~ s{^[^/]+/}{};
  231. $patchorder{$fn} = $i++;
  232. }
  233. # 'quilt refresh' sorts files as follows:
  234. # - Any files in the existing patch come first, in the order in
  235. # which they appear in the existing patch.
  236. # - New files follow, sorted lexicographically.
  237. # This seems a reasonable policy to follow, and avoids autopatches
  238. # being shuffled when they are regenerated.
  239. foreach my $diff_file (sort { $a->[0] cmp $b->[0] } @diff_files) {
  240. my $fn = $diff_file->[0];
  241. $patchorder{$fn} = $i++ unless exists $patchorder{$fn};
  242. }
  243. @diff_files = sort { $patchorder{$a->[0]} <=> $patchorder{$b->[0]} }
  244. @diff_files;
  245. }
  246. foreach my $diff_file (@diff_files) {
  247. my ($fn, $mode, $size,
  248. $old_file, $new_file, $label_old, $label_new) = @$diff_file;
  249. my $success = $self->add_diff_file($old_file, $new_file,
  250. label_old => $label_old,
  251. label_new => $label_new, %opts);
  252. if ($success and
  253. $old_file eq "/dev/null" and $new_file ne "/dev/null") {
  254. if (not $size) {
  255. warning(_g("newly created empty file '%s' will not " .
  256. "be represented in diff"), $fn);
  257. } else {
  258. if ($mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
  259. warning(_g("executable mode %04o of '%s' will " .
  260. "not be represented in diff"), $mode, $fn)
  261. unless $fn eq 'debian/rules';
  262. }
  263. if ($mode & (S_ISUID | S_ISGID | S_ISVTX)) {
  264. warning(_g("special mode %04o of '%s' will not " .
  265. "be represented in diff"), $mode, $fn);
  266. }
  267. }
  268. }
  269. }
  270. }
  271. sub finish {
  272. my ($self) = @_;
  273. close($self) || syserr(_g("cannot close %s"), $self->get_filename());
  274. return not *$self->{'errors'};
  275. }
  276. sub register_error {
  277. my ($self) = @_;
  278. *$self->{'errors'}++;
  279. }
  280. sub _fail_with_msg {
  281. my ($self, $file, $msg) = @_;
  282. errormsg(_g("cannot represent change to %s: %s"), $file, $msg);
  283. $self->register_error();
  284. }
  285. sub _fail_not_same_type {
  286. my ($self, $old, $new) = @_;
  287. my $old_type = get_type($old);
  288. my $new_type = get_type($new);
  289. errormsg(_g("cannot represent change to %s:"), $new);
  290. errormsg(_g(" new version is %s"), $new_type);
  291. errormsg(_g(" old version is %s"), $old_type);
  292. $self->register_error();
  293. }
  294. # check diff for sanity, find directories to create as a side effect
  295. sub analyze {
  296. my ($self, $destdir, %opts) = @_;
  297. my $diff = $self->get_filename();
  298. my %filepatched;
  299. my %dirtocreate;
  300. my @patchorder;
  301. my $diff_count = 0;
  302. sub getline {
  303. my $handle = shift;
  304. my $line = <$handle>;
  305. if (defined $line) {
  306. # Strip end-of-line chars
  307. chomp($line);
  308. $line =~ s/\r$//;
  309. }
  310. return $line;
  311. }
  312. sub strip_ts { # Strip timestamp
  313. my $header = shift;
  314. # Tab is the official separator, it's always used when
  315. # filename contain spaces. Try it first, otherwise strip on space
  316. # if there's no tab
  317. $header =~ s/\s.*// unless ($header =~ s/\t.*//);
  318. return $header;
  319. }
  320. sub intuit_file_patched {
  321. my ($old, $new) = @_;
  322. return $new unless defined $old;
  323. return $old unless defined $new;
  324. return $new if -e $new and not -e $old;
  325. return $old if -e $old and not -e $new;
  326. # We don't consider the case where both files are non-existent and
  327. # where patch picks the one with the fewest directories to create
  328. # since dpkg-source will pre-create the required directories
  329. #
  330. # Precalculate metrics used by patch
  331. my ($tmp_o, $tmp_n) = ($old, $new);
  332. my ($len_o, $len_n) = (length($old), length($new));
  333. $tmp_o =~ s{[/\\]+}{/}g;
  334. $tmp_n =~ s{[/\\]+}{/}g;
  335. my $nb_comp_o = ($tmp_o =~ tr{/}{/});
  336. my $nb_comp_n = ($tmp_n =~ tr{/}{/});
  337. $tmp_o =~ s{^.*/}{};
  338. $tmp_n =~ s{^.*/}{};
  339. my ($blen_o, $blen_n) = (length($tmp_o), length($tmp_n));
  340. # Decide like patch would
  341. if ($nb_comp_o != $nb_comp_n) {
  342. return ($nb_comp_o < $nb_comp_n) ? $old : $new;
  343. } elsif ($blen_o != $blen_n) {
  344. return ($blen_o < $blen_n) ? $old : $new;
  345. } elsif ($len_o != $len_n) {
  346. return ($len_o < $len_n) ? $old : $new;
  347. }
  348. return $old;
  349. }
  350. $_ = getline($self);
  351. HUNK:
  352. while (defined($_) || not eof($self)) {
  353. my (%path, %fn);
  354. # skip comments leading up to patch (if any)
  355. until (/^--- /) {
  356. last HUNK if not defined($_ = getline($self));
  357. }
  358. $diff_count++;
  359. # read file header (---/+++ pair)
  360. unless(s/^--- //) {
  361. error(_g("expected ^--- in line %d of diff `%s'"), $., $diff);
  362. }
  363. $path{'old'} = $_ = strip_ts($_);
  364. $fn{'old'} = $_ if $_ ne '/dev/null' and s{^[^/]*/+}{$destdir/};
  365. if (/\.dpkg-orig$/) {
  366. error(_g("diff `%s' patches file with name ending .dpkg-orig"), $diff);
  367. }
  368. unless (defined($_ = getline($self))) {
  369. error(_g("diff `%s' finishes in middle of ---/+++ (line %d)"), $diff, $.);
  370. }
  371. unless (s/^\+\+\+ //) {
  372. error(_g("line after --- isn't as expected in diff `%s' (line %d)"), $diff, $.);
  373. }
  374. $path{'new'} = $_ = strip_ts($_);
  375. $fn{'new'} = $_ if $_ ne '/dev/null' and s{^[^/]*/+}{$destdir/};
  376. unless (defined $fn{'old'} or defined $fn{'new'}) {
  377. error(_g("none of the filenames in ---/+++ are valid in diff '%s' (line %d)"),
  378. $diff, $.);
  379. }
  380. # Safety checks on both filenames that patch could use
  381. foreach my $key ("old", "new") {
  382. next unless defined $fn{$key};
  383. if ($path{$key} =~ m{/\.\./}) {
  384. error(_g("%s contains an insecure path: %s"), $diff, $path{$key});
  385. }
  386. my $path = $fn{$key};
  387. while (1) {
  388. if (-l $path) {
  389. error(_g("diff %s modifies file %s through a symlink: %s"),
  390. $diff, $fn{$key}, $path);
  391. }
  392. last unless $path =~ s{/+[^/]*$}{};
  393. last if length($path) <= length($destdir); # $destdir is assumed safe
  394. }
  395. }
  396. if ($path{'old'} eq '/dev/null' and $path{'new'} eq '/dev/null') {
  397. error(_g("original and modified files are /dev/null in diff `%s' (line %d)"),
  398. $diff, $.);
  399. } elsif ($path{'new'} eq '/dev/null') {
  400. error(_g("file removal without proper filename in diff `%s' (line %d)"),
  401. $diff, $. - 1) unless defined $fn{'old'};
  402. warning(_g("diff %s removes a non-existing file %s (line %d)"),
  403. $diff, $fn{'old'}, $.) unless -e $fn{'old'};
  404. }
  405. my $fn = intuit_file_patched($fn{'old'}, $fn{'new'});
  406. my $dirname = $fn;
  407. if ($dirname =~ s{/[^/]+$}{} && not -d $dirname) {
  408. $dirtocreate{$dirname} = 1;
  409. }
  410. if (-e $fn and not -f _) {
  411. error(_g("diff `%s' patches something which is not a plain file"), $diff);
  412. }
  413. if ($filepatched{$fn}) {
  414. error(_g("diff `%s' patches file %s twice"), $diff, $fn);
  415. }
  416. $filepatched{$fn} = 1;
  417. push @patchorder, $fn;
  418. # read hunks
  419. my $hunk = 0;
  420. while (defined($_ = getline($self))) {
  421. # read hunk header (@@)
  422. next if /^\\ No newline/;
  423. last unless (/^@@ -\d+(,(\d+))? \+\d+(,(\d+))? @\@( .*)?$/);
  424. my ($olines, $nlines) = ($1 ? $2 : 1, $3 ? $4 : 1);
  425. # read hunk
  426. while ($olines || $nlines) {
  427. unless (defined($_ = getline($self))) {
  428. if (($olines == $nlines) and ($olines < 3)) {
  429. warning(_g("unexpected end of diff `%s'"), $diff);
  430. last;
  431. } else {
  432. error(_g("unexpected end of diff `%s'"), $diff);
  433. }
  434. }
  435. next if /^\\ No newline/;
  436. # Check stats
  437. if (/^ / || /^$/) { --$olines; --$nlines; }
  438. elsif (/^-/) { --$olines; }
  439. elsif (/^\+/) { --$nlines; }
  440. else {
  441. error(_g("expected [ +-] at start of line %d of diff `%s'"),
  442. $., $diff);
  443. }
  444. }
  445. $hunk++;
  446. }
  447. unless($hunk) {
  448. error(_g("expected ^\@\@ at line %d of diff `%s'"), $., $diff);
  449. }
  450. }
  451. close($self);
  452. unless ($diff_count) {
  453. warning(_g("diff `%s' doesn't contain any patch"), $diff);
  454. }
  455. *$self->{'analysis'}{$destdir}{"dirtocreate"} = \%dirtocreate;
  456. *$self->{'analysis'}{$destdir}{"filepatched"} = \%filepatched;
  457. *$self->{'analysis'}{$destdir}{"patchorder"} = \@patchorder;
  458. return *$self->{'analysis'}{$destdir};
  459. }
  460. sub prepare_apply {
  461. my ($self, $analysis, %opts) = @_;
  462. if ($opts{"create_dirs"}) {
  463. foreach my $dir (keys %{$analysis->{'dirtocreate'}}) {
  464. eval { mkpath($dir, 0, 0777); };
  465. syserr(_g("cannot create directory %s"), $dir) if $@;
  466. }
  467. }
  468. }
  469. sub apply {
  470. my ($self, $destdir, %opts) = @_;
  471. # Set default values to options
  472. $opts{"force_timestamp"} = 1 unless exists $opts{"force_timestamp"};
  473. $opts{"remove_backup"} = 1 unless exists $opts{"remove_backup"};
  474. $opts{"create_dirs"} = 1 unless exists $opts{"create_dirs"};
  475. $opts{"options"} ||= [ '-t', '-F', '0', '-N', '-p1', '-u',
  476. '-V', 'never', '-g0', '-b', '-z', '.dpkg-orig'];
  477. $opts{"add_options"} ||= [];
  478. push @{$opts{"options"}}, @{$opts{"add_options"}};
  479. # Check the diff and create missing directories
  480. my $analysis = $self->analyze($destdir, %opts);
  481. $self->prepare_apply($analysis, %opts);
  482. # Apply the patch
  483. $self->ensure_open("r");
  484. my ($stdout, $stderr) = ('', '');
  485. spawn(
  486. 'exec' => [ 'patch', @{$opts{"options"}} ],
  487. 'chdir' => $destdir,
  488. 'env' => { LC_ALL => 'C', LANG => 'C' },
  489. 'delete_env' => [ 'POSIXLY_CORRECT' ], # ensure expected patch behaviour
  490. 'wait_child' => 1,
  491. 'nocheck' => 1,
  492. 'from_handle' => $self->get_filehandle(),
  493. 'to_string' => \$stdout,
  494. 'error_to_string' => \$stderr,
  495. );
  496. if ($?) {
  497. print STDOUT $stdout;
  498. print STDERR $stderr;
  499. subprocerr("LC_ALL=C patch " . join(" ", @{$opts{"options"}}) .
  500. " < " . $self->get_filename());
  501. }
  502. $self->close();
  503. # Reset the timestamp of all the patched files
  504. # and remove .dpkg-orig files
  505. my $now = $opts{"timestamp"} || time;
  506. foreach my $fn (keys %{$analysis->{'filepatched'}}) {
  507. if ($opts{"force_timestamp"}) {
  508. utime($now, $now, $fn) || $! == ENOENT ||
  509. syserr(_g("cannot change timestamp for %s"), $fn);
  510. }
  511. if ($opts{"remove_backup"}) {
  512. $fn .= ".dpkg-orig";
  513. unlink($fn) || syserr(_g("remove patch backup file %s"), $fn);
  514. }
  515. }
  516. return $analysis;
  517. }
  518. # Verify if check will work...
  519. sub check_apply {
  520. my ($self, $destdir, %opts) = @_;
  521. # Set default values to options
  522. $opts{"create_dirs"} = 1 unless exists $opts{"create_dirs"};
  523. $opts{"options"} ||= [ '--dry-run', '-s', '-t', '-F', '0', '-N', '-p1', '-u',
  524. '-V', 'never', '-g0', '-b', '-z', '.dpkg-orig'];
  525. $opts{"add_options"} ||= [];
  526. push @{$opts{"options"}}, @{$opts{"add_options"}};
  527. # Check the diff and create missing directories
  528. my $analysis = $self->analyze($destdir, %opts);
  529. $self->prepare_apply($analysis, %opts);
  530. # Apply the patch
  531. $self->ensure_open("r");
  532. my $error;
  533. my $patch_pid = spawn(
  534. 'exec' => [ 'patch', @{$opts{"options"}} ],
  535. 'chdir' => $destdir,
  536. 'env' => { LC_ALL => 'C', LANG => 'C' },
  537. 'delete_env' => [ 'POSIXLY_CORRECT' ], # ensure expected patch behaviour
  538. 'from_handle' => $self->get_filehandle(),
  539. 'to_file' => '/dev/null',
  540. 'error_to_file' => '/dev/null',
  541. );
  542. wait_child($patch_pid, nocheck => 1);
  543. my $exit = WEXITSTATUS($?);
  544. subprocerr("patch --dry-run") unless WIFEXITED($?);
  545. $self->close();
  546. return ($exit == 0);
  547. }
  548. # Helper functions
  549. sub get_type {
  550. my $file = shift;
  551. if (not lstat($file)) {
  552. return _g("nonexistent") if $! == ENOENT;
  553. syserr(_g("cannot stat %s"), $file);
  554. } else {
  555. -f _ && return _g("plain file");
  556. -d _ && return _g("directory");
  557. -l _ && return sprintf(_g("symlink to %s"), readlink($file));
  558. -b _ && return _g("block device");
  559. -c _ && return _g("character device");
  560. -p _ && return _g("named pipe");
  561. -S _ && return _g("named socket");
  562. }
  563. }
  564. 1;
  565. # vim: set et sw=4 ts=8