diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2018-01-25 14:21:51 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2018-01-25 17:58:37 +0100 |
commit | c016b29c530614445d102d824e8f2adb5cc57708 (patch) | |
tree | 0a266f8f02d7cea45babd9e1a55995a6441eda89 | |
parent | 3956b4d7f0512a3014192c4b79274caf7eaadc66 (diff) |
core: Make the Context Send + Clone.
- We no longer rely on TempDir to clean up the directory, but we do
it in Drop. Since we no longer need the TempDir around, we are
now Send.
- Implement Clone so that cloned contexts do not delete the
temporary home when dropped.
-rw-r--r-- | core/src/lib.rs | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/core/src/lib.rs b/core/src/lib.rs index ed2452ce..ee17137c 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -66,7 +66,31 @@ pub struct Context { network_policy: NetworkPolicy, ipc_policy: IPCPolicy, ephemeral: bool, - temp_dir: Option<TempDir>, + cleanup: bool, +} + +impl Clone for Context { + fn clone(&self) -> Self { + Context { + domain: self.domain.clone(), + home: self.home.clone(), + lib: self.lib.clone(), + network_policy: self.network_policy, + ipc_policy: self.ipc_policy, + ephemeral: self.ephemeral, + cleanup: false, // Prevent cleanup. + } + } +} + +impl Drop for Context { + fn drop(&mut self) { + use std::fs::remove_dir_all; + + if self.ephemeral && self.cleanup { + let _ = remove_dir_all(&self.home); + } + } } /// Returns $PREXIX at compile-time, or a reasonable default prefix. @@ -103,7 +127,7 @@ impl Context { network_policy: NetworkPolicy::Encrypted, ipc_policy: IPCPolicy::Robust, ephemeral: false, - temp_dir: None, + cleanup: false, }) } @@ -180,8 +204,8 @@ impl Config { let mut c = self.0; if c.ephemeral { let tmp = TempDir::new("sequoia")?; - c.home = tmp.path().clone().to_path_buf(); - c.temp_dir = Some(tmp); + c.home = tmp.into_path(); + c.cleanup = true; } else { fs::create_dir_all(c.home())?; } |