From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <f.ebner@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 90245695CF
 for <pve-devel@lists.proxmox.com>; Fri, 13 Nov 2020 14:09:04 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 8E2AC1AEAC
 for <pve-devel@lists.proxmox.com>; Fri, 13 Nov 2020 14:09:04 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [212.186.127.180])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id 626631AE96
 for <pve-devel@lists.proxmox.com>; Fri, 13 Nov 2020 14:09:03 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 2DB56424BE
 for <pve-devel@lists.proxmox.com>; Fri, 13 Nov 2020 14:09:03 +0100 (CET)
From: Fabian Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Fri, 13 Nov 2020 14:08:54 +0100
Message-Id: <20201113130855.18161-2-f.ebner@proxmox.com>
X-Mailer: git-send-email 2.20.1
In-Reply-To: <20201113130855.18161-1-f.ebner@proxmox.com>
References: <20201113130855.18161-1-f.ebner@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.025 Adjusted score from AWL reputation of From: address
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 RCVD_IN_DNSWL_MED        -2.3 Sender listed at https://www.dnswl.org/,
 medium trust
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [storage.pm]
Subject: [pve-devel] [PATCH storage 2/3] prune mark: keep all if all prune
 options are zero/missing
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Fri, 13 Nov 2020 13:09:04 -0000

as an additional safety measure. And add some tests.

Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
---

This also would've prevented the unwanted removals with the maxfiles
handling I had messed up, because there, prune_mark_backup_group was called
with an empty hash.

 PVE/Storage.pm             |  7 +++++++
 test/prune_backups_test.pm | 40 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)

diff --git a/PVE/Storage.pm b/PVE/Storage.pm
index 932f0f1..a2f400c 100755
--- a/PVE/Storage.pm
+++ b/PVE/Storage.pm
@@ -1622,6 +1622,13 @@ my $prune_mark = sub {
 sub prune_mark_backup_group {
     my ($backup_group, $keep) = @_;
 
+    if (!scalar(grep {$_ > 0} values %{$keep})) {
+	foreach my $prune_entry (@{$backup_group}) {
+	    $prune_entry->{mark} = 'keep';
+	}
+	return;
+    }
+
     my $prune_list = [ sort { $b->{ctime} <=> $a->{ctime} } @{$backup_group} ];
 
     $prune_mark->($prune_list, $keep->{'keep-last'}, sub {
diff --git a/test/prune_backups_test.pm b/test/prune_backups_test.pm
index 1e6a3d1..8bf564d 100644
--- a/test/prune_backups_test.pm
+++ b/test/prune_backups_test.pm
@@ -239,6 +239,18 @@ my $tests = [
 	},
 	expected => generate_expected(\@vmids, undef, ['keep', 'remove', 'keep', 'remove', 'keep', 'keep']),
     },
+    {
+	description => 'last=daily=weekly=1, others zero, multiple IDs',
+	keep => {
+	    'keep-hourly' => 0,
+	    'keep-last' => 1,
+	    'keep-daily' => 1,
+	    'keep-weekly' => 1,
+	    'keep-monthly' => 0,
+	    'keep-yearly' => 0,
+	},
+	expected => generate_expected(\@vmids, undef, ['keep', 'remove', 'keep', 'remove', 'keep', 'keep']),
+    },
     {
 	description => 'daily=2, one ID',
 	vmid => $vmids[0],
@@ -321,6 +333,34 @@ my $tests = [
 	    },
 	],
     },
+    {
+	description => 'all missing, multiple IDs',
+	keep => {},
+	expected => generate_expected(\@vmids, undef, ['keep', 'keep', 'keep', 'keep', 'keep', 'keep']),
+    },
+    {
+	description => 'all zero, multiple IDs',
+	keep => {
+	    'keep-last' => 0,
+	    'keep-hourly' => 0,
+	    'keep-daily' => 0,
+	    'keep-weekly' => 0,
+	    'keep-monthyl' => 0,
+	    'keep-yearly' => 0,
+	},
+	expected => generate_expected(\@vmids, undef, ['keep', 'keep', 'keep', 'keep', 'keep', 'keep']),
+    },
+    {
+	description => 'some zero, some missing, multiple IDs',
+	keep => {
+	    'keep-last' => 0,
+	    'keep-hourly' => 0,
+	    'keep-daily' => 0,
+	    'keep-monthyl' => 0,
+	    'keep-yearly' => 0,
+	},
+	expected => generate_expected(\@vmids, undef, ['keep', 'keep', 'keep', 'keep', 'keep', 'keep']),
+    },
 ];
 
 plan tests => scalar @$tests;
-- 
2.20.1