public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH proxmox 1/2] new helper atomic_open_or_create_file()
@ 2021-07-16  8:28 Dietmar Maurer
  2021-07-16  8:28 ` [pbs-devel] [PATCH proxmox-backup 1/2] use new atomic_open_or_create_file Dietmar Maurer
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Dietmar Maurer @ 2021-07-16  8:28 UTC (permalink / raw)
  To: pbs-devel

---
 proxmox/src/tools/fs.rs | 86 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 85 insertions(+), 1 deletion(-)

diff --git a/proxmox/src/tools/fs.rs b/proxmox/src/tools/fs.rs
index 12e96bd..2a93b30 100644
--- a/proxmox/src/tools/fs.rs
+++ b/proxmox/src/tools/fs.rs
@@ -12,9 +12,10 @@ use nix::errno::Errno;
 use nix::fcntl::OFlag;
 use nix::sys::stat;
 use nix::unistd::{self, Gid, Uid};
+use nix::NixPath;
 use serde_json::Value;
 
-use crate::sys::error::SysResult;
+use crate::sys::error::{SysError, SysResult};
 use crate::sys::timer;
 use crate::tools::fd::Fd;
 use crate::try_block;
@@ -187,6 +188,89 @@ pub fn replace_file<P: AsRef<Path>>(
     Ok(())
 }
 
+/// Like open(2), but allows setting initial data, perm, owner and group
+///
+/// Since we need to initialize the file, we also need a solid slow
+/// path where we create the file. In order to avoid races, we create
+/// it in a temporary location and rotate it in place.
+pub fn atomic_open_or_create_file<P: AsRef<Path>>(
+    path: P,
+    mut oflag: OFlag,
+    initial_data: &[u8],
+    options: CreateOptions,
+) -> Result<File, Error> {
+    let path = path.as_ref();
+
+    if oflag.contains(OFlag::O_TMPFILE) {
+        bail!("open {:?} failed - unsupported OFlag O_TMPFILE", path);
+    }
+
+    oflag.remove(OFlag::O_CREAT); // we want to handle CREAT ourselfes
+
+    // Note: 'mode' is ignored, because oflag does not contain O_CREAT or O_TMPFILE
+    match nix::fcntl::open(path, oflag, stat::Mode::empty()) {
+        Ok(fd) => return Ok(unsafe { File::from_raw_fd(fd) }),
+        Err(err) => {
+           if err.not_found() {
+               // fall thrue -  try to create the file
+           } else {
+               bail!("open {:?} failed - {}", path, err);
+           }
+        }
+    }
+
+    let (mut file, temp_file_name) = make_tmp_file(path, options)?;
+
+    if !initial_data.is_empty() {
+        file.write_all(initial_data).map_err(|err| {
+            let _ = nix::unistd::unlink(&temp_file_name);
+            format_err!(
+                "writing initial data to {:?} failed - {}",
+                temp_file_name,
+                err,
+            )
+        })?;
+    }
+
+    // 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 {
+            let rc = libc::renameat2(
+                libc::AT_FDCWD,
+                c_file_name.as_ptr(),
+                libc::AT_FDCWD,
+                new_path.as_ptr(),
+                libc::RENAME_NOREPLACE,
+            );
+            nix::errno::Errno::result(rc)
+        })?
+    })?;
+
+    match rename_result {
+        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);
+
+            if err.already_exists() {
+                match nix::fcntl::open(path, oflag, stat::Mode::empty()) {
+                    Ok(fd) => Ok(unsafe { File::from_raw_fd(fd) }),
+                    Err(err) => bail!("open {:?} failed - {}", path, err),
+                }
+            } else {
+                bail!(
+                    "failed to move file at {:?} into place at {:?} - {}",
+                    temp_file_name,
+                    path,
+                    err
+                );
+            }
+        }
+    }
+}
+
 /// Change ownership of an open file handle
 pub fn fchown(fd: RawFd, owner: Option<Uid>, group: Option<Gid>) -> Result<(), Error> {
     // According to the POSIX specification, -1 is used to indicate that owner and group
-- 
2.30.2




^ permalink raw reply	[flat|nested] 8+ messages in thread
* [pbs-devel] [PATCH proxmox-apt] depend on proxmox 0.12.0, bump version to 0.5.1-1
@ 2021-07-20 11:51 Dietmar Maurer
  2021-07-20 11:51 ` [pbs-devel] [PATCH proxmox-backup 2/2] add helpers to write configuration files Dietmar Maurer
  0 siblings, 1 reply; 8+ messages in thread
From: Dietmar Maurer @ 2021-07-20 11:51 UTC (permalink / raw)
  To: pbs-devel

---
 Cargo.toml       | 4 ++--
 debian/changelog | 6 ++++++
 debian/control   | 8 ++++----
 3 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index a316273..bdce2a1 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "proxmox-apt"
-version = "0.5.0"
+version = "0.5.1"
 authors = [
     "Fabian Ebner <f.ebner@proxmox.com>",
     "Proxmox Support Team <support@proxmox.com>",
@@ -20,5 +20,5 @@ path = "src/lib.rs"
 anyhow = "1.0"
 once_cell = "1.3.1"
 openssl = "0.10"
-proxmox = { version = "0.11.6", features = [ "api-macro" ] }
+proxmox = { version = "0.12.0", features = [ "api-macro" ] }
 serde = { version = "1.0", features = ["derive"] }
diff --git a/debian/changelog b/debian/changelog
index b8e116d..542ec36 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,9 @@
+rust-proxmox-apt (0.5.1-1) unstable; urgency=medium
+
+  * depend on proxmox 0.12.0
+
+ -- Proxmox Support Team <support@proxmox.com>  Tue, 20 Jul 2021 13:18:02 +0200
+
 rust-proxmox-apt (0.5.0-1) unstable; urgency=medium
 
   * standard repo detection: handle alternative URI for PVE repos
diff --git a/debian/control b/debian/control
index ced40f2..f63cd3a 100644
--- a/debian/control
+++ b/debian/control
@@ -9,8 +9,8 @@ Build-Depends: debhelper (>= 12),
  librust-anyhow-1+default-dev <!nocheck>,
  librust-once-cell-1+default-dev (>= 1.3.1-~~) <!nocheck>,
  librust-openssl-0.10+default-dev <!nocheck>,
- librust-proxmox-0.11+api-macro-dev (>= 0.11.6-~~) <!nocheck>,
- librust-proxmox-0.11+default-dev (>= 0.11.6-~~) <!nocheck>,
+ librust-proxmox-0.12+api-macro-dev <!nocheck>,
+ librust-proxmox-0.12+default-dev <!nocheck>,
  librust-serde-1+default-dev <!nocheck>,
  librust-serde-1+derive-dev <!nocheck>
 Maintainer: Proxmox Support Team <support@proxmox.com>
@@ -28,8 +28,8 @@ Depends:
  librust-anyhow-1+default-dev,
  librust-once-cell-1+default-dev (>= 1.3.1-~~),
  librust-openssl-0.10+default-dev,
- librust-proxmox-0.11+api-macro-dev (>= 0.11.6-~~),
- librust-proxmox-0.11+default-dev (>= 0.11.6-~~),
+ librust-proxmox-0.12+api-macro-dev,
+ librust-proxmox-0.12+default-dev,
  librust-serde-1+default-dev,
  librust-serde-1+derive-dev
 Provides:
-- 
2.30.2




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

end of thread, other threads:[~2021-07-20 11:52 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-16  8:28 [pbs-devel] [PATCH proxmox 1/2] new helper atomic_open_or_create_file() Dietmar Maurer
2021-07-16  8:28 ` [pbs-devel] [PATCH proxmox-backup 1/2] use new atomic_open_or_create_file Dietmar Maurer
     [not found]   ` <<20210716082834.2354163-2-dietmar@proxmox.com>
2021-07-19 10:45     ` Fabian Grünbichler
2021-07-16  8:28 ` [pbs-devel] [PATCH proxmox-backup 2/2] add helpers to write configuration files Dietmar Maurer
2021-07-16  8:28 ` [pbs-devel] [PATCH proxmox 2/2] open_file_locked: add options parameter (CreateOptions) Dietmar Maurer
     [not found]   ` <<20210716082834.2354163-4-dietmar@proxmox.com>
2021-07-19 10:44     ` Fabian Grünbichler
     [not found] ` <<20210716082834.2354163-1-dietmar@proxmox.com>
2021-07-19 10:44   ` [pbs-devel] [PATCH proxmox 1/2] new helper atomic_open_or_create_file() Fabian Grünbichler
2021-07-20 11:51 [pbs-devel] [PATCH proxmox-apt] depend on proxmox 0.12.0, bump version to 0.5.1-1 Dietmar Maurer
2021-07-20 11:51 ` [pbs-devel] [PATCH proxmox-backup 2/2] add helpers to write configuration files Dietmar Maurer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal