// package.c gets embedded bytes before cache lookup and preserves errno on success. // SPDX-License-Identifier: GPL-2.0-or-later module engine import os fn C.fstat(i32, &C.stat) i32 pub fn (mut handle Handle) package_signature_bytes(pkg &Package, length &uint) ![]u8 { handle.require_live()! if pkg == unsafe { nil } || length == unsafe { nil } { return handle.fail(.wrong_args, 'missing signature output') } if pkg.has_base64_sig { unsafe { *length = 0 } data := decode_signature(pkg.base64_sig) or { return handle.fail(.sig_invalid, err.msg()) } unsafe { *length = uint(data.len) } return data } mut path := pkg.filename // retain the V facade's direct-path support for archive-origin packages. if pkg.origin != .file { cached := handle.cached_file(pkg.filename)! if !cached.found { return handle.fail(.pkg_not_found, pkg.filename) } path = cached.path } sigpath := path + '.sig' if C.faccessat(C.AT_FDCWD, unsafe { &char(sigpath.str) }, C.R_OK, C.AT_SYMLINK_NOFOLLOW) != 0 { reason := os.last_error().msg() handle.log(.debug, '"${sigpath}" is not readable: ${reason}\n') return handle.fail(.sig_missing, reason) } mut file := os.open(sigpath) or { return handle.fail(.not_a_file, err.msg()) } defer { file.close() } mut info := C.stat{} if C.fstat(i32(file.fd), &info) != 0 { return handle.fail(.not_a_file, os.last_error().msg()) } unsafe { *length = uint(info.st_size) } if info.st_size <= 0 { return handle.fail(.system, 'empty signature file') } mut data := []u8{len: int(info.st_size)} mut offset := 0 for offset < data.len { count := file.read(mut data[offset..]) or { return handle.fail(.system, err.msg()) } if count <= 0 { return handle.fail(.system, 'short signature read') } offset += count } handle.log(.debug, 'found detached signature ${sigpath} with size ${data.len}\n') return data }