summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--core/Cargo.toml1
-rw-r--r--core/src/lib.rs50
-rw-r--r--ffi/src/lib.rs15
-rw-r--r--ffi/src/sequoia.h10
4 files changed, 74 insertions, 2 deletions
diff --git a/core/Cargo.toml b/core/Cargo.toml
index 034cc2fd..caafe03c 100644
--- a/core/Cargo.toml
+++ b/core/Cargo.toml
@@ -4,3 +4,4 @@ version = "0.1.0"
authors = ["Justus Winter <justus@pep-project.org>"]
[dependencies]
+tempdir = "0.3.5"
diff --git a/core/src/lib.rs b/core/src/lib.rs
index 1ef2cf54..bfd3cf9d 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -1,9 +1,12 @@
//! Core functionality.
+extern crate tempdir;
+
use std::env;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
+use tempdir::TempDir;
/// A `&Context` for Sequoia.
///
@@ -39,6 +42,8 @@ pub struct Context {
home: PathBuf,
lib: PathBuf,
network_policy: NetworkPolicy,
+ ephemeral: bool,
+ temp_dir: Option<TempDir>,
}
/// Returns $PREXIX, or a reasonable default prefix.
@@ -73,6 +78,8 @@ impl Context {
.join(".sequoia"),
lib: prefix().join("lib").join("sequoia"),
network_policy: NetworkPolicy::Encrypted,
+ ephemeral: false,
+ temp_dir: None,
})
}
@@ -96,6 +103,10 @@ impl Context {
&self.network_policy
}
+ /// Returns whether or not this is an ephemeral context.
+ pub fn ephemeral(&self) -> bool {
+ self.ephemeral
+ }
}
/// Represents a `Context` configuration.
@@ -113,13 +124,37 @@ impl Context {
/// # Ok(())
/// # }
/// ```
+///
+/// You can create ephemeral context that are useful for tests and
+/// one-shot programs:
+///
+/// ```
+/// # use sequoia_core::{Context, Result};
+/// # use std::path::Path;
+/// # f().unwrap();
+/// # fn f() -> Result<()> {
+/// let c = Context::configure("org.example.my.test")
+/// .ephemeral().build()?;
+/// let ephemeral_home = c.home().to_path_buf();
+/// // Do some tests.
+/// drop(c);
+/// assert!(! ephemeral_home.exists());
+/// # Ok(())
+/// # }
+/// ```
pub struct Config(Context);
impl Config {
/// Finalizes the configuration and returns a `Context`.
pub fn build(self) -> Result<Context> {
- let c = self.0;
- fs::create_dir_all(c.home())?;
+ 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);
+ } else {
+ fs::create_dir_all(c.home())?;
+ }
Ok(c)
}
@@ -155,6 +190,17 @@ impl Config {
pub fn set_network_policy(&mut self, policy: NetworkPolicy) {
self.0.network_policy = policy;
}
+
+ /// Makes this context ephemeral.
+ pub fn ephemeral(mut self) -> Self {
+ self.set_ephemeral();
+ self
+ }
+
+ /// Makes this context ephemeral.
+ pub fn set_ephemeral(&mut self) {
+ self.0.ephemeral = true;
+ }
}
/* Error handling. */
diff --git a/ffi/src/lib.rs b/ffi/src/lib.rs
index 034299c1..08a64c30 100644
--- a/ffi/src/lib.rs
+++ b/ffi/src/lib.rs
@@ -129,6 +129,13 @@ pub extern "system" fn sq_context_network_policy(ctx: Option<&Context>) -> uint8
}
}
+/// Returns whether or not this is an ephemeral context.
+#[no_mangle]
+pub extern "system" fn sq_context_ephemeral(ctx: Option<&Context>) -> uint8_t {
+ assert!(ctx.is_some());
+ if ctx.unwrap().ephemeral() { 1 } else { 0 }
+}
+
/* sequoia::Config. */
@@ -186,6 +193,14 @@ pub extern "system" fn sq_config_network_policy(cfg: Option<&mut Config>,
});
}
+/// Makes this context ephemeral.
+#[no_mangle]
+pub extern "system" fn sq_config_ephemeral(cfg: Option<&mut Config>) {
+ assert!(cfg.is_some());
+ cfg.unwrap().set_ephemeral();
+}
+
+
/* openpgp::types. */
/// Returns a KeyID with the given `id`.
diff --git a/ffi/src/sequoia.h b/ffi/src/sequoia.h
index c19db967..a39c63f4 100644
--- a/ffi/src/sequoia.h
+++ b/ffi/src/sequoia.h
@@ -94,6 +94,11 @@ const char *sq_context_lib(const struct sq_context *ctx);
/*/
uint8_t sq_context_network_policy(const struct sq_context *ctx);
+/*/
+/// Returns whether or not this is an ephemeral context.
+/*/
+uint8_t sq_context_ephemeral(const struct sq_context *ctx);
+
/* sequoia::Config. */
@@ -119,6 +124,11 @@ void sq_config_lib(struct sq_config *cfg, const char *lib);
/*/
void sq_config_network_policy(struct sq_config *cfg, uint8_t policy);
+/*/
+/// Makes this context ephemeral.
+/*/
+void sq_config_ephemeral(struct sq_config *cfg);
+
/* sequoia::openpgp::types. */