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 617FB605DA for ; Wed, 12 Aug 2020 16:35:00 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 4CBAC11EDE for ; Wed, 12 Aug 2020 16:34:30 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (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 id BBD8811ED6 for ; Wed, 12 Aug 2020 16:34:29 +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 897D7445C0 for ; Wed, 12 Aug 2020 16:34:29 +0200 (CEST) From: Mira Limbeck To: pbs-devel@lists.proxmox.com Date: Wed, 12 Aug 2020 16:34:20 +0200 Message-Id: <20200812143421.8899-1-m.limbeck@proxmox.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.170 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment KAM_LAZY_DOMAIN_SECURITY 1 Sending domain does not have any anti-forgery methods NO_DNS_FOR_FROM 0.379 Envelope sender has no MX or A DNS records RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_NONE 0.001 SPF: sender does not publish an 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. [fs.rs] Subject: [pbs-devel] [PATCH proxmox v2] tools: Add tempfile() helper function 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, 12 Aug 2020 14:35:00 -0000 The tempfile() helper function tries to create a temporary file in /tmp with the O_TMPFILE option. If that fails it falls back to using mkstemp(). As O_TMPFILE was introduced in kernel 3.11 this fallback can help with CentOS 7 and its 3.10 kernel as well as with WSL (Windows Subsystem for Linux). Signed-off-by: Mira Limbeck --- proxmox/src/tools/fs.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/proxmox/src/tools/fs.rs b/proxmox/src/tools/fs.rs index b1a95b5..b3072db 100644 --- a/proxmox/src/tools/fs.rs +++ b/proxmox/src/tools/fs.rs @@ -3,6 +3,7 @@ use std::ffi::CStr; use std::fs::{File, OpenOptions}; use std::io::{self, BufRead, BufReader, Write}; +use std::os::unix::fs::OpenOptionsExt; use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; use std::path::Path; use std::time::Duration; @@ -11,7 +12,7 @@ use anyhow::{bail, format_err, Error}; use nix::errno::Errno; use nix::fcntl::OFlag; use nix::sys::stat; -use nix::unistd::{self, Gid, Uid}; +use nix::unistd::{self, mkstemp, unlink, Gid, Uid}; use serde_json::Value; use crate::sys::error::SysResult; @@ -518,3 +519,25 @@ pub fn open_file_locked>(path: P, timeout: Duration) -> Result bail!("Unable to acquire lock {:?} - {}", path, err), } } + +/// Create a new tempfile by using O_TMPFILE with a fallback to mkstemp() if it fails (e.g. not supported). +pub fn tempfile() -> Result { + match std::fs::OpenOptions::new() + .write(true) + .read(true) + .custom_flags(libc::O_TMPFILE) + .open("/tmp") + { + Ok(file) => return Ok(file), + Err(err) => { + eprintln!( + "Error creating tempfile: '{}', trying mkstemp() instead", + err + ); + let (fd, path) = mkstemp("/tmp/proxmox-tmpfile_XXXXXX")?; + unlink(path.as_path())?; + let file = unsafe { File::from_raw_fd(fd) }; + Ok(file) + } + } +} -- 2.20.1