core/config.rs: add context for I/O errors
Signed-off-by: Manos Pitsidianakis <manos@pitsidianak.is>add-rfc9500-test-keys
parent
c470a6129d
commit
e7ca77aa8a
|
@ -116,9 +116,10 @@ For more information, try '--help'."#,
|
|||
output.code(255).stderr(predicates::str::is_empty()).stdout(
|
||||
predicate::eq(
|
||||
format!(
|
||||
"[1] Could not read configuration file from path: {} Caused by:\n[2] Error \
|
||||
returned from internal I/O operation: No such file or directory (os error 2)",
|
||||
conf.display()
|
||||
"[1] Could not read configuration file from path: {path} Caused by:\n[2] \
|
||||
Configuration file {path} not found. Caused by:\n[3] Error returned from \
|
||||
internal I/O operation: No such file or directory (os error 2)",
|
||||
path = conf.display()
|
||||
)
|
||||
.as_str(),
|
||||
)
|
||||
|
|
|
@ -76,14 +76,18 @@ impl Configuration {
|
|||
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
|
||||
let path = path.as_ref();
|
||||
let mut s = String::new();
|
||||
let mut file = std::fs::File::open(path)?;
|
||||
file.read_to_string(&mut s)?;
|
||||
let mut file = std::fs::File::open(path)
|
||||
.with_context(|| format!("Configuration file {} not found.", path.display()))?;
|
||||
file.read_to_string(&mut s)
|
||||
.with_context(|| format!("Could not read from file {}.", path.display()))?;
|
||||
let config: Self = toml::from_str(&s)
|
||||
.map_err(anyhow::Error::from)
|
||||
.context(format!(
|
||||
"Could not parse configuration file `{}` successfully: ",
|
||||
path.display()
|
||||
))?;
|
||||
.with_context(|| {
|
||||
format!(
|
||||
"Could not parse configuration file `{}` successfully: ",
|
||||
path.display()
|
||||
)
|
||||
})?;
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
|
@ -106,14 +110,20 @@ impl Configuration {
|
|||
}
|
||||
|
||||
debug_assert!(path != self.db_path());
|
||||
let mut file = std::fs::File::create(&path)?;
|
||||
let metadata = file.metadata()?;
|
||||
let mut file = std::fs::File::create(&path)
|
||||
.with_context(|| format!("Could not create file {}.", path.display()))?;
|
||||
let metadata = file
|
||||
.metadata()
|
||||
.with_context(|| format!("Could not fstat file {}.", path.display()))?;
|
||||
let mut permissions = metadata.permissions();
|
||||
|
||||
permissions.set_mode(0o600); // Read/write for owner only.
|
||||
file.set_permissions(permissions)?;
|
||||
file.write_all(msg.as_bytes())?;
|
||||
file.flush()?;
|
||||
file.set_permissions(permissions)
|
||||
.with_context(|| format!("Could not chmod 600 file {}.", path.display()))?;
|
||||
file.write_all(msg.as_bytes())
|
||||
.with_context(|| format!("Could not write message to file {}.", path.display()))?;
|
||||
file.flush()
|
||||
.with_context(|| format!("Could not flush message I/O to file {}.", path.display()))?;
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
|
|
|
@ -187,7 +187,9 @@ impl Connection {
|
|||
INIT_SQLITE_LOGGING.call_once(|| {
|
||||
_ = unsafe { rusqlite::trace::config_log(Some(log_callback)) };
|
||||
});
|
||||
let conn = DbConnection::open(conf.db_path.to_str().unwrap())?;
|
||||
let conn = DbConnection::open(conf.db_path.to_str().unwrap()).with_context(|| {
|
||||
format!("sqlite3 library could not open {}.", conf.db_path.display())
|
||||
})?;
|
||||
rusqlite::vtab::array::load_module(&conn)?;
|
||||
conn.pragma_update(None, "journal_mode", "WAL")?;
|
||||
conn.pragma_update(None, "foreign_keys", "on")?;
|
||||
|
@ -346,7 +348,14 @@ impl Connection {
|
|||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()?;
|
||||
.spawn()
|
||||
.with_context(|| {
|
||||
format!(
|
||||
"Could not launch {} {}.",
|
||||
std::env::var("SQLITE_BIN").unwrap_or_else(|_| "sqlite3".into()),
|
||||
db_path.display()
|
||||
)
|
||||
})?;
|
||||
let mut stdin = child.stdin.take().unwrap();
|
||||
std::thread::spawn(move || {
|
||||
stdin
|
||||
|
@ -381,12 +390,16 @@ impl Connection {
|
|||
.into());
|
||||
}
|
||||
|
||||
let file = std::fs::File::open(db_path)?;
|
||||
let metadata = file.metadata()?;
|
||||
let file = std::fs::File::open(db_path)
|
||||
.with_context(|| format!("Could not open database {}.", db_path.display()))?;
|
||||
let metadata = file
|
||||
.metadata()
|
||||
.with_context(|| format!("Could not fstat database {}.", db_path.display()))?;
|
||||
let mut permissions = metadata.permissions();
|
||||
|
||||
permissions.set_mode(0o600); // Read/write for owner only.
|
||||
file.set_permissions(permissions)?;
|
||||
file.set_permissions(permissions)
|
||||
.with_context(|| format!("Could not chmod 600 database {}.", db_path.display()))?;
|
||||
}
|
||||
Self::open_db(conf)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue