Skip to content
Open
Changes from all 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
11 changes: 10 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,21 @@ 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);
b.field("write", &file_attr.perm().readonly());
Comment on lines +1032 to +1033
Copy link
Member

Choose a reason for hiding this comment

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

This does not look correct to me. If you use File::create to create and open a write-only file this will claim it's readable when it is not. And a file opened only for reads may not have the readonly attribute set on the file itself.

Copy link
Author

Choose a reason for hiding this comment

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

This does not look correct to me. If you use File::create to create and open a write-only file this will claim it's readable when it is not. And a file opened only for reads may not have the readonly attribute set on the file itself.

I will rewrite it for ACL
I thought that if I added this information, it would also not bad

Copy link
Member

Choose a reason for hiding this comment

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

I think what matters here is how the file was opened rather than permissions specifically. For example, on Linux this program:

fn main() {
    let f = std::fs::File::options()
        .read(false)
        .write(true)
        .create(true)
        .open("foo")
        .unwrap();
    println!("{f:?}",);
}

Produces this result:

File { fd: 3, path: "/playground/foo", read: false, write: true }

So read is false because you can't read from the opened File. But you can reopen the file and read it.

Copy link
Author

Choose a reason for hiding this comment

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

As i understand, you interested only in permission during using "session" of file descriptor, not permissions given by system (that i think will be also informative). So, if i add ACCESS_MASK (also add windows target binding from NtQueryObject and another impls) and add acl (via winapi GetSecurityInfo or other similar lower cost functions) to File struct as method file_acl (like file_attr) and finally bind to debug, would that be acceptable to you?


// Getting analogue of file mode (unix) using file attributes
Copy link
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
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
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