1mod dir;
4mod file;
5
6use crate::io::{self, prelude::*};
7
8#[cfg(feature = "alloc")]
9use alloc::{string::String, vec::Vec};
10
11pub use self::dir::{DirBuilder, DirEntry, ReadDir};
12pub use self::file::{File, FileType, Metadata, OpenOptions, Permissions};
13
14#[cfg(feature = "alloc")]
16pub fn read(path: &str) -> io::Result<Vec<u8>> {
17 let mut file = File::open(path)?;
18 let size = file.metadata().map(|m| m.len()).unwrap_or(0);
19 let mut bytes = Vec::with_capacity(size as usize);
20 file.read_to_end(&mut bytes)?;
21 Ok(bytes)
22}
23
24#[cfg(feature = "alloc")]
26pub fn read_to_string(path: &str) -> io::Result<String> {
27 let mut file = File::open(path)?;
28 let size = file.metadata().map(|m| m.len()).unwrap_or(0);
29 let mut string = String::with_capacity(size as usize);
30 file.read_to_string(&mut string)?;
31 Ok(string)
32}
33
34pub fn write<C: AsRef<[u8]>>(path: &str, contents: C) -> io::Result<()> {
36 File::create(path)?.write_all(contents.as_ref())
37}
38
39pub fn metadata(path: &str) -> io::Result<Metadata> {
42 File::open(path)?.metadata()
43}
44
45pub fn read_dir(path: &str) -> io::Result<ReadDir> {
47 ReadDir::new(path)
48}
49
50pub fn create_dir(path: &str) -> io::Result<()> {
52 DirBuilder::new().create(path)
53}
54
55pub fn create_dir_all(path: &str) -> io::Result<()> {
58 DirBuilder::new().recursive(true).create(path)
59}
60
61pub fn remove_dir(path: &str) -> io::Result<()> {
63 arceos_api::fs::ax_remove_dir(path)
64}
65
66pub fn remove_file(path: &str) -> io::Result<()> {
68 arceos_api::fs::ax_remove_file(path)
69}
70
71pub fn rename(old: &str, new: &str) -> io::Result<()> {
76 arceos_api::fs::ax_rename(old, new)
77}