Files
libglacier-ng/src/deps.c
T
2026-10-14 00:50:56 -04:00

728 lines
21 KiB
C

#define _POSIX_C_SOURCE 200809L
#define _XOPEN_SOURCE 700
#include <archive.h>
#include <archive_entry.h>
#include <dirent.h>
#include <errno.h>
#include <unistd.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "common.h"
#include "deps.h"
#include "istoreutils.h"
#include "log.h"
#include "pkg.h"
/* Where one entry of a package lands once linked: its RUNTIME path (what
* a message names and what two packages are compared by). */
struct dest {
char *path;
bool dir;
};
/* One installed (or about to be installed) package. repo/name/dirname
* come from the store layout (store/<repo>/<name>/<name>-<ver>), or from
* the manifest for a package that isn't installed yet. deps/confs come
* from the manifest; a version dir without one (installed before
* manifests were saved) has none. */
struct rec {
char repo[NAME_MAX + 1];
char name[NAME_MAX + 1];
char dirname[NAME_MAX + 1];
semver_t ver;
gl_pkg_t pkg;
bool touched; /* added or replaced by this transaction */
bool gone; /* removed or replaced: not in the result */
/* For the file checks: where its files come from (a version dir in
* a store, or a .gpkg not installed yet), whether its link pass is
* this transaction's (fresh: it reports refusals), whether that
* pass may displace foreign files (-O), and, once computed, where
* every entry lands. */
char src[PATH_MAX];
bool from_archive;
bool fresh;
bool overwrite;
struct dest *dests;
size_t ndests;
bool dests_loaded;
};
/* What the file checks need to know about the transaction's context. */
struct env {
gl_ctx_scope_t scope;
uid_t uid;
const char *sysroot;
int strict;
char store_rt[PATH_MAX]; /* live store, runtime path: link targets */
char store_at[PATH_MAX]; /* the store being checked, install path */
};
struct set {
struct rec *v;
size_t n;
};
/* A package the transaction removes outright (not replaced). */
struct removed {
char repo[NAME_MAX + 1];
char name[NAME_MAX + 1];
};
static void
set_free(struct set *s)
{
for (size_t i = 0; i < s->n; i++) {
gl_free_pkg(&s->v[i].pkg);
gl_free_semver(&s->v[i].ver);
for (size_t k = 0; k < s->v[i].ndests; k++) { free(s->v[i].dests[k].path); }
free(s->v[i].dests);
}
free(s->v);
s->v = NULL;
s->n = 0;
}
static struct rec *
set_add(struct set *s)
{
struct rec *grown = realloc(s->v, (s->n + 1) * sizeof(*s->v));
if (!grown) { return NULL; }
s->v = grown;
memset(&s->v[s->n], 0, sizeof(*s->v));
return &s->v[s->n++];
}
static int
semver_cmp(const semver_t *a, const semver_t *b)
{
if (a->maj != b->maj) { return a->maj < b->maj ? -1 : 1; }
if (a->min != b->min) { return a->min < b->min ? -1 : 1; }
if (a->pat != b->pat) { return a->pat < b->pat ? -1 : 1; }
return 0;
}
/* Every installed package in `store`. A missing store is empty (nothing
* installed); any other failure to read it is an error, unless `lenient`
* (the system store as seen from user scope: unreadable means "nothing
* there to depend on", and the check then says what's missing). */
static int
load_store(const char *store, struct set *s, bool lenient)
{
DIR *sd = opendir(store);
if (!sd) {
return (errno == ENOENT || lenient) ? 0 : -1;
}
int rc = 0;
struct dirent *re;
while (rc == 0 && (re = readdir(sd))) {
if (re->d_name[0] == '.') { continue; }
char repo_path[PATH_MAX];
snprintf(repo_path, sizeof(repo_path), "%s/%s", store, re->d_name);
DIR *rd = opendir(repo_path);
if (!rd) { continue; } /* not a directory */
struct dirent *ne;
while (rc == 0 && (ne = readdir(rd))) {
if (ne->d_name[0] == '.') { continue; }
char name_path[PATH_MAX];
snprintf(name_path, sizeof(name_path), "%s/%s", repo_path, ne->d_name);
DIR *nd = opendir(name_path);
if (!nd) { continue; }
struct dirent *ve;
while ((ve = readdir(nd))) {
if (ve->d_name[0] == '.') { continue; }
struct rec *r = set_add(s);
if (!r) { rc = -1; break; }
snprintf(r->repo, sizeof(r->repo), "%s", re->d_name);
snprintf(r->name, sizeof(r->name), "%s", ne->d_name);
snprintf(r->dirname, sizeof(r->dirname), "%s", ve->d_name);
snprintf(r->src, sizeof(r->src), "%s/%s", name_path, ve->d_name);
char manifest[PATH_MAX];
snprintf(manifest, sizeof(manifest), "%s/%s/" GL_MANIFEST_COPY,
name_path, ve->d_name);
if (gl_gpm2gpkg(manifest, &r->pkg) != 0) {
gl_free_pkg(&r->pkg);
memset(&r->pkg, 0, sizeof(r->pkg));
}
/* The version is the dir name minus "<name>-". */
size_t prefix = strlen(ne->d_name) + 1;
r->ver = gl_parse_semver(strlen(ve->d_name) > prefix
? ve->d_name + prefix : NULL);
break; /* one version per package */
}
closedir(nd);
}
closedir(rd);
}
closedir(sd);
return rc;
}
static const struct rec *
find_live(const struct set *s, const char *repo, const char *name)
{
for (size_t i = 0; i < s->n; i++) {
if (!s->v[i].gone && strcmp(s->v[i].repo, repo) == 0 &&
strcmp(s->v[i].name, name) == 0) {
return &s->v[i];
}
}
return NULL;
}
/* Is there a package named `name` (any repo) in the result or, from user
* scope, the system? With `min`, it must also be at least that version. */
static bool
provided(const struct set *res, const struct set *sys, const char *name,
const semver_t *min)
{
const struct set *sets[] = { res, sys };
for (int k = 0; k < 2; k++) {
for (size_t i = 0; i < sets[k]->n; i++) {
const struct rec *r = &sets[k]->v[i];
if (r->gone || strcmp(r->name, name) != 0) { continue; }
if (!min || semver_cmp(&r->ver, min) >= 0) { return true; }
}
}
return false;
}
static bool
lists_conflict(const struct rec *r, const char *name)
{
if (!r->pkg.pkg_conflicts) { return false; }
for (const conf_t *c = r->pkg.pkg_conflicts; c->conf_name; c++) {
if (strcmp(c->conf_name, name) == 0) { return true; }
}
return false;
}
/* Does the transaction touch packages named `name`: add or replace one,
* or remove one outright? */
static bool
name_touched(const struct set *res, const struct removed *rm, size_t nrm,
const char *name)
{
for (size_t i = 0; i < res->n; i++) {
if (res->v[i].touched && strcmp(res->v[i].name, name) == 0) { return true; }
}
for (size_t i = 0; i < nrm; i++) {
if (strcmp(rm[i].name, name) == 0) { return true; }
}
return false;
}
static void
fmt_ver(const semver_t *v, char *out, size_t outsz)
{
snprintf(out, outsz, "%d.%d.%d", v->maj, v->min, v->pat);
}
/*
* File checks (ROADMAP 2.6 and 1.2's --strict).
*
* Every entry of a package lands somewhere once linked; dest_of mirrors
* gl_link_pkg exactly: entries under a dot-prefixed name are never
* linked, a leading usr/ is stripped, base-repo packages in system scope
* land in /usr and everything else in the scope's links tree, and in
* system scope the top-level etc/ is copied into /etc.
*/
static bool
has_dot_component(const char *rel)
{
for (const char *c = rel; c; c = strchr(c, '/')) {
if (*c == '/') { c++; }
if (*c == '.') { return true; }
if (!*c) { break; }
}
return false;
}
static int
dest_of(const struct env *e, const char *repo, const char *rel, char *out, size_t outsz)
{
if (e->scope == GL_SCOPE_SYS &&
(strcmp(rel, "etc") == 0 || strncmp(rel, "etc/", 4) == 0)) {
return snprintf(out, outsz, "/%s", rel) >= (int)outsz ? -1 : 0;
}
if (strncmp(rel, "usr/", 4) == 0) { rel += 4; }
char base[PATH_MAX];
if (e->scope == GL_SCOPE_SYS) {
snprintf(base, sizeof(base), "%s", strcmp(repo, "base") == 0
? "/usr" : GL_SYS_ROOT "/links");
} else {
char ub[PATH_MAX];
gl_usr_base(e->uid, ub, sizeof(ub));
snprintf(base, sizeof(base), "%s/links", ub);
}
return snprintf(out, outsz, "%s/%s", base, rel) >= (int)outsz ? -1 : 0;
}
static int
add_dest(struct rec *r, const char *path, bool dir)
{
for (size_t k = 0; k < r->ndests; k++) {
if (strcmp(r->dests[k].path, path) == 0) {
if (!dir) { r->dests[k].dir = false; }
return 0;
}
}
struct dest *grown = realloc(r->dests, (r->ndests + 1) * sizeof(*grown));
if (!grown) { return -1; }
r->dests = grown;
r->dests[r->ndests].path = strdup(path);
if (!r->dests[r->ndests].path) { return -1; }
r->dests[r->ndests].dir = dir;
r->ndests++;
return 0;
}
/* `rel` (and, for an archive entry, its parents, which tar needn't list) */
static int
add_rel(const struct env *e, struct rec *r, const char *rel, bool dir, bool parents)
{
if (rel[0] == '\0' || has_dot_component(rel)) { return 0; }
char d[PATH_MAX];
if (parents) {
char buf[PATH_MAX];
snprintf(buf, sizeof(buf), "%s", rel);
for (char *p = buf; (p = strchr(p, '/')); p++) {
*p = '\0';
if (dest_of(e, r->repo, buf, d, sizeof(d)) != 0 || add_dest(r, d, true) != 0) {
return -1;
}
*p = '/';
}
}
if (dest_of(e, r->repo, rel, d, sizeof(d)) != 0) { return -1; }
return add_dest(r, d, dir);
}
static int
walk_store_dir(const struct env *e, struct rec *r, const char *dir, size_t strip)
{
DIR *d = opendir(dir);
if (!d) { return 0; }
int rc = 0;
struct dirent *ent;
while (rc == 0 && (ent = readdir(d))) {
if (ent->d_name[0] == '.') { continue; }
char full[PATH_MAX];
snprintf(full, sizeof(full), "%s/%s", dir, ent->d_name);
struct stat st;
if (lstat(full, &st) != 0) { continue; }
const char *rel = full + strip;
if (S_ISDIR(st.st_mode)) {
rc = add_rel(e, r, rel, true, false);
if (rc == 0) { rc = walk_store_dir(e, r, full, strip); }
} else if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
rc = add_rel(e, r, rel, false, false);
}
}
closedir(d);
return rc;
}
static int
walk_archive(const struct env *e, struct rec *r)
{
char prefix[PATH_MAX];
snprintf(prefix, sizeof(prefix), "%s-%d.%d.%d/files/", r->name,
r->pkg.pkg_ver.maj, r->pkg.pkg_ver.min, r->pkg.pkg_ver.pat);
size_t plen = strlen(prefix);
struct archive *a = archive_read_new();
archive_read_support_filter_gzip(a);
archive_read_support_format_tar(a);
if (archive_read_open_filename(a, r->src, 10240) != ARCHIVE_OK) {
archive_read_free(a);
return -1;
}
int rc = 0;
struct archive_entry *entry;
while (rc == 0 && archive_read_next_header(a, &entry) == ARCHIVE_OK) {
const char *path = archive_entry_pathname(entry);
if (strncmp(path, prefix, plen) == 0) {
char rel[PATH_MAX];
snprintf(rel, sizeof(rel), "%s", path + plen);
size_t len = strlen(rel);
while (len > 0 && rel[len - 1] == '/') { rel[--len] = '\0'; }
mode_t type = archive_entry_filetype(entry);
if (type == AE_IFDIR) {
rc = add_rel(e, r, rel, true, true);
} else if (type == AE_IFREG || type == AE_IFLNK) {
rc = add_rel(e, r, rel, false, true);
}
}
archive_read_data_skip(a);
}
archive_read_free(a);
return rc;
}
static int
load_dests(const struct env *e, struct rec *r)
{
if (r->dests_loaded) { return 0; }
r->dests_loaded = true;
if (r->from_archive) { return walk_archive(e, r); }
return walk_store_dir(e, r, r->src, strlen(r->src) + 1);
}
struct item {
const char *path;
bool dir;
size_t rec;
};
static int
item_cmp(const void *a, const void *b)
{
const struct item *x = a, *y = b;
int c = strcmp(x->path, y->path);
if (c != 0) { return c; }
return x->rec < y->rec ? -1 : x->rec > y->rec;
}
/* Two packages shipping the same path (ROADMAP 2.6): whichever linked
* last used to win silently. Refused when either package is part of
* this transaction; at least one of the two entries must be a file or
* symlink (sharing a directory is normal). */
static int
check_collisions(const struct env *e, struct set *res)
{
size_t n = 0;
for (size_t i = 0; i < res->n; i++) {
if (res->v[i].gone) { continue; }
if (load_dests(e, &res->v[i]) != 0) { return -1; }
n += res->v[i].ndests;
}
if (n == 0) { return 0; }
struct item *items = malloc(n * sizeof(*items));
if (!items) { return -1; }
size_t k = 0;
for (size_t i = 0; i < res->n; i++) {
if (res->v[i].gone) { continue; }
for (size_t j = 0; j < res->v[i].ndests; j++) {
items[k++] = (struct item){ res->v[i].dests[j].path, res->v[i].dests[j].dir, i };
}
}
qsort(items, n, sizeof(*items), item_cmp);
int problems = 0;
for (size_t start = 0; start < n; ) {
size_t end = start + 1;
while (end < n && strcmp(items[end].path, items[start].path) == 0) { end++; }
for (size_t a = start; a < end; a++) {
const struct rec *ra = &res->v[items[a].rec];
if (!ra->touched && !ra->fresh) { continue; }
for (size_t b = start; b < end; b++) {
if (b == a || (items[a].dir && items[b].dir)) { continue; }
const struct rec *rb = &res->v[items[b].rec];
/* two of this transaction's packages: report once */
if ((rb->touched || rb->fresh) && b < a) { continue; }
lg_printf(2, "%s::%s and %s::%s both ship %s.",
ra->repo, ra->name, rb->repo, rb->name, items[a].path);
problems++;
}
}
start = end;
}
free(items);
return problems;
}
/* --strict: what the post-commit link pass would refuse, refused now
* instead, before anything is committed. Same rules as gl_link_pkg, for
* this transaction's packages only (the ones whose refusals it reports).
* User scope never has a foreign file in its links tree (rebuilt on
* every commit), and config in /etc is offered as .new, never refused. */
static int
check_links(const struct env *e, struct set *res)
{
if (!e->strict || e->scope != GL_SCOPE_SYS) { return 0; }
char store_prefix[PATH_MAX];
snprintf(store_prefix, sizeof(store_prefix), "%s/", e->store_rt);
size_t splen = strlen(store_prefix);
int problems = 0;
for (size_t i = 0; i < res->n; i++) {
struct rec *r = &res->v[i];
if (r->gone || !(r->touched || r->fresh)) { continue; }
if (load_dests(e, r) != 0) { return -1; }
for (size_t k = 0; k < r->ndests; k++) {
const struct dest *d = &r->dests[k];
if (strcmp(d->path, "/etc") == 0 || strncmp(d->path, "/etc/", 5) == 0) { continue; }
char at[PATH_MAX];
snprintf(at, sizeof(at), "%s%s", e->sysroot, d->path);
struct stat st;
if (lstat(at, &st) != 0) {
if (errno == ENOTDIR) {
lg_printf(2, "Refusing to overwrite non-glacier path: %s", at);
problems++;
}
continue;
}
bool ours = false;
if (S_ISLNK(st.st_mode)) {
char target[PATH_MAX];
ssize_t tn = readlink(at, target, sizeof(target) - 1);
ours = tn >= 0 && (size_t)tn >= splen &&
strncmp(target, store_prefix, splen) == 0;
}
if (d->dir) {
/* a foreign symlink here (usr/sbin -> bin) is followed,
* like gl_link_pkg does; one of glacier's isn't */
if (S_ISDIR(st.st_mode) || (S_ISLNK(st.st_mode) && !ours)) { continue; }
if (ours) {
lg_printf(2, "Refusing to merge directory %s into another package's symlink", at);
} else {
lg_printf(2, "Refusing to overwrite non-glacier path: %s", at);
}
problems++;
continue;
}
if (ours) { continue; }
if (r->overwrite && (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))) {
char kept[PATH_MAX];
snprintf(kept, sizeof(kept), "%s/.displaced%s", e->store_at, d->path);
struct stat kst;
if (lstat(kept, &kst) != 0) { continue; } /* -O moves it aside */
}
lg_printf(2, "Refusing to overwrite non-glacier path: %s", at);
problems++;
}
}
return problems;
}
/* The checks themselves, on the result of a transaction. */
static int
check(const struct env *e, struct set *res, const struct set *sys,
const struct removed *rm, size_t nrm)
{
int problems = 0;
char ver[64];
for (size_t i = 0; i < res->n; i++) {
const struct rec *p = &res->v[i];
if (p->gone) { continue; }
const dep_t *deps = p->pkg.pkg_dependencies;
for (const dep_t *d = deps; d && d->dep_name; d++) {
/* An added package: all its deps. An untouched one:
* only deps on something this transaction changes. */
if (!p->touched && !name_touched(res, rm, nrm, d->dep_name)) { continue; }
if (provided(res, sys, d->dep_name, &d->dep_ver)) { continue; }
const struct removed *gone = NULL;
for (size_t k = 0; !p->touched && k < nrm; k++) {
if (strcmp(rm[k].name, d->dep_name) == 0) { gone = &rm[k]; break; }
}
if (gone && !provided(res, sys, d->dep_name, NULL)) {
lg_printf(2, "Removing %s::%s breaks %s::%s.",
gone->repo, gone->name, p->repo, p->name);
} else {
fmt_ver(&d->dep_ver, ver, sizeof(ver));
lg_printf(2, "%s::%s requires %s >= %s, which is not installed.",
p->repo, p->name, d->dep_name, ver);
}
problems++;
}
if (!p->touched) { continue; }
for (size_t j = 0; j < res->n; j++) {
const struct rec *q = &res->v[j];
if (j == i || q->gone) { continue; }
/* Two added packages: report the pair once. */
if (q->touched && j < i) { continue; }
if (lists_conflict(p, q->name) || lists_conflict(q, p->name)) {
lg_printf(2, "%s::%s conflicts with %s::%s.",
p->repo, p->name, q->repo, q->name);
problems++;
}
}
}
int files = check_collisions(e, res);
if (files < 0) { return -1; }
problems += files;
files = check_links(e, res);
if (files < 0) { return -1; }
return problems + files;
}
/* The runtime path of the live store for a context's scope and root. */
static void
env_init(struct env *e, const gl_context_t *ctx, const char *store_at)
{
e->scope = ctx->scope;
e->uid = ctx->uid;
e->sysroot = ctx->sysroot;
e->strict = ctx->strict;
if (ctx->scope == GL_SCOPE_USR) {
char ub[PATH_MAX];
gl_usr_base(ctx->uid, ub, sizeof(ub));
snprintf(e->store_rt, sizeof(e->store_rt), "%s/store", ub);
} else {
snprintf(e->store_rt, sizeof(e->store_rt), "%s/store", GL_SYS_ROOT);
}
snprintf(e->store_at, sizeof(e->store_at), "%s", store_at);
}
/* The system store, which a user-scope transaction's deps may rely on.
* Always the running system's: user scope never has an install root. */
static int
load_sys(gl_ctx_scope_t scope, struct set *sys)
{
if (scope != GL_SCOPE_USR) { return 0; }
return load_store(GL_SYS_ROOT "/store", sys, true);
}
int
gl_check_plan(const gl_context_t *live,
const char *const *add_paths, size_t n_add,
const char *const *remove_ids, size_t n_remove)
{
if (!live) { return -1; }
struct set res = { NULL, 0 }, sys = { NULL, 0 };
struct removed *rm = calloc(n_remove + 1, sizeof(*rm));
size_t nrm = 0;
int rc = -1;
if (!rm || load_store(live->store_path, &res, false) != 0 ||
load_sys(live->scope, &sys) != 0) {
goto out;
}
for (size_t i = 0; i < n_remove; i++) {
const char *sep = strstr(remove_ids[i], "::");
if (!sep) { goto out; }
char repo[NAME_MAX + 1];
snprintf(repo, sizeof(repo), "%.*s", (int)(sep - remove_ids[i]), remove_ids[i]);
struct rec *r = (struct rec *)find_live(&res, repo, sep + 2);
if (!r) { continue; } /* not installed: nothing to break */
r->gone = true;
snprintf(rm[nrm].repo, sizeof(rm[nrm].repo), "%s", r->repo);
snprintf(rm[nrm].name, sizeof(rm[nrm].name), "%s", r->name);
nrm++;
}
for (size_t i = 0; i < n_add; i++) {
gl_pkg_t pkg = {0};
if (gl_read_pkg_manifest(add_paths[i], &pkg) != GL_INSTALL_OK) { goto out; }
/* Same repo::name installed: this replaces it. */
struct rec *old = (struct rec *)find_live(&res, pkg.pkg_repo, pkg.pkg_name);
if (old) { old->gone = true; }
struct rec *r = set_add(&res);
if (!r) { gl_free_pkg(&pkg); goto out; }
snprintf(r->repo, sizeof(r->repo), "%s", pkg.pkg_repo);
snprintf(r->name, sizeof(r->name), "%s", pkg.pkg_name);
r->ver = (semver_t){ pkg.pkg_ver.maj, pkg.pkg_ver.min, pkg.pkg_ver.pat, NULL };
r->pkg = pkg;
r->touched = true;
r->fresh = true;
r->overwrite = live->overwrite != 0;
r->from_archive = true;
snprintf(r->src, sizeof(r->src), "%s", add_paths[i]);
}
struct env e;
env_init(&e, live, live->store_path);
rc = check(&e, &res, &sys, rm, nrm);
out:
set_free(&res);
set_free(&sys);
free(rm);
return rc;
}
int
gl_check_txn(const gl_context_t *stage)
{
if (!stage || stage->mode != GL_CTX_STAGE) { return -1; }
char base[PATH_MAX], live_store[PATH_MAX];
if (stage->scope == GL_SCOPE_USR) {
gl_usr_base(stage->uid, base, sizeof(base));
} else {
snprintf(base, sizeof(base), "%s%s", stage->sysroot, GL_SYS_ROOT);
}
snprintf(live_store, sizeof(live_store), "%s/store", base);
struct set res = { NULL, 0 }, live = { NULL, 0 }, sys = { NULL, 0 };
struct removed *rm = NULL;
size_t nrm = 0;
int rc = -1;
if (load_store(stage->store_path, &res, false) != 0 ||
load_store(live_store, &live, false) != 0 ||
load_sys(stage->scope, &sys) != 0) {
goto out;
}
rm = calloc(live.n + 1, sizeof(*rm));
if (!rm) { goto out; }
/* Touched: in the stage, but not as this exact version in live.
* Fresh: installed by this transaction (a reinstall of the same
* version is fresh but not touched); its link pass is this one's. */
for (size_t i = 0; i < res.n; i++) {
struct rec *r = &res.v[i];
const struct rec *l = find_live(&live, r->repo, r->name);
r->touched = !l || strcmp(l->dirname, r->dirname) != 0;
char marker[PATH_MAX];
struct stat mst;
snprintf(marker, sizeof(marker), "%s/" GL_FRESH_MARKER, r->src);
r->fresh = lstat(marker, &mst) == 0;
snprintf(marker, sizeof(marker), "%s/" GL_OVERWRITE_MARKER, r->src);
r->overwrite = lstat(marker, &mst) == 0;
}
/* Removed: in live, and no longer in the stage under that repo::name. */
for (size_t i = 0; i < live.n; i++) {
if (find_live(&res, live.v[i].repo, live.v[i].name)) { continue; }
snprintf(rm[nrm].repo, sizeof(rm[nrm].repo), "%s", live.v[i].repo);
snprintf(rm[nrm].name, sizeof(rm[nrm].name), "%s", live.v[i].name);
nrm++;
}
struct env e;
env_init(&e, stage, stage->store_path);
rc = check(&e, &res, &sys, rm, nrm);
out:
set_free(&res);
set_free(&live);
set_free(&sys);
free(rm);
return rc;
}