mod dir;
mod file;
#[cfg(feature = "alloc")]
use alloc::{string::String, vec::Vec};
pub use ax_api::fs::{AxFilePermExt as PermissionsExt, AxFileTypeExt as FileTypeExt};
pub use self::{
dir::{DirBuilder, DirEntry, ReadDir},
file::{File, FileType, Metadata, OpenOptions, Permissions},
};
use crate::{StdResult, io::prelude::*};
#[cfg(feature = "alloc")]
pub fn read(path: &str) -> StdResult<Vec<u8>> {
let mut file = File::open(path)?;
let size = file.metadata().map(|m| m.len()).unwrap_or(0);
let mut bytes = Vec::with_capacity(size as usize);
file.read_to_end(&mut bytes)?;
Ok(bytes)
}
#[cfg(feature = "alloc")]
pub fn read_to_string(path: &str) -> StdResult<String> {
let mut file = File::open(path)?;
let size = file.metadata().map(|m| m.len()).unwrap_or(0);
let mut string = String::with_capacity(size as usize);
file.read_to_string(&mut string)?;
Ok(string)
}
pub fn write<C: AsRef<[u8]>>(path: &str, contents: C) -> StdResult {
File::create(path)?.write_all(contents.as_ref())?;
Ok(())
}
pub fn metadata(path: &str) -> StdResult<Metadata> {
File::open(path)?.metadata()
}
pub fn read_dir(path: &str) -> StdResult<ReadDir<'_>> {
ReadDir::new(path)
}
pub fn create_dir(path: &str) -> StdResult {
DirBuilder::new().create(path)
}
pub fn create_dir_all(path: &str) -> StdResult {
DirBuilder::new().recursive(true).create(path)
}
pub fn remove_dir(path: &str) -> StdResult {
ax_api::fs::ax_remove_dir(path)?;
Ok(())
}
pub fn remove_file(path: &str) -> StdResult {
ax_api::fs::ax_remove_file(path)?;
Ok(())
}
pub fn rename(old: &str, new: &str) -> StdResult {
ax_api::fs::ax_rename(old, new)?;
Ok(())
}