]> git.lizzy.rs Git - rust.git/commitdiff
Implement Debug for File
authorChris Wong <lambda.fairy@gmail.com>
Sun, 19 Apr 2015 09:27:19 +0000 (21:27 +1200)
committerChris Wong <lambda.fairy@gmail.com>
Tue, 21 Apr 2015 05:13:36 +0000 (17:13 +1200)
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

src/liblibc/lib.rs
src/libstd/fs.rs
src/libstd/sys/unix/fs2.rs
src/libstd/sys/windows/fs2.rs

index f7e92d614746c909bc84e60da47cda5e224dea31..24a4a457224715c1eba2f7fd1e9c0f607a459d5f 100644 (file)
@@ -3296,6 +3296,8 @@ pub mod posix01 {
             pub const F_GETFL : c_int = 3;
             pub const F_SETFL : c_int = 4;
 
+            pub const O_ACCMODE : c_int = 3;
+
             pub const SIGTRAP : c_int = 5;
             pub const SIG_IGN: size_t = 1;
 
index 8af3311c42623fec29c616d3042cf896c5487de5..e8cdee9aca499a44022d9592c5f38f3badb83244 100644 (file)
@@ -19,6 +19,7 @@
 
 use core::prelude::*;
 
+use fmt;
 use io::{self, Error, ErrorKind, SeekFrom, Seek, Read, Write};
 use path::{Path, PathBuf};
 use sys::fs2 as fs_imp;
@@ -305,6 +306,12 @@ fn from_inner(f: fs_imp::File) -> File {
     }
 }
 
+impl fmt::Debug for File {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        self.inner.fmt(f)
+    }
+}
+
 #[stable(feature = "rust1", since = "1.0.0")]
 impl Read for File {
     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
index ac121f1c82e09ddedade920cbea8f2e6a8300935..8eb84b26f22f2f33d604b380cd57732b20f2664d 100644 (file)
@@ -13,6 +13,7 @@
 use os::unix::prelude::*;
 
 use ffi::{CString, CStr, OsString, OsStr};
+use fmt;
 use io::{self, Error, SeekFrom};
 use libc::{self, c_int, size_t, off_t, c_char, mode_t};
 use mem;
@@ -294,6 +295,54 @@ fn from_inner(fd: c_int) -> File {
     }
 }
 
+impl fmt::Debug for File {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        #[cfg(target_os = "linux")]
+        fn get_path(fd: c_int) -> Option<PathBuf> {
+            use string::ToString;
+            let mut p = PathBuf::from("/proc/self/fd");
+            p.push(&fd.to_string());
+            readlink(&p).ok()
+        }
+
+        #[cfg(not(target_os = "linux"))]
+        fn get_path(_fd: c_int) -> Option<PathBuf> {
+            // FIXME(#24570): implement this for other Unix platforms
+            None
+        }
+
+        #[cfg(target_os = "linux")]
+        fn get_mode(fd: c_int) -> Option<(bool, bool)> {
+            let mode = unsafe { libc::fcntl(fd, libc::F_GETFL) };
+            if mode == -1 {
+                return None;
+            }
+            match mode & libc::O_ACCMODE {
+                libc::O_RDONLY => Some((true, false)),
+                libc::O_RDWR => Some((true, true)),
+                libc::O_WRONLY => Some((false, true)),
+                _ => None
+            }
+        }
+
+        #[cfg(not(target_os = "linux"))]
+        fn get_mode(_fd: c_int) -> Option<(bool, bool)> {
+            // FIXME(#24570): implement this for other Unix platforms
+            None
+        }
+
+        let fd = self.0.raw();
+        let mut b = f.debug_struct("File").field("fd", &fd);
+        if let Some(path) = get_path(fd) {
+            b = b.field("path", &path);
+        }
+        if let Some((read, write)) = get_mode(fd) {
+            b = b.field("read", &read).field("write", &write);
+        }
+        b.finish()
+    }
+}
+
 pub fn mkdir(p: &Path) -> io::Result<()> {
     let p = try!(cstr(p));
     try!(cvt(unsafe { libc::mkdir(p.as_ptr(), 0o777) }));
index 9645c51ec0b2343f085b63c6135d9773a3828f60..b0515a712297c28fe3a40905f8bfe128c17895db 100644 (file)
@@ -14,6 +14,7 @@
 
 use default::Default;
 use ffi::{OsString, AsOsStr};
+use fmt;
 use io::{self, Error, SeekFrom};
 use libc::{self, HANDLE};
 use mem;
@@ -271,6 +272,15 @@ fn from_inner(handle: libc::HANDLE) -> File {
     }
 }
 
+impl fmt::Debug for File {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        // FIXME(#24570): add more info here (e.g. path, mode)
+        f.debug_struct("File")
+            .field("handle", &self.handle.raw())
+            .finish()
+    }
+}
+
 pub fn to_utf16(s: &Path) -> Vec<u16> {
     s.as_os_str().encode_wide().chain(Some(0).into_iter()).collect()
 }