Repository navigation
DirEntry: warn about use potential races when fetching metadata or file_type - #163553
Conversation
| /// 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 |
There was a problem hiding this comment.
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:
rust/library/std/src/sys/fs/unix.rs
Lines 1059 to 1085 in 012c0bd
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
| /// 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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
The "in particular" clarifies a consequence of "calling symlink_metadata on the path".
69c11c3 to
90700c9
Compare
|
I guess I should look for a different reviewer. |
|
@bors r+ rollup |
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)
r? @the8472