test: run mkdir before mounting if necessary

This commit is contained in:
Ryan Heywood 2025-07-24 20:51:31 -04:00
parent 2f1a632556
commit 859041351f
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
2 changed files with 27 additions and 1 deletions

View File

@ -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(),

View File

@ -89,6 +89,21 @@ pub fn mount(
}
}
pub fn mkdir(path: impl AsRef<Path>) -> 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<Path>, mode: impl AsRef<str>, fd: &impl AsRawFd) -> Result<()> {
let path = path.as_ref();
let mode = mode.as_ref();