From f6af2cd78235f148750c69c946a5e0090d846cc2 Mon Sep 17 00:00:00 2001 From: LDA Date: Sat, 10 Aug 2024 09:24:42 +0200 Subject: [PATCH] [FIX/WIP] Temporary fix to the database system It used to crash, my bad. --- src/Db/Db.c | 13 ++++--------- src/Db/Flat.c | 25 +++++++++++++------------ 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/Db/Db.c b/src/Db/Db.c index 4b9b94a..d5fa449 100644 --- a/src/Db/Db.c +++ b/src/Db/Db.c @@ -434,6 +434,7 @@ DbInit(Db *db) db->mostRecent = NULL; db->leastRecent = NULL; db->cacheSize = 0; + db->maxCache = 0; if (db->maxCache) { @@ -460,15 +461,9 @@ DbRefInit(Db *db, DbRef *ref) ref->ts = UtilTsMillis(); ref->size = 0; - if (db->mostRecent) - { - db->mostRecent->next = ref; - } - if (!db->leastRecent) - { - db->leastRecent = ref; - } - db->mostRecent = ref; + /* TODO: Append the ref to the cache list. + * I removed it because it broke everything and crashed all the time. + * My bad! */ } void StringArrayAppend(Array *arr, char *str) diff --git a/src/Db/Flat.c b/src/Db/Flat.c index 4ecf19c..dbe8189 100644 --- a/src/Db/Flat.c +++ b/src/Db/Flat.c @@ -106,7 +106,7 @@ FlatLock(Db *d, Array *dir) FlatDb *db = (FlatDb *) d; FlatDbRef *ref = NULL; size_t i; - char *path; + char *path = NULL; if (!d || !dir) { return NULL; @@ -126,6 +126,11 @@ FlatLock(Db *d, Array *dir) } stream = StreamFd(fd); + if (!stream) + { + ref = NULL; + goto end; + } lock.l_start = 0; lock.l_len = 0; @@ -140,11 +145,11 @@ FlatLock(Db *d, Array *dir) } ref = Malloc(sizeof(*ref)); - DbRefInit(d, &(ref->base)); - ref->fd = fd; - ref->stream = stream; + DbRefInit(d, (DbRef *) ref); ref->base.ts = UtilLastModified(path); ref->base.json = JsonDecode(stream); + ref->stream = stream; + ref->fd = fd; if (!ref->base.json) { Free(ref); @@ -157,10 +162,7 @@ FlatLock(Db *d, Array *dir) ref->base.name = ArrayCreate(); for (i = 0; i < ArraySize(dir); i++) { - ArrayAdd( - ref->base.name, - StrDuplicate(ArrayGet(dir, i)) - ); + StringArrayAppend(ref->base.name, ArrayGet(dir, i)); } } end: @@ -246,7 +248,6 @@ FlatCreate(Db *d, Array *dir) /* FlatLock() will lock again for us */ pthread_mutex_unlock(&d->lock); - ret = FlatLock(d, dir); return ret; @@ -255,7 +256,7 @@ FlatCreate(Db *d, Array *dir) static bool FlatDelete(Db *d, Array *dir) { - bool ret = true; + bool ret = false; char *file; FlatDb *db = (FlatDb *) d; if (!d || !dir) @@ -273,7 +274,7 @@ FlatDelete(Db *d, Array *dir) } Free(file); - pthread_mutex_lock(&d->lock); + pthread_mutex_unlock(&d->lock); return ret; } @@ -355,7 +356,7 @@ DbOpen(char *dir, size_t cache) return NULL; } db = Malloc(sizeof(*db)); - DbInit(&(db->base)); + DbInit((Db *) db); db->dir = dir; db->base.cacheSize = cache;