Implement DbList()

This commit is contained in:
Jordan Bancino 2023-03-01 19:33:25 +00:00
parent ce6d483135
commit 8c4e6aa594
3 changed files with 48 additions and 4 deletions

View file

@ -16,7 +16,7 @@ Milestone: v0.2.0
[~] Db API
[x] If object is in cache, but doesn't exist on disk, delete from cache
[ ] Allow cache to be totally disabled (no MIN_CACHE_SIZE)
[ ] List keys under a path (DbList() using POSIX opendir())
[x] List keys under a path (DbList() using POSIX opendir())
[~] User login
[x] User manipulation functions (so we don't use the DB directly)
[x] Refresh tokens

View file

@ -28,6 +28,9 @@
#include <Util.h>
#include <Str.h>
#include <sys/types.h>
#include <dirent.h>
#include <pthread.h>
#include <fcntl.h>
#include <unistd.h>
@ -214,12 +217,12 @@ DbHashKey(Array * args)
}
static char *
DbDirName(Db * db, Array * args)
DbDirName(Db * db, Array * args, size_t strip)
{
size_t i;
char *str = StrConcat(2, db->dir, "/");
for (i = 0; i < ArraySize(args) - 1; i++)
for (i = 0; i < ArraySize(args) - strip; i++)
{
char *tmp;
@ -597,7 +600,7 @@ DbCreate(Db * db, size_t nArgs,...)
return NULL;
}
dir = DbDirName(db, args);
dir = DbDirName(db, args, 1);
if (UtilMkdir(dir, 0750) < 0)
{
Free(file);
@ -798,6 +801,11 @@ Array *
DbList(Db * db, size_t nArgs,...)
{
Array *result;
Array *path;
DIR* files;
struct dirent* file;
char* dir;
va_list ap;
if (!db || !nArgs)
{
@ -810,9 +818,42 @@ DbList(Db * db, size_t nArgs,...)
return NULL;
}
va_start(ap, nArgs);
path = ArrayFromVarArgs(nArgs, ap);
dir = DbDirName(db, path, 0);
files = opendir(dir);
if (!files)
{
ArrayFree(path);
Free(dir);
return NULL;
}
while((file = readdir(files))) {
if (file->d_type == DT_REG && file->d_namlen > 5)
{
int nameOffset = file->d_namlen - 5;
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);
return result;
}
void
DbListFree(Array *arr)
{
StringArrayFree(arr);
}
HashMap *
DbJson(DbRef * ref)
{

View file

@ -60,6 +60,9 @@ extern int
extern Array *
DbList(Db *, size_t,...);
extern void
DbListFree(Array *);
extern HashMap *
DbJson(DbRef *);