summaryrefslogtreecommitdiff
path: root/util.h
blob: 7520b195303ea221ff6a6840b569101a1d17a55b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * 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 <stddef.h>
#include <sys/types.h>
#include <sys/stat.h>

/* ── 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/<name>.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 */