Skip to content

Commit

Permalink
cargo fmt again
Browse files Browse the repository at this point in the history
  • Loading branch information
korewaChino committed Jul 2, 2024
1 parent 1bb0c77 commit 14baac8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
4 changes: 2 additions & 2 deletions examples/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ fn main() {

container.host_bind_mount();
container.mount().unwrap();
// or just do

// or just do
// Container::new("chroot".into())
// .host_bind_mount()
// .run(|| {
Expand Down
37 changes: 15 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
use std::{
collections::BTreeMap,
error::Error,
fs::File,
os::fd::AsRawFd,
path::PathBuf,
};
use std::{collections::BTreeMap, error::Error, fs::File, os::fd::AsRawFd, path::PathBuf};
use sys_mount::{FilesystemType, Mount, MountFlags, Unmount, UnmountDrop, UnmountFlags};
/// Mount object struct
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
Expand Down Expand Up @@ -177,8 +171,6 @@ pub struct Container {
}

impl Container {


/// Enter chroot jail
///
/// This makes use of the `chroot` syscall to enter the chroot jail.
Expand All @@ -188,7 +180,7 @@ impl Container {
if !self._initialized {
// mount the tmpfs first, idiot proofing in case the
// programmer forgets to mount it before chrooting
//
//
// This should be fine as it's going to be dismounted after dropping regardless
self.mount()?;
}
Expand All @@ -205,20 +197,20 @@ impl Container {
/// to a raw file descriptor of the sysroot we saved earlier
/// in `[Container::new]`, and then chrooting to the directory
/// we just moved to.
///
///
/// We then also take the pwd stored earlier and move back to it,
/// for good measure.
#[inline(always)]
pub fn exit_chroot(&mut self) -> Result<(), Box<dyn Error>> {
nix::unistd::fchdir(self.sysroot.as_raw_fd())?;
nix::unistd::chroot(".")?;
self.chroot = false;

// Let's return back to pwd
nix::unistd::fchdir(self.pwd.as_raw_fd())?;
Ok(())
}

/// Create a new tiffin container
///
/// To use it, you need to create a new container with `root`
Expand Down Expand Up @@ -279,7 +271,7 @@ impl Container {
self._initialized = false;
Ok(())
}

/// Adds a bind mount for the system's root filesystem to
/// the container's root filesystem at `/run/host`
pub fn host_bind_mount(&mut self) -> &mut Self {
Expand Down Expand Up @@ -374,16 +366,17 @@ mod tests {
fn test_container() {
let mut container = Container::new(PathBuf::from("/tmp/tiffin"));
container.host_bind_mount();
container.run(|| {
let mut file = File::create("/run/host/test.txt").unwrap();
file.write_all(b"Hello, world!").unwrap();
Ok(())
})
.unwrap();

container
.run(|| {
let mut file = File::create("/run/host/test.txt").unwrap();
file.write_all(b"Hello, world!").unwrap();
Ok(())
})
.unwrap();

let mut file = File::open("/tmp/tiffin/run/host/test.txt").unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
assert_eq!(contents, "Hello, world!");
}
}
}

0 comments on commit 14baac8

Please sign in to comment.