2022-01-19 18:54:33 +01:00
|
|
|
module package
|
2022-01-13 19:20:14 +01:00
|
|
|
|
2022-01-14 22:20:58 +01:00
|
|
|
import os
|
2022-01-18 21:41:09 +01:00
|
|
|
import util
|
2022-01-13 19:20:14 +01:00
|
|
|
|
2022-01-14 22:20:58 +01:00
|
|
|
// Represents a read archive
|
2022-06-22 20:17:11 +02:00
|
|
|
pub struct Pkg {
|
2022-01-13 21:47:14 +01:00
|
|
|
pub:
|
2022-01-31 22:59:12 +01:00
|
|
|
path string [required]
|
|
|
|
info PkgInfo [required]
|
|
|
|
files []string [required]
|
|
|
|
compression int [required]
|
2022-01-13 21:47:14 +01:00
|
|
|
}
|
|
|
|
|
2022-01-14 22:20:58 +01:00
|
|
|
// Represents the contents of a .PKGINFO file
|
2022-01-13 19:20:14 +01:00
|
|
|
struct PkgInfo {
|
2022-01-19 18:54:33 +01:00
|
|
|
pub mut:
|
2022-01-13 19:20:14 +01:00
|
|
|
// Single values
|
2022-01-13 21:47:14 +01:00
|
|
|
name string
|
|
|
|
base string
|
|
|
|
version string
|
2022-01-13 19:20:14 +01:00
|
|
|
description string
|
2022-01-13 21:47:14 +01:00
|
|
|
size i64
|
|
|
|
csize i64
|
|
|
|
url string
|
|
|
|
arch string
|
|
|
|
build_date i64
|
|
|
|
packager string
|
2022-01-19 17:22:36 +01:00
|
|
|
// md5sum string
|
|
|
|
// sha256sum string
|
|
|
|
pgpsig string
|
|
|
|
pgpsigsize i64
|
2022-01-13 19:20:14 +01:00
|
|
|
// Array values
|
2022-01-13 21:47:14 +01:00
|
|
|
groups []string
|
|
|
|
licenses []string
|
|
|
|
replaces []string
|
|
|
|
depends []string
|
|
|
|
conflicts []string
|
|
|
|
provides []string
|
|
|
|
optdepends []string
|
|
|
|
makedepends []string
|
2022-01-13 19:20:14 +01:00
|
|
|
checkdepends []string
|
|
|
|
}
|
|
|
|
|
2022-06-15 22:15:11 +02:00
|
|
|
// checksum calculates the sha256 hash of the package
|
2022-11-01 21:10:45 +01:00
|
|
|
pub fn (p &Pkg) checksum() !string {
|
2022-01-19 17:22:36 +01:00
|
|
|
return util.hash_file(p.path)
|
|
|
|
}
|
|
|
|
|
2022-01-14 22:46:04 +01:00
|
|
|
// parse_pkg_info_string parses a PkgInfo object from a string
|
2022-11-01 21:10:45 +01:00
|
|
|
fn parse_pkg_info_string(pkg_info_str &string) !PkgInfo {
|
2022-01-13 19:20:14 +01:00
|
|
|
mut pkg_info := PkgInfo{}
|
|
|
|
|
|
|
|
// Iterate over the entire string
|
|
|
|
for line in pkg_info_str.split_into_lines() {
|
|
|
|
// Skip any comment lines
|
|
|
|
if line.starts_with('#') {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
parts := line.split_nth('=', 2)
|
|
|
|
|
|
|
|
if parts.len < 2 {
|
|
|
|
return error('Invalid line detected.')
|
|
|
|
}
|
|
|
|
|
|
|
|
value := parts[1].trim_space()
|
|
|
|
key := parts[0].trim_space()
|
|
|
|
|
|
|
|
match key {
|
|
|
|
// Single values
|
|
|
|
'pkgname' { pkg_info.name = value }
|
|
|
|
'pkgbase' { pkg_info.base = value }
|
|
|
|
'pkgver' { pkg_info.version = value }
|
|
|
|
'pkgdesc' { pkg_info.description = value }
|
|
|
|
'size' { pkg_info.size = value.int() }
|
|
|
|
'url' { pkg_info.url = value }
|
|
|
|
'arch' { pkg_info.arch = value }
|
|
|
|
'builddate' { pkg_info.build_date = value.int() }
|
|
|
|
'packager' { pkg_info.packager = value }
|
|
|
|
'pgpsig' { pkg_info.pgpsig = value }
|
|
|
|
'pgpsigsize' { pkg_info.pgpsigsize = value.int() }
|
|
|
|
// Array values
|
|
|
|
'group' { pkg_info.groups << value }
|
|
|
|
'license' { pkg_info.licenses << value }
|
|
|
|
'replaces' { pkg_info.replaces << value }
|
|
|
|
'depend' { pkg_info.depends << value }
|
|
|
|
'conflict' { pkg_info.conflicts << value }
|
|
|
|
'provides' { pkg_info.provides << value }
|
|
|
|
'optdepend' { pkg_info.optdepends << value }
|
|
|
|
'makedepend' { pkg_info.makedepends << value }
|
|
|
|
'checkdepend' { pkg_info.checkdepends << value }
|
2022-03-27 15:10:45 +02:00
|
|
|
// There's no real point in trying to exactly manage which fields
|
|
|
|
// are allowed, so we just ignore any we don't explicitely need for
|
|
|
|
// in the db file
|
|
|
|
else { continue }
|
2022-01-13 19:20:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pkg_info
|
|
|
|
}
|
2022-01-13 21:47:14 +01:00
|
|
|
|
2022-03-27 19:54:49 +02:00
|
|
|
// read_pkg_archive extracts the file list & .PKGINFO contents from an archive
|
2022-04-07 21:29:08 +02:00
|
|
|
// NOTE: this command only supports zstd-, xz- & gzip-compressed tarballs.
|
2022-11-01 21:10:45 +01:00
|
|
|
pub fn read_pkg_archive(pkg_path string) !Pkg {
|
2022-01-14 22:20:58 +01:00
|
|
|
if !os.is_file(pkg_path) {
|
|
|
|
return error("'$pkg_path' doesn't exist or isn't a file.")
|
|
|
|
}
|
|
|
|
|
|
|
|
a := C.archive_read_new()
|
|
|
|
entry := C.archive_entry_new()
|
|
|
|
|
|
|
|
// Sinds 2020, all newly built Arch packages use zstd
|
|
|
|
C.archive_read_support_filter_zstd(a)
|
2022-01-30 14:13:37 +01:00
|
|
|
C.archive_read_support_filter_gzip(a)
|
2022-04-07 21:29:08 +02:00
|
|
|
C.archive_read_support_filter_xz(a)
|
2022-01-14 22:20:58 +01:00
|
|
|
// The content should always be a tarball
|
|
|
|
C.archive_read_support_format_tar(a)
|
|
|
|
|
|
|
|
// TODO find out where does this 10240 come from
|
2022-01-30 23:54:05 +01:00
|
|
|
r := C.archive_read_open_filename(a, &char(pkg_path.str), 10240)
|
2022-01-14 22:20:58 +01:00
|
|
|
|
|
|
|
if r != C.ARCHIVE_OK {
|
|
|
|
return error('Failed to open package.')
|
|
|
|
}
|
|
|
|
|
2022-01-30 14:13:37 +01:00
|
|
|
defer {
|
|
|
|
C.archive_read_free(a)
|
|
|
|
}
|
|
|
|
|
2022-01-31 22:59:12 +01:00
|
|
|
// 0: no compression (just a tarball)
|
|
|
|
// 1: gzip
|
|
|
|
// 14: zstd
|
|
|
|
compression_code := C.archive_filter_code(a, 0)
|
|
|
|
|
2022-01-14 22:20:58 +01:00
|
|
|
mut files := []string{}
|
2022-01-20 19:39:38 +01:00
|
|
|
mut pkg_info := PkgInfo{}
|
2022-01-14 22:46:04 +01:00
|
|
|
|
2022-01-14 22:20:58 +01:00
|
|
|
for C.archive_read_next_header(a, &entry) == C.ARCHIVE_OK {
|
|
|
|
pathname := C.archive_entry_pathname(entry)
|
|
|
|
|
|
|
|
ignored_names := [c'.BUILDINFO', c'.INSTALL', c'.MTREE', c'.PKGINFO', c'.CHANGELOG']
|
|
|
|
if ignored_names.all(C.strcmp(it, pathname) != 0) {
|
|
|
|
unsafe {
|
|
|
|
files << cstring_to_vstring(pathname)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if C.strcmp(pathname, c'.PKGINFO') == 0 {
|
|
|
|
size := C.archive_entry_size(entry)
|
|
|
|
|
|
|
|
// TODO can this unsafe block be avoided?
|
2022-01-30 23:54:05 +01:00
|
|
|
buf := unsafe { malloc(size) }
|
2022-01-27 19:18:11 +01:00
|
|
|
defer {
|
|
|
|
unsafe {
|
|
|
|
free(buf)
|
|
|
|
}
|
|
|
|
}
|
2022-01-20 19:39:38 +01:00
|
|
|
C.archive_read_data(a, buf, size)
|
|
|
|
|
2022-01-30 23:54:05 +01:00
|
|
|
pkg_text := unsafe { buf.vstring_with_len(size).clone() }
|
2022-01-30 21:03:32 +01:00
|
|
|
|
2022-11-01 21:10:45 +01:00
|
|
|
pkg_info = parse_pkg_info_string(pkg_text)!
|
2022-01-14 22:20:58 +01:00
|
|
|
} else {
|
|
|
|
C.archive_read_data_skip(a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-18 20:27:00 +01:00
|
|
|
pkg_info.csize = i64(os.file_size(pkg_path))
|
2022-01-13 21:47:14 +01:00
|
|
|
|
|
|
|
return Pkg{
|
2022-01-19 17:22:36 +01:00
|
|
|
path: pkg_path
|
2022-01-13 21:47:14 +01:00
|
|
|
info: pkg_info
|
|
|
|
files: files
|
2022-01-31 22:59:12 +01:00
|
|
|
compression: compression_code
|
2022-01-13 21:47:14 +01:00
|
|
|
}
|
|
|
|
}
|