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
use crate::{utils::*, LIBRARY, Library};
use crate::{Alpm, AlpmListMut, AsAlpmListItemPtr, AsPkg, Dep, IntoRawAlpmList};

use alpm_sys_ll::alpm_fileconflicttype_t::*;
use alpm_sys_ll::*;

use std::fmt;
use std::marker::PhantomData;
use std::mem::transmute;
use std::ptr::NonNull;

pub struct OwnedConflict {
    conflict: Conflict<'static>,
}

impl OwnedConflict {
    pub(crate) unsafe fn from_ptr(ptr: *mut alpm_conflict_t) -> OwnedConflict {
        OwnedConflict {
            conflict: Conflict::from_ptr(ptr),
        }
    }
}

impl fmt::Debug for OwnedConflict {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&self.conflict, f)
    }
}

pub struct Conflict<'a> {
    inner: NonNull<alpm_conflict_t>,
    _marker: PhantomData<&'a ()>,
}

impl<'a> fmt::Debug for Conflict<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Conflict")
            .field("package1", &self.package1())
            .field("package1_hash", &self.package1_hash())
            .field("package2", &self.package2())
            .field("package2_hash", &self.package2_hash())
            .field("reason", &self.reason())
            .finish()
    }
}

impl std::ops::Deref for OwnedConflict {
    type Target = Conflict<'static>;

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

impl Drop for OwnedConflict {
    fn drop(&mut self) {
        unsafe { LIBRARY.force_load().alpm_conflict_free(self.conflict.as_ptr()) }
    }
}

impl<'a> Conflict<'a> {
    pub(crate) unsafe fn from_ptr<'b>(ptr: *mut alpm_conflict_t) -> Conflict<'b> {
        Conflict {
            inner: NonNull::new_unchecked(ptr),
            _marker: PhantomData,
        }
    }

    pub(crate) fn as_ptr(&self) -> *mut alpm_conflict_t {
        self.inner.as_ptr()
    }

    pub fn package1_hash(&self) -> u64 {
        #[allow(clippy::useless_conversion)]
        unsafe {
            (*self.as_ptr()).package1_hash.into()
        }
    }

    pub fn package2_hash(&self) -> u64 {
        #[allow(clippy::useless_conversion)]
        unsafe {
            (*self.as_ptr()).package2_hash.into()
        }
    }

    pub fn package1(&self) -> &'a str {
        unsafe { from_cstr((*self.as_ptr()).package1) }
    }

    pub fn package2(&self) -> &'a str {
        unsafe { from_cstr((*self.as_ptr()).package2) }
    }

    pub fn reason(&self) -> Dep<'a> {
        unsafe { Dep::from_ptr((*self.as_ptr()).reason) }
    }
}

#[repr(u32)]
#[derive(Debug)]
pub enum FileConflictType {
    Target = ALPM_FILECONFLICT_TARGET as u32,
    Filesystem = ALPM_FILECONFLICT_FILESYSTEM as u32,
}

pub struct FileConflict<'a> {
    inner: NonNull<alpm_fileconflict_t>,
    _marker: PhantomData<&'a ()>,
}

impl<'a> fmt::Debug for FileConflict<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("FileConflict")
            .field("target", &self.target())
            .field("conflict_type", &self.conflict_type())
            .field("file", &self.file())
            .field("conflicting_target", &self.conflicting_target())
            .finish()
    }
}

impl std::ops::Deref for OwnedFileConflict {
    type Target = FileConflict<'static>;

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

pub struct OwnedFileConflict {
    pub(crate) inner: FileConflict<'static>,
}

impl fmt::Debug for OwnedFileConflict {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&self.inner, f)
    }
}

impl<'a> FileConflict<'a> {
    pub(crate) unsafe fn from_ptr<'b>(ptr: *mut alpm_fileconflict_t) -> FileConflict<'b> {
        FileConflict {
            inner: NonNull::new_unchecked(ptr),
            _marker: PhantomData,
        }
    }

    pub(crate) unsafe fn as_ptr(&self) -> *mut alpm_fileconflict_t {
        self.inner.as_ptr()
    }

    pub fn target(&self) -> &'a str {
        unsafe { from_cstr((*self.as_ptr()).target) }
    }

    pub fn conflict_type(&self) -> FileConflictType {
        let t = unsafe { (*self.as_ptr()).type_ };
        unsafe { transmute::<alpm_fileconflicttype_t, FileConflictType>(t) }
    }

    pub fn file(&self) -> &'a str {
        unsafe { from_cstr((*self.as_ptr()).file) }
    }

    // TODO: target is "" when empty. should be null instead.
    pub fn conflicting_target(&self) -> Option<&'a str> {
        let s = unsafe { from_cstr((*self.as_ptr()).target) };

        if s.is_empty() {
            None
        } else {
            Some(s)
        }
    }
}

impl Drop for OwnedFileConflict {
    fn drop(&mut self) {
        unsafe { LIBRARY.force_load().alpm_fileconflict_free(self.as_ptr()) }
    }
}

impl Alpm {
    pub fn check_conflicts<'a, P: 'a + AsPkg + AsAlpmListItemPtr<'a>, L: IntoRawAlpmList<'a, P>>(
        &self,
        list: L,
    ) -> AlpmListMut<OwnedConflict> {
        let list = unsafe { list.into_raw_alpm_list() };
        let ret = unsafe { self.lib.alpm_checkconflicts(self.as_ptr(), list.list()) };
        unsafe { AlpmListMut::from_parts(self, ret) }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::SigLevel;

    #[test]
    fn test_check_conflicts() {
        let handle = Alpm::new("/", "tests/db").unwrap();
        handle.register_syncdb("core", SigLevel::NONE).unwrap();
        handle.register_syncdb("extra", SigLevel::NONE).unwrap();
        handle.register_syncdb("community", SigLevel::NONE).unwrap();

        let i3 = handle.syncdbs().find_satisfier("i3-wm").unwrap();
        let i3gaps = handle.syncdbs().find_satisfier("i3-gaps").unwrap();
        let conflicts = handle.check_conflicts(vec![i3, i3gaps].iter());
        let conflict = conflicts.first().unwrap();
        assert_eq!(conflict.package1(), "i3-gaps");
        assert_eq!(conflict.package2(), "i3-wm");

        let xterm = handle.syncdbs().find_satisfier("xterm").unwrap();
        let systemd = handle.syncdbs().find_satisfier("systemd").unwrap();
        let conflicts = handle.check_conflicts(vec![xterm, systemd].iter());
        assert!(conflicts.is_empty());
    }
}