public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dietmar Maurer <dietmar@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox 2/2] atomic_open_or_create_file: add support for OFlag::O_EXCL
Date: Mon, 20 Sep 2021 08:36:14 +0200	[thread overview]
Message-ID: <20210920063614.3151663-2-dietmar@proxmox.com> (raw)
In-Reply-To: <20210920063614.3151663-1-dietmar@proxmox.com>

---
 proxmox/src/tools/fs.rs | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)

diff --git a/proxmox/src/tools/fs.rs b/proxmox/src/tools/fs.rs
index 3283d04..9d01aae 100644
--- a/proxmox/src/tools/fs.rs
+++ b/proxmox/src/tools/fs.rs
@@ -209,16 +209,25 @@ pub fn atomic_open_or_create_file<P: AsRef<Path>>(
         bail!("open {:?} failed - unsupported OFlag O_DIRECTORY", path);
     }
 
+    let exclusive = if oflag.contains(OFlag::O_EXCL) {
+        oflag.remove(OFlag::O_EXCL); // we nned to handle that ourselfes
+        true
+    } else {
+        false
+    };
+
     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);
+    if !exclusive {
+        // 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);
+                }
             }
         }
     }
@@ -266,7 +275,7 @@ pub fn atomic_open_or_create_file<P: AsRef<Path>>(
             // the file, let's just open theirs instead:
             let _ = nix::unistd::unlink(&temp_file_name);
 
-            if err.already_exists() {
+            if !exclusive && 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),
-- 
2.30.2





  reply	other threads:[~2021-09-20  6:36 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-20  6:36 [pbs-devel] [PATCH proxmox 1/2] atomic_open_or_create_file: catch unsupported flag OFlag::O_DIRECTORY Dietmar Maurer
2021-09-20  6:36 ` Dietmar Maurer [this message]
2021-09-22  5:48 ` [pbs-devel] applied: " Thomas Lamprecht

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=20210920063614.3151663-2-dietmar@proxmox.com \
    --to=dietmar@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 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