Skip to content
suit Documentation
Login

Documentation

#include "need.h"

#include "platform.h"

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

static char file_buf[DONE_FILE_MAX + 1];

static const char CONTAINS[] = "contains \"";

bool done_parse(DoneTest *t, const char *home, const char *s, char *why, size_t cap)
{
    size_t pl = strcspn(s, " ");
    if (pl == 0 || s[pl] == '\0') {
        snprintf(why, cap, "expected PATH exists or PATH contains \"TEXT\"");
        return false;
    }
    const char *rest = s + pl + 1;
    size_t rl = strlen(rest);
    if (strcmp(rest, "exists") == 0) {
        t->kind = DONE_EXISTS;
        t->text = nullptr;
        t->text_len = 0;
    } else if (strncmp(rest, CONTAINS, sizeof CONTAINS - 1) == 0) {
        if (rl < sizeof CONTAINS || rest[rl - 1] != '"') {
            snprintf(why, cap, "TEXT must be in quotes, with the closing quote last on the line");
            return false;
        }
        if (rl == sizeof CONTAINS) {
            snprintf(why, cap, "TEXT is empty");
            return false;
        }
        t->kind = DONE_CONTAINS;
        t->text = rest + sizeof CONTAINS - 1;
        t->text_len = rl - sizeof CONTAINS;
    } else {
        snprintf(why, cap, "expected PATH exists or PATH contains \"TEXT\"");
        return false;
    }
    bool tilde = s[0] == '~' && (pl == 1 || s[1] == '/');
    size_t hl = tilde ? strlen(home) : 0;
    const char *tail = tilde ? s + 1 : s;
    size_t tl = tilde ? pl - 1 : pl;
    if (hl + tl >= sizeof t->path) {
        snprintf(why, cap, "PATH is longer than %zu bytes", sizeof t->path - 1);
        return false;
    }
    memcpy(t->path, home, hl);
    memcpy(t->path + hl, tail, tl);
    t->path[hl + tl] = '\0';
    if (t->path[0] != '/') {
        snprintf(why, cap, "PATH must be absolute, or start with ~/ for the home folder");
        return false;
    }
    return true;
}

static bool has_text(const char *buf, size_t len, const char *text, size_t n)
{
    for (size_t at = 0; at + n <= len; at++) {
        const char *hit = memchr(buf + at, text[0], len - at - n + 1);
        if (hit == nullptr) return false;
        at = (size_t)(hit - buf);
        if (memcmp(hit, text, n) == 0) return true;
    }
    return false;
}

static NeedState unknown(char *why, size_t cap, const char *what, int err)
{
    if (err != 0) snprintf(why, cap, "%s: %s", what, strerror(err));
    else snprintf(why, cap, "%s", what);
    return NEED_UNKNOWN;
}

// A stat or lstat that failed: a file that is not there is not done.
static NeedState look_failed(char *why, size_t cap)
{
    bool missing = errno == ENOENT || errno == ENOTDIR;
    return missing ? NEED_NOT_DONE : unknown(why, cap, "cannot look at the file", errno);
}

static NeedState eval_contains(const DoneTest *t, char *why, size_t cap)
{
    struct stat st;
    if (sys_stat(t->path, &st) != 0) {
        return look_failed(why, cap);
    }
    if (!S_ISREG(st.st_mode)) return unknown(why, cap, "not a regular file", 0);
    int fd = sys_open(t->path, O_RDONLY, 0);
    if (fd < 0) return unknown(why, cap, "cannot open the file", errno);
    size_t len = 0;
    ssize_t n = 1;
    while (n > 0 && len <= DONE_FILE_MAX) {
        n = sys_read(fd, file_buf + len, DONE_FILE_MAX + 1 - len);
        if (n < 0 && errno == EINTR) n = 1;
        else if (n > 0) len += (size_t)n;
    }
    int err = errno;
    (void)sys_close(fd);
    if (n < 0) return unknown(why, cap, "cannot read the file", err);
    if (len > DONE_FILE_MAX) return unknown(why, cap, "file is over 4 MiB", 0);
    return has_text(file_buf, len, t->text, t->text_len) ? NEED_DONE : NEED_NOT_DONE;
}

NeedState done_eval(const DoneTest *t, char *why, size_t cap)
{
    if (t->kind == DONE_CONTAINS) return eval_contains(t, why, cap);
    struct stat st;
    if (sys_lstat(t->path, &st) == 0) return NEED_DONE;
    return look_failed(why, cap);
}

bool need_prompt_ok(const char *p)
{
    if (p[0] == '\0' || p[0] == '/') return false;
    for (const char *c = p; *c != '\0';) {
        size_t n = strcspn(c, "/");
        if (n == 2 && c[0] == '.' && c[1] == '.') return false;
        c += n + (c[n] == '/' ? 1 : 0);
    }
    return true;
}