Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion library/std/src/sys/fs/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1022,12 +1022,25 @@ impl FromRawHandle for File {

impl fmt::Debug for File {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// FIXME(#24570): add more info here (e.g., mode)
let mut b = f.debug_struct("File");
b.field("handle", &self.handle.as_raw_handle());
if let Ok(path) = get_path(self) {
b.field("path", &path);
}

if let Ok(file_attr) = self.file_attr() {
b.field("read", &true);
if file_attr.perm().readonly() {
b.field("write", &false);
} else {
b.field("write", &true);
}
Comment thread
WrldEngine marked this conversation as resolved.
Outdated

// Getting analogue of file mode (unix) using file attributes
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

File attributes aren't really an analogue of file mode because they don't have much to do with permissions. That would be ACLs. In any case, what we care most about is how the opened file handle can be used, rather than what the on-disk file has set.

E.g. can I write to this file using this particular file handle? Can I read from the file handle? Attributes don't tell you this.

Copy link
Copy Markdown
Author

@WrldEngine WrldEngine Oct 28, 2025

Choose a reason for hiding this comment

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

In windows files readable as default, if attrs set to the readonly of course you cannot write to them and use file handler for writing, i don't get the point about examples part, for debugging this infromation useful, than just check you opened the file in write or read mode i think so

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If the File was opened with write(false) you cannot write to it whatever the readonly attribute says.

// See https://learn.microsoft.com/windows/win32/fileio/file-attribute-constants
b.field("attrs", &file_attr.attributes);
}

b.finish()
}
}
Expand Down
Loading