use crate::{Error, FileEntry, Result};
use std::path::Path;
use tokio::fs;
pub async fn read_dir(path: impl AsRef<Path>) -> Result<Vec<FileEntry>> {
let path = path.as_ref();
if !path.exists() {
return Err(Error::NotFound(path.to_path_buf()));
}
if !path.is_dir() {
return Err(Error::NotDirectory(path.to_path_buf()));
}
let mut entries = Vec::new();
let mut read_dir = fs::read_dir(path).await?;
while let Some(entry) = read_dir.next_entry().await? {
let path = entry.path();
let file_type = entry.file_type().await?;
let metadata = entry.metadata().await?;
let is_symlink = file_type.is_symlink();
let is_readonly = metadata.permissions().readonly();
let file_entry = FileEntry::new(
path,
metadata.is_dir(),
metadata.len(),
metadata.modified().ok(),
is_symlink,
is_readonly,
);
entries.push(file_entry);
}
Ok(entries)
}