pub fn try_flock<P: AsRef<Path>>(
path: P,
mode: FlockMode,
) -> Result<Option<OwnedFd>>Expand description
Open a lock file and attempt flock with LOCK_NB.
Creates the file with mode 0o666 if absent. Returns
Ok(Some(fd)) on successful acquire, Ok(None) on
EWOULDBLOCK (peer already holds an incompatible lock), and
propagates other errors. The returned fd owns the open-file
description; dropping it closes the fd AND releases the kernel
flock (the kernel releases flock(2) only when the last fd
referring to its OFD closes — OwnedFd::drop is what makes that
work).
O_CLOEXEC is mandatory: a leaked fd across exec(2) (cargo
subcommand, build-pipeline subprocess, initramfs compressor) would
keep the lock alive in the child process after the parent’s
OwnedFd::drop runs, producing phantom holders the next acquirer
would blame on the wrong pid.
Calls super::fs_filter::reject_remote_fs before the open to
fail-fast on NFS / CIFS / SMB2 / CEPH / AFS / FUSE — see the
module-level rationale.
Accepts any AsRef<Path> so &str, &Path, &PathBuf, and
String callers all work without string-ifying round trips. LLC
lockfile paths are built as String via format! and cache
lockfile paths are built as PathBuf via Path::join — both
pass straight through.