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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
extern crate libloading as so;
#[macro_use] extern crate enum_primitive;
extern crate ini;
extern crate num;
use ini::Ini;
use num::FromPrimitive;
use std::cmp::Ordering;
use std::ffi::{CStr, CString};
use std::io::{Error, ErrorKind};
use std::os::raw::{c_char, c_int};
use so::Symbol;
pub const PACMAN_CONF: &'static str = "/etc/pacman.conf";
pub const PACMAN_DEFAULT_DBPATH: &'static str = "/var/lib/pacman/";
enum_from_primitive! {
#[repr(C)]
#[derive(Debug, PartialEq)]
enum AlpmErrno {
ALPM_ERR_MEMORY = 1,
ALPM_ERR_SYSTEM,
ALPM_ERR_BADPERMS,
ALPM_ERR_NOT_A_FILE,
ALPM_ERR_NOT_A_DIR,
ALPM_ERR_WRONG_ARGS,
ALPM_ERR_DISK_SPACE,
ALPM_ERR_HANDLE_NULL,
ALPM_ERR_HANDLE_NOT_NULL,
ALPM_ERR_HANDLE_LOCK,
ALPM_ERR_DB_OPEN,
ALPM_ERR_DB_CREATE,
ALPM_ERR_DB_NULL,
ALPM_ERR_DB_NOT_NULL,
ALPM_ERR_DB_NOT_FOUND,
ALPM_ERR_DB_INVALID,
ALPM_ERR_DB_INVALID_SIG,
ALPM_ERR_DB_VERSION,
ALPM_ERR_DB_WRITE,
ALPM_ERR_DB_REMOVE,
ALPM_ERR_SERVER_BAD_URL,
ALPM_ERR_SERVER_NONE,
ALPM_ERR_TRANS_NOT_NULL,
ALPM_ERR_TRANS_NULL,
ALPM_ERR_TRANS_DUP_TARGET,
ALPM_ERR_TRANS_NOT_INITIALIZED,
ALPM_ERR_TRANS_NOT_PREPARED,
ALPM_ERR_TRANS_ABORT,
ALPM_ERR_TRANS_TYPE,
ALPM_ERR_TRANS_NOT_LOCKED,
ALPM_ERR_TRANS_HOOK_FAILED,
ALPM_ERR_PKG_NOT_FOUND,
ALPM_ERR_PKG_IGNORED,
ALPM_ERR_PKG_INVALID,
ALPM_ERR_PKG_INVALID_CHECKSUM,
ALPM_ERR_PKG_INVALID_SIG,
ALPM_ERR_PKG_MISSING_SIG,
ALPM_ERR_PKG_OPEN,
ALPM_ERR_PKG_CANT_REMOVE,
ALPM_ERR_PKG_INVALID_NAME,
ALPM_ERR_PKG_INVALID_ARCH,
ALPM_ERR_PKG_REPO_NOT_FOUND,
ALPM_ERR_SIG_MISSING,
ALPM_ERR_SIG_INVALID,
ALPM_ERR_DLT_INVALID,
ALPM_ERR_DLT_PATCHFAILED,
ALPM_ERR_UNSATISFIED_DEPS,
ALPM_ERR_CONFLICTING_DEPS,
ALPM_ERR_FILE_CONFLICTS,
ALPM_ERR_RETRIEVE,
ALPM_ERR_INVALID_REGEX,
ALPM_ERR_LIBARCHIVE,
ALPM_ERR_LIBCURL,
ALPM_ERR_EXTERNAL_DOWNLOAD,
ALPM_ERR_GPGME
}
}
pub struct Alpm {
lib: so::Library,
handle: *const usize,
error_no: Box<usize>,
}
impl Alpm {
pub fn new() -> Result<Alpm, std::io::Error> {
Self::with_dbpath( extract_dbpath() )
}
pub fn with_dbpath(dbpath: String) -> Result<Alpm, std::io::Error> {
let lib = try!( so::Library::new("/usr/lib/libalpm.so") );
let root = try!( CString::new("/") );
let mut error_no = Box::new(0);
let handle = unsafe {
let init: Symbol<unsafe extern fn(*const c_char, *const c_char, *mut usize) -> *const usize> = try!(lib.get(b"alpm_initialize\0"));
let dbpath = CString::new(dbpath).unwrap();
init(root.as_ptr(), dbpath.as_ptr(), error_no.as_mut())
};
assert!(handle != 0 as *const usize,
"handle was {}/NULL, error_no: {}/{:?}/{:?}",
handle as usize,
error_no,
AlpmErrno::from_usize(*error_no),
translate_error_no(&lib, *error_no));
Ok(Alpm {
lib: lib,
handle: handle,
error_no: error_no,
})
}
pub fn query_package_version<S>(&self, query: S) -> std::io::Result<String> where S: Into<String> {
let s: String = query.into();
let cs = try!( CString::new(s.clone()) );
unsafe {
let get_db: Symbol<fn(*const usize) -> *const usize> = try!( self.lib.get(b"alpm_get_localdb\0") );
let db_get_pkgcache: Symbol<fn(*const usize) -> *const usize> = try!( self.lib.get(b"alpm_db_get_pkgcache\0") );
let pkg_find_in_list: Symbol<fn(*const usize, *const c_char) -> *const usize> = try!( self.lib.get(b"alpm_pkg_find\0") );
let get_version: Symbol<fn(*const usize) -> *const c_char> = try!( self.lib.get(b"alpm_pkg_get_version\0") );
let db = get_db(self.handle);
let list = db_get_pkgcache(db);
let pkg = pkg_find_in_list(list, cs.as_ptr() as *const c_char);
if pkg != std::ptr::null() {
let version_chars = get_version(pkg);
Ok(CStr::from_ptr(version_chars)
.to_string_lossy()
.into_owned())
} else {
Err(Error::new(ErrorKind::Other, format!("No package {} found!", s)))
}
}
}
pub fn vercmp(&self, a: String, b: String) -> std::io::Result<Ordering> {
let a = try!( CString::new(a) );
let b = try!( CString::new(b) );
unsafe {
let pkg_vercmp: Symbol<fn(*const c_char, *const c_char) -> *const c_int> = try!( self.lib.get(b"alpm_pkg_vercmp\0") );
let ret = pkg_vercmp(a.as_ptr() as *const c_char, b.as_ptr() as *const c_char) as i32;
Ok(if ret < 0 { Ordering::Less }
else if ret > 0 { Ordering::Greater }
else { Ordering::Equal })
}
}
}
impl Drop for Alpm {
fn drop(&mut self) {
unsafe {
let alpm_release: Symbol<unsafe extern fn(*const usize) -> *const usize> = self.lib.get(b"alpm_release\0").unwrap();
alpm_release(self.handle);
}
assert!(0 == *self.error_no, "Alpm: an error occured: {}", self.error_no);
}
}
fn translate_error_no(lib: &so::Library, error_no: usize) -> Result<String, std::io::Error> {
unsafe {
let alpm_strerror: Symbol<unsafe extern fn(usize) -> *const c_char> = try!(lib.get(b"alpm_strerror\0"));
let cs = alpm_strerror(error_no);
Ok(CStr::from_ptr(cs)
.to_string_lossy()
.into_owned())
}
}
fn extract_dbpath() -> String {
if let Ok(conf) = Ini::load_from_file(PACMAN_CONF) {
if let Some(path) = conf.section(Some("options".to_owned())).unwrap().get("DBPath") {
return path.to_string();
}
}
PACMAN_DEFAULT_DBPATH.to_string()
}
#[cfg(test)]
mod tests {
use ::Alpm;
#[test]
fn query_pacman() {
let pacman = Alpm::new().unwrap();
assert_eq!("5.0.1-4".to_string(), pacman.query_package_version("pacman").unwrap());
}
#[test]
#[should_panic]
fn query_not_installed() {
let pacman = Alpm::new().unwrap();
pacman.query_package_version("non-existing").unwrap();
}
#[test]
fn query_hdf5() {
let pacman = Alpm::new().unwrap();
assert_eq!("1.10.0_patch1-1".to_string(), pacman.query_package_version("hdf5").unwrap());
}
#[test]
fn vercmp_less() {
use std::cmp::Ordering;
let pacman = Alpm::new().unwrap();
assert_eq!(Ordering::Less, pacman.vercmp("1".to_string(), "1.0-2".to_string()).unwrap());
assert_eq!(Ordering::Less, pacman.vercmp("1.1".to_string(), "1.1.2".to_string()).unwrap());
assert_eq!(Ordering::Less, pacman.vercmp("1.1".to_string(), "1.2".to_string()).unwrap());
assert_eq!(Ordering::Less, pacman.vercmp("1.9".to_string(), "2".to_string()).unwrap());
assert_eq!(Ordering::Less, pacman.vercmp("1.1.10".to_string(), "2".to_string()).unwrap());
assert_eq!(Ordering::Less, pacman.vercmp("1".to_string(), "2".to_string()).unwrap());
}
#[test]
fn vercmp_equal() {
use std::cmp::Ordering;
let pacman = Alpm::new().unwrap();
assert_eq!(Ordering::Equal, pacman.vercmp("1.0".to_string(), "1.0-2".to_string()).unwrap());
assert_eq!(Ordering::Equal, pacman.vercmp("1:1-1".to_string(), "1:1-1".to_string()).unwrap());
}
#[test]
fn vercmp_greater() {
use std::cmp::Ordering;
let pacman = Alpm::new().unwrap();
assert_eq!(Ordering::Greater, pacman.vercmp("2.0-1".to_string(), "1.0-1".to_string()).unwrap());
}
}