From 0a05ba9a69d86c67af6f8adcb4fdab28360292dd Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Wed, 4 Jan 2023 22:57:39 +0100 Subject: cleanup, add per project token, improve build-id loading --- build_litexl.sh | 16 ---------------- build_polecat.sh | 30 ------------------------------ src/CMakeLists.txt | 12 ++++++++---- src/build.c | 9 +++++---- src/config.c | 35 ++++++++++++++--------------------- src/config.h | 4 +++- src/context.c | 18 ++++++++++-------- src/context.h | 2 -- src/parser.c | 35 ++++++++++++++++++----------------- src/ui.c | 25 +++++++++++++++++-------- 10 files changed, 75 insertions(+), 111 deletions(-) delete mode 100755 build_litexl.sh delete mode 100755 build_polecat.sh diff --git a/build_litexl.sh b/build_litexl.sh deleted file mode 100755 index 42a91b1..0000000 --- a/build_litexl.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash -set -e - -TEMP_DIR="$(mktemp -d)" -function cleanup { - rm -rf $TEMP_DIR -} -trap cleanup EXIT - -cd $TEMP_DIR - -git clone https://github.com/lite-xl/lite-xl repo -cd repo - -meson build -Dwrap_mode=forcefallback -ninja -C build diff --git a/build_polecat.sh b/build_polecat.sh deleted file mode 100755 index 545226a..0000000 --- a/build_polecat.sh +++ /dev/null @@ -1,30 +0,0 @@ -set -e - -TEMP_DIR="$(mktemp -d)" -function cleanup { - rm -rf $TEMP_DIR -} -trap cleanup EXIT - -cd $TEMP_DIR - -echo ======== CLONING ======== - -git clone https://github.com/Jan200101/polecat repo -cd repo - -echo ======== BUILDING ======== - -mkdir build -cd build -cmake .. -DBUILD_MOCK=ON -DENABLE_LUTRIS=ON -make - -echo ======== RUNNING TESTS ======== - -export XDG_DATA_HOME="$TEMP_DIR/data" - -./polecat_mock wine list -./polecat_mock wine download mock -./polecat_mock wine run mock test -./polecat_mock wine remove mock diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9f7e945..61606ba 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,13 +21,17 @@ list(APPEND ) set(CFLAGS - -Wall -Wextra -pedantic - -Wconversion -Wshadow -Wstrict-aliasing - -Winit-self -Wcast-align -Wpointer-arith - -Wmissing-declarations -Wmissing-include-dirs + -Wall -Wextra -Wfloat-equal + -Wstrict-overflow=5 -Wunreachable-code + -Wundef -Wcast-qual -Wconversion + -Wswitch-default -Wmissing-include-dirs + -Wshadow -Wstrict-aliasing -Winit-self + -Wcast-align -Wpointer-arith -Wno-unused-parameter -Wuninitialized ) + + set(CMAKE_EXECUTABLE_SUFFIX ".cgi") add_executable(${CMAKE_PROJECT_NAME} ${SOURCES}) diff --git a/src/build.c b/src/build.c index 0c4214f..8d7f369 100644 --- a/src/build.c +++ b/src/build.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "config.h" #include "fs.h" @@ -59,10 +60,10 @@ void create_build() if (!current_project) return; - int build_id = 0; + uintmax_t build_id = 0; if (current_project->build_count > 0) - build_id = atoi(current_project->builds[0].name)+1; + build_id = strtoumax(current_project->builds[0].name, NULL, 10)+1; if (build_id <= 0) build_id = 1; @@ -74,7 +75,7 @@ void create_build() build.status = STATUS_INPROGRESS; size_t name_len = 1; - int temp = build_id; + uintmax_t temp = build_id; while (temp > 10) { @@ -83,7 +84,7 @@ void create_build() } build.name = malloc((name_len + 1) * sizeof(char)); - sprintf(build.name, "%d", build_id); + sprintf(build.name, "%ld", build_id); write_build(current_project->name, build.name, &build); diff --git a/src/config.c b/src/config.c index 4eb5621..06d82f4 100644 --- a/src/config.c +++ b/src/config.c @@ -29,9 +29,10 @@ struct build_t* current_build = NULL; void init_config() { - config.cache_dir = NULL; + config.path_prefix = NULL; config.token = NULL; - config.projects = malloc(1); + + config.projects = NULL; config.project_count = 0; parse_config(); @@ -67,34 +68,26 @@ void init_config() void deinit_config() { - if (config.cache_dir) - free(config.cache_dir); - - if (config.token) - free(config.token); + free(config.path_prefix); + free(config.token); if (config.projects) { for (size_t i = 0; i < config.project_count; ++i) { - if (config.projects[i].name) - free(config.projects[i].name); + free(config.projects[i].name); - if (config.projects[i].script_path) - free(config.projects[i].script_path); + free(config.projects[i].script_path); - if (config.projects[i].description) - free(config.projects[i].description); + free(config.projects[i].description); if (config.projects[i].builds) { for (size_t j = 0; j < config.projects[i].build_count; ++j) { - if (config.projects[i].builds[j].name) - free(config.projects[i].builds[j].name); + free(config.projects[i].builds[j].name); - if (config.projects[i].builds[j].log) - free(config.projects[i].builds[j].log); + free(config.projects[i].builds[j].log); } free(config.projects[i].builds); @@ -130,10 +123,10 @@ char* project_dir(char* project) if (!cache) return cache; - size_t size = strlen(cache) + 1 + strlen(project) ; - cache = realloc(cache, size+1); - strncat(cache, "/", size); - strncat(cache, project, size); + size_t size = strlen(cache) + 1 + strlen(project) + 1; + cache = realloc(cache, size); + strncat(cache, "/", size - strlen(cache) - 1); + strncat(cache, project, size - strlen(cache) - 1); cache[size] = '\0'; return cache; diff --git a/src/config.h b/src/config.h index 8cc27a0..6997c03 100644 --- a/src/config.h +++ b/src/config.h @@ -25,14 +25,16 @@ struct project_t { char* name; char* script_path; char* description; + char* token; struct build_t* builds; size_t build_count; }; struct config_t { - char* cache_dir; + char* path_prefix; char* token; + struct project_t* projects; size_t project_count; }; diff --git a/src/context.c b/src/context.c index 4b99bea..c8f36e1 100644 --- a/src/context.c +++ b/src/context.c @@ -1,3 +1,6 @@ +#include +#include + #include "env.h" #include "parser.h" #include "context.h" @@ -6,9 +9,11 @@ struct context_t context; void init_context() { - context.document_root = getenv_default("DOCUMENT_ROOT", "/"); - context.raw_path = getenv_default("PATH_INFO", "/"); - context.path = malloc(1); + context.raw_path = getenv("PATH_INFO"); + if (!context.raw_path || !strlen(context.raw_path)) + context.raw_path = getenv_default("SCRIPT_NAME", "/"); + + context.path = NULL; context.path_length = 0; context.project = NULL; @@ -55,13 +60,10 @@ void deinit_context() { for (size_t i = 0; i < context.path_length; ++i) { - if (context.path[i]) - free(context.path[i]); + free(context.path[i]); } free(context.path); - - if (context.token) - free(context.token); + free(context.token); } } diff --git a/src/context.h b/src/context.h index 80c7e72..4d89497 100644 --- a/src/context.h +++ b/src/context.h @@ -5,8 +5,6 @@ #include struct context_t { - char* document_root; - char* raw_path; char** path; /* NULLABLE */ diff --git a/src/parser.c b/src/parser.c index f13cbe5..90705ce 100644 --- a/src/parser.c +++ b/src/parser.c @@ -74,7 +74,7 @@ void parse_config() ++tail; } - head = tail++; + head = ++tail; continue; } else if (*tail == '=') @@ -97,16 +97,16 @@ void parse_config() if (key && value) { - if (!strcmp(key, "cache-dir")) - { - assert(!config.cache_dir); - config.cache_dir = strdup(value); - } if (!strcmp(key, "token")) { - assert(!config.token); + free(config.token); config.token = strdup(value); } + else if (!strcmp(key, "path-prefix")) + { + free(config.path_prefix); + config.path_prefix = strdup(value); + } else if (!strcmp(key, "project.name")) { ++config.project_count; @@ -117,14 +117,13 @@ void parse_config() config.projects[config.project_count-1].name = strdup(value); config.projects[config.project_count-1].script_path = NULL; config.projects[config.project_count-1].description = NULL; + config.projects[config.project_count-1].token = NULL; } else if (!strcmp(key, "project.script")) { if (config.project_count > 0) { - if (config.projects[config.project_count-1].script_path) - free(config.projects[config.project_count-1].script_path); - + free(config.projects[config.project_count-1].script_path); config.projects[config.project_count-1].script_path = strdup(value); } } @@ -132,12 +131,18 @@ void parse_config() { if (config.project_count > 0) { - if (config.projects[config.project_count-1].description) - free(config.projects[config.project_count-1].description); - + free(config.projects[config.project_count-1].description); config.projects[config.project_count-1].description = strdup(value); } } + else if (!strcmp(key, "project.token")) + { + if (config.project_count > 0) + { + free(config.projects[config.project_count-1].token); + config.projects[config.project_count-1].token = strdup(value); + } + } key = NULL; value = NULL; @@ -265,9 +270,6 @@ void load_full_build(struct project_t* project, struct build_t* build) void parse_path(const char* path) { - size_t path_len = strlen(path) + 1; - const char* const orig_path = path; - if (path[0] != '/') return; @@ -275,7 +277,6 @@ void parse_path(const char* path) while(*path) { - assert(orig_path+path_len >= path); ++path; if (*path == '/' || *path == '\0') { diff --git a/src/ui.c b/src/ui.c index 027e5de..fdfc945 100644 --- a/src/ui.c +++ b/src/ui.c @@ -57,7 +57,11 @@ void print_html() } else if (!strcmp(context.action, "trigger")) { - if (context.token && config.token && !strcmp(context.token, config.token)) + char* token = current_project->token; + if (!token) + token = config.token; + + if (context.token && token && !strcmp(context.token, token)) { create_build(); @@ -119,7 +123,7 @@ void print_head() && context.token && config.token && !strcmp(context.token, config.token)) printf("", current_project->name); - printf(""); + printf("", config.path_prefix ? config.path_prefix : ""); printf(""); } @@ -127,11 +131,14 @@ void print_title() { printf("

"); - printf("CGCI", context.project != NULL ? "href=\"/\"" : ""); + printf("CGCI"); if (context.project) { - printf(" : %s", context.project, context.project); + printf(" : %s", config.path_prefix ? config.path_prefix : "", context.project, context.project); if (context.action) { printf(" : %s", context.action); @@ -152,7 +159,7 @@ void print_build_nav() current_project->name ); - if (config.token && current_project->script_path && strlen(current_project->script_path)) + if ((config.token || current_project->token) && current_project->script_path && strlen(current_project->script_path)) { printf( "" @@ -296,11 +303,12 @@ void print_project_nav() "" "" "" - "Projects" + "Projects" "" "" "" - "" + "", + config.path_prefix ? config.path_prefix : "" ); } @@ -339,13 +347,14 @@ void print_project_list() printf( "" "" - "%s" + "%s" "" "%s" "%s" "%s" "%s" "", + config.path_prefix ? config.path_prefix : "", project->name, project->name, project->description ? project->description : "", time, buildtime, -- cgit v1.2.3