diff options
| author | forever2k38 <forever2k38@noreply.codeberg.org> | 2026-09-02 15:30:34 +0200 |
|---|---|---|
| committer | forever2k38 <forever2k38@noreply.codeberg.org> | 2026-09-02 15:30:34 +0200 |
| commit | ed979ea2b7cc0caf3abb47dce94e02ef16c73e65 (patch) | |
| tree | 86ee7fda3e9fe8c708391c25e95e2b11b7aa53de /src | |
| parent | a2cb6e5b16bd8f5eddd778f813f3a9068be44e46 (diff) | |
lua work
Diffstat (limited to 'src')
| -rw-r--r-- | src/args.c | 15 | ||||
| -rw-r--r-- | src/args.h | 1 | ||||
| -rw-r--r-- | src/build.c | 5 | ||||
| -rw-r--r-- | src/build.h | 3 | ||||
| -rw-r--r-- | src/main.c | 17 | ||||
| -rw-r--r-- | src/package.c | 7 | ||||
| -rw-r--r-- | src/package.h | 13 | ||||
| -rw-r--r-- | src/pow_lua.c | 117 | ||||
| -rw-r--r-- | src/repo.c | 36 | ||||
| -rw-r--r-- | src/repo.h | 6 |
10 files changed, 202 insertions, 18 deletions
@@ -82,6 +82,8 @@ int pow_args_parse(int argc, char **argv, pow_args *args) { flags |= POW_FLAG_SYNC; next_flag_package = true; + + break; }; case 'Q': { if (flags & POW_FLAG_QUERY) { @@ -91,6 +93,19 @@ int pow_args_parse(int argc, char **argv, pow_args *args) { flags |= POW_FLAG_QUERY; next_flag_package = true; + + break; + } + case 'I': { + if (flags & POW_FLAG_INSTALL) { + printf("flag double defined, skipping\n"); + } + + flags |= POW_FLAG_INSTALL; + + next_flag_package = true; + + break; } } } @@ -5,6 +5,7 @@ #define POW_FLAG_HELP 0x01 #define POW_FLAG_SYNC 0x02 #define POW_FLAG_QUERY 0x04 +#define POW_FLAG_INSTALL 0x08 typedef struct { int flags; diff --git a/src/build.c b/src/build.c new file mode 100644 index 0000000..d680cf7 --- /dev/null +++ b/src/build.c @@ -0,0 +1,5 @@ +#include "build.h" + +#include "package.h" + +int pow_package_build(pow_package *pkg) { return 0; } diff --git a/src/build.h b/src/build.h new file mode 100644 index 0000000..eea6c75 --- /dev/null +++ b/src/build.h @@ -0,0 +1,3 @@ +#include "package.h" + +int pow_package_build(pow_package *pkg); @@ -1,8 +1,10 @@ #include "args.h" +#include "build.h" #include "config.h" #include "repo.h" #include "stdio.h" +#include "stdlib.h" int main(int argc, char *argv[]) { pow_args args = {0}; @@ -20,7 +22,20 @@ int main(int argc, char *argv[]) { pow_open_config(&config, args.cfg_path); if (args.flags & POW_FLAG_QUERY) { - repo_check_package(config.repos, config.repos_count, "samurai"); + pow_repo_check_package(config.repos, config.repos_count, args.package); + } + + if (args.flags & POW_FLAG_SYNC) { + pow_package *pkg = malloc(sizeof(pow_package)); + + if (!pow_repo_get_package(config.repos, config.repos_count, pkg, + args.package)) { + printf("error getting package\n"); + } + + pow_package_build(pkg); + + pow_package_close(pkg); } if (args.flags == 0) { diff --git a/src/package.c b/src/package.c index 2e571e9..832e7da 100644 --- a/src/package.c +++ b/src/package.c @@ -40,10 +40,12 @@ int extract_package_info(lua_State *L, pow_package *pkg) { printf("strdup failed trying to get package name\n"); } + pkg->pkg_ref = luaL_ref(L, LUA_REGISTRYINDEX); + return 0; } -int package_open(pow_package *pkg, const char *path) { +int pow_package_open(pow_package *pkg, const char *path) { FILE *fp; int c; int err; @@ -111,11 +113,12 @@ int package_open(pow_package *pkg, const char *path) { } extract_package_info(L, pkg); + pkg->path = path; return 0; } -int package_close(pow_package *pkg) { +int pow_package_close(pow_package *pkg) { free(pkg->name); free(pkg->version); diff --git a/src/package.h b/src/package.h index a1e4595..12804bc 100644 --- a/src/package.h +++ b/src/package.h @@ -1,3 +1,6 @@ +#ifndef POW_PACKAGE_H +#define POW_PACKAGE_H + #include "lua.h" #include "stddef.h" @@ -6,10 +9,16 @@ typedef struct { const char *version; const char *description; + const char *path; + size_t *arch; lua_State *L; + + int pkg_ref; } pow_package; -int package_open(pow_package *pkg, const char *path); -int package_close(pow_package *pkg); +int pow_package_open(pow_package *pkg, const char *path); +int pow_package_close(pow_package *pkg); + +#endif diff --git a/src/pow_lua.c b/src/pow_lua.c index 0d0214d..70b32ad 100644 --- a/src/pow_lua.c +++ b/src/pow_lua.c @@ -1,11 +1,122 @@ #include "pow_lua.h" +#include "archive.h" +#include "archive_entry.h" #include "lauxlib.h" -#include "lualib.h" +#include "stdlib.h" +#include "string.h" -int pow_lua_hello(lua_State *L) { printf("hi\n"); } +int pow_lua_extract(lua_State *L) { + int r; + ssize_t size; -luaL_Reg libpow[] = {{"hello", pow_lua_hello}}; + struct archive *a; + struct archive_entry *entry; + + if (!lua_isstring(L, 1)) { + lua_pushnil(L); + lua_pushstring(L, "arg 1 must be string (src)"); + + return 2; + } + + if (!lua_isstring(L, 2)) { + lua_pushnil(L); + lua_pushstring(L, "arg 2 must be string (dest)"); + + return 2; + } + + const char *filename = lua_tostring(L, 1); + const char *dest = lua_tostring(L, 2); + + a = archive_read_new(); + + archive_read_support_filter_all(a); + archive_read_support_format_all(a); + + if ((r = archive_read_open_filename(a, filename, 10240))) { + return 0; + } + + for (;;) { + r = archive_read_next_header(a, &entry); + + if (r == ARCHIVE_EOF) + break; + if (r < ARCHIVE_OK) + printf("warn while unpacking: %s\n", archive_error_string(a)); + if (r < ARCHIVE_WARN) { + printf("error while unpacking: %s\n", archive_error_string(a)); + + lua_pop(L, 1); + + char *error_msg = malloc(256); + + lua_pushnil(L); + + if (!error_msg) { + lua_pushstring(L, "error while unpacking and failed to malloc to push " + "real error message"); + printf("error while unpacking and failed to push real error message\n"); + + return 2; + } + + snprintf(error_msg, 256, "error while unpacking: %s", + archive_error_string(a)); + + lua_pushstring(L, error_msg); + + free(error_msg); + + return 2; + } + + const char *cur = archive_entry_pathname(entry); + + if (cur) { + size_t need = strlen(dest) + strlen(cur) + 2; + + char *full = malloc(need); + + if (!full) { + lua_pushnil(L); + lua_pushstring(L, "failed to malloc entry path\n"); + + archive_read_close(a); + archive_read_free(a); + + return 2; + } + + snprintf(full, need, "%s/%s", dest, cur); + archive_entry_set_pathname(entry, full); + } + + r = archive_read_extract(a, entry, 0); + + if (r < ARCHIVE_WARN) { + lua_pushnil(L); + lua_pushfstring( + L, "failed to extract '%s': %s", archive_entry_pathname(entry), + archive_error_string(a) ? archive_error_string(a) : "unknown"); + archive_read_close(a); + archive_read_free(a); + + return 2; + } + } + + archive_read_close(a); + archive_read_free(a); + + lua_pushboolean(L, 1); + + return 1; +} + +luaL_Reg libpow[] = {{"extract", pow_lua_extract}, {NULL, NULL}}; int pow_lua_load(lua_State *L) { luaL_newlib(L, libpow); @@ -7,7 +7,8 @@ #include "stdio.h" #include <lua.h> -int repo_check_package(pow_repo **repos, size_t repo_count, const char *query) { +int pow_repo_get_package(pow_repo **repos, size_t repo_count, pow_package *pkg, + const char *query) { if (!repos) { printf("no repos defined in your config, please define them\n"); @@ -21,7 +22,7 @@ int repo_check_package(pow_repo **repos, size_t repo_count, const char *query) { char *package_path = malloc(PATH_MAX); if (!package_path) { - printf("failed to allocate package_path, sizeof %zu\n", PATH_MAX); + printf("failed to allocate package_path, sizeof %d\n", PATH_MAX); return 1; } @@ -49,18 +50,35 @@ int repo_check_package(pow_repo **repos, size_t repo_count, const char *query) { printf("found package %s in repo %s\n", query, repos[i]->path); - pow_package *pkg = malloc(sizeof(pow_package)); + pow_package_open(pkg, "./testing/samurai/1.3.lua"); - package_open(pkg, "./testing/samurai/1.3.lua"); + closedir(dp); + } - printf("Name: %s\n" - "Version: %s\n", - pkg->name, pkg->version); + return 0; +} - package_close(pkg); +int pow_repo_check_package(pow_repo **repos, size_t repo_count, + const char *query) { + int err; - closedir(dp); + pow_package *pkg = malloc(sizeof(pow_package)); + + if (!pkg) { + printf("failed to malloc pow_package\n"); + + return 1; + } + + err = pow_repo_get_package(repos, repo_count, pkg, query); + + if (err) { + return 1; } + printf("Name: %s\n" + "Version: %s\n", + pkg->name, pkg->version); + return 0; } @@ -1,6 +1,7 @@ #ifndef _POW_REPO_H #define _POW_REPO_H +#include "package.h" #include "stddef.h" #define POW_X86_64 1 @@ -10,6 +11,9 @@ typedef struct { const char *path; } pow_repo; -int repo_check_package(pow_repo **repos, size_t repo_count, const char *query); +int pow_repo_check_package(pow_repo **repos, size_t repo_count, + const char *query); +int pow_repo_get_package(pow_repo **repos, size_t repo_count, pow_package *pkg, + const char *query); #endif |
