From 6dbac5918bcc0a1aff3f39bcf974e8e4ae916e75 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Wed, 12 Jan 2022 23:06:03 +0100 Subject: [PATCH] Start of .PKGINFO function --- src/archive.v | 37 +++++++++++++++++++++++++++++++++++++ src/main.v | 4 +++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/archive.v b/src/archive.v index 2e33f89..6846ddb 100644 --- a/src/archive.v +++ b/src/archive.v @@ -8,6 +8,7 @@ struct C.archive {} struct C.archive_entry {} +// Create a new archive struct fn C.archive_read_new() &C.archive fn C.archive_read_support_filter_all(&C.archive) fn C.archive_read_support_format_all(&C.archive) @@ -16,6 +17,8 @@ fn C.archive_read_next_header(&C.archive, &&C.archive_entry) int fn C.archive_entry_pathname(&C.archive_entry) &char fn C.archive_read_data_skip(&C.archive) fn C.archive_read_free(&C.archive) int +fn C.archive_read_data(&C.archive, voidptr, int) +fn C.archive_entry_size(&C.archive_entry) int pub fn list_filenames() { a := C.archive_read_new() @@ -34,3 +37,37 @@ pub fn list_filenames() { r = C.archive_read_free(a) // Note 3 } + +pub fn get_pkg_info(pkg_path string) ?string { + a := C.archive_read_new() + entry := &C.archive_entry{} + mut r := 0 + + C.archive_read_support_filter_all(a) + C.archive_read_support_format_all(a) + + // TODO find out where does this 10240 come from + r = C.archive_read_open_filename(a, &char(pkg_path.str), 10240) + + if r != C.ARCHIVE_OK { + return error('Failed to open package.') + } + + // We iterate over every header in search of the .PKGINFO one + mut pkg_info := '' + for C.archive_read_next_header(a, &entry) == C.ARCHIVE_OK { + // TODO possibly avoid this cstring_to_vstring + if cstring_to_vstring(C.archive_entry_pathname(entry)) == '.PKGINFO' { + size := C.archive_entry_size(entry) + + mut buf := []byte{len: size} + C.archive_read_data(a, voidptr(&buf), size) + break + }else{ + C.archive_read_data_skip(a) + } + } + + r = C.archive_read_free(a) // Note 3 + return '' +} diff --git a/src/main.v b/src/main.v index 54b3662..dbf4652 100644 --- a/src/main.v +++ b/src/main.v @@ -95,7 +95,9 @@ fn main() { logger.info("Created package directory '$repo.pkg_dir()'.") } - archive.list_filenames() + archive.get_pkg_info('test/homebank-5.5.1-1-x86_64.pkg.tar.zst') or { + eprintln(err.msg) + } web.run(&App{ logger: logger api_key: key