From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 5920869B44 for ; Wed, 28 Jul 2021 11:11:55 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 50CC522212 for ; Wed, 28 Jul 2021 11:11:25 +0200 (CEST) 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 id 1D1BF221DF for ; Wed, 28 Jul 2021 11:11:23 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id DBBAA42B9E; Wed, 28 Jul 2021 11:11:22 +0200 (CEST) From: Dietmar Maurer To: pbs-devel@lists.proxmox.com Date: Wed, 28 Jul 2021 11:11:15 +0200 Message-Id: <20210728091118.581161-2-dietmar@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210728091118.581161-1-dietmar@proxmox.com> References: <20210728091118.581161-1-dietmar@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.775 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% 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 Subject: [pbs-devel] [PATCH proxmox-backup v3 1/4] cleanup: factor out tape catalog path helpers X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 28 Jul 2021 09:11:55 -0000 --- src/tape/media_catalog.rs | 43 +++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/tape/media_catalog.rs b/src/tape/media_catalog.rs index 086c8a7d..62c6acb3 100644 --- a/src/tape/media_catalog.rs +++ b/src/tape/media_catalog.rs @@ -2,7 +2,7 @@ use std::convert::TryFrom; use std::fs::File; use std::io::{Write, Read, BufReader, Seek, SeekFrom}; use std::os::unix::io::AsRawFd; -use std::path::Path; +use std::path::{PathBuf, Path}; use std::collections::{HashSet, HashMap}; use anyhow::{bail, format_err, Error}; @@ -95,20 +95,29 @@ impl MediaCatalog { Ok(catalogs) } - /// Test if a catalog exists - pub fn exists(base_path: &Path, uuid: &Uuid) -> bool { + pub fn catalog_path(base_path: &Path, uuid: &Uuid) -> PathBuf { let mut path = base_path.to_owned(); path.push(uuid.to_string()); path.set_extension("log"); - path.exists() + path + } + + fn tmp_catalog_path(base_path: &Path, uuid: &Uuid) -> PathBuf { + let mut path = base_path.to_owned(); + path.push(uuid.to_string()); + path.set_extension("tmp"); + path + } + + /// Test if a catalog exists + pub fn exists(base_path: &Path, uuid: &Uuid) -> bool { + Self::catalog_path(base_path, uuid).exists() } /// Destroy the media catalog (remove all files) pub fn destroy(base_path: &Path, uuid: &Uuid) -> Result<(), Error> { - let mut path = base_path.to_owned(); - path.push(uuid.to_string()); - path.set_extension("log"); + let path = Self::catalog_path(base_path, uuid); match std::fs::remove_file(path) { Ok(()) => Ok(()), @@ -125,9 +134,7 @@ impl MediaCatalog { let uuid = &media_id.label.uuid; - let mut path = base_path.to_owned(); - path.push(uuid.to_string()); - path.set_extension("log"); + let path = Self::catalog_path(base_path, uuid); let file = match std::fs::OpenOptions::new().read(true).open(&path) { Ok(file) => file, @@ -198,9 +205,7 @@ impl MediaCatalog { let uuid = &media_id.label.uuid; - let mut path = base_path.to_owned(); - path.push(uuid.to_string()); - path.set_extension("log"); + let path = Self::catalog_path(base_path, uuid); let me = proxmox::try_block!({ @@ -251,9 +256,7 @@ impl MediaCatalog { Self::create_basedir(base_path)?; - let mut tmp_path = base_path.to_owned(); - tmp_path.push(uuid.to_string()); - tmp_path.set_extension("tmp"); + let tmp_path = Self::tmp_catalog_path(base_path, uuid); let file = std::fs::OpenOptions::new() .read(true) @@ -285,9 +288,7 @@ impl MediaCatalog { let uuid = &media_id.label.uuid; - let mut tmp_path = base_path.to_owned(); - tmp_path.push(uuid.to_string()); - tmp_path.set_extension("tmp"); + let tmp_path = Self::tmp_catalog_path(base_path, uuid); let me = proxmox::try_block!({ @@ -333,9 +334,7 @@ impl MediaCatalog { commit: bool, ) -> Result<(), Error> { - let mut tmp_path = base_path.to_owned(); - tmp_path.push(uuid.to_string()); - tmp_path.set_extension("tmp"); + let tmp_path = Self::tmp_catalog_path(base_path, uuid); if commit { let mut catalog_path = tmp_path.clone(); -- 2.30.2