Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File does not implement Debug #24570

Closed
steveklabnik opened this issue Apr 18, 2015 · 9 comments
Closed

File does not implement Debug #24570

steveklabnik opened this issue Apr 18, 2015 · 9 comments

Comments

@steveklabnik
Copy link
Member

All types should, but it doesn't:

the trait `core::fmt::Debug` is not implemented for the type `std::fs::File` [E0277]
@sfackler
Copy link
Member

We can easily slap one on that just prints the file descriptor/HANDLE, but it would ideally include the path of the open file. It looks like it'll be a bit of a pain to get.

Windows can get it via either GetFinalPathNameByHandle or GetFileInformationByHandleEx. It does look like both of those require Vista, but this page has a workaround for earlier versions of Windows: https://msdn.microsoft.com/en-us/library/windows/desktop/aa366789%28v=vs.85%29.aspx.

OSX can grab it by passing F_GETPATH to fnctl: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/fcntl.2.html.

Looks like Linux may have to poke around in /proc/pid/fds/ unfortunately.

@sfackler
Copy link
Member

It'd also be nice to print if the file's writable or read only.

@lambda-fairy
Copy link
Contributor

On Linux, you can read the link at /proc/self/fd/<FD>. The destination of that symlink is the name of the file.

That shouldn't be too hard, given that we have a read_link function in std.

@lambda-fairy
Copy link
Contributor

I'm working on this.

@nagisa
Copy link
Member

nagisa commented Apr 19, 2015

Relying on procfs is bad, because it might not be mounted or be mounted at a non-standard location.

@lambda-fairy
Copy link
Contributor

@nagisa We aren't really relying on it here. If /proc isn't present, the code would just not print a path. No biggie.

bors added a commit that referenced this issue Apr 21, 2015
This patch adds a `Debug` impl for `std::fs::File`.

On all platforms (Unix and Windows) it shows the file descriptor.

On Linux, it displays the path and access mode as well.

Ideally we should show the path/mode for all platforms, not just Linux,
but this will do for now.

cc #24570
@lambda-fairy
Copy link
Contributor

Currently the Debug impl gives path and access mode information on Linux only.

If anyone wants to flesh out the Windows or OS X or BSD code, then feel free.

@steveklabnik
Copy link
Member Author

use std::fs::File;

fn main() {
    let f = File::open("hello.rs");
    println!("{:?}", f);
}

prints

Ok(File { fd: 3, path: "/home/steve/tmp/hello.rs", read: true, write: false })

@Eh2406
Copy link
Contributor

Eh2406 commented Sep 3, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants