From fd32b65f5f29dbe8d80438863213ff7ed3074f4f Mon Sep 17 00:00:00 2001 From: ax-6 Date: Fri, 2 Aug 2024 22:45:01 +0800 Subject: [PATCH] Fixed many bugs in file, io, logging and time. --- aqvm/base/file/file.c | 16 ++++++------- aqvm/base/io/io.c | 21 ++++++++++++----- aqvm/base/logging/logging.c | 20 ++++++++++++---- aqvm/base/process/file_lock/unix/file_lock.c | 24 +++++++++++++++----- aqvm/base/time/time.c | 8 +++---- aqvm/base/time/unix/time.c | 2 +- 6 files changed, 62 insertions(+), 29 deletions(-) diff --git a/aqvm/base/file/file.c b/aqvm/base/file/file.c index 5b6c8f5..9b3ac80 100644 --- a/aqvm/base/file/file.c +++ b/aqvm/base/file/file.c @@ -46,11 +46,11 @@ int AqvmBaseFile_LockStream(struct AqvmBaseFile_File* stream) { return -1; } if (stream->file == stdin) { - if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_inputMutex) != 0) { + if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_inputMutex) != 0) { // TODO return -2; } -}else if (stream->file == stdout || stream->file == stderr) { + } else if (stream->file == stdout || stream->file == stderr) { if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_outputMutex) != 0) { // TODO return -3; @@ -60,7 +60,7 @@ int AqvmBaseFile_LockStream(struct AqvmBaseFile_File* stream) { // TODO return -4; } - if (AqvmBaseFile_LockStream(stream) != 0) { + if (AqvmBaseFile_LockFile(stream) != 0) { // TODO return -5; } @@ -75,21 +75,21 @@ int AqvmBaseFile_UnlockStream(struct AqvmBaseFile_File* stream) { return -1; } if (stream->file == stdin) { - if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_inputMutex) != 0) { + if (AqvmBaseThreadingMutex_UnlockMutex(&AqvmBaseIo_inputMutex) != 0) { // TODO return -2; } -} else if (stream->file == stdout || stream->file == stderr) { - if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_outputMutex) != 0) { + } else if (stream->file == stdout || stream->file == stderr) { + if (AqvmBaseThreadingMutex_UnlockMutex(&AqvmBaseIo_outputMutex) != 0) { // TODO return -3; } } else { - if (AqvmBaseProcessFileLock_LockFile(stream) != 0) { + if (AqvmBaseProcessFileLock_UnlockFile(stream) != 0) { // TODO return -4; } - if (AqvmBaseFile_LockStream(stream) != 0) { + if (AqvmBaseFile_UnlockFile(stream) != 0) { // TODO return -5; } diff --git a/aqvm/base/io/io.c b/aqvm/base/io/io.c index 321b977..b899283 100644 --- a/aqvm/base/io/io.c +++ b/aqvm/base/io/io.c @@ -11,8 +11,7 @@ #include "aqvm/base/file/file.h" AqvmBaseThreadingMutex_Mutex AqvmBaseIo_inputMutex; -AqvmBaseThreadingMutex_Mutex -AqvmBaseIo_outputMutex; +AqvmBaseThreadingMutex_Mutex AqvmBaseIo_outputMutex; struct AqvmBaseFile_File AqvmBaseIo_stdoutStream; struct AqvmBaseFile_File* AqvmBaseIo_stdout = &AqvmBaseIo_stdoutStream; @@ -75,25 +74,35 @@ int AqvmBaseIo_OutputLog(struct AqvmBaseFile_File* stream, const char* time, } char* name = va_arg(system_info, char*); - char* value = va_arg(system_info, char*); + char* value = NULL; + if (name != NULL) { + value = va_arg(system_info, char*); + } while (name != NULL && value != NULL) { if (fprintf(stream->file, ",\"%s\":\"%s\"", name, value) != 0) { // TODO return -4; } name = va_arg(system_info, char*); - value = va_arg(system_info, char*); + if (name != NULL) { + value = va_arg(system_info, char*); + } } name = va_arg(other_info, char*); - value = va_arg(other_info, char*); + value = NULL; + if (name != NULL) { + value = va_arg(system_info, char*); + } while (name != NULL && value != NULL) { if (fprintf(stream->file, ",\"%s\":\"%s\"", name, value) != 0) { // TODO return -5; } name = va_arg(other_info, char*); - value = va_arg(other_info, char*); + if (name != NULL) { + value = va_arg(system_info, char*); + } } if (fprintf(stream->file, "}\n") < 0) { diff --git a/aqvm/base/logging/logging.c b/aqvm/base/logging/logging.c index ba63802..1c0f89b 100644 --- a/aqvm/base/logging/logging.c +++ b/aqvm/base/logging/logging.c @@ -44,10 +44,22 @@ void AqvmBaseLogging_ProcessLog(const char* time, const char* type, va_list system_info; va_start(system_info, other_info); - AqvmBaseLogging_OutputLogToConsole(time, type, code, message, other_info, - system_info); - AqvmBaseLogging_OutputLogToFile(time, type, code, message, other_info, - system_info); + + va_list system_info_to_console; + va_list other_info_to_console; + va_copy(system_info_to_console, system_info); + va_copy(other_info_to_console, other_info); + + va_list system_info_to_file; + va_list other_info_to_file; + va_copy(system_info_to_file, system_info); + va_copy(other_info_to_file, other_info); + + AqvmBaseLogging_OutputLogToConsole( + time, type, code, message, other_info_to_console, system_info_to_console); + AqvmBaseLogging_OutputLogToFile(time, type, code, message, other_info_to_file, + system_info_to_file); + va_end(system_info); } diff --git a/aqvm/base/process/file_lock/unix/file_lock.c b/aqvm/base/process/file_lock/unix/file_lock.c index 1d75f42..2a7fcf8 100644 --- a/aqvm/base/process/file_lock/unix/file_lock.c +++ b/aqvm/base/process/file_lock/unix/file_lock.c @@ -7,39 +7,51 @@ #include #include +#include #include "aqvm/base/file/file.h" int AqvmBaseProcessFileLockUnix_LockFile(struct AqvmBaseFile_File* file) { - if (file == NULL || file->file == NULL) { + if (AqvmBaseFile_CheckStream(file) != 0) { // TODO return -1; } + if (file->file == stdin || file->file == stdout || file->file == stderr) { + // TODO + return -2; + } int file_descriptor = fileno(file->file); if (file_descriptor == -1) { - return -2; + return -3; } struct flock file_lock = {0}; file_lock.l_type = F_WRLCK; file_lock.l_whence = SEEK_SET; + + if (fcntl(file_descriptor, F_SETLK, &file_lock) == -1) { - return -3; + printf("%i",errno); + return -4; } return 0; } int AqvmBaseProcessFileLockUnix_UnlockFile(struct AqvmBaseFile_File* file) { - if (file == NULL || file->file == NULL) { + if (AqvmBaseFile_CheckStream(file) != 0) { // TODO return -1; } + if (file->file == stdin || file->file == stdout || file->file == stderr) { + // TODO + return -2; + } int file_descriptor = fileno(file->file); if (file_descriptor == -1) { - return -2; + return -3; } struct flock file_lock = {0}; @@ -47,7 +59,7 @@ int AqvmBaseProcessFileLockUnix_UnlockFile(struct AqvmBaseFile_File* file) { if (fcntl(file_descriptor, F_SETLK, &file_lock) == -1) { // TODO - return -3; + return -4; } return 0; } diff --git a/aqvm/base/time/time.c b/aqvm/base/time/time.c index 00bc0b1..93c34a6 100644 --- a/aqvm/base/time/time.c +++ b/aqvm/base/time/time.c @@ -16,12 +16,12 @@ int AqvmBaseTime_localtime(const time_t timestamp, struct tm* result) { #ifdef __unix__ - if (AqvmBaseTimeUnix_localtime(timestamp, result)) { + if (AqvmBaseTimeUnix_localtime(timestamp, result) != 0) { // TODO return -1; } #elif _WIN32 - if (AqvmBaseTimeWindows_localtime(timestamp, result)) { + if (AqvmBaseTimeWindows_localtime(timestamp, result) != 0) { // TODO return -2; } @@ -41,13 +41,13 @@ int AqvmBaseTime_localtime(const time_t timestamp, struct tm* result) { } int AqvmBaseTime_GetCurrentTimeString(char* result) { - if(result == NULL){ + if (result == NULL) { // TODO return -1; } struct tm local_time; - if(AqvmBaseTime_localtime(time(NULL), &local_time)){ + if (AqvmBaseTime_localtime(time(NULL), &local_time)) { // TODO return -2; } diff --git a/aqvm/base/time/unix/time.c b/aqvm/base/time/unix/time.c index aeb37ad..5d77e40 100644 --- a/aqvm/base/time/unix/time.c +++ b/aqvm/base/time/unix/time.c @@ -11,7 +11,7 @@ #include "aqvm/base/time/time.h" int AqvmBaseTimeUnix_localtime(const time_t timestamp, struct tm* result) { - if (localtime_r(×tamp, result) != 0) { + if (localtime_r(×tamp, result) == NULL) { // TODO return -1; }