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
use std::os::raw::{c_char, c_void};

use crate::db::alpm_db_t;
use crate::dependency::{alpm_conflict_t, alpm_depend_t, Conflict, Depend};
use crate::enums;
use crate::list::alpm_list_t;
use crate::package::{alpm_pkg_t, Package, PackageList};

#[allow(non_camel_case_types)]
type alpm_time_t = i64;

#[repr(C)]
#[derive(Copy, Clone)]
struct alpm_pgpkey_t {
    data: *mut c_void,
    fingerprint: *const c_char,
    uid: *const c_char,
    name: *const c_char,
    email: *const c_char,
    created: alpm_time_t,
    expires: alpm_time_t,
    length: u32,
    revoked: u32,
    pubkey_algo: u8,
}

#[repr(C)]
#[derive(Copy, Clone)]
struct alpm_question_any_t {
    question_type: i32,
    answer: i32,
}

#[repr(C)]
#[derive(Copy, Clone)]
struct alpm_question_install_ignorepkg_t {
    question_type: i32,
    answer: i32,
    pkg: *mut alpm_pkg_t,
}

#[repr(C)]
#[derive(Copy, Clone)]
struct alpm_question_replace_t {
    question_type: i32,
    answer: i32,
    oldpkg: *mut alpm_pkg_t,
    newpkg: *mut alpm_pkg_t,
    newdb: *mut alpm_db_t,
}

#[repr(C)]
#[derive(Copy, Clone)]
struct alpm_question_conflict_t {
    question_type: i32,
    answer: i32,
    conflict: *mut alpm_conflict_t,
}

#[repr(C)]
#[derive(Copy, Clone)]
struct alpm_question_corrupted_t {
    question_type: i32,
    answer: i32,
    filepath: *const c_char,
    reason: enums::ErrorNo,
}

#[repr(C)]
#[derive(Copy, Clone)]
struct alpm_question_remove_pkgs_t {
    question_type: i32,
    answer: i32,
    packages: *mut alpm_list_t,
}

#[repr(C)]
#[derive(Copy, Clone)]
struct alpm_question_select_provider_t {
    question_type: i32,
    answer: i32,
    providers: *mut alpm_list_t, //List of alpm_pkg_t
    dependant: *mut alpm_depend_t,
}

#[repr(C)]
#[derive(Copy, Clone)]
struct alpm_question_import_key_t {
    question_type: i32,
    answer: i32,
    key: *mut alpm_pgpkey_t,
}

#[repr(C)]
pub union alpm_question_t {
    question_type: i32,
    any: alpm_question_any_t,
    install_ignorepkg: alpm_question_install_ignorepkg_t,
    replace: alpm_question_replace_t,
    conflict: alpm_question_conflict_t,
    corrupted: alpm_question_corrupted_t,
    remove_pkgs: alpm_question_remove_pkgs_t,
    select_provider: alpm_question_select_provider_t,
    import_key: alpm_question_import_key_t,
}

pub struct PgpKey {
    pub data: *mut c_void,
    pub fingerprint: String,
    pub uid: String,
    pub name: String,
    pub email: String,
    pub created: i64,
    pub expires: i64,
    pub length: u32,
    pub revoked: u32,
    pub pubkey_algo: u8,
}

impl From<*mut alpm_pgpkey_t> for PgpKey {
    fn from(pgpk: *mut alpm_pgpkey_t) -> Self {
        let k = unsafe { *pgpk };
        PgpKey {
            data: k.data,
            fingerprint: cstring!(k.fingerprint),
            uid: cstring!(k.uid),
            name: cstring!(k.name),
            email: cstring!(k.email),
            created: k.created,
            expires: k.expires,
            length: k.length,
            revoked: k.revoked,
            pubkey_algo: k.pubkey_algo,
        }
    }
}

pub struct QuestionAny {
    pub question_type: i32,
    pub answer: i32,
}

impl From<*mut alpm_question_t> for QuestionAny {
    fn from(q_raw: *mut alpm_question_t) -> Self {
        let q = unsafe { *(q_raw as *mut alpm_question_any_t) };
        QuestionAny {
            question_type: q.question_type,
            answer: q.answer,
        }
    }
}

pub struct QuestionInstallIgnorePkg {
    pub question_type: i32,
    pub answer: i32,
    pub pkg: Package,
}

impl From<*mut alpm_question_t> for QuestionInstallIgnorePkg {
    fn from(q_raw: *mut alpm_question_t) -> Self {
        let q = unsafe { *(q_raw as *mut alpm_question_install_ignorepkg_t) };
        QuestionInstallIgnorePkg {
            question_type: q.question_type,
            answer: q.answer,
            pkg: q.pkg.into(),
        }
    }
}

pub struct QuestionReplacePkg {
    pub question_type: i32,
    pub answer: i32,
    pub conflict: Conflict,
}

impl From<*mut alpm_question_t> for QuestionReplacePkg {
    fn from(q_raw: *mut alpm_question_t) -> Self {
        let q = unsafe { *(q_raw as *mut alpm_question_conflict_t) };
        QuestionReplacePkg {
            question_type: q.question_type,
            answer: q.answer,
            conflict: q.conflict.into(),
        }
    }
}

pub struct QuestionConflict {
    pub question_type: i32,
    pub answer: i32,
    pub conflict: Conflict,
}

impl From<*mut alpm_question_t> for QuestionConflict {
    fn from(q_raw: *mut alpm_question_t) -> Self {
        let q = unsafe { *(q_raw as *mut alpm_question_conflict_t) };
        QuestionConflict {
            question_type: q.question_type,
            answer: q.answer,
            conflict: q.conflict.into(),
        }
    }
}

pub struct QuestionCorrupted {
    pub question_type: i32,
    pub answer: i32,
    pub filepath: String,
    pub reason: enums::ErrorNo,
}

impl From<*mut alpm_question_t> for QuestionCorrupted {
    fn from(q_raw: *mut alpm_question_t) -> Self {
        let q = unsafe { *(q_raw as *mut alpm_question_corrupted_t) };
        QuestionCorrupted {
            question_type: q.question_type,
            answer: q.answer,
            filepath: cstring!(q.filepath),
            reason: q.reason,
        }
    }
}

pub struct QuestionRemovePackages {
    pub question_type: i32,
    pub answer: i32,
    pub packages: PackageList,
}

impl From<*mut alpm_question_t> for QuestionRemovePackages {
    fn from(q_raw: *mut alpm_question_t) -> Self {
        let q = unsafe { *(q_raw as *mut alpm_question_remove_pkgs_t) };
        QuestionRemovePackages {
            question_type: q.question_type,
            answer: q.answer,
            packages: q.packages.into(),
        }
    }
}

pub struct QuestionSelectProvider {
    pub question_type: i32,
    pub answer: i32,
    pub providers: PackageList,
    pub dependant: Depend,
}

impl From<*mut alpm_question_t> for QuestionSelectProvider {
    fn from(q_raw: *mut alpm_question_t) -> Self {
        let q = unsafe { *(q_raw as *mut alpm_question_select_provider_t) };
        QuestionSelectProvider {
            question_type: q.question_type,
            answer: q.answer,
            providers: q.providers.into(),
            dependant: q.dependant.into(),
        }
    }
}

pub struct QuestionImportKey {
    pub question_type: i32,
    pub answer: i32,
    pub key: PgpKey,
}

impl From<*mut alpm_question_t> for QuestionImportKey {
    fn from(q_raw: *mut alpm_question_t) -> Self {
        let q = unsafe { *(q_raw as *mut alpm_question_import_key_t) };
        QuestionImportKey {
            question_type: q.question_type,
            answer: q.answer,
            key: q.key.into(),
        }
    }
}

pub enum Question {
    Any(QuestionAny),
    InstallIgnorePkg(QuestionInstallIgnorePkg),
    Replace(QuestionReplacePkg),
    Conflict(QuestionConflict),
    Corrupted(QuestionCorrupted),
    RemovePkgs(QuestionRemovePackages),
    SelectProvider(QuestionSelectProvider),
    ImportKey(QuestionImportKey),
}

pub struct QuestionArgs {
    question_raw: *mut alpm_question_any_t,
    pub question: Question,
}

impl QuestionArgs {
    pub fn set_answer(&self, ans: i32) {
        unsafe {
            (*self.question_raw).answer = ans;
        }
    }

    pub fn answer(&self) -> i32 {
        unsafe { (*self.question_raw).answer }
    }

    pub fn to_any(&self) -> QuestionAny {
        (self.question_raw as *mut alpm_question_t).into()
    }
}

impl From<*mut alpm_question_t> for QuestionArgs {
    fn from(q: *mut alpm_question_t) -> Self {
        QuestionArgs {
            question_raw: q as *mut alpm_question_any_t,
            question: unsafe {
                match (*q).question_type {
                    enums::ALPM_QUESTION_INSTALL_IGNOREPKG => Question::InstallIgnorePkg(q.into()),
                    enums::ALPM_QUESTION_REPLACE_PKG => Question::Replace(q.into()),
                    enums::ALPM_QUESTION_CONFLICT_PKG => Question::Conflict(q.into()),
                    enums::ALPM_QUESTION_CORRUPTED_PKG => Question::Corrupted(q.into()),
                    enums::ALPM_QUESTION_REMOVE_PKGS => Question::RemovePkgs(q.into()),
                    enums::ALPM_QUESTION_SELECT_PROVIDER => Question::SelectProvider(q.into()),
                    enums::ALPM_QUESTION_IMPORT_KEY => Question::ImportKey(q.into()),
                    _ => Question::Any(q.into()),
                }
            },
        }
    }
}