| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | | use super::*; |
| 8 | |
|
| 9 | | #[derive(Debug, Clone, PartialEq, Eq)] |
| 10 | | pub struct WorkspaceInfo { |
| 11 | | pub id: WorkspaceId, |
| 12 | | pub path: PathBuf, |
| 13 | | |
| 14 | | pub working_copy: Option<ChangeHash>, |
| 15 | | pub current: bool, |
| 16 | | } |
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | | pub struct FileLock { |
| 23 | | path: PathBuf, |
| 24 | | } |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | | pub struct WorkingCopyLock { |
| 32 | | path: PathBuf, |
| 33 | | _file: Option<FileLock>, |
| 34 | | } |
| 35 | |
|
| 36 | |
|
| 37 | | pub type SnapshotLock = WorkingCopyLock; |
| 38 | |
|
| 39 | | thread_local! { |
| 40 | | |
| 41 | | static HELD: std::cell::RefCell<HashMap<PathBuf, usize>> = Default::default(); |
| 42 | | } |
| 43 | |
|
| 44 | | impl Drop for WorkingCopyLock { |
| 45 | 2655× | fn drop(&mut self) { |
| 46 | 2655× | HELD.with(|h| { |
| 47 | 2655× | let mut h = h.borrow_mut(); |
| 48 | 2655× | if let Some(n) = h.get_mut(&self.path) { |
| 49 | 2654× | *n -= 1; |
| 50 | 2654× | if *n == 0 { |
| 51 | 1577× | h.remove(&self.path); |
| 52 | 1577× | } |
| 53 | 1× | } |
| 54 | 2655× | }); |
| 55 | 2655× | } |
| 56 | | } |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | 126× | pub(crate) fn lock_is_stale(path: &Path) -> bool { |
| 61 | 126× | let old = fs::metadata(path) |
| 62 | 126× | .and_then(|m| m.modified()) |
| 63 | 126× | .map(|t| t.elapsed().map(|d| d.as_secs() > 60).unwrap_or(false)) |
| 64 | 126× | .unwrap_or(true); |
| 65 | 126× | let proc_fs = Path::new("/proc/self").exists(); |
| 66 | 126× | match fs::read_to_string(path) |
| 67 | 126× | .ok() |
| 68 | 126× | .and_then(|s| s.trim().parse::<u32>().ok()) |
| 69 | | { |
| 70 | 126× | Some(pid) if proc_fs => !Path::new("/proc").join(pid.to_string()).exists(), |
| 71 | | |
| 72 | 0× | _ => old, |
| 73 | | } |
| 74 | 126× | } |
| 75 | |
|
| 76 | | impl Drop for FileLock { |
| 77 | 4938× | fn drop(&mut self) { |
| 78 | 4938× | let _ = fs::remove_file(&self.path); |
| 79 | 4938× | } |
| 80 | | } |
| 81 | |
|
| 82 | | impl FileLock { |
| 83 | | |
| 84 | 3361× | pub(crate) fn acquire(path: PathBuf, busy: &str) -> Result<FileLock> { |
| 85 | 3361× | Self::acquire_within(path, busy, std::time::Duration::from_secs(5)) |
| 86 | 3361× | } |
| 87 | |
|
| 88 | | |
| 89 | 4938× | pub(crate) fn acquire_within( |
| 90 | 4938× | path: PathBuf, |
| 91 | 4938× | busy: &str, |
| 92 | 4938× | wait: std::time::Duration, |
| 93 | 4938× | ) -> Result<FileLock> { |
| 94 | 4938× | let until = std::time::Instant::now() + wait; |
| 95 | | loop { |
| 96 | 5063× | match fs::OpenOptions::new() |
| 97 | 5063× | .write(true) |
| 98 | 5063× | .create_new(true) |
| 99 | 5063× | .open(&path) |
| 100 | | { |
| 101 | 4938× | Ok(mut f) => { |
| 102 | 4938× | let _ = write!(f, "{}", std::process::id()); |
| 103 | 4938× | return Ok(FileLock { path }); |
| 104 | | } |
| 105 | 125× | Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => { |
| 106 | 125× | if lock_is_stale(&path) { |
| 107 | 1× | let _ = fs::remove_file(&path); |
| 108 | 1× | continue; |
| 109 | 124× | } |
| 110 | 124× | if std::time::Instant::now() >= until { |
| 111 | 0× | return fail(busy); |
| 112 | 124× | } |
| 113 | 124× | std::thread::sleep(std::time::Duration::from_millis(10)); |
| 114 | | } |
| 115 | 0× | Err(e) => return Err(e.into()), |
| 116 | | } |
| 117 | | } |
| 118 | 4938× | } |
| 119 | | } |
| 120 | |
|
| 121 | |
|
| 122 | 9× | pub(crate) fn parse_workspace_file(text: &str) -> Option<(PathBuf, WorkspaceId)> { |
| 123 | 9× | let mut dir = None; |
| 124 | 9× | let mut id = None; |
| 125 | 18× | for line in text.lines() { |
| 126 | 18× | if let Some(v) = line.strip_prefix("oxydir:") { |
| 127 | 9× | dir = Some(PathBuf::from(v.trim())); |
| 128 | 9× | } else if let Some(v) = line.strip_prefix("workspace:") { |
| 129 | 9× | id = WorkspaceId::from_hex(v.trim()); |
| 130 | 9× | } |
| 131 | | } |
| 132 | 9× | Some((dir?, id?)) |
| 133 | 9× | } |
| 134 | |
|
| 135 | | impl Repo { |
| 136 | 3149× | fn workspace_dir(&self, id: &WorkspaceId) -> PathBuf { |
| 137 | 3149× | self.oxy.join("workspaces").join(id.to_hex()) |
| 138 | 3149× | } |
| 139 | |
|
| 140 | | |
| 141 | 489× | pub(crate) fn register_workspace(&self, id: &WorkspaceId, path: &Path) -> Result<()> { |
| 142 | 489× | let dir = self.workspace_dir(id); |
| 143 | 489× | fs::create_dir_all(&dir)?; |
| 144 | 489× | write_atomic(&dir.join("path"), path.to_string_lossy().as_bytes()) |
| 145 | 489× | } |
| 146 | |
|
| 147 | | |
| 148 | 4× | pub fn workspace_path(&self, id: &WorkspaceId) -> Option<PathBuf> { |
| 149 | 4× | fs::read_to_string(self.workspace_dir(id).join("path")) |
| 150 | 4× | .ok() |
| 151 | 4× | .map(|s| PathBuf::from(s.trim())) |
| 152 | 4× | } |
| 153 | |
|
| 154 | | |
| 155 | 6× | pub fn workspaces(&self) -> Result<Vec<WorkspaceInfo>> { |
| 156 | 6× | let view = self.view()?; |
| 157 | 6× | let mut ids: BTreeSet<WorkspaceId> = view.workspaces.keys().copied().collect(); |
| 158 | 6× | if let Ok(rd) = fs::read_dir(self.oxy.join("workspaces")) { |
| 159 | 9× | for e in rd.flatten() { |
| 160 | 9× | if let Some(id) = WorkspaceId::from_hex(&e.file_name().to_string_lossy()) { |
| 161 | 9× | if e.path().join("path").exists() { |
| 162 | 9× | ids.insert(id); |
| 163 | 9× | } |
| 164 | 0× | } |
| 165 | | } |
| 166 | 0× | } |
| 167 | 6× | let mut out = Vec::new(); |
| 168 | 9× | for id in ids { |
| 169 | 9× | let path = if id == self.workspace { |
| 170 | 6× | self.root.clone() |
| 171 | | } else { |
| 172 | 3× | self.workspace_path(&id) |
| 173 | 3× | .unwrap_or_else(|| PathBuf::from("(unknown)")) |
| 174 | | }; |
| 175 | 9× | out.push(WorkspaceInfo { |
| 176 | 9× | id, |
| 177 | 9× | path, |
| 178 | 9× | working_copy: view.workspaces.get(&id).copied(), |
| 179 | 9× | current: id == self.workspace, |
| 180 | 9× | }); |
| 181 | | } |
| 182 | 6× | Ok(out) |
| 183 | 6× | } |
| 184 | |
|
| 185 | | |
| 186 | | |
| 187 | | |
| 188 | | |
| 189 | 6× | pub fn workspace_add(&self, dir: &Path, heads: Option<&[ChangeHash]>) -> Result<Repo> { |
| 190 | 6× | self.workspace_add_on(dir, heads, None) |
| 191 | 6× | } |
| 192 | |
|
| 193 | | |
| 194 | 8× | pub fn workspace_add_on( |
| 195 | 8× | &self, |
| 196 | 8× | dir: &Path, |
| 197 | 8× | heads: Option<&[ChangeHash]>, |
| 198 | 8× | bookmark: Option<&str>, |
| 199 | 8× | ) -> Result<Repo> { |
| 200 | 8× | if dir.exists() && fs::read_dir(dir)?.next().is_some() { |
| 201 | 0× | return fail(format!("{} exists and is not empty", dir.display())); |
| 202 | 8× | } |
| 203 | 8× | fs::create_dir_all(dir)?; |
| 204 | 8× | let dir = dir.canonicalize()?; |
| 205 | 8× | if dir == self.root || dir.starts_with(&self.root) || self.root.starts_with(&dir) { |
| 206 | 1× | return fail("a workspace cannot be inside another workspace of the same repository"); |
| 207 | 7× | } |
| 208 | 7× | let heads = match heads { |
| 209 | 2× | Some(h) => h.to_vec(), |
| 210 | 5× | None => self.bookmark_heads()?, |
| 211 | | }; |
| 212 | 7× | let id = WorkspaceId::random(); |
| 213 | 7× | let wc = self.new_working_copy(heads.clone(), ""); |
| 214 | 7× | let wc_hash = self.store.put_change(&wc)?; |
| 215 | 7× | let (op_ref, op) = self.head_op()?; |
| 216 | 7× | let mut view = op.view.clone(); |
| 217 | 7× | view.workspaces.insert(id, wc_hash); |
| 218 | 7× | self.record_op( |
| 219 | 7× | &[op_ref], |
| 220 | 7× | &format!("oxy workspace add {}", dir.display()), |
| 221 | 7× | view, |
| 222 | 0× | )?; |
| 223 | 7× | self.register_workspace(&id, &dir)?; |
| 224 | 7× | self.set_workspace_bookmark(&id, bookmark.unwrap_or(&self.config.bookmark))?; |
| 225 | 7× | write_atomic( |
| 226 | 7× | &dir.join(OXY_DIR), |
| 227 | 7× | format!( |
| 228 | 7× | "oxydir: {}\nworkspace: {}\n", |
| 229 | 7× | self.oxy.display(), |
| 230 | 7× | id.to_hex() |
| 231 | 7× | ) |
| 232 | 7× | .as_bytes(), |
| 233 | 0× | )?; |
| 234 | 7× | let repo = Repo::open(&dir)?.with_home(self.keys_dir.parent().unwrap_or(&self.keys_dir)); |
| 235 | 7× | repo.checkout(&heads, true, "oxy workspace add (checkout)")?; |
| 236 | 7× | self.copy_subrepos_into(&repo); |
| 237 | 7× | Ok(repo) |
| 238 | 8× | } |
| 239 | |
|
| 240 | | |
| 241 | | |
| 242 | | |
| 243 | | |
| 244 | | |
| 245 | 7× | fn copy_subrepos_into(&self, new: &Repo) { |
| 246 | 7× | let Ok(subs) = new.subrepos() else { return }; |
| 247 | 7× | let modules = new.modules_on_disk(); |
| 248 | 7× | for s in subs { |
| 249 | 1× | let (src, dst) = (self.root.join(&s.path), new.root.join(&s.path)); |
| 250 | 1× | if dst.join(OXY_DIR).exists() || dst.join(".git").exists() { |
| 251 | 0× | continue; |
| 252 | 1× | } |
| 253 | 1× | let url = modules.entries.get(&s.path).map(|m| m.url.clone()); |
| 254 | 1× | if let Some(parent) = dst.parent() { |
| 255 | 1× | let _ = fs::create_dir_all(parent); |
| 256 | 1× | } |
| 257 | 1× | let _ = fs::remove_dir(&dst); |
| 258 | 1× | if src.join(OXY_DIR).exists() { |
| 259 | 1× | match Repo::clone(&src.to_string_lossy(), &dst) { |
| 260 | 1× | Ok(mut sub) => { |
| 261 | 1× | if let Some(u) = &url { |
| 262 | 1× | if sub.config.set("remote.origin.url", u).is_ok() { |
| 263 | 1× | let _ = sub.save_config(); |
| 264 | 1× | } |
| 265 | 0× | } |
| 266 | | } |
| 267 | 0× | Err(_) => continue, |
| 268 | | } |
| 269 | 0× | } else if src.join(".git").exists() { |
| 270 | 0× | let cloned = std::process::Command::new("git") |
| 271 | 0× | .args(["clone", "-q", "--local"]) |
| 272 | 0× | .arg(&src) |
| 273 | 0× | .arg(&dst) |
| 274 | 0× | .status() |
| 275 | 0× | .is_ok_and(|st| st.success()); |
| 276 | 0× | if !cloned { |
| 277 | 0× | continue; |
| 278 | 0× | } |
| 279 | 0× | if let Some(u) = &url { |
| 280 | 0× | let _ = std::process::Command::new("git") |
| 281 | 0× | .arg("-C") |
| 282 | 0× | .arg(&dst) |
| 283 | 0× | .args(["remote", "set-url", "origin", u]) |
| 284 | 0× | .status(); |
| 285 | 0× | } |
| 286 | | } else { |
| 287 | 0× | continue; |
| 288 | | } |
| 289 | 1× | let _ = new.sync_subrepo(&s.path, &s.pin, false); |
| 290 | | } |
| 291 | 7× | } |
| 292 | |
|
| 293 | | |
| 294 | 2× | pub fn workspace_forget(&self, id: &WorkspaceId) -> Result<()> { |
| 295 | 2× | if *id == self.workspace { |
| 296 | 1× | return fail("cannot forget the workspace you are in"); |
| 297 | 1× | } |
| 298 | 1× | let (op_ref, op) = self.head_op()?; |
| 299 | 1× | let mut view = op.view.clone(); |
| 300 | 1× | if view.workspaces.remove(id).is_none() && self.workspace_path(id).is_none() { |
| 301 | 0× | return fail(format!("no workspace {}", id.short(8))); |
| 302 | 1× | } |
| 303 | 1× | self.record_op( |
| 304 | 1× | &[op_ref], |
| 305 | 1× | &format!("oxy workspace forget {}", id.short(8)), |
| 306 | 1× | view, |
| 307 | 0× | )?; |
| 308 | 1× | let _ = fs::remove_dir_all(self.workspace_dir(id)); |
| 309 | 1× | Ok(()) |
| 310 | 2× | } |
| 311 | |
|
| 312 | | |
| 313 | 1292× | pub(crate) fn lock_snapshot(&self) -> Result<SnapshotLock> { |
| 314 | 1292× | self.lock_working_copy() |
| 315 | 1292× | } |
| 316 | |
|
| 317 | | |
| 318 | | |
| 319 | 2655× | pub fn lock_working_copy(&self) -> Result<WorkingCopyLock> { |
| 320 | 2655× | let dir = self.workspace_dir(&self.workspace); |
| 321 | 2655× | if self.bare { |
| 322 | | |
| 323 | 1× | return Ok(WorkingCopyLock { |
| 324 | 1× | path: dir, |
| 325 | 1× | _file: None, |
| 326 | 1× | }); |
| 327 | 2654× | } |
| 328 | 2654× | fs::create_dir_all(&dir)?; |
| 329 | 2654× | let path = dir.join("lock"); |
| 330 | 2654× | let held = HELD.with(|h| { |
| 331 | 2654× | let mut h = h.borrow_mut(); |
| 332 | 2654× | match h.get_mut(&path) { |
| 333 | 1077× | Some(n) => { |
| 334 | 1077× | *n += 1; |
| 335 | 1077× | true |
| 336 | | } |
| 337 | 1577× | None => false, |
| 338 | | } |
| 339 | 2654× | }); |
| 340 | 2654× | if held { |
| 341 | 1077× | return Ok(WorkingCopyLock { path, _file: None }); |
| 342 | 1577× | } |
| 343 | 1577× | let file = FileLock::acquire_within( |
| 344 | 1577× | path.clone(), |
| 345 | 1577× | "another oxy process is using this working copy (a checkout or snapshot); try again when it finishes", |
| 346 | 1577× | std::time::Duration::from_secs(60), |
| 347 | 0× | )?; |
| 348 | 1577× | HELD.with(|h| h.borrow_mut().insert(path.clone(), 1)); |
| 349 | 1577× | Ok(WorkingCopyLock { |
| 350 | 1577× | path, |
| 351 | 1577× | _file: Some(file), |
| 352 | 1577× | }) |
| 353 | 2655× | } |
| 354 | | } |