From: "Fabian Grünbichler" <f.gruenbichler@proxmox.com>
To: Proxmox Backup Server development discussion
<pbs-devel@lists.proxmox.com>
Subject: Re: [pbs-devel] [PATCH proxmox v2] tools: Add tempfile() helper function
Date: Thu, 13 Aug 2020 09:43:07 +0200 [thread overview]
Message-ID: <1597303267.ccruwq5t2k.astroid@nora.none> (raw)
In-Reply-To: <20200812143421.8899-1-m.limbeck@proxmox.com>
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().
>
> 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 <m.limbeck@proxmox.com>
> ---
> 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<P: AsRef<Path>>(path: P, timeout: Duration) -> Result<Fi
> Err(err) => 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<File, Error> {
> + 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
> + );
print this once, cache result, always use fallback until restart if
needed?
also, can we decide based on the error if we even want/can
fallback? e.g., /tmp might have strange permissions? the man page states
that open should return EISDIR in case O_TMPFILE is not supported..
> + let (fd, path) = mkstemp("/tmp/proxmox-tmpfile_XXXXXX")?;
I'd still like this to be in a sub-directory (owned by 'backup'), even
if it should be unproblematic..
> + unlink(path.as_path())?;
> + let file = unsafe { File::from_raw_fd(fd) };
> + Ok(file)
> + }
> + }
> +}
> --
> 2.20.1
>
>
>
> _______________________________________________
> pbs-devel mailing list
> pbs-devel@lists.proxmox.com
> https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel
>
>
>
prev parent reply other threads:[~2020-08-13 7:43 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-08-12 14:34 Mira Limbeck
2020-08-12 14:34 ` [pbs-devel] [PATCH proxmox-backup v2] Replace all occurences of open() with O_TMPFILE Mira Limbeck
2020-08-13 7:43 ` Fabian Grünbichler [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1597303267.ccruwq5t2k.astroid@nora.none \
--to=f.gruenbichler@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.