diff --git a/crates/nit/src/system/mod.rs b/crates/nit/src/system/mod.rs index 7be0b8e..956abb2 100644 --- a/crates/nit/src/system/mod.rs +++ b/crates/nit/src/system/mod.rs @@ -7,7 +7,11 @@ pub mod syscall; pub fn dmesg(value: impl std::fmt::Display) { let timespec = syscall::clock_gettime(libc::CLOCK_BOOTTIME).unwrap(); for line in value.to_string().lines() { - eprintln!("[{: >5}.{}] {line}", timespec.tv_sec, timespec.tv_nsec / 1000); + eprintln!( + "[{: >5}.{}] {line}", + timespec.tv_sec, + timespec.tv_nsec / 1000 + ); } } @@ -69,6 +73,13 @@ impl Mount { } pub fn mount_self(&self) -> Result<()> { + if !std::fs::exists(&self.target).context(format_args!( + "could not check if path exists: {path}", + path = self.target.display() + ))? { + syscall::mkdir(&self.target)?; + } + dmesg(format!( "Mounting {source} to {target}", source = self.source.display(), diff --git a/crates/nit/src/system/syscall.rs b/crates/nit/src/system/syscall.rs index 72dc32d..74cc0d9 100644 --- a/crates/nit/src/system/syscall.rs +++ b/crates/nit/src/system/syscall.rs @@ -89,6 +89,21 @@ pub fn mount( } } +pub fn mkdir(path: impl AsRef) -> Result<()> { + let path = path.as_ref(); + let path_cs = CString::new(path.as_os_str().as_encoded_bytes()) + .context(format_args!("bad path: {path}", path = path.display()))?; + + match unsafe { libc::mkdir(path_cs.as_ptr(), 0o755) } { + 0 => Ok(()), + -1 => ctx_os_error(format_args!( + "error calling mkdir({path}, 0o755)", + path = path.display() + )), + n => unreachable!("mount() syscall returned bad value: {n}"), + } +} + pub fn freopen(path: impl AsRef, mode: impl AsRef, fd: &impl AsRawFd) -> Result<()> { let path = path.as_ref(); let mode = mode.as_ref();