MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / try_lock_file

Function try_lock_file

atomic-repository/src/repository/locks.rs:590–632  ·  view source on GitHub ↗
(
    path: &Path,
    kind: RepositoryLockKind,
    reentrant_owner: bool,
)

Source from the content-addressed store, hash-verified

588}
589
590fn try_lock_file(
591 path: &Path,
592 kind: RepositoryLockKind,
593 reentrant_owner: bool,
594) -> Result<AdvisoryFileLock, RepositoryError> {
595 let path = path.to_path_buf();
596 let reentrant = HELD_LOCKS.with(|held| {
597 let mut held = held.borrow_mut();
598 let (count, file, reentrant) = held.get_mut(&path)?;
599 if !*reentrant {
600 return None;
601 }
602 let file = file.upgrade()?;
603 *count += 1;
604 Some(file)
605 });
606 if let Some(file) = reentrant {
607 return Ok(AdvisoryFileLock { file, path });
608 }
609 if let Some(parent) = path.parent() {
610 std::fs::create_dir_all(parent)?;
611 }
612 let file = OpenOptions::new()
613 .create(true)
614 .truncate(false)
615 .read(true)
616 .write(true)
617 .open(&path)?;
618 match file.try_lock_exclusive() {
619 Ok(()) => {
620 let file = Arc::new(file);
621 HELD_LOCKS.with(|held| {
622 held.borrow_mut()
623 .insert(path.clone(), (1, Arc::downgrade(&file), reentrant_owner));
624 });
625 Ok(AdvisoryFileLock { file, path })
626 }
627 Err(error) if is_lock_contended(&error) => {
628 Err(RepositoryError::LockContended { lock: kind, path })
629 }
630 Err(error) => Err(RepositoryError::Io(error)),
631 }
632}
633
634/// Classify the cross-platform error returned by a contended `fs2` try-lock.
635pub(super) fn is_lock_contended(error: &std::io::Error) -> bool {

Calls 10

get_mutMethod · 0.80
parentMethod · 0.80
truncateMethod · 0.80
is_lock_contendedFunction · 0.70
openMethod · 0.45
writeMethod · 0.45
readMethod · 0.45
createMethod · 0.45
insertMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected