diff --git a/tokio-fs/src/lib.rs b/tokio-fs/src/lib.rs index 9d7e46618..71fc35af5 100644 --- a/tokio-fs/src/lib.rs +++ b/tokio-fs/src/lib.rs @@ -31,11 +31,33 @@ pub use stdin::{stdin, Stdin}; pub use stdout::{stdout, Stdout}; pub use stderr::{stderr, Stderr}; -use futures::Poll; +use futures::{Future, Poll}; use futures::Async::*; +use std::fs::{self, Metadata}; use std::io; use std::io::ErrorKind::{Other, WouldBlock}; +use std::path::PathBuf; + +/// Queries the file system metadata for a path +pub fn metadata(path: PathBuf) -> MetadataFuture { + MetadataFuture { path } +} + +/// Future returned by `metadata` +#[derive(Debug)] +pub struct MetadataFuture { + path: PathBuf, +} + +impl Future for MetadataFuture { + type Item = Metadata; + type Error = io::Error; + + fn poll(&mut self) -> Poll { + blocking_io(|| fs::metadata(&self.path)) + } +} fn blocking_io(f: F) -> Poll where F: FnOnce() -> io::Result, diff --git a/tokio-fs/tests/file.rs b/tokio-fs/tests/file.rs index 690623682..b512e5f50 100644 --- a/tokio-fs/tests/file.rs +++ b/tokio-fs/tests/file.rs @@ -73,3 +73,33 @@ fn read_write() { }) }); } + +#[test] +fn metadata() { + let dir = TempDir::new("tokio-fs-tests").unwrap(); + let file_path = dir.path().join("metadata.txt"); + + let pool = Builder::new().pool_size(1).build(); + + let (tx, rx) = oneshot::channel(); + + pool.spawn({ + let file_path = file_path.clone(); + let file_path2 = file_path.clone(); + let file_path3 = file_path.clone(); + + tokio_fs::metadata(file_path) + .then(|r| { + let _ = r.err().unwrap(); + Ok(()) + }) + .and_then(|_| File::create(file_path2)) + .and_then(|_| tokio_fs::metadata(file_path3)) + .then(|r| { + assert!(r.unwrap().is_file()); + tx.send(()) + }) + }); + + rx.wait().unwrap(); +}