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
use crate::utils::*;
use crate::Result;

use alpm_sys::*;

use std::ffi::CString;
use std::slice;

#[derive(Debug)]
pub struct File {
    inner: alpm_file_t,
}

impl File {
    pub fn name(&self) -> &str {
        unsafe { from_cstr(self.inner.name) }
    }

    pub fn size(&self) -> i64 {
        #[allow(clippy::identity_conversion)]
        self.inner.size.into()
    }

    pub fn mode(&self) -> u32 {
        self.inner.mode
    }
}

#[derive(Debug)]
pub struct FileList {
    pub(crate) inner: alpm_filelist_t,
}

impl FileList {
    pub fn files(&self) -> &[File] {
        unsafe { slice::from_raw_parts(self.inner.files as *const File, self.inner.count) }
    }

    pub fn contains<S: Into<String>>(&self, path: S) -> Result<Option<File>> {
        let path = CString::new(path.into()).unwrap();
        let file = unsafe {
            alpm_filelist_contains(
                &self.inner as *const alpm_filelist_t as *mut alpm_filelist_t,
                path.as_ptr(),
            )
        };

        if file.is_null() {
            Ok(None)
        } else {
            let file = unsafe { *file };
            Ok(Some(File { inner: file }))
        }
    }
}