1use std::fs::{File, OpenOptions, TryLockError};
4use std::path::{Path, PathBuf};
5
6use crate::files::file_lock::{FileLock, LockMode};
7use crate::files::{atomic_file, settings, FileError, SettingsError, StoreSettings};
8use crate::id_source::StoreId;
9
10use super::held_open;
11use super::lock_registry as registry;
12use registry::LockKind;
13pub use registry::StoreClosingGuard;
14pub(super) use registry::{bootstrap_running, LockInstance, RegisteredLock};
15
16#[derive(Debug)]
19pub struct StoreLock {
20 store: LockedStore<RegisteredLock>,
21}
22
23#[derive(Debug)]
24struct LockedStore<L> {
25 _file: L,
26 directory: PathBuf,
27 id: StoreId,
28}
29
30impl StoreLock {
31 pub fn closing_guard(&self) -> StoreClosingGuard {
34 self.store._file.closing_guard()
35 }
36
37 pub fn settings(&self) -> Result<StoreSettings, SettingsError> {
39 settings::read(&self.store.directory, self.store.id)
40 }
41
42 pub(super) fn directory(&self) -> &Path {
44 &self.store.directory
45 }
46
47 pub(crate) fn verify(&self, directory: &Path, id: StoreId) -> Result<(), StoreLockError> {
48 let path = std::fs::canonicalize(directory)
49 .map_err(|source| FileError::at("resolve locked store", directory, source))?;
50 if path != self.store.directory || id != self.store.id {
51 return Err(StoreLockError::WrongDirectory(id));
52 }
53 Ok(())
54 }
55}
56
57#[derive(Debug)]
59pub struct StoreReadLock {
60 _file: RegisteredLock,
61}
62
63impl StoreReadLock {
64 pub fn closing_guard(&self) -> StoreClosingGuard {
68 self._file.closing_guard()
69 }
70}
71
72#[derive(Debug)]
77pub struct ClosedStoreLock {
78 _file: RegisteredLock,
79}
80
81#[derive(Debug)]
84pub struct CacheWriteLock {
85 _file: FileLock,
86}
87
88const CACHE_WRITES: &str = ".coven-cache-writes.lock";
91
92pub(super) fn cache_writes(store: &Path, shared: bool) -> Result<CacheWriteLock, FileError> {
95 let path = store.join(CACHE_WRITES);
96 let mode = if shared { LockMode::Shared } else { LockMode::Exclusive };
97 Ok(CacheWriteLock { _file: wait_for_lock(&path, mode, "lock cache writes")? })
98}
99
100fn wait_for_lock(path: &Path, mode: LockMode, what: &'static str) -> Result<FileLock, FileError> {
103 let file = open_lock(path)?;
104 let attempt = file.try_clone().map_err(|source| FileError::at(what, path, source))?;
105 match FileLock::try_lock(attempt, mode) {
106 Ok(file) => return Ok(file),
107 Err(TryLockError::WouldBlock) => {
108 #[cfg(any(test, feature = "test-utils"))]
109 super::reached::reached(path);
110 }
111 Err(TryLockError::Error(source)) => return Err(FileError::at(what, path, source)),
112 }
113 FileLock::lock(file, mode).map_err(|source| FileError::at(what, path, source))
114}
115
116#[cfg(any(test, feature = "test-utils"))]
119pub fn cache_writes_lock(store: &Path) -> std::path::PathBuf {
120 store.join(CACHE_WRITES)
121}
122
123#[derive(Debug)]
129pub struct StoreDeletionLock {
130 store: LockedStore<FileLock>,
131 _readers: FileLock,
132 bootstrap: HeldBootstrap,
133}
134
135#[derive(Debug)]
137enum HeldBootstrap {
138 Locked(FileLock),
140 Leased(RegisteredLock),
142}
143
144impl HeldBootstrap {
145 fn release(self) {
146 match self {
147 Self::Locked(file) => drop(file),
148 Self::Leased(lock) => drop(lock),
149 }
150 }
151}
152
153impl StoreDeletionLock {
154 pub fn unpublish(&mut self) -> Result<(), FileError> {
160 let _layout = lock_layout(self.store.directory.parent().expect("store parent"))?;
161 self.unpublish_locked()
162 }
163
164 fn unpublish_locked(&mut self) -> Result<(), FileError> {
166 let destination = deletion_path(&self.store.directory, self.store.id);
167 if self.store.directory != destination && inspect_directory(&self.store.directory)? {
168 crate::files::atomic_file::rename_new(&self.store.directory, &destination).map_err(
169 |source| {
170 held_open::directory_failure("unpublish store", &self.store.directory, source)
171 },
172 )?;
173 self.store.directory = destination;
174 #[cfg(unix)]
175 sync_removal(&self.store.directory)?;
176 }
177 Ok(())
178 }
179
180 pub fn remove_directory(mut self) -> Result<(), FileError> {
185 let root = self.store.directory.parent().expect("store parent");
186 let _layout = lock_layout(root)?;
191 let paths = deletion_lock_paths(&self.store.directory, self.store.id);
192 self.unpublish_locked()?;
193 if inspect_directory(&self.store.directory)? {
194 std::fs::remove_dir_all(&self.store.directory).map_err(|source| {
195 held_open::directory_failure(
196 "remove store directory",
197 &self.store.directory,
198 source,
199 )
200 })?;
201 }
202 #[cfg(unix)]
203 sync_removal(&self.store.directory)?;
204 let Self { store, _readers, bootstrap } = self;
210 drop((store, _readers));
211 bootstrap.release();
212 for path in paths {
213 atomic_file::remove(&path)?;
214 }
215 Ok(())
216 }
217}
218
219#[cfg(unix)]
220fn sync_removal(directory: &Path) -> Result<(), FileError> {
221 crate::files::atomic_file::sync_directory(directory.parent().expect("store parent")).map_err(
222 |source| FileError::AfterRemove { path: directory.to_owned(), source: Box::new(source) },
223 )
224}
225
226#[derive(Debug, thiserror::Error)]
228pub enum StoreLockError {
229 #[error("store {0} is already open")]
232 AlreadyOpen(StoreId),
233 #[error("store {0} requires restore or join to finish bootstrap")]
237 BootstrapPending(StoreId),
238 #[error("store {store} requires {by} to finish replacing this device's installation")]
243 ReplacementUnfinished {
244 store: StoreId,
246 by: super::ReplacedBy,
248 },
249 #[error("store {0} is being deleted from this device")]
255 DeletionUnfinished(StoreId),
256 #[error("lock does not protect store {0}")]
258 WrongDirectory(StoreId),
259 #[error("store lock: {0}")]
262 File(FileError),
263 #[error(transparent)]
266 Bug(#[from] crate::task::Bug),
267}
268
269impl From<FileError> for StoreLockError {
270 fn from(error: FileError) -> Self {
271 match error {
272 FileError::Bug(bug) => Self::Bug(bug),
273 error => Self::File(error),
274 }
275 }
276}
277
278impl crate::task::CarriesBug for StoreLockError {
279 fn bug(&self) -> Option<&crate::task::Bug> {
280 match self {
281 Self::Bug(bug) => Some(bug),
282 Self::File(error) => error.bug(),
283 _ => None,
284 }
285 }
286}
287
288fn open_lock(path: &Path) -> Result<File, FileError> {
289 OpenOptions::new()
290 .read(true)
291 .write(true)
292 .create(true)
293 .truncate(false)
294 .open(path)
295 .map_err(|source| FileError::at("open store lock", path, source))
296}
297
298pub(crate) fn try_lock(path: &Path, id: StoreId, shared: bool) -> Result<FileLock, StoreLockError> {
299 let mode = if shared { LockMode::Shared } else { LockMode::Exclusive };
300 match FileLock::try_lock(open_lock(path)?, mode) {
301 Ok(file) => Ok(file),
302 Err(TryLockError::WouldBlock) => Err(StoreLockError::AlreadyOpen(id)),
303 Err(TryLockError::Error(source)) => Err(FileError::at("lock store", path, source).into()),
304 }
305}
306
307pub(crate) fn lock_layout(root: &Path) -> Result<FileLock, FileError> {
311 wait_for_lock(&layout_lock(root), LockMode::Exclusive, "lock store layout")
312}
313
314pub(crate) fn layout_lock(root: &Path) -> PathBuf {
317 root.join(".coven-stores.lock")
318}
319
320#[cfg(any(test, feature = "test-utils"))]
321pub(crate) fn acquire(directory: &Path, id: StoreId) -> Result<StoreLock, StoreLockError> {
322 let directory = resolve_directory(directory)?;
323 let file = registry::try_acquire(&directory, id, LockKind::Writer)?;
324 Ok(StoreLock { store: LockedStore { _file: file, directory, id } })
325}
326
327pub(crate) async fn acquire_for_open(
328 directory: &Path,
329 id: StoreId,
330 bugs: &crate::task::BugSinkRef,
331) -> Result<StoreLock, StoreLockError> {
332 let directory = resolve_directory(directory)?;
333 let file = registry::acquire_registered(&directory, id, LockKind::Writer, bugs).await?;
334 Ok(StoreLock { store: LockedStore { _file: file, directory, id } })
335}
336
337pub(crate) async fn acquire_closed(
341 directory: &Path,
342 id: StoreId,
343 bugs: &crate::task::BugSinkRef,
344) -> Result<ClosedStoreLock, StoreLockError> {
345 let directory = resolve_directory(directory)?;
346 Ok(ClosedStoreLock { _file: registry::acquire_closing_writer(&directory, id, bugs).await? })
347}
348
349pub(crate) async fn closed(directory: &Path, id: StoreId) -> Result<(), FileError> {
352 let directory = match std::fs::canonicalize(directory) {
353 Ok(directory) => directory,
354 Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()),
355 Err(source) => return Err(FileError::at("resolve store directory", directory, source)),
356 };
357 registry::closed(&directory, id).await;
358 Ok(())
359}
360
361pub(super) async fn acquire_bootstrap(
362 directory: &Path,
363 id: StoreId,
364 bugs: &crate::task::BugSinkRef,
365) -> Result<RegisteredLock, StoreLockError> {
366 registry::acquire_registered(directory, id, LockKind::Bootstrap, bugs).await
367}
368
369fn resolve_directory(directory: &Path) -> Result<PathBuf, FileError> {
370 std::fs::canonicalize(directory)
371 .map_err(|source| FileError::at("resolve store directory", directory, source))
372}
373
374pub(super) fn exclude_readers(directory: &Path, id: StoreId) -> Result<FileLock, StoreLockError> {
375 let _layout = lock_layout(directory.parent().expect("store parent"))?;
376 try_lock(&lock_paths(directory, id)[1], id, false)
377}
378
379pub(crate) fn acquire_reader(
380 directory: &Path,
381 id: StoreId,
382) -> Result<StoreReadLock, StoreLockError> {
383 let directory = resolve_directory(directory)?;
384 Ok(StoreReadLock { _file: registry::acquire_reader(&directory, id)? })
385}
386
387pub(super) fn lock_existing(
388 directory: &Path,
389 id: StoreId,
390 shared: bool,
391) -> Result<(PathBuf, FileLock), StoreLockError> {
392 let directory = std::fs::canonicalize(directory)
393 .map_err(|source| FileError::at("resolve store directory", directory, source))?;
394 let _layout = lock_layout(directory.parent().expect("store parent"))?;
395 std::fs::canonicalize(&directory)
397 .map_err(|source| FileError::at("resolve store directory", &directory, source))?;
398 let [writer, readers] = lock_paths(&directory, id);
399 let path = if shared { readers } else { writer };
400 let file = try_lock(&path, id, shared)?;
401 Ok((directory, file))
402}
403
404pub(crate) fn lock_paths(directory: &Path, id: StoreId) -> [PathBuf; 2] {
405 [
406 directory.with_file_name(format!(".{id}.lock")),
407 directory.with_file_name(format!(".{id}.readers.lock")),
408 ]
409}
410
411fn bootstrap_lock_path(directory: &Path, id: StoreId) -> PathBuf {
414 directory.with_file_name(format!(".coven-bootstrap-{id}.lock"))
415}
416
417#[cfg(any(test, feature = "test-utils"))]
421pub fn writer_lock(directory: &Path, id: StoreId) -> PathBuf {
422 let [writer, _] = lock_paths(directory, id);
423 writer
424}
425
426#[cfg(any(test, feature = "test-utils"))]
429pub fn bootstrap_lock(directory: &Path, id: StoreId) -> PathBuf {
430 bootstrap_lock_path(directory, id)
431}
432
433#[cfg(any(test, feature = "test-utils"))]
435pub(super) fn kind_lock(directory: &Path, id: StoreId, kind: LockKind) -> PathBuf {
436 match kind {
437 LockKind::Writer => writer_lock(directory, id),
438 LockKind::Reader => lock_paths(directory, id)[1].clone(),
439 LockKind::Bootstrap => bootstrap_lock(directory, id),
440 }
441}
442
443pub(super) fn lock_bootstrap(directory: &Path, id: StoreId) -> Result<FileLock, StoreLockError> {
446 let _layout = lock_layout(directory.parent().expect("store parent"))?;
447 try_lock(&bootstrap_lock_path(directory, id), id, false)
448}
449
450fn deletion_lock_paths(directory: &Path, id: StoreId) -> [PathBuf; 3] {
454 let [writer, readers] = lock_paths(directory, id);
455 [bootstrap_lock_path(directory, id), writer, readers]
456}
457
458pub(crate) fn deletion_path(directory: &Path, id: StoreId) -> PathBuf {
459 directory.with_file_name(format!(".coven-delete-{id}"))
460}
461
462fn inspect_directory(directory: &Path) -> Result<bool, FileError> {
463 match std::fs::symlink_metadata(directory) {
464 Ok(metadata) if metadata.file_type().is_dir() => Ok(true),
465 Ok(_) => Err(FileError::at(
466 "inspect deletion directory",
467 directory,
468 std::io::Error::new(std::io::ErrorKind::InvalidInput, "store is not a directory"),
469 )),
470 Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
471 tracing::debug!(path = %directory.display(), "store directory is absent");
472 Ok(false)
473 }
474 Err(source) => Err(FileError::at("inspect store directory", directory, source)),
475 }
476}
477
478pub(super) fn for_deletion(
483 directory: &Path,
484 id: StoreId,
485 lease: Option<&mut Option<RegisteredLock>>,
486) -> Result<Option<StoreDeletionLock>, StoreLockError> {
487 let root = directory.parent().expect("store parent");
488 let root = match std::fs::canonicalize(root) {
489 Ok(root) => root,
490 Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
491 tracing::debug!(path = %root.display(), "store layout is absent");
492 return Ok(None);
493 }
494 Err(source) => return Err(FileError::at("resolve store layout", root, source).into()),
495 };
496 let _layout = lock_layout(&root)?;
497 let published = root.join(id.to_string());
498 let directory =
499 if inspect_directory(&published)? { published } else { deletion_path(&published, id) };
500 let paths = deletion_lock_paths(&directory, id);
501 if !inspect_directory(&directory)? {
502 let mut remaining = false;
503 for path in &paths {
504 remaining |= atomic_file::exists("inspect store lock", path)?;
505 }
506 if !remaining {
507 #[cfg(unix)]
510 sync_removal(&directory)?;
511 tracing::debug!(store = %id, "store and its locks are absent");
512 return Ok(None);
513 }
514 }
515 let [bootstrap, writer, readers] = paths;
516 let others =
517 || Ok::<_, StoreLockError>((try_lock(&writer, id, false)?, try_lock(&readers, id, false)?));
518 let (bootstrap, (writer, readers)) = match lease {
519 None => (HeldBootstrap::Locked(try_lock(&bootstrap, id, false)?), others()?),
522 Some(lease) => {
524 let others = others()?;
525 let held = lease.take().expect("a bootstrap hands over the bootstrap lock it holds");
526 (HeldBootstrap::Leased(held), others)
527 }
528 };
529 Ok(Some(StoreDeletionLock {
530 store: LockedStore { _file: writer, directory, id },
531 _readers: readers,
532 bootstrap,
533 }))
534}
535
536#[cfg(test)]
538pub(crate) fn store_entries(root: &Path) -> usize {
539 std::fs::read_dir(root)
540 .unwrap()
541 .filter(|entry| entry.as_ref().unwrap().file_name() != ".coven-stores.lock")
542 .count()
543}
544
545#[cfg(test)]
546#[path = "lock_tests.rs"]
547mod tests;