From f49992d452269be4975662e4856caca0ff65b888 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 13 Jan 2022 12:35:05 +0100 Subject: [PATCH] Working .PKGINFO read function! --- .gitignore | 6 +++++- Makefile | 23 +++++++++++++------- src/archive.v | 58 +++++++++++++++++++++++++++++++-------------------- src/main.v | 14 +++++++++---- 4 files changed, 65 insertions(+), 36 deletions(-) diff --git a/.gitignore b/.gitignore index be17f6b..8c67f97 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,11 @@ *.c data/ + +# Build artifacts vieter -vieter-prod +dvieter +pvieter +vieter.c # Ignore testing files *.pkg* diff --git a/Makefile b/Makefile index 3c58b14..15c52cd 100644 --- a/Makefile +++ b/Makefile @@ -10,17 +10,24 @@ LARCHIVE_LIB := $(LARCHIVE_DIR)/libarchive/libarchive.so # V := LDFLAGS=$(PWD)/$(LARCHIVE_LIB) v -cflags '-I$(PWD)/$(LARCHIVE_DIR) -I $(PWD)/$(LARCHIVE_DIR)' V := v +all: vieter # =====COMPILATION===== -.PHONY: debug -debug: vieter +# Regular binary vieter: $(SOURCES) - $(V) -cg -o vieter $(SRC_DIR) + $(V) -g -o vieter $(SRC_DIR) +# Debug build using gcc +.PHONY: debug +debug: dvieter +dvieter: $(SOURCES) + $(V) -keepc -cg -cc gcc -o dvieter $(SRC_DIR) + +# Optimised production build .PHONY: prod -prod: vieter-prod -vieter-prod: $(SOURCES) - $(V) -o vieter-prod -prod $(SRC_DIR) +prod: pvieter +pvieter: $(SOURCES) + $(V) -o pvieter -prod $(SRC_DIR) .PHONY: c c: @@ -30,7 +37,7 @@ c: # =====EXECUTION===== # Run the server in the default 'data' directory .PHONY: run -run: debug +run: vieter API_KEY=test REPO_DIR=data LOG_LEVEL=DEBUG ./vieter .PHONY: run-prod @@ -69,4 +76,4 @@ $(LARCHIVE_LIB): '$(MAKE)' -C "libarchive-${LARCHIVE_VER}" clean: - rm -rf '$(LARCHIVE_DIR)' 'data' 'vieter' 'vieter-prod' + rm -rf '$(LARCHIVE_DIR)' 'data' 'vieter' 'dvieter' 'pvieter' 'vieter.c' diff --git a/src/archive.v b/src/archive.v index c6e253a..1ecc8a6 100644 --- a/src/archive.v +++ b/src/archive.v @@ -1,8 +1,11 @@ module archive +import os + #flag -larchive #include "archive.h" #include "archive_entry.h" +#include struct C.archive {} @@ -10,66 +13,75 @@ struct C.archive_entry {} // Create a new archive struct fn C.archive_read_new() &C.archive +fn C.archive_entry_new() &C.archive_entry fn C.archive_read_support_filter_all(&C.archive) fn C.archive_read_support_format_all(&C.archive) +// Open an archive for reading fn C.archive_read_open_filename(&C.archive, &char, int) int fn C.archive_read_next_header(&C.archive, &&C.archive_entry) int +fn C.archive_read_next_header2(&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() - entry := &C.archive_entry{} - mut r := 0 +fn C.strcmp(&char, &char) int - C.archive_read_support_filter_all(a) - C.archive_read_support_format_all(a) +// pub fn list_filenames() { +// a := C.archive_read_new() +// entry := &C.archive_entry{} +// mut r := 0 - r = C.archive_read_open_filename(a, c'test/homebank-5.5.1-1-x86_64.pkg.tar.zst', 10240) +// C.archive_read_support_filter_all(a) +// C.archive_read_support_format_all(a) - for (C.archive_read_next_header(a, &entry) == C.ARCHIVE_OK) { - println(cstring_to_vstring(C.archive_entry_pathname(entry))) - C.archive_read_data_skip(a) // Note 2 - } +// r = C.archive_read_open_filename(a, c'test/homebank-5.5.1-1-x86_64.pkg.tar.zst', 10240) - r = C.archive_read_free(a) // Note 3 -} +// for (C.archive_read_next_header(a, &entry) == C.ARCHIVE_OK) { +// println(cstring_to_vstring(C.archive_entry_pathname(entry))) +// C.archive_read_data_skip(a) // Note 2 +// } + +// r = C.archive_read_free(a) // Note 3 +// } pub fn get_pkg_info(pkg_path string) ?string { + 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{} + entry := C.archive_entry_new() 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 - println('1') r = C.archive_read_open_filename(a, &char(pkg_path.str), 10240) + defer { + C.archive_read_free(a) + } if r != C.ARCHIVE_OK { return error('Failed to open package.') } - println('2') // We iterate over every header in search of the .PKGINFO one - mut buf := []byte{} + mut buf := voidptr(0) 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' { + if C.strcmp(C.archive_entry_pathname(entry), c'.PKGINFO') == 0 { size := C.archive_entry_size(entry) - buf = []byte{len: size} - C.archive_read_data(a, voidptr(&buf), size) + // TODO can this unsafe block be avoided? + buf = unsafe { malloc(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 buf.bytestr() + return unsafe { cstring_to_vstring(&char(buf)) } } diff --git a/src/main.v b/src/main.v index dbf4652..e779c8f 100644 --- a/src/main.v +++ b/src/main.v @@ -54,7 +54,7 @@ fn reader_to_file(mut reader io.BufferedReader, length int, path string) ? { } } -fn main() { +fn main2() { // Configure logger log_level_str := os.getenv_opt('LOG_LEVEL') or { 'WARN' } log_level := log.level_from_tag(log_level_str) or { @@ -95,12 +95,18 @@ fn main() { logger.info("Created package directory '$repo.pkg_dir()'.") } - 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 repo: repo }, port) } + +fn main() { + // archive.list_filenames() + info := archive.get_pkg_info('test/jjr-joplin-desktop-2.6.10-4-x86_64.pkg.tar.zst') or { + eprintln(err.msg) + return + } + println(info) +}