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
#[macro_use]
pub mod macros;
pub mod callbacks;
pub mod clib;
pub mod db;
pub mod dependency;
pub mod enums;
pub mod error;
pub mod list;
pub mod package;
pub mod question;
pub use crate::list::AlpmListItem;
extern crate libc;
use std::error::Error;
use std::os::raw::c_char;
use crate::db::{alpm_db_t, AlpmDB, DBList};
use crate::dependency::DepMissingList;
use crate::enums::ErrorNo;
use crate::list::{alpm_list_t, AlpmList, AnyList};
use crate::package::{alpm_pkg_t, Package};
#[link(name = "alpm")]
extern "C" {
fn alpm_initialize(
root: *const c_char,
dbpath: *const c_char,
error: *mut ErrorNo,
) -> *mut alpm_handle_t;
fn alpm_release(handle: *mut alpm_handle_t) -> i32;
fn alpm_errno(handle: *mut alpm_handle_t) -> ErrorNo;
fn alpm_pkg_should_ignore(handle: *mut alpm_handle_t, pkg_handle: *mut alpm_pkg_t) -> i32;
fn alpm_pkg_load(
handle: *mut alpm_handle_t,
filename: *const c_char,
full: i32,
level: i32,
pkg: *mut alpm_pkg_t,
) -> i32;
fn alpm_get_localdb(handle: *mut alpm_handle_t) -> *mut alpm_db_t;
fn alpm_get_syncdbs(handle: *mut alpm_handle_t) -> *mut alpm_list_t;
fn alpm_unregister_all_syncdbs(handle: *mut alpm_handle_t) -> i32;
fn alpm_register_syncdb(
handle: *mut alpm_handle_t,
treename: *const c_char,
level: i32,
) -> *mut alpm_db_t;
fn alpm_sync_sysupgrade(handle: *mut alpm_handle_t, enable_downgrade: i32) -> i32;
fn alpm_trans_init(handle: *mut alpm_handle_t, flags: i32) -> i32;
fn alpm_trans_get_add(handle: *mut alpm_handle_t) -> *mut alpm_list_t;
fn alpm_trans_commit(handle: *mut alpm_handle_t, list: *mut *mut alpm_list_t) -> i32;
fn alpm_trans_release(handle: *mut alpm_handle_t) -> i32;
fn alpm_trans_prepare(handle: *mut alpm_handle_t, data: *mut *mut alpm_list_t) -> i32;
fn alpm_add_pkg(handle: *mut alpm_handle_t, pkg: *mut alpm_pkg_t) -> i32;
fn alpm_remove_pkg(handle: *mut alpm_handle_t, pkg: *mut alpm_pkg_t) -> i32;
}
#[repr(C)]
struct alpm_handle_t {
__unused: [u8; 0],
}
pub struct Handle {
alpm_handle: *mut alpm_handle_t,
}
pub fn initialize(root: &str, dbpath: &str) -> Result<Handle, Box<dyn Error>> {
let mut err = enums::ErrorNo::ALPM_ERR_OK;
let handle: *mut alpm_handle_t;
unsafe {
handle = alpm_initialize(strc!(root), strc!(dbpath), &mut err as *mut enums::ErrorNo);
}
if err as i32 != 0 {
Err(error::AlpmError::new(err).into())
} else {
Ok(Handle::new(handle))
}
}
impl Handle {
fn new(handle: *mut alpm_handle_t) -> Self {
Handle {
alpm_handle: handle,
}
}
pub fn local_db(&self) -> AlpmDB {
unsafe { alpm_get_localdb(self.alpm_handle).into() }
}
pub fn sync_dbs(&self) -> DBList {
unsafe { alpm_get_syncdbs(self.alpm_handle).into() }
}
pub fn register_syncdb(&self, tree_name: &str, level: i32) -> AlpmDB {
unsafe { alpm_register_syncdb(self.alpm_handle, strc!(tree_name), level).into() }
}
pub fn unregister_all_syncdbs(&self) -> bool {
unsafe { to_bool!(alpm_unregister_all_syncdbs(self.alpm_handle)) }
}
pub fn release(&self) -> bool {
unsafe { to_bool!(alpm_release(self.alpm_handle)) }
}
pub fn error_no(&self) -> ErrorNo {
unsafe { alpm_errno(self.alpm_handle) }
}
pub fn should_ignore(&self, pkg: &Package) -> bool {
unsafe { to_bool!(alpm_pkg_should_ignore(self.alpm_handle, pkg.pkg)) }
}
pub fn load_package(&self, filename: &str, full: bool, level: i32) -> Option<Package> {
let mut lm_pkg = Box::new(alpm_pkg_t::none());
let pkg_ptr = &mut *lm_pkg as *mut alpm_pkg_t;
let err = unsafe {
alpm_pkg_load(
self.alpm_handle,
strc!(filename),
full.into(),
level,
pkg_ptr,
)
};
if err != -1 {
Some(pkg_ptr.into())
} else {
None
}
}
pub fn sys_upgrade(&self, enable_downgrade: bool) -> bool {
let en_dg = if enable_downgrade { 1 } else { 0 };
unsafe { to_bool!(alpm_sync_sysupgrade(self.alpm_handle, en_dg)) }
}
pub fn trans_init(&self, flags: i32) -> bool {
unsafe { to_bool!(alpm_trans_init(self.alpm_handle, flags)) }
}
pub fn add_pkg(&self, pkg: &Package) -> bool {
unsafe { to_bool!(alpm_add_pkg(self.alpm_handle, pkg.pkg)) }
}
pub fn remove_pkg(&self, pkg: &Package) -> bool {
unsafe { to_bool!(alpm_remove_pkg(self.alpm_handle, pkg.pkg)) }
}
pub fn trans_get_add(&self) -> package::PackageList {
unsafe { alpm_trans_get_add(self.alpm_handle).into() }
}
pub fn trans_release(&self) -> bool {
unsafe { to_bool!(alpm_trans_release(self.alpm_handle)) }
}
pub fn trans_prepare(&self, deps: *mut DepMissingList) -> bool {
unsafe {
let mut list = AlpmList::empty();
if alpm_trans_prepare(self.alpm_handle, &mut list.list as *mut *mut alpm_list_t) < 0 {
false
} else {
*deps = list;
true
}
}
}
pub fn trans_commit(&self, list: *mut AnyList) -> bool {
unsafe {
let mut t_list = AlpmList::empty();
if alpm_trans_commit(self.alpm_handle, &mut t_list.list as *mut *mut alpm_list_t) < 0 {
false
} else {
*list = t_list;
true
}
}
}
}