diff options
| author | dacctal <donotcontactmevia@email.invalid> | 2026-07-18 07:16:31 +0000 |
|---|---|---|
| committer | dacctal <donotcontactmevia@email.invalid> | 2026-07-18 07:16:31 +0000 |
| commit | 71b2cbcf9d1eb730fec8aa64436d7ea59d7cec08 (patch) | |
| tree | cfd990e272d854063e6f4e98b686f3c29d7d13b4 | |
| parent | 7b5719b26119545e8877f2144c1476ec97d00abc (diff) | |
fix lua vm bugs, remove git repo when one already exists
| -rw-r--r-- | Makefile | 5 | ||||
| -rw-r--r-- | include/files.h | 1 | ||||
| -rw-r--r-- | src/build.c | 65 | ||||
| -rw-r--r-- | src/fetch.c | 34 | ||||
| -rw-r--r-- | src/files.c | 144 | ||||
| -rw-r--r-- | src/lua_globs.c | 40 | ||||
| -rw-r--r-- | src/pkg_install.c | 85 | ||||
| -rw-r--r-- | src/pkg_remove.c | 86 |
8 files changed, 271 insertions, 189 deletions
@@ -22,7 +22,7 @@ PREFIX ?= /usr/local OBJDIR = obj SRCS = $(wildcard src/*.c) OBJS = $(SRCS:src/%.c=$(OBJDIR)/%.o) -CFLAGS += $(shell pkg-config --cflags luajit) -I./include -Wno-format-truncation -std=c99 -D_XOPEN_SOURCE=700 +CFLAGS += $(shell pkg-config --cflags luajit) -I./include -Wno-format-truncation -std=c99 # pretty print for compilation. To enable verbose (show commands), run with # `make V=1` @@ -47,6 +47,9 @@ $(OBJDIR)/%.o: src/%.c | $(OBJDIR) $(E) " CC " $@ $(Q) ${CC} $(CFLAGS) -c -o $@ $< +gdb: CFLAGS += -g -O0 -Wall -Wextra -Werror -Wvla -pedantic +gdb: pkgit + debug: CFLAGS += -g -O0 -Wall -Wextra -Werror -Wvla -pedantic -fsanitize=address debug: LDFLAGS += -fsanitize=address debug: pkgit diff --git a/include/files.h b/include/files.h index 8054bd8..9a17c88 100644 --- a/include/files.h +++ b/include/files.h @@ -29,5 +29,6 @@ bool file_exists(const char *path); bool is_directory(const char *path); str cmd_out(const char *cmd); void cpdir(const char *src_path, const char *dst_path); +int remove_tree(const char *path); #endif diff --git a/src/build.c b/src/build.c index 769106a..72b1e40 100644 --- a/src/build.c +++ b/src/build.c @@ -24,17 +24,20 @@ #include "globs.h" #include "log.h" +#include "lua.h" #include "pkg.h" #include "pkgit_lua.h" -bool target_build(lua_State *L, char* lua_file, str *target) { - if (!lua_try_table(L, lua_file, target->data)) return false; +bool target_build(lua_State *L, char *lua_file, str *target) { + if (!lua_try_table(L, lua_file, target->data)) + return false; if (lua_try_table(L, lua_file, "dependencies")) { lua_pushnil(L); install_dependencies(L); } lua_pop(L, 1); - if (!lua_try_function(L, lua_file, "build")) return false; + if (!lua_try_function(L, lua_file, "build")) + return false; lua_pop(L, 1); return true; } @@ -61,15 +64,16 @@ bool repo_build(package_t *pkg) { bool bldit(package_t *pkg) { init_bldit_state(); - if (!bldit_loaded) return false; + if (!bldit_loaded) + return false; lua_getglobal(B, "dependencies"); if (!lua_istable(B, -1)) { bldit_isnt_type("dependencies", "table"); } else { lua_pushnil(B); install_dependencies(B); + lua_pop(B, 1); } - lua_pop(B, 1); lua_getglobal(B, "targets"); if (!lua_istable(B, -1)) { bldit_isnt_type("targets", "table"); @@ -78,8 +82,9 @@ bool bldit(package_t *pkg) { return false; } bool target_success = target_build(B, "bldit.lua", &pkg->target); - lua_pop(B, 2); - lua_close(B); + lua_pop(B, 1); + // lua_pop(B, 2); + // lua_close(B); return target_success; } @@ -108,9 +113,11 @@ bool config_build(package_t *pkg) { } str_free(&file_path); str_free(&key); - if (!lua_try_table(L, "init.lua", "targets")) continue; + if (!lua_try_table(L, "init.lua", "targets")) + continue; target_success = target_build(L, "init.lua", &pkg->target); - if (target_success) break; + if (target_success) + break; lua_pop(L, 1); } lua_pop(L, 1); @@ -118,26 +125,42 @@ bool config_build(package_t *pkg) { } bool build_loop(package_t *pkg) { - if (flags.verbose) log_info("attempting init.lua: 'repositories.%.*s.build'", str_fmt(&pkg->name)); - if (repo_build(pkg)) { return true; } - if (flags.verbose) log_warn("failed init.lua: 'repositories.%.*s.build'", str_fmt(&pkg->name)); - - if (flags.verbose) log_info("attempting bldit.lua"); - if (bldit(pkg)) { return true; } - if (flags.verbose) log_warn("failed bldit.lua"); + if (flags.verbose) + log_info("attempting init.lua: 'repositories.%.*s.build'", + str_fmt(&pkg->name)); + if (repo_build(pkg)) { + return true; + } + if (flags.verbose) + log_warn("failed init.lua: 'repositories.%.*s.build'", + str_fmt(&pkg->name)); + + if (flags.verbose) + log_info("attempting bldit.lua"); + if (bldit(pkg)) { + return true; + } + if (flags.verbose) + log_warn("failed bldit.lua"); - if (flags.verbose) log_info("attempting init.lua: 'build_systems'"); - if (config_build(pkg)) { return true; } - if (flags.verbose) log_warn("failed init.lua: 'build_systems'"); + if (flags.verbose) + log_info("attempting init.lua: 'build_systems'"); + if (config_build(pkg)) { + return true; + } + if (flags.verbose) + log_warn("failed init.lua: 'build_systems'"); return false; } bool build(package_t *pkg) { char cwd[MAX_PATH_LEN]; getcwd(cwd, MAX_PATH_LEN); - if (!str_equal_cstr(&pkg->src, cwd) && !pkg->is_local) chdir(pkg->src.data); + if (!str_equal_cstr(&pkg->src, cwd) && !pkg->is_local) + chdir(pkg->src.data); - if (build_loop(pkg)) return true; + if (build_loop(pkg)) + return true; log_error("no usable build system was found for %.*s", str_fmt(&pkg->name)); return false; } diff --git a/src/fetch.c b/src/fetch.c index b2c74ef..af56cbf 100644 --- a/src/fetch.c +++ b/src/fetch.c @@ -12,28 +12,43 @@ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. */ #include <fcntl.h> -#include <stdio.h> -#include <string.h> #include <sys/wait.h> #include <unistd.h> #include "fetch.h" - +#include "files.h" #include "globs.h" #include "log.h" +#include "str.h" bool fetch(package_t *pkg) { if (pkg->url.data == NULL || pkg->src.data == NULL) { - if (flags.verbose) log_warn("invalid pkg: url or src is NULL"); + if (flags.verbose) + log_warn("invalid pkg: url or src is NULL"); return false; } + + str s = str_dupe(&pkg->src); + s.len -= pkg->version.len + 1; + s.data[s.len] = 0; + + if (is_directory(s.data) && flags.force) { + return true; + if (remove_tree(s.data)) { + log_error("could not remove %.*s", str_fmt(&s)); + return false; + } + } + + str_free(&s); + pid_t pid = fork(); if (pid < 0) { log_error("fork failed"); @@ -56,11 +71,12 @@ bool fetch(package_t *pkg) { argv[i++] = "-c"; argv[i++] = "advice.detachedHead=false"; argv[i++] = "clone"; - if (pkg->version.data != NULL && !str_equal_cstr(&pkg->version, "HEAD")) { + if (pkg->version.data != NULL && + !str_equal_cstr(&pkg->version, "HEAD")) { argv[i++] = "--branch"; argv[i++] = pkg->version.data; } - + argv[i++] = "--recursive"; argv[i++] = pkg->url.data; argv[i++] = pkg->src.data; @@ -72,7 +88,9 @@ bool fetch(package_t *pkg) { waitpid(pid, &status, 0); bool result = (WEXITSTATUS(status) == 0); - if (!result) { log_error("git clone failed"); } + if (!result) { + log_error("git clone failed"); + } return result; } diff --git a/src/files.c b/src/files.c index ec32ec2..0ce64b4 100644 --- a/src/files.c +++ b/src/files.c @@ -18,20 +18,23 @@ */ -#include "files.h" - -#include "str.h" +#define _XOPEN_SOURCE 700 #include <dirent.h> #include <errno.h> #include <fcntl.h> +#include <ftw.h> +#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <stdbool.h> #include <sys/stat.h> #include <unistd.h> +#include "files.h" +#include "log.h" +#include "str.h" + bool file_exists(const char *path) { struct stat buffer; return (stat(path, &buffer) == 0); @@ -49,46 +52,48 @@ FILE *popen(const char *command, const char *type); int pclose(FILE *stream); str cmd_out(const char *cmd) { - FILE *pipe = popen(cmd, "r"); - if (!pipe) return mstr(""); - - char buffer[128]; - size_t total_size = 0; - size_t capacity = 256; - char *result = malloc(capacity); - if (!result) { - pclose(pipe); - return mstr(""); - } - result[0] = '\0'; - - while (fgets(buffer, sizeof(buffer), pipe) != NULL) { - size_t len = strlen(buffer); - // Ensure enough space for the new chunk + 1 byte for '\0' - if (total_size + len + 1 > capacity) { - capacity *= 2; - char *new_result = realloc(result, capacity); - if (!new_result) { - free(result); - pclose(pipe); - return mstr(""); - } - result = new_result; - } - - snprintf(result + total_size, capacity - total_size, "%s", buffer); - total_size += len; - } - - pclose(pipe); - str str_result = mstr(result); - free(result); - return str_result; + FILE *pipe = popen(cmd, "r"); + if (!pipe) + return mstr(""); + + char buffer[128]; + size_t total_size = 0; + size_t capacity = 256; + char *result = malloc(capacity); + if (!result) { + pclose(pipe); + return mstr(""); + } + result[0] = '\0'; + + while (fgets(buffer, sizeof(buffer), pipe) != NULL) { + size_t len = strlen(buffer); + // Ensure enough space for the new chunk + 1 byte for '\0' + if (total_size + len + 1 > capacity) { + capacity *= 2; + char *new_result = realloc(result, capacity); + if (!new_result) { + free(result); + pclose(pipe); + return mstr(""); + } + result = new_result; + } + + snprintf(result + total_size, capacity - total_size, "%s", buffer); + total_size += len; + } + + pclose(pipe); + str str_result = mstr(result); + free(result); + return str_result; } const char *get_filename_ext(const char *filename) { const char *dot = strrchr(filename, '.'); - if (!dot || dot == filename) return ""; + if (!dot || dot == filename) + return ""; return dot + 1; } @@ -124,7 +129,8 @@ static int copy_file_content(int src_fd, int dst_fd) { return 0; } -static int copy_at(int src_dir_fd, const char *src_path, int dst_dir_fd, const char *dst_path); +static int copy_at(int src_dir_fd, const char *src_path, int dst_dir_fd, + const char *dst_path); static int copy_dir_content(int src_fd, int dst_fd) { int ret = -1; @@ -159,13 +165,14 @@ static int copy_dir_content(int src_fd, int dst_fd) { ret = 0; - cleanup1: - closedir(src_dir); - cleanup0: - return ret; +cleanup1: + closedir(src_dir); +cleanup0: + return ret; } -static int copy_at(int src_dir_fd, const char *src_path, int dst_dir_fd, const char *dst_path) { +static int copy_at(int src_dir_fd, const char *src_path, int dst_dir_fd, + const char *dst_path) { int ret = -1; int src_fd = openat(src_dir_fd, src_path, O_NOFOLLOW | O_RDONLY); if (src_fd < 0) { @@ -198,25 +205,42 @@ static int copy_at(int src_dir_fd, const char *src_path, int dst_dir_fd, const c } switch (stat.st_mode & S_IFMT) { - case S_IFDIR: - ret = copy_dir_content(src_fd, dst_fd); - break; - case S_IFREG: - ret = copy_file_content(src_fd, dst_fd); - break; - default: - fprintf(stderr, "copy_at: unsupported file inode type\n"); + case S_IFDIR: + ret = copy_dir_content(src_fd, dst_fd); + break; + case S_IFREG: + ret = copy_file_content(src_fd, dst_fd); + break; + default: + fprintf(stderr, "copy_at: unsupported file inode type\n"); } goto cleanup2; - cleanup2: - close(dst_fd); - cleanup1: - close(src_fd); - cleanup0: - return ret; +cleanup2: + close(dst_fd); +cleanup1: + close(src_fd); +cleanup0: + return ret; } void cpdir(const char *src_path, const char *dst_path) { copy_at(AT_FDCWD, src_path, AT_FDCWD, dst_path); } + +static int _remove_callback(const char *fpath, const struct stat *sb, + int typeflag, struct FTW *ftwbuf) { + (void)sb; + (void)typeflag; + (void)ftwbuf; + + int res = remove(fpath); + if (res) { + log_warn("could not remove: %s", fpath); + } + return 0; +} + +int remove_tree(const char *path) { + return nftw(path, _remove_callback, 256, FTW_DEPTH | FTW_PHYS); +} diff --git a/src/lua_globs.c b/src/lua_globs.c index d531fb8..d58512f 100644 --- a/src/lua_globs.c +++ b/src/lua_globs.c @@ -21,6 +21,7 @@ #include <stdlib.h> #include <string.h> +#include "lua.h" #include "pkgit_lua.h" #include "files.h" @@ -61,7 +62,9 @@ void init_lua_state(void) { } if (file_exists(cfg.repos.data)) { if (luaL_loadfile(L, cfg.repos.data) || lua_pcall(L, 0, 0, 0)) { - if (flags.verbose) log_warn("cannot load repository file: %s", lua_tostring(L, -1)); + if (flags.verbose) + log_warn("cannot load repository file: %s", + lua_tostring(L, -1)); lua_pop(L, 1); } } @@ -69,6 +72,7 @@ void init_lua_state(void) { config_loaded = true; } +// FIXME: ensure this is called ONCE for clarity void init_bldit_state(void) { if (B != NULL) return; @@ -95,14 +99,19 @@ void free_lua_state(void) { void free_bldit_state(void) { if (B != NULL) { - lua_close(L); + lua_close(B); B = NULL; } bldit_loaded = false; } -lua_State *get_lua_state(void) { return L; } -lua_State *get_bldit_state(void) { return B; } +lua_State *get_lua_state(void) { + return L; +} + +lua_State *get_bldit_state(void) { + return B; +} void lua_isnt_type(char *variable, char *type) { if (flags.verbose) @@ -123,18 +132,16 @@ bool lua_try_function(lua_State *L, char *lua_file, char *fname) { lua_isnt_type(fname, "function"); lua_pop(L, 1); } else if (lua_pcall(L, 0, 1, 0) != LUA_OK) { - if (flags.verbose) log_warn( - "%s: '%s' function borked: %s", - lua_file, fname, lua_tostring(L, -1) - ); + if (flags.verbose) + log_warn("%s: '%s' function borked: %s", lua_file, fname, + lua_tostring(L, -1)); lua_pop(L, 1); return false; } if (!lua_isnumber(L, -1) || lua_tonumber(L, -1) != 0) { - if (flags.verbose) log_warn( - "%s: '%s' failed: %s", - lua_file, fname, lua_tostring(L, -1) - ); + if (flags.verbose) + log_warn("%s: '%s' failed: %s", lua_file, fname, + lua_tostring(L, -1)); lua_pop(L, 1); return false; } @@ -187,13 +194,12 @@ str bldit_pkg_getver(void) { bool is_bldit_usable(void) { str bldit_version = bldit_getver(); - if ( - strcmp(bldit_version.data, "") != 0 && - strcmp(bldit_version.data, VERSION) == 0 - ) return true; + if (bldit_version.len && str_equal_cstr(&bldit_version, VERSION)) + return true; bool prev_pass = false; for (size_t i = 0; i < bldit_version.len; i++) { - if (bldit_version.data[i] == '.') continue; + if (bldit_version.data[i] == '.') + continue; if ((bldit_version.data[i] - '0') <= (VERSION[i] - '0')) { prev_pass = ((bldit_version.data[i] - '0') != (VERSION[i] - '0')); continue; diff --git a/src/pkg_install.c b/src/pkg_install.c index 6ee7e08..1f152ca 100644 --- a/src/pkg_install.c +++ b/src/pkg_install.c @@ -21,6 +21,7 @@ #include <sys/stat.h> #include <unistd.h> +#include "lua.h" #include "pkgit_lua.h" #include "add_repo.h" @@ -56,17 +57,17 @@ void install_dependencies(lua_State *L) { } } -bool target_install(lua_State *L, char* lua_file, str *target) { +bool target_install(lua_State *L, char *lua_file, str *target) { if (!lua_try_table(L, lua_file, target->data)) { lua_pop(L, 1); return false; } - //lua_try_function(L, lua_file, "pre_install"); + // lua_try_function(L, lua_file, "pre_install"); if (!lua_try_function(L, lua_file, "install")) { lua_pop(L, 1); return false; } - //lua_try_function(L, lua_file, "post_install"); + // lua_try_function(L, lua_file, "post_install"); lua_pop(L, 1); return true; } @@ -83,7 +84,8 @@ bool repo_install(package_t *pkg) { lua_pop(L, 2); return false; } - if (!lua_try_table(L, "init.lua", "targets")) return false; + if (!lua_try_table(L, "init.lua", "targets")) + return false; bool target_success = target_install(L, "init.lua", &pkg->target); lua_pop(L, 3); return target_success; @@ -97,12 +99,17 @@ bool bldit_install(package_t *pkg) { if (!is_bldit_usable()) { log_error("bldit version is newer than the installed pkgit version"); log_error("consider updating pkgit"); - if (!flags.force) return false; + if (!flags.force) + return false; } lua_pushfstring(B, "%s", inst_dirs.prefix.data); lua_setglobal(B, "prefix"); - lua_pop(B, 1); - if (!lua_try_table(B, "bldit.lua", "targets")) return false; + lua_getglobal(B, "targets"); + if (!lua_istable(L, -1)) { + // FIXME: change da message + log_error("targets is not a table in bldit.lua!"); + return false; + } bool target_success = target_install(B, "bldit.lua", &pkg->target); lua_pop(B, 2); lua_close(B); @@ -132,7 +139,8 @@ bool config_install(package_t *pkg) { } target_success = target_install(L, "init.lua", &pkg->target); lua_pop(L, 2); - if (target_success) break; + if (target_success) + break; } lua_pop(L, 1); return target_success; @@ -144,7 +152,9 @@ void pkg_install(package_t *pkg) { log_info("%.*s is already installed.", str_fmt(&pkg->name)); return; } else { - if (flags.verbose) log_warn("%.*s is already installed. reinstalling...", str_fmt(&pkg->name)); + if (flags.verbose) + log_warn("%.*s is already installed. reinstalling...", + str_fmt(&pkg->name)); } } else { mkdir(pkg->src.data, 0755); @@ -152,47 +162,52 @@ void pkg_install(package_t *pkg) { } char cwd[MAX_PATH_LEN]; getcwd(cwd, MAX_PATH_LEN); - if (str_equal_cstr(&pkg->src, cwd)) chdir(pkg->src.data); + if (str_equal_cstr(&pkg->src, cwd)) + chdir(pkg->src.data); if (pkg->is_local) { cpdir(cwd, pkg->src.data); } else { - log_pkgit("fetching " GREEN "%.*s" COLOR_RESET , str_fmt(&pkg->name)); - if (!fetch(pkg)) return; - if (flags.verbose) log_pkgit("fetched " GREEN "%.*s" COLOR_RESET , str_fmt(&pkg->name)); + log_pkgit("fetching " GREEN "%.*s" COLOR_RESET, str_fmt(&pkg->name)); + if (!fetch(pkg)) + return; + if (flags.verbose) + log_pkgit("fetched " GREEN "%.*s" COLOR_RESET, str_fmt(&pkg->name)); } - log_pkgit("building " GREEN "%.*s" COLOR_RESET , str_fmt(&pkg->name)); - if (!build(pkg)) return; - if (flags.verbose) log_pkgit("built " GREEN "%.*s" COLOR_RESET , str_fmt(&pkg->name)); + log_pkgit("building " GREEN "%.*s" COLOR_RESET, str_fmt(&pkg->name)); + if (!build(pkg)) + return; + if (flags.verbose) + log_pkgit("built " GREEN "%.*s" COLOR_RESET, str_fmt(&pkg->name)); bool install_success = false; - if (flags.verbose) log_info( - "attempting init.lua: 'repositories.%.*s.install'", - str_fmt(&pkg->name) - ); - if (!install_success && repo_install(pkg)) install_success = true; - if (flags.verbose) log_info( - "attempting bldit.lua: 'repositories.%.*s.install'", - str_fmt(&pkg->name) - ); - if (!install_success && bldit_install(pkg)) install_success = true; - if (flags.verbose) log_info( - "attempting init.lua: 'build_systems'", - str_fmt(&pkg->name) - ); - if (!install_success && config_install(pkg)) install_success = true; + if (flags.verbose) + log_info("attempting init.lua: 'repositories.%.*s.install'", + str_fmt(&pkg->name)); + if (!install_success && repo_install(pkg)) + install_success = true; + if (flags.verbose) + log_info("attempting bldit.lua: 'repositories.%.*s.install'", + str_fmt(&pkg->name)); + if (!install_success && bldit_install(pkg)) + install_success = true; + if (flags.verbose) + log_info("attempting init.lua: 'build_systems'", str_fmt(&pkg->name)); + if (!install_success && config_install(pkg)) + install_success = true; if (!install_success) { - log_error("no install function availible for package: %.*s", str_fmt(&pkg->name)); + log_error("no install function availible for package: %.*s", + str_fmt(&pkg->name)); return; } - log_success("installed " GREEN "%.*s" COLOR_RESET , str_fmt(&pkg->name)); + log_success("installed " GREEN "%.*s" COLOR_RESET, str_fmt(&pkg->name)); if (!pkg_exists(&pkg->name)) { - log_pkgit("adding " GREEN "%.*s" COLOR_RESET , &pkg->name); + log_pkgit("adding " GREEN "%.*s" COLOR_RESET, &pkg->name); if (pkg->url.len > 0) { add_repo(pkg); - log_pkgit("added " GREEN "%.*s" COLOR_RESET , &pkg->name); + log_pkgit("added " GREEN "%.*s" COLOR_RESET, &pkg->name); } } else { log_info("repo already exists, done"); diff --git a/src/pkg_remove.c b/src/pkg_remove.c index 9bc5305..3a0d0b2 100644 --- a/src/pkg_remove.c +++ b/src/pkg_remove.c @@ -17,6 +17,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ +#define _XOPEN_SOURCE 700 #include <ftw.h> #include <stdio.h> @@ -25,12 +26,12 @@ #include <sys/stat.h> #include <unistd.h> -#include "pkgit_lua.h" #include "files.h" #include "globs.h" #include "log.h" +#include "pkgit_lua.h" -bool target_uninstall(lua_State *L, char* lua_file, str *target) { +bool target_uninstall(lua_State *L, char *lua_file, str *target) { if (!lua_try_table(L, lua_file, target->data)) { lua_pop(L, 1); return false; @@ -54,7 +55,8 @@ bool repo_uninstall(package_t *pkg) { lua_pop(L, 2); return false; } - if (!lua_try_table(L, "init.lua", "targets")) return false; + if (!lua_try_table(L, "init.lua", "targets")) + return false; bool target_success = target_uninstall(L, "init.lua", &pkg->target); lua_pop(L, 3); return target_success; @@ -69,12 +71,14 @@ bool bldit_uninstall(package_t *pkg) { if (!is_bldit_usable()) { log_error("bldit version is newer than the installed pkgit version"); log_error("consider updating pkgit"); - if (!flags.force) return false; + if (!flags.force) + return false; } lua_pushfstring(B, "%s", inst_dirs.prefix.data); lua_setglobal(B, "prefix"); lua_pop(B, 1); - if (!lua_try_table(B, "bldit.lua", "targets")) return false; + if (!lua_try_table(B, "bldit.lua", "targets")) + return false; bool target_success = target_uninstall(B, "bldit.lua", &pkg->target); lua_pop(B, 2); lua_close(B); @@ -104,65 +108,50 @@ bool config_uninstall(package_t *pkg) { } target_success = target_uninstall(L, "init.lua", &pkg->target); lua_pop(L, 2); - if (target_success) break; + if (target_success) + break; } lua_pop(L, 1); return target_success; } -static int remove_installed( - const char *src_path, const struct stat *sb, - int typeflag, struct FTW *ftwbuf -) { +static int remove_installed(const char *src_path, const struct stat *sb, + int typeflag, struct FTW *ftwbuf) { (void)sb; (void)ftwbuf; if (typeflag == FTW_F) { const char *filename = src_path + ftwbuf->base; const char *ext = strrchr(filename, '.'); - if (!ext) ext = ""; + if (!ext) + ext = ""; if (strncmp(ext, ".so", 3) == 0) { char dest[MAX_PATH_LEN]; snprintf(dest, sizeof(dest), "%s/%s", inst_dirs.lib.data, filename); if (file_exists(dest)) - remove(dest); + remove(dest); } else if (access(src_path, X_OK) == 0) { - if ( - strcmp(ext, ".sample") != 0 && - strcmp(filename, "bldit") != 0 && + if (strcmp(ext, ".sample") != 0 && strcmp(filename, "bldit") != 0 && strcmp(filename, "build.sh") != 0 && - strcmp(filename, "compile.sh") != 0 - ) { + strcmp(filename, "compile.sh") != 0) { char dest[MAX_PATH_LEN]; - snprintf(dest, sizeof(dest), "%s/%s", inst_dirs.bin.data, filename); + snprintf(dest, sizeof(dest), "%s/%s", inst_dirs.bin.data, + filename); if (file_exists(dest)) - remove(dest); + remove(dest); } } else if (strcmp(ext, ".h") == 0) { char dest[MAX_PATH_LEN]; - snprintf(dest, sizeof(dest), "%s/%s", inst_dirs.include.data, filename); + snprintf(dest, sizeof(dest), "%s/%s", inst_dirs.include.data, + filename); if (file_exists(dest)) - remove(dest); + remove(dest); } } return 0; } -static int remove_tree( - const char *fpath, const struct stat *sb, - int typeflag, struct FTW *ftwbuf -) { - (void)sb; - (void)ftwbuf; - if (typeflag == FTW_F || typeflag == FTW_SL) { - unlink(fpath); - } else if (typeflag == FTW_DP) { - rmdir(fpath); - } - return 0; -} - void pkg_remove(package_t *pkg) { if (!is_directory(pkg->src.data)) { log_error("%.*s is not installed!", str_fmt(&pkg->name)); @@ -171,30 +160,33 @@ void pkg_remove(package_t *pkg) { chdir(pkg->src.data); bool uninstall_available = false; - if (!uninstall_available) if (repo_uninstall(pkg)) - uninstall_available = true; - if (!uninstall_available) if (bldit_uninstall(pkg)) - uninstall_available = true; - if (!uninstall_available) if (config_uninstall(pkg)) - uninstall_available = true; + if (!uninstall_available) + if (repo_uninstall(pkg)) + uninstall_available = true; + if (!uninstall_available) + if (bldit_uninstall(pkg)) + uninstall_available = true; + if (!uninstall_available) + if (config_uninstall(pkg)) + uninstall_available = true; if (!uninstall_available) { - log_error( - "no uninstall function availible for package: %.*s", - str_fmt(&pkg->name) - ); + log_error("no uninstall function availible for package: %.*s", + str_fmt(&pkg->name)); return; } + // TODO: refactor this nftw(pkg->src.data, remove_installed, 64, FTW_PHYS); const char *last_slash = strrchr(pkg->src.data, '/'); char target[MAX_PATH_LEN]; if (last_slash && last_slash != pkg->src.data) { size_t parent_len = last_slash - pkg->src.data; - snprintf(target, sizeof(target), "%.*s", (int)parent_len, pkg->src.data); + snprintf(target, sizeof(target), "%.*s", (int)parent_len, + pkg->src.data); } else { snprintf(target, sizeof(target), "%s", pkg->src.data); } - nftw(pkg->src.data, remove_tree, 64, FTW_DEPTH | FTW_PHYS); + remove_tree(pkg->src.data); log_success("removed %.*s", str_fmt(&pkg->name)); } |
