From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id A28561FF2D7 for ; Fri, 12 Jul 2024 09:37:14 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 181E03DAF3; Fri, 12 Jul 2024 09:37:38 +0200 (CEST) Date: Fri, 12 Jul 2024 09:37:03 +0200 (CEST) From: Christian Ebner To: Proxmox Backup Server development discussion , Christoph Heiss Message-ID: <793904308.2594.1720769823342@webmail.proxmox.com> In-Reply-To: <20240711170723.1115046-1-c.heiss@proxmox.com> References: <20240711170723.1115046-1-c.heiss@proxmox.com> MIME-Version: 1.0 X-Priority: 3 Importance: Normal X-Mailer: Open-Xchange Mailer v7.10.6-Rev66 X-Originating-Client: open-xchange-appsuite X-SPAM-LEVEL: Spam detection results: 0 AWL 0.022 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 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [docs.rs, file.rs, proxmox.com] Subject: Re: [pbs-devel] [PATCH proxmox] sys: file: use renameat2() from `nix` crate 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: , Reply-To: Proxmox Backup Server development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pbs-devel-bounces@lists.proxmox.com Sender: "pbs-devel" one small nit inline: > On 11.07.2024 19:07 CEST Christoph Heiss wrote: > > > Saves us from converting the paths to raw C strings ourselves, thus > simplifying it quite a bit. > > No functional changes. > > Signed-off-by: Christoph Heiss > --- > proxmox-sys/src/fs/file.rs | 51 ++++++++++++++++---------------------- > 1 file changed, 22 insertions(+), 29 deletions(-) > > diff --git a/proxmox-sys/src/fs/file.rs b/proxmox-sys/src/fs/file.rs > index ac513891..3b20c4ea 100644 > --- a/proxmox-sys/src/fs/file.rs > +++ b/proxmox-sys/src/fs/file.rs > @@ -10,7 +10,6 @@ use nix::errno::Errno; > use nix::fcntl::OFlag; > use nix::sys::stat; > use nix::unistd; > -use nix::NixPath; > use serde_json::Value; > > use proxmox_lang::try_block; > @@ -266,30 +265,32 @@ pub fn atomic_open_or_create_file>( > > // rotate the file into place, but use `RENAME_NOREPLACE`, so in case 2 processes race against > // the initialization, the first one wins! > - let rename_result = temp_file_name.with_nix_path(|c_file_name| { > - path.with_nix_path(|new_path| unsafe { > - // This also works on file systems which don't support hardlinks (eg. vfat) > - match Errno::result(libc::renameat2( > - libc::AT_FDCWD, > - c_file_name.as_ptr(), > - libc::AT_FDCWD, > - new_path.as_ptr(), > - libc::RENAME_NOREPLACE, > - )) { > - Err(Errno::EINVAL) => (), // dumb file system, try `link`+`unlink` > - other => return other, > - }; > - // but some file systems don't support `RENAME_NOREPLACE` > + let rename_result = match nix::fcntl::renameat2( > + None, // AT_FDCWD > + &temp_file_name, > + None, // AT_FDCWD > + path, > + nix::fcntl::RenameFlags::RENAME_NOREPLACE, > + ) { > + Err(Errno::EINVAL) => { > + // some file systems don't support `RENAME_NOREPLACE` > // so we just use `link` + `unlink` instead > - let result = Errno::result(libc::link(c_file_name.as_ptr(), new_path.as_ptr())); > - let _ = libc::unlink(c_file_name.as_ptr()); > + let result = nix::unistd::linkat( > + None, > + &temp_file_name, > + None, > + &path.to_path_buf(), > + nix::unistd::LinkatFlags::NoSymlinkFollow, According to https://docs.rs/nix/0.29.0/nix/unistd/type.LinkatFlags.html `LinkatFlags` will be deprecated with version 0.28. We currently use nix 0.16.1, which already provides the suggested replacment using the `AtFlags` https://docs.rs/nix/0.26.1/nix/fcntl/struct.AtFlags.html. So it would make sense to already use these instead. > + ); > + let _ = nix::unistd::unlink(&temp_file_name); > result > - }) > - }); > + } > + other => other, > + }; > > match rename_result { > - Ok(Ok(Ok(_))) => Ok(file), > - Ok(Ok(Err(err))) => { > + Ok(_) => Ok(file), > + Err(err) => { > // if another process has already raced ahead and created > // the file, let's just open theirs instead: > let _ = nix::unistd::unlink(&temp_file_name); > @@ -308,14 +309,6 @@ pub fn atomic_open_or_create_file>( > ); > } > } > - Ok(Err(err)) => { > - let _ = nix::unistd::unlink(&temp_file_name); > - bail!("with_nix_path {:?} failed - {}", path, err); > - } > - Err(err) => { > - let _ = nix::unistd::unlink(&temp_file_name); > - bail!("with_nix_path {:?} failed - {}", temp_file_name, err); > - } > } > } > > -- > 2.45.1 > > > > _______________________________________________ > pbs-devel mailing list > pbs-devel@lists.proxmox.com > https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel _______________________________________________ pbs-devel mailing list pbs-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel