[FIX/WIP] Temporary fix to the database system

It used to crash, my bad.
This commit is contained in:
LDA 2024-08-10 09:24:42 +02:00
parent 2b061f1226
commit f6af2cd782
2 changed files with 17 additions and 21 deletions

View file

@ -434,6 +434,7 @@ DbInit(Db *db)
db->mostRecent = NULL; db->mostRecent = NULL;
db->leastRecent = NULL; db->leastRecent = NULL;
db->cacheSize = 0; db->cacheSize = 0;
db->maxCache = 0;
if (db->maxCache) if (db->maxCache)
{ {
@ -460,15 +461,9 @@ DbRefInit(Db *db, DbRef *ref)
ref->ts = UtilTsMillis(); ref->ts = UtilTsMillis();
ref->size = 0; ref->size = 0;
if (db->mostRecent) /* TODO: Append the ref to the cache list.
{ * I removed it because it broke everything and crashed all the time.
db->mostRecent->next = ref; * My bad! */
}
if (!db->leastRecent)
{
db->leastRecent = ref;
}
db->mostRecent = ref;
} }
void void
StringArrayAppend(Array *arr, char *str) StringArrayAppend(Array *arr, char *str)

View file

@ -106,7 +106,7 @@ FlatLock(Db *d, Array *dir)
FlatDb *db = (FlatDb *) d; FlatDb *db = (FlatDb *) d;
FlatDbRef *ref = NULL; FlatDbRef *ref = NULL;
size_t i; size_t i;
char *path; char *path = NULL;
if (!d || !dir) if (!d || !dir)
{ {
return NULL; return NULL;
@ -126,6 +126,11 @@ FlatLock(Db *d, Array *dir)
} }
stream = StreamFd(fd); stream = StreamFd(fd);
if (!stream)
{
ref = NULL;
goto end;
}
lock.l_start = 0; lock.l_start = 0;
lock.l_len = 0; lock.l_len = 0;
@ -140,11 +145,11 @@ FlatLock(Db *d, Array *dir)
} }
ref = Malloc(sizeof(*ref)); ref = Malloc(sizeof(*ref));
DbRefInit(d, &(ref->base)); DbRefInit(d, (DbRef *) ref);
ref->fd = fd;
ref->stream = stream;
ref->base.ts = UtilLastModified(path); ref->base.ts = UtilLastModified(path);
ref->base.json = JsonDecode(stream); ref->base.json = JsonDecode(stream);
ref->stream = stream;
ref->fd = fd;
if (!ref->base.json) if (!ref->base.json)
{ {
Free(ref); Free(ref);
@ -157,10 +162,7 @@ FlatLock(Db *d, Array *dir)
ref->base.name = ArrayCreate(); ref->base.name = ArrayCreate();
for (i = 0; i < ArraySize(dir); i++) for (i = 0; i < ArraySize(dir); i++)
{ {
ArrayAdd( StringArrayAppend(ref->base.name, ArrayGet(dir, i));
ref->base.name,
StrDuplicate(ArrayGet(dir, i))
);
} }
} }
end: end:
@ -246,7 +248,6 @@ FlatCreate(Db *d, Array *dir)
/* FlatLock() will lock again for us */ /* FlatLock() will lock again for us */
pthread_mutex_unlock(&d->lock); pthread_mutex_unlock(&d->lock);
ret = FlatLock(d, dir); ret = FlatLock(d, dir);
return ret; return ret;
@ -255,7 +256,7 @@ FlatCreate(Db *d, Array *dir)
static bool static bool
FlatDelete(Db *d, Array *dir) FlatDelete(Db *d, Array *dir)
{ {
bool ret = true; bool ret = false;
char *file; char *file;
FlatDb *db = (FlatDb *) d; FlatDb *db = (FlatDb *) d;
if (!d || !dir) if (!d || !dir)
@ -273,7 +274,7 @@ FlatDelete(Db *d, Array *dir)
} }
Free(file); Free(file);
pthread_mutex_lock(&d->lock); pthread_mutex_unlock(&d->lock);
return ret; return ret;
} }
@ -355,7 +356,7 @@ DbOpen(char *dir, size_t cache)
return NULL; return NULL;
} }
db = Malloc(sizeof(*db)); db = Malloc(sizeof(*db));
DbInit(&(db->base)); DbInit((Db *) db);
db->dir = dir; db->dir = dir;
db->base.cacheSize = cache; db->base.cacheSize = cache;