From d83bb1bf4dc11a7566c83670145b91a66f70a3c2 Mon Sep 17 00:00:00 2001 From: Scott Lamb Date: Sat, 17 Oct 2020 16:53:57 -0700 Subject: [PATCH] better error msg when db dir open fails The previous poor error message was reported by Marlon Hendred: https://groups.google.com/g/moonfire-nvr-users/c/X9k2cOCJUDQ/m/1y1ikrWoAAAJ --- src/cmds/mod.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/cmds/mod.rs b/src/cmds/mod.rs index e635bc5..7025602 100644 --- a/src/cmds/mod.rs +++ b/src/cmds/mod.rs @@ -53,11 +53,17 @@ enum OpenMode { /// Locks the directory without opening the database. /// The returned `dir::Fd` holds the lock and should be kept open as long as the `Connection` is. fn open_dir(db_dir: &Path, mode: OpenMode) -> Result { - let dir = dir::Fd::open(db_dir, mode == OpenMode::Create)?; + let dir = dir::Fd::open(db_dir, mode == OpenMode::Create) + .map_err(|e| e.context(if e == nix::Error::Sys(nix::errno::Errno::ENOENT) { + format!("db dir {} not found; try running moonfire-nvr init", + db_dir.display()) + } else { + format!("unable to open db dir {}: {}", db_dir.display(), &e) + }))?; let ro = mode == OpenMode::ReadOnly; dir.lock(if ro { FlockArg::LockSharedNonblock } else { FlockArg::LockExclusiveNonblock }) - .map_err(|e| e.context(format!("db dir {:?} already in use; can't get {} lock", - db_dir, if ro { "shared" } else { "exclusive" })))?; + .map_err(|e| e.context(format!("db dir {} already in use; can't get {} lock", + db_dir.display(), if ro { "shared" } else { "exclusive" })))?; Ok(dir) }