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 8B35ECC08 for ; Wed, 16 Aug 2023 11:58:34 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 6E3C513E07 for ; Wed, 16 Aug 2023 11:58:04 +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 for ; Wed, 16 Aug 2023 11:58:03 +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 B35D640A76 for ; Wed, 16 Aug 2023 11:58:03 +0200 (CEST) From: Christian Ebner To: pbs-devel@lists.proxmox.com Date: Wed, 16 Aug 2023 11:57:45 +0200 Message-Id: <20230816095746.153036-2-c.ebner@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230816095746.153036-1-c.ebner@proxmox.com> References: <20230816095746.153036-1-c.ebner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.131 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 Subject: [pbs-devel] [PATCH v3 proxmox-backup 1/2] fix: #4761: unlink existing entries for hard/symlinks when overwrite 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, 16 Aug 2023 09:58:34 -0000 Creating symlinks or hardlinks might fail if a directory entry with the same name is already present on the filesystem during restore. When the overwrite flag is given, on failure unlink the existing entry (except directories) and retry hard/symlink creation. Signed-off-by: Christian Ebner --- changes since v1: * rebased to current master changes since v2: * Capture `err` in match pattern * Deduplicate code by introducing `dolink` closure pbs-client/src/pxar/extract.rs | 46 ++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/pbs-client/src/pxar/extract.rs b/pbs-client/src/pxar/extract.rs index 4dbaf52d..9a95faa0 100644 --- a/pbs-client/src/pxar/extract.rs +++ b/pbs-client/src/pxar/extract.rs @@ -547,7 +547,21 @@ impl Extractor { link: &OsStr, ) -> Result<(), Error> { let parent = self.parent_fd()?; - nix::unistd::symlinkat(link, Some(parent), file_name)?; + + match nix::unistd::symlinkat(link, Some(parent), file_name) { + Ok(()) => {} + Err(err @ nix::errno::Errno::EEXIST) => { + if !self.overwrite { + return Err(err.into()); + } + // Never unlink directories + let flag = nix::unistd::UnlinkatFlags::NoRemoveDir; + nix::unistd::unlinkat(Some(parent), file_name, flag)?; + nix::unistd::symlinkat(link, Some(parent), file_name)?; + } + Err(err) => return Err(err.into()) + } + metadata::apply_at( self.feature_flags, metadata, @@ -564,13 +578,29 @@ impl Extractor { let parent = self.parent_fd()?; let root = self.dir_stack.root_dir_fd()?; let target = CString::new(link.as_bytes())?; - nix::unistd::linkat( - Some(root.as_raw_fd()), - target.as_c_str(), - Some(parent), - file_name, - nix::unistd::LinkatFlags::NoSymlinkFollow, - )?; + let dolink = || { + nix::unistd::linkat( + Some(root.as_raw_fd()), + target.as_c_str(), + Some(parent), + file_name, + nix::unistd::LinkatFlags::NoSymlinkFollow, + ) + }; + + match dolink() { + Ok(()) => {} + Err(err @ nix::errno::Errno::EEXIST) => { + if !self.overwrite { + return Err(err.into()); + } + // Never unlink directories + let flag = nix::unistd::UnlinkatFlags::NoRemoveDir; + nix::unistd::unlinkat(Some(parent), file_name, flag)?; + dolink()?; + } + Err(err) => return Err(err.into()) + } Ok(()) } -- 2.39.2