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 9BFAE786A0
 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 91451A41B
 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 7CFAAA410
 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 4F2CF40518
 for <pbs-devel@lists.proxmox.com>; Tue, 28 Jun 2022 13:13:19 +0200 (CEST)
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Tue, 28 Jun 2022 13:13:18 +0200
Message-Id: <20220628111318.2648586-4-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.050 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
 POISEN_SPAM_PILL          0.1 Meta: its spam
 POISEN_SPAM_PILL_2        0.1 random spam to be learned in bayes
 POISEN_SPAM_PILL_4        0.1 random spam to be learned in bayes
 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 -
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [self.name, hierarchy.rs]
Subject: [pbs-devel] [PATCH proxmox-backup 2/2] partially fix #2915: 'stat'
 in case ReadDirEntry does not contain type
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

readdir/getdents may return 'DT_UNKNOWN' for the file type
(which corresponds to 'None' in nix::dir::Entry), so stat the file and
check the type

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 pbs-datastore/src/chunk_store.rs | 23 ++++++-------
 pbs-datastore/src/hierarchy.rs   | 56 ++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+), 13 deletions(-)

diff --git a/pbs-datastore/src/chunk_store.rs b/pbs-datastore/src/chunk_store.rs
index c9714c1e..2c281b0c 100644
--- a/pbs-datastore/src/chunk_store.rs
+++ b/pbs-datastore/src/chunk_store.rs
@@ -375,24 +375,21 @@ impl ChunkStore {
                 ),
             };
 
-            let file_type = match entry.file_type() {
-                Some(file_type) => file_type,
-                None => bail!(
-                    "unsupported file system type on chunk store '{}'",
-                    self.name
-                ),
-            };
-            if file_type != nix::dir::Type::File {
-                continue;
-            }
-
-            chunk_count += 1;
-
             let filename = entry.file_name();
 
             let lock = self.mutex.lock();
 
             if let Ok(stat) = fstatat(dirfd, filename, nix::fcntl::AtFlags::AT_SYMLINK_NOFOLLOW) {
+                if match entry.file_type() {
+                    Some(file_type) => file_type != nix::dir::Type::File,
+                    None => stat.st_mode & libc::S_IFREG == 0,
+                } {
+                    drop(lock);
+                    continue;
+                }
+
+                chunk_count += 1;
+
                 if stat.st_atime < min_atime {
                     //let age = now - stat.st_atime;
                     //println!("UNLINK {}  {:?}", age/(3600*24), filename);
diff --git a/pbs-datastore/src/hierarchy.rs b/pbs-datastore/src/hierarchy.rs
index d5007b07..a97a1d2a 100644
--- a/pbs-datastore/src/hierarchy.rs
+++ b/pbs-datastore/src/hierarchy.rs
@@ -9,6 +9,16 @@ use pbs_api_types::{BackupNamespace, BackupType, BACKUP_DATE_REGEX, BACKUP_ID_RE
 use crate::backup_info::{BackupDir, BackupGroup};
 use crate::DataStore;
 
+fn dir_entry_is_path(entry: &proxmox_sys::fs::ReadDirEntry) -> Result<bool, Error> {
+    let stat = nix::sys::stat::fstatat(
+        entry.parent_fd(),
+        entry.file_name(),
+        nix::fcntl::AtFlags::AT_SYMLINK_NOFOLLOW,
+    )?;
+    let is_dir = stat.st_mode & libc::S_IFDIR > 0;
+    Ok(is_dir)
+}
+
 /// A iterator for all BackupDir's (Snapshots) in a BackupGroup
 pub struct ListSnapshots {
     group: BackupGroup,
@@ -36,6 +46,17 @@ impl Iterator for ListSnapshots {
                 Ok(ref entry) => {
                     match entry.file_type() {
                         Some(nix::dir::Type::Directory) => entry, // OK
+                        None => match dir_entry_is_path(&entry) {
+                            Ok(true) => entry,
+                            Ok(false) => continue,
+                            Err(err) => {
+                                log::info!(
+                                    "error listing snapshots for {}: {err}",
+                                    self.group.group()
+                                );
+                                continue;
+                            }
+                        },
                         _ => continue,
                     }
                 }
@@ -93,6 +114,17 @@ impl Iterator for ListGroups {
                     Ok(ref entry) => {
                         match entry.file_type() {
                             Some(nix::dir::Type::Directory) => entry, // OK
+                            None => match dir_entry_is_path(&entry) {
+                                Ok(true) => entry,
+                                Ok(false) => continue,
+                                Err(err) => {
+                                    log::info!(
+                                        "error listing groups for {}: {err}",
+                                        self.store.name()
+                                    );
+                                    continue;
+                                }
+                            },
                             _ => continue,
                         }
                     }
@@ -114,6 +146,17 @@ impl Iterator for ListGroups {
                     Ok(ref entry) => {
                         match entry.file_type() {
                             Some(nix::dir::Type::Directory) => entry, // OK
+                            None => match dir_entry_is_path(&entry) {
+                                Ok(true) => entry,
+                                Ok(false) => continue,
+                                Err(err) => {
+                                    log::info!(
+                                        "error listing groups for {}: {err}",
+                                        self.store.name()
+                                    );
+                                    continue;
+                                }
+                            },
                             _ => continue,
                         }
                     }
@@ -176,6 +219,19 @@ impl Iterator for ListNamespaces {
                     Ok(ref entry) => {
                         match entry.file_type() {
                             Some(nix::dir::Type::Directory) => entry, // OK
+                            None => match dir_entry_is_path(&entry) {
+                                Ok(true) => entry,
+                                Ok(false) => continue,
+                                Err(err) => {
+                                    let mut base_path = self.base_path.to_owned();
+                                    if !self.ns.is_root() {
+                                        base_path.push(self.ns.path());
+                                    }
+                                    base_path.push("ns");
+                                    log::info!("error listing dirs in {:?}: {err}", base_path);
+                                    continue;
+                                }
+                            },
                             _ => continue,
                         }
                     }
-- 
2.30.2