aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdam Harrison <adamdharrison@gmail.com>2022-12-03 15:08:37 -0500
committerAdam Harrison <adamdharrison@gmail.com>2022-12-03 15:08:37 -0500
commite486ece910368c0e5849517b7dafefabd22f2209 (patch)
tree6c2b3505c4e5b26eeb22bd5b5b78642df4c67e89 /src
parent9afefc5f58f8d00926aa60a56a5672b739fa30b4 (diff)
downloadlite-xl-plugin-manager-e486ece910368c0e5849517b7dafefabd22f2209.tar.gz
lite-xl-plugin-manager-e486ece910368c0e5849517b7dafefabd22f2209.zip
Better error handling.
Diffstat (limited to 'src')
-rw-r--r--src/lpm.c45
-rw-r--r--src/lpm.lua17
2 files changed, 49 insertions, 13 deletions
diff --git a/src/lpm.c b/src/lpm.c
index ebd2426..42bfd5a 100644
--- a/src/lpm.c
+++ b/src/lpm.c
@@ -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)