From 99a73ccb032cb49fc64068e90c0c35ed3a6bdf3a Mon Sep 17 00:00:00 2001 From: Thom Chiovoloni Date: Wed, 13 Jun 2018 17:23:50 -0700 Subject: [PATCH] Avoid using 1.26.0-only features when using sqlcipher, and move the sqlcipher Store support to the correct file --- db/src/db.rs | 8 +++++--- src/conn.rs | 42 ------------------------------------------ src/store.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 45 deletions(-) diff --git a/db/src/db.rs b/db/src/db.rs index 8da5fe73..bfde80a9 100644 --- a/db/src/db.rs +++ b/db/src/db.rs @@ -125,12 +125,14 @@ pub fn new_connection(uri: T) -> rusqlite::Result where } #[cfg(feature = "sqlcipher")] -pub fn new_connection_with_key(uri: impl AsRef, encryption_key: impl AsRef) -> rusqlite::Result { +pub fn new_connection_with_key(uri: P, encryption_key: S) -> rusqlite::Result +where P: AsRef, S: AsRef { make_connection(uri.as_ref(), Some(encryption_key.as_ref())) } #[cfg(feature = "sqlcipher")] -pub fn change_encryption_key(conn: &rusqlite::Connection, encryption_key: impl AsRef) -> rusqlite::Result<()> { +pub fn change_encryption_key(conn: &rusqlite::Connection, encryption_key: S) -> rusqlite::Result<()> +where S: AsRef { let escaped = escape_string_for_pragma(encryption_key.as_ref()); // `conn.execute` complains that this returns a result, and using a query // for it requires more boilerplate. @@ -2752,7 +2754,7 @@ mod tests { } #[cfg(feature = "sqlcipher")] - fn test_open_fail(opener: impl FnOnce() -> rusqlite::Result) { + fn test_open_fail(opener: F) where F: FnOnce() -> rusqlite::Result { let err = opener().expect_err("Should fail to open encrypted DB"); match err { rusqlite::Error::SqliteFailure(err, ..) => { diff --git a/src/conn.rs b/src/conn.rs index 182ce77b..894de855 100644 --- a/src/conn.rs +++ b/src/conn.rs @@ -160,48 +160,6 @@ pub struct Conn { pub(crate) tx_observer_service: Mutex, } -#[cfg(feature = "sqlcipher")] -impl Store { - /// Variant of `open` that allows a key (for encryption/decryption) to be - /// supplied. Fails unless linked against sqlcipher (or something else that - /// supports the Sqlite Encryption Extension). - pub fn open_with_key(path: &str, encryption_key: &str) -> Result { - let mut connection = ::new_connection_with_key(path, encryption_key)?; - let conn = Conn::connect(&mut connection)?; - Ok(Store { - conn: conn, - sqlite: connection, - }) - } - - /// Variant of `open_empty` that allows a key (for encryption/decryption) to - /// be supplied. Fails unless linked against sqlcipher (or something else - /// that supports the Sqlite Encryption Extension). - pub fn open_empty_with_key(path: &str, encryption_key: &str) -> Result { - if !path.is_empty() { - if Path::new(path).exists() { - bail!(ErrorKind::PathAlreadyExists(path.to_string())); - } - } - - let mut connection = ::new_connection_with_key(path, encryption_key)?; - let conn = Conn::empty(&mut connection)?; - Ok(Store { - conn: conn, - sqlite: connection, - }) - } - - /// Change the key for a database that was opened using `open_with_key` or - /// `open_empty_with_key` (using `PRAGMA rekey`). Fails unless linked - /// against sqlcipher (or something else that supports the Sqlite Encryption - /// Extension). - pub fn change_encryption_key(&mut self, new_encryption_key: &str) -> Result<()> { - ::change_encryption_key(&self.sqlite, new_encryption_key)?; - Ok(()) - } -} - pub trait Queryable { fn q_explain(&self, query: &str, inputs: T) -> Result where T: Into>; diff --git a/src/store.rs b/src/store.rs index 871f470d..0fd106f8 100644 --- a/src/store.rs +++ b/src/store.rs @@ -104,6 +104,48 @@ impl Store { } } +#[cfg(feature = "sqlcipher")] +impl Store { + /// Variant of `open` that allows a key (for encryption/decryption) to be + /// supplied. Fails unless linked against sqlcipher (or something else that + /// supports the Sqlite Encryption Extension). + pub fn open_with_key(path: &str, encryption_key: &str) -> Result { + let mut connection = ::new_connection_with_key(path, encryption_key)?; + let conn = Conn::connect(&mut connection)?; + Ok(Store { + conn: conn, + sqlite: connection, + }) + } + + /// Variant of `open_empty` that allows a key (for encryption/decryption) to + /// be supplied. Fails unless linked against sqlcipher (or something else + /// that supports the Sqlite Encryption Extension). + pub fn open_empty_with_key(path: &str, encryption_key: &str) -> Result { + if !path.is_empty() { + if Path::new(path).exists() { + bail!(ErrorKind::PathAlreadyExists(path.to_string())); + } + } + + let mut connection = ::new_connection_with_key(path, encryption_key)?; + let conn = Conn::empty(&mut connection)?; + Ok(Store { + conn: conn, + sqlite: connection, + }) + } + + /// Change the key for a database that was opened using `open_with_key` or + /// `open_empty_with_key` (using `PRAGMA rekey`). Fails unless linked + /// against sqlcipher (or something else that supports the Sqlite Encryption + /// Extension). + pub fn change_encryption_key(&mut self, new_encryption_key: &str) -> Result<()> { + ::change_encryption_key(&self.sqlite, new_encryption_key)?; + Ok(()) + } +} + impl Store { /// Intended for use from tests. pub fn sqlite_mut(&mut self) -> &mut rusqlite::Connection {