Skip to main content

rspack_fs/
write.rs

1use std::fmt::Debug;
2
3use rspack_paths::Utf8Path;
4
5use super::{FileMetadata, Result};
6use crate::file_metadata::FilePermissions;
7
8#[async_trait::async_trait]
9pub trait WritableFileSystem: Debug + Send + Sync {
10  /// Creates a new, empty directory at the provided path.
11  ///
12  /// NOTE: If a parent of the given path doesn’t exist, this function is supposed to return an error.
13  /// To create a directory and all its missing parents at the same time, use the [`create_dir_all`] function.
14  ///
15  /// Error:
16  /// This function is supposed to return an error in the following situations, but is not limited to just these cases:
17  /// - User lacks permissions to create directory at path.
18  /// - A parent of the given path doesn’t exist. (To create a directory and all its missing parents at the same time, use the create_dir_all function.)
19  /// - Path already exists.
20  async fn create_dir(&self, dir: &Utf8Path) -> Result<()>;
21
22  /// Recursively create a directory and all of its parent components if they are missing.
23  async fn create_dir_all(&self, dir: &Utf8Path) -> Result<()>;
24
25  /// Write a slice as the entire contents of a file.
26  /// This function will create a file if it does not exist, and will entirely replace its contents if it does.
27  async fn write(&self, file: &Utf8Path, data: &[u8]) -> Result<()>;
28
29  /// Removes a file from the filesystem.
30  async fn remove_file(&self, file: &Utf8Path) -> Result<()>;
31
32  /// Removes a directory at this path, after removing all its contents. Use carefully.
33  async fn remove_dir_all(&self, dir: &Utf8Path) -> Result<()>;
34
35  /// Returns a list of all files in a directory.
36  async fn read_dir(&self, dir: &Utf8Path) -> Result<Vec<String>>;
37
38  /// Read the entire contents of a file into a bytes vector.
39  async fn read_file(&self, file: &Utf8Path) -> Result<Vec<u8>>;
40
41  async fn stat(&self, file: &Utf8Path) -> Result<FileMetadata>;
42
43  /// See [std::fs::set_permissions]
44  async fn set_permissions(&self, _path: &Utf8Path, _perm: FilePermissions) -> Result<()>;
45}