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 3E91A60800 for ; Thu, 13 Aug 2020 09:43:45 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 2BB2017828 for ; Thu, 13 Aug 2020 09:43:15 +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)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 3E9EB1781E for ; Thu, 13 Aug 2020 09:43:14 +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 03EAE445DC for ; Thu, 13 Aug 2020 09:43:14 +0200 (CEST) Date: Thu, 13 Aug 2020 09:43:07 +0200 From: Fabian =?iso-8859-1?q?Gr=FCnbichler?= To: Proxmox Backup Server development discussion References: <20200812143421.8899-1-m.limbeck@proxmox.com> In-Reply-To: <20200812143421.8899-1-m.limbeck@proxmox.com> MIME-Version: 1.0 User-Agent: astroid/0.15.0 (https://github.com/astroidmail/astroid) Message-Id: <1597303267.ccruwq5t2k.astroid@nora.none> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-SPAM-LEVEL: Spam detection results: 0 AWL 0.035 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment 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_PASS -0.001 SPF: sender matches SPF record Subject: Re: [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: Thu, 13 Aug 2020 07:43:45 -0000 On August 12, 2020 4:34 pm, Mira Limbeck wrote: > 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(). >=20 > 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). >=20 > Signed-off-by: Mira Limbeck > --- > proxmox/src/tools/fs.rs | 25 ++++++++++++++++++++++++- > 1 file changed, 24 insertions(+), 1 deletion(-) >=20 > 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; > =20 > use crate::sys::error::SysResult; > @@ -518,3 +519,25 @@ pub fn open_file_locked>(path: P, tim= eout: Duration) -> Result Err(err) =3D> bail!("Unable to acquire lock {:?} - {}", path, er= r), > } > } > + > +/// 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) =3D> return Ok(file), > + Err(err) =3D> { > + eprintln!( > + "Error creating tempfile: '{}', trying mkstemp() instead= ", > + err > + ); print this once, cache result, always use fallback until restart if=20 needed? also, can we decide based on the error if we even want/can=20 fallback? e.g., /tmp might have strange permissions? the man page states=20 that open should return EISDIR in case O_TMPFILE is not supported.. > + let (fd, path) =3D mkstemp("/tmp/proxmox-tmpfile_XXXXXX")?; I'd still like this to be in a sub-directory (owned by 'backup'), even=20 if it should be unproblematic.. > + unlink(path.as_path())?; > + let file =3D unsafe { File::from_raw_fd(fd) }; > + Ok(file) > + } > + } > +} > --=20 > 2.20.1 >=20 >=20 >=20 > _______________________________________________ > pbs-devel mailing list > pbs-devel@lists.proxmox.com > https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel >=20 >=20 >=20 =