From 12fa86694ce1f652826c2e73c3a2aa52c3952f58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Sun, 17 Nov 2024 19:50:14 +0100 Subject: [PATCH] Fix all errors and most warnings --- heed/src/databases/database.rs | 17 ----- heed/src/databases/encrypted_database.rs | 32 ++++----- heed/src/envs/encrypted_env.rs | 83 +++++++----------------- heed/src/envs/env.rs | 5 +- heed/src/envs/env_open_options.rs | 7 +- heed/src/envs/mod.rs | 2 +- heed/src/lib.rs | 4 ++ 7 files changed, 50 insertions(+), 100 deletions(-) diff --git a/heed/src/databases/database.rs b/heed/src/databases/database.rs index 69f4328a..2f57bef6 100644 --- a/heed/src/databases/database.rs +++ b/heed/src/databases/database.rs @@ -341,7 +341,6 @@ impl Database { /// wtxn.commit()?; /// # Ok(()) } /// ``` - #[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))] pub fn get<'a, 'txn>(&self, txn: &'txn RoTxn, key: &'a KC::EItem) -> Result> where KC: BytesEncode<'a>, @@ -426,7 +425,6 @@ impl Database { /// wtxn.commit()?; /// # Ok(()) } /// ``` - #[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))] pub fn get_duplicates<'a, 'txn>( &self, txn: &'txn RoTxn, @@ -490,7 +488,6 @@ impl Database { /// wtxn.commit()?; /// # Ok(()) } /// ``` - #[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))] pub fn get_lower_than<'a, 'txn>( &self, txn: &'txn RoTxn, @@ -560,7 +557,6 @@ impl Database { /// wtxn.commit()?; /// # Ok(()) } /// ``` - #[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))] pub fn get_lower_than_or_equal_to<'a, 'txn>( &self, txn: &'txn RoTxn, @@ -634,7 +630,6 @@ impl Database { /// wtxn.commit()?; /// # Ok(()) } /// ``` - #[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))] pub fn get_greater_than<'a, 'txn>( &self, txn: &'txn RoTxn, @@ -707,7 +702,6 @@ impl Database { /// wtxn.commit()?; /// # Ok(()) } /// ``` - #[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))] pub fn get_greater_than_or_equal_to<'a, 'txn>( &self, txn: &'txn RoTxn, @@ -767,7 +761,6 @@ impl Database { /// wtxn.commit()?; /// # Ok(()) } /// ``` - #[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))] pub fn first<'txn>(&self, txn: &'txn RoTxn) -> Result> where KC: BytesDecode<'txn>, @@ -822,7 +815,6 @@ impl Database { /// wtxn.commit()?; /// # Ok(()) } /// ``` - #[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))] pub fn last<'txn>(&self, txn: &'txn RoTxn) -> Result> where KC: BytesDecode<'txn>, @@ -880,7 +872,6 @@ impl Database { /// wtxn.commit()?; /// # Ok(()) } /// ``` - #[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))] pub fn len(&self, txn: &RoTxn) -> Result { self.stat(txn).map(|stat| stat.entries as u64) } @@ -924,7 +915,6 @@ impl Database { /// wtxn.commit()?; /// # Ok(()) } /// ``` - #[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))] pub fn is_empty(&self, txn: &RoTxn) -> Result { self.len(txn).map(|l| l == 0) } @@ -967,7 +957,6 @@ impl Database { /// wtxn.commit()?; /// # Ok(()) } /// ``` - #[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))] pub fn stat(&self, txn: &RoTxn) -> Result { assert_eq_env_db_txn!(self, txn); @@ -1032,7 +1021,6 @@ impl Database { /// wtxn.commit()?; /// # Ok(()) } /// ``` - #[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))] pub fn iter<'txn>(&self, txn: &'txn RoTxn) -> Result> { assert_eq_env_db_txn!(self, txn); RoCursor::new(txn, self.dbi).map(|cursor| RoIter::new(cursor)) @@ -1134,7 +1122,6 @@ impl Database { /// wtxn.commit()?; /// # Ok(()) } /// ``` - #[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))] pub fn rev_iter<'txn>(&self, txn: &'txn RoTxn) -> Result> { assert_eq_env_db_txn!(self, txn); @@ -1241,7 +1228,6 @@ impl Database { /// wtxn.commit()?; /// # Ok(()) } /// ``` - #[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))] pub fn range<'a, 'txn, R>( &self, txn: &'txn RoTxn, @@ -1416,7 +1402,6 @@ impl Database { /// wtxn.commit()?; /// # Ok(()) } /// ``` - #[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))] pub fn rev_range<'a, 'txn, R>( &self, txn: &'txn RoTxn, @@ -1592,7 +1577,6 @@ impl Database { /// wtxn.commit()?; /// # Ok(()) } /// ``` - #[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))] pub fn prefix_iter<'a, 'txn>( &self, txn: &'txn RoTxn, @@ -1725,7 +1709,6 @@ impl Database { /// wtxn.commit()?; /// # Ok(()) } /// ``` - #[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))] pub fn rev_prefix_iter<'a, 'txn>( &self, txn: &'txn RoTxn, diff --git a/heed/src/databases/encrypted_database.rs b/heed/src/databases/encrypted_database.rs index 60b75b20..d55a61da 100644 --- a/heed/src/databases/encrypted_database.rs +++ b/heed/src/databases/encrypted_database.rs @@ -1,16 +1,12 @@ -use std::borrow::Cow; -use std::ops::{Bound, RangeBounds}; -use std::{any, fmt, marker, mem, ptr}; +use std::ops::RangeBounds; +use std::{any, fmt}; use heed_traits::{Comparator, LexicographicComparator}; -use types::{DecodeIgnore, LazyDecode}; +use types::LazyDecode; -use crate::cursor::MoveOperation; -use crate::env::DefaultComparator; +use crate::envs::DefaultComparator; use crate::iteration_method::MoveOnCurrentKeyDuplicates; -use crate::mdb::error::mdb_result; -use crate::mdb::ffi; -use crate::mdb::lmdb_flags::{AllDatabaseFlags, DatabaseFlags}; +use crate::mdb::lmdb_flags::DatabaseFlags; use crate::*; /// Options and flags which can be used to configure how a [`Database`] is opened. @@ -61,8 +57,8 @@ pub struct EncryptedDatabaseOpenOptions<'e, 'n, KC, DC, C = DefaultComparator> { impl<'e> EncryptedDatabaseOpenOptions<'e, 'static, Unspecified, Unspecified> { /// Create an options struct to open/create a database with specific flags. - pub fn new(env: &'e Env) -> Self { - EncryptedDatabaseOpenOptions { inner: DatabaseOpenOptions::new(env) } + pub fn new(env: &'e EncryptedEnv) -> Self { + EncryptedDatabaseOpenOptions { inner: DatabaseOpenOptions::new(&env.inner) } } } @@ -119,7 +115,7 @@ impl<'e, 'n, KC, DC, C> EncryptedDatabaseOpenOptions<'e, 'n, KC, DC, C> { DC: 'static, C: Comparator + 'static, { - self.inner.open(rtxn) + self.inner.open(rtxn).map(|opt| opt.map(EncryptedDatabase::new)) } /// Creates a typed database that can already exist in this environment. @@ -137,7 +133,7 @@ impl<'e, 'n, KC, DC, C> EncryptedDatabaseOpenOptions<'e, 'n, KC, DC, C> { DC: 'static, C: Comparator + 'static, { - self.inner.create(wtxn) + self.inner.create(wtxn).map(EncryptedDatabase::new) } } @@ -266,8 +262,8 @@ pub struct EncryptedDatabase { } impl EncryptedDatabase { - pub(crate) fn new(env_ident: usize, dbi: ffi::MDB_dbi) -> Database { - EncryptedDatabase { inner: Database::mew(env_ident, dbi) } + pub(crate) fn new(inner: Database) -> EncryptedDatabase { + EncryptedDatabase { inner } } /// Retrieves the value associated with a key. @@ -2074,7 +2070,7 @@ impl EncryptedDatabase { KC: BytesEncode<'a> + BytesDecode<'txn>, R: RangeBounds, { - self.inner.delete_range(range) + self.inner.delete_range(txn, range) } /// Deletes all key/value pairs in this database. @@ -2119,7 +2115,7 @@ impl EncryptedDatabase { /// # Ok(()) } /// ``` pub fn clear(&self, txn: &mut RwTxn) -> Result<()> { - self.inner.clear() + self.inner.clear(txn) } /// Change the codec types of this database, specifying the codecs. @@ -2164,7 +2160,7 @@ impl EncryptedDatabase { /// # Ok(()) } /// ``` pub fn remap_types(&self) -> EncryptedDatabase { - EncryptedDatabase::new(self.inner.env_ident, self.inner.dbi) + EncryptedDatabase::new(self.inner.remap_types::()) } /// Change the key codec type of this database, specifying the new codec. diff --git a/heed/src/envs/encrypted_env.rs b/heed/src/envs/encrypted_env.rs index 2c38e651..b37d5b2c 100644 --- a/heed/src/envs/encrypted_env.rs +++ b/heed/src/envs/encrypted_env.rs @@ -1,40 +1,22 @@ -use std::any::TypeId; -use std::ffi::CString; +use std::fmt; use std::fs::File; -use std::path::{Path, PathBuf}; -use std::ptr::{self, NonNull}; -use std::{fmt, io, mem}; - -use heed_traits::Comparator; -use lmdb_master_sys::mdb_env_close; - -use super::{ - custom_key_cmp_wrapper, get_file_fd, metadata_from_fd, DefaultComparator, EnvInfo, FlagSetMode, - OPENED_ENV, -}; -use crate::cursor::{MoveOperation, RoCursor}; -use crate::mdb::ffi::{self, MDB_env}; -use crate::mdb::lmdb_error::mdb_result; -use crate::mdb::lmdb_flags::AllDatabaseFlags; -use crate::{ - CompactionOption, Database, DatabaseOpenOptions, EnvFlags, Error, Result, RoTxn, RwTxn, - Unspecified, -}; +use std::panic::catch_unwind; +use std::path::Path; + +use aead::generic_array::typenum::Unsigned; +use aead::{AeadMutInPlace, Key, KeyInit, Nonce, Tag}; + +use super::{Env, EnvInfo, FlagSetMode}; +use crate::databases::{EncryptedDatabase, EncryptedDatabaseOpenOptions}; +use crate::mdb::ffi::{self}; +use crate::{CompactionOption, EnvFlags, Result, RoTxn, RwTxn, Unspecified}; /// An environment handle constructed by using [`EnvOpenOptions::open_encrypted`]. pub struct EncryptedEnv { - inner: Env, + pub(crate) inner: Env, } -impl Env { - pub(crate) fn new(env_ptr: NonNull, path: PathBuf) -> Env { - Env { env_ptr, path } - } - - pub(crate) fn env_mut_ptr(&self) -> NonNull { - self.inner.env_mut_ptr() - } - +impl EncryptedEnv { /// The size of the data file on disk. /// /// # Example @@ -182,24 +164,6 @@ impl Env { options.create(wtxn) } - pub(crate) fn raw_init_database( - &self, - raw_txn: *mut ffi::MDB_txn, - name: Option<&str>, - flags: AllDatabaseFlags, - ) -> Result { - self.inner.raw_init_database(raw_txn, name, flags) - } - - fn raw_open_dbi( - &self, - raw_txn: *mut ffi::MDB_txn, - name: Option<&str>, - flags: u32, - ) -> std::result::Result { - self.inner.raw_open_dbi(raw_txn, name, flags) - } - /// Create a transaction with read and write access for use with the environment. /// /// ## LMDB Limitations @@ -346,7 +310,9 @@ unsafe impl Sync for EncryptedEnv {} impl fmt::Debug for EncryptedEnv { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("EncryptedEnv").field("path", &self.path.display()).finish_non_exhaustive() + f.debug_struct("EncryptedEnv") + .field("path", &self.inner.path.display()) + .finish_non_exhaustive() } } @@ -359,7 +325,7 @@ fn encrypt( auth_out: &mut [u8], ) -> aead::Result<()> { chipertext_out.copy_from_slice(plaintext); - let key: &Key = key.try_into().unwrap(); + let key: &Key = key.into(); let nonce: &Nonce = if nonce.len() >= A::NonceSize::USIZE { nonce[..A::NonceSize::USIZE].into() } else { @@ -380,20 +346,20 @@ fn decrypt( auth_in: &[u8], ) -> aead::Result<()> { output.copy_from_slice(chipher_text); - let key: &Key = key.try_into().unwrap(); + let key: &Key = key.into(); let nonce: &Nonce = if nonce.len() >= A::NonceSize::USIZE { nonce[..A::NonceSize::USIZE].into() } else { return Err(aead::Error); }; - let tag: &Tag = auth_in.try_into().unwrap(); + let tag: &Tag = auth_in.into(); let mut aead = A::new(key); aead.decrypt_in_place_detached(nonce, aad, output, tag) } /// The wrapper function that is called by LMDB that directly calls /// the Rust idiomatic function internally. -unsafe extern "C" fn encrypt_func_wrapper( +pub(crate) unsafe extern "C" fn encrypt_func_wrapper( src: *const ffi::MDB_val, dst: *mut ffi::MDB_val, key_ptr: *const ffi::MDB_val, @@ -415,16 +381,13 @@ unsafe extern "C" fn encrypt_func_wrapper( let aad = []; let nonce = iv; let result = if encdec == 1 { - encrypt::(&key, nonce, &aad, input, output, auth) + encrypt::(key, nonce, &aad, input, output, auth) } else { - decrypt::(&key, nonce, &aad, input, output, auth) + decrypt::(key, nonce, &aad, input, output, auth) }; result.is_err() as i32 }); - match result { - Ok(out) => out, - Err(_) => 1, - } + result.unwrap_or(1) } diff --git a/heed/src/envs/env.rs b/heed/src/envs/env.rs index 2f3080c9..5100041e 100644 --- a/heed/src/envs/env.rs +++ b/heed/src/envs/env.rs @@ -6,7 +6,6 @@ use std::ptr::{self, NonNull}; use std::{fmt, io, mem}; use heed_traits::Comparator; -use lmdb_master_sys::mdb_env_close; use super::{ custom_key_cmp_wrapper, get_file_fd, metadata_from_fd, DefaultComparator, EnvInfo, FlagSetMode, @@ -24,7 +23,7 @@ use crate::{ /// An environment handle constructed by using [`EnvOpenOptions::open`]. pub struct Env { env_ptr: NonNull, - path: PathBuf, + pub(crate) path: PathBuf, } impl Env { @@ -466,7 +465,7 @@ impl fmt::Debug for Env { impl Drop for Env { fn drop(&mut self) { - unsafe { mdb_env_close(self.env_ptr.as_mut()) }; + unsafe { ffi::mdb_env_close(self.env_ptr.as_mut()) }; let mut lock = OPENED_ENV.write().unwrap(); debug_assert!(lock.remove(&self.path)); } diff --git a/heed/src/envs/env_open_options.rs b/heed/src/envs/env_open_options.rs index 0515e406..b31cf217 100644 --- a/heed/src/envs/env_open_options.rs +++ b/heed/src/envs/env_open_options.rs @@ -8,6 +8,11 @@ use std::path::Path; use std::ptr::NonNull; use std::{io, ptr}; +#[cfg(master3)] +use aead::{generic_array::typenum::Unsigned, AeadCore, AeadMutInPlace, Key, KeyInit}; + +#[cfg(master3)] +use super::encrypted_env::encrypt_func_wrapper; use super::env::Env; use super::{canonicalize_path, OPENED_ENV}; #[cfg(windows)] @@ -357,7 +362,7 @@ impl EnvOpenOptions { let mut env: *mut ffi::MDB_env = ptr::null_mut(); mdb_result(ffi::mdb_env_create(&mut env))?; - let encrypt_key = crate::into_val(&self.encrypt); + let encrypt_key = crate::into_val(&key); mdb_result(ffi::mdb_env_set_encrypt( env, Some(encrypt_func_wrapper::), diff --git a/heed/src/envs/mod.rs b/heed/src/envs/mod.rs index 996b47e4..c2d2f251 100644 --- a/heed/src/envs/mod.rs +++ b/heed/src/envs/mod.rs @@ -25,7 +25,7 @@ mod env; mod env_open_options; #[cfg(master3)] -pub use env::EncryptedEnv; +pub use encrypted_env::EncryptedEnv; pub use env::Env; pub use env_open_options::EnvOpenOptions; diff --git a/heed/src/lib.rs b/heed/src/lib.rs index fa2c06ac..a7c94dfd 100644 --- a/heed/src/lib.rs +++ b/heed/src/lib.rs @@ -80,6 +80,10 @@ pub use {byteorder, heed_types as types}; use self::cursor::{RoCursor, RwCursor}; pub use self::databases::{Database, DatabaseOpenOptions, DatabaseStat}; +#[cfg(master3)] +pub use self::databases::{EncryptedDatabase, EncryptedDatabaseOpenOptions}; +#[cfg(master3)] +pub use self::envs::EncryptedEnv; pub use self::envs::{ CompactionOption, DefaultComparator, Env, EnvInfo, EnvOpenOptions, FlagSetMode, };