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) server-digest SHA256) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 578579BD0 for ; Fri, 4 Aug 2023 14:20:42 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 43534EADB for ; Fri, 4 Aug 2023 14:20:42 +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) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Fri, 4 Aug 2023 14:20:41 +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 5F6DC40BF2 for ; Fri, 4 Aug 2023 14:20:41 +0200 (CEST) Date: Fri, 4 Aug 2023 14:20:40 +0200 From: Wolfgang Bumiller To: Christian Ebner Cc: pbs-devel@lists.proxmox.com Message-ID: <7jplu7x73x2ebk4owsu2dpibgykjqgccbzdmc4w4loerpiks5k@wlihalt5h7c7> References: <20230801103412.182490-1-c.ebner@proxmox.com> <20230801103412.182490-2-c.ebner@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230801103412.182490-2-c.ebner@proxmox.com> X-SPAM-LEVEL: Spam detection results: 0 AWL 0.114 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 - URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [extract.rs] Subject: Re: [pbs-devel] [PATCH v2 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: Fri, 04 Aug 2023 12:20:42 -0000 On Tue, Aug 01, 2023 at 12:34:11PM +0200, Christian Ebner wrote: > 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 > > pbs-client/src/pxar/extract.rs | 39 +++++++++++++++++++++++++++++++--- > 1 file changed, 36 insertions(+), 3 deletions(-) > > diff --git a/pbs-client/src/pxar/extract.rs b/pbs-client/src/pxar/extract.rs > index 4dbaf52d..c1e7b417 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(nix::errno::Errno::EEXIST) => { > + if !self.overwrite { > + return Err(nix::errno::Errno::EEXIST.into()); > + } The above could be shortened to a pattern gaurd: Err(nix::errno::Errno::EEXIST) if self.overwrite => { then we don't need the extra `return Err()` since we just run into the final `Err(err)` case. > + // 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,32 @@ 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( > + > + match nix::unistd::linkat( > Some(root.as_raw_fd()), > target.as_c_str(), > Some(parent), > file_name, > nix::unistd::LinkatFlags::NoSymlinkFollow, > - )?; We can avoid some duplication by moving the `linkat` call into a closure: let dolink = || { nix::unistd::linkat( Some(root.as_raw_fd()), target.as_c_str(), Some(parent), file_name, nix::unistd::LinkatFlags::NoSymlinkFollow, ) }; match dolink() { ... nix::unistd::unlinkat(Some(parent), file_name, flag)?; dolink()?; } ... > + ) { > + Ok(()) => {} > + Err(nix::errno::Errno::EEXIST) => { > + if !self.overwrite { > + return Err(nix::errno::Errno::EEXIST.into()); > + } > + // Never unlink directories > + let flag = nix::unistd::UnlinkatFlags::NoRemoveDir; > + nix::unistd::unlinkat(Some(parent), file_name, flag)?; > + nix::unistd::linkat( > + Some(root.as_raw_fd()), > + target.as_c_str(), > + Some(parent), > + file_name, > + nix::unistd::LinkatFlags::NoSymlinkFollow, > + )?; > + } > + Err(err) => return Err(err.into()) > + } > > Ok(()) > } > -- > 2.39.2