all lists on lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH proxmox v2] tools: Add tempfile() helper function
@ 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 ` [pbs-devel] [PATCH proxmox v2] tools: Add tempfile() helper function Fabian Grünbichler
  0 siblings, 2 replies; 3+ messages in thread
From: Mira Limbeck @ 2020-08-12 14:34 UTC (permalink / raw)
  To: pbs-devel

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
+            );
+            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





^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-08-13  7:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-12 14:34 [pbs-devel] [PATCH proxmox v2] tools: Add tempfile() helper function 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 ` [pbs-devel] [PATCH proxmox v2] tools: Add tempfile() helper function Fabian Grünbichler

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.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal