1pub(crate) mod dev_directory;
2pub(crate) mod mem;
3#[cfg(feature = "uhyve")]
4pub(crate) mod uhyve;
5#[cfg(feature = "virtio-fs")]
6pub(crate) mod virtio_fs;
7
8use alloc::borrow::ToOwned;
9use alloc::boxed::Box;
10use alloc::string::String;
11use alloc::sync::Arc;
12use alloc::vec::Vec;
13use core::fmt;
14use core::mem::MaybeUninit;
15use core::ops::BitAnd;
16
17use embedded_io::{Read, Write};
18use hermit_sync::{InterruptSpinMutex, OnceCell};
19use mem::MemDirectory;
20use num_enum::{IntoPrimitive, TryFromPrimitive};
21
22use crate::errno::Errno;
23use crate::executor::block_on;
24use crate::fd::{AccessPermission, Fd, ObjectInterface, OpenOption, insert_object, remove_object};
25use crate::io;
26use crate::time::{SystemTime, timespec};
27
28static FILESYSTEM: OnceCell<Filesystem> = OnceCell::new();
29
30static WORKING_DIRECTORY: InterruptSpinMutex<Option<String>> = InterruptSpinMutex::new(None);
31
32static UMASK: InterruptSpinMutex<AccessPermission> =
33 InterruptSpinMutex::new(AccessPermission::from_bits_retain(0o777));
34
35#[derive(Debug, Clone)]
36pub struct DirectoryEntry {
37 pub name: String,
38}
39
40impl DirectoryEntry {
41 pub fn new(name: String) -> Self {
42 Self { name }
43 }
44}
45
46#[derive(Copy, Clone, Debug, PartialEq, Eq)]
48pub(crate) enum NodeKind {
49 File,
51 Directory,
53}
54
55pub(crate) trait VfsNode: Send + Sync + fmt::Debug {
57 fn get_kind(&self) -> NodeKind;
59
60 fn get_file_attributes(&self) -> io::Result<FileAttr> {
62 Err(Errno::Nosys)
63 }
64
65 fn get_object(&self) -> io::Result<Arc<async_lock::RwLock<Fd>>> {
67 Err(Errno::Nosys)
68 }
69
70 fn traverse_mkdir(&self, _path: &str, _mode: AccessPermission) -> io::Result<()> {
72 Err(Errno::Nosys)
73 }
74
75 fn traverse_rmdir(&self, _path: &str) -> io::Result<()> {
77 Err(Errno::Nosys)
78 }
79
80 fn traverse_unlink(&self, _path: &str) -> io::Result<()> {
82 Err(Errno::Nosys)
83 }
84
85 fn traverse_readdir(&self, _path: &str) -> io::Result<Vec<DirectoryEntry>> {
87 Err(Errno::Nosys)
88 }
89
90 fn traverse_lstat(&self, _path: &str) -> io::Result<FileAttr> {
92 Err(Errno::Nosys)
93 }
94
95 fn traverse_stat(&self, _path: &str) -> io::Result<FileAttr> {
97 Err(Errno::Nosys)
98 }
99
100 fn traverse_mount(&self, _path: &str, _obj: Box<dyn VfsNode>) -> io::Result<()> {
102 Err(Errno::Nosys)
103 }
104
105 fn traverse_open(
107 &self,
108 _path: &str,
109 _option: OpenOption,
110 _mode: AccessPermission,
111 ) -> io::Result<Arc<async_lock::RwLock<Fd>>> {
112 Err(Errno::Nosys)
113 }
114
115 fn traverse_create_file(
117 &self,
118 _path: &str,
119 _data: &'static [u8],
120 _mode: AccessPermission,
121 ) -> io::Result<()> {
122 Err(Errno::Nosys)
123 }
124}
125
126#[derive(Clone)]
127pub(crate) struct DirectoryReader(Vec<DirectoryEntry>);
128
129impl DirectoryReader {
130 pub fn new(data: Vec<DirectoryEntry>) -> Self {
131 Self(data)
132 }
133}
134
135impl ObjectInterface for DirectoryReader {
136 async fn getdents(&self, buf: &mut [MaybeUninit<u8>]) -> io::Result<usize> {
137 let _buf = buf;
138 let _ = &self.0; unimplemented!()
140 }
141}
142
143#[derive(Debug)]
144pub(crate) struct Filesystem {
145 root: MemDirectory,
146}
147
148impl Filesystem {
149 pub fn new() -> Self {
150 Self {
151 root: MemDirectory::new(AccessPermission::from_bits(0o777).unwrap()),
152 }
153 }
154
155 pub fn open(
157 &self,
158 path: &str,
159 opt: OpenOption,
160 mode: AccessPermission,
161 ) -> io::Result<Arc<async_lock::RwLock<Fd>>> {
162 debug!("Open file {path} with {opt:?}");
163
164 let path = path.strip_prefix('/').unwrap_or(path);
165
166 self.root.traverse_open(path, opt, mode)
167 }
168
169 pub fn unlink(&self, path: &str) -> io::Result<()> {
171 debug!("Unlinking file {path}");
172
173 let path = path.strip_prefix('/').unwrap_or(path);
174
175 self.root.traverse_unlink(path)
176 }
177
178 pub fn rmdir(&self, path: &str) -> io::Result<()> {
180 debug!("Removing directory {path}");
181
182 let path = path.strip_prefix('/').unwrap_or(path);
183
184 self.root.traverse_rmdir(path)
185 }
186
187 pub fn mkdir(&self, path: &str, mode: AccessPermission) -> io::Result<()> {
189 debug!("Create directory {path}");
190
191 let path = path.strip_prefix('/').unwrap_or(path);
192
193 self.root.traverse_mkdir(path, mode)
194 }
195
196 pub fn opendir(&self, path: &str) -> io::Result<Arc<async_lock::RwLock<Fd>>> {
197 debug!("Open directory {path}");
198 Ok(Arc::new(async_lock::RwLock::new(
199 DirectoryReader::new(self.readdir(path)?).into(),
200 )))
201 }
202
203 pub fn readdir(&self, path: &str) -> io::Result<Vec<DirectoryEntry>> {
205 debug!("Readdir {path}");
206
207 let path = path.strip_prefix('/').unwrap_or(path);
208
209 self.root.traverse_readdir(path)
210 }
211
212 pub fn stat(&self, path: &str) -> io::Result<FileAttr> {
214 debug!("Getting stats {path}");
215
216 let path = path.strip_prefix('/').unwrap_or(path);
217
218 self.root.traverse_stat(path)
219 }
220
221 pub fn lstat(&self, path: &str) -> io::Result<FileAttr> {
223 debug!("Getting lstats {path}");
224
225 let path = path.strip_prefix('/').unwrap_or(path);
226
227 self.root.traverse_lstat(path)
228 }
229
230 pub fn mount(&self, path: &str, obj: Box<dyn VfsNode>) -> io::Result<()> {
232 debug!("Mounting {path}");
233
234 let path = path.strip_prefix('/').unwrap_or(path);
235
236 self.root.traverse_mount(path, obj)
237 }
238
239 pub fn create_file(
241 &self,
242 path: &str,
243 data: &'static [u8],
244 mode: AccessPermission,
245 ) -> io::Result<()> {
246 debug!("Create read-only file {path}");
247
248 let path = path.strip_prefix('/').unwrap_or(path);
249
250 self.root.traverse_create_file(path, data, mode)
251 }
252}
253
254#[repr(C)]
255#[derive(Debug, Default, Copy, Clone)]
256pub struct FileAttr {
257 pub st_dev: u64,
258 pub st_ino: u64,
259 pub st_nlink: u64,
260 pub st_mode: AccessPermission,
262 pub st_uid: u32,
264 pub st_gid: u32,
266 pub st_rdev: u64,
268 pub st_size: i64,
270 pub st_blksize: i64,
272 pub st_blocks: i64,
274 pub st_atim: timespec,
276 pub st_mtim: timespec,
278 pub st_ctim: timespec,
280}
281
282#[derive(TryFromPrimitive, IntoPrimitive, PartialEq, Eq, Clone, Copy, Debug)]
283#[repr(u8)]
284pub enum FileType {
285 Unknown = 0, Fifo = 1, CharacterDevice = 2, Directory = 4, BlockDevice = 6, RegularFile = 8, SymbolicLink = 10, Socket = 12, Whiteout = 14, }
295
296#[derive(TryFromPrimitive, IntoPrimitive, PartialEq, Eq, Clone, Copy, Debug)]
297#[repr(u8)]
298pub enum SeekWhence {
299 Set = 0,
300 Cur = 1,
301 End = 2,
302 Data = 3,
303 Hole = 4,
304}
305
306pub(crate) fn init() {
307 const VERSION: &str = env!("CARGO_PKG_VERSION");
308 const UTC_BUILT_TIME: &str = build_time::build_time_utc!();
309
310 let root_filesystem = Filesystem::new();
311
312 root_filesystem
313 .mkdir("/tmp", AccessPermission::from_bits(0o777).unwrap())
314 .expect("Unable to create /tmp");
315 root_filesystem
316 .mkdir("/proc", AccessPermission::from_bits(0o777).unwrap())
317 .expect("Unable to create /proc");
318
319 FILESYSTEM.set(root_filesystem).unwrap();
320
321 if let Ok(mut file) = File::create("/proc/version") {
322 if write!(file, "Hermit version {VERSION} # UTC {UTC_BUILT_TIME}").is_err() {
323 error!("Unable to write in /proc/version");
324 }
325 } else {
326 error!("Unable to create /proc/version");
327 }
328
329 *WORKING_DIRECTORY.lock() = Some("/tmp".to_owned());
330
331 #[cfg(feature = "virtio-fs")]
332 virtio_fs::init();
333
334 #[cfg(feature = "uhyve")]
335 if crate::env::is_uhyve() {
336 uhyve::init();
337 }
338
339 dev_directory::init();
340}
341
342pub fn create_file(name: &str, data: &'static [u8], mode: AccessPermission) -> io::Result<()> {
343 with_relative_filename(name, |name| {
344 FILESYSTEM
345 .get()
346 .ok_or(Errno::Inval)?
347 .create_file(name, data, mode)
348 })
349}
350
351pub fn remove_dir(path: &str) -> io::Result<()> {
353 with_relative_filename(path, |path| {
354 FILESYSTEM.get().ok_or(Errno::Inval)?.rmdir(path)
355 })
356}
357
358pub fn unlink(path: &str) -> io::Result<()> {
359 with_relative_filename(path, |path| {
360 FILESYSTEM.get().ok_or(Errno::Inval)?.unlink(path)
361 })
362}
363
364pub fn create_dir(path: &str, mode: AccessPermission) -> io::Result<()> {
366 let mask = *UMASK.lock();
367
368 with_relative_filename(path, |path| {
369 FILESYSTEM
370 .get()
371 .ok_or(Errno::Inval)?
372 .mkdir(path, mode.bitand(mask))
373 })
374}
375
376pub fn readdir(name: &str) -> io::Result<Vec<DirectoryEntry>> {
378 debug!("Read directory {name}");
379
380 with_relative_filename(name, |name| {
381 FILESYSTEM.get().ok_or(Errno::Inval)?.readdir(name)
382 })
383}
384
385pub fn read_stat(name: &str) -> io::Result<FileAttr> {
386 with_relative_filename(name, |name| {
387 FILESYSTEM.get().ok_or(Errno::Inval)?.stat(name)
388 })
389}
390
391pub fn read_lstat(name: &str) -> io::Result<FileAttr> {
392 with_relative_filename(name, |name| {
393 FILESYSTEM.get().ok_or(Errno::Inval)?.lstat(name)
394 })
395}
396
397fn with_relative_filename<F, T>(name: &str, callback: F) -> io::Result<T>
398where
399 F: FnOnce(&str) -> io::Result<T>,
400{
401 if name.starts_with("/") {
402 return callback(name);
403 }
404
405 let cwd = WORKING_DIRECTORY.lock();
406
407 let Some(cwd) = cwd.as_ref() else {
408 return Err(Errno::Badf);
410 };
411
412 let mut path = String::with_capacity(cwd.len() + name.len() + 1);
413 path.push_str(cwd);
414 path.push('/');
415 path.push_str(name);
416
417 callback(&path)
418}
419
420pub fn truncate(name: &str, size: usize) -> io::Result<()> {
421 with_relative_filename(name, |name| {
422 let fs = FILESYSTEM.get().ok_or(Errno::Inval)?;
423 let file = fs
424 .open(name, OpenOption::O_TRUNC, AccessPermission::empty())
425 .map_err(|_| Errno::Badf)?;
426
427 block_on(async { file.read().await.truncate(size).await }, None)
428 })
429}
430
431pub fn open(name: &str, flags: OpenOption, mode: AccessPermission) -> io::Result<RawFd> {
432 let mask = *UMASK.lock();
436
437 with_relative_filename(name, |name| {
438 debug!("Open {name}, {flags:?}, {mode:?}");
439
440 let fs = FILESYSTEM.get().ok_or(Errno::Inval)?;
441 let file = fs.open(name, flags, mode.bitand(mask))?;
442 let fd = insert_object(file)?;
443 Ok(fd)
444 })
445}
446
447pub fn get_cwd() -> io::Result<String> {
448 let cwd = WORKING_DIRECTORY.lock();
449 let cwd = cwd.as_ref().ok_or(Errno::Noent)?;
450 Ok(cwd.clone())
451}
452
453pub fn set_cwd(cwd: &str) -> io::Result<()> {
454 let mut working_dir = WORKING_DIRECTORY.lock();
457 if cwd.starts_with("/") {
458 *working_dir = Some(cwd.to_owned());
459 } else {
460 let working_dir = working_dir.as_mut().ok_or(Errno::Badf)?;
461 working_dir.push('/');
462 working_dir.push_str(cwd);
463 }
464
465 Ok(())
466}
467
468pub fn umask(new_mask: AccessPermission) -> AccessPermission {
469 let mut lock = UMASK.lock();
470 let old = *lock;
471 *lock = new_mask;
472 old
473}
474
475pub(crate) fn opendir(name: &str) -> io::Result<RawFd> {
477 let obj = FILESYSTEM.get().ok_or(Errno::Inval)?.opendir(name)?;
478 insert_object(obj)
479}
480
481use crate::fd::{self, RawFd};
482
483pub fn file_attributes(path: &str) -> io::Result<FileAttr> {
484 FILESYSTEM.get().ok_or(Errno::Inval)?.lstat(path)
485}
486
487#[allow(clippy::len_without_is_empty)]
488#[derive(Debug, Copy, Clone)]
489pub struct Metadata(FileAttr);
490
491impl Metadata {
492 pub fn len(&self) -> usize {
494 self.0.st_size.try_into().unwrap()
495 }
496
497 pub fn is_file(&self) -> bool {
499 self.0.st_mode.contains(AccessPermission::S_IFREG)
500 }
501
502 pub fn is_dir(&self) -> bool {
504 self.0.st_mode.contains(AccessPermission::S_IFDIR)
505 }
506
507 pub fn modified(&self) -> io::Result<SystemTime> {
509 Ok(SystemTime::from(self.0.st_mtim))
510 }
511
512 pub fn accessed(&self) -> io::Result<SystemTime> {
514 Ok(SystemTime::from(self.0.st_atim))
515 }
516}
517
518pub fn metadata(path: &str) -> io::Result<Metadata> {
520 Ok(Metadata(file_attributes(path)?))
521}
522
523#[derive(Debug)]
524pub struct File {
525 fd: RawFd,
526 path: String,
527}
528
529impl File {
530 fn create(path: &str) -> io::Result<File> {
534 let fd = open(
535 path,
536 OpenOption::O_CREAT | OpenOption::O_TRUNC | OpenOption::O_WRONLY,
537 AccessPermission::from_bits(0o666).unwrap(),
538 )?;
539
540 Ok(File {
541 fd,
542 path: path.to_owned(),
543 })
544 }
545
546 pub fn create_new(path: &str) -> io::Result<Self> {
552 let fd = open(
553 path,
554 OpenOption::O_CREAT | OpenOption::O_EXCL | OpenOption::O_RDWR,
555 AccessPermission::from_bits(0o666).unwrap(),
556 )?;
557
558 Ok(File {
559 fd,
560 path: path.to_owned(),
561 })
562 }
563
564 pub fn open(path: &str) -> io::Result<Self> {
566 let fd = open(
567 path,
568 OpenOption::O_RDWR,
569 AccessPermission::from_bits(0o666).unwrap(),
570 )?;
571
572 Ok(File {
573 fd,
574 path: path.to_owned(),
575 })
576 }
577
578 pub fn metadata(&self) -> io::Result<Metadata> {
579 metadata(&self.path)
580 }
581}
582
583impl embedded_io::ErrorType for File {
584 type Error = Errno;
585}
586
587impl Read for File {
588 fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
589 fd::read(self.fd, buf)
590 }
591}
592
593impl Write for File {
594 fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
595 fd::write(self.fd, buf)
596 }
597
598 fn flush(&mut self) -> Result<(), Self::Error> {
599 Ok(())
600 }
601}
602
603impl Drop for File {
604 fn drop(&mut self) {
605 if let Err(err) = remove_object(self.fd) {
606 error!("File::drop failed: {err}");
607 }
608 }
609}