From c1610c754da95923b1245a2786b3683a645ec05f Mon Sep 17 00:00:00 2001 From: Axel Burri Date: Sat, 24 Jul 2021 20:11:32 +0200 Subject: [PATCH] btrbk: cleanup: add config_subsection --- btrbk | 45 ++++++++++++++++++--------------------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/btrbk b/btrbk index 055a2c0..7ba5053 100755 --- a/btrbk +++ b/btrbk @@ -3364,6 +3364,14 @@ sub vinfo_snapshot_root($) } +sub config_subsection($$;$) +{ + my $config = shift || die; + my $context = shift || die; + die if grep($_->{CONTEXT} ne $context, @{$config->{SUBSECTION}}); + return @{$config->{SUBSECTION}}; +} + sub vinfo_subsection($$;$) { # if config: must have SUBSECTION key @@ -3371,32 +3379,20 @@ sub vinfo_subsection($$;$) my $config_or_vinfo = shift || die; my $context = shift || die; my $include_aborted = shift; - my $config_list; + my @config_list; my $vinfo_check; if(exists($config_or_vinfo->{SUBSECTION})) { - # config - $config_list = $config_or_vinfo->{SUBSECTION}; + @config_list = config_subsection($config_or_vinfo, $context); } else { - # vinfo - $config_list = $config_or_vinfo->{CONFIG}->{SUBSECTION}; + @config_list = config_subsection($config_or_vinfo->{CONFIG}, $context); die unless($config_or_vinfo->{CONFIG}->{VINFO} == $config_or_vinfo); # check back reference } - # for now be paranoid and check all contexts - my @ret; - foreach (@$config_list) { - die unless($_->{CONTEXT} eq $context); - next if((not $include_aborted) && $_->{ABORTED}); - die unless($_->{VINFO}); - die unless($_->{VINFO}->{CONFIG}); + return map { die unless($_->{VINFO} == $_->{VINFO}->{CONFIG}->{VINFO}); # check all back references - push @ret, $_->{VINFO}; - } - return @ret; - - # much simpler implementation, without checks - #return map { $_->{ABORTED} ? () : $_->{VINFO} } @$config_list; + ($include_aborted || !$_->{ABORTED}) ? $_->{VINFO} : () + } @config_list; } @@ -6264,15 +6260,12 @@ MAIN: # # expand subvolume globs (wildcards) # - foreach my $config_vol (@{$config->{SUBSECTION}}) { - die unless($config_vol->{CONTEXT} eq "volume"); - + foreach my $config_vol (config_subsection($config, "volume")) { # read-in subvolume list (and expand globs) only if needed next unless(grep defined($_->{GLOB_CONTEXT}), @{$config_vol->{SUBSECTION}}); my $sroot_glob; # read-in late my @vol_subsection_expanded; - foreach my $config_subvol (@{$config_vol->{SUBSECTION}}) { - die unless($config_subvol->{CONTEXT} eq "subvolume"); + foreach my $config_subvol (config_subsection($config_vol, "subvolume")) { if($config_subvol->{GLOB_CONTEXT}) { my $globs = $config_subvol->{rel_path}; @@ -6335,12 +6328,10 @@ MAIN: # # create vinfo nodes (no readin yet) # - foreach my $config_vol (@{$config->{SUBSECTION}}) { - die unless($config_vol->{CONTEXT} eq "volume"); + foreach my $config_vol (config_subsection($config, "volume")) { my $sroot = vinfo($config_vol->{url}, $config_vol); vinfo_assign_config($sroot); - foreach my $config_subvol (@{$config_vol->{SUBSECTION}}) { - die unless($config_subvol->{CONTEXT} eq "subvolume"); + foreach my $config_subvol (config_subsection($config_vol, "subvolume")) { my $svol = vinfo_child($sroot, $config_subvol->{rel_path}, $config_subvol); # TODO: add config option "snapshot_path", reuse snaproot with same URL my $snapshot_dir = config_key($svol, "snapshot_dir", prefix => '/') // "";