Skip to content
pi-c Artifact [ff0072066a]
Login

Artifact [ff0072066a]

Artifact ff0072066a4745148f556ccdb3d79011f6b732891a7a3345091ed1da522825f6:


#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE 1
#endif
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>

#include "folder.h"
#include "ignore.h"
#include "input.h"
#include "platform.h"

static int fld_fs(void) { return pic_platform_get() == pic_platform_host(); }

int pic_folder_seal(const char *dir) {
    char path[PATH_MAX];
    int fd, ok;
    if (!fld_fs() || snprintf(path, sizeof path, "%s/.gitignore", dir) >= (int)sizeof path) {
        return 0;
    }
    if ((fd = open(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0644)) < 0) {
        return 0;
    }
    ok = write(fd, "*\n", 2) == 2;
    close(fd);
    return ok;
}

/* ---- the checkout ------------------------------------------------------------------------- */

typedef struct {
    char root[PATH_MAX];
    const char *tool; /* "zit" or "fossil": what to ask for the settings it keeps outside the tree */
} fld_checkout;

static int fld_file(const char *dir, const char *name) {
    char path[PATH_MAX];
    struct stat sb;
    return snprintf(path, sizeof path, "%s/%s", dir, name) < (int)sizeof path && stat(path, &sb) == 0;
}

/* The checkout DIR is in: its root, from the folder up. */
static int fld_find(const char *dir, fld_checkout *c) {
    char cur[PATH_MAX];
    if (!realpath(dir, cur)) {
        return 0;
    }
    for (;;) {
        char *slash;
        if (fld_file(cur, ".zit-checkout") || fld_file(cur, ".fslckout") || fld_file(cur, "_FOSSIL_")) {
            c->tool = fld_file(cur, ".zit-checkout") ? "zit" : "fossil";
            (void)snprintf(c->root, sizeof c->root, "%s", cur);
            return 1;
        }
        if (!(slash = strrchr(cur, '/')) || slash == cur) {
            return 0;
        }
        *slash = '\0';
    }
}

/* What `TOOL settings NAME` says in the checkout, run there: the repository and global tiers, which
 * are not files in the tree. NULL when the tool is missing or fails. */
static char *fld_tool(const fld_checkout *c, const char *name) {
    int fds[2];
    pid_t pid;
    char buf[4096], *s;
    size_t n = 0;
    int zit = strcmp(c->tool, "zit") == 0, status;
    if (pipe(fds) != 0) {
        return NULL;
    }
    if ((pid = fork()) < 0) {
        close(fds[0]);
        close(fds[1]);
        return NULL;
    }
    if (pid == 0) {
        int nul = open("/dev/null", O_RDWR);
        if (nul >= 0) {
            (void)dup2(nul, 0);
            (void)dup2(nul, 2);
        }
        (void)dup2(fds[1], 1);
        if (chdir(c->root) == 0) {
            if (zit) {
                execlp("zit", "zit", "settings", "--value", name, (char *)NULL);
            } else {
                execlp("fossil", "fossil", "settings", name, (char *)NULL);
            }
        }
        _exit(127);
    }
    close(fds[1]);
    for (ssize_t r; n < sizeof buf - 1 && (r = read(fds[0], buf + n, sizeof buf - 1 - n)) > 0;) {
        n += (size_t)r;
    }
    close(fds[0]);
    (void)waitpid(pid, &status, 0);
    if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
        return NULL;
    }
    buf[n] = '\0';
    s = buf;
    if (!zit) { /* "NAME  (local)  VALUE": the name and its tier word are not the value */
        s += strlen(name) * (strncmp(s, name, strlen(name)) == 0);
        while (*s == ' ' || *s == '\t') {
            s++;
        }
        if (*s == '(' && (s = strchr(s, ')'))) {
            s++;
        }
    }
    while (s && isspace((unsigned char)*s)) {
        s++;
    }
    while (s && n && (buf[n - 1] == '\n' || buf[n - 1] == ' ')) {
        buf[--n] = '\0';
    }
    return s ? strdup(s) : NULL;
}

/* The versioned file .fossil-settings/NAME, else .zit-settings/NAME, in the root (Fossil's wins). */
static char *fld_versioned(const fld_checkout *c, const char *name) {
    static const char *const dirs[2] = {".fossil-settings", ".zit-settings"};
    for (int i = 0; i < 2; i++) {
        char path[PATH_MAX];
        char *t;
        size_t len;
        if (snprintf(path, sizeof path, "%s/%s/%s", c->root, dirs[i], name) < (int)sizeof path &&
            (t = pic_input_read(path, PIC_IGNORE_MAX, 0, &len, NULL))) {
            return t;
        }
    }
    return NULL;
}

/* Whether the project's own ignore rules cover REL (a folder): .zitignore, .fslignore, and the
 * versioned ignore-glob; with ASK, the setting the tool reports when no file gives one. *HELD is set
 * when that setting is not empty. */
static int fld_covered(const fld_checkout *c, const char *rel, int ask, int *held) {
    pic_ignore ig = {0};
    char path[PATH_MAX], *t;
    size_t len;
    int hit;
    static const char *const files[2] = {".zitignore", ".fslignore"};
    for (int i = 0; i < 2; i++) {
        if (snprintf(path, sizeof path, "%s/%s", c->root, files[i]) < (int)sizeof path &&
            (t = pic_input_read(path, PIC_IGNORE_MAX, 0, &len, NULL))) {
            (void)pic_ignore_add_named(&ig, files[i], t, len, "");
            free(t);
        }
    }
    if (!(t = fld_versioned(c, "ignore-glob")) && ask) {
        t = fld_tool(c, "ignore-glob");
    }
    if (t) {
        *held = strspn(t, " \t\r\n,") < strlen(t);
        (void)pic_ignore_add_glob(&ig, t, strlen(t), "");
        free(t);
    }
    hit = pic_ignore_ignored(&ig, rel, 1);
    if (!hit && snprintf(path, sizeof path, "%s/journal.sqlite", rel) < (int)sizeof path) {
        hit = pic_ignore_ignored(&ig, path, 0);
    }
    pic_ignore_free(&ig);
    return hit;
}

/* The checkout's root and DIR's path from it, for a workspace's .pic folder DIR. */
static int fld_where(const char *dir, fld_checkout *c, char *rel, size_t cap) {
    char real[PATH_MAX];
    if (!fld_find(dir, c) || !realpath(dir, real) || strncmp(real, c->root, strlen(c->root)) != 0 ||
        real[strlen(c->root)] != '/') {
        return 0;
    }
    return snprintf(rel, cap, "%s", real + strlen(c->root) + 1) < (int)cap;
}

/* The line to add and the file it goes in, relative to the root. */
static void fld_line(const fld_checkout *c, const char *rel, char *line, size_t lcap, const char **file) {
    int zit = strcmp(c->tool, "zit") == 0;
    (void)snprintf(line, lcap, zit ? "%s/" : "%s/*", rel);
    *file = zit ? ".zitignore" : ".fossil-settings/ignore-glob";
}

/* Adds LINE to the file at PATH once, leaving every other byte as it is: on a new line, or, in a
 * list kept on one comma-separated line, after the last entry. Creates the file when absent. */
static int fld_add(const char *path, const char *line) {
    char *t = NULL;
    size_t len = 0, at;
    char add[PATH_MAX + 4];
    int fd, ok, comma;
    struct stat sb;
    if (stat(path, &sb) == 0 && (!S_ISREG(sb.st_mode) || sb.st_size > PIC_IGNORE_MAX)) {
        return 0;
    }
    if (stat(path, &sb) == 0 && !(t = pic_input_read(path, PIC_IGNORE_MAX, 0, &len, NULL)) && sb.st_size > 0) {
        return 0;
    }
    at = len;
    while (at && (t[at - 1] == '\n' || t[at - 1] == '\r')) {
        at--;
    }
    comma = t && memchr(t, ',', at) && !memchr(t, '\n', at);
    if (comma) {
        snprintf(add, sizeof add, ",%s\n", line);
    } else {
        snprintf(add, sizeof add, "%s%s\n", len && t[len - 1] != '\n' ? "\n" : "", line);
        at = len;
    }
    free(t);
    if ((fd = open(path, O_WRONLY | O_CREAT | O_CLOEXEC, 0644)) < 0) {
        return 0;
    }
    ok = pwrite(fd, add, strlen(add), (off_t)at) == (ssize_t)strlen(add);
    if (ok && ftruncate(fd, (off_t)(at + strlen(add))) != 0) {
        ok = 0;
    }
    close(fd);
    return ok;
}

int pic_folder_vcs(const char *dir, int first, char *out, size_t cap) {
    fld_checkout c;
    char rel[PATH_MAX], line[PATH_MAX + 4], path[PATH_MAX];
    const char *file;
    int held = 0, ok;
    if (!fld_fs() || !fld_where(dir, &c, rel, sizeof rel) || fld_covered(&c, rel, 0, &held)) {
        return 0;
    }
    fld_line(&c, rel, line, sizeof line, &file);
    ok = snprintf(path, sizeof path, "%s/%s", c.root, file) < (int)sizeof path;
    if (ok && strcmp(c.tool, "fossil") == 0 && !fld_file(c.root, file)) {
        /* a new versioned ignore-glob replaces what the repository's own setting holds */
        (void)fld_covered(&c, rel, 1, &held);
        if (held) {
            ok = 0;
        } else {
            (void)snprintf(path, sizeof path, "%s/.fossil-settings", c.root);
            (void)mkdir(path, 0755);
            (void)snprintf(path, sizeof path, "%s/%s", c.root, file);
        }
    }
    if (ok && fld_add(path, line)) {
        (void)snprintf(out, cap, "added %s to %s, so %s leaves the .pic folder out of commits", line, file, c.tool);
        return 1;
    }
    if (!first) {
        return 0;
    }
    (void)snprintf(out, cap, "this folder is a %s checkout, and %s will list %s/ as new files to add; add the line %s to %s",
                   c.tool, c.tool, rel, line, file);
    return -1;
}

int pic_folder_report(const char *dir, char *out, size_t cap) {
    char real[PATH_MAX], cur[PATH_MAX], rel[PATH_MAX], line[PATH_MAX + 4];
    const char *file;
    fld_checkout c;
    size_t n = 0;
    int git = 0, found = 0, held;
    out[0] = '\0';
    if (!fld_fs() || !realpath(dir, real)) {
        return 0;
    }
    (void)snprintf(cur, sizeof cur, "%s", real);
    for (char *slash;;) {
        if (fld_file(cur, ".git")) {
            git = 1;
            break;
        }
        if (!(slash = strrchr(cur, '/')) || slash == cur) {
            break;
        }
        *slash = '\0';
    }
    if (git) {
        int on = fld_file(real, ".gitignore");
        n += (size_t)snprintf(out + n, cap - n, "git: .pic/ is %s\n",
                              on ? "covered by .pic/.gitignore" : "not covered; add .pic/.gitignore holding *");
        found = 1;
    }
    if (fld_where(dir, &c, rel, sizeof rel) && n < cap) {
        int hit = fld_covered(&c, rel, 1, &held);
        fld_line(&c, rel, line, sizeof line, &file);
        if (hit) {
            n += (size_t)snprintf(out + n, cap - n, "%s: .pic/ is covered\n", c.tool);
        } else {
            n += (size_t)snprintf(out + n, cap - n, "%s: .pic/ is not covered; add the line %s to %s\n", c.tool, line, file);
        }
        found = 1;
    }
    if (n && out[n - 1] == '\n') {
        out[n - 1] = '\0';
    }
    return found;
}