From bc4996ca564462b5c19d36e439ecb1afbe1d0951 Mon Sep 17 00:00:00 2001 From: ianmdlvl Date: Tue, 1 Jan 2002 17:55:29 +0000 Subject: [PATCH] Actually write output files. --- scripts/named-conf | 53 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/scripts/named-conf b/scripts/named-conf index 24f9906..0cad802 100755 --- a/scripts/named-conf +++ b/scripts/named-conf @@ -3,6 +3,7 @@ use strict; use IO::File; use Data::Dumper; +use POSIX; use vars qw($quis $mode $doall @@ -72,12 +73,11 @@ usage: named-conf-regen [-rvq] -f|-y|-n|...\n". " -v --verbose extra verbose\n"; } -cfg_fail("config filename $etcfile should have been absolute path of a file") - unless $etcfile =~ m,^/, && $etcfile !~ m,/$,; +cfg_fail("config filename $etcfile should not be directory") + if $etcfile =~ m,/$,; use vars qw($default_dir); -$default_dir= $etcfile; -$default_dir =~ s,/[^/]+$,,; +$default_dir= $etcfile =~ m,^.*/, ? $& : './'; use vars qw($slave_dir $slave_prefix $slave_suffix); $slave_dir= 'slave'; @@ -104,6 +104,7 @@ read_config($etcfile); debug_dump('@zone_cfg_list %zone_cfg'); process_zones($mode ? @zone_cfg_list : @ARGV); debug_dump('%output_contents'); +output_files() if $install; exit 0; @@ -177,7 +178,7 @@ sub read_config ($) { sub qualify ($) { my ($i) = @_; - $i= "$default_dir/$i" unless $i =~ m,^/,; + $i= "$default_dir$i" unless $i =~ m,^/,; return $i; } @@ -514,6 +515,48 @@ END $$cfg{'file'}); } +sub output_files () { + my ($fn,$ofn,$mfn,$l,$dir, $maxmode,$h,@to_install); + + foreach $ofn (keys %output_contents) { + $fn= $ofn; $mfn= "output file $fn"; + for (;;) { + if (!lstat $fn) { + $! == &ENOENT or die "$quis: stat $mfn:\n $!\n"; + $maxmode= 0666; + last; + } elsif (-f _) { + $maxmode= (stat _)[2]; + last; + } elsif (-l _) { + defined($l= readlink $fn) + or die "$quis: readlink $mfn:\n $!\n"; + $dir= $fn =~ m,^.*/, ? $& : './'; + $fn= "$dir$l" unless $l =~ m,^/,; + $mfn= "output file $fn (symlink target of $ofn)"; + } else { + die "$quis: output file $mfn exists but is not a file". + " (or symlink to one)"; + } + } + unlink "$fn.new" or $! == &ENOENT or + die "$quis: cannot clear out old .new version of $mfn:\n $!"; + $h= new IO::File "$fn.new",'w',$maxmode + or die("$quis: create .new version of $mfn:\n $!"); + print $h + "# generated by $quis, do not edit\n", + $output_contents{$ofn} + or die "$quis: write data to .new version of $mfn:\n $!"; + $h->close + or die "$quis: close .new version of $mfn:\n $!"; + push @to_install, $fn,$mfn; + } + + while (($fn,$mfn, @to_install) = @to_install) { + rename "$fn.new",$fn + or die "$quis: install new version of $mfn:\n $!"; + } +} #-------------------- general utilities -- 2.30.2