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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
use alpm_sys::*;
use libc::{self, c_char, c_ulong};
use chrono::{NaiveDateTime, NaiveDate};
use util;
use {Alpm, SigLevel, AlpmResult, Error, Db};
use std::ops::Deref;
use std::ffi::{CStr, CString};
use std::cmp;
use std::ptr;
use std::fmt;
use std::marker::PhantomData;
pub struct Package<'a> {
inner: *const Struct_alpm_pkg,
handle: &'a Alpm
}
impl<'b> Package<'b> {
pub(crate) fn new<'a>(raw: *const Struct_alpm_pkg, handle: &'a Alpm) -> Package<'a> {
Package {
inner: raw,
handle: handle,
}
}
pub fn load<'a>(alpm: &'a Alpm, filename: &str, full: bool, level: SigLevel)
-> AlpmResult<Package<'a>>
{
unsafe {
let pkg: *mut Struct_alpm_pkg = ptr::null_mut();
let res = alpm_pkg_load(alpm.handle,
CString::new(filename).unwrap().as_ptr(),
if full { 1 } else { 0 },
level.into(),
&pkg as *const *mut Struct_alpm_pkg);
if res == 0 {
Ok(Package::new(pkg, alpm))
} else {
Err(alpm.error().unwrap_or(Error::__Unknown))
}
}
}
}
impl<'a> Drop for Package<'a> {
fn drop(&mut self) {
unsafe { alpm_pkg_free(self.inner); }
}
}
impl<'a> Deref for Package<'a> {
type Target = PackageRef;
fn deref(&self) -> &PackageRef {
unsafe { &*(self.inner as *const PackageRef as *mut PackageRef) }
}
}
pub enum PackageRef {}
impl fmt::Debug for PackageRef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Package(\"{}\")", self.name())
}
}
impl PackageRef {
pub(crate) unsafe fn new<'b>(p: *const Struct_alpm_pkg) -> &'b PackageRef {
&*(p as *const PackageRef as *mut PackageRef)
}
pub fn check_md5(&self) -> bool {
unsafe { alpm_pkg_checkmd5sum(self as *const _ as _) == 0 }
}
pub fn compute_required_by(&self) -> Vec<String> {
unsafe {
let pkg_list = alpm_pkg_compute_requiredby(self as *const _ as _);
let pkgs = util::alpm_list_to_vec(pkg_list, |char_ptr| {
CStr::from_ptr(char_ptr as *const c_char).to_str().unwrap().to_owned()
});
alpm_list_free_inner(pkg_list, Some(libc::free));
alpm_list_free(pkg_list);
pkgs
}
}
pub fn compute_optional_for(&self) -> Vec<String> {
unsafe {
let pkg_list = alpm_pkg_compute_optionalfor(self as *const _ as _);
let pkgs = util::alpm_list_to_vec(pkg_list, |char_ptr| {
CStr::from_ptr(char_ptr as *const c_char).to_str().unwrap().to_owned()
});
alpm_list_free_inner(pkg_list, Some(libc::free));
alpm_list_free(pkg_list);
pkgs
}
}
pub fn should_ignore(&self, alpm: &Alpm) -> bool {
unsafe {
let res = alpm_pkg_should_ignore(alpm.handle, self as *const _ as _);
res != 0
}
}
pub fn filename(&self) -> &str {
unsafe {
let fname = alpm_pkg_get_filename(self as *const _ as _);
assert!(!fname.is_null());
CStr::from_ptr(fname).to_str().unwrap()
}
}
pub fn base(&self) -> Option<&str> {
unsafe {
let char_ptr = alpm_pkg_get_base(self as *const _ as _);
if char_ptr.is_null() {
None
} else {
Some(CStr::from_ptr(char_ptr).to_str().unwrap())
}
}
}
pub fn name(&self) -> &str {
unsafe {
let char_ptr = alpm_pkg_get_name(self as *const _ as _);
assert!(!char_ptr.is_null());
CStr::from_ptr(char_ptr).to_str().unwrap()
}
}
pub fn version<'a>(&'a self) -> PackageVersion<'a> {
unsafe {
let v = alpm_pkg_get_version(self as *const _ as _);
PackageVersion::new(v)
}
}
pub fn origin(&self) -> PackageFrom {
unsafe { alpm_pkg_get_origin(self as *const _ as _).into() }
}
pub fn description(&self) -> &str {
unsafe {
let char_ptr = alpm_pkg_get_desc(self as *const _ as _);
assert!(!char_ptr.is_null());
CStr::from_ptr(char_ptr).to_str().unwrap()
}
}
pub fn url(&self) -> &str {
unsafe {
let char_ptr = alpm_pkg_get_url(self as *const _ as _);
assert!(!char_ptr.is_null());
CStr::from_ptr(char_ptr).to_str().unwrap()
}
}
pub fn build_date(&self) -> NaiveDate {
unsafe {
let time = alpm_pkg_get_builddate(self as *const _ as _);
NaiveDateTime::from_timestamp(time, 0).date()
}
}
pub fn install_date(&self) -> Option<NaiveDate> {
unsafe {
let time = alpm_pkg_get_installdate(self as *const _ as _);
if time == 0 {
None
} else {
Some(NaiveDateTime::from_timestamp(time, 0).date())
}
}
}
pub fn packager(&self) -> &str {
unsafe {
let char_ptr = alpm_pkg_get_packager(self as *const _ as _);
assert!(!char_ptr.is_null());
CStr::from_ptr(char_ptr).to_str().unwrap()
}
}
pub fn md5(&self) -> Option<&str> {
unsafe {
let str_ptr = alpm_pkg_get_md5sum(self as *const _ as _);
if str_ptr.is_null() {
None
} else {
Some(CStr::from_ptr(str_ptr).to_str().unwrap())
}
}
}
pub fn sha256(&self) -> Option<&str> {
unsafe {
let str_ptr = alpm_pkg_get_sha256sum(self as *const _ as _);
if str_ptr.is_null() {
None
} else {
Some(CStr::from_ptr(str_ptr).to_str().unwrap())
}
}
}
pub fn arch(&self) -> &str {
unsafe {
let char_ptr = alpm_pkg_get_arch(self as *const _ as _);
assert!(!char_ptr.is_null());
CStr::from_ptr(char_ptr).to_str().unwrap()
}
}
pub fn remote_size(&self) -> u64 {
unsafe { alpm_pkg_get_size(self as *const _ as _) as u64 }
}
pub fn local_size(&self) -> u64 {
unsafe { alpm_pkg_get_isize(self as *const _ as _) as u64 }
}
pub fn reason(&self) -> Reason {
unsafe { alpm_pkg_get_reason(self as *const _ as _).into() }
}
pub fn licenses(&self) -> Vec<&str> {
unsafe {
let licenses = alpm_pkg_get_licenses(self as *const _ as _);
util::alpm_list_to_vec(licenses, |char_ptr| {
CStr::from_ptr(char_ptr as *const c_char).to_str().unwrap()
})
}
}
pub fn groups(&self) -> Vec<&str> {
unsafe {
let groups = alpm_pkg_get_groups(self as *const _ as _);
util::alpm_list_to_vec(groups, |char_ptr| {
CStr::from_ptr(char_ptr as *const c_char).to_str().unwrap()
})
}
}
pub fn depends<'a>(&'a self) -> Vec<Dependency<'a>> {
unsafe {
let deps = alpm_pkg_get_depends(self as *const _ as _);
util::alpm_list_to_vec(deps, |dep| {
Dependency::new(dep as *const alpm_depend_t)
})
}
}
pub fn optionally_depends<'a>(&'a self) -> Vec<Dependency<'a>> {
unsafe {
let deps = alpm_pkg_get_optdepends(self as *const _ as _);
util::alpm_list_to_vec(deps, |dep| {
Dependency::new(dep as *const alpm_depend_t)
})
}
}
pub fn conflicts<'a>(&'a self) -> Vec<Dependency<'a>> {
unsafe {
let deps = alpm_pkg_get_conflicts(self as *const _ as _);
util::alpm_list_to_vec(deps, |dep| {
Dependency::new(dep as *const alpm_depend_t)
})
}
}
pub fn provides<'a>(&'a self) -> Vec<Dependency<'a>> {
unsafe {
let deps = alpm_pkg_get_provides(self as *const _ as _);
util::alpm_list_to_vec(deps, |dep| {
Dependency::new(dep as *const alpm_depend_t)
})
}
}
pub fn deltas(&self) -> Vec<&str> {
unsafe {
let deltas = alpm_pkg_get_deltas(self as *const _ as _);
util::alpm_list_to_vec(deltas, |char_ptr| {
CStr::from_ptr(char_ptr as *const c_char).to_str().unwrap()
})
}
}
pub fn replaces<'a>(&'a self) -> Vec<Dependency<'a>> {
unsafe {
let deps = alpm_pkg_get_replaces(self as *const _ as _);
util::alpm_list_to_vec(deps, |dep| {
Dependency::new(dep as *const alpm_depend_t)
})
}
}
pub fn files<'a>(&'a self) -> FileList<'a> {
unsafe { FileList::from_raw(alpm_pkg_get_files(self as *const _ as _)) }
}
pub fn backup<'a>(&self) -> Vec<Backup<'a>> {
unsafe {
let backups = alpm_pkg_get_backup(self as *const _ as _);
util::alpm_list_to_vec(backups, |bkup| Backup {
name: CStr::from_ptr((*(bkup as *const alpm_backup_t)).name).to_str().unwrap(),
hash: CStr::from_ptr((*(bkup as *const alpm_backup_t)).hash).to_str().unwrap(),
})
}
}
pub fn base64_signature(&self) -> &str {
unsafe {
let char_ptr = alpm_pkg_get_packager(self as *const _ as _);
assert!(!char_ptr.is_null());
CStr::from_ptr(char_ptr).to_str().unwrap()
}
}
pub fn validation(&self) -> Validation {
unsafe { alpm_pkg_get_validation(self as *const _ as _).into() }
}
pub fn changelog(&self) {
unimplemented!()
}
pub fn mtree(&self) {
unimplemented!()
}
pub fn has_scriptlet(&self) -> bool {
unsafe { alpm_pkg_has_scriptlet(self as *const _ as _) != 0 }
}
pub fn download_size(&self) -> u64 {
unsafe { alpm_pkg_download_size(self as *const _ as _) as u64 }
}
pub fn unused_deltas(&self) -> Vec<&str> {
unsafe {
let deltas = alpm_pkg_get_licenses(self as *const _ as _);
util::alpm_list_to_vec(deltas, |char_ptr| {
CStr::from_ptr(char_ptr as *const c_char).to_str().unwrap()
})
}
}
pub fn set_reason(&self, r: Reason) -> bool {
unsafe { alpm_pkg_set_reason(self as *const _ as _, r.into()) == 0 }
}
pub fn sync_new_version<'a>(&self, dbs: Vec<Db<'a>>) -> Option<&'a PackageRef> {
unsafe {
let dbs = util::vec_to_alpm_list(dbs, |db| db.inner as *const libc::c_void);
let new_pkg_ptr = alpm_sync_newversion(self as *const _ as _, dbs);
if new_pkg_ptr.is_null() {
None
} else {
Some(PackageRef::new(new_pkg_ptr))
}
}
}
}
pub enum PackageOperation<'a> {
Install {
new_pkg: &'a PackageRef,
},
Upgrade {
old_pkg: &'a PackageRef,
new_pkg: &'a PackageRef,
},
Reinstall {
old_pkg: &'a PackageRef,
new_pkg: &'a PackageRef,
},
Downgrade {
old_pkg: &'a PackageRef,
new_pkg: &'a PackageRef,
},
Remove {
old_pkg: &'a PackageRef,
},
}
impl<'a> PackageOperation<'a> {
pub(crate) unsafe fn new<'b>(op: &alpm_event_package_operation_t) -> PackageOperation<'b> {
match op.operation {
ALPM_PACKAGE_INSTALL => PackageOperation::Install {
new_pkg: PackageRef::new(op.newpkg),
},
ALPM_PACKAGE_UPGRADE => PackageOperation::Upgrade {
new_pkg: PackageRef::new(op.newpkg),
old_pkg: PackageRef::new(op.oldpkg),
},
ALPM_PACKAGE_REINSTALL => PackageOperation::Reinstall {
new_pkg: PackageRef::new(op.newpkg),
old_pkg: PackageRef::new(op.oldpkg),
},
ALPM_PACKAGE_DOWNGRADE => PackageOperation::Downgrade {
new_pkg: PackageRef::new(op.newpkg),
old_pkg: PackageRef::new(op.oldpkg),
},
ALPM_PACKAGE_REMOVE => PackageOperation::Remove {
old_pkg: PackageRef::new(op.oldpkg),
},
_ => panic!("Unrecognised package operation"),
}
}
}
pub struct Group<'a> {
pub name: &'a str,
pub packages: Vec<&'a PackageRef>,
}
impl<'a> fmt::Debug for Group<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Group(\"{}\")", self.name)
}
}
impl<'a> Group<'a> {
pub(crate) unsafe fn new<'b>(name: *const c_char, packages: *const alpm_list_t) -> Group<'b> {
let name = CStr::from_ptr(name).to_str().unwrap();
let packages = util::alpm_list_to_vec(packages, |pkg_ptr| {
&*(pkg_ptr as *const PackageRef)
});
Group {
name: name,
packages: packages,
}
}
}
pub struct PackageVersion<'a>(*const c_char, PhantomData<&'a u8>);
impl<'b> PackageVersion<'b> {
fn new<'a>(p: *const c_char) -> PackageVersion<'a> {
PackageVersion(p, PhantomData)
}
}
impl<'a> AsRef<str> for PackageVersion<'a> {
fn as_ref(&self) -> &str {
unsafe { CStr::from_ptr(self.0).to_str().unwrap() }
}
}
impl<'a> fmt::Display for PackageVersion<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.as_ref())
}
}
impl<'a> PartialEq for PackageVersion<'a> {
fn eq(&self, other: &Self) -> bool {
unsafe {
CStr::from_ptr(self.0) == CStr::from_ptr(other.0)
}
}
}
impl<'a> Eq for PackageVersion<'a> { }
impl<'a> cmp::Ord for PackageVersion<'a> {
fn cmp(&self, other: &Self) -> cmp::Ordering {
let res = unsafe { alpm_pkg_vercmp(self.0, other.0) };
match res {
x if x > 0 => cmp::Ordering::Greater,
x if x < 0 => cmp::Ordering::Less,
_ => cmp::Ordering::Equal,
}
}
}
impl<'a> cmp::PartialOrd for PackageVersion<'a> {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(self.0.cmp(&other.0))
}
}
#[test]
fn test_cmp_pkg_version() {
use std::ffi::CString;
unsafe {
let less = CString::new("1.0").unwrap();
let greater = CString::new("1.1").unwrap();
let less_v = PackageVersion::new(less.as_ptr());
let greater_v = PackageVersion::new(greater.as_ptr());
assert!(less_v < greater_v);
assert!(!(less_v >= greater_v));
assert!(greater_v > less_v);
assert!(!(greater_v <= less_v));
assert!(greater_v <= greater_v);
assert!(!(greater_v > greater_v));
assert!(greater_v >= greater_v);
assert!(!(greater_v < greater_v));
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum PackageFrom {
File,
LocalDb,
SyncDb,
}
impl Into<u32> for PackageFrom {
fn into(self) -> u32 {
match self {
PackageFrom::File => ALPM_PKG_FROM_FILE,
PackageFrom::LocalDb => ALPM_PKG_FROM_LOCALDB,
PackageFrom::SyncDb => ALPM_PKG_FROM_SYNCDB,
}
}
}
impl From<u32> for PackageFrom {
fn from(f: u32) -> Self {
match f {
ALPM_PKG_FROM_FILE => PackageFrom::File,
ALPM_PKG_FROM_LOCALDB => PackageFrom::LocalDb,
ALPM_PKG_FROM_SYNCDB => PackageFrom::SyncDb,
_ => unreachable!()
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Reason {
Explicit,
Depend
}
impl Into<u32> for Reason {
fn into(self) -> u32 {
match self {
Reason::Explicit => ALPM_PKG_REASON_EXPLICIT,
Reason::Depend => ALPM_PKG_REASON_DEPEND,
}
}
}
impl From<u32> for Reason {
fn from(f: u32) -> Self {
match f {
ALPM_PKG_REASON_EXPLICIT => Reason::Explicit,
ALPM_PKG_REASON_DEPEND => Reason::Depend,
_ => unreachable!()
}
}
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub struct ValidationMethod {
pub md5sum: bool,
pub sha256sum: bool,
pub signature: bool,
}
impl Default for ValidationMethod {
fn default() -> ValidationMethod {
ValidationMethod {
md5sum: false,
sha256sum: false,
signature: false,
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Validation {
Unknown,
None,
Some(ValidationMethod)
}
impl Validation {
pub fn unknown() -> Validation {
Validation::Unknown
}
pub fn none() -> Validation {
Validation::None
}
pub fn md5sum() -> Validation {
Validation::Some(ValidationMethod { md5sum: true, ..Default::default() })
}
pub fn sha256sum() -> Validation {
Validation::Some(ValidationMethod { sha256sum: true, ..Default::default() })
}
pub fn signature() -> Validation {
Validation::Some(ValidationMethod { signature: true, ..Default::default() })
}
}
impl Into<u32> for Validation {
fn into(self) -> u32 {
match self {
Validation::Unknown => ALPM_PKG_VALIDATION_UNKNOWN,
Validation::None => ALPM_PKG_VALIDATION_NONE,
Validation::Some(methods) => {
let mut acc = 0;
if methods.md5sum {
acc |= ALPM_PKG_VALIDATION_MD5SUM;
};
if methods.sha256sum {
acc |= ALPM_PKG_VALIDATION_SHA256SUM;
};
if methods.signature {
acc |= ALPM_PKG_VALIDATION_SIGNATURE;
};
acc
}
}
}
}
impl From<u32> for Validation {
fn from(from: u32) -> Validation {
match from {
ALPM_PKG_VALIDATION_UNKNWON => Validation::Unknown,
ALPM_PKG_VALIDATION_NONE => Validation::None,
other => Validation::Some(ValidationMethod {
md5sum: from & ALPM_PKG_VALIDATION_MD5SUM != 0,
sha256sum: from & ALPM_PKG_VALIDATION_SHA256SUM != 0,
signature: from & ALPM_PKG_VALIDATION_SIGNATURE != 0,
}),
}
}
}
pub struct Dependency<'a> {
name: *const c_char,
version: PackageVersion<'a>,
description: *const c_char,
name_hash: c_ulong,
version_constraint_type: VersionConstraintType
}
impl<'a> fmt::Debug for Dependency<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Dependency(\"{}\")", self.name())
}
}
impl<'a> Dependency<'a> {
unsafe fn new<'b>(raw: *const alpm_depend_t) -> Dependency<'b> {
Dependency {
name: (*raw).name,
version: PackageVersion::new((*raw).version),
description: (*raw).desc,
name_hash: (*raw).name_hash,
version_constraint_type: (*raw).mod_.into()
}
}
pub fn name(&self) -> &str {
unsafe { CStr::from_ptr(self.name).to_str().unwrap() }
}
pub fn version(&self) -> &PackageVersion<'a> {
&self.version
}
pub fn description(&self) -> &str {
unsafe { CStr::from_ptr(self.description).to_str().unwrap() }
}
pub fn hash(&self) -> u64 {
self.name_hash as u64
}
pub fn version_constraint_type(&self) -> VersionConstraintType {
self.version_constraint_type
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum VersionConstraintType {
Any,
Equal,
GreaterOrEqual,
LessOrEqual,
Greater,
Less,
}
impl From<u32> for VersionConstraintType {
fn from(f: u32) -> VersionConstraintType {
match f {
ALPM_DEP_MOD_ANY => VersionConstraintType::Any,
ALPM_DEP_MOD_EQ => VersionConstraintType::Equal,
ALPM_DEP_MOD_GE => VersionConstraintType::GreaterOrEqual,
ALPM_DEP_MOD_LE => VersionConstraintType::LessOrEqual,
ALPM_DEP_MOD_GT => VersionConstraintType::Greater,
ALPM_DEP_MOD_LT => VersionConstraintType::Less,
_ => unreachable!()
}
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct FileList<'a> {
pub list: Vec<File<'a>>,
inner: *const alpm_filelist_t,
}
impl<'a> FileList<'a> {
pub(crate) unsafe fn from_raw<'b>(raw: *const alpm_filelist_t) -> FileList<'b> {
let count = (*raw).count;
let mut file_ptr = (*raw).files;
let mut files: Vec<File<'b>> = Vec::new();
for i in 0..count {
files.push(File::new(file_ptr));
file_ptr = file_ptr.offset(1);
}
FileList {
list: files,
inner: raw,
}
}
pub fn contains(&self, path: &str) -> Option<File<'a>> {
unsafe {
let path = CString::new(path).unwrap();
let file_ptr = alpm_filelist_contains(self.inner, path.as_ptr());
if file_ptr.is_null() {
None
} else {
Some(File::new(file_ptr))
}
}
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct File<'a> {
pub name: &'a str,
pub size: u64,
pub mode: u32,
}
impl<'a> File<'a> {
pub(crate) unsafe fn new<'b>(file_ptr: *const alpm_file_t) -> File<'b> {
File {
name: CStr::from_ptr((*file_ptr).name).to_str().unwrap(),
size: (*file_ptr).size as u64,
mode: (*file_ptr).mode,
}
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct Backup<'a> {
pub name: &'a str,
pub hash: &'a str,
}