From 8ebcc1fa354fac87771caa88ee67600412bbcb57 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Tue, 25 Aug 2026 19:39:56 -0400 Subject: Global temp path, android build fixes Signed-off-by: Andrew Opalach --- src/common.c | 40 ++++++++++++++++++++++++++++++++++++---- src/common.h | 5 ++++- src/curl/curl.c | 2 +- src/util/buffer.c | 1 + 4 files changed, 42 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/common.c b/src/common.c index d0bd863..4358945 100644 --- a/src/common.c +++ b/src/common.c @@ -7,13 +7,23 @@ #else #include #endif +#ifdef NAUNET_ON_ANDROID +#include +#endif #include "util/timer/timer.h" #include "util/thread/thread.h" #include "common.h" -#ifndef NAUNET_ON_WINDOWS +#ifdef NAUNET_ON_WINDOWS +static char temp_directory[MAX_PATH + 1]; +#else +static char temp_directory[PATH_MAX]; +#endif +str nn_temp_directory; + +#if !defined NAUNET_ON_WINDOWS && !defined NAUNET_ON_ANDROID #define RESET "\033[0m" static inline const char *get_color_for_level(u8 level) { @@ -37,11 +47,15 @@ s32 al_print_default(void *userdata, u8 level, char *message) { (void)userdata; s32 ret = 0; -#ifdef NAUNET_ON_WINDOWS +#if defined NAUNET_ON_ANDROID + (void)level; + __android_log_print(ANDROID_LOG_DEBUG, APPLICATION_NAME, "%*.*s", 0, AL_LOG_MESSAGE_SIZE, message); + ret = al_strnlen(message, AL_LOG_MESSAGE_SIZE); +#elif defined NAUNET_ON_WINDOWS (void)level; - ret = al_printf("%*.*s\n", 0, AL_LOG_MESSAGE_SIZE, message); + ret = al_printf("%*.*s\n", 0, AL_LOG_MESSAGE_LENGTH, message); #else - ret = al_printf("%s%*.*s%s\n", get_color_for_level(level), 0, AL_LOG_MESSAGE_SIZE, message, RESET); + ret = al_printf("%s%*.*s%s\n", get_color_for_level(level), 0, AL_LOG_MESSAGE_LENGTH, message, RESET); #endif return ret; } @@ -90,6 +104,17 @@ bool nn_common_init(const char *thread_name) al_random_init((u32)(nn_get_timestamp() / 1000000)); +#if defined NAUNET_ON_WINDOWS + DWORD length = GetTempPathA(sizeof(temp_directory), temp_directory); + nn_temp_directory = al_str_w(temp_directory, 0, length); +#elif defined NAUNET_ON_ANDROID + // Default to empty. +#else + const char unix_tmpdir[] = { "/tmp" }; + al_memcpy(temp_directory, unix_tmpdir, sizeof(unix_tmpdir)); + nn_temp_directory = al_str_w(temp_directory, 0, sizeof(unix_tmpdir) - 1); +#endif + #ifdef NAUNET_ON_WINDOWS if (!nn_windows_init()) return false; #endif @@ -97,6 +122,13 @@ bool nn_common_init(const char *thread_name) return true; } +void nn_common_set_temp_directory(str *dir) +{ + al_assert(dir->length < sizeof(temp_directory)); + al_memcpy(temp_directory, dir->data, dir->length); + nn_temp_directory = al_str_w(temp_directory, 0, dir->length); +} + void nn_common_close(void) { #ifdef NAUNET_ON_WINDOWS diff --git a/src/common.h b/src/common.h index 30a8464..d22264b 100644 --- a/src/common.h +++ b/src/common.h @@ -1,6 +1,9 @@ #pragma once -#include +#include + +extern str nn_temp_directory; bool nn_common_init(const char *thread_name); +void nn_common_set_temp_directory(str *dir); void nn_common_close(void); diff --git a/src/curl/curl.c b/src/curl/curl.c index d18035c..10d3ed8 100644 --- a/src/curl/curl.c +++ b/src/curl/curl.c @@ -65,7 +65,7 @@ static void timeout_callback(struct ev_loop *loop, ev_timer *w, s32 revents) curl->handle_events(curl->userdata, curl); } -static s32 timer_callback(CURLM *multi, s64 timeout_ms, void *userp) +static s32 timer_callback(CURLM *multi, long timeout_ms, void *userp) { (void)multi; struct nn_curl *curl = (struct nn_curl *)userp; diff --git a/src/util/buffer.c b/src/util/buffer.c index ab7adac..9d8c9cf 100644 --- a/src/util/buffer.c +++ b/src/util/buffer.c @@ -36,6 +36,7 @@ void nn_buffer_write(struct nn_buffer *buffer, void *data, size_t index, size_t nn_buffer_ensure_space(buffer, reach); buffer->size = reach; } + al_assert(data && size > 0); al_memcpy(&buffer->data[index], data, size); } -- cgit v1.2.3-101-g0448