diff options
| author | 2025-03-15 20:02:43 -0400 | |
|---|---|---|
| committer | 2025-03-15 20:02:43 -0400 | |
| commit | b4e45dd89e6dd53d8cb940e5609b812e19ade913 (patch) | |
| tree | c633916f4e67323402b05e94e61febc69cb2c3f9 | |
| parent | ce102493f377d0452e44c3a4bb6e6e15ab0c819d (diff) | |
| download | libnaunet-b4e45dd89e6dd53d8cb940e5609b812e19ade913.tar.gz libnaunet-b4e45dd89e6dd53d8cb940e5609b812e19ade913.tar.bz2 libnaunet-b4e45dd89e6dd53d8cb940e5609b812e19ade913.zip | |
64-bit stdio file, no retry in multiplex id read
- Some cleanup.
Signed-off-by: Andrew Opalach <andrew@akon.city>
| -rw-r--r-- | src/common.c | 14 | ||||
| -rw-r--r-- | src/ev_embed_compat.c | 2 | ||||
| -rw-r--r-- | src/multiplex.c | 12 | ||||
| -rw-r--r-- | src/util/file/file.h | 9 | ||||
| -rw-r--r-- | src/util/file/file_linux.c | 44 | ||||
| -rw-r--r-- | src/util/file/file_stdio.c | 35 | ||||
| -rw-r--r-- | src/util/thread/thread_windows.c | 2 | ||||
| -rw-r--r-- | src/winwrap.c | 19 | ||||
| -rw-r--r-- | src/winwrap.h | 4 | ||||
| -rw-r--r-- | subprojects/libalabaster.wrap | 2 |
10 files changed, 64 insertions, 79 deletions
diff --git a/src/common.c b/src/common.c index 3c97a7a..929a455 100644 --- a/src/common.c +++ b/src/common.c @@ -62,13 +62,7 @@ static void malloc_unlock(void) bool nn_common_init(void) { -#ifdef NAUNET_ON_WINDOWS - if (!setlocale(LC_ALL, ".UTF8")) { -#else - if (!setlocale(LC_ALL, "")) { -#endif - return false; - } + if (!setlocale(LC_ALL, "")) return false; al_set_print(al_print_default, NULL); @@ -90,10 +84,10 @@ bool nn_common_init(void) free, malloc_lock, malloc_unlock); #endif - al_rand_init((u32)(nn_get_timestamp()/1000)); + al_rand_init((u32)(nn_get_timestamp() / 1000)); #ifdef NAUNET_ON_WINDOWS - nn_windows_init(); + if (!nn_windows_init()) return false; #endif return true; @@ -111,7 +105,7 @@ void nn_common_close(void) size_t total; al_malloc_stats(¤t, &peak, &total); al_printf("malloc stats, current: %.2fKB, peak: %.2fKB, total: %.2fKB.\n", - current/1024.f, peak/1024.f, total/1024.f); + current / 1024.f, peak / 1024.f, total / 1024.f); nn_mutex_destroy(&malloc_mutex); #endif } diff --git a/src/ev_embed_compat.c b/src/ev_embed_compat.c index d090e5e..a907af4 100644 --- a/src/ev_embed_compat.c +++ b/src/ev_embed_compat.c @@ -11,7 +11,7 @@ #include "evwrap.h" #if defined(__clang__) || defined(__GNUC__) -_Pragma("GCC diagnostic push") \ +_Pragma("GCC diagnostic push") _Pragma("GCC diagnostic ignored \"-Wstrict-aliasing\"") _Pragma("GCC diagnostic ignored \"-Wcomment\"") _Pragma("GCC diagnostic ignored \"-Wparentheses\"") diff --git a/src/multiplex.c b/src/multiplex.c index 3bc171f..2eed7df 100644 --- a/src/multiplex.c +++ b/src/multiplex.c @@ -1,3 +1,5 @@ +#include <al/log.h> + #include "multiplex.h" bool nn_multiplex_socket_init(struct nn_multiplex_socket *multi, u8 type, @@ -22,15 +24,13 @@ static void socket_read_callback(struct ev_loop *loop, ev_io *w, s32 revents) u8 id; ssize_t ret = nn_socket_read(&conn->sock, &id, sizeof(u8)); - if (ret < 0) { + if (ret <= 0) { ev_io_stop(loop, &conn->event); nn_socket_close(&conn->sock); al_free(conn); - return; - } - - if ((size_t)ret < sizeof(u8)) { - // Try again. + if (ret == 0) { + al_log_warn("multiplex", "read() returned 0 on POLLIN."); + } return; } diff --git a/src/util/file/file.h b/src/util/file/file.h index e53390d..03b82ba 100644 --- a/src/util/file/file.h +++ b/src/util/file/file.h @@ -4,7 +4,15 @@ #include <al/lib.h> #ifdef NAUNET_NEEDS_STDIO_ASSIST +#define _FILE_OFFSET_BITS 64 #include <stdio.h> +#ifdef NAUNET_ON_WINDOWS +#define fseek _fseeki64 +#define ftell _ftelli64 +#else +#define fseek fseeko +#define ftell ftello +#endif #else #ifdef NAUNET_ON_WINDOWS // @TODO: @@ -72,7 +80,6 @@ bool nn_file_exists(str *path); bool nn_file_create(str *path); bool nn_file_truncate(struct nn_file *file, off_t size); off_t nn_file_seek(struct nn_file *file, off_t offset, s32 whence); -size_t nn_file_get_filesize(struct nn_file *file); bool nn_file_write(struct nn_file *file, void *buf, size_t size); bool nn_file_read(struct nn_file *file, void *buf, size_t size); s32 nn_file_read_as_str(struct nn_file *file, str *out); diff --git a/src/util/file/file_linux.c b/src/util/file/file_linux.c index 6b52c60..5a3621b 100644 --- a/src/util/file/file_linux.c +++ b/src/util/file/file_linux.c @@ -1,3 +1,4 @@ +#define AL_LOG_SECTION "file_linux" #include <al/log.h> #include <sys/mman.h> @@ -9,7 +10,7 @@ static bool lock_file_internal(s32 fd, size_t size) { struct flock l = { .l_type = F_WRLCK, .l_whence = SEEK_SET, .l_start = 0, .l_len = size }; if (fcntl(fd, F_SETLKW, &l) == -1) { - al_log_error("file", "fcntl(F_SETLKW, F_WRLCK) failed (%s).", nn_strerror(errno)); + error("fcntl(F_SETLKW, F_WRLCK) failed (%s).", nn_strerror(errno)); close(fd); return false; } @@ -20,7 +21,7 @@ static bool unlock_file_internal(s32 fd, size_t size) { struct flock l = { .l_type = F_ULOCK, .l_whence = SEEK_SET, .l_start = 0, .l_len = size }; if (fcntl(fd, F_SETLKW, &l) == -1) { - al_log_error("file", "fcntl(F_SETLKW, F_ULOCK) failed (%s).", nn_strerror(errno)); + error("fcntl(F_SETLKW, F_ULOCK) failed (%s).", nn_strerror(errno)); return false; } return true; @@ -43,15 +44,14 @@ bool nn_file_open(struct nn_file *file, str *path, s32 flags) } if (!open_file_linux(&file->fd, path, oflags)) { - al_log_error("file", "open(%.*s) failed (%d: %s).", - al_str_fmt(path), errno, nn_strerror(errno)); + error("open(%.*s) failed (%d: %s).", al_str_fmt(path), errno, nn_strerror(errno)); return false; } struct stat sb; s32 res = fstat(file->fd, &sb); if (res == -1) { - al_log_error("file", "fstat() failed (%s).", nn_strerror(errno)); + error("fstat() failed (%s).", nn_strerror(errno)); close(file->fd); return false; } @@ -87,8 +87,7 @@ off_t nn_file_seek(struct nn_file *file, off_t offset, s32 whence) { off_t res = lseek(file->fd, offset, whence); if (res == (off_t)-1) { - al_log_error("file", "lseek(%jd, %d) failed (%s).", - (intmax_t)offset, whence, nn_strerror(errno)); + error("lseek(%zd, %d) failed (%s).", offset, whence, nn_strerror(errno)); } return res; } @@ -97,26 +96,20 @@ bool nn_file_truncate(struct nn_file *file, off_t size) { s32 ret = ftruncate(file->fd, size); if (ret != 0) { - al_log_error("file", "ftruncate(%jd) failed (%s).", - (intmax_t)size, nn_strerror(errno)); + error("ftruncate(%zd) failed (%s).", size, nn_strerror(errno)); return false; } file->size = size; return true; } -size_t nn_file_get_filesize(struct nn_file *file) -{ - return file->size; -} - #define file_io_loop(call, fail_case) \ size_t bc = 0; \ ssize_t res; \ for (;;) { \ res = call(file->fd, buf, size - bc); \ if (fail_case) { \ - al_log_error("file", ""#call"() failed (%s).", nn_strerror(errno)); \ + error(""#call"() failed (%s).", nn_strerror(errno)); \ return false; \ } \ if ((bc += res) >= size) break; \ @@ -138,8 +131,7 @@ void *nn_file_mmap(struct nn_file *file) s32 flags = PROT_READ | (file->flags & NNWT_FILE_READONLY ? 0 : PROT_WRITE); void *map = mmap(NULL, file->size, flags, MAP_SHARED, file->fd, 0L); if (map == MAP_FAILED) { - al_log_error("file", "mmap(%.*s) failed (%s).", - al_str_fmt(&file->path), nn_strerror(errno)); + error("mmap(%.*s) failed (%s).", al_str_fmt(&file->path), nn_strerror(errno)); return NULL; } return map; @@ -149,8 +141,7 @@ void *nn_file_mremap(struct nn_file *file, size_t size, void *old_map) { void *map = mremap(old_map, file->size, size, MREMAP_MAYMOVE); if (map == MAP_FAILED) { - al_log_error("file", "mremap(%p, %zu, %zu) failed (%s).", - old_map, file->size, size, nn_strerror(errno)); + error("mremap(%p, %zu, %zu) failed (%s).", old_map, file->size, size, nn_strerror(errno)); return NULL; } return map; @@ -160,8 +151,7 @@ void nn_file_munmap(struct nn_file *file, void *map) { s32 ret = munmap(map, file->size); if (ret != 0) { - al_log_error("file", "munmap(%p, %zu) failed (%s).", - map, file->size, nn_strerror(errno)); + error("munmap(%p, %zu) failed (%s).", map, file->size, nn_strerror(errno)); } } @@ -180,8 +170,7 @@ bool nn_dir_open(struct nn_dir *dir, str *path) dir->dir = opendir(c_str); al_free(c_str); if (!dir->dir) { - al_log_error("file", "opendir(%.*s) failed (%s).", - al_str_fmt(path), nn_strerror(errno)); + error("opendir(%.*s) failed (%s).", al_str_fmt(path), nn_strerror(errno)); return false; } al_str_clone(&dir->path, path); @@ -202,8 +191,7 @@ bool nn_dir_exists(str *path) al_free(c_str); if (!dir) { if (errno != ENOENT) { - al_log_error("file", "opendir(%.*s) failed (%s).", - al_str_fmt(path), nn_strerror(errno)); + error("opendir(%.*s) failed (%s).", al_str_fmt(path), nn_strerror(errno)); } return false; } @@ -217,8 +205,7 @@ bool nn_dir_create(str *path) s32 ret = mkdir(c_str, 0755); al_free(c_str); if (ret == -1) { - al_log_error("file", "mkdir(%.*s) failed (%s).", - al_str_fmt(path), nn_strerror(errno)); + error("mkdir(%.*s) failed (%s).", al_str_fmt(path), nn_strerror(errno)); return false; } return true; @@ -229,8 +216,7 @@ bool nn_dir_read(struct nn_dir *dir, struct nn_dir_entry *entry) errno = 0; if (!(entry->entry = readdir(dir->dir))) { if (errno) { - al_log_error("file", "readdir(%.*s) failed (%s).", - al_str_fmt(&dir->path), nn_strerror(errno)); + error("readdir(%.*s) failed (%s).", al_str_fmt(&dir->path), nn_strerror(errno)); } return false; } diff --git a/src/util/file/file_stdio.c b/src/util/file/file_stdio.c index ada5ae3..3f58664 100644 --- a/src/util/file/file_stdio.c +++ b/src/util/file/file_stdio.c @@ -1,3 +1,4 @@ +#define AL_LOG_SECTION "file_stdio" #include <al/log.h> #include "../error.h" @@ -11,7 +12,7 @@ static inline bool open_stdio_file(FILE **file, str *path, const char *mode) { char *c_str = al_str_to_c_str(path); #ifdef NAUNET_ON_WINDOWS - // Still sets errno, according to the microsoft docs. + // Still sets errno, according to the Microsoft docs. errno_t ret = fopen_s(file, c_str, mode); #else s32 ret = (*file = fopen(c_str, mode)) != NULL ? 0 : errno; @@ -20,22 +21,27 @@ static inline bool open_stdio_file(FILE **file, str *path, const char *mode) return ret == 0; } +static bool query_filesize_stdio(struct nn_file *file) +{ + off_t pos = nn_file_seek(file, 0, SEEK_END); + if (pos == -1) return false; + file->size = pos; + rewind(file->file); + return true; +} + bool nn_file_open(struct nn_file *file, str *path, s32 flags) { const char *mode = flags & NNWT_FILE_CREATE ? "wb+" : "rb+"; if (!open_stdio_file(&file->file, path, mode)) { - al_log_error("file_stdio", "fopen(%.*s) failed (%d: %s).", - al_str_fmt(path), errno, nn_strerror(errno)); + error("fopen(%.*s) failed (%d: %s).", al_str_fmt(path), errno, nn_strerror(errno)); return false; } - off_t pos = nn_file_seek(file, 0, SEEK_END); - if (pos == -1) { + if (!query_filesize_stdio(file)) { nn_file_close(file); return false; } - file->size = pos; - rewind(file->file); return true; } @@ -59,8 +65,7 @@ bool nn_file_create(str *path) off_t nn_file_seek(struct nn_file *file, off_t offset, s32 whence) { if (fseek(file->file, offset, whence) != 0) { - al_log_error("file_stdio", "fseek(%jd, %d) failed (%s).", - (intmax_t)offset, whence, nn_strerror(errno)); + error("fseek(%zd, %d) failed (%s).", offset, whence, nn_strerror(errno)); return -1; } return ftell(file->file); @@ -68,21 +73,15 @@ off_t nn_file_seek(struct nn_file *file, off_t offset, s32 whence) bool nn_file_truncate(struct nn_file *file, off_t size) { - (void)file; - (void)size; + file->size = size; return true; } -size_t nn_file_get_filesize(struct nn_file *file) -{ - return file->size; -} - bool nn_file_write(struct nn_file *file, void *buf, size_t size) { size_t ret = fwrite(buf, size, 1, file->file); if (ret == 0) { - al_log_error("file_stdio", "fwrite(%zu) failed (%s).", size, nn_strerror(errno)); + error("fwrite(%zu) failed (%s).", size, nn_strerror(errno)); return false; } return true; @@ -92,7 +91,7 @@ bool nn_file_read(struct nn_file *file, void *buf, size_t size) { size_t ret = fread(buf, size, 1, file->file); if (ret == 0) { - al_log_error("file_stdio", "fread(%zu) failed (%s).", size, nn_strerror(errno)); + error("fread(%zu) failed (%s).", size, nn_strerror(errno)); return false; } return true; diff --git a/src/util/thread/thread_windows.c b/src/util/thread/thread_windows.c index ca9be80..f1f1dd7 100644 --- a/src/util/thread/thread_windows.c +++ b/src/util/thread/thread_windows.c @@ -4,7 +4,7 @@ NTSTATUS(__stdcall *NtDelayExecution)(BOOL Alertable, PLARGE_INTEGER DelayInterv void nn_thread_sleep(nn_os_tstamp delay) { - NtDelayExecution(false, &((LARGE_INTEGER){ .QuadPart = -(s64)(delay * 1e7L) })); + NtDelayExecution(false, &((LARGE_INTEGER){ .QuadPart = -(LONGLONG)(delay * 1E7L) })); } void nn_thread_create(struct nn_thread *thread, nn_thread_func func, void *userdata) diff --git a/src/winwrap.c b/src/winwrap.c index 1a97c94..c287339 100644 --- a/src/winwrap.c +++ b/src/winwrap.c @@ -1,29 +1,26 @@ #include "winwrap.h" -void nn_windows_init(void) +bool nn_windows_init(void) { // https://learn.microsoft.com/en-us/windows/win32/api/objbase/ne-objbase-coinit //CoInitializeEx(NULL, COINIT_MULTITHREADED | COINIT_SPEED_OVER_MEMORY | COINIT_DISABLE_OLE1DDE); WSADATA wsa_data; - WSAStartup(MAKEWORD(2, 2), &wsa_data); + if (WSAStartup(MAKEWORD(2, 2), &wsa_data) != 0) { + return false; + } -#if defined(__clang__) || defined(__GNUC__) -_Pragma("GCC diagnostic push") \ -_Pragma("GCC diagnostic ignored \"-Wcast-function-type\"") -#endif NTSTATUS(__stdcall *ZwSetTimerResolution)(IN ULONG RequestedResolution, IN BOOLEAN Set, OUT PULONG ActualResolution); - ZwSetTimerResolution = (NTSTATUS(__stdcall *)(ULONG, BOOLEAN, PULONG))GetProcAddress(GetModuleHandleW(L"ntdll"), "ZwSetTimerResolution"); + ZwSetTimerResolution = (NTSTATUS(__stdcall *)(IN ULONG, IN BOOLEAN, OUT PULONG))(void *)GetProcAddress(GetModuleHandleW(L"ntdll"), "ZwSetTimerResolution"); ULONG actual_resolution; ZwSetTimerResolution(1, TRUE, &actual_resolution); - NtDelayExecution = (NTSTATUS(__stdcall *)(BOOL, PLARGE_INTEGER))GetProcAddress(GetModuleHandleW(L"ntdll"), "NtDelayExecution"); -#if defined(__clang__) || defined(__GNUC__) -_Pragma("GCC diagnostic pop") -#endif + NtDelayExecution = (NTSTATUS(__stdcall *)(BOOL, PLARGE_INTEGER))(void *)GetProcAddress(GetModuleHandleW(L"ntdll"), "NtDelayExecution"); QueryPerformanceFrequency(&performance_frequency); + + return true; } void nn_windows_close(void) diff --git a/src/winwrap.h b/src/winwrap.h index 60179a9..0dfa8c6 100644 --- a/src/winwrap.h +++ b/src/winwrap.h @@ -1,5 +1,7 @@ #pragma once +#include <al/types.h> + // https://kirkshoop.github.io/2011/09/20/ntstatus.html #include <winsock2.h> #include <ws2tcpip.h> @@ -12,5 +14,5 @@ extern NTSTATUS(__stdcall *NtDelayExecution)(BOOL Alertable, PLARGE_INTEGER DelayInterval); extern LARGE_INTEGER performance_frequency; -void nn_windows_init(void); +bool nn_windows_init(void); void nn_windows_close(void); diff --git a/subprojects/libalabaster.wrap b/subprojects/libalabaster.wrap index adaec6a..014f5ab 100644 --- a/subprojects/libalabaster.wrap +++ b/subprojects/libalabaster.wrap @@ -1,4 +1,4 @@ [wrap-git] url = https://git.akon.city/libalabaster -revision = 31715673a5b98991ef276decbd90f026559eaf4b +revision = cf45d71c49a7e7364b315dcc45fe7eac060590df depth = 1 |