aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile4
-rw-r--r--src/build.c46
-rw-r--r--src/download.c40
-rw-r--r--src/download.h1
-rw-r--r--src/main.c13
-rw-r--r--src/package.c8
-rw-r--r--src/paths.c28
-rw-r--r--src/paths.h1
8 files changed, 133 insertions, 8 deletions
diff --git a/Makefile b/Makefile
index 158a7b9..70e3f42 100644
--- a/Makefile
+++ b/Makefile
@@ -6,8 +6,8 @@ RM = rm -rf
CFLAGS=-Wall -Wpedantic
-LIBFLAGS=$(shell pkg-config --cflags --libs lua55) $(shell pkg-config --cflags --libs libarchive)
-DEBUG_CFLAGS=-g -fsanitize=address
+LIBFLAGS=$(shell pkg-config --cflags --libs lua55) $(shell pkg-config --cflags --libs libarchive) $(shell pkg-config --cflags --libs libcurl)
+DEBUG_CFLAGS=-g
SRC_DIR=src
OUT_DIR=out
diff --git a/src/build.c b/src/build.c
index d680cf7..855b38b 100644
--- a/src/build.c
+++ b/src/build.c
@@ -1,5 +1,47 @@
#include "build.h"
-
#include "package.h"
+#include "paths.h"
+
+#include "lauxlib.h"
+#include "lua.h"
+#include "lualib.h"
+#include "stdio.h"
+#include "unistd.h"
+
+int pow_package_build(pow_package *pkg) {
+ if (pkg->pkg_ref == LUA_NOREF) {
+ printf("error! pkg_ref is equal to LUA_NOREF\n");
+
+ return 1;
+ }
+
+ lua_rawgeti(pkg->L, LUA_REGISTRYINDEX, pkg->pkg_ref);
+
+ char *tmp_path = NULL;
+
+ int err = pow_create_dir_for_package(&tmp_path);
+
+ if (err) {
+ printf("error creating temp dir for package\n");
+
+ return 1;
+ }
+
+ printf("%s\n", tmp_path);
+
+ if ((err = chdir(tmp_path))) {
+ printf("uh oh oops %d\n", err);
+ }
+
+ lua_getfield(pkg->L, -1, "build");
+
+ if (lua_pcall(pkg->L, 0, 0, 0) != LUA_OK) {
+ printf("error running script: %s\n", lua_tostring(pkg->L, -1));
+
+ return 1;
+ }
+
+ fflush(stdout);
-int pow_package_build(pow_package *pkg) { return 0; }
+ return 0;
+}
diff --git a/src/download.c b/src/download.c
new file mode 100644
index 0000000..bb8365c
--- /dev/null
+++ b/src/download.c
@@ -0,0 +1,40 @@
+#include "curl/curl.h"
+#include "stdio.h"
+
+static size_t write_cb(char *ptr, size_t size, size_t nmemb, void *stream) {
+ size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
+
+ return written;
+}
+
+int pow_download_file(char *path, char *url) {
+ FILE *pagefile;
+ CURL *curl = curl_easy_init();
+
+ if (!curl) {
+ printf("failed to init curl\n");
+
+ return 1;
+ }
+
+ curl_easy_setopt(curl, CURLOPT_URL, url);
+ curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
+
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
+
+ pagefile = fopen(path, "wb");
+
+ if (!pagefile) {
+ curl_easy_cleanup(curl);
+
+ return 1;
+ }
+
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, pagefile);
+
+ int result = curl_easy_perform(curl);
+
+ fclose(pagefile);
+
+ return result;
+}
diff --git a/src/download.h b/src/download.h
new file mode 100644
index 0000000..c2db32b
--- /dev/null
+++ b/src/download.h
@@ -0,0 +1 @@
+int pow_download_file(char *path, char *url);
diff --git a/src/main.c b/src/main.c
index 9c38b18..4301657 100644
--- a/src/main.c
+++ b/src/main.c
@@ -3,10 +3,13 @@
#include "config.h"
#include "repo.h"
+#include "curl/curl.h"
#include "stdio.h"
#include "stdlib.h"
int main(int argc, char *argv[]) {
+ curl_global_init(CURL_GLOBAL_ALL);
+
pow_args args = {0};
pow_args_parse(argc, argv, &args);
@@ -14,6 +17,8 @@ int main(int argc, char *argv[]) {
if (args.flags & POW_FLAG_HELP) {
pow_args_print_help();
+ curl_global_cleanup();
+
return 0;
}
@@ -28,9 +33,11 @@ int main(int argc, char *argv[]) {
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)) {
+ if (pow_repo_get_package(config.repos, config.repos_count, pkg,
+ args.package)) {
printf("error getting package\n");
+
+ return 1;
}
pow_package_build(pkg);
@@ -46,5 +53,7 @@ int main(int argc, char *argv[]) {
fflush(stdout);
+ curl_global_cleanup();
+
return 0;
}
diff --git a/src/package.c b/src/package.c
index 832e7da..98d6f00 100644
--- a/src/package.c
+++ b/src/package.c
@@ -36,11 +36,13 @@ int extract_package_info(lua_State *L, pow_package *pkg) {
pkg->version = strdup(lua_tostring(L, -1));
+ lua_pop(L, 1);
+
if (!pkg->version) {
printf("strdup failed trying to get package name\n");
- }
- pkg->pkg_ref = luaL_ref(L, LUA_REGISTRYINDEX);
+ return 1;
+ }
return 0;
}
@@ -113,7 +115,9 @@ int pow_package_open(pow_package *pkg, const char *path) {
}
extract_package_info(L, pkg);
+
pkg->path = path;
+ pkg->pkg_ref = luaL_ref(L, LUA_REGISTRYINDEX);
return 0;
}
diff --git a/src/paths.c b/src/paths.c
new file mode 100644
index 0000000..20af56d
--- /dev/null
+++ b/src/paths.c
@@ -0,0 +1,28 @@
+#include "paths.h"
+
+#include "stdio.h"
+#include "stdlib.h"
+#include "string.h"
+
+char template[] = "/tmp/pow/XXXXXX";
+
+int pow_create_dir_for_package(char **path) {
+ char *new_path = malloc(strlen(template) + 1);
+
+ if (!new_path) {
+ return 1;
+ }
+
+ strcpy(new_path, template);
+
+ if (!mkdtemp(new_path)) {
+ free(new_path);
+ return 1;
+ }
+
+ printf("%s\n", new_path);
+
+ *path = new_path;
+
+ return 0;
+}
diff --git a/src/paths.h b/src/paths.h
new file mode 100644
index 0000000..5f77932
--- /dev/null
+++ b/src/paths.h
@@ -0,0 +1 @@
+int pow_create_dir_for_package(char **path);