nspkg / engine / package_load.v
52 lines · 47 sloc · 1.69 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1// handle policy surrounds the raw reader, as in pacman be_package.c.
2// SPDX-License-Identifier: GPL-2.0-or-later
3module engine
4
5import os
6
7fn C.access(&char, i32) i32
8
9pub 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
23fn (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
45fn (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