diff options
| author | Yuri Kunde Schlesner <yuriks@yuriks.net> | 2014-05-12 02:31:22 -0300 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2014-05-12 19:52:29 -0700 |
| commit | 8c55fcd1f2ef3674d4bda4e38e2e7cacdd7cd5b8 (patch) | |
| tree | 26e430f86234c4be047df9369327942a4d0861c7 /src/libstd | |
| parent | f096516d2b5e0ac1a634742a11520dff4b59014b (diff) | |
| download | rust-8c55fcd1f2ef3674d4bda4e38e2e7cacdd7cd5b8.tar.gz rust-8c55fcd1f2ef3674d4bda4e38e2e7cacdd7cd5b8.zip | |
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]
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/io/fs.rs | 20 | ||||
| -rw-r--r-- | src/libstd/io/mod.rs | 3 | ||||
| -rw-r--r-- | src/libstd/rt/rtio.rs | 1 |
3 files changed, 15 insertions, 9 deletions
diff --git a/src/libstd/io/fs.rs b/src/libstd/io/fs.rs index 125b4ddad88..a497ffd40a0 100644 --- a/src/libstd/io/fs.rs +++ b/src/libstd/io/fs.rs @@ -214,6 +214,11 @@ impl File { pub fn eof(&self) -> bool { self.last_nread == 0 } + + /// Queries information about the underlying file. + pub fn stat(&mut self) -> IoResult<FileStat> { + self.fd.fstat() + } } /// Unlink a file from the underlying filesystem. @@ -887,9 +892,12 @@ mod test { let tmpdir = tmpdir(); let filename = &tmpdir.join("file_stat_correct_on_is_file.txt"); { - let mut fs = File::open_mode(filename, Open, ReadWrite); + let mut fs = check!(File::open_mode(filename, Open, ReadWrite)); let msg = "hw"; fs.write(msg.as_bytes()).unwrap(); + + let fstat_res = check!(fs.stat()); + assert_eq!(fstat_res.kind, io::TypeFile); } let stat_res_fn = check!(stat(filename)); assert_eq!(stat_res_fn.kind, io::TypeFile); @@ -1228,12 +1236,12 @@ mod test { check!(file.fsync()); // Do some simple things with truncation - assert_eq!(check!(stat(&path)).size, 3); + assert_eq!(check!(file.stat()).size, 3); check!(file.truncate(10)); - assert_eq!(check!(stat(&path)).size, 10); + assert_eq!(check!(file.stat()).size, 10); check!(file.write(bytes!("bar"))); check!(file.fsync()); - assert_eq!(check!(stat(&path)).size, 10); + assert_eq!(check!(file.stat()).size, 10); assert_eq!(check!(File::open(&path).read_to_end()), (Vec::from_slice(bytes!("foobar", 0, 0, 0, 0)))); @@ -1241,10 +1249,10 @@ mod test { // Ensure that the intermediate zeroes are all filled in (we're seeked // past the end of the file). check!(file.truncate(2)); - assert_eq!(check!(stat(&path)).size, 2); + assert_eq!(check!(file.stat()).size, 2); check!(file.write(bytes!("wut"))); check!(file.fsync()); - assert_eq!(check!(stat(&path)).size, 9); + assert_eq!(check!(file.stat()).size, 9); assert_eq!(check!(File::open(&path).read_to_end()), (Vec::from_slice(bytes!("fo", 0, 0, 0, 0, "wut")))); drop(file); diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 37edab99915..36352542590 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -228,7 +228,6 @@ use ops::{BitOr, BitAnd, Sub}; use option::{Option, Some, None}; use os; use owned::Box; -use path::Path; use result::{Ok, Err, Result}; use slice::{Vector, MutableVector, ImmutableVector}; use str::{StrSlice, StrAllocating}; @@ -1516,8 +1515,6 @@ pub enum FileType { /// ``` #[deriving(Hash)] pub struct FileStat { - /// The path that this stat structure is describing - pub path: Path, /// The size of the file, in bytes pub size: u64, /// The kind of file this path points to (directory, file, pipe, etc.) diff --git a/src/libstd/rt/rtio.rs b/src/libstd/rt/rtio.rs index bc3a483f30d..d23d327d558 100644 --- a/src/libstd/rt/rtio.rs +++ b/src/libstd/rt/rtio.rs @@ -269,6 +269,7 @@ pub trait RtioFileStream { fn fsync(&mut self) -> IoResult<()>; fn datasync(&mut self) -> IoResult<()>; fn truncate(&mut self, offset: i64) -> IoResult<()>; + fn fstat(&mut self) -> IoResult<FileStat>; } pub trait RtioProcess { |
