From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dietmar@proxmox.com>
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 DB2A76B14D
 for <pbs-devel@lists.proxmox.com>; Mon, 20 Sep 2021 08:36:24 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id D96C71BA79
 for <pbs-devel@lists.proxmox.com>; Mon, 20 Sep 2021 08:36:24 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id 8C52D1BA61
 for <pbs-devel@lists.proxmox.com>; Mon, 20 Sep 2021 08:36:23 +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 4F0F844999;
 Mon, 20 Sep 2021 08:36:17 +0200 (CEST)
From: Dietmar Maurer <dietmar@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Mon, 20 Sep 2021 08:36:14 +0200
Message-Id: <20210920063614.3151663-2-dietmar@proxmox.com>
X-Mailer: git-send-email 2.30.2
In-Reply-To: <20210920063614.3151663-1-dietmar@proxmox.com>
References: <20210920063614.3151663-1-dietmar@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.667 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pbs-devel] [PATCH proxmox 2/2] atomic_open_or_create_file: add
 support for OFlag::O_EXCL
X-BeenThere: pbs-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Backup Server development discussion
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Mon, 20 Sep 2021 06:36:24 -0000

---
 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