diff --git a/apps/origin-src/src/workspace/mod.rs b/apps/origin-src/src/workspace/mod.rs index 4dfacb0e..ef0f72d9 100644 --- a/apps/origin-src/src/workspace/mod.rs +++ b/apps/origin-src/src/workspace/mod.rs @@ -8,12 +8,15 @@ //! workspace the revision the working copy is //! state the stat cache of the last snapshot //! moving the move in progress, if a checkout is +//! lock held by the command working in the working copy //! ``` //! //! A repository can have several working copies, its workspaces, which -//! share its store and op log. The others have only `workspace`, `state` -//! and `moving` in their `.origin`, and a `repository` file naming the main -//! one's. +//! share its store and op log. The others have only `workspace`, `state`, +//! `moving` and `lock` in their `.origin`, and a `repository` file naming +//! the main one's. Commands in different workspaces run at once; in one, +//! each waits for the one before, except `watch`, which never touches the +//! working copy, and `workspace rm`, which opens the workspace it removes. //! //! The working copy is always on a revision of the current change. Every //! command that reads it snapshots it first. The files the snapshot finds @@ -78,7 +81,7 @@ use std::mem; use std::path::{Path, PathBuf}; use std::sync::{Arc, OnceLock}; -use origin_platform::{LockedFile, Native}; +use origin_platform::{LockMode, LockedFile, Native}; use origin_shared::api::{LogQuery, RevisionEntry, Revisions}; use origin_shared::{ AcceptanceId, Base, BlobId, ChangeId, EncodedObject, Kind, Principal, Record, RecordId, @@ -118,6 +121,9 @@ pub struct Workspace { unreadable_move: Option, /// The repository's remote, once opened: see [`Workspace::remote`]. remote: OnceLock>, + /// Held while the command that opened the working copy works in it, + /// so only one at a time does: see [`Workspace::open`]. + lock: Option, } /// The working copy as of a snapshot. @@ -235,6 +241,10 @@ impl Workspace { /// in progress: see [`Moving`]. const MOVING: &str = "moving"; + /// The file in [`Workspace::META_DIR`] held locked by the command + /// working in the working copy. + const LOCK: &str = "lock"; + /// `origin init`: creates a repository in `root`, with the `policy` /// target naming this node the authority of `policy` and `main`, an /// empty `main`, and a first change on top of it. @@ -303,10 +313,19 @@ impl Workspace { /// Opens the working copy at `root`: the main one, whose `.origin` holds /// the store, or another workspace, whose `.origin/repository` names - /// the main one's. Finishes a move a crash cut short first. + /// the main one's. Waits for the command working in it, if any, then + /// finishes a move a crash cut short. fn open(root: &Path, config: UserConfig, actor: &Actor) -> Result { let root = &Self::canonical(root)?; let meta = root.join(Self::META_DIR); + // Held until the command is done, so `moving` is only ever found + // once the move it records was cut short. + let lock = LockedFile::open( + &Native::default(), + &meta.join(Self::LOCK), + LockMode::Exclusive, + ) + .map_err(StoreError::from)?; let repository = meta.join(Self::REPOSITORY); let store = match Self::read_path(&repository) { Ok(store) if store.is_dir() => store, @@ -320,7 +339,10 @@ impl Workspace { Self::read_pointer(root)?.ok_or_else(|| WorkspaceError::Incomplete(root.to_owned()))?; let store = Store::open_writable(store, Native::default())?; let store = store.with_actor(actor.in_workspace(root)); - let mut workspace = Self::at(root.to_owned(), store, config, revision); + let mut workspace = Self { + lock: Some(lock), + ..Self::at(root.to_owned(), store, config, revision) + }; workspace.finish_move()?; Ok(workspace) } @@ -336,6 +358,7 @@ impl Workspace { unlogged: None, unreadable_move: None, remote: OnceLock::new(), + lock: None, } } diff --git a/apps/origin-src/src/workspace/ops.rs b/apps/origin-src/src/workspace/ops.rs index 2dbb4139..6a35d020 100644 --- a/apps/origin-src/src/workspace/ops.rs +++ b/apps/origin-src/src/workspace/ops.rs @@ -218,12 +218,14 @@ impl Workspace { /// Fails if the cursor expired, the remote fails, or the op log or a /// record cannot be read. pub fn watch( - &self, + &mut self, kinds: Vec, subjects: Vec, since: Option, local: bool, ) -> Result<(), WorkspaceError> { + // It never touches the working copy, so others may meanwhile. + self.lock = None; let remote = if local { None } else { self.remote()? }; let service = self.service(None)?; let mut query = EventsQuery { diff --git a/apps/origin-src/src/workspace/registry.rs b/apps/origin-src/src/workspace/registry.rs index 66bd3f8b..980c2812 100644 --- a/apps/origin-src/src/workspace/registry.rs +++ b/apps/origin-src/src/workspace/registry.rs @@ -3,8 +3,8 @@ //! //! The main working copy's `.origin/workspaces/` has a file for each other //! workspace, named by the hash of its root and holding that root. Records -//! are immutable, and only target chains and the index are locked, so -//! commands run concurrently in different workspaces. +//! are immutable, and only target chains, the index and each working copy +//! are locked, so commands run concurrently in different workspaces. //! //! Creating a workspace registers it, checks out its files, and only then //! publishes its `.origin/workspace`, so a crash never leaves partial files @@ -115,7 +115,10 @@ impl Workspace { /// Fails if `path` is the main working copy or no workspace, if the /// workspace is behind its change with edits, which no revision holds /// then, or if it cannot be snapshotted or deleted. - pub fn remove_workspace(&self, path: &Path) -> Result<(), WorkspaceError> { + pub fn remove_workspace(&mut self, path: &Path) -> Result<(), WorkspaceError> { + // It works in the workspace it removes, which may be this one, and + // opening it takes its lock. + self.lock = None; let root = Self::canonical(path)?; if root == self.main_root() { return Err(WorkspaceError::RemoveMain(root)); diff --git a/apps/origin-src/tests/cli.rs b/apps/origin-src/tests/cli.rs index 516dc383..589699f1 100644 --- a/apps/origin-src/tests/cli.rs +++ b/apps/origin-src/tests/cli.rs @@ -1093,6 +1093,13 @@ fn removing_a_workspace_keeps_its_work() { .contains("+not snapshotted yet\n") ); + // A workspace removes itself. + let own = others.path().join("own"); + let change = repo.workspace(&own, &[]); + let removed = repo.ok_in(&own, &["workspace", "rm", "."]); + assert!(removed.starts_with(&format!("Kept its working copy: {change} "))); + assert!(!own.exists()); + assert!( repo.fails(&["workspace", "rm", "."]) .contains("is the main working copy") @@ -1419,6 +1426,19 @@ fn commands_in_different_workspaces_run_concurrently() { } } +#[test] +fn commands_in_one_workspace_wait_for_each_other() { + let repo = Repo::new(); + // As a command working in the working copy holds it. + let lock = fs::File::create(repo.path(".origin/lock")).unwrap(); + lock.lock().unwrap(); + let (mut status, _) = repo.spawn(&["status"]); + thread::sleep(Duration::from_millis(500)); + assert!(status.try_wait().unwrap().is_none(), "it waits"); + drop(lock); + assert!(status.wait().unwrap().success()); +} + #[test] fn workspaces_land_their_own_changes() { let repo = Repo::new();