Skip to content

Commit

Permalink
container: allow opening containers without locking
Browse files Browse the repository at this point in the history
  • Loading branch information
xtexChooser committed Jan 11, 2025
1 parent cc9af4f commit e910265
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
28 changes: 17 additions & 11 deletions ciel/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub struct Container {
instance: Instance,
config: Arc<ContainerConfig>,
#[allow(unused)]
lock: Arc<FileLock>,
lock: Option<Arc<FileLock>>,
ns_name: String,

rootfs_path: PathBuf,
Expand Down Expand Up @@ -89,15 +89,21 @@ impl Drop for FileLock {
}

impl Container {
/// Opens the build container, locking it exclusively.
pub fn open(instance: Instance) -> Result<Self> {
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<Self> {
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());
Expand Down Expand Up @@ -134,7 +140,7 @@ impl Container {
Ok(Self {
instance,
config: Arc::new(config),
lock: Arc::new(lock),
lock,
ns_name,
rootfs_path,
config_path,
Expand Down
7 changes: 6 additions & 1 deletion ciel/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ impl Instance {
///
/// This is equivalent to calling [Container::open].
pub fn open(&self) -> Result<Container> {
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> {
Container::open(self.to_owned(), false)
}

/// Destories the container, removing all related files.
Expand Down
2 changes: 1 addition & 1 deletion cli/src/actions/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit e910265

Please sign in to comment.