Skip to content
suit Artifact [984ab8126c]
Login

Artifact [984ab8126c]

Artifact 984ab8126ccb5c6f5b344e46c7e81a0cc696d752a726aa591b6f9cd6468b1eaa:


#ifndef SUIT_UPSTREAM_H
#define SUIT_UPSTREAM_H

#include "conf.h"

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

// Upstreams (docs/upstreams.md): an upstream's files are taken from a folder
// onto the branch upstream-NAME of the wardrobe that defines it, and merged
// into trunk when they are accepted.

constexpr uint32_t UPSTREAM_FILES_MAX = 4096;
constexpr uint64_t UPSTREAM_BYTES_MAX = 64ull * 1024 * 1024;
constexpr size_t UPSTREAM_REL_MAX = 512;                          // a file's path inside its entry's folder
constexpr size_t UPSTREAM_NAME_MAX = CONF_INTO_MAX + 1 + UPSTREAM_REL_MAX;  // INTO/REL
constexpr size_t UPSTREAM_PIN_MAX = 256;                          // the longest pin
constexpr uint32_t UPSTREAM_SETTLE_MAX = 64;                      // --theirs and --mine in all

// The files a take reads, in byte order of name: NAME is the path in the
// wardrobe (INTO/ENTRY/...) and the file is ROOT/REL, REL being NAME without
// the INTO/ in front.
typedef struct {
    const char *name;
    uint64_t size;
    bool exec;
} UpstreamFile;

typedef struct {
    uint32_t n;
    uint64_t bytes;
    char prefix[CONF_INTO_MAX + 2];  // INTO and a slash
    size_t into_len;                 // strlen of prefix
    char root[4096];                 // the folder read
    UpstreamFile f[UPSTREAM_FILES_MAX];
    char pool[UPSTREAM_FILES_MAX][UPSTREAM_NAME_MAX + 1];
} UpstreamFiles;

// What a take is asked to do, after the config has been read.
typedef struct {
    const char *name;     // the upstream
    const char *pin;      // the config's pin: a word
    const char *into;     // where the files land in the wardrobe
    bool auto_accept;     // trust = auto
} UpstreamSpec;

// The paths `accept` is told to settle: the upstream's version for THEIRS and
// trunk's for MINE.
typedef struct {
    uint32_t ntheirs;
    uint32_t nmine;
    const char *theirs[UPSTREAM_SETTLE_MAX];
    const char *mine[UPSTREAM_SETTLE_MAX];
} UpstreamSettle;

// Where an upstream stands.
typedef enum {
    UPSTREAM_NEVER_TAKEN,
    UPSTREAM_CURRENT,
    UPSTREAM_WAITING,
    UPSTREAM_CONFLICT,
} UpstreamState;

// The folder of the wardrobe that holds upstreams/NAME, the record of what was accepted.
constexpr char UPSTREAM_RECORD_DIR[] = "upstreams";

// Read FOLDER, or FOLDER/PATH when PATH is given, for the upstream NAME into F: skip every entry whose
// name starts with a dot, refuse a symlink, take regular files, keep the
// executable bit. Returns 0; 1 after a message on stderr (a file or folder
// that cannot be read, a symlink, a path that is too long, more than
// UPSTREAM_FILES_MAX files or UPSTREAM_BYTES_MAX bytes).
int upstream_scan(UpstreamFiles *f, const char *name, const char *folder, const char *path, const char *into);

// `suit upstream NAME take FOLDER`, `accept [--theirs PATH] [--mine PATH]...`,
// `diff` and `undo`. ARGV is the whole command line; ARGC is at least 4.
int cmd_upstream(int argc, char **argv);
// `suit upstreams`.
int cmd_upstreams(void);

#endif