virtual_file_system/
hierarchy.rs

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