diff options
author | Adam Harrison <adamdharrison@gmail.com> | 2022-12-03 15:08:37 -0500 |
---|---|---|
committer | Adam Harrison <adamdharrison@gmail.com> | 2022-12-03 15:08:37 -0500 |
commit | e486ece910368c0e5849517b7dafefabd22f2209 (patch) | |
tree | 6c2b3505c4e5b26eeb22bd5b5b78642df4c67e89 | |
parent | 9afefc5f58f8d00926aa60a56a5672b739fa30b4 (diff) | |
download | lite-xl-plugin-manager-e486ece910368c0e5849517b7dafefabd22f2209.tar.gz lite-xl-plugin-manager-e486ece910368c0e5849517b7dafefabd22f2209.zip |
Better error handling.
-rw-r--r-- | README.md | 9 | ||||
-rwxr-xr-x | build.sh | 2 | ||||
-rw-r--r-- | src/lpm.c | 45 | ||||
-rw-r--r-- | src/lpm.lua | 17 |
4 files changed, 57 insertions, 16 deletions
@@ -86,13 +86,18 @@ lpm --help ### Linux to Windows ``` -CC=x86_64-w64-mingw32-gcc AR=x86_64-w64-mingw32-gcc-ar WINDRES=x86_64-w64-mingw32-windres CMAKE_DEFAULT_FLAGS="-DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=NEVER -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=NEVER -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_SYSTEM_INCLUDE_PATH=/usr/share/mingw-w64/include" GIT2_CONFIGURE="-DDLLTOOL=x86_64-w64-mingw32-dlltool" ./build.sh -DLPM_VERSION='"'$VERSION-x86_64-windows-`git rev-parse --short HEAD`'"' +CC=x86_64-w64-mingw32-gcc AR=x86_64-w64-mingw32-gcc-ar WINDRES=x86_64-w64-mingw32-windres CMAKE_DEFAULT_FLAGS="-DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER\ + -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=NEVER -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=NEVER -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_SYSTEM_INCLUDE_PATH=/usr/share/mingw-w64/include"\ + GIT2_CONFIGURE="-DDLLTOOL=x86_64-w64-mingw32-dlltool" ./build.sh -DLPM_VERSION='"'$VERSION-x86_64-windows-`git rev-parse --short HEAD`'"' ``` +## Tests + +To run the test suite, simply use lua to run `lua t/run.lua`. use `FAST=1 t/run.lua` to avoid the costs of tearing down and building up suites each time. ## Bugs -If you find a bug, please create an [issue](issues) with the following information: +If you find a bug, please create an issue with the following information: * Your operating system. * The commit or version of LPM you're using. @@ -17,7 +17,7 @@ mkdir -p lib/prefix/include lib/prefix/lib if [[ "$@" != *"-lz"* ]]; then [ ! -e "lib/zlib" ] && echo "Make sure you've cloned submodules. (git submodule update --init --depth=1)" && exit -1 [[ ! -e "lib/zlib/build" && $OSTYPE != 'msys'* ]] && cd lib/zlib && mkdir build && cd build && ../configure --static --prefix=`pwd`/../../prefix && $MAKE -j $JOBS && $MAKE install && cd ../../../ - [[ ! -e "lib/zlib/build" && $OSTYPE == 'msys'* ]] && cd lib/zlib && mkdir build && $MAKE -f ../win32/Makefile.gcc -j $JOBS && cp *.a ../prefix/lib && cp *.h ../prefix/include && cd ../../ + [[ ! -e "lib/zlib/build" && $OSTYPE == 'msys'* ]] && cd lib/zlib && mkdir build && $MAKE -f win32/Makefile.gcc -j $JOBS && cp *.a ../prefix/lib && cp *.h ../prefix/include && cd ../../ LDFLAGS="$LDFLAGS -l:libz.a" fi if [[ "$@" != *"-lmbedtls"* && "$@" != *"-lmbedcrypto"* ]]; then @@ -30,6 +30,7 @@ #include <mbedtls/entropy.h> #include <mbedtls/ctr_drbg.h> #include <mbedtls/ssl.h> +#include <mbedtls/error.h> #include <mbedtls/net.h> #include <zlib.h> @@ -131,7 +132,7 @@ static int lpm_ls(lua_State *L) { #ifdef _WIN32 lua_settop(L, 1); - lua_pushstring(L, path[0] == 0 || strchr("\\/", path[strlen(path) - 1]) != NULL ? "*" : "/*"); + lua_pushstring(L, path[0] == 0 || strchr("\\/", path[strlen(path) - 1]) != NULL ? "*" : "\\*"); lua_concat(L, 2); path = lua_tostring(L, -1); @@ -371,6 +372,34 @@ static mbedtls_ctr_drbg_context drbg_context; static mbedtls_ssl_config ssl_config; static mbedtls_ssl_context ssl_context; +static int mbedtls_snprintf(char* buffer, int len, int status, const char* str, ...) { + char mbed_buffer[128]; + mbedtls_strerror(status, mbed_buffer, sizeof(mbed_buffer)); + int error_len = strlen(mbed_buffer); + va_list va; + int offset = 0; + va_start(va, str); + offset = vsnprintf(buffer, len, str, va); + va_end(va); + if (offset < len - 2) { + strcat(buffer, ": "); + if (offset < len - error_len - 2) + strcat(buffer, mbed_buffer); + } + return strlen(buffer); +} + +static int luaL_mbedtls_error(lua_State* L, int code, const char* str, ...) { + char vsnbuffer[1024]; + char mbed_buffer[128]; + mbedtls_strerror(code, mbed_buffer, sizeof(mbed_buffer)); + va_list va; + va_start(va, str); + vsnprintf(vsnbuffer, sizeof(vsnbuffer), str, va); + va_end(va); + return luaL_error(L, "%s: %s", vsnbuffer, mbed_buffer); +} + static int lpm_certs(lua_State* L) { const char* type = luaL_checkstring(L, 1); @@ -386,9 +415,11 @@ static int lpm_certs(lua_State* L) { mbedtls_entropy_init(&entropy_context); mbedtls_ctr_drbg_init(&drbg_context); if ((status = mbedtls_ctr_drbg_seed(&drbg_context, mbedtls_entropy_func, &entropy_context, NULL, 0)) != 0) - return luaL_error(L, "failed to setup mbedtls_x509"); + return luaL_mbedtls_error(L, status, "failed to setup mbedtls_x509"); mbedtls_ssl_config_init(&ssl_config); status = mbedtls_ssl_config_defaults(&ssl_config, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT); + if (status) + return luaL_mbedtls_error(L, status, "can't set ssl_config defaults"); mbedtls_ssl_conf_max_version(&ssl_config, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3); mbedtls_ssl_conf_min_version(&ssl_config, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3); mbedtls_ssl_conf_authmode(&ssl_config, MBEDTLS_SSL_VERIFY_REQUIRED); @@ -430,7 +461,7 @@ static int lpm_certs(lua_State* L) { } git_libgit2_opts(GIT_OPT_SET_SSL_CERT_LOCATIONS, path, NULL); if ((status = mbedtls_x509_crt_parse_file(&x509_certificate, path)) != 0) - return luaL_error(L, "mbedtls_x509_crt_parse_file failed to parse CA certificate %s: %d", path, -status); + return luaL_mbedtls_error(L, status, "mbedtls_x509_crt_parse_file failed to parse CA certificate %s: %d", path, -status); mbedtls_ssl_conf_ca_chain(&ssl_config, &x509_certificate, NULL); } return 0; @@ -628,13 +659,13 @@ static int lpm_get(lua_State* L) { mbedtls_net_set_block(&net_context); mbedtls_ssl_set_bio(&ssl_context, &net_context, mbedtls_net_send, NULL, mbedtls_net_recv_timeout); if ((status = mbedtls_net_connect(&net_context, hostname, port, MBEDTLS_NET_PROTO_TCP)) != 0) { - snprintf(err, sizeof(err), "can't connect to hostname %s: %d", hostname, status); goto cleanup; + mbedtls_snprintf(err, sizeof(err), status, "can't connect to hostname %s", hostname); goto cleanup; } else if ((status = mbedtls_ssl_set_hostname(&ssl_context, hostname)) != 0) { - snprintf(err, sizeof(err), "can't set hostname %s: %d", hostname, status); goto cleanup; + mbedtls_snprintf(err, sizeof(err), status, "can't set hostname %s", hostname); goto cleanup; } else if ((status = mbedtls_ssl_handshake(&ssl_context)) != 0) { - snprintf(err, sizeof(err), "can't handshake with %s: %d", hostname, status); goto cleanup; + mbedtls_snprintf(err, sizeof(err), status, "can't handshake with %s", hostname); goto cleanup; } else if ((status = mbedtls_ssl_get_verify_result(&ssl_context)) != 0) { - snprintf(err, sizeof(err), "can't verify result for %s: %d", hostname, status); goto cleanup; + mbedtls_snprintf(err, sizeof(err), status, "can't verify result for %s", hostname); goto cleanup; } } else { int port = luaL_checkinteger(L, 3); diff --git a/src/lpm.lua b/src/lpm.lua index ddbb1f5..992ab9f 100644 --- a/src/lpm.lua +++ b/src/lpm.lua @@ -389,9 +389,13 @@ function common.normalize_path(path) if not path or not path:find("^~") then ret function common.rmrf(root) local info = root and root ~= "" and system.stat(root) if not info then return end - if info.type == "file" or info.symlink then return os.remove(root) end - for i,v in ipairs(system.ls(root)) do common.rmrf(root .. PATHSEP .. v) end - system.rmdir(root) + if info.type == "file" or info.symlink then + local status, err = os.remove(root) + if not status then error("can't remove " .. root .. ": " .. err) end + else + for i,v in ipairs(system.ls(root)) do common.rmrf(root .. PATHSEP .. v) end + system.rmdir(root) + end end function common.mkdirp(path) local stat = system.stat(path) @@ -400,7 +404,7 @@ function common.mkdirp(path) local target for _, dirname in ipairs({ common.split("[/\\]", path) }) do target = target and target .. PATHSEP .. dirname or dirname - if target ~= "" and not system.stat(target) then system.mkdir(target) end + if target ~= "" and not target:find("^[A-Z]:$") and not system.stat(target) then system.mkdir(target) end end end function common.copy(src, dst) @@ -1617,7 +1621,7 @@ Flags have the following effects: AUTO_PULL_REMOTES = ARGS["remotes"] if not system.stat(USERDIR) then error("can't find user directory " .. USERDIR) end CACHEDIR = common.normalize_path(ARGS["cachedir"]) or os.getenv("LPM_CACHE") or USERDIR .. PATHSEP .. "lpm" - TMPDIR = common.normalize_path(ARGS["tmpdir"]) or CACHEDIR .. "/tmp" + TMPDIR = common.normalize_path(ARGS["tmpdir"]) or CACHEDIR .. PATHSEP .. "tmp" repositories = {} if ARGS[2] == "purge" then return lpm_purge() end @@ -1641,7 +1645,8 @@ Flags have the following effects: "/var/ssl/certs", -- AIX } if PLATFORM == "windows" then - system.certs("system", TMPDIR .. "certs.crt") + common.mkdirp(TMPDIR) + system.certs("system", TMPDIR .. PATHSEP .. "certs.crt") else for i, path in ipairs(paths) do local stat = system.stat(path) |