1use alloc::{borrow::ToOwned, vec::Vec};
2
3use exported_file_system::Permissions;
4use file_system::{Kind, Path};
5use task::TaskIdentifier;
6
7use crate::{Directory, IoErrorKind, Result, VirtualFileSystem};
8
9pub fn ignore_already_exists_error<T>(result: Result<T>) -> Result<()> {
10 match result {
11 Ok(_) | Err(IoErrorKind::AlreadyExists) => Ok(()),
12 Err(error) => Err(error),
13 }
14}
15
16async fn set_permissions_if_different(
17 virtual_file_system: &VirtualFileSystem,
18 task: TaskIdentifier,
19 path: &Path,
20 permissions: Permissions,
21) -> Result<()> {
22 if virtual_file_system
23 .get_statistics_for_task(task, &path)
24 .await?
25 .permissions
26 != permissions
27 {
28 virtual_file_system
29 .set_permissions(task, path, permissions)
30 .await?;
31 }
32
33 Ok(())
34}
35
36pub async fn create_default_hierarchy(
38 virtual_file_system: &VirtualFileSystem,
39 task: TaskIdentifier,
40) -> Result<()> {
41 set_permissions_if_different(
42 virtual_file_system,
43 task,
44 Path::ROOT,
45 Permissions::DIRECTORY_DEFAULT,
46 )
47 .await?;
48
49 let paths = [
50 Path::SYSTEM,
51 Path::SYSTEM_ACCOUNTS,
52 Path::SYSTEM_ACCOUNT_USERS,
53 Path::SYSTEM_ACCOUNT_GROUPS,
54 Path::SYSTEM_EXECUTABLES,
55 Path::SYSTEM_LIBRARIES,
56 Path::SYSTEM_SERVICES,
57 Path::CONFIGURATION,
58 Path::SYSTEM_CONFIGURATION,
59 Path::PACKAGE_CONFIGURATION,
60 Path::APPLICATION_CONFIGURATION,
61 Path::SHARED_CONFIGURATION,
62 Path::DEVICES,
63 Path::AUDIO_DEVICES,
64 Path::COMPUTE_DEVICES,
65 Path::GRAPHICS_DEVICES,
66 Path::INPUT_DEVICES,
67 Path::NETWORK_DEVICES,
68 Path::RANDOM_DEVICES,
69 Path::STANDARD_IO_DEVICES,
70 Path::STORAGE_DEVICES,
71 Path::TIME_DEVICES,
72 Path::APPLICATIONS,
73 Path::APPLICATION_EXECUTABLES,
74 Path::APPLICATION_LIBRARIES,
75 Path::APPLICATION_BUNDLES,
76 Path::DATA,
77 Path::SYSTEM_DATA,
78 Path::SYSTEM_LOGS,
79 Path::APPLICATION_DATA,
80 Path::SHARED_DATA,
81 Path::USERS,
82 Path::TEMPORARY,
83 Path::TEMPORARY_CACHE,
84 Path::TEMPORARY_SESSIONS,
85 ];
86
87 for path in paths {
88 match virtual_file_system.create_directory(task, &path).await {
89 Ok(()) => {}
90 Err(IoErrorKind::AlreadyExists) => {
91 if virtual_file_system
92 .get_statistics_for_task(task, &path)
93 .await?
94 .kind
95 != Kind::Directory
96 {
97 return Err(IoErrorKind::NotADirectory);
98 }
99 }
100 Err(error) => return Err(error),
101 }
102 }
103
104 set_permissions_if_different(
105 virtual_file_system,
106 task,
107 Path::DEVICES,
108 Permissions::ALL_FULL,
109 )
110 .await?;
111
112 for path in [Path::APPLICATION_DATA, Path::APPLICATION_CONFIGURATION] {
115 set_permissions_if_different(virtual_file_system, task, path, Permissions::USER_FULL)
116 .await?;
117 }
118
119 for path in [Path::SHARED_DATA, Path::TEMPORARY, Path::TEMPORARY_CACHE] {
120 set_permissions_if_different(
121 virtual_file_system,
122 task,
123 path,
124 Permissions::SHARED_DIRECTORY,
125 )
126 .await?;
127 }
128
129 Ok(())
130}
131
132pub async fn clear_temporary(
135 virtual_file_system: &VirtualFileSystem,
136 task: TaskIdentifier,
137) -> Result<()> {
138 clear_directory_contents(
141 virtual_file_system,
142 task,
143 Path::TEMPORARY,
144 &[
145 Path::TEMPORARY_CACHE.as_str(),
146 Path::TEMPORARY_SESSIONS.as_str(),
147 ],
148 )
149 .await
150}
151
152async fn clear_directory_contents(
153 virtual_file_system: &VirtualFileSystem,
154 task: TaskIdentifier,
155 path: &Path,
156 kept: &[&str],
157) -> Result<()> {
158 let mut pending = Vec::new();
161 pending.push((path.to_owned(), false));
162
163 while let Some((directory_path, remove)) = pending.pop() {
164 if remove {
165 virtual_file_system.remove(task, &directory_path).await?;
166 continue;
167 }
168
169 let mut directory = Directory::open(virtual_file_system, task, &directory_path).await?;
170 if directory_path.as_str() != path.as_str() && !kept.contains(&directory_path.as_str()) {
171 pending.push((directory_path.clone(), true));
172 }
173 while let Some(entry) = directory.read().await? {
174 if entry.name == "." || entry.name == ".." {
176 continue;
177 }
178 let entry_path = entry
179 .join_path(&directory_path)
180 .ok_or(IoErrorKind::InvalidPath)?;
181 pending.push((entry_path, entry.kind != Kind::Directory));
182 }
183 directory.close(virtual_file_system).await?;
185 }
186
187 Ok(())
188}
189
190pub async fn clean_devices_in_directory(
191 virtual_file_system: &VirtualFileSystem,
192 task: TaskIdentifier,
193 path: &Path,
194) -> Result<()> {
195 for entry in Directory::open(virtual_file_system, task, path).await? {
196 if entry.kind != Kind::File {
197 continue;
198 }
199
200 let entry_path = path.append(&entry.name).ok_or(IoErrorKind::InvalidPath)?;
201 let kind = virtual_file_system.get_statistics(&entry_path).await?.kind;
202
203 if kind != Kind::CharacterDevice && kind != Kind::BlockDevice {
204 continue;
205 }
206
207 match virtual_file_system.remove(task, &entry_path).await {
208 Ok(()) | Err(IoErrorKind::BadDescriptor) => {}
214 Err(error) => return Err(error),
215 }
216 }
217
218 Ok(())
219}
220
221pub async fn clean_devices(
222 virtual_file_system: &VirtualFileSystem,
223 task: TaskIdentifier,
224) -> Result<()> {
225 clean_devices_in_directory(virtual_file_system, task, Path::DEVICES).await
226}
227
228#[cfg(test)]
229mod tests {
230 use super::*;
231
232 #[test]
233 fn ignore_already_exists_error_only_suppresses_existing_paths() {
234 assert_eq!(
235 ignore_already_exists_error::<()>(Err(IoErrorKind::AlreadyExists)),
236 Ok(())
237 );
238 assert_eq!(
239 ignore_already_exists_error::<()>(Err(IoErrorKind::PermissionDenied)),
240 Err(IoErrorKind::PermissionDenied)
241 );
242 }
243}