Skip to main content

ax_fs/api/
mod.rs

1// Copyright 2025 The Axvisor Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! [`std::fs`]-like high-level filesystem manipulation operations.
16
17mod dir;
18mod file;
19
20use alloc::{string::String, vec::Vec};
21
22use ax_io::{self as io, prelude::*};
23
24pub use self::{
25    dir::{DirBuilder, DirEntry, ReadDir},
26    file::{File, FileType, Metadata, OpenOptions, Permissions},
27};
28
29/// Returns an iterator over the entries within a directory.
30pub fn read_dir(path: &str) -> io::Result<ReadDir<'_>> {
31    ReadDir::new(path)
32}
33
34/// Returns the canonical, absolute form of a path with all intermediate
35/// components normalized.
36pub fn canonicalize(path: &str) -> io::Result<String> {
37    crate::root::absolute_path(path)
38}
39
40/// Returns the current working directory as a [`String`].
41pub fn current_dir() -> io::Result<String> {
42    crate::root::current_dir()
43}
44
45/// Changes the current working directory to the specified path.
46pub fn set_current_dir(path: &str) -> io::Result<()> {
47    crate::root::set_current_dir(path)
48}
49
50/// Read the entire contents of a file into a bytes vector.
51pub fn read(path: &str) -> io::Result<Vec<u8>> {
52    let mut file = File::open(path)?;
53    let size = file.metadata().map(|m| m.len()).unwrap_or(0);
54    let mut bytes = Vec::with_capacity(size as usize);
55    file.read_to_end(&mut bytes)?;
56    Ok(bytes)
57}
58
59/// Read the entire contents of a file into a string.
60pub fn read_to_string(path: &str) -> io::Result<String> {
61    let mut file = File::open(path)?;
62    let size = file.metadata().map(|m| m.len()).unwrap_or(0);
63    let mut string = String::with_capacity(size as usize);
64    file.read_to_string(&mut string)?;
65    Ok(string)
66}
67
68/// Write a slice as the entire contents of a file.
69pub fn write<C: AsRef<[u8]>>(path: &str, contents: C) -> io::Result<()> {
70    File::create(path)?.write_all(contents.as_ref())
71}
72
73/// Given a path, query the file system to get information about a file,
74/// directory, etc.
75pub fn metadata(path: &str) -> io::Result<Metadata> {
76    File::open(path)?.metadata()
77}
78
79/// Creates a new, empty directory at the provided path.
80pub fn create_dir(path: &str) -> io::Result<()> {
81    DirBuilder::new().create(path)
82}
83
84/// Recursively create a directory and all of its parent components if they
85/// are missing.
86pub fn create_dir_all(path: &str) -> io::Result<()> {
87    DirBuilder::new().recursive(true).create(path)
88}
89
90/// Removes an empty directory.
91pub fn remove_dir(path: &str) -> io::Result<()> {
92    crate::root::remove_dir(None, path)
93}
94
95/// Removes a file from the filesystem.
96pub fn remove_file(path: &str) -> io::Result<()> {
97    crate::root::remove_file(None, path)
98}
99
100/// Rename a file or directory to a new name.
101/// Delete the original file if `old` already exists.
102///
103/// This only works then the new path is in the same mounted fs.
104pub fn rename(old: &str, new: &str) -> io::Result<()> {
105    crate::root::rename(old, new)
106}