/* * util.h - shared utility layer for rawnix.org package tools * https://rawnix.org * * Copyright (c) 2025 zorz@gmx.com * SPDX-License-Identifier: BSD-2-Clause */ #ifndef UTIL_H #define UTIL_H #include #include #include /* ── Version ────────────────────────────────────────────────────────── */ #define MKPKG_VERSION "1.6.2" /* ── Compile-time limits ─────────────────────────────────────────────── */ #define PATH_MAX_LEN 4096 /* max filesystem path length */ #define LINE_MAX_LEN 8192 /* max line in any config or script file */ #define CMD_MAX_LEN 8192 /* max shell command string */ /* ── Package database ────────────────────────────────────────────────── */ #define PKG_DBDIR "/var/lib/pkg" #define PKG_DBFILE "db" #define PKG_DEPSFILE "deps.db" /* maintained by pkg(8) */ #define PKG_METADIR "var/lib/pkg/meta" /* relative to root */ /* ── Port tree ───────────────────────────────────────────────────────── */ #define PORTS_CONF_DIR "/etc/ports" #define TRUSTED_KEYS_DIR "/etc/ports/keys" /* trusted signify *.pub */ #define PORTS_PATH "/ports/core:/usr/ports" /* Package filename format: name#version.pkg.tar.gz */ #define PKG_EXT ".pkg.tar." #define PKG_DELIM '#' /* ── Error codes ─────────────────────────────────────────────────────── */ #define E_OK 0 #define E_GENERAL 1 #define E_PKGFILE 2 #define E_DIR_PERM 3 #define E_DOWNLOAD 4 #define E_UNPACK 5 #define E_CHECKSUM 6 #define E_FOOTPRINT 7 #define E_BUILD 8 #define E_INSTALL 9 #define E_SIGNATURE 10 /* ── Dynamic string array ────────────────────────────────────────────── */ typedef struct { char **items; size_t count; size_t capacity; int sorted; } strarray_t; strarray_t *strarray_new(void); void strarray_free(strarray_t *a); int strarray_add(strarray_t *a, const char *s); int strarray_contains(const strarray_t *a, const char *s); int strarray_remove(strarray_t *a, const char *s); void strarray_sort(strarray_t *a); /* ── Allocation — abort on OOM, never return NULL ───────────────────── */ void *xmalloc(size_t size); void *xrealloc(void *ptr, size_t size); char *xstrdup(const char *s); /* ── String utilities ────────────────────────────────────────────────── */ char *str_trim(char *s); /* in-place trim */ int str_starts_with(const char *s, const char *pfx); int str_ends_with(const char *s, const char *sfx); /* ── Path utilities ──────────────────────────────────────────────────── */ /* * path_join: allocate and return "dir/file". * Caller must free() the result. * Handles trailing slash on dir and leading slash on file. * Calls err(1,...) on OOM — never returns NULL. */ char *path_join(const char *dir, const char *file); /* * mkdirp: create directory and all parents, like mkdir -p. * Returns 0 on success, -1 on error (errno set). */ int mkdirp(const char *path, mode_t mode); /* * file_exists: return 1 if path exists (lstat), 0 otherwise. */ int file_exists(const char *path); /* * rmrf: remove a directory tree recursively. * More robust than system("rm -rf ..."). * Returns 0 on success, -1 on error. */ int rmrf(const char *path); /* ── Output ──────────────────────────────────────────────────────────── */ /* * Two output tiers: * * info(fmt, ...) — informational, prefix "mkpkg: " to stdout * warning(fmt, ...) — warnings, prefix "mkpkg: warning: " to stderr * die(fmt, ...) — fatal error, prefix "mkpkg: error: " to stderr, exit(1) * * The error() function is intentionally NOT exported — each source file * defines its own static error() to keep the prefix consistent. * Use die() when the error is immediately fatal. */ /* * Trust anchor for port signatures. `keytext` is a signify public key * as carried in a MAKEPKG signify() block ("untrusted comment: ..." * line + base64 line). If its base64 key equals the key of some * TRUSTED_KEYS_DIR/.pub, writes that file's path to `path` and * returns 0 — callers hand THAT file to `signify -p`, never the key * from MAKEPKG. Returns -1 when the key is absent or untrusted. */ int signify_trusted_pubkey(const char *keytext, char *path, size_t pathsz); void info(const char *fmt, ...) __attribute__((format(printf, 1, 2))); void warning(const char *fmt, ...) __attribute__((format(printf, 1, 2))); void die(const char *fmt, ...) __attribute__((format(printf, 1, 2), noreturn)); #endif /* UTIL_H */