Skip to content

Commit

Permalink
Fixes to fixes; better error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
markfeit committed Oct 24, 2017
1 parent cd4c572 commit d31149b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 20 deletions.
49 changes: 34 additions & 15 deletions drop-in
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,17 @@ undef $/; # Read things in one fell swoop. Dangerous for large files.

# Read the original text

if ( $inoutfile =~ /^-$/ )
my $standard_io = $inoutfile eq '-';

if ( $standard_io )
{
open( INPUT, "<&STDIN" )
|| barf "Can't dupe standard input: $!\n";
or barf "Can't dupe standard input: $!\n";
}
else
{
open( INPUT, $inoutfile )
|| barf "Can't open $inoutfile: $!\n";
or barf "Can't open $inoutfile: $!\n";
}

$_ = <INPUT>;
Expand Down Expand Up @@ -131,7 +133,7 @@ if ( $opt_find ) {

# Read the additional text
open ADD, $addfile
|| barf "Can't open $addfile: $!\n";
or barf "Can't open $addfile: $!\n";
my($add) = <ADD>;
close ADD;

Expand All @@ -151,6 +153,9 @@ my $rtext = ( $opt_remove ? ""
if ( /\n?${opt_comment}BEGIN-$key\n.*\n${opt_comment}END-$key\n/s )
{
$rtext =~ s/\n*$//;
if ( $rtext ne "" ) {
$rtext .= "\n";
}
debug "Replacing/Removing '''$rtext'''";
s/(${opt_comment}BEGIN-$key\n).*(${opt_comment}END-$key\n)/$rtext/s;
}
Expand Down Expand Up @@ -184,34 +189,48 @@ debug "New Text: '''$_'''";
# Dump the results to the output file

my $output;
my @infile_stat;

if ( $inoutfile =~ /^-$/ )
if ( $standard_io )
{
open( $output, ">&STDOUT" )
|| barf "Can't dupe standard output: $!\n";
or barf "Can't dupe standard output: $!\n";
}
else
{
unlink $inoutfile;
@infile_stat = stat($inoutfile);
@infile_stat or barf "Can't stat $inoutfile: $!\n";

$output = new File::Temp(
UNLINK => 1,
TEMPLATE => "$inoutfile.XXXXXX"
UNLINK => 0,
TEMPLATE => "$inoutfile.XXXXXX",
);
$output || barf "Can't open temporary file: $!\n";
$output or barf "Can't open temporary file: $!\n";
debug "Output is to temporary file $output";
}

debug "Output is to $output";
print $output $_;

if ( $inoutfile !~ /^-$/ )
if ( ! $standard_io )
{
debug "Moving $output -> $inoutfile";
move($output, $inoutfile);
my $output_path = $output->filename;
$output->close();

chmod $infile_stat[2], $output_path
or barf "Unable to set mode of $output_path: $!\n";
chown $infile_stat[4], $infile_stat[5], $output_path
or barf "Unable to set ownership of $output_path: $!\n";

debug "Moving $output_path -> $inoutfile";
move($output_path, $inoutfile)
or barf "Failed to move $output_path to $inoutfile: $!\n";

debug "Output file $inoutfile written:";
system("ls -alh $inoutfile") if $opt_debug;
system("cat $inoutfile") if $opt_debug;
}

$output->close();


# Buh-bye...

Expand Down
18 changes: 13 additions & 5 deletions test
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/sh -e

FILE=/tmp/file

Expand All @@ -12,10 +12,6 @@ one
two
EOF

echo INITIAL:
cat "${FILE}"
echo

echo DROP-IN STANDARD OUTPUT:
./drop-in ${DEBUG} -n stuff /etc/issue - <<EOF
Line 1
Expand All @@ -24,6 +20,10 @@ Line 3
EOF
echo

echo INITIAL FILE:
cat "${FILE}"
echo

echo DROP-IN TOP:
./drop-in ${DEBUG} -n -t stuff - "${FILE}" <<EOF
This was dropped in
Expand All @@ -43,8 +43,16 @@ EOF
cat "${FILE}"
echo

echo REPLACE BOTTOM:
./drop-in ${DEBUG} -n stuff - "${FILE}" <<EOF
This is a replacement
EOF
cat "${FILE}"
echo

echo REMOVE BOTTOM:
./drop-in ${DEBUG} -r stuff /dev/null "${FILE}"
cat "${FILE}"


rm -f "${FILE}"

0 comments on commit d31149b

Please sign in to comment.