virtual_file_system/
hierarchy.rs1use exported_file_system::Permissions;
2use file_system::{Kind, Path};
4use task::TaskIdentifier;
5
6use crate::{Directory, Error, Result, VirtualFileSystem};
7
8pub fn ignore_already_exists_error<T>(result: Result<T>) -> Result<()> {
9 match result {
10 Ok(_) | Err(Error::AlreadyExists) => Ok(()),
11 Err(error) => Err(error),
12 }
13}
14
15pub async fn create_default_hierarchy(
17 virtual_file_system: &VirtualFileSystem,
18 task: TaskIdentifier,
19) -> Result<()> {
20 virtual_file_system
21 .set_permissions(task, &Path::ROOT, Permissions::DIRECTORY_DEFAULT)
22 .await?;
23
24 let paths = [
25 Path::SYSTEM,
26 Path::CONFIGURATION,
27 Path::SHARED_CONFIGURATION,
28 Path::DEVICES,
29 Path::USERS,
30 Path::DATA,
31 Path::SHARED_DATA,
32 Path::BINARIES,
33 Path::TEMPORARY,
34 Path::LOGS,
35 Path::NETWORK_DEVICES,
36 ];
37
38 for path in paths {
39 ignore_already_exists_error(virtual_file_system.create_directory(task, &path).await)?;
40 }
41
42 virtual_file_system
43 .set_permissions(task, &Path::DEVICES, Permissions::ALL_FULL)
44 .await?;
45
46 Ok(())
47}
48
49pub async fn clean_devices_in_directory(
50 virtual_file_system: &VirtualFileSystem,
51 task: TaskIdentifier,
52 path: &Path,
53) -> Result<()> {
54 for entry in Directory::open(virtual_file_system, task, path).await? {
56 if entry.kind != Kind::File {
57 continue;
58 }
59
60 let entry_path = path.append(&entry.name).unwrap();
61
62 let kind = virtual_file_system.get_statistics(&entry_path).await?.kind;
63
64 if kind != Kind::CharacterDevice && kind != Kind::BlockDevice {
65 continue;
66 }
67
68 match virtual_file_system.remove(task, &entry_path).await {
69 Ok(_) | Err(Error::InvalidIdentifier) => {}
70 Err(error) => {
71 return Err(error);
72 }
73 }
74 }
75
76 Ok(())
77}
78
79pub async fn clean_devices(
80 virtual_file_system: &VirtualFileSystem,
81 task: TaskIdentifier,
82) -> Result<()> {
83 clean_devices_in_directory(virtual_file_system, task, Path::DEVICES).await?;
84
85 clean_devices_in_directory(virtual_file_system, task, Path::BINARIES).await?;
86
87 Ok(())
88}