diff options
| author | 2025-01-17 11:28:50 -0500 | |
|---|---|---|
| committer | 2025-01-17 11:28:50 -0500 | |
| commit | cef6cabce7ae97e9edfb6821ce4393d714579866 (patch) | |
| tree | ddae72f25aa290a47308c7c955fb647cf9d759fe /src | |
| parent | 86dd8affaff59cf9d2657ad1c04fd62571b9a470 (diff) | |
| download | libnaunet-cef6cabce7ae97e9edfb6821ce4393d714579866.tar.gz libnaunet-cef6cabce7ae97e9edfb6821ce4393d714579866.tar.bz2 libnaunet-cef6cabce7ae97e9edfb6821ce4393d714579866.zip | |
Incorporate libalabaster changes, Windows fixes
Signed-off-by: Andrew Opalach <andrew@akon.city>
Diffstat (limited to 'src')
| -rw-r--r-- | src/common.c | 11 | ||||
| -rw-r--r-- | src/curl/http.c | 2 | ||||
| -rw-r--r-- | src/line_processor.c | 14 | ||||
| -rw-r--r-- | src/packet_cache.c | 6 | ||||
| -rw-r--r-- | src/packet_pool.c | 14 | ||||
| -rw-r--r-- | src/packet_stream.c | 8 | ||||
| -rw-r--r-- | src/socket/socket_internal.h | 6 | ||||
| -rw-r--r-- | src/socket/socket_linux.c | 10 | ||||
| -rw-r--r-- | src/socket/socket_windows.c | 4 | ||||
| -rw-r--r-- | src/util/error.c | 16 | ||||
| -rw-r--r-- | src/util/error.h | 17 | ||||
| -rw-r--r-- | src/util/file/file.h | 8 | ||||
| -rw-r--r-- | src/util/file/file_linux.c | 91 | ||||
| -rw-r--r-- | src/util/file/file_stdio.c | 47 | ||||
| -rw-r--r-- | src/util/file/util.c | 8 | ||||
| -rw-r--r-- | src/util/packet.c | 17 | ||||
| -rw-r--r-- | src/util/packet.h | 11 | ||||
| -rw-r--r-- | src/winwrap.c | 4 | ||||
| -rw-r--r-- | src/winwrap.h | 2 |
19 files changed, 170 insertions, 126 deletions
diff --git a/src/common.c b/src/common.c index 4f9baa5..7b2a958 100644 --- a/src/common.c +++ b/src/common.c @@ -9,6 +9,8 @@ #include <unistd.h> #endif +#include "util/timer/timer.h" + #include "common.h" #define RESET "\033[0m" @@ -39,7 +41,6 @@ s32 al_print_default(void *userdata, u8 level, char *message) #else ret = al_printf("%s%*.*s%s\n", get_color_for_level(level), 0, AL_LOG_MESSAGE_SIZE, message, RESET); #endif - al_free(message); return ret; } @@ -61,7 +62,9 @@ static void malloc_unlock(void) bool nn_common_init(void) { - if (!setlocale(LC_ALL, "")) return false; + if (!setlocale(LC_ALL, "")) { + return false; + } al_set_print(al_print_default, NULL); @@ -82,7 +85,9 @@ bool nn_common_init(void) #endif free, malloc_lock, malloc_unlock); #endif - al_rand_init(); + + al_rand_init((u32)(nn_get_timestamp()/1000)); + #ifdef NAUNET_ON_WINDOWS nn_windows_init(); #endif diff --git a/src/curl/http.c b/src/curl/http.c index d040f53..ad2a156 100644 --- a/src/curl/http.c +++ b/src/curl/http.c @@ -57,7 +57,7 @@ static void http_handle_events(void *userdata, struct nn_curl *curl) http->callback(http->userdata, NNWT_HTTP_ERROR, NULL, &http->status_code); return; } - nn_curl_set_url(curl, al_str_cr(redirect_url)); + nn_curl_set_url(curl, &al_str_cr(redirect_url)); http->status_code = -1L; http->content_length = -1L; diff --git a/src/line_processor.c b/src/line_processor.c index 41f516e..f4f04c5 100644 --- a/src/line_processor.c +++ b/src/line_processor.c @@ -6,7 +6,7 @@ // This isn't meant to be used seriously and is currently just for testing. #define LINE_PROCESSOR_CHUNK_SIZE 128 -#define LINE_PROCESSOR_END al_str_c("X") +static str LINE_PROCESSOR_END = al_str_c("X"); static void remove_client(struct nn_line_processor *pro, struct nn_line_processor_client *client) { @@ -48,15 +48,15 @@ static void read_callback(struct ev_loop *loop, ev_io *w, s32 revents) cl->index += ret; ptr = nn_buffer_get_ptr(&cl->buf, 0); - str s = *al_str_w((char *)ptr, 0, cl->index); + str s = al_str_w((char *)ptr, 0, cl->index); str *dl = &pro->delim; - u32 offset = dl->len - 1; + u32 offset = dl->length - 1; for (size_t i = cl->mark; i < cl->index - offset; i++) { - if (al_str_cmp(&s, dl, i, dl->len) == 0) { - s.len = i; + if (al_str_cmp(&s, dl, i, dl->length) == 0) { + s.length = i; - if (al_str_eq(&s, LINE_PROCESSOR_END)) { + if (al_str_eq(&s, &LINE_PROCESSOR_END)) { remove_client(pro, cl); return; } @@ -72,7 +72,7 @@ static void read_callback(struct ev_loop *loop, ev_io *w, s32 revents) size_t skip = i + offset + 1; cl->index = cl->index - skip; al_memmove(ptr, &ptr[skip], cl->index); - s = *al_str_w((char *)ptr, 0, cl->index); + s = al_str_w((char *)ptr, 0, cl->index); cl->mark = i = 0; } diff --git a/src/packet_cache.c b/src/packet_cache.c index 3604eb0..d46f2f4 100644 --- a/src/packet_cache.c +++ b/src/packet_cache.c @@ -21,7 +21,7 @@ bool nn_packet_cache_send_packet(struct nn_packet_cache *cache, struct nn_packet al_array_push(cache->cache, packet); - bool flush = !packet || cache->cache.size >= cache->flush; + bool flush = !packet || cache->cache.count >= cache->flush; if (flush && nn_cond_is_waiting(&cache->cond)) { nn_cond_signal(&cache->cond); } @@ -44,7 +44,7 @@ bool nn_packet_cache_wait(struct nn_packet_cache *cache, u32 *count) { nn_mutex_lock(&cache->mutex); - if (!cache->disabled && cache->cache.size == 0) { + if (!cache->disabled && !cache->cache.count) { nn_cond_wait(&cache->cond, &cache->mutex); } @@ -56,7 +56,7 @@ bool nn_packet_cache_wait(struct nn_packet_cache *cache, u32 *count) return false; } - *count = cache->cache.size; + *count = cache->cache.count; return true; } diff --git a/src/packet_pool.c b/src/packet_pool.c index db74eb9..8df520f 100644 --- a/src/packet_pool.c +++ b/src/packet_pool.c @@ -20,7 +20,7 @@ static void signal_callback(struct ev_loop *loop, ev_async *w, s32 revents) } al_array_copy(pool->sending, pool->ready); - pool->ready.size = 0; + pool->ready.count = 0; nn_mutex_unlock(&pool->mutex); @@ -28,7 +28,7 @@ static void signal_callback(struct ev_loop *loop, ev_async *w, s32 revents) al_array_foreach(pool->sending, i, packet) { pool->callback(pool->userdata, packet); } - pool->sending.size = 0; + pool->sending.count = 0; } void nn_packet_pool_init(struct nn_packet_pool *pool, u32 size, @@ -65,7 +65,7 @@ struct nn_packet *nn_packet_pool_get(struct nn_packet_pool *pool) { nn_mutex_lock(&pool->mutex); - if (!pool->disabled && !pool->empty.size) { + if (!pool->disabled && !pool->empty.count) { if (pool->grow) { al_array_push(pool->empty, nn_packet_create()); } else { @@ -97,7 +97,7 @@ void nn_packet_pool_submit(struct nn_packet_pool *pool, struct nn_packet *packet al_array_push(pool->ready, packet); - bool flush = pool->ready.size >= pool->size / 2; + bool flush = pool->ready.count >= pool->size / 2; nn_mutex_unlock(&pool->mutex); @@ -121,7 +121,7 @@ void nn_packet_pool_return(struct nn_packet_pool *pool, struct nn_packet *packet void nn_packet_pool_unlock(struct nn_packet_pool *pool) { - if (!pool->disabled && pool->empty.size > 0 && nn_cond_is_waiting(&pool->cond)) { + if (!pool->disabled && pool->empty.count > 0 && nn_cond_is_waiting(&pool->cond)) { nn_cond_signal(&pool->cond); } nn_mutex_unlock(&pool->mutex); @@ -138,7 +138,7 @@ void nn_packet_pool_disable(struct nn_packet_pool *pool) nn_packet_reset(packet); al_array_push(pool->empty, packet); } - pool->ready.size = 0; + pool->ready.count = 0; if (nn_cond_is_waiting(&pool->cond)) { nn_cond_signal(&pool->cond); @@ -157,7 +157,7 @@ void nn_packet_pool_enable(struct nn_packet_pool *pool) struct nn_packet *nn_packet_pool_pop(struct nn_packet_pool *pool) { struct nn_packet *packet = NULL; - if (pool->ready.size > 0) { + if (pool->ready.count > 0) { al_array_pop_at(pool->ready, 0, packet); } return packet; diff --git a/src/packet_stream.c b/src/packet_stream.c index e3dfa0a..0a6951a 100644 --- a/src/packet_stream.c +++ b/src/packet_stream.c @@ -81,15 +81,15 @@ static void stop_internal(struct nn_packet_stream *stream) } if (stream->packets_sent_callback) { - u32 count = stream->out.queue.size; - stream->packets_sent_callback(stream->userdata, stream->out.queue.data, count); + stream->packets_sent_callback(stream->userdata, + stream->out.queue.data, stream->out.queue.count); } else { struct nn_packet *packet; al_array_foreach(stream->out.queue, i, packet) { stream->packet_sent_callback(stream->userdata, packet); } } - stream->out.queue.size = 0; + stream->out.queue.count = 0; stream->connection_closed_callback(stream->userdata, stream); } @@ -166,7 +166,7 @@ static void stream_write_callback(struct ev_loop *loop, ev_io *w, s32 revents) } if (!stream->out.packet) { - if (stream->out.queue.size > 0) { + if (stream->out.queue.count > 0) { al_array_pop_at(stream->out.queue, 0, stream->out.packet); stream->out.index = 0; } else { diff --git a/src/socket/socket_internal.h b/src/socket/socket_internal.h index 193fa43..b79e888 100644 --- a/src/socket/socket_internal.h +++ b/src/socket/socket_internal.h @@ -4,13 +4,13 @@ #include "socket.h" -static str *ipany = al_str_c("0.0.0.0"); +static str ipany = al_str_c("0.0.0.0"); static inline str *nn_socket_display_addr(str *addr) { - return addr ? addr : ipany; + return addr ? addr : &ipany; } -#define nn_socket_printf_addr(addr) AL_STR_PRINTF(nn_socket_display_addr(addr)) +#define nn_socket_fmt_addr(addr) al_str_fmt(nn_socket_display_addr(addr)) static inline void nn_socket_apply_flags(struct nn_socket *sock, s32 flags) { diff --git a/src/socket/socket_linux.c b/src/socket/socket_linux.c index 0dadfbe..2d032f3 100644 --- a/src/socket/socket_linux.c +++ b/src/socket/socket_linux.c @@ -159,11 +159,11 @@ bool nn_socket_bind(struct nn_socket *sock, str *addr, u16 port) if (bind(sock->fd, saddr, addrlen) < 0) { al_log_error("socket", "bind(%.*s:%hu) failed: %s (%d).", - nn_socket_printf_addr(addr), port, nn_strerror(errno), errno); + nn_socket_fmt_addr(addr), port, nn_strerror(errno), errno); return false; } - al_log_info("socket", "Socket bound to %.*s:%hu.", nn_socket_printf_addr(addr), port); + al_log_info("socket", "Socket bound to %.*s:%hu.", nn_socket_fmt_addr(addr), port); return true; } @@ -235,8 +235,8 @@ bool nn_socket_connect(struct nn_socket *sock, str *addr, u16 port) break; case NNWT_SOCKET_UNIX: sock->addr_un.sun_family = AF_UNIX; - al_memcpy(sock->addr_un.sun_path, addr->data, addr->len); - sock->addr_un.sun_path[addr->len] = '\0'; + al_memcpy(sock->addr_un.sun_path, addr->data, addr->length); + sock->addr_un.sun_path[addr->length] = '\0'; ret = connect(sock->fd, (struct sockaddr *)&sock->addr_un, sizeof(sock->addr_un)); break; default: @@ -245,7 +245,7 @@ bool nn_socket_connect(struct nn_socket *sock, str *addr, u16 port) if (ret != 0 && errno != EINPROGRESS) { al_log_error("socket", "connect(%.*s:%hu) failed: %s (%d).", - nn_socket_printf_addr(addr), port, nn_strerror(errno), errno); + nn_socket_fmt_addr(addr), port, nn_strerror(errno), errno); return false; } diff --git a/src/socket/socket_windows.c b/src/socket/socket_windows.c index 28e6dc5..ef7f3bd 100644 --- a/src/socket/socket_windows.c +++ b/src/socket/socket_windows.c @@ -56,11 +56,11 @@ bool nn_socket_bind(struct nn_socket *sock, str *addr, u16 port) s32 ret = bind(sock->fd, (SOCKADDR *)&sock->addr_in, sizeof(sock->addr_in)); if (ret == SOCKET_ERROR) { al_log_error("socket", "bind(%.*s:%hu) failed: %d.", - nn_socket_printf_addr(addr), port, WSAGetLastError()); + nn_socket_fmt_addr(addr), port, WSAGetLastError()); return false; } - al_log_info("socket", "Socket bound to %.*s:%hu.", nn_socket_printf_addr(addr), port); + al_log_info("socket", "Socket bound to %.*s:%hu.", nn_socket_fmt_addr(addr), port); return true; } diff --git a/src/util/error.c b/src/util/error.c index 928e083..db2d001 100644 --- a/src/util/error.c +++ b/src/util/error.c @@ -1,3 +1,17 @@ #include "error.h" -__thread char errorbuf[NNWT_ERROR_STR_MAXLEN]; +static __thread char errorbuf[NNWT_ERROR_STR_MAXLEN]; + +char *nn_strerror(s32 errnum) +{ +#ifdef NAUNET_ON_WINDOWS + strerror_s(errorbuf, NNWT_ERROR_STR_MAXLEN, errnum); +#else +#if (_POSIX_C_SOURCE >= 200112L) && ! _GNU_SOURCE + if (strerror_r(errnum, errorbuf, NNWT_ERROR_STR_MAXLEN) != 0) errorbuf[0] = '\0'; +#else + return strerror_r(errnum, errorbuf, NNWT_ERROR_STR_MAXLEN); +#endif +#endif + return errorbuf; +} diff --git a/src/util/error.h b/src/util/error.h index 95ae282..3731a01 100644 --- a/src/util/error.h +++ b/src/util/error.h @@ -1,23 +1,8 @@ #pragma once #include <al/lib.h> - #include <errno.h> #define NNWT_ERROR_STR_MAXLEN 94 -extern __thread char errorbuf[NNWT_ERROR_STR_MAXLEN]; - -static inline char *nn_strerror(s32 errnum) -{ -#ifdef NAUNET_ON_WINDOWS - strerror_s(errorbuf, NNWT_ERROR_STR_MAXLEN, errnum); -#else -#if (_POSIX_C_SOURCE >= 200112L) && ! _GNU_SOURCE - if (strerror_r(errnum, errorbuf, NNWT_ERROR_STR_MAXLEN) != 0) errorbuf[0] = '\0'; -#else - return strerror_r(errnum, errorbuf, NNWT_ERROR_STR_MAXLEN); -#endif -#endif - return errorbuf; -} +char *nn_strerror(s32 errnum); diff --git a/src/util/file/file.h b/src/util/file/file.h index f79bfe5..13a4165 100644 --- a/src/util/file/file.h +++ b/src/util/file/file.h @@ -30,6 +30,7 @@ enum { }; struct nn_file { + s32 flags; #ifdef NAUNET_NEEDS_STDIO_ASSIST FILE *file; #else @@ -38,7 +39,6 @@ struct nn_file { #else s32 fd; #endif - bool locked; #endif str path; size_t size; @@ -65,8 +65,6 @@ struct nn_dir_entry { struct dirent *entry; #endif #endif - str name; - str path; }; bool nn_file_open(struct nn_file *file, str *path, s32 flags); @@ -90,4 +88,6 @@ void nn_dir_close(struct nn_dir *dir); bool nn_dir_exists(str *path); bool nn_dir_create(str *path); bool nn_dir_read(struct nn_dir *dir, struct nn_dir_entry *entry); -void nn_dir_entry_free(struct nn_dir_entry *entry); +void nn_dir_entry_get_name(struct nn_dir_entry *entry, str *name); +const char *nn_dir_entry_get_os_name(struct nn_dir_entry *entry); +void nn_dir_entry_get_path(struct nn_dir_entry *entry, struct nn_dir *dir, str *path); diff --git a/src/util/file/file_linux.c b/src/util/file/file_linux.c index bbc4c67..6b52c60 100644 --- a/src/util/file/file_linux.c +++ b/src/util/file/file_linux.c @@ -26,19 +26,25 @@ static bool unlock_file_internal(s32 fd, size_t size) return true; } +static bool open_file_linux(s32 *fd, str *path, s32 flags) +{ + char *c_str = al_str_to_c_str(path); + bool success = (*fd = open(c_str, flags, 0644)) != -1; + al_free(c_str); + return success; +} + bool nn_file_open(struct nn_file *file, str *path, s32 flags) { - al_assert(!((flags & NNWT_FILE_READONLY) && (flags & NNWT_FILE_LOCK))); + file->flags = flags; s32 oflags = flags & NNWT_FILE_READONLY ? O_RDONLY : O_RDWR; if (flags & NNWT_FILE_CREATE) { oflags |= O_CREAT; } - char *c_str = al_str_to_c_str(path); - file->fd = open(c_str, oflags, 0644); - al_free(c_str); - if (file->fd == -1) { - al_log_error("file", "open(%.*s) failed (%d: %s).", AL_STR_PRINTF(path), errno, nn_strerror(errno)); + 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)); return false; } @@ -51,8 +57,7 @@ bool nn_file_open(struct nn_file *file, str *path, s32 flags) } file->size = sb.st_size; - file->locked = flags & NNWT_FILE_LOCK; - if (file->locked && !lock_file_internal(file->fd, file->size)) { + if (flags & NNWT_FILE_LOCK && !lock_file_internal(file->fd, file->size)) { close(file->fd); return false; } @@ -72,19 +77,18 @@ bool nn_file_exists(str *path) bool nn_file_create(str *path) { - struct nn_file file; - if (nn_file_open(&file, path, NNWT_FILE_CREATE)) { - nn_file_close(&file); - return true; - } - return false; + s32 fd; + bool opened = open_file_linux(&fd, path, O_RDWR | O_CREAT); + if (opened) close(fd); + return opened; } 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)); + al_log_error("file", "lseek(%jd, %d) failed (%s).", + (intmax_t)offset, whence, nn_strerror(errno)); } return res; } @@ -93,7 +97,8 @@ 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)); + al_log_error("file", "ftruncate(%jd) failed (%s).", + (intmax_t)size, nn_strerror(errno)); return false; } file->size = size; @@ -130,9 +135,11 @@ bool nn_file_read(struct nn_file *file, void *buf, size_t size) void *nn_file_mmap(struct nn_file *file) { - void *map = mmap(NULL, file->size, PROT_READ | PROT_WRITE, MAP_SHARED, file->fd, 0L); + 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_PRINTF(&file->path), nn_strerror(errno)); + al_log_error("file", "mmap(%.*s) failed (%s).", + al_str_fmt(&file->path), nn_strerror(errno)); return NULL; } return map; @@ -153,13 +160,14 @@ 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)); + al_log_error("file", "munmap(%p, %zu) failed (%s).", + map, file->size, nn_strerror(errno)); } } void nn_file_close(struct nn_file *file) { - if (file->locked) { + if (file->flags & NNWT_FILE_LOCK) { unlock_file_internal(file->fd, file->size); } close(file->fd); @@ -172,7 +180,8 @@ 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_PRINTF(path), nn_strerror(errno)); + al_log_error("file", "opendir(%.*s) failed (%s).", + al_str_fmt(path), nn_strerror(errno)); return false; } al_str_clone(&dir->path, path); @@ -187,17 +196,18 @@ void nn_dir_close(struct nn_dir *dir) bool nn_dir_exists(str *path) { - struct nn_dir dir; + DIR *dir; char *c_str = al_str_to_c_str(path); - dir.dir = opendir(c_str); + dir = opendir(c_str); al_free(c_str); - if (!dir.dir) { + if (!dir) { if (errno != ENOENT) { - al_log_error("file", "opendir(%.*s) failed (%s).", AL_STR_PRINTF(path), nn_strerror(errno)); + al_log_error("file", "opendir(%.*s) failed (%s).", + al_str_fmt(path), nn_strerror(errno)); } return false; } - closedir(dir.dir); + closedir(dir); return true; } @@ -207,7 +217,8 @@ 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_PRINTF(path), nn_strerror(errno)); + al_log_error("file", "mkdir(%.*s) failed (%s).", + al_str_fmt(path), nn_strerror(errno)); return false; } return true; @@ -215,9 +226,11 @@ bool nn_dir_create(str *path) 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() failed (%s).", nn_strerror(errno)); + al_log_error("file", "readdir(%.*s) failed (%s).", + al_str_fmt(&dir->path), nn_strerror(errno)); } return false; } @@ -235,16 +248,22 @@ bool nn_dir_read(struct nn_dir *dir, struct nn_dir_entry *entry) break; } - al_str_clone(&entry->name, al_str_cr(entry->entry->d_name)); - al_str_clone(&entry->path, &dir->path); - al_str_cat(&entry->path, al_str_c("/")); - al_str_cat(&entry->path, &entry->name); - return true; } -void nn_dir_entry_free(struct nn_dir_entry *entry) +void nn_dir_entry_get_name(struct nn_dir_entry *entry, str *name) +{ + al_str_clone(name, &al_str_cr(entry->entry->d_name)); +} + +const char *nn_dir_entry_get_os_name(struct nn_dir_entry *entry) +{ + return entry->entry->d_name; +} + +void nn_dir_entry_get_path(struct nn_dir_entry *entry, struct nn_dir *dir, str *path) { - al_str_free(&entry->name); - al_str_free(&entry->path); + al_str_clone(path, &dir->path); + al_str_cat(path, &al_str_c("/")); + al_str_cat(path, &al_str_cr(entry->entry->d_name)); } diff --git a/src/util/file/file_stdio.c b/src/util/file/file_stdio.c index 2a18c81..ada5ae3 100644 --- a/src/util/file/file_stdio.c +++ b/src/util/file/file_stdio.c @@ -2,24 +2,30 @@ #include "../error.h" +#ifndef NAUNET_NEEDS_STDIO_ASSIST +#define NAUNET_NEEDS_STDIO_ASSIST +#endif #include "file.h" -bool nn_file_open(struct nn_file *file, str *path, s32 flags) +static inline bool open_stdio_file(FILE **file, str *path, const char *mode) { - const char *mode = flags & NNWT_FILE_CREATE ? "wb+" : "rb+"; char *c_str = al_str_to_c_str(path); #ifdef NAUNET_ON_WINDOWS - errno_t ret = fopen_s(&file->file, c_str, mode); + // Still sets errno, according to the microsoft docs. + errno_t ret = fopen_s(file, c_str, mode); #else - file->file = fopen(c_str, mode); + s32 ret = (*file = fopen(c_str, mode)) != NULL ? 0 : errno; #endif al_free(c_str); -#ifdef NAUNET_ON_WINDOWS - if (ret != 0) { -#else - if (!file->file) { -#endif - al_log_error("file_stdio", "fopen(%.*s) failed (%s).", AL_STR_PRINTF(path), nn_strerror(errno)); + return ret == 0; +} + +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)); return false; } @@ -36,8 +42,18 @@ bool nn_file_open(struct nn_file *file, str *path, s32 flags) bool nn_file_exists(str *path) { - (void)path; - return false; + FILE *file; + bool opened = open_stdio_file(&file, path, "r"); + if (opened) fclose(file); + return opened; +} + +bool nn_file_create(str *path) +{ + FILE *file; + bool opened = open_stdio_file(&file, path, "w+"); + if (opened) fclose(file); + return opened; } off_t nn_file_seek(struct nn_file *file, off_t offset, s32 whence) @@ -85,8 +101,7 @@ bool nn_file_read(struct nn_file *file, void *buf, size_t size) void *nn_file_mmap(struct nn_file *file) { (void)file; - al_assert(false); - return NULL; + al_assert_and_return(NULL); } void nn_file_close(struct nn_file *file) @@ -99,4 +114,6 @@ void nn_dir_close(struct nn_dir *dir) { (void)dir; } bool nn_dir_exists(str *path) { (void)path; return false; } bool nn_dir_create(str *path) { (void)path; return false; } bool nn_dir_read(struct nn_dir *dir, struct nn_dir_entry *entry) { (void)dir; (void)entry; return false; } -void nn_dir_entry_free(struct nn_dir_entry *entry) { (void)entry; } +void nn_dir_entry_get_name(struct nn_dir_entry *entry, str *name) { (void)entry; (void)name; }; +const char *nn_dir_entry_get_os_name(struct nn_dir_entry *entry) { (void)entry; return NULL; }; +void nn_dir_entry_get_path(struct nn_dir_entry *entry, struct nn_dir *dir, str *path) { (void)entry; (void)dir; (void)path; } diff --git a/src/util/file/util.c b/src/util/file/util.c index 93d0825..cf2d31c 100644 --- a/src/util/file/util.c +++ b/src/util/file/util.c @@ -8,7 +8,7 @@ s32 nn_file_read_as_str(struct nn_file *file, str *out) al_str_free(out); return -1; } - out->len = size; + out->length = size; return size; } @@ -27,9 +27,9 @@ s32 nn_file_read_as_c_str(struct nn_file *file, char **out) s32 nn_file_replace(struct nn_file *file, str *buf) { if ((nn_file_seek(file, 0, SEEK_SET) == (off_t)-1) || - (!nn_file_write(file, buf->data, buf->len)) || - (!nn_file_truncate(file, buf->len))) { + (!nn_file_write(file, buf->data, buf->length)) || + (!nn_file_truncate(file, buf->length))) { return -1; } - return buf->len; + return buf->length; } diff --git a/src/util/packet.c b/src/util/packet.c index 5fe63de..1516f0f 100644 --- a/src/util/packet.c +++ b/src/util/packet.c @@ -57,8 +57,8 @@ DEFINE_PACKET_WRITE_FUNC(f64) void nn_packet_write_str(struct nn_packet *packet, str *s) { - NNWT_PACKET_WRITE_TYPE(packet, u32, s->len); - NNWT_PACKET_WRITE_DATA(packet, s->data, s->len); + NNWT_PACKET_WRITE_TYPE(packet, u32, s->length); + NNWT_PACKET_WRITE_DATA(packet, s->data, s->length); } void nn_packet_write_buffer(struct nn_packet *packet, struct nn_buffer *buf) @@ -92,6 +92,7 @@ DEFINE_PACKET_READ_FUNC(u16) DEFINE_PACKET_READ_FUNC(s16) DEFINE_PACKET_READ_FUNC(u32) DEFINE_PACKET_READ_FUNC(s32) +DEFINE_PACKET_READ_FUNC_EXT(s32) DEFINE_PACKET_READ_FUNC(u64) DEFINE_PACKET_READ_FUNC(s64) DEFINE_PACKET_READ_FUNC(f32) @@ -99,8 +100,8 @@ DEFINE_PACKET_READ_FUNC(f64) void nn_packet_read_str(struct nn_packet *packet, str *s) { - NNWT_PACKET_READ_TYPE(packet, u32, s->len); - NNWT_PACKET_READ_DATA(packet, s->len, s->data); + NNWT_PACKET_READ_TYPE(packet, u32, s->length); + NNWT_PACKET_READ_DATA(packet, s->length, s->data); s->alloc = 0; } @@ -116,14 +117,14 @@ void nn_packet_read_buffer(struct nn_packet *packet, struct nn_buffer *buf) void nn_packet_write_wstr(struct nn_packet *packet, wstr *w) { - NNWT_PACKET_WRITE_TYPE(packet, u32, w->len); - NNWT_PACKET_WRITE_DATA(packet, w->data, w->len * sizeof(wchar_t)); + NNWT_PACKET_WRITE_TYPE(packet, u32, w->length); + NNWT_PACKET_WRITE_DATA(packet, w->data, w->length * sizeof(wchar_t)); } void nn_packet_read_wstr(struct nn_packet *packet, wstr *w) { - NNWT_PACKET_READ_TYPE(packet, u32, w->len); - NNWT_PACKET_READ_DATA(packet, w->len * sizeof(wchar_t), w->data); + NNWT_PACKET_READ_TYPE(packet, u32, w->length); + NNWT_PACKET_READ_DATA(packet, w->length * sizeof(wchar_t), w->data); w->alloc = 0; } diff --git a/src/util/packet.h b/src/util/packet.h index 4ae9776..8af502c 100644 --- a/src/util/packet.h +++ b/src/util/packet.h @@ -23,9 +23,9 @@ struct nn_packet { nn_buffer_write(&(p)->buffer, &(v), (p)->windex, sizeof(type)); \ (p)->windex += (u32)sizeof(type) -#define NNWT_PACKET_WRITE_DATA(p, data, len) \ - nn_buffer_write(&(p)->buffer, (data), (p)->windex, (len)); \ - (p)->windex += (u32)len +#define NNWT_PACKET_WRITE_DATA(p, data, length) \ + nn_buffer_write(&(p)->buffer, (data), (p)->windex, (length)); \ + (p)->windex += (u32)length #define NNWT_PACKET_READ_TYPE(p, type, r) \ r = *((type *)nn_buffer_get_ptr(&(p)->buffer, (p)->rindex)); \ @@ -34,9 +34,9 @@ struct nn_packet { #define NNWT_PACKET_PEEK_TYPE(p, type, r) \ r = *((type *)nn_buffer_get_ptr(&(p)->buffer, (p)->rindex)); -#define NNWT_PACKET_READ_DATA(p, len, r) \ +#define NNWT_PACKET_READ_DATA(p, length, r) \ r = (void *)nn_buffer_get_ptr(&(p)->buffer, (p)->rindex); \ - (p)->rindex += (u32)len; + (p)->rindex += (u32)length; struct nn_packet *nn_packet_create(void); struct nn_packet *nn_packet_clone(struct nn_packet *packet); @@ -66,6 +66,7 @@ u8 nn_packet_peek_u8(struct nn_packet *packet); s16 nn_packet_read_s16(struct nn_packet *packet); u16 nn_packet_read_u16(struct nn_packet *packet); s32 nn_packet_read_s32(struct nn_packet *packet); +s32 nn_packet_peek_s32(struct nn_packet *packet); u32 nn_packet_read_u32(struct nn_packet *packet); s64 nn_packet_read_s64(struct nn_packet *packet); u64 nn_packet_read_u64(struct nn_packet *packet); diff --git a/src/winwrap.c b/src/winwrap.c index c695b83..1a97c94 100644 --- a/src/winwrap.c +++ b/src/winwrap.c @@ -3,7 +3,7 @@ void 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); + //CoInitializeEx(NULL, COINIT_MULTITHREADED | COINIT_SPEED_OVER_MEMORY | COINIT_DISABLE_OLE1DDE); WSADATA wsa_data; WSAStartup(MAKEWORD(2, 2), &wsa_data); @@ -30,7 +30,7 @@ void nn_windows_close(void) { // Still crashes on Windows. //WSACleanup(); - CoUninitialize(); + //CoUninitialize(); } LARGE_INTEGER performance_frequency; diff --git a/src/winwrap.h b/src/winwrap.h index e47cd76..430d196 100644 --- a/src/winwrap.h +++ b/src/winwrap.h @@ -1,5 +1,7 @@ #pragma once +// https://kirkshoop.github.io/2011/09/20/ntstatus.html +#define NOMINMAX #include <winsock2.h> #include <ws2tcpip.h> #include <io.h> |