public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox 1/1] partially fix #2915: proxmox-sys: scandir: stat if file_type is unknown
Date: Tue, 28 Jun 2022 13:13:16 +0200	[thread overview]
Message-ID: <20220628111318.2648586-2-d.csapak@proxmox.com> (raw)
In-Reply-To: <20220628111318.2648586-1-d.csapak@proxmox.com>

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





  reply	other threads:[~2022-06-28 11:13 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-28 11:13 [pbs-devel] [PATCH proxmox/proxmox-backup] fix #2915: stat when necessary Dominik Csapak
2022-06-28 11:13 ` Dominik Csapak [this message]
2022-06-28 11:45   ` [pbs-devel] [PATCH proxmox 1/1] partially fix #2915: proxmox-sys: scandir: stat if file_type is unknown Wolfgang Bumiller
2022-06-28 11:13 ` [pbs-devel] [PATCH proxmox-backup 1/2] adapt to changed callback signature of 'scandir' Dominik Csapak
2022-06-28 11:47   ` Fabian Grünbichler
2022-06-28 11:13 ` [pbs-devel] [PATCH proxmox-backup 2/2] partially fix #2915: 'stat' in case ReadDirEntry does not contain type Dominik Csapak
2022-06-28 11:47   ` Fabian Grünbichler
2022-06-28 11:49   ` Wolfgang Bumiller
2022-06-28 11:52     ` Wolfgang Bumiller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220628111318.2648586-2-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal