diff options
Diffstat (limited to 'sboconfig')
-rwxr-xr-x | sboconfig | 38 |
1 files changed, 21 insertions, 17 deletions
@@ -19,7 +19,7 @@ use File::Copy; use File::Path qw(make_path); use File::Temp qw(tempfile);; -my %config = %SBO::Lib::config; +#my %config = %SBO::Lib::config; my $self = basename ($0); sub show_usage () { @@ -80,11 +80,11 @@ if (exists $changes{JOBS}) { ($changes{JOBS} =~ /^\d+$/ || $changes{JOBS} eq 'FALSE'); } -my $conf_dir = $SBO::Lib::conf_dir; -my $conf_file = $SBO::Lib::conf_file; +#my $conf_dir = $SBO::Lib::conf_dir; +#my $conf_file = $SBO::Lib::conf_file; -# safely modify our conf file; copy to a temp location, edit the temp file, -# move the edited file into place +# safely modify our conf file; write its contents to a temp file, modify the +# temp file, write the contents of the temp file back to the conf file sub config_write ($$) { exists $_[1] or script_error 'config_write requires two arguments.'; my ($key, $val) = @_; @@ -92,12 +92,14 @@ sub config_write ($$) { mkdir $conf_dir or die "Unable to create $conf_dir. Exiting.\n"; } if (-f $conf_file) { - my $tempfh = tempfile (DIR => $SBO::Lib::tempdir); - my $tempfn = get_tmp_perlfn $tempfh; - copy ($conf_file, $tempfn); +# my $tempfh = tempfile (DIR => $SBO::Lib::tempdir); + my $tempfh = tempfile (DIR => $tempdir); + my $conffh = open_read $conf_file; + my $conftents = do {local $/; <$conffh>}; + print {$tempfh} $conftents; # tie the file so that if $key is already there, we just change that # line and untie it - tie my @temp, 'Tie::File', $tempfn; + tie my @temp, 'Tie::File', $tempfh; my $has; my $regex = qr/\A\Q$key\E=/; FIRST: for my $tmpline (@temp) { @@ -105,16 +107,18 @@ sub config_write ($$) { } untie @temp; # otherwise, append our new $key=$value pair - unless ($has) { - my $fh = open_fh ($tempfn, '>>'); - print {$fh} "$key=$val\n"; - close $fh; - } - move ($tempfn, $conf_file) || return; + print {$tempfh} "$key=$val\n" unless $has; + seek $tempfh, 0, 0; + my $contents = do {local $/; <$tempfh>}; + close $conffh; + eval { $conffh = open_fh $conf_file, '>>' }; + warn "Cannot write configuration: $@\n", return if $@; + print {$conffh} $contents or return; + close $conffh, close $tempfh; } else { # no config file, easiest case of all. - my $fh = open_fh $conf_file, '>' || return; - say {$fh} "$key=$val"; + my $fh = open_fh $conf_file, '>' or return; + print {$fh} "$key=$val\n"; close $fh; } return 1; |