#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE 1
#endif
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "pic.h"
#include "platform.h"
#include "update.h"
#include "userdb.h"
#define UPD_EVERY_S 1800
#define UPD_BUILD_S (2 * 3600)
#define UPD_TEST_S (6 * 3600)
#define UPD_REPO "dmestas/pi-c"
#define UPD_URL "https://zeitforge.app/r/dmestas/pi-c"
typedef struct {
char dir[PATH_MAX], status[PATH_MAX], log[PATH_MAX], step[PATH_MAX], lock[PATH_MAX], src[PATH_MAX];
int quiet, force, full, own_clone;
/* update.status */
long long checked, at;
char result[24], message[512], built[64], failed[64];
} upd;
static void upd_say(const upd *u, const char *fmt, ...) {
va_list ap;
if (u->quiet) {
return;
}
va_start(ap, fmt);
(void)vprintf(fmt, ap);
va_end(ap);
(void)putchar('\n');
(void)fflush(stdout);
}
static long long upd_now(void) {
int64_t ns = pic_platform_get()->wall_ns();
return ns < 0 ? 0 : ns / 1000000000ll;
}
/* update.status: key=value lines, replaced whole by a rename. */
static void upd_load(upd *u) {
FILE *f = fopen(u->status, "r");
char line[768];
while (f && fgets(line, sizeof line, f)) {
char *eq = strchr(line, '='), *nl = strchr(line, '\n');
if (nl) {
*nl = '\0';
}
if (!eq) {
continue;
}
*eq++ = '\0';
if (strcmp(line, "checked") == 0) {
u->checked = atoll(eq);
} else if (strcmp(line, "at") == 0) {
u->at = atoll(eq);
} else if (strcmp(line, "result") == 0) {
(void)snprintf(u->result, sizeof u->result, "%s", eq);
} else if (strcmp(line, "message") == 0) {
(void)snprintf(u->message, sizeof u->message, "%s", eq);
} else if (strcmp(line, "built") == 0) {
(void)snprintf(u->built, sizeof u->built, "%s", eq);
} else if (strcmp(line, "failed") == 0) {
(void)snprintf(u->failed, sizeof u->failed, "%s", eq);
}
}
if (f) {
fclose(f);
}
}
static void upd_save(upd *u, const char *result, const char *fmt, ...) {
char tmp[PATH_MAX + 8];
va_list ap;
FILE *f;
va_start(ap, fmt);
(void)vsnprintf(u->message, sizeof u->message, fmt, ap);
va_end(ap);
(void)snprintf(u->result, sizeof u->result, "%s", result);
u->at = upd_now();
(void)snprintf(tmp, sizeof tmp, "%s.new", u->status);
if ((f = fopen(tmp, "w")) == NULL) {
return;
}
(void)fprintf(f, "checked=%lld\nat=%lld\nresult=%s\nmessage=%s\nbuilt=%s\nfailed=%s\n", u->checked, u->at, u->result, u->message,
u->built, u->failed);
if (fclose(f) == 0) {
(void)rename(tmp, u->status);
}
}
/* Runs ARGV in DIR: its exit status, or -1 when it did not start. Output goes to update.log. */
static int upd_run(const upd *u, const char *dir, int seconds, const char *const *argv) {
int st = -1;
FILE *l = fopen(u->log, "a");
if (l) {
(void)fprintf(l, "$ %s", argv[0]);
for (int i = 1; argv[i]; i++) {
(void)fprintf(l, " %s", argv[i]);
}
(void)fputc('\n', l);
fclose(l);
}
if (pic_platform_get()->run_program(argv, dir, u->log, 1, seconds, &st) != 0) {
return -1;
}
return st;
}
/* Runs ARGV in DIR and reads what it wrote into OUT (replaced each time). */
static int upd_capture(const upd *u, const char *dir, const char *const *argv, char *out, size_t cap) {
int st = -1;
FILE *f;
size_t n = 0;
out[0] = '\0';
if (pic_platform_get()->run_program(argv, dir, u->step, 0, 120, &st) != 0) {
return -1;
}
if ((f = fopen(u->step, "r")) != NULL) {
n = fread(out, 1, cap - 1, f);
fclose(f);
}
out[n] = '\0';
return st;
}
static int upd_exists(const char *path) {
struct stat sb;
return stat(path, &sb) == 0;
}
static char *upd_setting(const char *key) {
char *v = pic_setting(key);
if (v && !*v) {
free(v);
v = NULL;
}
return v;
}
/* The checkout: autoUpdate.source, or pic's own under ~/.pic/src, cloned when it is not there. 0, or
* -1 with the reason in u->message. */
static int upd_source(upd *u) {
char *src = upd_setting("autoUpdate.source");
char make[PATH_MAX + 16], parent[PATH_MAX + 8];
if (src) {
(void)snprintf(u->src, sizeof u->src, "%s", src);
free(src);
} else {
u->own_clone = 1;
(void)snprintf(parent, sizeof parent, "%s/src", u->dir);
(void)snprintf(u->src, sizeof u->src, "%s/pi-c", parent);
}
(void)snprintf(make, sizeof make, "%s/Makefile", u->src);
if (upd_exists(make)) {
return 0;
}
if (!u->own_clone) {
(void)snprintf(u->message, sizeof u->message,
"autoUpdate.source %s is not a pic source checkout; point it at one or unset it", u->src);
return -1;
}
(void)mkdir(parent, 0700);
{
const char *quarry[] = {"quarry", "repo", "clone", UPD_REPO, NULL};
const char *zit[] = {"zit", "clone", UPD_URL, "pi-c", NULL};
upd_say(u, "cloning %s into %s", UPD_REPO, parent);
if (upd_run(u, parent, 1800, quarry) == 0 && upd_exists(make)) {
return 0;
}
if (upd_run(u, parent, 1800, zit) == 0 && upd_exists(make)) {
return 0;
}
}
(void)snprintf(u->message, sizeof u->message,
"no source checkout: install quarry or zit so pic can clone pic's source into %s, or set autoUpdate.source "
"to a checkout of trunk (pic settings autoUpdate.source PATH); see %s",
parent, u->log);
return -1;
}
/* The first word after "NAME:" in zit status's header. */
static int upd_field(const char *text, const char *name, char *out, size_t cap) {
size_t n = strlen(name);
for (const char *l = text; l && *l; l = strchr(l, '\n') ? strchr(l, '\n') + 1 : NULL) {
if (strncmp(l, name, n) == 0 && l[n] == ':') {
const char *w = l + n + 1 + strspn(l + n + 1, " \t");
size_t k = strcspn(w, " \t\n");
if (k > 0 && k < cap) {
memcpy(out, w, k);
out[k] = '\0';
return 0;
}
}
}
return -1;
}
static int upd_dirty(const char *status) {
static const char *const marks[] = {"EDITED ", "ADDED ", "DELETED ", "MISSING ", "RENAMED ", "CONFLICT", "MERGED "};
for (const char *l = status; l && *l; l = strchr(l, '\n') ? strchr(l, '\n') + 1 : NULL) {
for (size_t i = 0; i < sizeof marks / sizeof *marks; i++) {
if (strncmp(l, marks[i], strlen(marks[i])) == 0) {
return 1;
}
}
}
return 0;
}
/* Copies FROM over TO the way make install-pic does: a temporary file beside it, then a rename. */
static int upd_install(const char *from, const char *to, char *err, size_t errcap) {
char tmp[PATH_MAX + 32], buf[65536];
int in = open(from, O_RDONLY), out = -1;
ssize_t n;
(void)snprintf(tmp, sizeof tmp, "%s.update.%ld", to, (long)getpid());
if (in < 0 || (out = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, 0755)) < 0) {
(void)snprintf(err, errcap, "cannot write %s: %s", tmp, strerror(errno));
goto fail;
}
while ((n = read(in, buf, sizeof buf)) > 0) {
if (write(out, buf, (size_t)n) != n) {
(void)snprintf(err, errcap, "cannot write %s: %s", tmp, strerror(errno));
goto fail;
}
}
if (n < 0 || fchmod(out, 0755) != 0 || fsync(out) != 0 || close(out) != 0) {
out = -1;
(void)snprintf(err, errcap, "cannot finish %s: %s", tmp, strerror(errno));
goto fail;
}
out = -1;
close(in);
if (rename(tmp, to) != 0) {
(void)snprintf(err, errcap, "cannot replace %s: %s", to, strerror(errno));
(void)unlink(tmp);
return -1;
}
return 0;
fail:
if (in >= 0) {
close(in);
}
if (out >= 0) {
close(out);
}
(void)unlink(tmp);
return -1;
}
static void upd_print_status(upd *u) {
time_t at;
char when[64] = "never";
upd_load(u);
at = (time_t)u->at;
if (u->at) {
(void)strftime(when, sizeof when, "%Y-%m-%d %H:%M:%S", localtime(&at));
}
if (!u->result[0]) {
puts("pic update: nothing has run yet; pic update runs one now");
return;
}
printf("pic update: %s at %s: %s\n", u->result, when, u->message);
if (u->built[0]) {
printf("last built and installed: %s\n", u->built);
}
if (u->failed[0]) {
printf("last failed: %s (the output is in %s)\n", u->failed, u->log);
}
}
/* One cycle, with the lock held. */
static int upd_cycle(upd *u, const char *self) {
char buf[8192], tip[64], branch[64], *s;
char built[PATH_MAX + 32], err[256];
if (upd_source(u) != 0) {
char why[sizeof u->message]; /* upd_save writes u->message: it cannot also be read from it */
(void)snprintf(why, sizeof why, "%s", u->message);
upd_save(u, "failed", "%s", why);
upd_say(u, "pic update: %s", u->message);
return 1;
}
{
const char *st[] = {"zit", "status", NULL};
const char *br[] = {"zit", "branch", "current", NULL};
const char *pull[] = {"zit", "pull", NULL};
const char *up[] = {"zit", "update", "trunk", NULL};
if (upd_capture(u, u->src, st, buf, sizeof buf) != 0) {
upd_save(u, "failed", "zit status failed in %s", u->src);
upd_say(u, "pic update: %s", u->message);
return 1;
}
if (upd_dirty(buf)) {
upd_save(u, "skipped", "%s has changes of its own; update never touches those", u->src);
upd_say(u, "pic update: %s", u->message);
return 1;
}
if (upd_capture(u, u->src, br, branch, sizeof branch) != 0 || (s = strchr(branch, '\n')) == NULL ||
(*s = '\0', strcmp(branch, "trunk") != 0)) {
upd_save(u, "skipped", "%s is not on trunk", u->src);
upd_say(u, "pic update: %s", u->message);
return 1;
}
upd_say(u, "pulling trunk");
if (upd_run(u, u->src, 600, pull) != 0 || upd_run(u, u->src, 600, up) != 0 ||
upd_capture(u, u->src, st, buf, sizeof buf) != 0 || upd_field(buf, "checkout", tip, sizeof tip) != 0) {
upd_save(u, "failed", "could not pull trunk into %s (see %s)", u->src, u->log);
upd_say(u, "pic update: %s", u->message);
return 1;
}
}
if (strcmp(tip, u->built) == 0 && !u->force) {
upd_save(u, "uptodate", "trunk %s is already installed", tip);
upd_say(u, "pic update: %s", u->message);
return 0;
}
if (strcmp(tip, u->failed) == 0 && !u->force) {
upd_save(u, "failed", "trunk %s failed before; waiting for a newer trunk (pic update --force tries again)", tip);
upd_say(u, "pic update: %s", u->message);
return 1;
}
{
const char *mk[] = {"nice", "-n", "10", "make", "-j2", "build/pic/release/pic", NULL};
const char *tests[] = {"nice", "-n", "10", "make", "-j2", u->full ? "test-release" : "test-smoke", NULL};
const char *ver[] = {built, "--version", NULL};
int st;
(void)snprintf(built, sizeof built, "%s/build/pic/release/pic", u->src);
upd_say(u, "building %s", tip);
if ((st = upd_run(u, u->src, UPD_BUILD_S, mk)) != 0 || !upd_exists(built)) {
(void)snprintf(u->failed, sizeof u->failed, "%s", tip);
upd_save(u, "failed", "the build of %s failed (exit %d); nothing installed; see %s", tip, st, u->log);
upd_say(u, "pic update: %s", u->message);
return 1;
}
upd_say(u, "testing %s", tip);
if ((st = upd_run(u, u->src, UPD_TEST_S, tests)) != 0) {
(void)snprintf(u->failed, sizeof u->failed, "%s", tip);
upd_save(u, "failed", "the tests of %s failed (exit %d); nothing installed; see %s", tip, st, u->log);
upd_say(u, "pic update: %s", u->message);
return 1;
}
if (upd_run(u, u->src, 60, ver) != 0) {
(void)snprintf(u->failed, sizeof u->failed, "%s", tip);
upd_save(u, "failed", "the new pic of %s does not start; nothing installed", tip);
upd_say(u, "pic update: %s", u->message);
return 1;
}
}
if (upd_install(built, self, err, sizeof err) != 0) {
upd_save(u, "failed", "built and tested %s but could not install it: %s", tip, err);
upd_say(u, "pic update: %s", u->message);
return 1;
}
(void)snprintf(u->built, sizeof u->built, "%s", tip);
u->failed[0] = '\0';
upd_save(u, "installed", "installed trunk %s over %s; restart pic to use it", tip, self);
upd_say(u, "pic update: %s", u->message);
return 0;
}
int pic_update_main(const char *self, int argc, char **argv) {
upd u;
const char *home = getenv("HOME");
const pic_platform *p = pic_platform_get();
int status = 0, autorun = 0, lock, rc;
char *v;
memset(&u, 0, sizeof u);
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i], "--status") == 0) {
status = 1;
} else if (strcmp(argv[i], "--auto") == 0) {
autorun = u.quiet = 1;
} else if (strcmp(argv[i], "--full") == 0) {
u.full = 1;
} else if (strcmp(argv[i], "--force") == 0) {
u.force = 1;
} else {
fputs("usage: pic update [--status | --auto] [--force] [--full]\n", stderr);
return 2;
}
}
if (!home || !*home || snprintf(u.dir, sizeof u.dir, "%s/.pic", home) >= (int)sizeof u.dir) {
fputs("pic update: no home directory\n", stderr);
return 1;
}
(void)mkdir(u.dir, 0700);
(void)snprintf(u.status, sizeof u.status, "%s/update.status", u.dir);
(void)snprintf(u.log, sizeof u.log, "%s/update.log", u.dir);
(void)snprintf(u.step, sizeof u.step, "%s/update.step", u.dir);
(void)snprintf(u.lock, sizeof u.lock, "%s/update.lock", u.dir);
if (status) {
upd_print_status(&u);
return 0;
}
if (autorun) {
v = pic_setting("autoUpdate");
rc = v && strcmp(v, "false") == 0;
free(v);
upd_load(&u);
if (rc || pic_offline() || (u.checked && upd_now() - u.checked < UPD_EVERY_S)) {
return 0;
}
}
if (!p->run_program || !p->self_path || !self || !*self) {
fputs("pic update: this platform cannot start programs, so pic cannot build itself here\n", stderr);
return 1;
}
if (pic_offline()) {
upd_load(&u);
upd_save(&u, "skipped", "offline is true; no pull was made");
upd_say(&u, "pic update: %s", u.message);
return 1;
}
lock = p->lock(u.lock, 0);
if (lock < 0) {
upd_say(&u, "pic update: %s", lock == -2 ? "another update is running" : "cannot take ~/.pic/update.lock");
return lock == -2 ? 0 : 1;
}
upd_load(&u);
u.checked = upd_now();
{
FILE *l = fopen(u.log, "w"); /* the log is the last update's */
if (l) {
fclose(l);
}
}
rc = upd_cycle(&u, self);
p->unlock(lock);
return rc;
}