summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2018-01-25 15:47:17 +0100
committerJustus Winter <justus@sequoia-pgp.org>2018-01-25 18:01:16 +0100
commit10a5646d02d48a14146c960a0223561667364a52 (patch)
treeab74fc701ce259dc7dbc48d26464378807a691b7 /core
parent9d174f4152c509a7b8d2c6ac18d26d2b3401265c (diff)
core: Refine default home directory.
- Move the call to env::home_dir from Context::configure to Config.build. This lets us handle failures, which should be rare. On systems where it fails (think embedded systems), users have to explicitly provide a path. - Allow ephemeral contexts with explicitly configure home directories. E.g. clients and servers share ephemeral contexts with the same home directory.
Diffstat (limited to 'core')
-rw-r--r--core/src/lib.rs20
1 files changed, 17 insertions, 3 deletions
diff --git a/core/src/lib.rs b/core/src/lib.rs
index ee17137c..470916a5 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -121,8 +121,7 @@ impl Context {
pub fn configure(domain: &str) -> Config {
Config(Context {
domain: String::from(domain),
- home: env::home_dir().unwrap_or(env::temp_dir())
- .join(".sequoia"),
+ home: PathBuf::from(""), // Defer computation of default.
lib: prefix().join("lib").join("sequoia"),
network_policy: NetworkPolicy::Encrypted,
ipc_policy: IPCPolicy::Robust,
@@ -202,11 +201,26 @@ impl Config {
/// Finalizes the configuration and returns a `Context`.
pub fn build(self) -> Result<Context> {
let mut c = self.0;
- if c.ephemeral {
+
+ // As a special case, we defer the computation of the default
+ // home, because env::home_dir() may fail.
+ let home_not_set = c.home == PathBuf::from("");
+
+ // If we have an ephemeral home, and home is not explicitly
+ // set, create a temporary directory. Ephemeral contexts can
+ // share home directories, e.g. client and server processes
+ // share one home.
+ if c.ephemeral && home_not_set {
let tmp = TempDir::new("sequoia")?;
c.home = tmp.into_path();
c.cleanup = true;
} else {
+ if home_not_set {
+ c.home =
+ env::home_dir().ok_or(
+ format_err!("Failed to get users home directory"))?
+ .join(".sequoia");
+ }
fs::create_dir_all(c.home())?;
}
Ok(c)