apt-pkg-native 0.3.3

Bindings for libapt-pkg
Documentation
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
use std::cmp;
use std::ffi;
use std::marker::PhantomData;
use std::sync::MutexGuard;

use crate::citer::CIterator;
use crate::citer::RawIterator;
use crate::raw;

/// A reference to the package cache singleton,
/// from which most functionality can be accessed.
#[derive(Debug)]
pub struct Cache {
    ptr_mutex: &'static raw::CACHE_SINGLETON,
}

impl Cache {
    /// Get a reference to the singleton.
    pub fn get_singleton() -> Cache {
        Cache {
            ptr_mutex: raw::pkg_cache_get_singleton(),
        }
    }

    /// Drop the cache, and re-create it from scratch.
    ///
    /// It's super important that there are no other outstanding
    /// references to the cache at this point. Again, I remind you
    /// not to try and outsmart the borrow checker. It doesn't know
    /// how much trouble there is in here.
    pub fn reload(&mut self) {
        self.ptr_mutex.lock().expect("poisoned mutex").re_up()
    }

    /// Walk through all of the packages, in a random order.
    ///
    /// If there are multiple architectures, multiple architectures will be returned.
    ///
    /// See the module documentation for apologies about how this isn't an iterator.
    pub fn iter(&mut self) -> CIterator<PkgIterator> {
        let lock = self.ptr_mutex.lock().expect("poisoned mutex");
        unsafe {
            let raw_iter = raw::pkg_cache_pkg_iter(lock.ptr);
            PkgIterator::new(lock, raw_iter)
        }
    }

    /// Find a package by name. It's not clear whether this picks a random arch,
    /// or the primary one.
    ///
    /// The returned iterator will either be at the end, or at a package with the name.
    pub fn find_by_name(&mut self, name: &str) -> CIterator<PkgIterator> {
        let lock = self.ptr_mutex.lock().expect("poisoned mutex");
        unsafe {
            let name = ffi::CString::new(name).unwrap();
            let ptr = raw::pkg_cache_find_name(lock.ptr, name.as_ptr());
            PkgIterator::new(lock, ptr)
        }
    }

    /// Find a package by name and architecture.
    ///
    /// The returned iterator will either be at the end, or at a matching package.
    pub fn find_by_name_arch(&mut self, name: &str, arch: &str) -> CIterator<PkgIterator> {
        let lock = self.ptr_mutex.lock().expect("poisoned mutex");
        unsafe {
            let name = ffi::CString::new(name).unwrap();
            let arch = ffi::CString::new(arch).unwrap();
            let ptr = raw::pkg_cache_find_name_arch(lock.ptr, name.as_ptr(), arch.as_ptr());
            PkgIterator::new(lock, ptr)
        }
    }

    /// Compare two versions, returning an `Ordering`, as used by most Rusty `sort()` methods.
    ///
    /// This uses the "versioning scheme" currently set, which, in theory, can change,
    /// but in practice is always the "Standard .deb" scheme. As of 2017, there aren't even any
    /// other implementations. As such, this may eventually become a static method somewhere.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # let mut cache = apt_pkg_native::Cache::get_singleton();
    /// let mut packages = vec!["3.0", "3.1", "3.0~1"];
    /// packages.sort_by(|left, right| cache.compare_versions(left, right));
    /// assert_eq!(vec!["3.0~1", "3.0", "3.1"], packages);
    /// ```
    pub fn compare_versions(&self, left: &str, right: &str) -> cmp::Ordering {
        unsafe {
            let left = ffi::CString::new(left).unwrap();
            let right = ffi::CString::new(right).unwrap();

            let lock = self.ptr_mutex.lock().expect("poisoned mutex");
            raw::pkg_cache_compare_versions(lock.ptr, left.as_ptr(), right.as_ptr()).cmp(&0)
        }
    }
}

/// An "iterator"/pointer to a point in a package list.
#[derive(Debug)]
pub struct PkgIterator<'c> {
    cache: MutexGuard<'c, raw::CacheHolder>,
    ptr: raw::PPkgIterator,
}

impl<'c> PkgIterator<'c> {
    fn new(cache: MutexGuard<'c, raw::CacheHolder>, ptr: raw::PCache) -> CIterator<Self> {
        CIterator {
            first: true,
            raw: PkgIterator { cache, ptr },
        }
    }
}

// TODO: could this be a ref to the iterator?
// TODO: Can't get the lifetimes to work.
pub struct PkgView<'c> {
    cache: PhantomData<&'c MutexGuard<'c, raw::CacheHolder>>,
    ptr: raw::PPkgIterator,
}

impl<'c> RawIterator for PkgIterator<'c> {
    type View = PkgView<'c>;

    fn is_end(&self) -> bool {
        unsafe { raw::pkg_iter_end(self.ptr) }
    }

    fn next(&mut self) {
        unsafe { raw::pkg_iter_next(self.ptr) }
    }

    fn as_view(&self) -> Self::View {
        assert!(!self.is_end());

        PkgView {
            ptr: self.ptr,
            cache: PhantomData,
        }
    }

    fn release(&mut self) {
        unsafe { raw::pkg_iter_release(self.ptr) }
    }
}

/// Actual accessors
impl<'c> PkgView<'c> {
    pub fn name(&self) -> String {
        unsafe {
            make_owned_ascii_string(raw::pkg_iter_name(self.ptr))
                .expect("packages always have names")
        }
    }

    pub fn arch(&self) -> String {
        unsafe {
            make_owned_ascii_string(raw::pkg_iter_arch(self.ptr))
                .expect("packages always have architectures")
        }
    }

    pub fn current_version(&self) -> Option<String> {
        unsafe { make_owned_ascii_string(raw::pkg_iter_current_version(self.ptr)) }
    }

    pub fn candidate_version(&self) -> Option<String> {
        unsafe { make_owned_ascii_string(raw::pkg_iter_candidate_version(self.ptr)) }
    }

    pub fn versions(&self) -> CIterator<VerIterator> {
        CIterator {
            first: true,
            raw: VerIterator {
                cache: PhantomData,
                ptr: unsafe { raw::pkg_iter_ver_iter(self.ptr) },
            },
        }
    }
}

/// Represents a single PkgView without associated PkgIterator. Derefs to
/// regular PkgView and releases the internal iterator on drop.
pub struct SinglePkgView<'c> {
    view: PkgView<'c>,
}

impl<'c> std::ops::Deref for SinglePkgView<'c> {
    type Target = PkgView<'c>;

    fn deref(&self) -> &Self::Target {
        &self.view
    }
}

impl<'c> Drop for SinglePkgView<'c> {
    fn drop(&mut self) {
        unsafe {
            raw::pkg_iter_release(self.view.ptr);
        }
    }
}

/// An "iterator"/pointer to a point in a version list.
pub struct VerIterator<'c> {
    cache: PhantomData<&'c MutexGuard<'c, raw::CacheHolder>>,
    ptr: raw::PVerIterator,
}

pub struct VerView<'c> {
    cache: PhantomData<&'c MutexGuard<'c, raw::CacheHolder>>,
    ptr: raw::PVerIterator,
}

impl<'c> RawIterator for VerIterator<'c> {
    type View = VerView<'c>;

    fn is_end(&self) -> bool {
        unsafe { raw::ver_iter_end(self.ptr) }
    }

    fn next(&mut self) {
        unsafe { raw::ver_iter_next(self.ptr) }
    }

    fn as_view(&self) -> Self::View {
        assert!(!self.is_end());

        VerView {
            ptr: self.ptr,
            cache: self.cache,
        }
    }

    fn release(&mut self) {
        unsafe { raw::ver_iter_release(self.ptr) }
    }
}

/// Actual accessors
impl<'c> VerView<'c> {
    pub fn version(&self) -> String {
        unsafe {
            make_owned_ascii_string(raw::ver_iter_version(self.ptr))
                .expect("versions always have a version")
        }
    }

    pub fn arch(&self) -> String {
        unsafe {
            make_owned_ascii_string(raw::ver_iter_arch(self.ptr))
                .expect("versions always have an arch")
        }
    }

    pub fn section(&self) -> Option<String> {
        unsafe { make_owned_ascii_string(raw::ver_iter_section(self.ptr)) }
    }

    pub fn priority_type(&self) -> Option<String> {
        unsafe { make_owned_ascii_string(raw::ver_iter_priority_type(self.ptr)) }
    }

    #[cfg(not(feature = "ye-olde-apt"))]
    pub fn source_package(&self) -> String {
        unsafe {
            make_owned_ascii_string(raw::ver_iter_source_package(self.ptr))
                .expect("versions always have a source package")
        }
    }

    #[cfg(not(feature = "ye-olde-apt"))]
    pub fn source_version(&self) -> String {
        unsafe {
            make_owned_ascii_string(raw::ver_iter_source_version(self.ptr))
                .expect("versions always have a source_version")
        }
    }

    #[cfg(not(feature = "ye-olde-apt"))]
    pub fn priority(&self) -> i32 {
        unsafe { raw::ver_iter_priority(self.ptr) }
    }

    pub fn origin_iter(&self) -> CIterator<VerFileIterator> {
        CIterator {
            first: true,
            raw: VerFileIterator {
                cache: PhantomData,
                ptr: unsafe { raw::ver_iter_ver_file_iter(self.ptr) },
            },
        }
    }

    pub fn dep_iter(&self) -> CIterator<DepIterator> {
        CIterator {
            first: true,
            raw: DepIterator {
                cache: PhantomData,
                ptr: unsafe { raw::ver_iter_dep_iter(self.ptr) },
            },
        }
    }
}

/// An "iterator"/pointer to a point in a dependency list.
pub struct DepIterator<'c> {
    cache: PhantomData<&'c MutexGuard<'c, raw::CacheHolder>>,
    ptr: raw::PDepIterator,
}

pub struct DepView<'c> {
    cache: PhantomData<&'c MutexGuard<'c, raw::CacheHolder>>,
    ptr: raw::PDepIterator,
}

impl<'c> RawIterator for DepIterator<'c> {
    type View = DepView<'c>;

    fn is_end(&self) -> bool {
        unsafe { raw::dep_iter_end(self.ptr) }
    }

    fn next(&mut self) {
        unsafe { raw::dep_iter_next(self.ptr) }
    }

    fn as_view(&self) -> Self::View {
        assert!(!self.is_end());

        DepView {
            ptr: self.ptr,
            cache: self.cache,
        }
    }

    fn release(&mut self) {
        unsafe { raw::dep_iter_release(self.ptr) }
    }
}

/// Actual accessors
impl<'c> DepView<'c> {
    pub fn target_pkg(&self) -> SinglePkgView {
        let ptr = unsafe { raw::dep_iter_target_pkg(self.ptr) };
        SinglePkgView {
            view: PkgView {
                cache: self.cache,
                ptr,
            },
        }
    }

    pub fn target_ver(&self) -> String {
        unsafe {
            make_owned_ascii_string(raw::dep_iter_target_ver(self.ptr))
                .expect("dependency always has target version")
        }
    }

    pub fn comp_type(&self) -> String {
        unsafe {
            make_owned_ascii_string(raw::dep_iter_comp_type(self.ptr))
                .expect("dependency always has comp type")
        }
    }

    pub fn dep_type(&self) -> String {
        unsafe {
            make_owned_ascii_string(raw::dep_iter_dep_type(self.ptr))
                .expect("dependency always has dep type")
        }
    }
}

/// An "iterator"/pointer to a point in a version's file list(?).
pub struct VerFileIterator<'c> {
    cache: PhantomData<&'c MutexGuard<'c, raw::CacheHolder>>,
    ptr: raw::PVerFileIterator,
}

// TODO: could this be a ref to the iterator?
// TODO: Can't get the lifetimes to work.
pub struct VerFileView<'c> {
    cache: PhantomData<&'c MutexGuard<'c, raw::CacheHolder>>,
    ptr: raw::PVerFileIterator,
    parser: raw::PVerFileParser,
}

impl<'c> RawIterator for VerFileIterator<'c> {
    type View = VerFileView<'c>;

    fn is_end(&self) -> bool {
        unsafe { raw::ver_file_iter_end(self.ptr) }
    }

    fn next(&mut self) {
        unsafe { raw::ver_file_iter_next(self.ptr) }
    }

    fn as_view(&self) -> Self::View {
        assert!(!self.is_end());

        let parser = unsafe { raw::ver_file_iter_get_parser(self.ptr) };

        VerFileView {
            ptr: self.ptr,
            cache: self.cache,
            parser,
        }
    }

    fn release(&mut self) {
        unsafe { raw::ver_file_iter_release(self.ptr) }
    }
}

impl<'c> VerFileView<'c> {
    pub fn file(&self) -> CIterator<PkgFileIterator> {
        CIterator {
            first: true,
            raw: PkgFileIterator {
                cache: PhantomData,
                ptr: unsafe { raw::ver_file_iter_pkg_file_iter(self.ptr) },
            },
        }
    }

    pub fn short_desc(&self) -> Option<String> {
        unsafe { make_owned_ascii_string(raw::ver_file_parser_short_desc(self.parser)) }
    }

    pub fn long_desc(&self) -> Option<String> {
        unsafe { make_owned_ascii_string(raw::ver_file_parser_long_desc(self.parser)) }
    }

    pub fn maintainer(&self) -> Option<String> {
        unsafe { make_owned_ascii_string(raw::ver_file_parser_maintainer(self.parser)) }
    }

    pub fn homepage(&self) -> Option<String> {
        unsafe { make_owned_ascii_string(raw::ver_file_parser_homepage(self.parser)) }
    }
}

/// An "iterator"/pointer to a point in a file list.
pub struct PkgFileIterator<'c> {
    cache: PhantomData<&'c MutexGuard<'c, raw::CacheHolder>>,
    ptr: raw::PPkgFileIterator,
}

// TODO: could this be a ref to the iterator?
// TODO: Can't get the lifetimes to work.
pub struct PkgFileView<'c> {
    cache: PhantomData<&'c MutexGuard<'c, raw::CacheHolder>>,
    ptr: raw::PPkgFileIterator,
}

impl<'c> RawIterator for PkgFileIterator<'c> {
    type View = PkgFileView<'c>;

    fn is_end(&self) -> bool {
        unsafe { raw::pkg_file_iter_end(self.ptr) }
    }

    fn next(&mut self) {
        unsafe { raw::pkg_file_iter_next(self.ptr) }
    }

    fn as_view(&self) -> Self::View {
        assert!(!self.is_end());

        PkgFileView {
            ptr: self.ptr,
            cache: self.cache,
        }
    }

    fn release(&mut self) {
        unsafe { raw::pkg_file_iter_release(self.ptr) }
    }
}

impl<'c> PkgFileView<'c> {
    pub fn file_name(&self) -> String {
        unsafe {
            make_owned_ascii_string(raw::pkg_file_iter_file_name(self.ptr))
                .expect("package file always has a file name")
        }
    }
    pub fn archive(&self) -> String {
        unsafe {
            make_owned_ascii_string(raw::pkg_file_iter_archive(self.ptr))
                .expect("package file always has an archive")
        }
    }
    pub fn version(&self) -> Option<String> {
        unsafe { make_owned_ascii_string(raw::pkg_file_iter_version(self.ptr)) }
    }
    pub fn origin(&self) -> Option<String> {
        unsafe { make_owned_ascii_string(raw::pkg_file_iter_origin(self.ptr)) }
    }
    pub fn codename(&self) -> Option<String> {
        unsafe { make_owned_ascii_string(raw::pkg_file_iter_codename(self.ptr)) }
    }
    pub fn label(&self) -> Option<String> {
        unsafe { make_owned_ascii_string(raw::pkg_file_iter_label(self.ptr)) }
    }
    pub fn site(&self) -> Option<String> {
        unsafe { make_owned_ascii_string(raw::pkg_file_iter_site(self.ptr)) }
    }
    pub fn component(&self) -> String {
        unsafe {
            make_owned_ascii_string(raw::pkg_file_iter_component(self.ptr))
                .expect("package file always has a component")
        }
    }
    pub fn architecture(&self) -> Option<String> {
        unsafe { make_owned_ascii_string(raw::pkg_file_iter_architecture(self.ptr)) }
    }
    pub fn index_type(&self) -> String {
        unsafe {
            make_owned_ascii_string(raw::pkg_file_iter_index_type(self.ptr))
                .expect("package file always has a index_type")
        }
    }
}

unsafe fn make_owned_ascii_string(ptr: *const libc::c_char) -> Option<String> {
    if ptr.is_null() {
        None
    } else {
        Some(
            ffi::CStr::from_ptr(ptr)
                .to_str()
                .expect("value should always be low-ascii")
                .to_string(),
        )
    }
}