// handle policy surrounds the raw reader, as in pacman be_package.c. // SPDX-License-Identifier: GPL-2.0-or-later module engine import os fn C.access(&char, i32) i32 pub fn (mut handle Handle) load_package(filename string, full bool, level u32) !&Package { handle.check()! path := c_string_prefix(filename) signature_readable := handle.package_readable(path + '.sig') if path == '' { return handle.fail(.wrong_args, 'empty package path') } if signature_readable && level & 1 != 0 { handle.import_package_keys(path, full)! } validation := handle.validate_package(path, unsafe { nil }, level)! mut pkg := handle.read_package_for_handle(path, full)! pkg.validation = validation return pkg } fn (mut handle Handle) read_package_for_handle(path string, full bool) !&Package { mut reader := open_handle_archive(path, handle, .pkg_open) or { if err is ArchiveOpenError { kind := match err.system_code { 2 { Errno.pkg_not_found } 13 { Errno.badperms } else { Errno.pkg_open } } return handle.fail(kind, err.msg()) } return handle.fail(.pkg_open, err.msg()) } mut pkg := parse_package_archive(mut reader, path, reader.file_size, full, handle) or { kind := if err is PackageArchiveError { err.kind } else { Errno.pkg_invalid } problem := reader.close_error(err) return handle.fail(kind, problem.msg()) } reader.close() or { return handle.fail(.libarchive, err.msg()) } pkg.owner = handle return &pkg } fn (handle &Handle) package_readable(path string) bool { if C.faccessat(C.AT_FDCWD, unsafe { &char(path.str) }, C.R_OK, C.AT_SYMLINK_NOFOLLOW) == 0 { return true } problem := os.last_error() handle.log(.debug, '"${path}" is not readable: ${problem.msg()}\n') return false }