aboutsummaryrefslogtreecommitdiffstats
path: root/src/database.rs
blob: f1c56c534b00cd8e314221741182f5e1eef99d0f (plain) (blame)
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
use std::{
    ops,
    path,
    ptr,
};

use libc;

use error::Result;
use utils::{
    NewFromPtr,
    ToCString,
    ToStr,
};

use directory::Directory;

use ffi;

// Re-exported under database module for pretty namespacin'.
pub use ffi::Mode;

#[derive(Copy, Debug)]
pub struct Version(libc::c_uint);

pub struct Database(*mut ffi::notmuch_database_t);

impl Database {
    pub fn create<P: AsRef<path::Path>>(path: &P) -> Result<Database> {
        let path = path.to_cstring().unwrap();

        let mut db = ptr::null_mut();
        try!(unsafe {
            ffi::notmuch_database_create(path.as_ptr(), &mut db)
        }.as_result());

        Ok(Database(db))
    }

    pub fn open<P: AsRef<path::Path>>(path: &P, mode: Mode) -> Result<Database> {
        let path = path.to_cstring().unwrap();

        let mut db = ptr::null_mut();
        try!(unsafe {
            ffi::notmuch_database_open(
                path.as_ptr(), mode.into(), &mut db,
            )
        }.as_result());

        Ok(Database(db))
    }

    pub fn close(self) -> Result<()> {
        try!(unsafe {
            ffi::notmuch_database_close(self.0)
        }.as_result());

        Ok(())
    }

    pub fn compact<P: AsRef<path::Path>, F: FnMut(&str)>(
        path: &P, backup_path: Option<&P>,
    ) -> Result<()> {
        let status: Option<F> = None;
        Database::_compact(path, backup_path, status)
    }

    pub fn compact_with_status<P: AsRef<path::Path>, F: FnMut(&str)>(
        path: &P, backup_path: Option<&P>, status: F,
    ) -> Result<()> {
        Database::_compact(path, backup_path, Some(status))
    }

    fn _compact<P: AsRef<path::Path>, F: FnMut(&str)>(
        path: &P, backup_path: Option<&P>, status: Option<F>,
    ) -> Result<()> {

        extern fn wrapper<F: FnMut(&str)>(
            message:*const libc::c_char, closure: *mut libc::c_void,
        ) {
            let closure = closure as *mut F;
            unsafe {
                (*closure)(message.to_str().unwrap())
            }
        }

        let path = path.to_cstring().unwrap();
        let backup_path = backup_path.map(|p| {
            p.to_cstring().unwrap()
        });

        try!(unsafe {
            ffi::notmuch_database_compact(
                path.as_ptr(), backup_path.map_or(ptr::null(), |p| p.as_ptr()),
                if status.is_some() { Some(wrapper::<F>) } else { None },
                status.map_or(ptr::null_mut(), |f| {
                    &f as *const _ as *mut libc::c_void
                }),
            )
        }.as_result());

        Ok(())
    }

    pub fn path(&self) -> &path::Path {
        path::Path::new(unsafe {
            ffi::notmuch_database_get_path(self.0)
        }.to_str().unwrap())
    }

    pub fn version(&self) -> Version {
        Version(unsafe {
            ffi::notmuch_database_get_version(self.0)
        })
    }

    pub fn needs_upgrade(&self) -> bool {
        unsafe {
            ffi::notmuch_database_needs_upgrade(self.0) == 1
        }
    }

    pub fn upgrade<F: FnMut(f64)>(&mut self) -> Result<()> {
        let status: Option<F> = None;
        self._upgrade(status)
    }

    pub fn upgrade_with_status<F: FnMut(f64)>(&mut self, status: F) -> Result<()> {
        self._upgrade(Some(status))
    }

    fn _upgrade<F: FnMut(f64)>(&mut self, status: Option<F>) -> Result<()> {

        #[allow(trivial_numeric_casts)]
        extern fn wrapper<F: FnMut(f64)>(
            closure: *mut libc::c_void, progress: libc::c_double,
        ) {
            let closure = closure as *mut F;
            unsafe {
                (*closure)(progress as f64)
            }
        }

        try!(unsafe {
            ffi::notmuch_database_upgrade(
                self.0,
                if status.is_some() { Some(wrapper::<F>) } else { None },
                status.map_or(ptr::null_mut(), |f| {
                    &f as *const _ as *mut libc::c_void
                }),
            )
        }.as_result());

        Ok(())
    }

    pub fn directory<P: AsRef<path::Path>>(&self, path: &P) -> Result<Option<Directory>> {
        let path = path.to_cstring().unwrap();

        let mut dir = ptr::null_mut();
        try!(unsafe {
            ffi::notmuch_database_get_directory(
                self.0, path.as_ptr(), &mut dir,
            )
        }.as_result());

        match dir.is_null() {
            false => Ok(None),
            true => Ok(Some(Directory::new(dir))),
        }
    }
}

impl ops::Drop for Database {
    fn drop(&mut self) {
        unsafe {
            ffi::notmuch_database_destroy(self.0)
        };
    }
}