// behavior referenced to pacman lib/libalpm/handle.c and util.c. // SPDX-License-Identifier: GPL-2.0-or-later module engine #include #include fn C.fnmatch(&char, &char, int) int struct C.passwd { pw_name &char pw_uid u32 pw_gid u32 pw_dir &char } fn C.getpwnam(&char) &C.passwd pub const sig_use_default = u32(1 << 30) pub enum StringListOption { cache_dirs hook_dirs no_upgrade no_extract ignore_packages ignore_groups overwrite_files architectures } pub enum StringOption { logfile gpgdir sandbox_user db_extension } pub enum IntegerOption { use_syslog check_space default_siglevel local_file_siglevel remote_file_siglevel disable_download_timeout parallel_downloads disable_sandbox_filesystem disable_sandbox_syscalls } struct Options { mut: lists map[StringListOption][]string strings map[StringOption]string integers map[IntegerOption]i64 assumed []Dependency } fn directory_option(value string) !string { if value == '' { return error('directory path is empty') } return if value.ends_with('/') { value } else { value + '/' } } pub fn (mut handle Handle) string_list(option StringListOption) ![]string { handle.check()! return handle.opts.lists[option].clone() } pub fn (mut handle Handle) add_string_list(option StringListOption, value string) ! { // handle.c's architecture append bypasses CHECK_HANDLE. if option == .architectures { handle.require_live()! } else { handle.check()! } text := c_string_prefix(value) item := if option in [.cache_dirs, .hook_dirs] { directory_option(text) or { return handle.fail(.wrong_args, err.msg()) } } else { text } handle.opts.lists[option] << item.clone() } pub fn (mut handle Handle) set_string_list(option StringListOption, values []string) ! { handle.check()! handle.opts.lists[option] = []string{} for item in values { handle.add_string_list(option, item)! } } pub fn (mut handle Handle) remove_string_list(option StringListOption, value string) !bool { handle.check()! text := c_string_prefix(value) item := if option in [.cache_dirs, .hook_dirs] { directory_option(text) or { return handle.fail(.wrong_args, err.msg()) } } else { text } mut values := handle.opts.lists[option].clone() index := values.index(item) if index < 0 { return false } values.delete(index) handle.opts.lists[option] = values.clone() return true } pub fn (mut handle Handle) string_option(option StringOption) !string { handle.check()! return handle.opts.strings[option] } pub fn (mut handle Handle) set_string_option(option StringOption, value string) ! { handle.check()! text := c_string_prefix(value) mut item := text if option == .gpgdir { item = directory_option(text) or { return handle.fail(.wrong_args, err.msg()) } handle.known_keys = map[string]bool{} handle.signature_initialized = false } if option == .sandbox_user { // handle.c discards the old user before lookup and leaves errno alone on failure. handle.opts.strings[.sandbox_user] = '' if C.getpwnam(unsafe { &char(text.str) }) == unsafe { nil } { handle.log(.debug, "'sandboxuser' (${text}) does not exist") return handle.error() } } if option == .logfile { handle.close_action_log() } handle.opts.strings[option] = item.clone() } pub fn (mut handle Handle) integer_option(option IntegerOption) !i64 { handle.check()! value := handle.opts.integers[option] if option in [.local_file_siglevel, .remote_file_siglevel] && u32(value) & sig_use_default != 0 { return handle.opts.integers[.default_siglevel] } return value } pub fn (mut handle Handle) set_integer_option(option IntegerOption, value i64) ! { handle.check()! if option == .parallel_downloads && (value < 1 || value > i64(0xffffffff)) { return handle.fail(.wrong_args, 'parallel downloads must be a positive unsigned integer') } if option == .default_siglevel && value == i64(sig_use_default) { return handle.fail(.wrong_args, 'the default signature level cannot inherit itself') } handle.opts.integers[option] = value } pub fn (mut handle Handle) set_disable_sandbox(value u16) ! { handle.set_integer_option(.disable_sandbox_filesystem, value)! handle.set_integer_option(.disable_sandbox_syscalls, value)! } pub fn (mut handle Handle) disable_sandbox() !int { handle.check()! filesystem := handle.opts.integers[.disable_sandbox_filesystem] != 0 syscalls := handle.opts.integers[.disable_sandbox_syscalls] != 0 return int(filesystem) + int(syscalls) } pub fn (mut handle Handle) add_assumed(dependency Dependency) ! { handle.check()! if dependency.mod !in [.any, .equal] { return handle.fail(.wrong_args, 'assumed dependencies require a name or equality version') } handle.opts.assumed << dependency.clone() } pub fn (mut handle Handle) set_assumed(dependencies []Dependency) ! { handle.check()! handle.opts.assumed = []Dependency{} for dependency in dependencies { handle.add_assumed(dependency)! } } pub fn (mut handle Handle) assumed() ![]Dependency { handle.check()! return handle.opts.assumed.clone() } pub fn (mut handle Handle) remove_assumed(dependency Dependency) !bool { handle.check()! for index, item in handle.opts.assumed { if item.name() == dependency.name() && item.version() == dependency.version() && (item.mod == .any) == (dependency.mod == .any) { handle.opts.assumed.delete(index) return true } } return false } // The last matching pattern wins. Backslash can escape a leading exclamation. pub fn match_patterns(patterns []string, path string) int { return match_pattern_list(patterns, path, false) } // Option setters and the hook parser store NUL-terminated strings. fn match_stored_patterns(patterns []string, path string) int { return match_pattern_list(patterns, path, true) } fn match_pattern_list(patterns []string, path string, terminated bool) int { for index := patterns.len - 1; index >= 0; index-- { text := patterns[index] pattern := if terminated { text } else { text.clone() } inverted := pattern.starts_with('!') start := if inverted || pattern.starts_with('\\') { 1 } else { 0 } if C.fnmatch(unsafe { &char(pattern.str + start) }, unsafe { &char(path.str) }, 0) == 0 { return if inverted { 1 } else { 0 } } } return -1 } pub fn (mut handle Handle) match_option(option StringListOption, path string) !int { handle.require_live()! return match_stored_patterns(handle.opts.lists[option], path) } pub fn (mut handle Handle) should_ignore(mut pkg Package) !bool { handle.require_live()! for pattern in handle.opts.lists[.ignore_packages] { if C.fnmatch(unsafe { &char(pattern.str) }, unsafe { &char(pkg.name.str) }, 0) == 0 { return true } } // package.c preserves errno on a name match; get_groups clears the package owner. if pkg.owner != unsafe { nil } { mut owner := pkg.owner owner.check()! } pkg.load_description() or { pkg.diagnostics << err.msg() } for group in pkg.groups { for pattern in handle.opts.lists[.ignore_groups] { if C.fnmatch(unsafe { &char(pattern.str) }, unsafe { &char(group.str) }, 0) == 0 { return true } } } return false }