diff --git a/apr-install-script.sh b/apr-install-script.sh index f7a202e..870d3ba 100755 --- a/apr-install-script.sh +++ b/apr-install-script.sh @@ -16,9 +16,8 @@ cd apr-1.4.5 make sudo make install -# reset and cleanup +# reset cd /tmp -rm -rf apr-1.4.5 apr-1.4.5.tar.gz # do the same with apr-util curl -L -O http://download.nextag.com/apache/apr/apr-util-1.3.12.tar.gz @@ -37,4 +36,4 @@ sudo make install # cleanup cd /tmp -rm -rf apr-util-1.3.12* apr-1.4.5* +#rm -rvf apr-util-1.3.12* apr-1.4.5* diff --git a/devpkg/Makefile b/devpkg/Makefile index abaa64b..4a1f3e3 100644 --- a/devpkg/Makefile +++ b/devpkg/Makefile @@ -1,5 +1,5 @@ PREFIX?=/usr/local -CFLAGS=-g -Wall -I${PREFIX}/apr/include/apr-1 -I${PREFIX}/apr/include/apr-util-1 +CFLAGS=-g -Wall -I${PREFIX}/apr/include/apr-1 LDFLAGS=-lapr-1 -pthread -laprutil-1 all: devpkg diff --git a/devpkg/apr_errno_test.c b/devpkg/apr_errno_test.c new file mode 100644 index 0000000..7a9be27 --- /dev/null +++ b/devpkg/apr_errno_test.c @@ -0,0 +1,7 @@ +#include + + +int main(int argc, char *argv[]) +{ + return 0; +} diff --git a/devpkg/db.c b/devpkg/db.c new file mode 100644 index 0000000..3a5c333 --- /dev/null +++ b/devpkg/db.c @@ -0,0 +1,124 @@ +#include +#include +#include + +#include "db.h" +#include "bstrlib.h" +#include "dbg.h" + +static FILE *DB_open(const char *path, const char *mode) +{ + return fopen(path, mode); +} + + +static void DB_close(FILE *db) +{ + fclose(db); +} + + +static bstring DB_load(const char *path) +{ + FILE *db = NULL; + bstring data = NULL; + + db = DB_open(DB_FILE, "r"); + check(db, "Failed to open database: %s", DB_FILE); + + data = bread((bNread)fread, db); + check(data, "Failed to read from db file: %s", DB_FILE); + + DB_close(db); + return data; + +error: + if(db) DB_close(db); + if(data) bdestroy(data); + return NULL; +} + + +int DB_update(const char *url) +{ + if(DB_find(url)) { + log_info("Already recorded as installed: %s", url); + } + + FILE *db = DB_open(DB_FILE, "a+"); + check(db, "Failed to open DB file: %s", DB_FILE); + + bstring line = bfromcstr(url); + bconchar(line, '\n'); + int rc = fwrite(line->data, blength(line), 1, db); + check(rc == 1, "Failed to append to the db."); + + return 0; +error: + if(db) DB_close(db); + return -1; +} + + +int DB_find(const char *url) +{ + bstring data = NULL; + bstring line = bfromcstr(url); + int res = -1; + + data = DB_load(DB_FILE); + check(data, "Failed to load: %s", DB_FILE); + + if(binstr(data, 0, line) == BSTR_ERR) { + res = 0; + } else { + res = 1; + } + +error: //fallthrough + if(data) bdestroy(data); + if(line) bdestroy(line); + return res; +} + + +int DB_init() +{ + apr_pool_t *p = NULL; + apr_pool_initialize(); + apr_pool_create(&p, NULL); + + if(access(DB_DIR, W_OK | X_OK) == -1) { + apr_status_t rc = apr_dir_make_recursive(DB_DIR, + APR_UREAD | APR_UWRITE | APR_UEXECUTE | + APR_GREAD | APR_GWRITE | APR_GEXECUTE, p); + check(rc == APR_SUCCESS, "Failed to make database dir: %s", DB_DIR); + } + + if(access(DB_FILE, W_OK) == -1) { + FILE *db = DB_open(DB_FILE, "w"); + check(db, "Cannot open database: %s", DB_FILE); + DB_close(db); + } + + apr_pool_destroy(p); + return 0; + +error: + apr_pool_destroy(p); + return -1; +} + + +int DB_list() +{ + bstring data = DB_load(DB_FILE); + check(data, "Failed to read load: %s", DB_FILE); + + printf("%s", bdata(data)); + bdestroy(data); + return 0; + +error: + return -1; +} diff --git a/devpkg/db.h b/devpkg/db.h new file mode 100644 index 0000000..6318d62 --- /dev/null +++ b/devpkg/db.h @@ -0,0 +1,13 @@ +#ifndef _db_h +#define _db_h + +#define DB_FILE "/usr/local/.devpkg/db" +#define DB_DIR "/usr/local/.devpkg" + + +int DB_init(); +int DB_list(); +int DB_update(const char *url); +int DB_find(const char *url); + +#endif diff --git a/devpkg/shell.c b/devpkg/shell.c new file mode 100644 index 0000000..054903f --- /dev/null +++ b/devpkg/shell.c @@ -0,0 +1,113 @@ +#include "shell.h" +#include "dbg.h" +#include + +int Shell_exec(Shell template, ...) +{ + apr_pool_t *p = NULL; + apr_pool_create(&p, NULL); + + va_list argp; + const char *key = NULL; + const char *arg = NULL; + int i = 0; + + va_start(argp, template); + + for(key = va_arg(argp, const char*); + key != NULL; + key = va_arg(argp, const char *)) + { + arg = va_arg(argp, const char *); + + for(i = 0; template.args[i] != NULL; i++) { + if(strcmp(template.args[i], key) == 0) { + template.args[i] = arg; + break; // found it + } + } + } + + int rc = Shell_run(p, &template); + apr_pool_create(&p, NULL); + va_end(argp); + return rc; +} + + +int Shell_run(apr_pool_t *p, Shell *cmd) +{ + apr_procattr_t *attr; + apr_status_t rv; + apr_proc_t newproc; + + rv = apr_procattr_create(&attr, p); + check(rv == APR_SUCCESS, "Failed to create proc attr."); + + rv = apr_procattr_io_set(attr, APR_NO_PIPE, APR_NO_PIPE, + APR_NO_PIPE); + check(rv == APR_SUCCESS, "Failed to set IO of command."); + + rv = apr_procattr_dir_set(attr, cmd->dir); + check(rv == APR_SUCCESS, "Failed to set root to %s", cmd->dir); + + rv = apr_procattr_cmdtype_set(attr, APR_PROGRAM_PATH); + check(rv == APR_SUCCESS, "Failed to set cmd type."); + + rv = apr_proc_create(&newproc, cmd->exe, cmd->args, NULL, attr, p); + check(rv == APR_SUCCESS, "Failed to run command."); + + rv = apr_proc_wait(&newproc, &cmd->exit_code, &cmd->exit_why, APR_WAIT); + check(rv == APR_CHILD_DONE, "Failed to wait."); + + check(cmd->exit_code == 0, "%s exited badly.", cmd->exe); + check(cmd->exit_why == APR_PROC_EXIT, "%s was killed or crashed", cmd->exe); + + return 0; + +error: + return -1; +} + +Shell CLEANUP_SH = { + .exe = "rm", + .dir = "/tmp", + .args = {"rm", "-rf", "/tmp/pkg-build", "/tmp/pkg-src.tar.gz", + "/tmp/pkg-src.tar.bz2", "/tmp/DEPENDS", NULL} +}; + +Shell GIT_SH = { + .dir = "/tmp", + .exe = "git", + .args = {"git", "clone", "URL", "pkg-build", NULL} +}; + +Shell TAR_SH = { + .dir = "/tmp/pkg-build", + .exe = "tar", + .args = {"tar", "-xzf", "FILE", "--strip-components", "1", NULL} +}; + +Shell CURL_SH = { + .dir = "/tmp", + .exe = "curl", + .args = {"curl", "-L", "-o", "TARGET", "URL", NULL} +} + +Shell CONFIGURE_SH = { + .exe = "./configure", + .dir = "/tmp/pkg-build", + .args = {"configure", "OPTS", NULL} +}; + +Shell MAKE_SH = { + .exe = "make", + .dir = "/tmp/pkg-build", + .args = {"make", "OPTS", NULL} +}; + +Shell INSTALL_SH = { + .exe = "sudo", + .dir = "/tmp/pkg-build", + .args = {"sudo", "make", "TARGET", NULL} +}; diff --git a/devpkg/shell.h b/devpkg/shell.h new file mode 100644 index 0000000..a4301d3 --- /dev/null +++ b/devpkg/shell.h @@ -0,0 +1,31 @@ +#ifndef _shell_h +#define _shell_h + +#define MAX_COMMAND_ARGS 100 + +#include + +typedef struct Shell { + const char *dir; + const char *exe; + + apr_procattr_t *attr; + apr_proc_t proc; + apr_exit_why_e exit_why; + int exit_code; + + const char *args[MAX_COMMAND_ARGS]; +} Shell; + +int Shell_run(apr_pool_t *p, Shell *cmd); +int Shell_exec(Shell cmd, ...); + +extern Shell CLEANUP_SH; +extern Shell GIT_SH; +extern Shell TAR_SH; +extern Shell CURL_SH; +extern Shell CONFIGURE_SH; +extern Shell MAKE_SH; +extern Shell INSTALL_SH; + +#endif