Skip to content
suit Artifact [3fac07a96e]
Login

Artifact [3fac07a96e]

Artifact 3fac07a96e47763614bb533b0712ab050bd3d356ca89f492d41fd5488c57dfda:


#ifndef SUIT_PROJECT_H
#define SUIT_PROJECT_H

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

// Projects: the code repositories on this disk, and the three small files that
// say which they are (docs/projects.md). A project is matched to config by its
// remote key: host and path of its remote address, nothing else.
//
//   $SUIT_HOME/roots         ROOT                where suit looks for projects
//   $SUIT_HOME/projects      KEY PATH KIND       what the last scan found
//   $SUIT_HOME/projects.set  KEY PATH            keys set by hand
//
// A KEY is "-" in the projects file when the project has none. A PATH is
// absolute and has no control character; it may have spaces. KIND is git,
// fossil or zit. Every file ends each line with a newline and has no NUL byte.
// Anything else is an error and the whole file is rejected.

constexpr size_t PROJECT_KEY_MAX = 512;
constexpr size_t PROJECT_PATH_MAX = 4096;  // including the terminating NUL
constexpr size_t PROJECT_ROOT_MAX = 3000;  // a root's path, so the paths below it fit
constexpr size_t PROJECT_LINE_MAX = 8192;
constexpr size_t PROJECT_FILE_MAX = 1048576;
constexpr uint32_t PROJECT_ENTRY_MAX = 4096;
constexpr size_t ROOTS_MAX = 64;
constexpr size_t ROOTS_FILE_MAX = 262144;
constexpr size_t PROJECT_URL_MAX = 2048;  // including the terminating NUL

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

// ---- remote keys ----------------------------------------------------------

// The key for the remote address URL: lower-case host and path, no scheme, user,
// password, port, query, trailing "/" or ".git". Understands scheme://... and
// the scp form user@host:path. False for an address with no host (a local
// path, file://) or no path, or one too long. KEY has room for CAP bytes; use
// PROJECT_KEY_MAX + 1. KEY is empty when it returns false.
[[nodiscard]] bool project_key_from_url(const char *url, char *key, size_t cap);

// True if KEY is in the form project_key_from_url gives: HOST/PATH, the host
// of a-z 0-9 . - only, and no space or control character.
[[nodiscard]] bool project_key_ok(const char *key);

// True if PATH is one absolute path of fewer than PROJECT_PATH_MAX bytes with
// no control character.
[[nodiscard]] bool project_path_ok(const char *path);

// The remote address in git config TEXT (LEN bytes, NUL-terminated, changed in
// place): url under [remote "origin"], or else the first remote's. Anything it
// does not understand is skipped. False if there is none or it does not fit.
[[nodiscard]] bool project_git_remote(char *text, size_t len, char *url, size_t cap);

// ---- reading a file -------------------------------------------------------

typedef enum { READ_OK, READ_MISSING, READ_FAILED, READ_BIG } ReadResult;

// Read the whole of PATH into BUF (room for CAP + 1 bytes), at most CAP bytes,
// and end it with a NUL. READ_MISSING if there is no such file, READ_BIG if it
// holds more than CAP bytes, READ_FAILED with ERR set to the errno otherwise.
ReadResult project_read_file(const char *path, char *buf, size_t cap, size_t *len, int *err);

// ---- roots ----------------------------------------------------------------

typedef struct {
    uint32_t n;
    char path[ROOTS_MAX][PROJECT_PATH_MAX];
} Roots;

[[nodiscard]] bool roots_parse(Roots *r, const char *data, size_t len, ProjectError *err);
// A file that is not there is no roots.
[[nodiscard]] bool roots_load(Roots *r, const char *path, ProjectError *err);
// Atomically. Returns 0 or an errno value.
int roots_write(const Roots *r, const char *path);

typedef enum { ADD_OK, ADD_DUPLICATE, ADD_FULL } AddResult;
AddResult roots_add(Roots *r, const char *path);
// True if PATH was a root.
bool roots_remove(Roots *r, const char *path);

// ---- projects -------------------------------------------------------------

typedef enum { KIND_NONE, KIND_GIT, KIND_FOSSIL, KIND_ZIT } ProjectKind;

typedef struct {
    const char *key;  // "" for none
    const char *path;
    ProjectKind kind;
} ProjectEntry;

typedef struct {
    uint32_t n;
    ProjectEntry e[PROJECT_ENTRY_MAX];
    size_t used;
    char buf[2 * PROJECT_FILE_MAX];  // the strings
} Projects;

// WITH_KIND is the projects file (KEY PATH KIND, "-" for no key) or, false, the
// set file (KEY PATH).
[[nodiscard]] bool projects_parse(Projects *p, const char *data, size_t len, bool with_kind, ProjectError *err);
// A file that is not there is empty.
[[nodiscard]] bool projects_load(Projects *p, const char *path, bool with_kind, ProjectError *err);
// Sorted by path, atomically. Returns 0 or an errno value.
int projects_write(const Projects *p, const char *path, bool with_kind);

// Copies the strings. ADD_DUPLICATE if PATH is already there.
AddResult projects_add(Projects *p, const char *key, const char *path, ProjectKind kind);
const ProjectEntry *projects_find(const Projects *p, const char *path);
// True if PATH was there.
bool projects_remove(Projects *p, const char *path);

const char *project_kind_name(ProjectKind kind);

#endif