nspkg / engine / file_conflict.v
269 lines · 255 sloc · 9.6 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1// target intersection, filesystem ownership and transfer skips follow conflict.c/filelist.c.
2// SPDX-License-Identifier: GPL-2.0-or-later
3module engine
4
5pub enum FileConflictKind {
6 target = 1
7 filesystem
8}
9
10pub struct FileConflict {
11pub:
12 target string
13 kind FileConflictKind
14 file string
15 conflicting_target string
16}
17
18pub struct FileConflictResult {
19pub:
20 success bool
21 conflicts []FileConflict
22}
23
24fn file_path_compare(first string, second string) int {
25 // File and directory spellings of the same path intersect.
26 difference := first.len - second.len
27 if difference == 1 && first.ends_with('/') && first.starts_with(second) {
28 return 0
29 }
30 if difference == -1 && second.ends_with('/') && second.starts_with(first) {
31 return 0
32 }
33 return first.compare(second)
34}
35
36fn file_names_sorted(files []PackageFile) bool {
37 for i in 1 .. files.len {
38 if files[i].name < files[i - 1].name { return false }
39 }
40 return true
41}
42
43fn next_file_index(files []PackageFile, index int, other string) int {
44 next := index + 1
45 // Stay with the linear merge for interleaved lists, and skip long sorted runs.
46 if files.len - next <= 8 { return next }
47 probe := next + 8
48 // A directory can also match its basename before punctuation intervenes in sort order.
49 bound := if other.ends_with('/') { other.substr_unsafe(0, other.len - 1) } else { other }
50 if files[probe].name >= bound { return next }
51 if files.last().name < bound { return files.len }
52 mut low := probe + 1
53 mut high := files.len
54 for low < high {
55 middle := low + (high - low) / 2
56 if files[middle].name < bound { low = middle + 1 } else { high = middle }
57 }
58 return low
59}
60
61fn (handle &Handle) target_file_conflicts(first &Package, second &Package, sorted bool, mut conflicts []FileConflict) {
62 mut a := 0
63 mut b := 0
64 for a < first.files.len && b < second.files.len {
65 path := first.files[a].name
66 other := second.files[b].name
67 cmp := file_path_compare(path, other)
68 if cmp < 0 {
69 a = if sorted { next_file_index(first.files, a, other) } else { a + 1 }
70 } else if cmp > 0 {
71 b = if sorted { next_file_index(second.files, b, path) } else { b + 1 }
72 } else {
73 if !path.ends_with('/') || !other.ends_with('/') {
74 full := handle.root + path
75 if !handle.can_overwrite_file(path, full) || !package_has_file(second, path) {
76 conflicts << FileConflict{ target: first.name, kind: .target, file: full, conflicting_target: second.name }
77 }
78 }
79 a++
80 b++
81 }
82 }
83}
84
85fn package_has_file(pkg &Package, path string) bool {
86 return pkg != unsafe { nil } && pkg.find_file(path) != none
87}
88
89fn (handle &Handle) can_overwrite_file(path string, rooted_path string) bool {
90 patterns := handle.opts.lists[.overwrite_files]
91 return match_stored_patterns(patterns, path) == 0 || match_stored_patterns(patterns, rooted_path) == 0
92}
93
94struct FileDirectoryIdentity {
95 device u64
96 inode u64
97}
98
99fn (mut root RootFilesystem) directory_owned_by(path string, owners []&Package, ancestors []FileDirectoryIdentity) bool {
100 mut parent := root.resolve(path, true) or { return false }
101 defer { parent.close() }
102 fd := C.openat(parent.fd, unsafe { &char(parent.name.str) }, root_directory_flags())
103 if fd < 0 { return false }
104 defer { root.owner.close_process_descriptor(fd) }
105 mut directory_info := C.v_alpm_root_info{}
106 if C.v_alpm_root_stat(fd, c'.', &directory_info) != 0 { return false }
107 identity := FileDirectoryIdentity{ device: directory_info.device, inode: directory_info.inode }
108 if identity in ancestors { return false }
109 mut visited := ancestors.clone()
110 visited << identity
111 names := root.directory_names(fd, 0) or { return false }
112 for name in names {
113 mut info := C.v_alpm_root_info{}
114 if C.v_alpm_root_stat(fd, unsafe { &char(name.str) }, &info) != 0 { return false }
115 is_dir := info.mode & 0o170000 == 0o040000
116 entry := path + name + if is_dir { '/' } else { '' }
117 if !owners.any(package_has_file(it, entry)) { return false }
118 if is_dir && !root.directory_owned_by(entry, owners, visited) { return false }
119 }
120 return true
121}
122
123fn (mut handle Handle) find_file_conflicts(mut root RootFilesystem, mut trans Transaction) ![]FileConflict {
124 mut installed := handle.local.package_cache()!
125 mut installed_by_name := map[string]&Package{}
126 for pkg in installed {
127 if pkg.name !in installed_by_name { installed_by_name[pkg.name] = pkg }
128 }
129 mut owners_loaded := false
130 mut conflicts := []FileConflict{}
131 // Callbacks can reorder public file arrays between package pairs.
132 callbacks_can_edit := handle.has_log_callback() || voidptr(handle.commit_progress_callback) != unsafe { nil }
133 mut sorted_targets := false
134 for current, first in trans.add {
135 handle.commit_progress_for(.file_conflicts, '', (current * 100) / trans.add.len, trans.add.len, current)
136 if current + 1 < trans.add.len && (current == 0 || callbacks_can_edit) {
137 sorted_targets = trans.add[current..].all(file_names_sorted(it.files))
138 }
139 for second in trans.add[current + 1..] {
140 handle.target_file_conflicts(first, second, sorted_targets, mut conflicts)
141 }
142 old := installed_by_name[first.name] or { unsafe { nil } }
143 if old != unsafe { nil } {
144 mut previous := unsafe { old }
145 previous.load_files() or { return handle.fail(.db_invalid, err.msg()) }
146 }
147 mut skip_directory := ''
148 mut old_index := 0
149 // Loading another installed package may run a log callback inside this loop.
150 ordered_old := old != unsafe { nil } && !handle.has_log_callback() && file_names_sorted(old.files)
151 for file in first.files {
152 if ordered_old {
153 // Walk unchanged runs together, including after inserts or removals.
154 for old_index < old.files.len && old.files[old_index].name < file.name {
155 old_index = next_file_index(old.files, old_index, file.name)
156 }
157 if old_index < old.files.len && old.files[old_index].name == file.name {
158 old_index++
159 continue
160 }
161 }
162 // Duplicate or out-of-order input can refer to an earlier entry.
163 if package_has_file(old, file.name) { continue }
164 if skip_directory != '' && file.name.starts_with(skip_directory) { continue }
165 skip_directory = ''
166 status := root.stat(file.name, false) or { return handle.fail(.system, err.msg()) }
167 if !status.present { continue }
168 is_directory := file.name.ends_with('/')
169 if is_directory && status.info.mode & 0o170000 == 0o040000 { continue }
170 relative := if is_directory { file.name[..file.name.len - 1] } else { file.name }
171 full := handle.root + relative
172 mut resolved := is_directory && package_has_file(old, relative)
173 if !resolved { resolved = trans.remove.any(package_has_file(it, relative)) }
174 if !resolved {
175 for second in trans.add {
176 if voidptr(first) == voidptr(second) { continue }
177 owner := installed_by_name[second.name] or { continue }
178 if package_has_file(owner, relative) {
179 trans.skip_remove << relative
180 resolved = true
181 break
182 }
183 }
184 }
185 if resolved && is_directory { skip_directory = file.name }
186 if !resolved && !owners_loaded {
187 for mut pkg in installed {
188 pkg.load_files() or { return handle.fail(.db_invalid, err.msg()) }
189 }
190 owners_loaded = true
191 }
192 if !resolved && status.info.mode & 0o170000 == 0o040000 {
193 directory := relative + '/'
194 owners := installed.filter(package_has_file(it, directory))
195 if owners.len > 0 && owners.all(it.name == first.name || find_package(trans.remove, it.name) != none) {
196 resolved = root.directory_owned_by(directory, owners, [])
197 }
198 }
199 if !resolved {
200 if _ := package_backup(first, relative) {
201 resolved = !installed.any(package_has_file(it, relative))
202 }
203 }
204 if !resolved && status.info.mode & 0o170000 != 0o040000 {
205 resolved = handle.can_overwrite_file(file.name, full)
206 }
207 if !resolved {
208 mut owner_name := ''
209 for owner in installed {
210 if package_has_file(owner, relative) {
211 owner_name = owner.name
212 break
213 }
214 }
215 conflicts << FileConflict{
216 target: first.name
217 kind: .filesystem
218 file: full
219 conflicting_target: owner_name
220 }
221 }
222 }
223 }
224 handle.commit_progress_for(.file_conflicts, '', 100, trans.add.len, trans.add.len)
225 return conflicts
226}
227
228pub fn (mut handle Handle) check_file_conflicts() !FileConflictResult {
229 mut trans := handle.current_transaction()!
230 if trans.state !in [.prepared, .downloading] { return handle.fail(.trans_not_prepared, '') }
231 if trans.loading_packages {
232 return handle.fail(.trans_not_null, 'transaction archive loading is active')
233 }
234 if trans.checking_files { return handle.fail(.trans_not_null, 'file-conflict check is active') }
235 trans.checking_files = true
236 was_executing := handle.execution_active
237 handle.execution_active = true
238 defer {
239 trans.checking_files = false
240 handle.execution_active = was_executing
241 }
242 trans.file_conflicts.clear()
243 if trans.flags.has(.db_only) || trans.add.len == 0 {
244 return FileConflictResult{ success: true }
245 }
246 for pkg in trans.add {
247 if pkg.origin != .file || !pkg.files_loaded {
248 return handle.fail(.wrong_args, 'file-conflict checks require fully loaded package archives')
249 }
250 for file in pkg.files {
251 validate_package_path(file.name) or { return handle.fail(.pkg_invalid, err.msg()) }
252 }
253 }
254 mut root := handle.open_root_filesystem()!
255 defer { root.close() }
256 handle.commit_event(CommitEvent{ kind: .file_conflicts_start })
257 trans.file_conflicts = handle.find_file_conflicts(mut root, mut trans)!
258 if trans.file_conflicts.len > 0 {
259 handle.fail(.file_conflicts, '')
260 return FileConflictResult{ conflicts: trans.file_conflicts.clone() }
261 }
262 handle.commit_event(CommitEvent{ kind: .file_conflicts_done })
263 return FileConflictResult{ success: true }
264}
265
266pub fn (mut handle Handle) transaction_file_conflicts() ![]FileConflict {
267 trans := handle.current_transaction()!
268 return trans.file_conflicts.clone()
269}
270