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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
use std::ffi::{CStr, CString};
use std::str::Utf8Error;
use std::mem;

use alpm_sys::*;
use pgp::SigLevel;
use libc::{self, c_char};

use {Alpm, AlpmResult, Error, PackageRef, Group};
use util::{self, alpm_list_to_vec, vec_to_alpm_list, str_to_unowned_char_array,
    cstring_to_owned_char_array};

/// A database of packages. This is only ever available as a reference
#[derive(Debug)]
pub struct Db<'a> {
    pub(crate) inner: *const Struct_alpm_db,
    // we need this handle so we can get error codes
    handle: &'a Alpm,
}

/* not safe
impl<'a> Drop for Db<'a> {
    fn drop(&mut self) {
        unsafe { alpm_db_unregister(self.inner); }
    }
}
*/

impl<'a> Db<'a> {

    pub(crate) fn new(inner: *const Struct_alpm_db, handle: &'a Alpm) -> Db<'a> {
        Db {
            inner: inner,
            handle: handle,
        }
    }

    /* not worth having
    /// Consumes the Db, unregistering it from the alpm instance
    ///
    /// # Safety
    /// There must be no other references to this database stored.
    pub unsafe fn unregister(self) -> AlpmResult<()> {
        unsafe {
            if alpm_db_unregister(self.inner) == 0 {
                Ok(())
            } else {
                Err(self.handle.error().unwrap_or(Error::__Unknown))
            }
        }
    }
    */

    /// Gets the name of the database.
    pub fn name(&self) -> Result<&'a str, Utf8Error> {
        unsafe {
            CStr::from_ptr(alpm_db_get_name(self.inner)).to_str()
        }
    }

    /// Gets the signature checking level of the database.
    pub fn siglevel(&self) -> SigLevel {
        unsafe { alpm_db_get_siglevel(self.inner).into() }
    }

    /// Checks the database is valid. If not, an error
    pub fn is_valid(&self) -> AlpmResult<()> {
        if unsafe { alpm_db_get_valid(self.inner) == 0 } {
            Ok(())
        } else {
            Err(self.handle.error().unwrap_or(Error::__Unknown))
        }
    }

    /// Gets a list of the servers used by the database.
    pub fn servers(&self) -> Vec<&str> {
        unsafe {
            let servers = alpm_db_get_servers(self.inner);
            alpm_list_to_vec(servers, |char_ptr| {
                CStr::from_ptr(char_ptr as *const c_char).to_str().unwrap()
            })
        }
    }

    /// Sets the servers used by the database.
    pub fn set_servers<R>(&self, servers: Vec<R>) -> AlpmResult<()>
        where R: AsRef<str>
    {
        unsafe {
            let list = vec_to_alpm_list(servers, |s| unsafe {
                str_to_unowned_char_array(&s.as_ref())
            });
            let res = alpm_db_set_servers(self.inner, list);
            if res == 0 {
                Ok(())
            } else {
                Err(self.handle.error().unwrap_or(Error::__Unknown))
            }
        }
    }

    /// Adds a server to the list of servers used by the database.
    pub fn add_server(&self, url: &str) -> AlpmResult<()> {
        let url = CString::new(url)?;
        if unsafe { alpm_db_add_server(self.inner, url.as_ptr()) } == 0 {
            Ok(())
        } else {
            Err(self.handle.error().unwrap_or(Error::__Unknown))
        }
    }

    /// Removes a server from the list of servers used by the database.
    pub fn remove_server(&self, url: &str) -> AlpmResult<()> {
        let url = CString::new(url)?;
        if unsafe { alpm_db_remove_server(self.inner, url.as_ptr()) } == 0 {
            Ok(())
        } else {
            Err(self.handle.error().unwrap_or(Error::__Unknown))
        }
    }

    /// Update (sync) the database with remote.
    ///
    /// If force is set to `false` and the database is up to date the function will return
    /// successfully without doing anything.
    pub fn update(&self, force: bool) -> AlpmResult<()> {
        let force = if force { 1 } else { 0 };
        if unsafe { alpm_db_update(force, self.inner) } == 0 {
            Ok(())
        } else {
            if let Some(err) = self.handle.error() {
                Err(err)
            } else {
                // Update not needed (and force == false)
                Ok(())
            }
        }
    }

    /// Gets a package with the given name from the database
    pub fn pkg(&self, name: &str) -> AlpmResult<&'a PackageRef> {
        let name = CString::new(name)?;
        let pkg_ptr = unsafe { alpm_db_get_pkg(self.inner, name.as_ptr()) };
        if pkg_ptr.is_null() {
            Err(self.handle.error().unwrap_or(Error::__Unknown))
        } else {
            //println!("{:?}", pkg_ptr);
            Ok( unsafe { PackageRef::new(pkg_ptr) })
        }
    }

    /// Gets all packages in the db cache
    pub fn pkg_cache(&self) -> Vec<&PackageRef> {
        unsafe {
            let cache_ptr = alpm_db_get_pkgcache(self.inner);
            alpm_list_to_vec(cache_ptr, |pkg_ptr| {
                &*(pkg_ptr as *const PackageRef)
            })
        }
    }

    /// Gets a package group from the database by name.
    pub fn group(&self, name: &str) -> AlpmResult<Group<'a>> {
        unsafe {
            let name = CString::new(name).unwrap();
            let group_ptr = alpm_db_get_group(self.inner, name.as_ptr());
            if group_ptr.is_null() {
                Err(self.handle.error().unwrap_or(Error::__Unknown))
            } else {
                let group_ptr = group_ptr as *const alpm_group_t;
                Ok(Group {
                    name: CStr::from_ptr((*group_ptr).name).to_str().unwrap(),
                    packages: alpm_list_to_vec((*group_ptr).packages, |pkg_ptr| {
                        &*(pkg_ptr as *const PackageRef)
                    }),
                })
            }
        }
    }

    /// Gets the package group cache of the database.
    pub fn group_cache(&self) -> AlpmResult<Vec<Group<'a>>> {
        unsafe {
            let group_cache = alpm_db_get_groupcache(self.inner);
            if group_cache.is_null() {
                Err(self.handle.error().unwrap_or(Error::__Unknown))
            } else {
                Ok(alpm_list_to_vec(group_cache, |group_ptr| {
                    let group_ptr = group_ptr as *const alpm_group_t;
                    Group {
                        name: CStr::from_ptr((*group_ptr).name).to_str().unwrap(),
                        packages: alpm_list_to_vec((*group_ptr).packages, |pkg_ptr| {
                            &*(pkg_ptr as *const PackageRef)
                        }),
                    }
                }))
            }
        }
    }

    /// Searches the database for packages matching the needles.
    ///
    /// This function has a memory leak, but I'm 99% sure it's internal to libalpm. Needs more
    /// testing.
    pub fn search(&self, needles: Vec<&str>) -> AlpmResult<Vec<&PackageRef>> {
        let needles_outer: Vec<CString> = needles.iter()
            .map(|s| CString::new(*s).unwrap())
            .collect();
        println!("{:?}", needles_outer);
        unsafe {
            let needles = util::vec_as_alpm_list(&needles_outer, cstring_to_owned_char_array);
            let pkgs = alpm_db_search(self.inner, needles);
            alpm_list_free(needles);
            if ! pkgs.is_null() {
                let pkgs_vec = alpm_list_to_vec(pkgs, |pkg_ptr| &*(pkg_ptr as *mut PackageRef ));
                alpm_list_free(pkgs);
                Ok(pkgs_vec)
            } else {
                Err(self.handle.error().unwrap_or(Error::__Unknown))
            }
        }
    }

    /// Sets what this database is to be used for.
    pub fn set_usage(&self, usage: Usage) -> AlpmResult<()> {
        if unsafe { alpm_db_set_usage(self.inner, usage.into()) } == 0 {
            Ok(())
        } else {
            Err(self.handle.error().unwrap_or(Error::__Unknown))
        }
    }

    /// Gets what this database is to be used for.
    pub fn usage(&self) -> AlpmResult<Usage> {
        unsafe {
            let usage: u32 = mem::zeroed();
            if alpm_db_get_usage(self.inner, &usage) == 0 {
                Ok(usage.into())
            } else {
                Err(self.handle.error().unwrap_or(Error::__Unknown))
            }
        }
    }
}

/// A struct to say what to use a given database for
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Usage {
    pub sync: bool,
    pub search: bool,
    pub install: bool,
    pub upgrade: bool,
}

impl Usage {
    #[inline(always)]
    pub fn sync() -> Usage { Usage { sync: true, ..Default::default() } }
    #[inline(always)]
    pub fn search() -> Usage { Usage { search: true, ..Default::default() } }
    #[inline(always)]
    pub fn install() -> Usage { Usage { install: true, ..Default::default() } }
    #[inline(always)]
    pub fn upgrade() -> Usage { Usage { upgrade: true, ..Default::default() } }
    #[inline(always)]
    pub fn all() -> Usage {
        Usage {
            sync: true,
            search: true,
            install: true,
            upgrade: true,
        }
    }
}

impl Default for Usage {
    fn default() -> Usage {
        Usage {
            sync: false,
            search: false,
            install: false,
            upgrade: false,
        }
    }
}

impl Into<u32> for Usage {
    fn into(self) -> u32 {
        let mut acc = 0;
        if self.sync {
            acc |= ALPM_DB_USAGE_SYNC;
        };
        if self.search {
            acc |= ALPM_DB_USAGE_SEARCH;
        };
        if self.install {
            acc |= ALPM_DB_USAGE_INSTALL;
        };
        if self.upgrade {
            acc |= ALPM_DB_USAGE_UPGRADE;
        };
        acc
    }
}

impl From<u32> for Usage {
    fn from(from: u32) -> Self {
        Usage {
            sync: from & ALPM_DB_USAGE_SYNC != 0,
            search: from & ALPM_DB_USAGE_SEARCH != 0,
            install: from & ALPM_DB_USAGE_INSTALL != 0,
            upgrade: from & ALPM_DB_USAGE_UPGRADE != 0,
        }
    }
}