diff options
author | Lars Wirzenius <liw@liw.fi> | 2022-12-17 17:03:56 +0000 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2022-12-17 17:03:56 +0000 |
commit | 4813c370e9cefca80f4158cc070cebdf7a57d24d (patch) | |
tree | b3db31d9be48da98dfc4f5f8655fc8d3cbbcee92 | |
parent | d951606f5eaf5611c9fb114ccf4b1c39e6ddf7c9 (diff) | |
parent | fdd7f9abd935420c1d2403be9be8833d2b59d262 (diff) | |
download | sshca-4813c370e9cefca80f4158cc070cebdf7a57d24d.tar.gz |
Merge branch 'fix-create-store-dir' into 'main'
fix: create directory where store file is, if necessary
Closes #29
See merge request larswirzenius/sshca!64
-rw-r--r-- | src/error.rs | 3 | ||||
-rw-r--r-- | src/util.rs | 5 | ||||
-rw-r--r-- | sshca.md | 2 |
3 files changed, 8 insertions, 2 deletions
diff --git a/src/error.rs b/src/error.rs index 704f4ad..5ef6d9d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -63,6 +63,9 @@ pub enum CAError { #[error("Failed to read {0}")] ReadError(PathBuf, #[source] std::io::Error), + #[error("Could not create directory {0}")] + CreateDir(PathBuf, #[source] std::io::Error), + #[error("Could not create file {0}")] Create(PathBuf, #[source] std::io::Error), diff --git a/src/util.rs b/src/util.rs index a589702..358bb52 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,7 +1,7 @@ //! Utility functions for SSH CA tool. use crate::error::CAError; -use std::fs::{File, Permissions}; +use std::fs::{create_dir_all, File, Permissions}; use std::io::Write; use std::os::unix::fs::PermissionsExt; use std::path::Path; @@ -22,6 +22,9 @@ pub(crate) fn read_as_string<P: AsRef<Path>>(filename: P) -> Result<String, CAEr /// Write string to file. pub fn write_string<P: AsRef<Path>>(filename: P, s: &str) -> Result<(), CAError> { let filename = filename.as_ref(); + if let Some(parent) = filename.parent() { + create_dir_all(parent).map_err(|e| CAError::CreateDir(parent.into(), e))?; + } let mut file = File::create(filename).map_err(|err| CAError::Create(filename.to_path_buf(), err))?; let ro_user = Permissions::from_mode(0o600); @@ -270,7 +270,7 @@ then stdout contains "[]" ~~~ ~~~{#config.yaml .file .yaml} -store: store.yaml +store: xdg/data/dir/store.yaml ~~~ When we create two new CA keys, for host and user certification, they |