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 C0C7D788C2 for ; Wed, 29 Jun 2022 09:43:38 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id B01FD1ABBE for ; Wed, 29 Jun 2022 09:43:08 +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 for ; Wed, 29 Jun 2022 09:43:07 +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 41F5F43D42 for ; Wed, 29 Jun 2022 09:43:07 +0200 (CEST) Date: Wed, 29 Jun 2022 09:43:06 +0200 From: Wolfgang Bumiller To: Dominik Csapak Cc: pbs-devel@lists.proxmox.com Message-ID: <20220629074306.7weci3ql6kjg2xb2@casey.proxmox.com> References: <20220628131514.3798702-1-d.csapak@proxmox.com> <20220628131514.3798702-2-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220628131514.3798702-2-d.csapak@proxmox.com> X-SPAM-LEVEL: Spam detection results: 0 AWL 0.302 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 T_SCC_BODY_TEXT_LINE -0.01 - Subject: [pbs-devel] applied: [PATCH proxmox v2 1/1] partially fix #2915: proxmox-sys: scandir: stat if file_type is unknown 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: Wed, 29 Jun 2022 07:43:38 -0000 applied On Tue, Jun 28, 2022 at 03:15:13PM +0200, Dominik Csapak wrote: > when using readdir/getdents the file type might be 'DT_UNKNOWN' > (depending on the filesystem). Do a fstatat in that case to > get the file type. Since maybe the callback wants to do > a stat anyway, pass it there (if done) > > adds two new helpers: > 'file_type_from_file_stat': uses a FileStat struct to get the file type > 'get_file_type': calls fstatat to determine the file_type > > Signed-off-by: Dominik Csapak > --- > proxmox-sys/src/fs/read_dir.rs | 34 ++++++++++++++++++++++++++++++++-- > 1 file changed, 32 insertions(+), 2 deletions(-) > > diff --git a/proxmox-sys/src/fs/read_dir.rs b/proxmox-sys/src/fs/read_dir.rs > index f687748..8c0c5a0 100644 > --- a/proxmox-sys/src/fs/read_dir.rs > +++ b/proxmox-sys/src/fs/read_dir.rs > @@ -2,7 +2,7 @@ use std::borrow::{Borrow, BorrowMut}; > use std::ops::{Deref, DerefMut}; > use std::os::unix::io::{AsRawFd, RawFd}; > > -use anyhow::{bail, format_err, Error}; > +use anyhow::{format_err, Error}; > use nix::dir; > use nix::dir::Dir; > use nix::fcntl::OFlag; > @@ -148,7 +148,7 @@ where > let entry = entry?; > let file_type = match entry.file_type() { > Some(file_type) => file_type, > - None => bail!("unable to detect file type"), > + None => get_file_type(entry.parent_fd(), entry.file_name())?, > }; > > callback( > @@ -321,3 +321,33 @@ fn do_lock_dir_noblock( > > Ok(handle) > } > + > +/// extracts [`nix::dir::Type`] from a [`nix::sys::stat::FileStat`] if possible > +pub fn file_type_from_file_stat(stat: &nix::sys::stat::FileStat) -> Option { > + use nix::dir::Type; > + > + // mask out all unnecessary bits > + Some(match stat.st_mode & libc::S_IFMT { > + libc::S_IFSOCK => Type::Socket, > + libc::S_IFLNK => Type::Symlink, > + libc::S_IFREG => Type::File, > + libc::S_IFBLK => Type::BlockDevice, > + libc::S_IFDIR => Type::Directory, > + libc::S_IFCHR => Type::CharacterDevice, > + libc::S_IFIFO => Type::Fifo, > + _ => return None, > + }) > +} > + > +/// Returns the file type of the `path` in the `parent_fd` > +/// > +/// calls [`nix::sys::stat::fstatat`] to determine it > +pub fn get_file_type( > + parent_fd: RawFd, > + path: &P, > +) -> Result { > + let stat = nix::sys::stat::fstatat(parent_fd, path, nix::fcntl::AtFlags::AT_SYMLINK_NOFOLLOW)?; > + let file_type = > + file_type_from_file_stat(&stat).ok_or_else(|| format_err!("unable to detect file type"))?; > + Ok(file_type) > +} > -- > 2.30.2