From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <m.sandoval@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 3C5C29BDFA
 for <pbs-devel@lists.proxmox.com>; Tue, 21 Nov 2023 14:51:03 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 1CC7FAB95
 for <pbs-devel@lists.proxmox.com>; Tue, 21 Nov 2023 14:50:33 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (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
 for <pbs-devel@lists.proxmox.com>; Tue, 21 Nov 2023 14:50:32 +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 56DED416C3
 for <pbs-devel@lists.proxmox.com>; Tue, 21 Nov 2023 14:50:32 +0100 (CET)
From: Maximiliano Sandoval R <m.sandoval@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Tue, 21 Nov 2023 14:50:31 +0100
Message-Id: <20231121135031.171323-1-m.sandoval@proxmox.com>
X-Mailer: git-send-email 2.39.2
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.002 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 T_SCC_BODY_TEXT_LINE    -0.01 -
Subject: [pbs-devel] [PATCH offline-mirror] mirror: Use PathBuf instead of
 strings for paths
X-BeenThere: pbs-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Backup Server development discussion
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Tue, 21 Nov 2023 13:51:03 -0000

Joining the strings might results in a double `//` in a path. This was
experienced in a ticket at our customer support in the following error:

    Error: unable to read
    "/var/lib/proxmox-offline-mirror/mirrors//.pool/sha256/<SOME_HASH>"
    - Input/output error (os error 5) after downloading ±60GB of data.

Suggested-by: Stefan Sterz <s.sterz@proxmox.com>
Signed-off-by: Maximiliano Sandoval R <m.sandoval@proxmox.com>
---

One could also store .base_dir and .id as PathBuf instead of Strings in
MirrorConfig but that would constitute an API break.

 src/mirror.rs | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)

diff --git a/src/mirror.rs b/src/mirror.rs
index 3766f23..e26457b 100644
--- a/src/mirror.rs
+++ b/src/mirror.rs
@@ -30,13 +30,13 @@ use proxmox_apt::{
 
 use crate::helpers;
 
-fn mirror_dir(config: &MirrorConfig) -> String {
-    format!("{}/{}", config.base_dir, config.id)
+fn mirror_dir(config: &MirrorConfig) -> PathBuf {
+    PathBuf::from(&config.base_dir).join(&config.id)
 }
 
 pub(crate) fn pool(config: &MirrorConfig) -> Result<Pool, Error> {
-    let pool_dir = format!("{}/.pool", config.base_dir);
-    Pool::open(Path::new(&mirror_dir(config)), Path::new(&pool_dir))
+    let pool_dir = PathBuf::from(&config.base_dir).join(".pool");
+    Pool::open(&mirror_dir(config), &pool_dir)
 }
 
 /// `MirrorConfig`, but some fields converted/parsed into usable types.
@@ -450,11 +450,11 @@ fn fetch_plain_file(
 
 /// Initialize a new mirror (by creating the corresponding pool).
 pub fn init(config: &MirrorConfig) -> Result<(), Error> {
-    let pool_dir = format!("{}/.pool", config.base_dir);
+    let pool_dir = PathBuf::from(&config.base_dir).join(".pool");
 
-    let dir = format!("{}/{}", config.base_dir, config.id);
+    let dir = PathBuf::from(&config.base_dir).join(&config.id);
 
-    Pool::create(Path::new(&dir), Path::new(&pool_dir))?;
+    Pool::create(&dir, &pool_dir)?;
     Ok(())
 }
 
@@ -472,13 +472,11 @@ pub fn list_snapshots(config: &MirrorConfig) -> Result<Vec<Snapshot>, Error> {
 
     let mut list: Vec<Snapshot> = vec![];
 
-    let dir = mirror_dir(config);
-
-    let path = Path::new(&dir);
+    let path = mirror_dir(config);
 
     proxmox_sys::fs::scandir(
         libc::AT_FDCWD,
-        path,
+        &path,
         &SNAPSHOT_REGEX,
         |_l2_fd, snapshot, file_type| {
             if file_type != nix::dir::Type::Directory {
-- 
2.39.2