aboutsummaryrefslogtreecommitdiff
path: root/src/lpm.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lpm.c')
-rw-r--r--src/lpm.c101
1 files changed, 66 insertions, 35 deletions
diff --git a/src/lpm.c b/src/lpm.c
index b1396d1..189ff7d 100644
--- a/src/lpm.c
+++ b/src/lpm.c
@@ -1,7 +1,13 @@
#ifdef _WIN32
- #include <direct.h>
+ // needed for symbolic link creation and VT processing stuff
+ #undef _WIN32_WINNT
+ #define _WIN32_WINNT 0x0600 // _WIN32_WINNT_VISTA
+ // prevent windows.h from including winsock.h, which will allow us to include winsock2.h
+ #define WIN32_LEAN_AND_MEAN
#include <windows.h>
- #include <fileapi.h>
+ #include <direct.h>
+ #include <wincrypt.h>
+ #include <process.h>
#else
#ifndef LPM_NO_THRAEDS
#include <pthread.h>
@@ -80,7 +86,7 @@ typedef struct {
typedef struct {
#if _WIN32
- HANDLE mutex;
+ CRITICAL_SECTION mutex;
#else
pthread_mutex_t mutex;
#endif
@@ -90,7 +96,7 @@ static lpm_mutex_t* new_mutex() {
lpm_mutex_t* mutex = malloc(sizeof(lpm_mutex_t));
#ifndef LPM_NO_THREADS
#if _WIN32
- mutex->mutex = CreateMutex(NULL, FALSE, NULL);
+ InitializeCriticalSection(&mutex->mutex);
#else
pthread_mutex_init(&mutex->mutex, NULL);
#endif
@@ -101,7 +107,7 @@ static lpm_mutex_t* new_mutex() {
static void free_mutex(lpm_mutex_t* mutex) {
#ifndef LPM_NO_THREADS
#if _WIN32
- CloseHandle(mutex->mutex);
+ DeleteCriticalSection(&mutex->mutex);
#else
pthread_mutex_destroy(&mutex->mutex);
#endif
@@ -112,7 +118,7 @@ static void free_mutex(lpm_mutex_t* mutex) {
static void lock_mutex(lpm_mutex_t* mutex) {
#ifndef LPM_NO_THREADS
#if _WIN32
- WaitForSingleObject(mutex->mutex, INFINITE);
+ EnterCriticalSection(&mutex->mutex);
#else
pthread_mutex_lock(&mutex->mutex);
#endif
@@ -122,7 +128,7 @@ static void lock_mutex(lpm_mutex_t* mutex) {
static void unlock_mutex(lpm_mutex_t* mutex) {
#ifndef LPM_NO_THREADS
#if _WIN32
- ReleaseMutex(mutex->mutex);
+ LeaveCriticalSection(&mutex->mutex);
#else
pthread_mutex_unlock(&mutex->mutex);
#endif
@@ -131,7 +137,7 @@ static void unlock_mutex(lpm_mutex_t* mutex) {
#if _WIN32
-static DWORD windows_thread_callback(void* data) {
+static WINAPI unsigned int windows_thread_callback(void* data) {
lpm_thread_t* thread = data;
thread->data = thread->func(thread->data);
return 0;
@@ -144,7 +150,7 @@ static lpm_thread_t* create_thread(void* (*func)(void*), void* data) {
#if _WIN32
thread->func = func;
thread->data = data;
- thread->thread = CreateThread(NULL, 0, windows_thread_callback, thread, 0, NULL);
+ thread->thread = (HANDLE) _beginthreadex(NULL, 0, &windows_thread_callback, thread, 0, NULL);
#else
pthread_create(&thread->thread, NULL, func, data);
#endif
@@ -161,6 +167,8 @@ static void* join_thread(lpm_thread_t* thread) {
#ifndef LPM_NO_THREADS
#if _WIN32
WaitForSingleObject(thread->thread, INFINITE);
+ CloseHandle(thread->thread);
+ retval = thread->data;
#else
pthread_join(thread->thread, &retval);
#endif
@@ -399,7 +407,7 @@ static int lpm_hash(lua_State* L) {
}
fclose(file);
} else {
- sha256_update(&hash_ctx, data, len);
+ sha256_update(&hash_ctx, (unsigned char *) data, len);
}
sha256_final(&hash_ctx, buffer);
lua_pushhexstring(L, buffer, digest_length);
@@ -441,7 +449,12 @@ static int lpm_symlink(lua_State* L) {
return luaL_error(L, "can't create symlink %s: %s", luaL_checkstring(L, 2), strerror(errno));
return 0;
#else
- return luaL_error(L, "can't create symbolic link %s: your operating system sucks", luaL_checkstring(L, 2));
+ DWORD flags = SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE | ((GetFileAttributesW(lua_toutf16(L, luaL_checkstring(L, 2))) & FILE_ATTRIBUTE_DIRECTORY) ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0);
+ if (!CreateSymbolicLinkW(lua_toutf16(L, luaL_checkstring(L, 2)), lua_toutf16(L, luaL_checkstring(L, 1)), flags)) {
+ DWORD err = GetLastError();
+ return luaL_win32_error(L, err, "can't create symbolic link %s%s", luaL_checkstring(L, 2), err == ERROR_PRIVILEGE_NOT_HELD ? " (did you enable Windows Developer Mode?)" : "");
+ }
+ return 0;
#endif
}
@@ -871,8 +884,8 @@ static int mkdirp(lua_State* L, char* path, int len) {
lua_pop(L, 1);
#else
if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) && errno != EEXIST)
- #endif
return -1;
+ #endif
path[i] = '/';
}
}
@@ -1210,7 +1223,7 @@ static int lpm_extract(lua_State* L) {
} break;
}
int err;
- if (err = mtar_next(&tar)) {
+ if ((err = mtar_next(&tar))) {
mtar_close(&tar);
return luaL_error(L, "Error while reading tar archive: %s", mtar_strerror(err));
}
@@ -1451,7 +1464,7 @@ static int lpm_extract(lua_State* L) {
static int lpm_socket_write(get_context_t* context, int len) {
- return context->is_ssl ? mbedtls_ssl_write(&context->ssl, context->buffer, len) : write(context->s, context->buffer, len);
+ return context->is_ssl ? mbedtls_ssl_write(&context->ssl, (unsigned char *) context->buffer, len) : write(context->s, context->buffer, len);
}
static int lpm_socket_read(get_context_t* context, int len) {
@@ -1459,7 +1472,7 @@ static int lpm_extract(lua_State* L) {
len = sizeof(context->buffer) - context->buffer_length;
if (len == 0)
return len;
- len = context->is_ssl ? mbedtls_ssl_read(&context->ssl, &context->buffer[context->buffer_length], len) : read(context->s, &context->buffer[context->buffer_length], len);
+ len = context->is_ssl ? mbedtls_ssl_read(&context->ssl, (unsigned char *) &context->buffer[context->buffer_length], len) : read(context->s, &context->buffer[context->buffer_length], len);
if (len > 0)
context->buffer_length += len;
return len;
@@ -1637,6 +1650,7 @@ static int lpm_extract(lua_State* L) {
}
}
}
+ default: break;
}
finish:
if (context->file) {
@@ -1882,38 +1896,38 @@ static const luaL_Reg system_lib[] = {
{ NULL, NULL }
};
-#ifndef ARCH_PROCESSOR
+#ifndef LPM_ARCH_PROCESSOR
#if defined(__x86_64__) || defined(_M_AMD64) || defined(__MINGW64__)
- #define ARCH_PROCESSOR "x86_64"
+ #define LPM_ARCH_PROCESSOR "x86_64"
#elif defined(__i386__) || defined(_M_IX86) || defined(__MINGW32__)
- #define ARCH_PROCESSOR "x86"
+ #define LPM_ARCH_PROCESSOR "x86"
#elif defined(__aarch64__) || defined(_M_ARM64) || defined (_M_ARM64EC)
- #define ARCH_PROCESSOR "aarch64"
+ #define LPM_ARCH_PROCESSOR "aarch64"
#elif defined(__arm__) || defined(_M_ARM)
- #define ARCH_PROCESSOR "arm"
+ #define LPM_ARCH_PROCESSOR "arm"
#elif defined(__riscv_xlen) && __riscv_xlen == 32
- #define ARCH_PROCESSOR "riscv32"
+ #define LPM_ARCH_PROCESSOR "riscv32"
#elif defined(__riscv_xlen) && __riscv_xlen == 64
- #define ARCH_PROCESSOR "riscv64"
+ #define LPM_ARCH_PROCESSOR "riscv64"
#else
- #error "Please define -DARCH_PROCESSOR."
+ #error "Please define -DLPM_ARCH_PROCESSOR."
#endif
#endif
-#ifndef ARCH_PLATFORM
+#ifndef LPM_ARCH_PLATFORM
#if _WIN32
- #define ARCH_PLATFORM "windows"
+ #define LPM_ARCH_PLATFORM "windows"
#elif __ANDROID__
- #define ARCH_PLATFORM "android"
+ #define LPM_ARCH_PLATFORM "android"
#elif __linux__
- #define ARCH_PLATFORM "linux"
+ #define LPM_ARCH_PLATFORM "linux"
#elif __APPLE__
- #define ARCH_PLATFORM "darwin"
+ #define LPM_ARCH_PLATFORM "darwin"
#else
- #error "Please define -DARCH_PLATFORM."
+ #error "Please define -DLPM_ARCH_PLATFORM."
#endif
#endif
-#ifndef LITE_ARCH_TUPLE
- #define LITE_ARCH_TUPLE ARCH_PROCESSOR "-" ARCH_PLATFORM
+#ifndef LPM_ARCH_TUPLE
+ #define LPM_ARCH_TUPLE LPM_ARCH_PROCESSOR "-" LPM_ARCH_PLATFORM
#endif
@@ -1926,9 +1940,9 @@ static const luaL_Reg system_lib[] = {
// If this is defined as empty string, we disable self-upgrading, as well as switching the executable symlink.
#ifndef LPM_DEFAULT_RELEASE
#if _WIN32
- #define LPM_DEFAULT_RELEASE "https://github.com/lite-xl/lite-xl-plugin-manager/releases/download/%r/lpm." LITE_ARCH_TUPLE ".exe"
+ #define LPM_DEFAULT_RELEASE "https://github.com/lite-xl/lite-xl-plugin-manager/releases/download/%r/lpm." LPM_ARCH_TUPLE ".exe"
#else
- #define LPM_DEFAULT_RELEASE "https://github.com/lite-xl/lite-xl-plugin-manager/releases/download/%r/lpm." LITE_ARCH_TUPLE
+ #define LPM_DEFAULT_RELEASE "https://github.com/lite-xl/lite-xl-plugin-manager/releases/download/%r/lpm." LPM_ARCH_TUPLE
#endif
#endif
@@ -1937,6 +1951,7 @@ static const luaL_Reg system_lib[] = {
extern unsigned int lpm_luac_len;
#endif
+
int main(int argc, char* argv[]) {
lua_State* L = luaL_newstate();
luaL_openlibs(L);
@@ -1950,7 +1965,7 @@ int main(int argc, char* argv[]) {
lua_setglobal(L, "ARGV");
lua_pushliteral(L, LPM_VERSION);
lua_setglobal(L, "VERSION");
- lua_pushliteral(L, ARCH_PLATFORM);
+ lua_pushliteral(L, LPM_ARCH_PLATFORM);
lua_setglobal(L, "PLATFORM");
#ifdef LPM_NO_NETWORK
lua_pushboolean(L, 1);
@@ -1964,6 +1979,18 @@ int main(int argc, char* argv[]) {
lua_pushboolean(L, 0);
#endif
lua_setglobal(L, "NO_GIT");
+ #ifdef LPM_NO_LXL
+ lua_pushboolean(L, 1);
+ #else
+ lua_pushboolean(L, 0);
+ #endif
+ lua_setglobal(L, "NO_LXL");
+ #ifdef LPM_NO_REMOTE_EXECUTABLE
+ lua_pushboolean(L, 1);
+ #else
+ lua_pushboolean(L, 0);
+ #endif
+ lua_setglobal(L, "NO_REMOTE_EXEUCTABLE");
#if _WIN32
DWORD handles[] = { STD_OUTPUT_HANDLE, STD_ERROR_HANDLE };
int setVirtualProcessing = 0;
@@ -1979,6 +2006,10 @@ int main(int argc, char* argv[]) {
#else
lua_pushboolean(L, isatty(fileno(stdout)));
#endif
+ #if _WIN32
+ lua_pushboolean(L, GetConsoleProcessList(&(DWORD){ 0 }, 1) == 1);
+ lua_setglobal(L, "LPM_RUN_FROM_GUI");
+ #endif
lua_setglobal(L, "TTY");
#if _WIN32
lua_pushliteral(L, "\\");
@@ -2012,7 +2043,7 @@ int main(int argc, char* argv[]) {
#endif
lua_setglobal(L, "EXEFILE");
- lua_pushliteral(L, LITE_ARCH_TUPLE);
+ lua_pushliteral(L, LPM_ARCH_TUPLE);
lua_setglobal(L, "DEFAULT_ARCH");
lua_pushliteral(L, LPM_DEFAULT_REPOSITORY);
lua_setglobal(L, "DEFAULT_REPO_URL");