[pbs-devel] [PATCH proxmox 1/1] partially fix #2915: proxmox-sys: scandir: stat if file_type is unknown

Wolfgang Bumiller w.bumiller at proxmox.com
Tue Jun 28 13:45:32 CEST 2022


On Tue, Jun 28, 2022 at 01:13:16PM +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)
> 
> Signed-off-by: Dominik Csapak <d.csapak at 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) {

Using `.contains()` here is wrong. The file type is not actually a
bitfield, but a number *within* a bitfield.

You mask out the type via `st_mode & S_IFMT` and can then use a `match`
statement to map it to `dir::Type`. (Which also makes for shorter code
;-) )

> +                    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





More information about the pbs-devel mailing list