use crate::FileMetadata; use std::io; use std::path::Path; #[cfg(windows)] use std::time::SystemTime; #[cfg(windows)] use std::time::UNIX_EPOCH; #[cfg(unix)] mod unix; #[cfg(windows)] mod windows; #[cfg(unix)] use unix as imp; #[cfg(windows)] use windows as imp; pub(crate) async fn open_file(path: &Path) -> io::Result { imp::open_file(path.to_path_buf()).await } pub(crate) async fn write_file(path: &Path, contents: Vec) -> io::Result<()> { imp::write_file(path.to_path_buf(), contents).await } #[cfg(unix)] pub(crate) async fn metadata(path: &Path) -> io::Result { imp::metadata(path.to_path_buf()).await } #[cfg(windows)] pub(crate) async fn metadata(path: &Path) -> io::Result { imp::metadata(path.to_path_buf()) .await .map(|metadata| FileMetadata { is_directory: metadata.is_dir(), is_file: metadata.is_file(), is_symlink: false, size: metadata.len(), created_at_ms: metadata.created().ok().map_or(0, system_time_to_unix_ms), modified_at_ms: metadata.modified().ok().map_or(0, system_time_to_unix_ms), }) } #[cfg(windows)] fn system_time_to_unix_ms(time: SystemTime) -> i64 { time.duration_since(UNIX_EPOCH) .ok() .and_then(|duration| i64::try_from(duration.as_millis()).ok()) .unwrap_or(0) } pub(crate) async fn create_directory(path: &Path, recursive: bool) -> io::Result<()> { imp::create_directory(path.to_path_buf(), recursive).await } pub(crate) async fn remove(path: &Path, recursive: bool, force: bool) -> io::Result<()> { imp::remove(path.to_path_buf(), recursive, force).await }