summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2022-12-17 19:03:10 +0200
committerLars Wirzenius <liw@liw.fi>2022-12-17 19:03:10 +0200
commitfdd7f9abd935420c1d2403be9be8833d2b59d262 (patch)
treeb3db31d9be48da98dfc4f5f8655fc8d3cbbcee92
parentd951606f5eaf5611c9fb114ccf4b1c39e6ddf7c9 (diff)
downloadsshca-fdd7f9abd935420c1d2403be9be8833d2b59d262.tar.gz
fix: create directory where store file is, if necessary
Sponsored-by: author
-rw-r--r--src/error.rs3
-rw-r--r--src/util.rs5
-rw-r--r--sshca.md2
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);
diff --git a/sshca.md b/sshca.md
index e7f4867..f999540 100644
--- a/sshca.md
+++ b/sshca.md
@@ -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