#include "stamp.h"
#include "platform.h"
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
static char file_buf[STAMP_FILE_MAX + 1];
// Join A, B and C into OUT without stdio, so the run path pulls in no
// formatting code. False if it does not fit in CAP.
static bool join3(char *out, size_t cap, const char *a, const char *b, const char *c)
{
size_t la = strlen(a), lb = strlen(b), lc = strlen(c);
if (la + lb + lc >= cap) return false;
memcpy(out, a, la);
memcpy(out + la, b, lb);
memcpy(out + la + lb, c, lc + 1);
return true;
}
// Fill OUT with "$SUIT_HOME/NAME" ("$HOME/.suit/NAME" without SUIT_HOME).
// Returns the length of the home folder part, or 0 after saying why.
size_t suit_home_path(char *out, size_t cap, const char *name)
{
const char *home = sys_getenv("SUIT_HOME");
const char *infix = "/";
if (home == nullptr || home[0] == '\0') {
home = sys_getenv("HOME");
infix = "/.suit/";
if (home == nullptr || home[0] == '\0') {
say(STDERR_FILENO, "suit: neither SUIT_HOME nor HOME is set\n");
return 0;
}
}
if (!join3(out, cap, home, infix, name)) {
say(STDERR_FILENO, "suit: home path is longer than %zu bytes\n", cap - 1);
return 0;
}
return strlen(home) + strlen(infix) - 1;
}
// ---- stamp ----------------------------------------------------------------
// Append one stamp line. False if the buffer is full.
static bool append(size_t *len, const char *path, bool missing, const struct stat *st)
{
size_t room = sizeof file_buf - *len;
int n;
if (missing)
n = snprintf(file_buf + *len, room, "- - - %s\n", path);
else
n = snprintf(file_buf + *len, room, "%lld %lld %lld %s\n", (long long)st->st_size,
(long long)platform_mtime_sec(st), (long long)platform_mtime_nsec(st), path);
ensure(n > 0);
if ((size_t)n >= room) return false;
*len += (size_t)n;
return true;
}
static bool path_ok(const char *p)
{
size_t n = strlen(p);
return n > 0 && n < STAMP_PATH_MAX && strchr(p, '\n') == nullptr;
}
// Build the whole stamp in file_buf. Returns its length, or -1 after saying why.
static ssize_t build_stamp(int npaths, char *const *paths)
{
size_t len = 0;
for (int i = 0; i < npaths; i++) {
const char *p = paths[i];
if (!path_ok(p)) {
say(STDERR_FILENO, "suit: stamp: path %d is empty, has a newline, or is "
"longer than %zu bytes\n", i + 1, STAMP_PATH_MAX - 1);
return -1;
}
struct stat st = {};
bool missing = false;
if (sys_stat(p, &st) != 0) {
if (errno != ENOENT && errno != ENOTDIR) {
say(STDERR_FILENO, "suit: stamp: cannot stat %s: %s\n", p, strerror(errno));
return -1;
}
missing = true;
}
if (!append(&len, p, missing, &st)) {
say(STDERR_FILENO, "suit: stamp is larger than %zu bytes\n", STAMP_FILE_MAX);
return -1;
}
}
return (ssize_t)len;
}
// Write the stamp to TEMP and move it over FINAL. On failure TEMP is removed.
static bool replace_file(const char *temp, const char *final, size_t len)
{
int fd = sys_open(temp, O_WRONLY | O_CREAT | O_EXCL, 0644);
if (fd < 0) return false;
if (!sys_write_all(fd, file_buf, len) || sys_fsync(fd) != 0 || sys_close(fd) != 0 ||
sys_rename(temp, final) != 0) {
int err = errno;
(void)sys_unlink(temp);
errno = err;
return false;
}
return true;
}
int stamp_write(int npaths, char *const *paths)
{
char final_path[STAMP_PATH_MAX];
char temp_path[STAMP_PATH_MAX + 32];
size_t home_len = suit_home_path(final_path, sizeof final_path, "stamp");
if (home_len == 0) return 1;
ssize_t len = build_stamp(npaths, paths);
if (len < 0) return 1;
final_path[home_len] = '\0';
if (sys_mkdir(final_path, 0755) != 0 && errno != EEXIST) {
say(STDERR_FILENO, "suit: cannot create %s: %s\n", final_path, strerror(errno));
return 1;
}
final_path[home_len] = '/';
[[maybe_unused]] int n = snprintf(temp_path, sizeof temp_path, "%s.tmp.%d", final_path, (int)getpid());
ensure(n > 0 && (size_t)n < sizeof temp_path);
if (!replace_file(temp_path, final_path, (size_t)len)) {
say(STDERR_FILENO, "suit: cannot write %s: %s\n", final_path, strerror(errno));
return 1;
}
return 0;
}
// ---- run ------------------------------------------------------------------
// Parse a decimal field ending at a space. Returns the position after the
// space, or nullptr. A nullptr S gives nullptr.
static char *read_number(char *s, int64_t *out)
{
if (s == nullptr || *s < '0' || *s > '9') return nullptr;
int64_t v = 0;
for (; *s >= '0' && *s <= '9'; s++) {
if (v > (INT64_MAX - 9) / 10) return nullptr;
v = v * 10 + (*s - '0');
}
if (*s != ' ') return nullptr;
*out = v;
return s + 1;
}
static int bad_stamp(int line, const char *why)
{
say(STDERR_FILENO, "suit: stamp:%d: %s\n", line, why);
return EXIT_BAD_STAMP;
}
// Read the stamp into file_buf. Returns its length, -1 for an error (errno
// set), -2 if there is no stamp, -3 if it is over the limit.
static ssize_t read_stamp(const char *path)
{
int fd = sys_open(path, O_RDONLY, 0);
if (fd < 0) return errno == ENOENT ? -2 : -1;
size_t len = 0;
ssize_t result = -3;
while (len <= STAMP_FILE_MAX) {
ssize_t n = sys_read(fd, file_buf + len, STAMP_FILE_MAX + 1 - len);
if (n < 0 && errno == EINTR) continue;
if (n <= 0) {
result = n < 0 ? -1 : (ssize_t)len;
break;
}
len += (size_t)n;
}
int err = errno;
(void)sys_close(fd);
errno = err;
return result;
}
// Stat paths that share a folder through one open handle on it, so the
// kernel walks the folder's path once instead of once per file.
typedef struct {
char dir[STAMP_PATH_MAX];
size_t len; // 0: no folder remembered yet
int fd; // -1: not opened; opened on the second path in a folder
} DirCache;
static int stat_path(DirCache *dc, const char *path, struct stat *st)
{
const char *slash = strrchr(path, '/');
if (slash == nullptr || slash == path || slash[1] == '\0') return sys_stat(path, st);
size_t len = (size_t)(slash - path);
if (dc->len != len || memcmp(dc->dir, path, len) != 0) {
if (dc->fd >= 0) (void)sys_close(dc->fd);
dc->fd = -1;
dc->len = len;
memcpy(dc->dir, path, len);
return sys_stat(path, st);
}
if (dc->fd < 0) {
dc->dir[len] = '\0';
dc->fd = sys_open(dc->dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC, 0);
if (dc->fd < 0) return sys_stat(path, st);
}
return sys_fstatat(dc->fd, slash + 1, st);
}
// True if the path still matches what the stamp line recorded.
static bool unchanged(DirCache *dc, const char *path, bool recorded_missing,
int64_t size, int64_t sec, int64_t nsec)
{
struct stat st;
if (stat_path(dc, path, &st) != 0)
return recorded_missing && (errno == ENOENT || errno == ENOTDIR);
return !recorded_missing && st.st_size == size && platform_mtime_sec(&st) == sec &&
platform_mtime_nsec(&st) == nsec;
}
int stamp_check(bool report)
{
DirCache dir_cache; // dir is written before it is read
dir_cache.len = 0;
dir_cache.fd = -1;
char path[STAMP_PATH_MAX];
if (suit_home_path(path, sizeof path, "stamp") == 0) return 1;
ssize_t got = read_stamp(path);
if (got == -2) {
say(STDERR_FILENO, "suit: no stamp at %s; run `suit stamp PATH...` first\n", path);
return EXIT_NO_STAMP;
}
if (got == -3) return bad_stamp(1, "stamp is larger than the limit");
if (got < 0) {
say(STDERR_FILENO, "suit: cannot read %s: %s\n", path, strerror(errno));
return 1;
}
char *p = file_buf;
char *end = file_buf + got;
int line = 0;
int rc = EXIT_UNCHANGED;
while (p < end) {
char *nl = memchr(p, '\n', (size_t)(end - p));
line++;
if (nl == nullptr) {
rc = bad_stamp(line, "line has no newline");
break;
}
*nl = '\0';
char *s = p;
p = nl + 1;
bool missing = strncmp(s, "- - - ", 6) == 0;
int64_t size = 0, sec = 0, nsec = 0;
if (missing) s += 6;
else s = read_number(read_number(read_number(s, &size), &sec), &nsec);
if (s == nullptr) {
rc = bad_stamp(line, "expected SIZE MTIME_SEC MTIME_NSEC PATH");
break;
}
if (s[0] == '\0') {
rc = bad_stamp(line, "path is empty");
break;
}
if (!unchanged(&dir_cache, s, missing, size, sec, nsec)) {
if (report) say(STDOUT_FILENO, "changed: %s\n", s);
rc = EXIT_CHANGED;
}
}
if (dir_cache.fd >= 0) (void)sys_close(dir_cache.fd);
return rc;
}