Skip to content

DirEntry: warn about use potential races when fetching metadata or file_type - #163553

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
RalfJung:dir-entry-docs
Oct 20, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
RalfJung:dir-entry-docs

Conversation

@RalfJung

Copy link
Copy Markdown
Member

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 30, 2026
@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Sep 30, 2026
Comment thread library/std/src/fs.rs Outdated
/// On Windows and most Unix platforms this function is free (no extra
/// system calls needed), but some Unix platforms may require the equivalent
/// call to `symlink_metadata` to learn about the target file type.
/// call to `symlink_metadata` to learn about the target file type. In particular, if the file

@asder8215 asder8215 Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to talk about certain platforms/filesystems that causes this behavior where this could fail or return the file type of a different file?

I'm asking because reading off the readdir(3) manpages it says that the dirent structure is defined as follows:

           struct dirent {
               ino_t          d_ino;       /* Inode number */
               off_t          d_off;       /* Not an offset; see below */
               unsigned short d_reclen;    /* Length of this record */
               unsigned char  d_type;      /* Type of file; not supported
                                              by all filesystem types */
               char           d_name[256]; /* Null-terminated filename */
           };

And that:

Currently, only some filesystems (among them: Btrfs, ext2, ext3, and ext4) have full support for returning the file type in .d_type. All applications must properly handle a return of DT_UNKNOWN.

Based on what DirEntry::file_type does underneath the hood for Unix:

pub fn file_type(&self) -> io::Result<FileType> {
// Use `entry.d_type` if available.
#[cfg(not(any(
target_os = "solaris",
target_os = "illumos",
target_os = "haiku",
target_os = "vxworks",
target_os = "aix",
target_os = "nto",
target_os = "qnx",
target_os = "vita",
target_os = "l4re",
)))]
match self.entry.d_type {
libc::DT_CHR => return Ok(FileType { mode: libc::S_IFCHR }),
libc::DT_FIFO => return Ok(FileType { mode: libc::S_IFIFO }),
libc::DT_LNK => return Ok(FileType { mode: libc::S_IFLNK }),
libc::DT_REG => return Ok(FileType { mode: libc::S_IFREG }),
libc::DT_SOCK => return Ok(FileType { mode: libc::S_IFSOCK }),
libc::DT_DIR => return Ok(FileType { mode: libc::S_IFDIR }),
libc::DT_BLK => return Ok(FileType { mode: libc::S_IFBLK }),
_ => {}
}
// Fall back to loading the metadata.
self.metadata().map(|m| m.file_type())
}

It's possible that some people wouldn't have to worry about this behavior of calling symlink_metadata (or rather lstat through DirEntry::metadata) since it retrieves this information from dirent's d_type field.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I noticed that DT_UNKNOWN is not handled here. Should we not return DT_UNKNOWN file type and let users handle that scenario or should this actually run the fallback DirEntry::metadata here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we not return DT_UNKNOWN file type and let users handle that scenario or should this actually run the fallback DirEntry::metadata here?

The entire point of this is to handle DT_UNKNOWN for the user.

Comment thread library/std/src/fs.rs Outdated
/// On Windows this function is cheap to call (no extra system calls
/// needed), but on Unix platforms this function is the equivalent of
/// calling `symlink_metadata` on the path.
/// calling `symlink_metadata` on the path. In particular, if the file has been renamed or

@asder8215 asder8215 Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel a little off with the standalone phrase "In particular" because the previous sentence mentions Windows and Unix platforms. Maybe you want to mention that this issue is specific to Unix here (e.g. "In particular on Unix,")? Since Windows doesn't invoke any extra syscalls, I'm assuming all the metadata info is already inside DirEntry so we wouldn't have to worry about this failing or returning metadata of a different file?

Could we also add a link to symlink_metadata just like how it's done earlier for std::fs::metadata?

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "in particular" clarifies a consequence of "calling symlink_metadata on the path".

@RalfJung

Copy link
Copy Markdown
Member Author

I guess I should look for a different reviewer.
@rustbot reroll

@rustbot rustbot assigned ChrisDenton and unassigned the8472 Oct 17, 2026
@ChrisDenton

Copy link
Copy Markdown
Member

@bors r+ rollup

@rust-bors

rust-bors Bot commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 90700c9 has been approved by ChrisDenton

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 20, 2026
rust-bors Bot pushed a commit that referenced this pull request Oct 20, 2026
Rollup of 8 pull requests

Successful merges:

 - #161163 (windows: stabilise inherit_handles)
 - #162615 (Add a `wasm32-none-component` target definition)
 - #163553 (DirEntry: warn about use potential races when fetching metadata or file_type)
 - #163618 ( Avoid ICE when closure expected signature contains non-lifetime bound variables  )
 - #163756 (xous: tcpstream fix timeout rounding with small and wrap large values)
 - #163818 (remove_dir_all: document and test symlink behavior more explicitly)
 - #163823 (enable fchmodat_works test under android)
 - #164596 (Update books)
@rust-bors
rust-bors Bot merged commit 99f508a into rust-lang:main Oct 20, 2026
13 checks passed
@rustbot rustbot added this to the 1.101.0 milestone Oct 20, 2026
rust-bors Bot pushed a commit that referenced this pull request Oct 20, 2026
Rollup merge of #163553 - RalfJung:dir-entry-docs, r=ChrisDenton

DirEntry: warn about use potential races when fetching metadata or file_type

r? @the8472
@RalfJung
RalfJung deleted the dir-entry-docs branch October 20, 2026 09:59
@clarfonthey clarfonthey added the A-filesystem Area: `std::fs` label Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-filesystem Area: `std::fs` S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants