From 49e7b9de5ddf66d2d28e5af2884973c3be050095 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Wed, 12 Jan 2022 18:18:23 +0100 Subject: [PATCH 01/14] Added linking of libarchive to Makefile --- .gitignore | 3 +++ Makefile | 33 +++++++++++++++++++++++++-------- vieter/repo.v | 4 ++++ 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 8d15c59..b96b80c 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,6 @@ vieter/vieter *.pkg* vieter.log + +# External lib; gets added by Makefile +libarchive-* diff --git a/Makefile b/Makefile index a48a802..0f1db9d 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,22 @@ +# =====CONFIG===== +LARCHIVE_VER := 3.5.2 +LARCHIVE_DIR := libarchive-$(LARCHIVE_VER) +LARCHIVE_LIB := $(LARCHIVE_DIR)/.libs/libarchive.a + +# Custom V command for linking libarchive +V := LD_FLAGS=$(PWD)/$(LARCHIVE_LIB) v -cflags -I$(PWD)/$(LARCHIVE_DIR) + +# Run the server in the default 'data' directory .PHONY: run -run: - API_KEY=test REPO_DIR=data LOG_LEVEL=DEBUG v -cg run vieter - -.PHONY: run-prod -run-prod: - API_KEY=test REPO_DIR=data LOG_LEVEL=DEBUG v -prod run vieter +run: libarchive + API_KEY=test REPO_DIR=data LOG_LEVEL=DEBUG $(V) run vieter +# Same as run, but restart when the source code changes .PHONY: watch -watch: - API_KEY=test REPO_DIR=data LOG_LEVEL=DEBUG v watch run vieter +watch: libarchive + API_KEY=test REPO_DIR=data LOG_LEVEL=DEBUG $(V) watch run vieter +# Format the V codebase .PHONY: fmt fmt: v fmt -w vieter @@ -23,3 +30,13 @@ customv: --single-branch \ https://github.com/ChewingBever/v jjr-v '$(MAKE)' -C jjr-v + +.PHONY: libarchive +libarchive: $(LARCHIVE_LIB) +$(LARCHIVE_LIB): + curl -o - "https://libarchive.org/downloads/libarchive-${LARCHIVE_VER}.tar.gz" | tar xzf - + cd "libarchive-${LARCHIVE_VER}" && ./configure --disable-bsdtar --disable-bsdcpio + '$(MAKE)' -C "libarchive-${LARCHIVE_VER}" + +clean: + rm -rf '$(LARCHIVE_DIR)' 'data' diff --git a/vieter/repo.v b/vieter/repo.v index 6b307e8..c7e52bf 100644 --- a/vieter/repo.v +++ b/vieter/repo.v @@ -2,6 +2,10 @@ module repo import os +#include "libarchive/archive.h" + +struct C.archive {} + const pkgs_subpath = 'pkgs' // Dummy struct to work around the fact that you can only share structs, maps & From cfcca90dfcbb726c9deab38643d7ae98468e8bff Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Wed, 12 Jan 2022 18:54:13 +0100 Subject: [PATCH 02/14] Fixed broken linking --- .gitignore | 3 ++- Makefile | 18 +++++++++++++++++- vieter/archive.v | 13 +++++++++++++ vieter/repo.v | 4 ---- 4 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 vieter/archive.v diff --git a/.gitignore b/.gitignore index b96b80c..cd150cd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *.c data/ -vieter/vieter +vieter.exe +vieter-prod.exe # Ignore testing files *.pkg* diff --git a/Makefile b/Makefile index 0f1db9d..5c6f8da 100644 --- a/Makefile +++ b/Makefile @@ -4,8 +4,20 @@ LARCHIVE_DIR := libarchive-$(LARCHIVE_VER) LARCHIVE_LIB := $(LARCHIVE_DIR)/.libs/libarchive.a # Custom V command for linking libarchive -V := LD_FLAGS=$(PWD)/$(LARCHIVE_LIB) v -cflags -I$(PWD)/$(LARCHIVE_DIR) +V := LDFLAGS=$(PWD)/$(LARCHIVE_LIB) v -cflags -I$(PWD)/$(LARCHIVE_DIR) + +# =====COMPILATION===== +.PHONY: vieter +vieter: + $(V) -cg -o vieter.exe vieter + +.PHONY: prod +prod: + $(V) -o vieter-prod.exe -prod vieter + + +# =====EXECUTION===== # Run the server in the default 'data' directory .PHONY: run run: libarchive @@ -16,6 +28,8 @@ run: libarchive watch: libarchive API_KEY=test REPO_DIR=data LOG_LEVEL=DEBUG $(V) watch run vieter + +# =====OTHER===== # Format the V codebase .PHONY: fmt fmt: @@ -31,6 +45,8 @@ customv: https://github.com/ChewingBever/v jjr-v '$(MAKE)' -C jjr-v + +# =====LIBARCHIVE===== .PHONY: libarchive libarchive: $(LARCHIVE_LIB) $(LARCHIVE_LIB): diff --git a/vieter/archive.v b/vieter/archive.v new file mode 100644 index 0000000..4830c24 --- /dev/null +++ b/vieter/archive.v @@ -0,0 +1,13 @@ +module archive + +#include "libarchive/archive.h" +#include "libarchive/archive_entry.h" + +struct C.archive {} +struct C.archive_entry {} + +fn C.archive_read_new() &C.archive + +pub fn list_filenames() { + a := C.archive_read_new() +} diff --git a/vieter/repo.v b/vieter/repo.v index c7e52bf..6b307e8 100644 --- a/vieter/repo.v +++ b/vieter/repo.v @@ -2,10 +2,6 @@ module repo import os -#include "libarchive/archive.h" - -struct C.archive {} - const pkgs_subpath = 'pkgs' // Dummy struct to work around the fact that you can only share structs, maps & From 05e725eb4e59e3a78872c188c99e5b416f241c30 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Wed, 12 Jan 2022 20:54:54 +0100 Subject: [PATCH 03/14] Still trying to just link some stuff bro --- .gitignore | 4 ++-- Makefile | 32 +++++++++++++++++++------------- src/archive.v | 20 ++++++++++++++++++++ {vieter => src}/auth.v | 0 {vieter => src}/main.v | 2 ++ {vieter => src}/repo.v | 0 {vieter => src}/routes.v | 0 {vieter => src}/web/logging.v | 0 {vieter => src}/web/parse.v | 0 {vieter => src}/web/web.v | 0 vieter/archive.v | 13 ------------- 11 files changed, 43 insertions(+), 28 deletions(-) create mode 100644 src/archive.v rename {vieter => src}/auth.v (100%) rename {vieter => src}/main.v (98%) rename {vieter => src}/repo.v (100%) rename {vieter => src}/routes.v (100%) rename {vieter => src}/web/logging.v (100%) rename {vieter => src}/web/parse.v (100%) rename {vieter => src}/web/web.v (100%) delete mode 100644 vieter/archive.v diff --git a/.gitignore b/.gitignore index cd150cd..be17f6b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ *.c data/ -vieter.exe -vieter-prod.exe +vieter +vieter-prod # Ignore testing files *.pkg* diff --git a/Makefile b/Makefile index 5c6f8da..137c52b 100644 --- a/Makefile +++ b/Makefile @@ -1,31 +1,37 @@ # =====CONFIG===== +SRC_DIR := src +SOURCES != find '$(SRC_DIR)' -iname '*.v' + LARCHIVE_VER := 3.5.2 LARCHIVE_DIR := libarchive-$(LARCHIVE_VER) -LARCHIVE_LIB := $(LARCHIVE_DIR)/.libs/libarchive.a +LARCHIVE_LIB := $(LARCHIVE_DIR)/libarchive/libarchive.so # Custom V command for linking libarchive -V := LDFLAGS=$(PWD)/$(LARCHIVE_LIB) v -cflags -I$(PWD)/$(LARCHIVE_DIR) +# V := LDFLAGS=$(PWD)/$(LARCHIVE_LIB) v -cflags '-I$(PWD)/$(LARCHIVE_DIR) -I $(PWD)/$(LARCHIVE_DIR)' +V := v # =====COMPILATION===== -.PHONY: vieter -vieter: - $(V) -cg -o vieter.exe vieter +.PHONY: debug +debug: vieter +vieter: $(SOURCES) + $(V) -cg -o vieter $(SRC_DIR) .PHONY: prod -prod: - $(V) -o vieter-prod.exe -prod vieter +prod: vieter-prod +vieter-prod: $(SOURCES) + $(V) -o vieter-prod -prod $(SRC_DIR) # =====EXECUTION===== # Run the server in the default 'data' directory .PHONY: run -run: libarchive - API_KEY=test REPO_DIR=data LOG_LEVEL=DEBUG $(V) run vieter +run: vieter + API_KEY=test REPO_DIR=data LOG_LEVEL=DEBUG ./vieter # Same as run, but restart when the source code changes .PHONY: watch -watch: libarchive +watch: API_KEY=test REPO_DIR=data LOG_LEVEL=DEBUG $(V) watch run vieter @@ -33,7 +39,7 @@ watch: libarchive # Format the V codebase .PHONY: fmt fmt: - v fmt -w vieter + v fmt -w $(SRC_DIR) # Pulls & builds my personal build of the v compiler, required for this project to function .PHONY: customv @@ -51,8 +57,8 @@ customv: libarchive: $(LARCHIVE_LIB) $(LARCHIVE_LIB): curl -o - "https://libarchive.org/downloads/libarchive-${LARCHIVE_VER}.tar.gz" | tar xzf - - cd "libarchive-${LARCHIVE_VER}" && ./configure --disable-bsdtar --disable-bsdcpio + cd "libarchive-${LARCHIVE_VER}" && cmake . '$(MAKE)' -C "libarchive-${LARCHIVE_VER}" clean: - rm -rf '$(LARCHIVE_DIR)' 'data' + rm -rf '$(LARCHIVE_DIR)' 'data' 'vieter' 'vieter-prod' diff --git a/src/archive.v b/src/archive.v new file mode 100644 index 0000000..ef36205 --- /dev/null +++ b/src/archive.v @@ -0,0 +1,20 @@ +module archive + +#flag -larchive +#include "archive.h" +#include "archive_entry.h" + +struct C.archive {} + +struct C.archive_entry {} + +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) + +pub fn list_filenames() { + a := C.archive_read_new() + C.archive_read_support_filter_all(a) + C.archive_read_support_format_all(a) + println(a) +} diff --git a/vieter/auth.v b/src/auth.v similarity index 100% rename from vieter/auth.v rename to src/auth.v diff --git a/vieter/main.v b/src/main.v similarity index 98% rename from vieter/main.v rename to src/main.v index b974864..54b3662 100644 --- a/vieter/main.v +++ b/src/main.v @@ -5,6 +5,7 @@ import os import log import io import repo +import archive const port = 8000 @@ -94,6 +95,7 @@ fn main() { logger.info("Created package directory '$repo.pkg_dir()'.") } + archive.list_filenames() web.run(&App{ logger: logger api_key: key diff --git a/vieter/repo.v b/src/repo.v similarity index 100% rename from vieter/repo.v rename to src/repo.v diff --git a/vieter/routes.v b/src/routes.v similarity index 100% rename from vieter/routes.v rename to src/routes.v diff --git a/vieter/web/logging.v b/src/web/logging.v similarity index 100% rename from vieter/web/logging.v rename to src/web/logging.v diff --git a/vieter/web/parse.v b/src/web/parse.v similarity index 100% rename from vieter/web/parse.v rename to src/web/parse.v diff --git a/vieter/web/web.v b/src/web/web.v similarity index 100% rename from vieter/web/web.v rename to src/web/web.v diff --git a/vieter/archive.v b/vieter/archive.v deleted file mode 100644 index 4830c24..0000000 --- a/vieter/archive.v +++ /dev/null @@ -1,13 +0,0 @@ -module archive - -#include "libarchive/archive.h" -#include "libarchive/archive_entry.h" - -struct C.archive {} -struct C.archive_entry {} - -fn C.archive_read_new() &C.archive - -pub fn list_filenames() { - a := C.archive_read_new() -} From d6e686a8dc5f550da4c26ac53e1fca821f1ef10d Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Wed, 12 Jan 2022 21:22:23 +0100 Subject: [PATCH 04/14] This print killed it, my lord --- Makefile | 6 +++++- src/archive.v | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 137c52b..0aa3f87 100644 --- a/Makefile +++ b/Makefile @@ -26,9 +26,13 @@ vieter-prod: $(SOURCES) # =====EXECUTION===== # Run the server in the default 'data' directory .PHONY: run -run: vieter +run: debug API_KEY=test REPO_DIR=data LOG_LEVEL=DEBUG ./vieter +.PHONY: run-prod +run-prod: prod + API_KEY=test REPO_DIR=data LOG_LEVEL=DEBUG ./vieter-prod + # Same as run, but restart when the source code changes .PHONY: watch watch: diff --git a/src/archive.v b/src/archive.v index ef36205..4ae94d7 100644 --- a/src/archive.v +++ b/src/archive.v @@ -16,5 +16,5 @@ pub fn list_filenames() { a := C.archive_read_new() C.archive_read_support_filter_all(a) C.archive_read_support_format_all(a) - println(a) + // println(a) } From 476588274f23987892436811c6e8d20399eda6ab Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Wed, 12 Jan 2022 21:50:17 +0100 Subject: [PATCH 05/14] Geniet ervan Maxim --- Makefile | 4 ++++ src/archive.v | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 0aa3f87..3c58b14 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,10 @@ prod: vieter-prod vieter-prod: $(SOURCES) $(V) -o vieter-prod -prod $(SRC_DIR) +.PHONY: c +c: + $(V) -o vieter.c $(SRC_DIR) + # =====EXECUTION===== # Run the server in the default 'data' directory diff --git a/src/archive.v b/src/archive.v index 4ae94d7..40be95a 100644 --- a/src/archive.v +++ b/src/archive.v @@ -11,10 +11,26 @@ struct C.archive_entry {} 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) +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_entry_pathname(C.archive_entry) &char +fn C.archive_read_data_skip(&C.archive) +fn C.archive_read_free(&C.archive) int pub fn list_filenames() { 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) - // println(a) + + r = C.archive_read_open_filename(a, c'test/homebank-5.5.1-1-x86_64.pkg.tar.zst', 10240) + + for (C.archive_read_next_header(a, entry) == C.ARCHIVE_OK) { + println(C.archive_entry_pathname(entry)) + C.archive_read_data_skip(a) // Note 2 + } + + r = C.archive_read_free(a) // Note 3 } From 74c8bfa1580c366e98ece04b63505dc537bafce1 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Wed, 12 Jan 2022 22:08:27 +0100 Subject: [PATCH 06/14] First actual working libarchive function! --- src/archive.v | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/archive.v b/src/archive.v index 40be95a..2e33f89 100644 --- a/src/archive.v +++ b/src/archive.v @@ -12,8 +12,8 @@ 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) 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_entry_pathname(C.archive_entry) &char +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 @@ -27,8 +27,8 @@ pub fn list_filenames() { r = C.archive_read_open_filename(a, c'test/homebank-5.5.1-1-x86_64.pkg.tar.zst', 10240) - for (C.archive_read_next_header(a, entry) == C.ARCHIVE_OK) { - println(C.archive_entry_pathname(entry)) + 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 } From 6dbac5918bcc0a1aff3f39bcf974e8e4ae916e75 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Wed, 12 Jan 2022 23:06:03 +0100 Subject: [PATCH 07/14] 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 From fd334d93a25e6e1eecdc623049c456e7a2ad1c3e Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Wed, 12 Jan 2022 23:15:11 +0100 Subject: [PATCH 08/14] We're done for today --- src/archive.v | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/archive.v b/src/archive.v index 6846ddb..c6e253a 100644 --- a/src/archive.v +++ b/src/archive.v @@ -47,20 +47,22 @@ pub fn get_pkg_info(pkg_path string) ?string { 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) 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 pkg_info := '' + mut buf := []byte{} 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} + buf = []byte{len: size} C.archive_read_data(a, voidptr(&buf), size) break }else{ @@ -69,5 +71,5 @@ pub fn get_pkg_info(pkg_path string) ?string { } r = C.archive_read_free(a) // Note 3 - return '' + return buf.bytestr() } From f49992d452269be4975662e4856caca0ff65b888 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 13 Jan 2022 12:35:05 +0100 Subject: [PATCH 09/14] 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) +} From 44ddd57f55158d4d0ec97db4934270930182f6cf Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 13 Jan 2022 12:46:36 +0100 Subject: [PATCH 10/14] Moved archive C bindings into separate file --- src/archive.v | 87 ------------------------------------------ src/archive/archive.v | 43 +++++++++++++++++++++ src/archive/bindings.v | 46 ++++++++++++++++++++++ 3 files changed, 89 insertions(+), 87 deletions(-) delete mode 100644 src/archive.v create mode 100644 src/archive/archive.v create mode 100644 src/archive/bindings.v diff --git a/src/archive.v b/src/archive.v deleted file mode 100644 index 1ecc8a6..0000000 --- a/src/archive.v +++ /dev/null @@ -1,87 +0,0 @@ -module archive - -import os - -#flag -larchive -#include "archive.h" -#include "archive_entry.h" -#include - -struct C.archive {} - -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 - -fn C.strcmp(&char, &char) int - -// pub fn list_filenames() { -// 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) - -// r = C.archive_read_open_filename(a, c'test/homebank-5.5.1-1-x86_64.pkg.tar.zst', 10240) - -// 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_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 - 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.') - } - - // We iterate over every header in search of the .PKGINFO one - mut buf := voidptr(0) - for C.archive_read_next_header(a, &entry) == C.ARCHIVE_OK { - if C.strcmp(C.archive_entry_pathname(entry), c'.PKGINFO') == 0 { - size := C.archive_entry_size(entry) - - // 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) - } - } - - return unsafe { cstring_to_vstring(&char(buf)) } -} diff --git a/src/archive/archive.v b/src/archive/archive.v new file mode 100644 index 0000000..ca911c7 --- /dev/null +++ b/src/archive/archive.v @@ -0,0 +1,43 @@ +module archive + +import os + +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_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 + 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.') + } + + // We iterate over every header in search of the .PKGINFO one + mut buf := voidptr(0) + for C.archive_read_next_header(a, &entry) == C.ARCHIVE_OK { + if C.strcmp(C.archive_entry_pathname(entry), c'.PKGINFO') == 0 { + size := C.archive_entry_size(entry) + + // 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) + } + } + + return unsafe { cstring_to_vstring(&char(buf)) } +} diff --git a/src/archive/bindings.v b/src/archive/bindings.v new file mode 100644 index 0000000..678d715 --- /dev/null +++ b/src/archive/bindings.v @@ -0,0 +1,46 @@ +module archive + +#flag -larchive + +#include "archive.h" + +struct C.archive {} + +// 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) + +// Open an archive for reading +fn C.archive_read_open_filename(&C.archive, &char, int) int + +// Go to next entry header in archive +fn C.archive_read_next_header(&C.archive, &&C.archive_entry) int + +// Skip reading the current entry +fn C.archive_read_data_skip(&C.archive) + +// Free an archive +fn C.archive_read_free(&C.archive) int + +// Read an archive entry's contents into a pointer +fn C.archive_read_data(&C.archive, voidptr, int) + +#include "archive_entry.h" + +struct C.archive_entry {} + +// Create a new archive_entry struct +fn C.archive_entry_new() &C.archive_entry + +// Get the filename of the given entry +fn C.archive_entry_pathname(&C.archive_entry) &char + +// Get an entry's file size +// Note: this function actually returns an i64, but as this can't be used as an arugment to malloc, we'll just roll with it & assume an entry is never bigger than 4 gigs +fn C.archive_entry_size(&C.archive_entry) int + +#include + +// Compare two C strings; 0 means they're equal +fn C.strcmp(&char, &char) int From eed6ec0644dd76f610a8094db88736a3c1705eb4 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 13 Jan 2022 13:40:33 +0100 Subject: [PATCH 11/14] Updated CI configuration --- .woodpecker/.build.yml | 18 ++++++++++++++++++ .woodpecker/.lint.yml | 5 +++++ 2 files changed, 23 insertions(+) create mode 100644 .woodpecker/.build.yml create mode 100644 .woodpecker/.lint.yml diff --git a/.woodpecker/.build.yml b/.woodpecker/.build.yml new file mode 100644 index 0000000..40a50e1 --- /dev/null +++ b/.woodpecker/.build.yml @@ -0,0 +1,18 @@ +pipeline: + vieter: + image: 'chewingbever/vlang:latest' + group: 'build' + commands: + - make vieter + + debug: + image: 'chewingbever/vlang:latest' + group: 'build' + commands: + - make debug + + prod: + image: 'chewingbever/vlang:latest' + group: 'build' + commands: + - make prod diff --git a/.woodpecker/.lint.yml b/.woodpecker/.lint.yml new file mode 100644 index 0000000..72529e7 --- /dev/null +++ b/.woodpecker/.lint.yml @@ -0,0 +1,5 @@ +pipeline: + lint: + image: 'chewingbever/vlang:latest' + commands: + - make fmt From 6fb893ff41bb394f4b40e655708251583eb846ba Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 13 Jan 2022 13:51:02 +0100 Subject: [PATCH 12/14] Added development & release image publishing --- .dockerignore | 2 +- .woodpecker/.publish.yml | 22 ++++++++++++++++++++++ Dockerfile | 23 ++++++++++++----------- src/main.v | 18 +++++++++--------- 4 files changed, 44 insertions(+), 21 deletions(-) create mode 100644 .woodpecker/.publish.yml diff --git a/.dockerignore b/.dockerignore index d55305a..643e6db 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,4 +1,4 @@ * -!vieter/ +!src/ !Makefile diff --git a/.woodpecker/.publish.yml b/.woodpecker/.publish.yml new file mode 100644 index 0000000..4456649 --- /dev/null +++ b/.woodpecker/.publish.yml @@ -0,0 +1,22 @@ +pipeline: + dev: + image: plugins/docker + secrets: [ docker_username, docker_password ] + settings: + repo: chewingbever/vieter + tag: dev + when: + event: push + branch: dev + + release: + image: plugins/docker + secrets: [ docker_username, docker_password ] + settings: + repo: chewingbever/vieter + tag: + - latest + - $CI_COMMIT_TAG + when: + event: tag + branch: main diff --git a/Dockerfile b/Dockerfile index e8b2484..556ca58 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,20 +1,21 @@ -FROM archlinux:latest AS builder +FROM chewingbever/vlang:latest AS builder -WORKDIR /src -COPY vieter ./vieter +WORKDIR /app + +# Copy over source code & build production binary +COPY src ./src COPY Makefile ./ - -RUN pacman \ - -Syu --noconfirm --needed \ - gcc git openssl make && \ - make customv && \ - jjr-v/v -prod vieter +RUN make prod -FROM archlinux:latest +FROM alpine:3.15 ENV REPO_DIR=/data -COPY --from=builder /src/vieter/vieter /usr/local/bin/ +RUN apk update && \ + apk add --no-cache \ + libarchive + +COPY --from=builder /app/pvieter /usr/local/bin/vieter ENTRYPOINT [ "/usr/local/bin/vieter" ] diff --git a/src/main.v b/src/main.v index e779c8f..af6b06c 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 main2() { +fn main() { // Configure logger log_level_str := os.getenv_opt('LOG_LEVEL') or { 'WARN' } log_level := log.level_from_tag(log_level_str) or { @@ -102,11 +102,11 @@ fn main2() { }, 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) -} +// 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) +// } From 4085068587c3745e38db8ca23a2015df59d1ed13 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 13 Jan 2022 13:53:39 +0100 Subject: [PATCH 13/14] Added publish branch restriction --- .woodpecker/.publish.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.woodpecker/.publish.yml b/.woodpecker/.publish.yml index 4456649..d1743d7 100644 --- a/.woodpecker/.publish.yml +++ b/.woodpecker/.publish.yml @@ -1,3 +1,5 @@ +branch: [main, dev] + pipeline: dev: image: plugins/docker From 465b83df5c6752479deb8fc7d1f037113d21df41 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 13 Jan 2022 13:54:20 +0100 Subject: [PATCH 14/14] Apparently it's branches --- .woodpecker/.publish.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.woodpecker/.publish.yml b/.woodpecker/.publish.yml index d1743d7..490d422 100644 --- a/.woodpecker/.publish.yml +++ b/.woodpecker/.publish.yml @@ -1,4 +1,4 @@ -branch: [main, dev] +branches: [main, dev] pipeline: dev: