mirror of https://github.com/digint/btrbk
btrbk: correctly close config file after parsing
parent
79a66caed6
commit
bfda14358e
89
btrbk
89
btrbk
|
@ -619,42 +619,9 @@ sub check_file($$;$$)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
sub parse_config(@)
|
sub parse_config_line($$$$$)
|
||||||
{
|
{
|
||||||
my @config_files = @_;
|
my ($file, $root, $cur, $key, $value) = @_;
|
||||||
my $file = undef;
|
|
||||||
foreach(@config_files) {
|
|
||||||
TRACE "config: checking for file: $_";
|
|
||||||
if(-r "$_") {
|
|
||||||
$file = $_;
|
|
||||||
last;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
unless($file) {
|
|
||||||
ERROR "Configuration file not found: " . join(', ', @config_files);
|
|
||||||
return undef;
|
|
||||||
}
|
|
||||||
|
|
||||||
my $root = { CONTEXT => "root", SRC_FILE => $file };
|
|
||||||
my $cur = $root;
|
|
||||||
# set defaults
|
|
||||||
foreach (keys %config_options) {
|
|
||||||
next if $config_options{$_}->{deprecated}; # don't pollute hash with deprecated options
|
|
||||||
$root->{$_} = $config_options{$_}->{default};
|
|
||||||
}
|
|
||||||
|
|
||||||
INFO "Using configuration: $file";
|
|
||||||
open(FILE, '<', $file) or die $!;
|
|
||||||
while (<FILE>) {
|
|
||||||
chomp;
|
|
||||||
next if /^\s*#/; # ignore comments
|
|
||||||
next if /^\s*$/; # ignore empty lines
|
|
||||||
TRACE "config: parsing line $. with context=$cur->{CONTEXT}: \"$_\"";
|
|
||||||
if(/^(\s*)([a-zA-Z_]+)\s+(.*)$/)
|
|
||||||
{
|
|
||||||
my ($indent, $key, $value) = (length($1), lc($2), $3);
|
|
||||||
$value =~ s/\s*$//;
|
|
||||||
# NOTE: we do not perform checks on indentation!
|
|
||||||
|
|
||||||
if($key eq "volume")
|
if($key eq "volume")
|
||||||
{
|
{
|
||||||
|
@ -805,16 +772,64 @@ sub parse_config(@)
|
||||||
return undef;
|
return undef;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $cur;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
sub parse_config(@)
|
||||||
|
{
|
||||||
|
my @config_files = @_;
|
||||||
|
my $file = undef;
|
||||||
|
foreach(@config_files) {
|
||||||
|
TRACE "config: checking for file: $_";
|
||||||
|
if(-r "$_") {
|
||||||
|
$file = $_;
|
||||||
|
last;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unless($file) {
|
||||||
|
ERROR "Configuration file not found: " . join(', ', @config_files);
|
||||||
|
return undef;
|
||||||
|
}
|
||||||
|
|
||||||
|
my $root = { CONTEXT => "root", SRC_FILE => $file };
|
||||||
|
my $cur = $root;
|
||||||
|
# set defaults
|
||||||
|
foreach (keys %config_options) {
|
||||||
|
next if $config_options{$_}->{deprecated}; # don't pollute hash with deprecated options
|
||||||
|
$root->{$_} = $config_options{$_}->{default};
|
||||||
|
}
|
||||||
|
|
||||||
|
INFO "Using configuration: $file";
|
||||||
|
open(FILE, '<', $file) or die $!;
|
||||||
|
while (<FILE>) {
|
||||||
|
chomp;
|
||||||
|
next if /^\s*#/; # ignore comments
|
||||||
|
next if /^\s*$/; # ignore empty lines
|
||||||
|
TRACE "config: parsing line $. with context=$cur->{CONTEXT}: \"$_\"";
|
||||||
|
if(/^(\s*)([a-zA-Z_]+)\s+(.*)$/)
|
||||||
|
{
|
||||||
|
# NOTE: we do not perform checks on indentation!
|
||||||
|
my ($indent, $key, $value) = (length($1), lc($2), $3);
|
||||||
|
$value =~ s/\s*$//;
|
||||||
|
$cur = parse_config_line($file, $root, $cur, $key, $value);
|
||||||
|
unless(defined($cur)) {
|
||||||
|
# error, bail out
|
||||||
|
$root = undef;
|
||||||
|
last;
|
||||||
|
}
|
||||||
TRACE "line processed: new context=$cur->{CONTEXT}";
|
TRACE "line processed: new context=$cur->{CONTEXT}";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ERROR "Parse error in \"$file\" line $.";
|
ERROR "Parse error in \"$file\" line $.";
|
||||||
return undef;
|
$root = undef;
|
||||||
|
last;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
close FILE || ERROR "Failed to close configuration file: $!";
|
||||||
|
|
||||||
TRACE(Data::Dumper->Dump([$root], ["config{$file}"]));
|
TRACE(Data::Dumper->Dump([$root], ["config{$file}"])) if($root);
|
||||||
return $root;
|
return $root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue