diff options
Diffstat (limited to 'lib/parse-patch')
-rwxr-xr-x | lib/parse-patch | 46 |
1 files changed, 35 insertions, 11 deletions
diff --git a/lib/parse-patch b/lib/parse-patch index b4dded2..9d38e36 100755 --- a/lib/parse-patch +++ b/lib/parse-patch @@ -1,10 +1,17 @@ #!/usr/bin/perl -w +# This script is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation. +# +# See the COPYING and AUTHORS files for more details. + # Extract or update a section from a combined patch + documentation + # meta information file. use FileHandle; use Getopt::Long; +use File::Temp qw(tempfile); use strict; my $select; @@ -20,7 +27,9 @@ if (!GetOptions("s|select=s" => \$select, foreach my $arg (@ARGV) { my $fh; - if ($arg =~ /\.gz$/) { + if (! -e $arg) { + $fh = new FileHandle("/dev/null"); + } elsif ($arg =~ /\.gz$/) { $fh = new FileHandle("gzip -cd $arg |"); } elsif ($arg =~ /\.bz2$/) { $fh = new FileHandle("bzip2 -cd $arg |"); @@ -53,31 +62,43 @@ foreach my $arg (@ARGV) { } } } elsif (defined $update) { - my $fh2; + my ($fh2, $tempname) = tempfile("$arg.XXXXXX"); if ($arg =~ /\.gz$/) { - $fh2 = new FileHandle("| gzip -c > $arg.parse"); + $fh2->close(); + if (! -e $tempname) { + die "File $tempname disappeared!\n"; + } + $fh2 = new FileHandle("| gzip -c > $tempname"); } elsif ($arg =~ /\.bz2$/) { - $fh2 = new FileHandle("| bzip2 -c > $arg.parse"); - } else { - $fh2 = new FileHandle("$arg.parse", O_CREAT|O_WRONLY); + $fh2->close(); + if (! -e $tempname) { + die "File $tempname disappeared!\n"; + } + $fh2 = new FileHandle("| bzip2 -c > $tempname"); } unless ($fh2) { - die "$arg.parse: $!\n"; + die "$tempname: $!\n"; } # Copy things before updated section + my $last_was_newline=1; # start first section in first line while (<$fh>) { if (/^%(.*)/ && $1 eq $update) { last; } + $last_was_newline = ($_ eq "\n"); print $fh2 $_; } + print $fh2 "\n" + unless ($last_was_newline); + # Create/replace updated section print $fh2 "%$update\n"; while (<STDIN>) { print $fh2 $_; } print $fh2 "\n"; + # Skip obsolete section while (<$fh>) { if (/^%(.*)/) { @@ -93,11 +114,14 @@ foreach my $arg (@ARGV) { die "$arg.patch: $!\n"; } - unlink "$arg~"; - unless (rename $arg, "$arg~") { - die "Failed to rename $arg to $arg~: $!\n"; + if (-e $arg) { + unlink "$arg~"; + unless (rename $arg, "$arg~") { + die "Failed to rename $arg to $arg~: $!\n"; + } } - unless (rename "$arg.parse", $arg) { + unless (rename $tempname, $arg) { + rename("$arg~", $arg); die "Failed to rename $arg.parse to $arg: $!\n"; } } |