-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlib.rs
283 lines (242 loc) · 7 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
extern crate libc;
use std::io;
use std::mem;
use std::os::unix::ffi::OsStringExt;
use std::slice;
pub const MDBM_O_RDONLY: usize = mdbm_sys::MDBM_O_RDONLY as usize;
pub const MDBM_O_WRONLY: usize = mdbm_sys::MDBM_O_WRONLY as usize;
pub const MDBM_O_RDWR: usize = mdbm_sys::MDBM_O_RDWR as usize;
pub const MDBM_O_CREAT: usize = mdbm_sys::MDBM_O_CREAT as usize;
pub const MDBM_O_TRUNC: usize = mdbm_sys::MDBM_O_TRUNC as usize;
pub const MDBM_O_ASYNC: usize = mdbm_sys::MDBM_O_ASYNC as usize;
pub struct MDBM {
db: *mut mdbm_sys::MDBM,
}
impl MDBM {
/// Open a database.
pub fn new<P: Into<std::path::PathBuf>>(
path: P,
flags: usize,
mode: usize,
psize: usize,
presize: usize,
) -> Result<MDBM, io::Error> {
// Rust Path objects are not null-terminated.
// To null-terminate it, we need to:
// 1. Take ownership of it, so we can modify the underlying buf.
// - This may or may not copy, depending on what was passed in.
let path_buf = path.into();
// 2. Treat the string as a Unix string (i.e. assume Unix utf8 encoding)
// - This should be a no-op
let path_bytes = path_buf.into_os_string();
// 3. Treat it as a vector of bytes
// - This should be a no-op
let path_vec: Vec<u8> = path_bytes.into_vec();
// 4. Append a null byte
let path_cstring = std::ffi::CString::new(path_vec)?;
unsafe {
let db = mdbm_sys::mdbm_open(
path_cstring.into_raw(),
flags as libc::c_int,
mode as libc::c_int,
psize as libc::c_int,
presize as libc::c_int,
);
if db.is_null() {
Err(io::Error::last_os_error())
} else {
Ok(MDBM { db: db })
}
}
}
/// Set a key.
pub fn set<'k, 'v, K, V>(&self, key: &'k K, value: &'v V, flags: isize) -> Result<(), io::Error>
where
K: AsDatum<'k> + ?Sized,
V: AsDatum<'v> + ?Sized,
{
unsafe {
let rc = mdbm_sys::mdbm_store(
self.db,
to_raw_datum(&key.as_datum()),
to_raw_datum(&value.as_datum()),
flags as libc::c_int,
);
if rc == -1 {
Err(io::Error::last_os_error())
} else {
Ok(())
}
}
}
/// Lock a key.
pub fn lock<'a, K>(&'a self, key: &'a K, flags: isize) -> Result<Lock<'a>, io::Error>
where
K: AsDatum<'a> + ?Sized,
{
let rc = unsafe {
mdbm_sys::mdbm_lock_smart(
self.db,
&to_raw_datum(&key.as_datum()),
flags as libc::c_int,
)
};
if rc == 1 {
Ok(Lock {
db: self,
key: key.as_datum(),
})
} else {
Err(io::Error::last_os_error())
}
}
}
impl Drop for MDBM {
fn drop(&mut self) {
unsafe {
mdbm_sys::mdbm_sync(self.db);
mdbm_sys::mdbm_close(self.db);
}
}
}
pub struct Datum<'a> {
bytes: &'a [u8],
}
impl<'a> Datum<'a> {
pub fn new(bytes: &'a [u8]) -> Datum<'a> {
Datum { bytes: bytes }
}
}
pub trait AsDatum<'a> {
fn as_datum(&'a self) -> Datum<'a>;
}
impl<'a, T: AsDatum<'a> + ?Sized> AsDatum<'a> for &'a T {
fn as_datum(&'a self) -> Datum<'a> {
(**self).as_datum()
}
}
impl<'a> AsDatum<'a> for [u8] {
fn as_datum(&'a self) -> Datum<'a> {
Datum::new(self)
}
}
impl<'a> AsDatum<'a> for str {
fn as_datum(&'a self) -> Datum<'a> {
self.as_bytes().as_datum()
}
}
fn to_raw_datum(datum: &Datum) -> mdbm_sys::datum {
mdbm_sys::datum {
dptr: datum.bytes.as_ptr() as *mut _,
dsize: datum.bytes.len() as libc::c_int,
}
}
pub struct Lock<'a> {
db: &'a MDBM,
key: Datum<'a>,
}
impl<'a> Lock<'a> {
/// Fetch a key.
pub fn get(&'a self) -> Option<&'a [u8]> {
unsafe {
let value = mdbm_sys::mdbm_fetch(self.db.db, to_raw_datum(&self.key));
if value.dptr.is_null() {
None
} else {
// Cast pointer from signed char (c) to unsigned char (rust)
let u8_ptr: *const u8 = mem::transmute::<*mut i8, *const u8>(value.dptr);
Some(slice::from_raw_parts(u8_ptr, value.dsize as usize))
}
}
}
}
impl<'a> Drop for Lock<'a> {
fn drop(&mut self) {
unsafe {
let rc = mdbm_sys::mdbm_unlock_smart(self.db.db, &to_raw_datum(&self.key), 0);
assert_eq!(rc, 1);
}
}
}
#[cfg(test)]
mod tests {
use super::MDBM;
use std::fs::remove_file;
use std::path::Path;
use std::str;
#[test]
fn test_set_get() {
let path = Path::new("test.db");
let db = MDBM::new(&path, super::MDBM_O_RDWR | super::MDBM_O_CREAT, 0o644, 0, 0).unwrap();
db.set(&"hello", &"world", 0).unwrap();
{
// key needs to be an lvalue so the lock can hold a reference to
// it.
let key = "hello";
// Lock the key. RIAA will unlock it when we exit this scope.
let value = db.lock(&key, 0).unwrap();
// Convert the value into a string. The lock is still live at this
// point.
let value = str::from_utf8(value.get().unwrap()).unwrap();
assert_eq!(value, "world");
println!("hello: {}", value);
}
let _ = remove_file(path);
}
// Tests that should fail to compile
/*
#[test]
fn test_keys_cannot_escape() {
let db = MDBM::new(
&Path::new("test.db"),
super::MDBM_O_RDWR | super::MDBM_O_CREAT,
0o644,
0,
0
).unwrap();
db.set(&"hello", &"world", 0).unwrap();
let _ = {
let key = vec![1];
db.lock(&key.as_slice(), 0).unwrap()
};
}
*/
/*
#[test]
fn test_values_cannot_escape() {
let db = MDBM::new(
&Path::new("test.db"),
super::MDBM_O_RDWR | super::MDBM_O_CREAT,
0o644,
0,
0,
)
.unwrap();
let _ = {
db.set(&"hello", &"world", 0).unwrap();
let key = "hello";
let value = db.lock(&key, 0).unwrap();
str::from_utf8(value.get().unwrap()).unwrap()
};
}
*/
/*
#[test]
fn test_values_cannot_escape_database() {
let _ = {
let db = MDBM::new(
&Path::new("test.db"),
super::MDBM_O_RDWR | super::MDBM_O_CREAT,
0o644,
0,
0,
)
.unwrap();
db.set(&"hello", &"world", 0).unwrap();
let key = "hello";
let value = db.lock(&key, 0).unwrap();
str::from_utf8(value.get().unwrap()).unwrap()
};
}
*/
}