2022-10-24 16:44:29 +00:00
|
|
|
/*
|
2022-12-26 15:52:52 +00:00
|
|
|
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
2022-10-24 16:44:29 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person
|
|
|
|
* obtaining a copy of this software and associated documentation files
|
|
|
|
* (the "Software"), to deal in the Software without restriction,
|
|
|
|
* including without limitation the rights to use, copy, modify, merge,
|
|
|
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
* and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include <Db.h>
|
|
|
|
|
|
|
|
#include <Memory.h>
|
2022-11-06 00:47:17 +00:00
|
|
|
#include <Json.h>
|
2022-11-15 18:20:05 +00:00
|
|
|
#include <Util.h>
|
2023-01-08 04:38:06 +00:00
|
|
|
#include <Str.h>
|
2022-10-24 16:44:29 +00:00
|
|
|
|
2023-03-01 19:33:25 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
2022-10-24 16:44:29 +00:00
|
|
|
#include <pthread.h>
|
2022-11-21 16:13:11 +00:00
|
|
|
#include <fcntl.h>
|
2022-11-22 22:31:36 +00:00
|
|
|
#include <unistd.h>
|
2022-10-24 16:44:29 +00:00
|
|
|
|
2023-02-27 15:39:12 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2022-10-24 16:44:29 +00:00
|
|
|
struct Db
|
|
|
|
{
|
2022-11-15 18:20:05 +00:00
|
|
|
char *dir;
|
2022-10-24 16:44:29 +00:00
|
|
|
pthread_mutex_t lock;
|
2022-10-31 12:12:42 +00:00
|
|
|
|
2022-11-18 20:42:08 +00:00
|
|
|
size_t cacheSize;
|
|
|
|
size_t maxCache;
|
2022-10-31 12:12:42 +00:00
|
|
|
HashMap *cache;
|
2022-11-18 20:42:08 +00:00
|
|
|
|
2022-12-15 22:14:16 +00:00
|
|
|
/*
|
|
|
|
* The cache uses a double linked list (see DbRef
|
|
|
|
* below) to know which objects are most and least
|
|
|
|
* recently used. The following diagram helps me
|
|
|
|
* know what way all the pointers go, because it
|
|
|
|
* can get very confusing sometimes. For example,
|
|
|
|
* there's nothing stopping "next" from pointing to
|
|
|
|
* least recent, and "prev" from pointing to most
|
|
|
|
* recent, so hopefully this clarifies the pointer
|
|
|
|
* terminology used when dealing with the linked
|
|
|
|
* list:
|
|
|
|
*
|
|
|
|
* mostRecent leastRecent
|
|
|
|
* | prev prev | prev
|
|
|
|
* +---+ ---> +---+ ---> +---+ ---> NULL
|
|
|
|
* |ref| |ref| |ref|
|
|
|
|
* NULL <--- +---+ <--- +---+ <--- +---+
|
|
|
|
* next next next
|
|
|
|
*/
|
2022-11-18 20:42:08 +00:00
|
|
|
DbRef *mostRecent;
|
|
|
|
DbRef *leastRecent;
|
2022-10-24 16:44:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct DbRef
|
|
|
|
{
|
2022-10-31 12:12:42 +00:00
|
|
|
HashMap *json;
|
2022-10-24 16:44:29 +00:00
|
|
|
pthread_mutex_t lock;
|
2022-10-31 12:12:42 +00:00
|
|
|
|
|
|
|
unsigned long ts;
|
2022-11-15 18:20:05 +00:00
|
|
|
size_t size;
|
|
|
|
|
2022-11-21 20:31:37 +00:00
|
|
|
Array *name;
|
2022-11-18 20:42:08 +00:00
|
|
|
|
|
|
|
DbRef *prev;
|
|
|
|
DbRef *next;
|
2022-11-21 16:13:11 +00:00
|
|
|
|
|
|
|
FILE *fp;
|
2022-10-24 16:44:29 +00:00
|
|
|
};
|
|
|
|
|
2022-12-15 20:29:19 +00:00
|
|
|
static void
|
2022-12-15 20:31:51 +00:00
|
|
|
StringArrayFree(Array * arr)
|
2022-12-15 20:29:19 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < ArraySize(arr); i++)
|
|
|
|
{
|
|
|
|
Free(ArrayGet(arr, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayFree(arr);
|
|
|
|
}
|
|
|
|
|
2022-11-12 18:26:31 +00:00
|
|
|
static ssize_t DbComputeSize(HashMap *);
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
DbComputeSizeOfValue(JsonValue * val)
|
|
|
|
{
|
|
|
|
MemoryInfo *a;
|
|
|
|
ssize_t total = 0;
|
|
|
|
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
Array *arr;
|
|
|
|
} u;
|
|
|
|
|
|
|
|
if (!val)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
a = MemoryInfoGet(val);
|
|
|
|
if (a)
|
|
|
|
{
|
|
|
|
total += MemoryInfoGetSize(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (JsonValueType(val))
|
|
|
|
{
|
|
|
|
case JSON_OBJECT:
|
|
|
|
total += DbComputeSize(JsonValueAsObject(val));
|
|
|
|
break;
|
|
|
|
case JSON_ARRAY:
|
|
|
|
u.arr = JsonValueAsArray(val);
|
|
|
|
a = MemoryInfoGet(u.arr);
|
|
|
|
|
|
|
|
if (a)
|
|
|
|
{
|
|
|
|
total += MemoryInfoGetSize(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < ArraySize(u.arr); i++)
|
|
|
|
{
|
|
|
|
total += DbComputeSizeOfValue(ArrayGet(u.arr, i));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case JSON_STRING:
|
|
|
|
u.str = JsonValueAsString(val);
|
|
|
|
a = MemoryInfoGet(u.str);
|
|
|
|
if (a)
|
|
|
|
{
|
|
|
|
total += MemoryInfoGetSize(a);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case JSON_NULL:
|
|
|
|
case JSON_INTEGER:
|
|
|
|
case JSON_FLOAT:
|
|
|
|
case JSON_BOOLEAN:
|
|
|
|
default:
|
|
|
|
/* These don't use any extra heap space */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2022-11-06 00:47:17 +00:00
|
|
|
static ssize_t
|
2022-11-08 01:05:28 +00:00
|
|
|
DbComputeSize(HashMap * json)
|
2022-11-06 00:47:17 +00:00
|
|
|
{
|
2022-11-08 01:05:28 +00:00
|
|
|
char *key;
|
|
|
|
JsonValue *val;
|
|
|
|
MemoryInfo *a;
|
|
|
|
size_t total;
|
|
|
|
|
|
|
|
if (!json)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
total = 0;
|
|
|
|
|
|
|
|
a = MemoryInfoGet(json);
|
|
|
|
if (a)
|
|
|
|
{
|
|
|
|
total += MemoryInfoGetSize(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (HashMapIterate(json, &key, (void **) &val))
|
|
|
|
{
|
|
|
|
a = MemoryInfoGet(key);
|
|
|
|
if (a)
|
|
|
|
{
|
|
|
|
total += MemoryInfoGetSize(a);
|
|
|
|
}
|
|
|
|
|
2022-11-12 18:26:31 +00:00
|
|
|
total += DbComputeSizeOfValue(val);
|
2022-11-08 01:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
2022-11-06 00:47:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 18:20:05 +00:00
|
|
|
static char *
|
2022-11-21 20:31:37 +00:00
|
|
|
DbHashKey(Array * args)
|
2022-11-15 18:20:05 +00:00
|
|
|
{
|
2022-11-21 20:31:37 +00:00
|
|
|
size_t i;
|
|
|
|
char *str = NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < ArraySize(args); i++)
|
|
|
|
{
|
2023-01-08 04:38:06 +00:00
|
|
|
char *tmp = StrConcat(2, str, ArrayGet(args, i));
|
2022-11-21 20:31:37 +00:00
|
|
|
|
|
|
|
Free(str);
|
|
|
|
str = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
2022-11-15 18:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2023-03-01 19:33:25 +00:00
|
|
|
DbDirName(Db * db, Array * args, size_t strip)
|
2022-11-15 18:20:05 +00:00
|
|
|
{
|
2022-11-21 20:31:37 +00:00
|
|
|
size_t i;
|
2023-01-08 04:38:06 +00:00
|
|
|
char *str = StrConcat(2, db->dir, "/");
|
2022-11-21 20:31:37 +00:00
|
|
|
|
2023-03-01 19:33:25 +00:00
|
|
|
for (i = 0; i < ArraySize(args) - strip; i++)
|
2022-11-21 20:31:37 +00:00
|
|
|
{
|
2023-01-07 03:17:06 +00:00
|
|
|
char *tmp;
|
2022-11-15 18:20:05 +00:00
|
|
|
|
2023-01-08 04:38:06 +00:00
|
|
|
tmp = StrConcat(3, str, ArrayGet(args, i), "/");
|
2022-11-15 18:20:05 +00:00
|
|
|
|
2022-11-21 20:31:37 +00:00
|
|
|
Free(str);
|
2022-11-15 18:20:05 +00:00
|
|
|
|
2023-01-07 03:17:06 +00:00
|
|
|
str = tmp;
|
2022-11-21 20:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
DbFileName(Db * db, Array * args)
|
|
|
|
{
|
|
|
|
size_t i;
|
2023-01-08 04:38:06 +00:00
|
|
|
char *str = StrConcat(2, db->dir, "/");
|
2022-11-21 20:31:37 +00:00
|
|
|
|
|
|
|
for (i = 0; i < ArraySize(args); i++)
|
|
|
|
{
|
2023-01-07 03:17:06 +00:00
|
|
|
char *tmp;
|
2023-01-08 04:38:06 +00:00
|
|
|
char *arg = StrDuplicate(ArrayGet(args, i));
|
2022-12-15 20:29:19 +00:00
|
|
|
size_t j = 0;
|
2022-11-21 20:31:37 +00:00
|
|
|
|
2022-12-15 16:07:45 +00:00
|
|
|
/* Sanitize name to prevent directory traversal attacks */
|
2022-12-15 20:29:19 +00:00
|
|
|
while (arg[j])
|
2022-12-15 16:07:45 +00:00
|
|
|
{
|
2022-12-15 20:29:19 +00:00
|
|
|
switch (arg[j])
|
2022-12-15 16:07:45 +00:00
|
|
|
{
|
|
|
|
case '/':
|
2022-12-15 20:29:19 +00:00
|
|
|
arg[j] = '_';
|
2022-12-15 16:07:45 +00:00
|
|
|
break;
|
|
|
|
case '.':
|
2022-12-15 20:29:19 +00:00
|
|
|
arg[j] = '-';
|
2022-12-15 16:07:45 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2022-12-15 20:29:19 +00:00
|
|
|
j++;
|
2022-12-15 16:07:45 +00:00
|
|
|
}
|
|
|
|
|
2023-01-08 04:38:06 +00:00
|
|
|
tmp = StrConcat(3, str, arg,
|
|
|
|
(i < ArraySize(args) - 1) ? "/" : ".json");
|
2022-11-21 20:31:37 +00:00
|
|
|
|
2022-12-15 16:07:45 +00:00
|
|
|
Free(arg);
|
2022-11-21 20:31:37 +00:00
|
|
|
Free(str);
|
|
|
|
|
2023-01-07 03:17:06 +00:00
|
|
|
str = tmp;
|
2022-11-21 20:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
2022-11-15 18:20:05 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 20:42:08 +00:00
|
|
|
static void
|
|
|
|
DbCacheEvict(Db * db)
|
|
|
|
{
|
|
|
|
DbRef *ref = db->leastRecent;
|
2022-11-18 22:17:56 +00:00
|
|
|
DbRef *tmp;
|
2022-11-18 20:42:08 +00:00
|
|
|
|
|
|
|
while (ref && db->cacheSize > db->maxCache)
|
|
|
|
{
|
2022-11-21 20:31:37 +00:00
|
|
|
char *hash = DbHashKey(ref->name);
|
2022-11-18 20:42:08 +00:00
|
|
|
|
2022-11-18 22:17:56 +00:00
|
|
|
if (pthread_mutex_trylock(&ref->lock) != 0)
|
|
|
|
{
|
|
|
|
/* This ref is locked by another thread, don't evict it. */
|
|
|
|
ref = ref->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-11-18 20:42:08 +00:00
|
|
|
JsonFree(ref->json);
|
2022-11-18 22:17:56 +00:00
|
|
|
pthread_mutex_unlock(&ref->lock);
|
2022-11-18 20:42:08 +00:00
|
|
|
pthread_mutex_destroy(&ref->lock);
|
|
|
|
|
2022-11-21 20:31:37 +00:00
|
|
|
hash = DbHashKey(ref->name);
|
2022-11-18 20:42:08 +00:00
|
|
|
HashMapDelete(db->cache, hash);
|
|
|
|
Free(hash);
|
2022-11-18 22:17:56 +00:00
|
|
|
|
2022-12-15 20:29:19 +00:00
|
|
|
StringArrayFree(ref->name);
|
2022-11-18 20:42:08 +00:00
|
|
|
|
|
|
|
db->cacheSize -= ref->size;
|
|
|
|
|
2022-11-18 22:17:56 +00:00
|
|
|
ref->next->prev = ref->prev;
|
|
|
|
if (!ref->prev)
|
|
|
|
{
|
|
|
|
db->leastRecent = ref->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ref->prev->next = ref->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = ref->next;
|
2022-11-18 20:42:08 +00:00
|
|
|
Free(ref);
|
|
|
|
|
2022-11-18 22:17:56 +00:00
|
|
|
ref = tmp;
|
2022-11-18 20:42:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-24 16:44:29 +00:00
|
|
|
Db *
|
2022-11-15 18:20:05 +00:00
|
|
|
DbOpen(char *dir, size_t cache)
|
2022-10-24 16:44:29 +00:00
|
|
|
{
|
|
|
|
Db *db;
|
|
|
|
|
2022-11-21 16:22:50 +00:00
|
|
|
if (!dir || cache < DB_MIN_CACHE)
|
2022-10-24 16:44:29 +00:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
db = Malloc(sizeof(Db));
|
|
|
|
if (!db)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
db->dir = dir;
|
2022-10-31 12:12:42 +00:00
|
|
|
db->maxCache = cache;
|
2022-10-24 16:44:29 +00:00
|
|
|
|
|
|
|
pthread_mutex_init(&db->lock, NULL);
|
|
|
|
|
2022-11-18 22:17:56 +00:00
|
|
|
db->cache = HashMapCreate();
|
|
|
|
if (!db->cache)
|
2022-11-15 18:20:05 +00:00
|
|
|
{
|
2022-11-18 22:17:56 +00:00
|
|
|
return NULL;
|
2022-11-15 18:20:05 +00:00
|
|
|
}
|
2022-10-24 16:44:29 +00:00
|
|
|
|
2022-11-18 20:42:08 +00:00
|
|
|
db->mostRecent = NULL;
|
|
|
|
db->leastRecent = NULL;
|
|
|
|
|
2022-10-24 16:44:29 +00:00
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DbClose(Db * db)
|
|
|
|
{
|
2022-11-15 18:20:05 +00:00
|
|
|
char *key;
|
|
|
|
DbRef *val;
|
|
|
|
|
2022-10-24 16:44:29 +00:00
|
|
|
if (!db)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-31 12:12:42 +00:00
|
|
|
pthread_mutex_destroy(&db->lock);
|
2022-11-15 18:20:05 +00:00
|
|
|
|
2022-11-21 16:13:11 +00:00
|
|
|
while (HashMapIterate(db->cache, &key, (void **) &val))
|
2022-11-15 18:20:05 +00:00
|
|
|
{
|
2022-11-21 16:13:11 +00:00
|
|
|
JsonFree(val->json);
|
2022-12-15 20:29:19 +00:00
|
|
|
StringArrayFree(val->name);
|
2022-11-21 16:13:11 +00:00
|
|
|
pthread_mutex_destroy(&val->lock);
|
|
|
|
Free(val);
|
2022-11-15 18:20:05 +00:00
|
|
|
}
|
2022-11-21 16:13:11 +00:00
|
|
|
HashMapFree(db->cache);
|
2022-10-31 12:12:42 +00:00
|
|
|
|
2022-10-24 16:44:29 +00:00
|
|
|
Free(db);
|
|
|
|
}
|
|
|
|
|
2022-11-21 20:31:37 +00:00
|
|
|
static DbRef *
|
|
|
|
DbLockFromArr(Db * db, Array * args)
|
2022-10-24 16:44:29 +00:00
|
|
|
{
|
2022-11-15 18:20:05 +00:00
|
|
|
char *file;
|
|
|
|
char *hash;
|
|
|
|
DbRef *ref;
|
2022-11-21 16:13:11 +00:00
|
|
|
FILE *fp;
|
|
|
|
struct flock lock;
|
2022-11-15 18:20:05 +00:00
|
|
|
|
2022-11-21 20:31:37 +00:00
|
|
|
if (!db || !args)
|
2022-10-31 12:12:42 +00:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-11-15 18:20:05 +00:00
|
|
|
ref = NULL;
|
|
|
|
hash = NULL;
|
|
|
|
|
2022-10-31 12:12:42 +00:00
|
|
|
pthread_mutex_lock(&db->lock);
|
|
|
|
|
2022-11-15 18:20:05 +00:00
|
|
|
/* Check if the item is in the cache */
|
2022-11-21 20:31:37 +00:00
|
|
|
hash = DbHashKey(args);
|
2022-11-18 22:17:56 +00:00
|
|
|
ref = HashMapGet(db->cache, hash);
|
2022-11-21 20:31:37 +00:00
|
|
|
file = DbFileName(db, args);
|
|
|
|
|
2022-11-21 16:13:11 +00:00
|
|
|
/* Open the file for reading and writing so we can lock it */
|
|
|
|
fp = fopen(file, "r+");
|
|
|
|
if (!fp)
|
|
|
|
{
|
2023-02-27 15:39:12 +00:00
|
|
|
if (ref)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&ref->lock);
|
|
|
|
|
|
|
|
HashMapDelete(db->cache, hash);
|
|
|
|
JsonFree(ref->json);
|
|
|
|
StringArrayFree(ref->name);
|
|
|
|
|
|
|
|
db->cacheSize -= ref->size;
|
|
|
|
|
|
|
|
if (ref->next)
|
|
|
|
{
|
|
|
|
ref->next->prev = ref->prev;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
db->mostRecent = ref->prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ref->prev)
|
|
|
|
{
|
|
|
|
ref->prev->next = ref->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
db->leastRecent = ref->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&ref->lock);
|
|
|
|
pthread_mutex_destroy(&ref->lock);
|
|
|
|
Free(ref);
|
|
|
|
}
|
2022-11-21 16:13:11 +00:00
|
|
|
ref = NULL;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
lock.l_start = 0;
|
|
|
|
lock.l_len = 0;
|
|
|
|
lock.l_type = F_WRLCK;
|
|
|
|
lock.l_whence = SEEK_SET;
|
|
|
|
|
|
|
|
/* Lock the file on the disk */
|
|
|
|
if (fcntl(fileno(fp), F_SETLK, &lock) < 0)
|
|
|
|
{
|
2023-02-27 15:39:12 +00:00
|
|
|
printf("fcntl() failed on %s (%s)\n", file, strerror(errno));
|
2022-11-21 16:13:11 +00:00
|
|
|
fclose(fp);
|
|
|
|
ref = NULL;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2022-11-15 18:20:05 +00:00
|
|
|
if (ref) /* In cache */
|
|
|
|
{
|
|
|
|
unsigned long diskTs = UtilLastModified(file);
|
|
|
|
|
2022-11-18 22:17:56 +00:00
|
|
|
pthread_mutex_lock(&ref->lock);
|
2022-11-21 16:13:11 +00:00
|
|
|
ref->fp = fp;
|
2022-11-18 22:17:56 +00:00
|
|
|
|
2022-11-15 18:20:05 +00:00
|
|
|
if (diskTs > ref->ts)
|
|
|
|
{
|
|
|
|
/* File was modified on disk since it was cached */
|
2022-11-21 16:13:11 +00:00
|
|
|
HashMap *json = JsonDecode(fp);
|
2022-11-15 18:20:05 +00:00
|
|
|
|
|
|
|
if (!json)
|
|
|
|
{
|
2022-11-18 22:17:56 +00:00
|
|
|
pthread_mutex_unlock(&ref->lock);
|
2022-11-21 16:13:11 +00:00
|
|
|
fclose(fp);
|
2022-11-15 18:20:05 +00:00
|
|
|
ref = NULL;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonFree(ref->json);
|
|
|
|
ref->json = json;
|
|
|
|
ref->ts = diskTs;
|
|
|
|
ref->size = DbComputeSize(ref->json);
|
2022-11-18 20:42:08 +00:00
|
|
|
|
2022-11-21 16:13:11 +00:00
|
|
|
}
|
2022-11-18 20:42:08 +00:00
|
|
|
|
2022-11-21 16:13:11 +00:00
|
|
|
/* Float this ref to mostRecent */
|
|
|
|
if (ref->next)
|
|
|
|
{
|
|
|
|
ref->next->prev = ref->prev;
|
2022-11-18 20:42:08 +00:00
|
|
|
|
2022-11-21 16:13:11 +00:00
|
|
|
if (!ref->prev)
|
|
|
|
{
|
|
|
|
db->leastRecent = ref->next;
|
2022-11-18 20:42:08 +00:00
|
|
|
}
|
2022-11-23 14:56:31 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ref->prev->next = ref->next;
|
|
|
|
}
|
2022-11-18 22:17:56 +00:00
|
|
|
|
2022-11-21 16:13:11 +00:00
|
|
|
ref->prev = db->mostRecent;
|
|
|
|
ref->next = NULL;
|
|
|
|
db->mostRecent = ref;
|
2022-11-15 18:20:05 +00:00
|
|
|
}
|
2022-11-21 16:13:11 +00:00
|
|
|
|
|
|
|
/* The file on disk may be larger than what we have in memory,
|
|
|
|
* which may require items in cache to be evicted. */
|
|
|
|
DbCacheEvict(db);
|
2022-11-15 18:20:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-23 14:56:31 +00:00
|
|
|
Array *name = ArrayCreate();
|
|
|
|
size_t i;
|
|
|
|
|
2022-11-15 18:20:05 +00:00
|
|
|
/* Not in cache; load from disk */
|
|
|
|
|
|
|
|
ref = Malloc(sizeof(DbRef));
|
|
|
|
if (!ref)
|
|
|
|
{
|
|
|
|
fclose(fp);
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
ref->json = JsonDecode(fp);
|
2022-11-21 16:13:11 +00:00
|
|
|
ref->fp = fp;
|
2022-11-15 18:20:05 +00:00
|
|
|
|
|
|
|
if (!ref->json)
|
|
|
|
{
|
|
|
|
Free(ref);
|
2022-11-21 16:13:11 +00:00
|
|
|
fclose(fp);
|
2022-11-15 18:20:05 +00:00
|
|
|
ref = NULL;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_init(&ref->lock, NULL);
|
2022-11-18 22:17:56 +00:00
|
|
|
pthread_mutex_lock(&ref->lock);
|
|
|
|
|
2022-11-15 18:20:05 +00:00
|
|
|
ref->ts = UtilServerTs();
|
|
|
|
ref->size = DbComputeSize(ref->json);
|
2022-11-23 14:56:31 +00:00
|
|
|
|
|
|
|
for (i = 0; i < ArraySize(args); i++)
|
|
|
|
{
|
2023-01-08 04:38:06 +00:00
|
|
|
ArrayAdd(name, StrDuplicate(ArrayGet(args, i)));
|
2022-11-23 14:56:31 +00:00
|
|
|
}
|
|
|
|
ref->name = name;
|
2022-11-15 18:20:05 +00:00
|
|
|
|
2023-02-23 23:19:23 +00:00
|
|
|
HashMapSet(db->cache, hash, ref);
|
2022-11-18 22:17:56 +00:00
|
|
|
db->cacheSize += ref->size;
|
2022-11-15 18:20:05 +00:00
|
|
|
|
2022-11-18 22:17:56 +00:00
|
|
|
ref->next = NULL;
|
|
|
|
ref->prev = db->mostRecent;
|
|
|
|
db->mostRecent = ref;
|
2022-11-18 20:42:08 +00:00
|
|
|
|
2022-11-18 22:17:56 +00:00
|
|
|
/* Adding this item to the cache may case it to grow too large,
|
|
|
|
* requiring some items to be evicted */
|
|
|
|
DbCacheEvict(db);
|
2022-11-15 18:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
finish:
|
2022-11-17 23:51:45 +00:00
|
|
|
pthread_mutex_unlock(&db->lock);
|
|
|
|
|
2022-11-15 18:20:05 +00:00
|
|
|
Free(file);
|
|
|
|
Free(hash);
|
2022-11-18 19:36:15 +00:00
|
|
|
|
2022-11-15 18:20:05 +00:00
|
|
|
return ref;
|
2022-10-24 16:44:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 20:31:37 +00:00
|
|
|
DbRef *
|
|
|
|
DbCreate(Db * db, size_t nArgs,...)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char *file;
|
|
|
|
char *dir;
|
|
|
|
va_list ap;
|
|
|
|
Array *args;
|
2022-12-15 20:29:19 +00:00
|
|
|
DbRef *ret;
|
2022-11-21 20:31:37 +00:00
|
|
|
|
|
|
|
if (!db)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(ap, nArgs);
|
|
|
|
args = ArrayFromVarArgs(nArgs, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (!args)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
file = DbFileName(db, args);
|
|
|
|
|
2022-11-21 20:59:22 +00:00
|
|
|
if (UtilLastModified(file))
|
2022-11-21 20:31:37 +00:00
|
|
|
{
|
|
|
|
Free(file);
|
|
|
|
ArrayFree(args);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-03-01 19:33:25 +00:00
|
|
|
dir = DbDirName(db, args, 1);
|
2022-11-21 20:31:37 +00:00
|
|
|
if (UtilMkdir(dir, 0750) < 0)
|
|
|
|
{
|
|
|
|
Free(file);
|
|
|
|
ArrayFree(args);
|
|
|
|
Free(dir);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-11-23 14:56:31 +00:00
|
|
|
Free(dir);
|
2022-11-21 20:59:22 +00:00
|
|
|
|
2022-11-21 20:31:37 +00:00
|
|
|
fp = fopen(file, "w");
|
|
|
|
Free(file);
|
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
ArrayFree(args);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(fp, "{}");
|
|
|
|
fflush(fp);
|
|
|
|
fclose(fp);
|
|
|
|
|
2022-12-15 20:29:19 +00:00
|
|
|
ret = DbLockFromArr(db, args);
|
|
|
|
|
|
|
|
ArrayFree(args);
|
|
|
|
|
|
|
|
return ret;
|
2022-11-21 20:31:37 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 02:39:58 +00:00
|
|
|
int
|
|
|
|
DbDelete(Db * db, size_t nArgs,...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
Array *args;
|
|
|
|
char *file;
|
|
|
|
char *hash;
|
|
|
|
int ret = 1;
|
|
|
|
DbRef *ref;
|
|
|
|
|
|
|
|
if (!db)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(ap, nArgs);
|
|
|
|
args = ArrayFromVarArgs(nArgs, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&db->lock);
|
|
|
|
|
|
|
|
hash = DbHashKey(args);
|
|
|
|
file = DbFileName(db, args);
|
|
|
|
|
|
|
|
ref = HashMapGet(db->cache, hash);
|
|
|
|
if (ref)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&ref->lock);
|
|
|
|
|
|
|
|
HashMapDelete(db->cache, hash);
|
|
|
|
JsonFree(ref->json);
|
2022-12-15 20:29:19 +00:00
|
|
|
StringArrayFree(ref->name);
|
2022-12-15 02:39:58 +00:00
|
|
|
|
|
|
|
db->cacheSize -= ref->size;
|
|
|
|
|
|
|
|
if (ref->next)
|
|
|
|
{
|
|
|
|
ref->next->prev = ref->prev;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
db->mostRecent = ref->prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ref->prev)
|
|
|
|
{
|
|
|
|
ref->prev->next = ref->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
db->leastRecent = ref->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&ref->lock);
|
|
|
|
pthread_mutex_destroy(&ref->lock);
|
|
|
|
Free(ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
Free(hash);
|
|
|
|
|
|
|
|
if (UtilLastModified(file))
|
|
|
|
{
|
|
|
|
ret = remove(file) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&db->lock);
|
|
|
|
|
2022-12-28 15:44:21 +00:00
|
|
|
ArrayFree(args);
|
|
|
|
Free(file);
|
2022-12-15 02:39:58 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-11-21 20:31:37 +00:00
|
|
|
DbRef *
|
|
|
|
DbLock(Db * db, size_t nArgs,...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
Array *args;
|
|
|
|
DbRef *ret;
|
|
|
|
|
|
|
|
va_start(ap, nArgs);
|
|
|
|
args = ArrayFromVarArgs(nArgs, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (!args)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = DbLockFromArr(db, args);
|
|
|
|
|
|
|
|
ArrayFree(args);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-11-17 22:57:29 +00:00
|
|
|
int
|
2022-10-24 16:44:29 +00:00
|
|
|
DbUnlock(Db * db, DbRef * ref)
|
|
|
|
{
|
2022-10-31 12:12:42 +00:00
|
|
|
if (!db || !ref)
|
|
|
|
{
|
2022-11-17 22:57:29 +00:00
|
|
|
return 0;
|
2022-10-31 12:12:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_lock(&db->lock);
|
|
|
|
|
2022-11-21 16:13:11 +00:00
|
|
|
rewind(ref->fp);
|
2022-11-23 00:44:29 +00:00
|
|
|
if (ftruncate(fileno(ref->fp), 0) < 0)
|
2022-11-23 14:56:31 +00:00
|
|
|
{
|
|
|
|
pthread_mutex_unlock(&db->lock);
|
|
|
|
return 0;
|
|
|
|
}
|
2022-10-31 12:12:42 +00:00
|
|
|
|
2022-11-21 16:13:11 +00:00
|
|
|
JsonEncode(ref->json, ref->fp);
|
2022-11-15 18:20:05 +00:00
|
|
|
|
2022-11-21 16:13:11 +00:00
|
|
|
fflush(ref->fp);
|
|
|
|
fclose(ref->fp);
|
2022-11-15 18:20:05 +00:00
|
|
|
|
2022-11-18 22:17:56 +00:00
|
|
|
db->cacheSize -= ref->size;
|
|
|
|
ref->size = DbComputeSize(ref->json);
|
|
|
|
db->cacheSize += ref->size;
|
2022-11-18 20:42:08 +00:00
|
|
|
|
2022-11-18 22:17:56 +00:00
|
|
|
pthread_mutex_unlock(&ref->lock);
|
|
|
|
|
|
|
|
/* If this ref has grown significantly since we last computed its
|
|
|
|
* size, it may have filled the cache and require some items to be
|
|
|
|
* evicted. */
|
|
|
|
DbCacheEvict(db);
|
2022-11-17 22:57:29 +00:00
|
|
|
|
2022-11-18 19:36:15 +00:00
|
|
|
pthread_mutex_unlock(&db->lock);
|
2022-11-17 22:57:29 +00:00
|
|
|
return 1;
|
2022-10-24 16:44:29 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 15:51:56 +00:00
|
|
|
int
|
|
|
|
DbExists(Db * db, size_t nArgs,...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
Array *args;
|
|
|
|
char *file;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
va_start(ap, nArgs);
|
|
|
|
args = ArrayFromVarArgs(nArgs, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
2023-01-09 19:25:49 +00:00
|
|
|
if (!args)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Though it's not explicitly required, we lock the
|
|
|
|
* database before checking that an object exists to
|
|
|
|
* prevent any potential race conditions.
|
|
|
|
*/
|
|
|
|
pthread_mutex_lock(&db->lock);
|
2023-01-07 15:51:56 +00:00
|
|
|
|
2023-01-09 19:25:49 +00:00
|
|
|
file = DbFileName(db, args);
|
2023-01-07 15:51:56 +00:00
|
|
|
ret = UtilLastModified(file);
|
|
|
|
|
2023-01-09 19:25:49 +00:00
|
|
|
pthread_mutex_unlock(&db->lock);
|
|
|
|
|
2023-01-07 15:51:56 +00:00
|
|
|
Free(file);
|
|
|
|
ArrayFree(args);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-02-28 18:44:02 +00:00
|
|
|
Array *
|
2023-03-01 19:03:42 +00:00
|
|
|
DbList(Db * db, size_t nArgs,...)
|
2023-02-28 18:44:02 +00:00
|
|
|
{
|
|
|
|
Array *result;
|
2023-03-01 19:33:25 +00:00
|
|
|
Array *path;
|
2023-03-01 19:52:44 +00:00
|
|
|
DIR *files;
|
|
|
|
struct dirent *file;
|
|
|
|
char *dir;
|
2023-03-01 19:33:25 +00:00
|
|
|
va_list ap;
|
2023-02-28 18:44:02 +00:00
|
|
|
|
|
|
|
if (!db || !nArgs)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = ArrayCreate();
|
|
|
|
if (!result)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-03-01 19:33:25 +00:00
|
|
|
va_start(ap, nArgs);
|
|
|
|
path = ArrayFromVarArgs(nArgs, ap);
|
|
|
|
dir = DbDirName(db, path, 0);
|
|
|
|
|
|
|
|
files = opendir(dir);
|
|
|
|
if (!files)
|
|
|
|
{
|
|
|
|
ArrayFree(path);
|
|
|
|
Free(dir);
|
|
|
|
return NULL;
|
|
|
|
}
|
2023-03-01 19:52:44 +00:00
|
|
|
while ((file = readdir(files)))
|
|
|
|
{
|
2023-03-01 21:39:22 +00:00
|
|
|
size_t namlen = strlen(file->d_name);
|
2023-03-02 22:06:33 +00:00
|
|
|
|
2023-03-02 02:53:43 +00:00
|
|
|
if (namlen > 5)
|
2023-03-01 19:33:25 +00:00
|
|
|
{
|
2023-03-01 21:39:22 +00:00
|
|
|
int nameOffset = namlen - 5;
|
2023-03-01 19:52:44 +00:00
|
|
|
|
2023-03-01 19:33:25 +00:00
|
|
|
if (strcmp(file->d_name + nameOffset, ".json") == 0)
|
|
|
|
{
|
|
|
|
file->d_name[nameOffset] = '\0';
|
|
|
|
ArrayAdd(result, StrDuplicate(file->d_name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(files);
|
|
|
|
|
|
|
|
ArrayFree(path);
|
|
|
|
Free(dir);
|
|
|
|
|
2023-02-28 18:44:02 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-03-01 19:33:25 +00:00
|
|
|
void
|
2023-03-01 19:52:44 +00:00
|
|
|
DbListFree(Array * arr)
|
2023-03-01 19:33:25 +00:00
|
|
|
{
|
|
|
|
StringArrayFree(arr);
|
|
|
|
}
|
|
|
|
|
2022-10-24 16:44:29 +00:00
|
|
|
HashMap *
|
|
|
|
DbJson(DbRef * ref)
|
|
|
|
{
|
2022-10-31 12:12:42 +00:00
|
|
|
return ref ? ref->json : NULL;
|
2022-10-24 16:44:29 +00:00
|
|
|
}
|