Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Container.run() generic return type #1

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{
collections::BTreeMap,
error::Error,
fs::File,
os::fd::AsRawFd,
path::{Path, PathBuf},
Expand Down Expand Up @@ -256,10 +255,9 @@ impl Container {

/// Run a function inside the container chroot
#[inline(always)]
pub fn run<F, E>(&mut self, f: F) -> Result<(), E>
pub fn run<F, T>(&mut self, f: F) -> std::io::Result<T>
where
F: FnOnce() -> Result<(), E>,
E: Error + From<std::io::Error>,
F: FnOnce() -> T,
{
// Only mount and chroot if we're not already initialized
if !self._initialized {
Expand All @@ -269,15 +267,14 @@ impl Container {
self.chroot()?;
}
tracing::trace!("Running function inside container");
f()?;
let ret = f();
if self.chroot {
self.exit_chroot()?;
}
if self._initialized {
self.umount()?;
}

Ok(())
Ok(ret)
}

/// Start mounting files inside the container
Expand Down Expand Up @@ -388,10 +385,7 @@ mod tests {
std::fs::create_dir_all("/tmp/tiffin").unwrap();
let mut container = Container::new(PathBuf::from("/tmp/tiffin"));
container
.run(|| {
std::fs::create_dir_all("/tmp/tiffin/test").unwrap();
std::io::Result::Ok(())
})
.run(|| std::fs::create_dir_all("/tmp/tiffin/test").unwrap())
.unwrap();
}
}