From 8c55fcd1f2ef3674d4bda4e38e2e7cacdd7cd5b8 Mon Sep 17 00:00:00 2001 From: Yuri Kunde Schlesner Date: Mon, 12 May 2014 02:31:22 -0300 Subject: Add `stat` method to `std::io::fs::File` to stat without a Path. The `FileStat` struct contained a `path` field, which was filled by the `stat` and `lstat` function. Since this field isn't in fact returned by the operating system (it was copied from the paths passed to the functions) it was removed, as in the `fstat` case we aren't working with a `Path`, but directly with a fd. If your code used the `path` field of `FileStat` you will now have to manually store the path passed to `stat` along with the returned struct. [breaking-change] --- src/libnative/io/file_unix.rs | 21 +++++++++++++++------ src/libnative/io/file_win32.rs | 14 ++++++++++---- 2 files changed, 25 insertions(+), 10 deletions(-) (limited to 'src/libnative') diff --git a/src/libnative/io/file_unix.rs b/src/libnative/io/file_unix.rs index 87225a10e76..c2b69483fa1 100644 --- a/src/libnative/io/file_unix.rs +++ b/src/libnative/io/file_unix.rs @@ -166,6 +166,14 @@ impl rtio::RtioFileStream for FileDesc { libc::ftruncate(self.fd(), offset as libc::off_t) })) } + + fn fstat(&mut self) -> IoResult { + let mut stat: libc::stat = unsafe { mem::uninit() }; + match retry(|| unsafe { libc::fstat(self.fd(), &mut stat) }) { + 0 => Ok(mkstat(&stat)), + _ => Err(super::last_error()), + } + } } impl rtio::RtioPipe for FileDesc { @@ -317,6 +325,10 @@ impl rtio::RtioFileStream for CFile { fn truncate(&mut self, offset: i64) -> Result<(), IoError> { self.flush().and_then(|()| self.fd.truncate(offset)) } + + fn fstat(&mut self) -> IoResult { + self.flush().and_then(|()| self.fd.fstat()) + } } impl Drop for CFile { @@ -455,9 +467,7 @@ pub fn link(src: &CString, dst: &CString) -> IoResult<()> { })) } -fn mkstat(stat: &libc::stat, path: &CString) -> io::FileStat { - let path = unsafe { CString::new(path.with_ref(|p| p), false) }; - +fn mkstat(stat: &libc::stat) -> io::FileStat { // FileStat times are in milliseconds fn mktime(secs: u64, nsecs: u64) -> u64 { secs * 1000 + nsecs / 1000000 } @@ -481,7 +491,6 @@ fn mkstat(stat: &libc::stat, path: &CString) -> io::FileStat { fn gen(_stat: &libc::stat) -> u64 { 0 } io::FileStat { - path: Path::new(path), size: stat.st_size as u64, kind: kind, perm: unsafe { @@ -508,7 +517,7 @@ fn mkstat(stat: &libc::stat, path: &CString) -> io::FileStat { pub fn stat(p: &CString) -> IoResult { let mut stat: libc::stat = unsafe { mem::uninit() }; match retry(|| unsafe { libc::stat(p.with_ref(|p| p), &mut stat) }) { - 0 => Ok(mkstat(&stat, p)), + 0 => Ok(mkstat(&stat)), _ => Err(super::last_error()), } } @@ -516,7 +525,7 @@ pub fn stat(p: &CString) -> IoResult { pub fn lstat(p: &CString) -> IoResult { let mut stat: libc::stat = unsafe { mem::uninit() }; match retry(|| unsafe { libc::lstat(p.with_ref(|p| p), &mut stat) }) { - 0 => Ok(mkstat(&stat, p)), + 0 => Ok(mkstat(&stat)), _ => Err(super::last_error()), } } diff --git a/src/libnative/io/file_win32.rs b/src/libnative/io/file_win32.rs index 707b0c0cf3f..d721e1d67f1 100644 --- a/src/libnative/io/file_win32.rs +++ b/src/libnative/io/file_win32.rs @@ -197,6 +197,14 @@ impl rtio::RtioFileStream for FileDesc { let _ = self.seek(orig_pos as i64, io::SeekSet); return ret; } + + fn fstat(&mut self) -> IoResult { + let mut stat: libc::stat = unsafe { mem::uninit() }; + match unsafe { libc::fstat(self.fd(), &mut stat) } { + 0 => Ok(mkstat(&stat)), + _ => Err(super::last_error()), + } + } } impl rtio::RtioPipe for FileDesc { @@ -471,8 +479,7 @@ pub fn link(src: &CString, dst: &CString) -> IoResult<()> { })) } -fn mkstat(stat: &libc::stat, path: &CString) -> io::FileStat { - let path = unsafe { CString::new(path.with_ref(|p| p), false) }; +fn mkstat(stat: &libc::stat) -> io::FileStat { let kind = match (stat.st_mode as c_int) & libc::S_IFMT { libc::S_IFREG => io::TypeFile, libc::S_IFDIR => io::TypeDirectory, @@ -483,7 +490,6 @@ fn mkstat(stat: &libc::stat, path: &CString) -> io::FileStat { }; io::FileStat { - path: Path::new(path), size: stat.st_size as u64, kind: kind, perm: unsafe { @@ -511,7 +517,7 @@ pub fn stat(p: &CString) -> IoResult { let mut stat: libc::stat = unsafe { mem::uninit() }; as_utf16_p(p.as_str().unwrap(), |up| { match unsafe { libc::wstat(up, &mut stat) } { - 0 => Ok(mkstat(&stat, p)), + 0 => Ok(mkstat(&stat)), _ => Err(super::last_error()), } }) -- cgit 1.4.1-3-g733a5