aboutsummaryrefslogtreecommitdiff
path: root/src/querymanager.cc
diff options
context:
space:
mode:
authorfusion32 <marcopuzziello@gmail.com>2025-08-01 19:23:13 -0300
committerfusion32 <marcopuzziello@gmail.com>2025-08-01 19:23:13 -0300
commit634554a0df8dfd35630f17b5fee44c54b7af73a5 (patch)
tree752503318c639afd205e72ba88c16c87d5f3e298 /src/querymanager.cc
parent82b82dca7d621bf4530416c7c5e9fc69874eecd3 (diff)
downloadquerymanager-634554a0df8dfd35630f17b5fee44c54b7af73a5.tar.gz
querymanager-634554a0df8dfd35630f17b5fee44c54b7af73a5.zip
fix problem with concurrent writes + implement website queries
There was a problem with concurrent writes not being visible until after restart, due to how cached prepared statements work with SQLite (see comment in `database.cc`).
Diffstat (limited to 'src/querymanager.cc')
-rw-r--r--src/querymanager.cc34
1 files changed, 32 insertions, 2 deletions
diff --git a/src/querymanager.cc b/src/querymanager.cc
index 5086032..1ed818e 100644
--- a/src/querymanager.cc
+++ b/src/querymanager.cc
@@ -4,6 +4,7 @@
#if OS_LINUX
# include <errno.h>
# include <signal.h>
+# include <sys/random.h>
#else
# error "Operating system not currently supported."
#endif
@@ -31,7 +32,7 @@ void LogAdd(const char *Prefix, const char *Format, ...){
vsnprintf(Entry, sizeof(Entry), Format, ap);
va_end(ap);
- if(Entry[0] != 0){
+ if(!StringEmpty(Entry)){
struct tm LocalTime = GetLocalTime(time(NULL));
fprintf(stdout, "%04d/%02d/%02d %02d:%02d:%02d [%s] %s\n",
LocalTime.tm_year + 1900, LocalTime.tm_mon + 1, LocalTime.tm_mday,
@@ -48,7 +49,7 @@ void LogAddVerbose(const char *Prefix, const char *Function,
vsnprintf(Entry, sizeof(Entry), Format, ap);
va_end(ap);
- if(Entry[0] != 0){
+ if(!StringEmpty(Entry)){
(void)File;
(void)Line;
struct tm LocalTime = GetLocalTime(time(NULL));
@@ -94,6 +95,30 @@ void SleepMS(int64 DurationMS){
#endif
}
+void CryptoRandom(uint8 *Buffer, int Count){
+#if 0 && OS_WINDOWS
+ // TODO(fusion): Not sure about this one.
+ if(BCryptGenRandom(NULL, Buffer, (ULONG)Count, BCRYPT_USE_SYSTEM_PREFERRED_RNG) != STATUS_SUCCESS){
+ PANIC("Failed to generate cryptographically safe random data.");
+ }
+#else
+ // NOTE(fusion): This shouldn't fail unless the kernel doesn't implement the
+ // required system call, in which case we should have a fallback method. See
+ // `getrandom(2)` for the whole story.
+ if((int)getrandom(Buffer, Count, 0) != Count){
+ PANIC("Failed to generate cryptographically safe random data.");
+ }
+#endif
+}
+
+int RoundSecondsToDays(int Seconds){
+ return (Seconds + 86399) / 86400;
+}
+
+bool StringEmpty(const char *String){
+ return String[0] == 0;
+}
+
bool StringEq(const char *A, const char *B){
int Index = 0;
while(true){
@@ -139,6 +164,11 @@ bool StringCopy(char *Dest, int DestCapacity, const char *Src){
}
bool ParseIPAddress(const char *String, int *OutAddr){
+ if(StringEmpty(String)){
+ LOG_ERR("Empty IP Address string");
+ return false;
+ }
+
int Addr[4];
if(sscanf(String, "%d.%d.%d.%d", &Addr[0], &Addr[1], &Addr[2], &Addr[3]) != 4){
LOG_ERR("Invalid IP Address format \"%s\"", String);