Skip to content
suit Artifact [ad8fa50da4]
Login

Artifact [ad8fa50da4]

Artifact ad8fa50da404166795a559ccf732a5f7cfc7e8cd5a0c72784f804c7d238cf509:


#include "ledger.h"

#include "platform.h"

#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static char out_buf[LEDGER_FILE_MAX + 1];

__attribute__((format(printf, 3, 4)))
static bool fail(LedgerError *e, int line, const char *fmt, ...)
{
    va_list ap;
    e->line = line;
    va_start(ap, fmt);
    vsnprintf(e->msg, sizeof e->msg, fmt, ap);
    va_end(ap);
    return false;
}

// True if S is one absolute path: it starts with '/' and has no space or tab.
static bool path_ok(const char *s)
{
    return s[0] == '/' && strpbrk(s, " \t") == nullptr;
}

static bool parse_line(Ledger *l, LedgerError *e, int line, char *s)
{
    char *kind = s;
    char *dest = strchr(s, ' ');
    if (dest == nullptr) return fail(e, line, "expected 'link DEST SOURCE' or 'dir DEST'");
    *dest++ = '\0';
    char *src = strchr(dest, ' ');
    if (src != nullptr) *src++ = '\0';

    bool is_link = strcmp(kind, "link") == 0;
    if (!is_link && strcmp(kind, "dir") != 0)
        return fail(e, line, "unknown record '%.40s' (link or dir)", kind);
    if ((src != nullptr) != is_link)
        return fail(e, line, is_link ? "link needs DEST and SOURCE" : "dir takes only DEST");
    if (!path_ok(dest) || (is_link && !path_ok(src)))
        return fail(e, line, "paths must be absolute and have no spaces");
    for (uint32_t i = 0; i < l->n; i++)
        if (l->e[i].kind == (is_link ? ENTRY_LINK : ENTRY_DIR) && strcmp(l->e[i].dest, dest) == 0)
            return fail(e, line, "duplicate %s %.80s", kind, dest);
    if (l->n == LEDGER_ENTRY_MAX)
        return fail(e, line, "too many entries (limit %u)", LEDGER_ENTRY_MAX);
    l->e[l->n++] = (LedgerEntry){is_link ? ENTRY_LINK : ENTRY_DIR, dest, src};
    return true;
}

static bool parse_in_place(Ledger *l, size_t len, LedgerError *e)
{
    l->n = 0;
    l->buf[len] = '\0';
    char *p = l->buf;
    char *end = l->buf + len;
    int line = 0;
    while (p < end) {
        char *nl = memchr(p, '\n', (size_t)(end - p));
        line++;
        if (nl == nullptr) return fail(e, line, "line has no newline");
        size_t n = (size_t)(nl - p);
        if (n > LEDGER_LINE_MAX)
            return fail(e, line, "line is %zu bytes, limit is %zu", n, LEDGER_LINE_MAX);
        if (memchr(p, '\0', n) != nullptr) return fail(e, line, "NUL byte in line");
        *nl = '\0';
        char *s = p;
        p = nl + 1;
        if (!parse_line(l, e, line, s)) return false;
    }
    return true;
}

bool ledger_parse(Ledger *l, const char *data, size_t len, LedgerError *e)
{
    l->n = 0;
    if (len > LEDGER_FILE_MAX)
        return fail(e, 0, "file is %zu bytes, limit is %zu", len, LEDGER_FILE_MAX);
    memcpy(l->buf, data, len);
    bool ok = parse_in_place(l, len, e);
    l->n = ok ? l->n : 0;
    return ok;
}

bool ledger_load(Ledger *l, const char *path, LedgerError *e)
{
    l->n = 0;
    int fd = sys_open(path, O_RDONLY, 0);
    if (fd < 0 && errno == ENOENT) return true;
    if (fd < 0) return fail(e, 0, "cannot open: %s", strerror(errno));
    size_t len = 0;
    ssize_t n = 1;
    while (len <= LEDGER_FILE_MAX && n > 0) {
        n = sys_read(fd, l->buf + len, LEDGER_FILE_MAX + 1 - len);
        if (n < 0 && errno == EINTR) {
            n = 1;
            continue;
        }
        len += n > 0 ? (size_t)n : 0;
    }
    int err = errno;
    (void)sys_close(fd);
    if (n < 0) return fail(e, 0, "cannot read: %s", strerror(err));
    if (len > LEDGER_FILE_MAX)
        return fail(e, 0, "file is over the limit of %zu bytes", LEDGER_FILE_MAX);
    bool ok = parse_in_place(l, len, e);
    l->n = ok ? l->n : 0;
    return ok;
}

static int cmp_entry(const void *pa, const void *pb)
{
    const LedgerEntry *a = pa;
    const LedgerEntry *b = pb;
    if (a->kind != b->kind) return a->kind < b->kind ? -1 : 1;
    return strcmp(a->dest, b->dest);
}

int ledger_write(const Ledger *l, const char *path)
{
    static LedgerEntry sorted[LEDGER_ENTRY_MAX];
    ensure(l->n <= LEDGER_ENTRY_MAX);
    memcpy(sorted, l->e, l->n * sizeof sorted[0]);
    qsort(sorted, l->n, sizeof sorted[0], cmp_entry);
    size_t len = 0;
    for (uint32_t i = 0; i < l->n; i++) {
        const LedgerEntry *en = &sorted[i];
        size_t room = sizeof out_buf - len;
        int n = en->kind == ENTRY_LINK ? snprintf(out_buf + len, room, "link %s %s\n", en->dest, en->src)
                                       : snprintf(out_buf + len, room, "dir %s\n", en->dest);
        ensure(n >= 0);
        if ((size_t)n >= room) return EFBIG;
        len += (size_t)n;
    }
    return sys_write_atomic(path, out_buf, len);
}

const LedgerEntry *ledger_find_link(const Ledger *l, const char *dest)
{
    for (uint32_t i = 0; i < l->n; i++)
        if (l->e[i].kind == ENTRY_LINK && strcmp(l->e[i].dest, dest) == 0) return &l->e[i];
    return nullptr;
}