Debian Codegraph @

script/applypatch

raw
1 #!/usr/bin/perl -w
2 # applypatch -- apply a 'makepatch' generated patch kit.
3 # Author : Johan Vromans
4 # Created On : Sat Nov 14 14:34:28 1998
5 # Last Modified By: Johan Vromans
6 # Last Modified On: Sun Oct 8 15:07:03 2006
7 # Update Count : 147
8 # Status : Released
9 #
10 my $RCS_Id = '$Id: applypatch.pl,v 1.20 2006/10/08 13:07:07 jv Exp $ ';
11
12 use strict;
13 use Getopt::Long 2.00;
14 use File::Basename;
15 use File::Spec;
16 use IO::File;
17 use Text::ParseWords;
18
19 ################ Common stuff ################
20
21 my $my_package = 'Sciurix';
22 my $my_name = "applypatch";
23 my $my_version = "2.03";
24 my $data_version = '1.0';
25
26 $my_version .= '*' if length('$Locker: $ ') > 12;
27
28 ################ Globals ################
29
30 ## Options and defaults.
31
32 my $dir; # source directory
33 my $check = 0; # check only
34 my $retain = 0; # retain .orig files
35 my $patch = 'patch -p0 -N'; # patch command
36 my $verbose = 0; # verbose processing
37 my $force = 0; # allow continuation after trunc/corruption
38
39 # Development options (not shown with -help).
40 my $trace = 0; # trace (show process)
41 my $test = 0; # test (no actual processing)
42 my $debug = 0; # extensive debugging info
43
44 ## Misc
45
46 my $applypatch = 0; # it's for us
47 my $timestamp; # create date/time of patch kit
48 my @workq = (); # work queue
49
50 ## Subroutine prototypes
51
52 sub app_options ();
53 sub app_usage ($);
54 sub copy_input ();
55 sub execute_patch ();
56 sub post_patch ();
57 sub pre_patch ();
58 sub verify_files ();
59
60 ################ Program parameters ################
61
62 app_options();
63 $trace ||= $debug;
64 $verbose ||= $trace;
65
66 ################ Presets ################
67
68 $patch .= " -s" unless $verbose;
69 my $tmpfile = IO::File->new_tmpfile;
70
71 ################ The Process ################
72
73 # Validate input and copy to temp file.
74 copy_input ();
75
76 # Change dir if requested.
77 (defined $dir) && (chdir ($dir) || die ("Cannot change to $dir: $!\n"));
78
79 # Verify that we are in the right place.
80 verify_files ();
81
82 # Exit if just checking.
83 die ("Okay\n") if $test && $check;
84 exit (0) if $check;
85
86 # Pre patch: create directories and files.
87 pre_patch ();
88
89 # Run the patch program.
90 execute_patch ();
91
92 # Post patch: adjust timestamps, remove obsolete files and directories.
93 post_patch ();
94
95 die ("Okay\n") if $test;
96 exit (0);
97
98 ################ Subroutines ################
99
100 sub copy_input () {
101
102 my $lines = 0; # checksum: #lines
103 my $bytes = 0; # checksum: #bytes
104 my $sum = 0; # checksum: system V sum
105 my $all_lines = 0; # overall checksum: #lines
106 my $all_bytes = 0; # overall checksum: #bytes
107 my $all_sum = 0; # overall checksum: system V sum
108 my $patchdata = 0; # saw patch data
109 my $pos = 0; # start of patch data
110 my $endkit = 0; # saw end of kit
111 my $fail = 0; # failed
112 my $patch_checksum_okay = 0;# checksum for the patch was okay
113
114 print STDERR ("Validate input.\n") if $verbose;
115
116 @ARGV = "-" if !@ARGV;
117 for my $file (@ARGV) {
118 my $argv = new IO::File;
119 open($argv, $file) or die "Can't open $file: $!";
120 binmode($argv);
121 while ( <$argv> ) {
122 chomp;
123 if ( /^#### Patch data follows ####/ ) {
124 print STDERR (": $_\n") if $trace;
125 $patchdata |= 1; # bit 0 means: start seen
126 $pos = $tmpfile->getpos;
127 $lines = $bytes = $sum = 0;
128 }
129 elsif ( /^#### End of Patch data ####/ ) {
130 print STDERR (": $_\n") if $trace;
131 $patchdata |= 2; # bit 1 means: end seen
132 }
133 elsif ( /^#### ApplyPatch data follows ####/ ) {
134 print STDERR (": $_\n") if $trace;
135 $applypatch |= 1;
136 }
137 elsif ( /^#### End of ApplyPatch data ####/ ) {
138 print STDERR (": $_\n") if $trace;
139 $applypatch |= 2;
140 }
141 elsif ( /^#### End of Patch kit (\[created: ([^\]]+)\] )?####/ ) {
142 print STDERR (": $_\n") if $trace;
143 $endkit = 1;
144 if ( defined $timestamp && defined $2 && $2 ne $timestamp ) {
145 warn ("Timestamp mismatch ",
146 "in \"#### End of Patch kit\" line.\n",
147 " expecting \"$timestamp\", got \"$2\".\n");
148 $fail = 1;
149 }
150 }
151 elsif ( /^#### Patch checksum: (\d+) (\d+) (\d+) ####/ ) {
152 # Checksum for patch data only.
153 # This _MUST_ preceed the overall checksum.
154 print STDERR (": $_\n") if $trace;
155 $patch_checksum_okay = 1;
156 if ( $1 != $lines ) {
157 warn ("Linecount error: expecting $1, got $lines.\n");
158 $fail = 1;
159 $patch_checksum_okay = 0;
160 }
161 if ( $2 != $bytes ) {
162 warn ("Bytecount error: expecting $2, got $bytes.\n");
163 $fail = 1;
164 $patch_checksum_okay = 0;
165 }
166 if ( $3 != $sum ) {
167 warn ("Checksum error: expecting $3, got $sum.\n");
168 $fail = 1;
169 $patch_checksum_okay = 0;
170 }
171 }
172 elsif ( /^#### Checksum: (\d+) (\d+) (\d+) ####/ ) {
173 print STDERR (": $_\n") if $trace;
174 if ( $patch_checksum_okay ) {
175 warn ("Warning: Overall linecount mismatch: ".
176 "expecting $1, got $all_lines.\n")
177 unless $1 == $all_lines || !$verbose;
178 warn ("Warning: Overall bytecount mismatch: ".
179 "expecting $2, got $all_bytes.\n")
180 unless $2 == $all_bytes || !$verbose;
181 warn ("Warning: Overall checksum mismatch: ".
182 "expecting $3, got $all_sum.\n")
183 unless $3 == $all_sum || !$verbose;
184 }
185 else {
186 if ( $1 != $all_lines ) {
187 warn ("Overall linecount error: ".
188 "expecting $1, got $all_lines.\n");
189 $fail = 1;
190 }
191 if ( $2 != $all_bytes ) {
192 warn ("Overall bytecount error: ".
193 "expecting $2, got $all_bytes.\n");
194 $fail = 1;
195 }
196 if ( $3 != $all_sum ) {
197 warn ("Overall checksum error: ".
198 "expecting $3, got $all_sum.\n");
199 $fail = 1;
200 }
201 }
202 }
203 elsif ( $applypatch == 1 ) {
204 if ( /^# Data version\s*:\s*(\d+\.\d+)$/ ) {
205 print STDERR (": $_\n") if $trace;
206 if ( $1 > $data_version ) {
207 warn ("This program is not capable of handling ",
208 "this input data.\n",
209 "Please upgrade to a newer version.\n");
210 $fail = 1;
211 }
212 }
213 elsif ( /^# Date generated\s*:\s+(.*)$/ ) {
214 $timestamp = $1;
215 }
216 elsif ( /^# (\S) (.*)$/ ) {
217 push (@workq, [ $1, shellwords ($2) ]);
218 }
219 }
220 }
221 continue {
222 # Calculate checksum.
223 $lines++;
224 $all_lines++;
225 $_ .= "\n";
226 $bytes += length ($_);
227 $all_bytes += length ($_);
228 # System V 'sum' checksum
229 $sum = ($sum + unpack ("%16C*", $_)) % 65535;
230 $all_sum = ($all_sum + unpack ("%16C*", $_)) % 65535;
231
232 # Copy the line to the temp file.
233 print $tmpfile ($_);
234 }
235 close($argv);
236 }
237
238 # If we saw an ApplyPatch data section, it must be reliable.
239 if ( $applypatch == 1 ) {
240 warn ("ApplyPatch data section not properly terminated.\n");
241 $fail = 1;
242 }
243 elsif ( $applypatch == 2 ) {
244 warn ("ApplyPatch data section not reliable.\n");
245 $fail = 1;
246 }
247
248 if ( $applypatch ) {
249 # If we saw a Patch data section, it must be reliable.
250 if ( $patchdata == 0 ) {
251 warn ("Patch data section not delimited.\n");
252 $fail = 1;
253 }
254 elsif ( $patchdata == 1 ) {
255 warn ("Patch data section not properly terminated.\n");
256 $fail = 1;
257 }
258 elsif ( $patchdata == 2 ) {
259 warn ("Patch data section not reliable.\n");
260 $fail = 1;
261 }
262
263 if ($endkit == 0 ) {
264 warn ("Missing \"#### End of Patch kit\" line.\n");
265 $fail = 1;
266 }
267 }
268
269 if ( $fail ) {
270 if ( $force ) {
271 warn ("WARNING: Verification of patch kit failed, ",
272 "continuing anyway.\n");
273 }
274 else {
275 die ("Verification of patch kit failed, aborting.\n",
276 "Use \"--force\" to override this.\n");
277 }
278 }
279
280 print STDERR ($applypatch == 3 ? "Apply" : "",
281 "Patch kit apparently okay.\n") if $verbose;
282
283 # Reset file to start of patch data.
284 $tmpfile->setpos ($pos);
285 }
286
287 sub verify_files () {
288
289 my $fail = 0;
290
291 print STDERR ("Verify source directory.\n") if $verbose;
292
293 foreach ( @workq ) {
294 my ($op, $fn, @args) = @$_;
295
296 if ( $op eq 'c' ) {
297 if ( -f $fn || -d _ ) {
298 warn ("Verify error: file $fn must be created, ",
299 "but already exists.\n");
300 $fail = 1;
301 }
302 }
303 elsif ( $op eq 'C' ) {
304 if ( -f $fn || -d _ ) {
305 warn ("Verify error: directory $fn must be created, ",
306 "but already exists.\n");
307 $fail = 1;
308 }
309 }
310 elsif ( $op eq 'r' || $op eq 'p' || $op eq 'v' ) {
311 my $sz = -s $fn;
312 if ( defined $sz ) {
313 if ( $sz != $args[0] ) {
314 warn ("Verify error: size of $fn should be $args[0], but is ",
315 "$sz.\n");
316 $fail = 1;
317 }
318 }
319 else {
320 warn ("Verify error: file $fn is missing.\n");
321 $fail = 1;
322 }
323 }
324 elsif ( $op eq 'R' ) {
325 unless ( -d $fn ) {
326 warn ("Verify error: directory $fn must be removed, ",
327 "but does not exist.\n");
328 $fail = 1;
329 }
330 }
331 }
332
333 if ( $fail ) {
334 if ( $force ) {
335 warn ("WARNING: This does not look like expected source ",
336 "directory, continuing anyway.\n");
337 }
338 else {
339 warn ("Apparently this is not the expected source directory, ",
340 "aborting.\n");
341 die ("Use \"--force\" to override this.\n");
342 }
343 }
344
345 print STDERR ("Source directory apparently okay.\n") if $verbose;
346 }
347
348 sub pre_patch () {
349
350 foreach ( @workq ) {
351 my ($op, $fn, $size, $mtime, $mode) = @$_;
352
353 if ( $op eq 'C' ) {
354 $mode = oct($mode) & 0777;
355 $mode = 0777 unless $mode; # sanity
356 printf STDERR ("+ mkpath $fn 0%o\n", $mode) if $trace;
357 mkdir ($fn, $mode)
358 || die ("Cannot create directory $fn: $!\n");
359 }
360 }
361
362 foreach ( @workq ) {
363 my ($op, $fn, $size, $mtime, $mode) = @$_;
364
365 if ( $op eq 'c' ) {
366 #$mode = oct($mode) & 0777;
367 #$mode = 0666 unless $mode; # sanity
368 print STDERR ("+ create $fn\n") if $trace;
369 open (F, '>'.$fn)
370 || die ("Cannot create $fn: $!\n");
371 close (F);
372 #printf STDERR ("+ chmod 0%o $fn\n", $mode) if $trace;
373 #chmod ($mode, $fn)
374 # || warn sprintf ("WARNING: Cannot chmod 0%o $fn: $!\n", $mode);
375 }
376 }
377
378 }
379
380 sub execute_patch () {
381 my $p = new IO::File;
382 print STDERR ("+ $patch\n") if $trace;
383 $p->open("|$patch") || die ("Cannot open pipe to \"$patch\": $!\n");
384 binmode($p);
385 if ( $applypatch ) {
386 my $lines = 0;
387 while ( <$tmpfile> ) {
388 chomp;
389 $lines++;
390 print STDERR ("++ ", $_, "\n") if $debug;
391 print $p ($_, "\n");
392 last if $_ eq "#### End of Patch data ####";
393 }
394 print STDERR ("+ $lines lines sent to \"$patch\"\n") if $trace;
395 }
396 else {
397 print $p ($_) while <$tmpfile>;
398 }
399 $p->close || die ("Possible problems with \"$patch\", status = $?.\n");
400 }
401
402 sub set_utime ($$;$) {
403 my ($fn, $mtime, $mode) = @_;
404 $mode = (stat ($fn))[2] unless defined $mode;
405 chmod (0777, $fn)
406 || warn ("WARNING: Cannot utime/chmod a+rwx $fn: $!\n");
407 print STDERR ("+ utime $fn $mtime (".localtime($mtime).")\n") if $trace;
408 # Set times. Ignore errors for directories since some systems
409 # (like MSWin32) do not allow directories to be stamped.
410 utime ($mtime, $mtime, $fn)
411 || -d $fn || warn ("WARNING: utime($mtime,$fn): $!\n");
412 printf STDERR ("+ chmod 0%o $fn\n", $mode) if $trace;
413 chmod ($mode, $fn)
414 || warn sprintf ("WARNING: Cannot utime/chmod 0%o $fn: $!\n", $mode);
415 }
416
417 sub do_unlink ($) {
418 my ($fn) = @_;
419 my $mode = (stat($fn))[2];
420 chmod (0777, $fn)
421 || warn ("WARNING: Cannot unlink/chmod a+rwx $fn: $!\n");
422 print STDERR ("+ unlink $fn\n") if $verbose;
423 return if unlink ($fn);
424 warn ("WARNING: Cannot remove $fn: $!\n");
425 chmod ($mode, $fn)
426 || warn sprintf ("WARNING: Cannot unlink/chmod 0%o $fn: $!\n", $mode);
427 }
428
429 sub do_rmdir ($) {
430 my ($fn) = @_;
431 my $mode = (stat($fn))[2];
432 chmod (0777, $fn)
433 || warn ("WARNING: Cannot rmdir/chmod a+rwx $fn: $!\n");
434 print STDERR ("+ rmdir $fn\n") if $verbose;
435 return if rmdir ($fn);
436 warn ("WARNING: Cannot rmdir $fn: $!\n");
437 chmod ($mode, $fn)
438 || warn sprintf ("WARNING: Cannot rmdir/chmod 0%o $fn: $!\n", $mode);
439 }
440
441 sub post_patch () {
442
443 my $suffix = $ENV{SIMPLE_BACKUP_SUFFIX} || ".orig";
444
445 foreach ( @workq ) {
446 my ($op, $fn, $size, $mtime, $mode) = @$_;
447
448 if ( $op eq 'c' || $op eq 'C' || $op eq 'p' ) {
449 if ( defined $mode ) {
450 $mode = oct($mode) & 0777;
451 $mode = 0666 unless $mode; # sanity
452 }
453 set_utime ($fn, $mtime, $mode);
454 next if $retain;
455 $fn .= $suffix;
456 if ( -f $fn ) {
457 do_unlink ($fn);
458 }
459 }
460 elsif ( $op eq 'r' ) {
461 print STDERR ("+ unlink $fn\n") if $trace;
462 # Be forgiving, maybe patch already removed the file.
463 if ( -e $fn ) {
464 do_unlink ($fn);
465 }
466 else {
467 warn ("Apparently, $fn has been removed already.\n");
468 }
469 }
470 elsif ( $op eq 'R' ) {
471 print STDERR ("+ rmdir $fn\n") if $trace;
472 # Maybe some future version of patch will take care of directories.
473 if ( -e $fn ) {
474 do_rmdir ($fn);
475 }
476 else {
477 warn ("Apparently, $fn has been removed already.\n");
478 }
479 }
480 }
481
482 }
483
484 ################ Options and Help ################
485
486 sub app_options () {
487 my $help = 0; # handled locally
488
489 # Process options, if any.
490 # Make sure defaults are set before returning!
491 return unless @ARGV > 0;
492 my @opts = ('check' => \$check,
493 'dir|d=s' => \$dir,
494 'retain' => \$retain,
495 'force' => \$force,
496 'verbose' => \$verbose,
497 'quiet' => sub { $verbose = 0; },
498 'patch=s' => \$patch,
499 'test' => \$test,
500 'trace' => \$trace,
501 'debug' => \$debug,
502 'help' => \$help);
503
504 (!GetOptions (@opts) || $help) && app_usage (2);
505
506 }
507
508 sub app_usage ($) {
509 my ($exit) = @_;
510 print STDERR <<EndOfUsage;
511 Usage: $0 [options] patch-kit
512
513 -help this message
514 -dir XXX change to this directory before executing
515 -check check, but does not execute
516 -retain retain .orig file after patching
517 -force continue after verification failures
518 -patch XXX the patch command, default "$patch"
519 -quiet no information
520 -verbose verbose information
521 EndOfUsage
522 exit $exit if defined $exit && $exit != 0;
523 }
524
525 1;
526
527 __END__
528
529 ################ Documentation ################
530
531 =head1 NAME
532
533 applypatch - apply 'makepatch' generated script to update a source tree
534
535 =head1 SYNOPSIS
536
537 B<applypatch> [ I<options> ] I<patch-kit>
538
539 =head1 DESCRIPTION
540
541 B<Applypatch> applies a patch kit as generated by the B<makepatch>
542 program. It performs the following actions:
543
544 =over 4
545
546 =item *
547
548 First, it will extensively verify that the patch kit is complete and
549 did not get corrupted during transfer.
550
551 =item *
552
553 Then it will apply some heuristics to verify that the directory in
554 which the patch will be applied does indeed contain the expected
555 sources.
556
557 If a corruption or verification error is detected, B<applypatch> exits
558 without making changes.
559
560 =item *
561
562 If the kit is okay, and the directory seems to be the right one: it
563 creates new files and directories as necessary.
564
565 =item *
566
567 Then it runs the B<patch> program to apply the patch to the source files.
568
569 =item *
570
571 Upon completion, obsolete files, directories and .orig files are
572 removed, file modes of new files are set, and the timestamps of
573 all patched files are adjusted.
574
575 =back
576
577 =head1 Applypatch arguments
578
579 B<Applypatch> takes one argument, the name of the patch kit as
580 generated by B<makepatch>. If no name is specified, the patch kit is
581 read from standard input.
582
583 =head1 Applypatch options
584
585 Options are matched case insensitive, and may be abbreviated to uniqueness.
586
587 =over 4
588
589 =item B<-directory> I<dir>
590
591 The name of the source directory to be patched.
592
593 =item B<-check>
594
595 Perform the checks on the patch kit and patch directory, but do not
596 make any changes.
597
598 =item B<-force>
599
600 Force continuation of the patch process even when corruption or
601 verification errors are detected. This is very dangerous!
602
603 =item B<-retain>
604
605 Do not remove patch backup files (with extension C<.orig>) upon
606 completion.
607
608 =item B<-patch> I<cmd>
609
610 The patch command to be used. Default is "C<patch -p0 -N>".
611 Additionally, a "C<-s>" will be added unless option B<-verbose> was
612 specified.
613
614 =item B<-verbose>
615
616 This option will cause B<applypatch> and the B<patch> program to emit
617 verbose progress information.
618
619 =item B<-quiet>
620
621 This option will cause B<applypatch> and the B<patch> program to emit
622 no progress information, only error messages.
623
624 =back
625
626 =head1 Environment variables
627
628 =over 4
629
630 =item SIMPLE_BACKUP_SUFFIX
631
632 The suffix used by (some versions of?) B<patch> to back up the
633 originals of patched files. Upon completion, these files are removed
634 by B<applypatch> unless the option B<-retain> was specified.
635 Default value if "C<.orig>".
636
637 =back
638
639 =head1 SEE ALSO
640
641 B<makepatch>(1),
642 B<diff>(1),
643 B<patch>(1),
644 B<perl>(1),
645 B<rm>(1).
646
647 =head1 AUTHOR AND CREDITS
648
649 This program is written by Johan Vromans <[email protected]>.
650
651 See section AUTHOR AND CREDITS of the makepatch program for an
652 extensive list of people that helped one way or another in the
653 makepatch / applypatch project.
654
655 =head1 COPYRIGHT AND DISCLAIMER
656
657 This program is Copyright 1992,1999,2006 by Squirrel Consultancy. All
658 rights reserved.
659
660 This program is free software; you can redistribute it and/or modify
661 it under the terms of either: a) the GNU General Public License as
662 published by the Free Software Foundation; either version 1, or (at
663 your option) any later version, or b) the "Artistic License" which
664 comes with Perl.
665
666 This program is distributed in the hope that it will be useful, but
667 WITHOUT ANY WARRANTY; without even the implied warranty of
668 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the
669 GNU General Public License or the Artistic License for more details.
670
671 =cut