From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <d.csapak@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 3C1F77869E
 for <pbs-devel@lists.proxmox.com>; Tue, 28 Jun 2022 13:13:50 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 32BEAA419
 for <pbs-devel@lists.proxmox.com>; Tue, 28 Jun 2022 13:13:20 +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))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id 2F133A3FE
 for <pbs-devel@lists.proxmox.com>; Tue, 28 Jun 2022 13:13:19 +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 F422A4098B
 for <pbs-devel@lists.proxmox.com>; Tue, 28 Jun 2022 13:13:18 +0200 (CEST)
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Tue, 28 Jun 2022 13:13:16 +0200
Message-Id: <20220628111318.2648586-2-d.csapak@proxmox.com>
X-Mailer: git-send-email 2.30.2
In-Reply-To: <20220628111318.2648586-1-d.csapak@proxmox.com>
References: <20220628111318.2648586-1-d.csapak@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.100 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] [PATCH proxmox 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
 <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: Tue, 28 Jun 2022 11:13:50 -0000

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)

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 proxmox-sys/src/fs/read_dir.rs | 36 ++++++++++++++++++++++++++++++----
 1 file changed, 32 insertions(+), 4 deletions(-)

diff --git a/proxmox-sys/src/fs/read_dir.rs b/proxmox-sys/src/fs/read_dir.rs
index f687748..785a412 100644
--- a/proxmox-sys/src/fs/read_dir.rs
+++ b/proxmox-sys/src/fs/read_dir.rs
@@ -141,20 +141,48 @@ pub fn scandir<P, F>(
     mut callback: F,
 ) -> Result<(), Error>
 where
-    F: FnMut(RawFd, &str, nix::dir::Type) -> Result<(), Error>,
+    F: FnMut(RawFd, &str, nix::dir::Type, Option<nix::sys::stat::FileStat>) -> Result<(), Error>,
     P: ?Sized + nix::NixPath,
 {
     for entry in scan_subdir(dirfd, path, regex)? {
         let entry = entry?;
-        let file_type = match entry.file_type() {
-            Some(file_type) => file_type,
-            None => bail!("unable to detect file type"),
+        let (file_type, stat) = match entry.file_type() {
+            Some(file_type) => (file_type, None),
+            None => {
+                use nix::sys::stat::SFlag;
+                let stat = nix::sys::stat::fstatat(
+                    entry.parent_fd(),
+                    entry.file_name(),
+                    nix::fcntl::AtFlags::AT_SYMLINK_NOFOLLOW,
+                )?;
+                let type_flag = SFlag::from_bits_truncate(stat.st_mode);
+                let file_type = if type_flag.contains(SFlag::S_IFDIR) {
+                    nix::dir::Type::Directory
+                } else if type_flag.contains(SFlag::S_IFIFO) {
+                    nix::dir::Type::Fifo
+                } else if type_flag.contains(SFlag::S_IFCHR) {
+                    nix::dir::Type::CharacterDevice
+                } else if type_flag.contains(SFlag::S_IFBLK) {
+                    nix::dir::Type::BlockDevice
+                } else if type_flag.contains(SFlag::S_IFREG) {
+                    nix::dir::Type::File
+                } else if type_flag.contains(SFlag::S_IFLNK) {
+                    nix::dir::Type::Symlink
+                } else if type_flag.contains(SFlag::S_IFSOCK) {
+                    nix::dir::Type::Socket
+                } else {
+                    bail!("unable to detect file type")
+                };
+
+                (file_type, Some(stat))
+            }
         };
 
         callback(
             entry.parent_fd(),
             unsafe { entry.file_name_utf8_unchecked() },
             file_type,
+            stat,
         )?;
     }
     Ok(())
-- 
2.30.2