Skip to content
suit Documentation
Login

Documentation

#ifndef SUIT_LEDGER_H
#define SUIT_LEDGER_H

#include <stddef.h>
#include <stdint.h>

// The ledger, $SUIT_HOME/ledger: the only record of what suit owns. One line
// per thing suit made:
//
//   link DEST SOURCE    a symlink at DEST pointing at SOURCE
//   dir DEST            a folder suit made to hold a link
//
// Paths are absolute, with no space or newline in them. Nothing else is
// allowed in the file: a line that is not one of these is an error, and the
// whole file is rejected.

constexpr size_t LEDGER_FILE_MAX = 1048576;
constexpr size_t LEDGER_LINE_MAX = 8192;
constexpr uint32_t LEDGER_ENTRY_MAX = 4096;

typedef enum { ENTRY_LINK, ENTRY_DIR } EntryKind;

typedef struct {
    EntryKind kind;
    const char *dest;
    const char *src;  // nullptr for a dir
} LedgerEntry;

typedef struct {
    uint32_t n;
    LedgerEntry e[LEDGER_ENTRY_MAX];
    char buf[LEDGER_FILE_MAX + 1];
} Ledger;

// line is 0 when the error concerns the whole file.
typedef struct {
    int line;
    char msg[256];
} LedgerError;

// Parse DATA (LEN bytes). On failure L is empty and ERR says what and where.
[[nodiscard]] bool ledger_parse(Ledger *l, const char *data, size_t len, LedgerError *err);

// Read PATH. A file that is not there is an empty ledger.
[[nodiscard]] bool ledger_load(Ledger *l, const char *path, LedgerError *err);

// Write the entries, links then dirs, each sorted by path, atomically. Returns 0
// or an errno value. L's entries need not point into L's own buffer.
int ledger_write(const Ledger *l, const char *path);

// The link entry for DEST, or nullptr.
const LedgerEntry *ledger_find_link(const Ledger *l, const char *dest);

#endif