summaryrefslogtreecommitdiff
path: root/src/server/db.c
blob: 162d127830666c76dfe94c1bd4faff1d904152f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#define AL_LOG_SECTION "db"
#include <al/log.h>
#include <nnwt/file.h>

#include "server.h"

#if 0
// This is still nothing.
#include <jansson.h>

static void open_user(struct camu_server *server, struct nn_dir *camu_db, struct nn_dir_entry *dir)
{
    str path;
    nn_dir_entry_get_path(dir, camu_db, &path);
    struct nn_file file;
    if (!nn_file_open(&file, &path, 0)) {
        goto out;
    }
    str buffer;
    nn_file_read_as_str(&file, &buffer);
    json_error_t error;
    json_t *root = json_loadb(buffer.data, buffer.length, 0, &error);
    al_str_free(&buffer);
    if (!root) {
        log_error("Failed to parse %.*s:%d:%d (%s).", al_str_x(&path), error.line, error.column, error.text);
        goto out;
    }
    struct camu_user *user = al_alloc_object(struct camu_user);
    al_str_from(&user->name, json_string_value(json_object_get(root, "username")));
    log_info("Loaded user \"%.*s\"", al_str_x(&user->name));
    al_array_push(server->users, user);
    json_decref(root);
out:
    al_str_free(&path);
}
#endif

static void open_user(struct camu_server *server, struct nn_dir *camu_db, struct nn_dir_entry *dir)
{
    (void)server;
    (void)camu_db;
    (void)dir;
}

bool camu_db_open(struct camu_server *server, str *path)
{
    struct nn_dir camu_db;
    if (!nn_dir_open(&camu_db, path)) {
        return false;
    }
    struct nn_dir_entry entry;
    while (nn_dir_read(&camu_db, &entry)) {
        if (al_str_eq(&al_str_cr(nn_dir_entry_get_os_name(&entry)), &al_str_c("users"))) {
            struct nn_dir users;
            str users_path;
            nn_dir_entry_get_path(&entry, &camu_db, &users_path);
            if (nn_dir_open(&users, &users_path)) {
                struct nn_dir_entry user;
                while (nn_dir_read(&users, &user)) {
                    if (user.type == NNWT_ENTRY_FILE) {
                        open_user(server, &camu_db, &user);
                    }
                }
                nn_dir_close(&users);
            }
            al_str_free(&users_path);
            break;
        }
    }
    nn_dir_close(&camu_db);
    return true;
}

void camu_db_close(struct camu_server *server)
{
    (void)server;
}