nspkg / engine / package_signature.v
50 lines · 47 sloc · 1.82 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1// package.c gets embedded bytes before cache lookup and preserves errno on success.
2// SPDX-License-Identifier: GPL-2.0-or-later
3module engine
4
5import os
6
7fn C.fstat(i32, &C.stat) i32
8
9pub fn (mut handle Handle) package_signature_bytes(pkg &Package, length &uint) ![]u8 {
10 handle.require_live()!
11 if pkg == unsafe { nil } || length == unsafe { nil } {
12 return handle.fail(.wrong_args, 'missing signature output')
13 }
14 if pkg.has_base64_sig {
15 unsafe { *length = 0 }
16 data := decode_signature(pkg.base64_sig) or { return handle.fail(.sig_invalid, err.msg()) }
17 unsafe { *length = uint(data.len) }
18 return data
19 }
20 mut path := pkg.filename
21 // retain the V facade's direct-path support for archive-origin packages.
22 if pkg.origin != .file {
23 cached := handle.cached_file(pkg.filename)!
24 if !cached.found { return handle.fail(.pkg_not_found, pkg.filename) }
25 path = cached.path
26 }
27 sigpath := path + '.sig'
28 if C.faccessat(C.AT_FDCWD, unsafe { &char(sigpath.str) }, C.R_OK, C.AT_SYMLINK_NOFOLLOW) != 0 {
29 reason := os.last_error().msg()
30 handle.log(.debug, '"${sigpath}" is not readable: ${reason}\n')
31 return handle.fail(.sig_missing, reason)
32 }
33 mut file := os.open(sigpath) or { return handle.fail(.not_a_file, err.msg()) }
34 defer { file.close() }
35 mut info := C.stat{}
36 if C.fstat(i32(file.fd), &info) != 0 {
37 return handle.fail(.not_a_file, os.last_error().msg())
38 }
39 unsafe { *length = uint(info.st_size) }
40 if info.st_size <= 0 { return handle.fail(.system, 'empty signature file') }
41 mut data := []u8{len: int(info.st_size)}
42 mut offset := 0
43 for offset < data.len {
44 count := file.read(mut data[offset..]) or { return handle.fail(.system, err.msg()) }
45 if count <= 0 { return handle.fail(.system, 'short signature read') }
46 offset += count
47 }
48 handle.log(.debug, 'found detached signature ${sigpath} with size ${data.len}\n')
49 return data
50}
51