dirmgr: Add a helper to create LockFile errors.

This commit is contained in:
Nick Mathewson 2022-07-18 10:10:28 -04:00
parent c983987782
commit 3c424c82d7
2 changed files with 9 additions and 8 deletions

View File

@ -180,6 +180,12 @@ impl Error {
Error::NetDocError { source, cause }
}
/// Construct a new `Error` from `std::io::Error` for an error that occurred
/// while locking a file.
pub(crate) fn from_lockfile(err: std::io::Error) -> Error {
Error::LockFile(Arc::new(err))
}
/// Return true if this error is serious enough that we should mark this
/// cache as having failed.
pub(crate) fn indicates_cache_failure(&self) -> bool {

View File

@ -93,13 +93,8 @@ impl SqliteStore {
}
}
let mut lockfile =
fslock::LockFile::open(&lockpath).map_err(|e| Error::LockFile(Arc::new(e)))?;
if !readonly
&& !lockfile
.try_lock()
.map_err(|e| Error::LockFile(Arc::new(e)))?
{
let mut lockfile = fslock::LockFile::open(&lockpath).map_err(Error::from_lockfile)?;
if !readonly && !lockfile.try_lock().map_err(Error::from_lockfile)? {
readonly = true; // we couldn't get the lock!
};
let flags = if readonly {
@ -276,7 +271,7 @@ impl Store for SqliteStore {
.lockfile
.as_mut()
.expect("No lockfile open; cannot upgrade to read-write storage");
if !lf.try_lock().map_err(|e| Error::LockFile(Arc::new(e)))? {
if !lf.try_lock().map_err(Error::from_lockfile)? {
// Somebody else has the lock.
return Ok(false);
}