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 146091FF2CF for ; Thu, 11 Jul 2024 19:07:39 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 68F2637968; Thu, 11 Jul 2024 19:08:02 +0200 (CEST) From: Christoph Heiss To: pbs-devel@lists.proxmox.com Date: Thu, 11 Jul 2024 19:07:09 +0200 Message-ID: <20240711170723.1115046-1-c.heiss@proxmox.com> X-Mailer: git-send-email 2.45.1 MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.017 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 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" 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, + ); + 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