| 1 | // handle policy surrounds the raw reader, as in pacman be_package.c. |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | module engine |
| 4 | |
| 5 | import os |
| 6 | |
| 7 | fn C.access(&char, i32) i32 |
| 8 | |
| 9 | pub fn (mut handle Handle) load_package(filename string, full bool, level u32) !&Package { |
| 10 | handle.check()! |
| 11 | path := c_string_prefix(filename) |
| 12 | signature_readable := handle.package_readable(path + '.sig') |
| 13 | if path == '' { return handle.fail(.wrong_args, 'empty package path') } |
| 14 | if signature_readable && level & 1 != 0 { |
| 15 | handle.import_package_keys(path, full)! |
| 16 | } |
| 17 | validation := handle.validate_package(path, unsafe { nil }, level)! |
| 18 | mut pkg := handle.read_package_for_handle(path, full)! |
| 19 | pkg.validation = validation |
| 20 | return pkg |
| 21 | } |
| 22 | |
| 23 | fn (mut handle Handle) read_package_for_handle(path string, full bool) !&Package { |
| 24 | mut reader := open_handle_archive(path, handle, .pkg_open) or { |
| 25 | if err is ArchiveOpenError { |
| 26 | kind := match err.system_code { |
| 27 | 2 { Errno.pkg_not_found } |
| 28 | 13 { Errno.badperms } |
| 29 | else { Errno.pkg_open } |
| 30 | } |
| 31 | return handle.fail(kind, err.msg()) |
| 32 | } |
| 33 | return handle.fail(.pkg_open, err.msg()) |
| 34 | } |
| 35 | mut pkg := parse_package_archive(mut reader, path, reader.file_size, full, handle) or { |
| 36 | kind := if err is PackageArchiveError { err.kind } else { Errno.pkg_invalid } |
| 37 | problem := reader.close_error(err) |
| 38 | return handle.fail(kind, problem.msg()) |
| 39 | } |
| 40 | reader.close() or { return handle.fail(.libarchive, err.msg()) } |
| 41 | pkg.owner = handle |
| 42 | return &pkg |
| 43 | } |
| 44 | |
| 45 | fn (handle &Handle) package_readable(path string) bool { |
| 46 | if C.faccessat(C.AT_FDCWD, unsafe { &char(path.str) }, C.R_OK, C.AT_SYMLINK_NOFOLLOW) == 0 { |
| 47 | return true |
| 48 | } |
| 49 | problem := os.last_error() |
| 50 | handle.log(.debug, '"${path}" is not readable: ${problem.msg()}\n') |
| 51 | return false |
| 52 | } |
| 53 | |