From e910265c9382820dd8f53d4931775b7d48eb5b9e Mon Sep 17 00:00:00 2001 From: xtex Date: Sat, 11 Jan 2025 18:49:54 +0800 Subject: [PATCH] container: allow opening containers without locking --- ciel/src/container.rs | 28 +++++++++++++++++----------- ciel/src/instance.rs | 7 ++++++- cli/src/actions/workspace.rs | 2 +- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/ciel/src/container.rs b/ciel/src/container.rs index 7f20a52..fcff0e0 100644 --- a/ciel/src/container.rs +++ b/ciel/src/container.rs @@ -49,7 +49,7 @@ pub struct Container { instance: Instance, config: Arc, #[allow(unused)] - lock: Arc, + lock: Option>, ns_name: String, rootfs_path: PathBuf, @@ -89,15 +89,21 @@ impl Drop for FileLock { } impl Container { - /// Opens the build container, locking it exclusively. - pub fn open(instance: Instance) -> Result { - let lock = File::options() - .read(true) - .write(true) - .create(true) - .open(instance.directory().join(".lock"))?; - fs3::FileExt::lock_exclusive(&lock)?; - let lock = FileLock(lock); + /// Opens the build container. + /// + /// If `lock` is true, it will be locked exclusively. + pub fn open(instance: Instance, lock: bool) -> Result { + let lock = if lock { + let lock = File::options() + .read(true) + .write(true) + .create(true) + .open(instance.directory().join(".lock"))?; + fs3::FileExt::lock_exclusive(&lock)?; + Some(Arc::new(FileLock(lock))) + } else { + None + }; let ns_name = make_container_ns_name(instance.name())?; let rootfs_path = instance.workspace().directory().join(instance.name()); @@ -134,7 +140,7 @@ impl Container { Ok(Self { instance, config: Arc::new(config), - lock: Arc::new(lock), + lock, ns_name, rootfs_path, config_path, diff --git a/ciel/src/instance.rs b/ciel/src/instance.rs index 83c7f82..8545041 100644 --- a/ciel/src/instance.rs +++ b/ciel/src/instance.rs @@ -89,7 +89,12 @@ impl Instance { /// /// This is equivalent to calling [Container::open]. pub fn open(&self) -> Result { - Container::open(self.to_owned()) + Container::open(self.to_owned(), true) + } + + /// Opens the build container for further operations without locking. + pub fn open_unlocked(&self) -> Result { + Container::open(self.to_owned(), false) } /// Destories the container, removing all related files. diff --git a/cli/src/actions/workspace.rs b/cli/src/actions/workspace.rs index 22face8..714bc80 100644 --- a/cli/src/actions/workspace.rs +++ b/cli/src/actions/workspace.rs @@ -27,7 +27,7 @@ pub fn list_instances() -> Result<()> { writeln!(&mut formatter, "NAME\tMOUNTED\tSTARTED\tBOOTED")?; for inst in ws.instances()? { - let container = inst.open()?; + let container = inst.open_unlocked()?; let state = container.state()?; let (mounted, started, running) = match state { ContainerState::Down => (false, false, false),