While the current way to detect settings like { 'keep-last' => 0 } is
concise, it's also wrong, because but the delete operation is visible
to the caller. This resulted in e.g.
# $hash is { 'keep-all' => 1 }
my $s = print_property_string($hash, 'prune-backups');
# $hash is now {}, $s is 'keep-all=1'
because validation is called in print_property_string. The same issue
is present when calling prune_mark_backup_group.
Because validation complains when keep-all and something else is set,
this shouldn't have caused any real issues, besides vzdump with
keep-all=1 wrongly taking the removal path, but without any settings,
so not removing anything:
INFO: prune older backups with retention:
INFO: pruned 0 backup(s)
Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
sub prune_mark_backup_group {
my ($backup_group, $keep) = @_;
- my $keep_all = delete $keep->{'keep-all'};
+ my @positive_opts = grep { $_ ne 'keep-all' && $keep->{$_} > 0 } keys $keep->%*;
- if ($keep_all || !scalar(grep {$_ > 0} values %{$keep})) {
- $keep = { 'keep-all' => 1 } if $keep_all;
+ if ($keep->{'keep-all'} || scalar(@positive_opts) == 0) {
foreach my $prune_entry (@{$backup_group}) {
$prune_entry->{mark} = 'keep';
}
sub validate_prune_backups {
my ($prune_backups) = @_;
- my $keep_all = delete $prune_backups->{'keep-all'};
+ my @positive_opts =
+ grep { $_ ne 'keep-all' && $prune_backups->{$_} > 0 } keys $prune_backups->%*;
- if (!scalar(grep {$_ > 0} values %{$prune_backups})) {
+ if (scalar(@positive_opts) == 0) {
$prune_backups = { 'keep-all' => 1 };
- } elsif ($keep_all) {
+ } elsif ($prune_backups->{'keep-all'}) {
die "keep-all cannot be set together with other options.\n";
}