From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 278CC6985A for ; Fri, 7 Aug 2020 10:18:41 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 24F6D22F2F for ; Fri, 7 Aug 2020 10:18:41 +0200 (CEST) Received: from elsa.proxmox.com (212-186-127-178.static.upcbusiness.at [212.186.127.178]) by firstgate.proxmox.com (Proxmox) with ESMTP id 8F86122F27 for ; Fri, 7 Aug 2020 10:18:40 +0200 (CEST) Received: by elsa.proxmox.com (Postfix, from userid 0) id 7CBAAAE3B57; Fri, 7 Aug 2020 10:18:40 +0200 (CEST) From: Dietmar Maurer To: pbs-devel@lists.proxmox.com, s.reiter@proxmox.com Date: Fri, 7 Aug 2020 10:18:21 +0200 Message-Id: <20200807081823.17200-2-dietmar@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200807081823.17200-1-dietmar@proxmox.com> References: <20200807081823.17200-1-dietmar@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -1.041 Adjusted score from AWL reputation of From: address 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 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [fs.rs] Subject: [pbs-devel] [PATCH proxmox-backup 1/3] src/tools/fs.rs: new helper lock_dir_noblock X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 07 Aug 2020 08:18:41 -0000 --- src/tools/fs.rs | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/src/tools/fs.rs b/src/tools/fs.rs index ff625d3..5aaaecd 100644 --- a/src/tools/fs.rs +++ b/src/tools/fs.rs @@ -7,10 +7,18 @@ use std::os::unix::io::{AsRawFd, RawFd}; use anyhow::{format_err, Error}; use nix::dir; use nix::dir::Dir; +use nix::fcntl::OFlag; +use nix::sys::stat::Mode; + use regex::Regex; +use proxmox::sys::error::SysError; + + use crate::tools::borrow::Tied; +pub type DirLockGuard = Dir; + /// This wraps nix::dir::Entry with the parent directory's file descriptor. pub struct ReadDirEntry { entry: dir::Entry, @@ -94,9 +102,6 @@ impl Iterator for ReadDir { /// Create an iterator over sub directory entries. /// This uses `openat` on `dirfd`, so `path` can be relative to that or an absolute path. pub fn read_subdir(dirfd: RawFd, path: &P) -> nix::Result { - use nix::fcntl::OFlag; - use nix::sys::stat::Mode; - let dir = Dir::openat(dirfd, path, OFlag::O_RDONLY, Mode::empty())?; let fd = dir.as_raw_fd(); let iter = Tied::new(dir, |dir| { @@ -259,3 +264,31 @@ impl Default for FSXAttr { } } } + + +pub fn lock_dir_noblock( + path: &std::path::Path, + what: &str, + would_block_msg: &str, +) -> Result { + let mut handle = Dir::open(path, OFlag::O_RDONLY, Mode::empty()) + .map_err(|err| { + format_err!("unable to open {} directory {:?} for locking - {}", what, path, err) + })?; + + // acquire in non-blocking mode, no point in waiting here since other + // backups could still take a very long time + proxmox::tools::fs::lock_file(&mut handle, true, Some(std::time::Duration::from_nanos(0))) + .map_err(|err| { + format_err!( + "unable to acquire lock on {} directory {:?} - {}", what, path, + if err.would_block() { + String::from(would_block_msg) + } else { + err.to_string() + } + ) + })?; + + Ok(handle) +} -- 2.20.1