@@ -10,19 +10,48 @@ use std::path::Path;
10
10
use std:: sync:: Arc ;
11
11
use std:: { fmt, io, ptr} ;
12
12
13
- use aead:: consts:: U0 ;
14
13
use aead:: generic_array:: typenum:: Unsigned ;
15
- use aead:: generic_array:: GenericArray ;
16
- use aead:: { AeadCore , AeadMutInPlace , Key , KeyInit , KeySizeUser , Nonce , Tag } ;
14
+ use aead:: { AeadCore , AeadMutInPlace , Key , KeyInit , Nonce , Tag } ;
17
15
use synchronoise:: SignalEvent ;
18
16
19
17
#[ cfg( windows) ]
20
18
use crate :: env:: OsStrExtLmdb as _;
21
- use crate :: env:: { canonicalize_path, Env , EnvEntry , EnvFlags , EnvInner , OPENED_ENV } ;
19
+ use crate :: env:: { canonicalize_path, Env , EnvFlags , EnvInner , OPENED_ENV } ;
22
20
use crate :: mdb:: ffi;
23
21
use crate :: mdb:: lmdb_error:: mdb_result;
24
22
use crate :: { Error , Result } ;
25
23
24
+ pub struct EnvEntry {
25
+ pub ( super ) env : Option < Env > ,
26
+ pub ( super ) signal_event : Arc < SignalEvent > ,
27
+ pub ( super ) options : SimplifiedEncryptedOpenOptions ,
28
+ }
29
+
30
+ /// A simplified version of the options that were used to open a given [`Env`].
31
+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
32
+ pub struct SimplifiedEncryptedOpenOptions {
33
+ /// The maximum size this [`Env`] with take in bytes or [`None`] if it was not specified.
34
+ pub map_size : Option < usize > ,
35
+ /// The maximum number of concurrent readers or [`None`] if it was not specified.
36
+ pub max_readers : Option < u32 > ,
37
+ /// The maximum number of opened database or [`None`] if it was not specified.
38
+ pub max_dbs : Option < u32 > ,
39
+ /// The raw flags enabled for this [`Env`] or [`None`] if it was not specified.
40
+ pub flags : u32 ,
41
+ }
42
+
43
+ impl < E : AeadMutInPlace + KeyInit > From < & EnvOpenOptions < E > > for SimplifiedEncryptedOpenOptions {
44
+ fn from ( eoo : & EnvOpenOptions < E > ) -> SimplifiedEncryptedOpenOptions {
45
+ let EnvOpenOptions { encrypt : _, map_size, max_readers, max_dbs, flags } = eoo;
46
+ SimplifiedEncryptedOpenOptions {
47
+ map_size : * map_size,
48
+ max_readers : * max_readers,
49
+ max_dbs : * max_dbs,
50
+ flags : flags. bits ( ) ,
51
+ }
52
+ }
53
+ }
54
+
26
55
/// Options and flags which can be used to configure how an environment is opened.
27
56
#[ derive( Clone , PartialEq , Eq ) ]
28
57
#[ cfg_attr( feature = "serde" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
@@ -55,8 +84,8 @@ impl<E: AeadMutInPlace + KeyInit> EnvOpenOptions<E> {
55
84
/// use std::path::Path;
56
85
/// use argon2::Argon2;
57
86
/// use chacha20poly1305::{ChaCha20Poly1305, Key};
58
- /// use heed3 ::types::*;
59
- /// use heed3 ::{EnvOpenOptions, Database};
87
+ /// use heed3_encryption ::types::*;
88
+ /// use heed3_encryption ::{EnvOpenOptions, Database};
60
89
///
61
90
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
62
91
/// let env_path = Path::new("target").join("encrypt.mdb");
@@ -70,7 +99,7 @@ impl<E: AeadMutInPlace + KeyInit> EnvOpenOptions<E> {
70
99
/// Argon2::default().hash_password_into(password.as_bytes(), salt.as_bytes(), &mut key)?;
71
100
///
72
101
/// // We open the environment
73
- /// let mut options = EnvOpenOptions::new().encrypt_with:: <ChaCha20Poly1305>(key);
102
+ /// let mut options = EnvOpenOptions::<ChaCha20Poly1305>::new_encrypted_with (key);
74
103
/// let env = unsafe {
75
104
/// options
76
105
/// .map_size(10 * 1024 * 1024) // 10MB
@@ -94,13 +123,13 @@ impl<E: AeadMutInPlace + KeyInit> EnvOpenOptions<E> {
94
123
///
95
124
/// ## Example Showing limitations
96
125
///
97
- /// ```compile_fail
126
+ /// ```compile_fail,E0499
98
127
/// use std::fs;
99
128
/// use std::path::Path;
100
129
/// use argon2::Argon2;
101
130
/// use chacha20poly1305::{ChaCha20Poly1305, Key};
102
- /// use heed3 ::types::*;
103
- /// use heed3 ::{EnvOpenOptions, Database};
131
+ /// use heed3_encryption ::types::*;
132
+ /// use heed3_encryption ::{EnvOpenOptions, Database};
104
133
///
105
134
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
106
135
/// let env_path = Path::new("target").join("encrypt.mdb");
@@ -114,7 +143,7 @@ impl<E: AeadMutInPlace + KeyInit> EnvOpenOptions<E> {
114
143
/// Argon2::default().hash_password_into(password.as_bytes(), salt.as_bytes(), &mut key)?;
115
144
///
116
145
/// // We open the environment
117
- /// let mut options = EnvOpenOptions::new().encrypt_with:: <ChaCha20Poly1305>(key);
146
+ /// let mut options = EnvOpenOptions::<ChaCha20Poly1305>::new_encrypted_with (key);
118
147
/// let env = unsafe {
119
148
/// options
120
149
/// .map_size(10 * 1024 * 1024) // 10MB
@@ -125,6 +154,11 @@ impl<E: AeadMutInPlace + KeyInit> EnvOpenOptions<E> {
125
154
/// let key1 = "first-key";
126
155
/// let key2 = "second-key";
127
156
///
157
+ /// // We create the database
158
+ /// let mut wtxn = env.write_txn()?;
159
+ /// let db: Database<Str, Str> = env.create_database(&mut wtxn, Some("first"))?;
160
+ /// wtxn.commit()?;
161
+ ///
128
162
/// // Declare the read transaction as mutable because LMDB, when using encryption,
129
163
/// // does not allow keeping keys between reads due to the use of an internal cache.
130
164
/// let mut rtxn = env.read_txn()?;
@@ -263,7 +297,7 @@ impl<E: AeadMutInPlace + KeyInit> EnvOpenOptions<E> {
263
297
Ok ( path) => path,
264
298
} ;
265
299
266
- let original_options = SimplifiedOpenOptions :: from ( self ) ;
300
+ let original_options = SimplifiedEncryptedOpenOptions :: from ( self ) ;
267
301
match lock. entry ( path) {
268
302
Entry :: Occupied ( entry) => {
269
303
let env = entry. get ( ) . env . clone ( ) . ok_or ( Error :: DatabaseClosing ) ?;
@@ -282,7 +316,7 @@ impl<E: AeadMutInPlace + KeyInit> EnvOpenOptions<E> {
282
316
let mut env: * mut ffi:: MDB_env = ptr:: null_mut ( ) ;
283
317
mdb_result ( ffi:: mdb_env_create ( & mut env) ) ?;
284
318
285
- let encrypt_key = crate :: into_val ( self . encrypt ) ;
319
+ let encrypt_key = crate :: into_val ( & self . encrypt ) ;
286
320
mdb_result ( ffi:: mdb_env_set_encrypt (
287
321
env,
288
322
Some ( encrypt_func_wrapper :: < E > ) ,
0 commit comments