| 1 |
use crate::errors::{Error, ErrorKind}; |
| 2 |
use std::ffi::OsString; |
| 3 |
use std::fs::{FileType, Metadata}; |
| 4 |
use std::io; |
| 5 |
use std::path::{Path, PathBuf}; |
| 6 |
use std::task::{ready, Context, Poll}; |
| 7 |
use tokio::fs; |
| 8 |
|
| 9 |
/// Wrapper for [`tokio::fs::read_dir`]. |
| 10 |
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))] |
| 11 |
pub async fn read_dir(path: impl AsRef<Path>) -> io::Result<ReadDir> { |
| 12 |
let path = path.as_ref(); |
| 13 |
let tokio = fs::read_dir(path) |
| 14 |
.await |
| 15 |
.map_err(|err| Error::build(err, ErrorKind::ReadDir, path))?; |
| 16 |
Ok(ReadDir { |
| 17 |
tokio, |
| 18 |
path: path.to_owned(), |
| 19 |
}) |
| 20 |
} |
| 21 |
|
| 22 |
/// Reads the entries in a directory. |
| 23 |
/// |
| 24 |
/// This is a wrapper around [`tokio::fs::ReadDir`]. |
| 25 |
#[derive(Debug)] |
| 26 |
#[must_use = "streams do nothing unless polled"] |
| 27 |
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))] |
| 28 |
pub struct ReadDir { |
| 29 |
tokio: fs::ReadDir, |
| 30 |
path: PathBuf, |
| 31 |
} |
| 32 |
|
| 33 |
impl ReadDir { |
| 34 |
/// Wrapper around [`tokio::fs::ReadDir::next_entry`]. |
| 35 |
pub async fn next_entry(&mut self) -> io::Result<Option<DirEntry>> { |
| 36 |
match self.tokio.next_entry().await { |
| 37 |
Ok(entry) => Ok(entry.map(|e| DirEntry { tokio: e })), |
| 38 |
Err(err) => Err(Error::build(err, ErrorKind::ReadDir, &self.path)), |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/// Wrapper around [`tokio::fs::ReadDir::poll_next_entry`]. |
| 43 |
pub fn poll_next_entry(&mut self, cx: &mut Context<'_>) -> Poll<io::Result<Option<DirEntry>>> { |
| 44 |
Poll::Ready(match ready!(self.tokio.poll_next_entry(cx)) { |
| 45 |
Ok(entry) => Ok(entry.map(|e| DirEntry { tokio: e })), |
| 46 |
Err(err) => Err(Error::build(err, ErrorKind::ReadDir, &self.path)), |
| 47 |
}) |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/// Entries returned by the [`ReadDir`] stream. |
| 52 |
/// |
| 53 |
/// This is a wrapper around [`tokio::fs::DirEntry`]. |
| 54 |
#[derive(Debug)] |
| 55 |
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))] |
| 56 |
pub struct DirEntry { |
| 57 |
tokio: fs::DirEntry, |
| 58 |
} |
| 59 |
|
| 60 |
impl DirEntry { |
| 61 |
/// Wrapper around [`tokio::fs::DirEntry::path`]. |
| 62 |
pub fn path(&self) -> PathBuf { |
| 63 |
self.tokio.path() |
| 64 |
} |
| 65 |
|
| 66 |
/// Wrapper around [`tokio::fs::DirEntry::file_name`]. |
| 67 |
pub fn file_name(&self) -> OsString { |
| 68 |
self.tokio.file_name() |
| 69 |
} |
| 70 |
|
| 71 |
/// Wrapper around [`tokio::fs::DirEntry::metadata`]. |
| 72 |
pub async fn metadata(&self) -> io::Result<Metadata> { |
| 73 |
self.tokio |
| 74 |
.metadata() |
| 75 |
.await |
| 76 |
.map_err(|err| Error::build(err, ErrorKind::Metadata, self.path())) |
| 77 |
} |
| 78 |
|
| 79 |
/// Wrapper around [`tokio::fs::DirEntry::file_type`]. |
| 80 |
pub async fn file_type(&self) -> io::Result<FileType> { |
| 81 |
self.tokio |
| 82 |
.file_type() |
| 83 |
.await |
| 84 |
.map_err(|err| Error::build(err, ErrorKind::Metadata, self.path())) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
#[cfg(unix)] |
| 89 |
impl DirEntry { |
| 90 |
/// Wrapper around [`tokio::fs::DirEntry::ino`]. |
| 91 |
pub fn ino(&self) -> u64 { |
| 92 |
self.tokio.ino() |
| 93 |
} |
| 94 |
} |