nspkg / engine / options.v
240 lines · 211 sloc · 6.92 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1// behavior referenced to pacman lib/libalpm/handle.c and util.c.
2// SPDX-License-Identifier: GPL-2.0-or-later
3module engine
4
5#include <fnmatch.h>
6#include <pwd.h>
7
8fn C.fnmatch(&char, &char, int) int
9
10struct C.passwd {
11 pw_name &char
12 pw_uid u32
13 pw_gid u32
14 pw_dir &char
15}
16
17fn C.getpwnam(&char) &C.passwd
18
19pub const sig_use_default = u32(1 << 30)
20
21pub enum StringListOption {
22 cache_dirs
23 hook_dirs
24 no_upgrade
25 no_extract
26 ignore_packages
27 ignore_groups
28 overwrite_files
29 architectures
30}
31
32pub enum StringOption {
33 logfile
34 gpgdir
35 sandbox_user
36 db_extension
37}
38
39pub enum IntegerOption {
40 use_syslog
41 check_space
42 default_siglevel
43 local_file_siglevel
44 remote_file_siglevel
45 disable_download_timeout
46 parallel_downloads
47 disable_sandbox_filesystem
48 disable_sandbox_syscalls
49}
50
51struct Options {
52mut:
53 lists map[StringListOption][]string
54 strings map[StringOption]string
55 integers map[IntegerOption]i64
56 assumed []Dependency
57}
58
59fn directory_option(value string) !string {
60 if value == '' { return error('directory path is empty') }
61 return if value.ends_with('/') { value } else { value + '/' }
62}
63
64pub fn (mut handle Handle) string_list(option StringListOption) ![]string {
65 handle.check()!
66 return handle.opts.lists[option].clone()
67}
68
69pub fn (mut handle Handle) add_string_list(option StringListOption, value string) ! {
70 // handle.c's architecture append bypasses CHECK_HANDLE.
71 if option == .architectures { handle.require_live()! } else { handle.check()! }
72 text := c_string_prefix(value)
73 item := if option in [.cache_dirs, .hook_dirs] {
74 directory_option(text) or { return handle.fail(.wrong_args, err.msg()) }
75 } else {
76 text
77 }
78 handle.opts.lists[option] << item.clone()
79}
80
81pub fn (mut handle Handle) set_string_list(option StringListOption, values []string) ! {
82 handle.check()!
83 handle.opts.lists[option] = []string{}
84 for item in values { handle.add_string_list(option, item)! }
85}
86
87pub fn (mut handle Handle) remove_string_list(option StringListOption, value string) !bool {
88 handle.check()!
89 text := c_string_prefix(value)
90 item := if option in [.cache_dirs, .hook_dirs] {
91 directory_option(text) or { return handle.fail(.wrong_args, err.msg()) }
92 } else {
93 text
94 }
95 mut values := handle.opts.lists[option].clone()
96 index := values.index(item)
97 if index < 0 { return false }
98 values.delete(index)
99 handle.opts.lists[option] = values.clone()
100 return true
101}
102
103pub fn (mut handle Handle) string_option(option StringOption) !string {
104 handle.check()!
105 return handle.opts.strings[option]
106}
107
108pub fn (mut handle Handle) set_string_option(option StringOption, value string) ! {
109 handle.check()!
110 text := c_string_prefix(value)
111 mut item := text
112 if option == .gpgdir {
113 item = directory_option(text) or { return handle.fail(.wrong_args, err.msg()) }
114 handle.known_keys = map[string]bool{}
115 handle.signature_initialized = false
116 }
117 if option == .sandbox_user {
118 // handle.c discards the old user before lookup and leaves errno alone on failure.
119 handle.opts.strings[.sandbox_user] = ''
120 if C.getpwnam(unsafe { &char(text.str) }) == unsafe { nil } {
121 handle.log(.debug, "'sandboxuser' (${text}) does not exist")
122 return handle.error()
123 }
124 }
125 if option == .logfile { handle.close_action_log() }
126 handle.opts.strings[option] = item.clone()
127}
128
129pub fn (mut handle Handle) integer_option(option IntegerOption) !i64 {
130 handle.check()!
131 value := handle.opts.integers[option]
132 if option in [.local_file_siglevel, .remote_file_siglevel] && u32(value) & sig_use_default != 0 {
133 return handle.opts.integers[.default_siglevel]
134 }
135 return value
136}
137
138pub fn (mut handle Handle) set_integer_option(option IntegerOption, value i64) ! {
139 handle.check()!
140 if option == .parallel_downloads && (value < 1 || value > i64(0xffffffff)) {
141 return handle.fail(.wrong_args, 'parallel downloads must be a positive unsigned integer')
142 }
143 if option == .default_siglevel && value == i64(sig_use_default) {
144 return handle.fail(.wrong_args, 'the default signature level cannot inherit itself')
145 }
146 handle.opts.integers[option] = value
147}
148
149pub fn (mut handle Handle) set_disable_sandbox(value u16) ! {
150 handle.set_integer_option(.disable_sandbox_filesystem, value)!
151 handle.set_integer_option(.disable_sandbox_syscalls, value)!
152}
153
154pub fn (mut handle Handle) disable_sandbox() !int {
155 handle.check()!
156 filesystem := handle.opts.integers[.disable_sandbox_filesystem] != 0
157 syscalls := handle.opts.integers[.disable_sandbox_syscalls] != 0
158 return int(filesystem) + int(syscalls)
159}
160
161pub fn (mut handle Handle) add_assumed(dependency Dependency) ! {
162 handle.check()!
163 if dependency.mod !in [.any, .equal] {
164 return handle.fail(.wrong_args, 'assumed dependencies require a name or equality version')
165 }
166 handle.opts.assumed << dependency.clone()
167}
168
169pub fn (mut handle Handle) set_assumed(dependencies []Dependency) ! {
170 handle.check()!
171 handle.opts.assumed = []Dependency{}
172 for dependency in dependencies { handle.add_assumed(dependency)! }
173}
174
175pub fn (mut handle Handle) assumed() ![]Dependency {
176 handle.check()!
177 return handle.opts.assumed.clone()
178}
179
180pub fn (mut handle Handle) remove_assumed(dependency Dependency) !bool {
181 handle.check()!
182 for index, item in handle.opts.assumed {
183 if item.name() == dependency.name() && item.version() == dependency.version() && (item.mod == .any) == (dependency.mod == .any) {
184 handle.opts.assumed.delete(index)
185 return true
186 }
187 }
188 return false
189}
190
191// The last matching pattern wins. Backslash can escape a leading exclamation.
192pub fn match_patterns(patterns []string, path string) int {
193 return match_pattern_list(patterns, path, false)
194}
195
196// Option setters and the hook parser store NUL-terminated strings.
197fn match_stored_patterns(patterns []string, path string) int {
198 return match_pattern_list(patterns, path, true)
199}
200
201fn match_pattern_list(patterns []string, path string, terminated bool) int {
202 for index := patterns.len - 1; index >= 0; index-- {
203 text := patterns[index]
204 pattern := if terminated { text } else { text.clone() }
205 inverted := pattern.starts_with('!')
206 start := if inverted || pattern.starts_with('\\') { 1 } else { 0 }
207 if C.fnmatch(unsafe { &char(pattern.str + start) }, unsafe { &char(path.str) }, 0) == 0 {
208 return if inverted { 1 } else { 0 }
209 }
210 }
211 return -1
212}
213
214pub fn (mut handle Handle) match_option(option StringListOption, path string) !int {
215 handle.require_live()!
216 return match_stored_patterns(handle.opts.lists[option], path)
217}
218
219pub fn (mut handle Handle) should_ignore(mut pkg Package) !bool {
220 handle.require_live()!
221 for pattern in handle.opts.lists[.ignore_packages] {
222 if C.fnmatch(unsafe { &char(pattern.str) }, unsafe { &char(pkg.name.str) }, 0) == 0 {
223 return true
224 }
225 }
226 // package.c preserves errno on a name match; get_groups clears the package owner.
227 if pkg.owner != unsafe { nil } {
228 mut owner := pkg.owner
229 owner.check()!
230 }
231 pkg.load_description() or { pkg.diagnostics << err.msg() }
232 for group in pkg.groups {
233 for pattern in handle.opts.lists[.ignore_groups] {
234 if C.fnmatch(unsafe { &char(pattern.str) }, unsafe { &char(group.str) }, 0) == 0 {
235 return true
236 }
237 }
238 }
239 return false
240}
241