The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
internal.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file internal.h
5 * @brief the parts of librat that its source files share.
6 */
7
8#include "conf.h"
9#include "rat.h"
10
11#include <stdio.h>
12
13/** the architecture of this machine, as packages name it */
14#ifdef __x86_64__
15#define RAT_MACHINE_ARCH "x86_64"
16#else
17#define RAT_MACHINE_ARCH "i686"
18#endif
19
20/** the most members in a package */
21#define RAT_ENTRIES_MAX 65536
22
23/** the longest description and licence */
24#define RAT_TEXT_MAX 512
25
26/** the folder of the public keys that sign the index, relative to the root */
27#define RAT_KEYS_DIR "etc/rat/keys"
28
29/** the largest .RATINFO */
30#define RAT_RATINFO_MAX 65536
31
32/** the size of a name in the stage folder: 'f', 64 hex digits, '\0' */
33#define RAT_STAGE_NAME_MAX 66
34
35struct rat
36{
37 char *root; // the root folder without a '/' at the end ("" for "/")
38 int lock_fd; // -1 when not open for writing
40 bool pending; // a query handle saw an interrupted change
42};
43
44/**
45 * set the message of err (printf format).
46 * @return -1, so a caller can write "return rat_fail(err, ...)".
47 */
48int rat_fail(rat_error_t *err, const char *format, ...)
49 __attribute__((format(printf, 2, 3)));
50
51/** give a note to the caller (printf format); nothing when note is NULL */
52void rat_say(rat_note_t note, void *context, const char *format, ...)
53 __attribute__((format(printf, 3, 4)));
54
55/**
56 * a path on the root: <root>/<rel>.
57 * @return a string to free, or NULL (no memory).
58 */
59char *rat_path(const rat_t *rat, const char *rel);
60
61/**
62 * check a path of a package and make it the normal form: a "./" at the
63 * start and a '/' at the end are removed.
64 * @param out gets the normal form (RAT_PATH_MAX + 1 bytes).
65 * @return NULL if the path is good, else the rule that it breaks.
66 */
67const char *rat_check_path(const char *path, char out[RAT_PATH_MAX + 1]);
68
69/**
70 * check the target of a symbolic link at path: relative, and inside the
71 * package root when it is resolved from the folder of the link.
72 * @return NULL if the target is good, else the rule that it breaks.
73 */
74const char *rat_check_link(const char *path, const char *target);
75
76/** the SHA-256 as 64 hexadecimal digits and a '\0' */
77void rat_hex(const uint8_t sha[32], char out[65]);
78
79/** read 64 hexadecimal digits. @return 0, or -1 */
80int rat_unhex(const char *text, uint8_t sha[32]);
81
82/**
83 * the SHA-256 and size of a file.
84 * @return 0, or -1 with errno set.
85 */
86int rat_sha256_file(const char *path, uint8_t sha[32], uint64_t *size_out);
87
88/**
89 * read a whole file into memory, with a '\0' after it.
90 * @param max the largest size that is read.
91 * @return a buffer to free, or NULL with errno set (EFBIG past max).
92 */
93char *rat_read_file(const char *path, size_t max, size_t *size_out);
94
95/**
96 * write a file so that a crash leaves the old file or the new one: to
97 * <path>.new, fsync, then rename.
98 * @return 0, or -1 with errno set.
99 */
100int rat_write_file(const char *path, const void *data, size_t size, uint32_t mode);
101
102/**
103 * make a folder and the folders above it that are not there.
104 * @return 0, or -1 with errno set.
105 */
106int rat_mkdirs(const char *path, uint32_t mode);
107
108/**
109 * remove a file, a link, or a folder with everything in it. it never
110 * follows a symbolic link. a path that is not there is not an error.
111 * @return 0, or -1 with errno set.
112 */
113int rat_remove_tree(const char *path);
114
115/** a copy of a string, or NULL when there is no memory */
116char *rat_strdup(const char *s);
117
118/** release the members of an entry */
119void rat_entry_clear(rat_entry_t *entry);
120
121/**
122 * fill info from the text of .RATINFO and check its fields.
123 * @return 0, or -1.
124 */
125int rat_parse_info(const char *text, rat_info_t *info, rat_error_t *err);
126
127/**
128 * fill info from one section of a parsed file and check its fields.
129 * @param where the place for messages: ".RATINFO" or "index.conf [lua]".
130 * @return 0, or -1.
131 */
132int rat_info_from_conf(const conf_t *conf, const char *section, const char *where,
133 rat_info_t *info, rat_error_t *err);
134
135/** a version: 1 to RAT_VERSION_MAX characters of 0-9 a-z A-Z . + ~ _ */
136bool rat_version_ok(const char *version);
137
138/** a text field: at most RAT_TEXT_MAX bytes, and no control characters */
139bool rat_text_ok(const char *text);
140
141/**
142 * a whole number of decimal digits only, at most max.
143 * @return 0, or -1.
144 */
145int rat_parse_number(const char *text, uint64_t max, uint64_t *out);
146
147/** sort entries by path */
148void rat_sort_entries(rat_entry_t *entries, int count);
149
150/** compare two entries by path, for qsort and bsearch */
151int rat_compare_entries(const void *a, const void *b);
152
153/**
154 * read and check a package file, as rat_package_read. with a stage
155 * folder, also write each file and link there, under the name that
156 * rat_stage_name gives for its path.
157 * @param stage the stage folder (it must exist), or NULL.
158 */
159int rat_package_unpack(const char *path, const char *arch, const char *stage,
160 rat_package_t *package, rat_error_t *err);
161
162/** the name of a path in the stage folder: 'f' and the SHA-256 of the path */
163void rat_stage_name(const char *path, char out[RAT_STAGE_NAME_MAX]);
164
165/** a list of operations that is being made */
166typedef struct
167{
168 char *text; // the lines, each with a '\n'
169 size_t used;
170 size_t capacity;
171} rat_txn_t;
172
173void rat_txn_init(rat_txn_t *txn);
174void rat_txn_free(rat_txn_t *txn);
175
176/**
177 * add one operation (printf format, without the '\n').
178 * @return 0, or -1 (no memory).
179 */
180int rat_txn_add(rat_txn_t *txn, const char *format, ...) __attribute__((format(printf, 2, 3)));
181
182/**
183 * write the list and do it, then remove the list and the stage folder.
184 * @param note a line that tells what the change is ("install lua 5.4.9-1").
185 * @return 0, or -1. after -1 the list can still be there: the next
186 * rat_open for writing finishes it.
187 */
188int rat_txn_commit(rat_t *rat, rat_txn_t *txn, const char *note, rat_error_t *err);
189
190/** true if the list of an interrupted change is there */
191bool rat_txn_pending(const rat_t *rat);
192
193/**
194 * for the host tests only: when 0 or more, a transaction stops after
195 * this number of operations, as if the power went off. -1 normally.
196 */
197extern int rat_test_stop_after;
198
199/**
200 * read the record of the files of a package (installed/<name>/files).
201 * @return 0, or -1.
202 */
203int rat_read_files_record(const char *path, rat_entry_t **entries_out, int *count_out,
204 rat_error_t *err);
205
206/**
207 * the text of a files record.
208 * @return a string to free, or NULL (no memory).
209 */
210char *rat_files_record_text(const rat_entry_t *entries, int count);
211
212/**
213 * finish the transaction of an earlier rat that stopped, or remove its
214 * stage folder. rat->recovered gets a message when there was one.
215 * @return 0, or -1.
216 */
217int rat_recover(rat_t *rat, rat_error_t *err);
218
219/** the owners of all paths: the base image and the installed packages */
220typedef struct
221{
222 const char *path;
223 const char *owner; // the package name, or NULL for the base image
224 char type; // RAT_ENTRY_*, or 0 for a path of the base image
226
227typedef struct
228{
229 rat_owned_t *items; // sorted by path; a folder can have more than one owner
230 int count;
231 char **strings; // the memory of the paths and names
234
235/**
236 * read base.files and the files records of every installed package.
237 * @return 0, or -1.
238 */
240
241/** release the owners */
242void rat_owners_free(rat_owners_t *owners);
243
244/**
245 * the owners of a path.
246 * @param count_out gets how many there are (0 when none).
247 * @return the first of them, or NULL.
248 */
249const rat_owned_t *rat_owners_find(const rat_owners_t *owners, const char *path, int *count_out);
250
251/**
252 * true if a path of a package is a settings file: a file under etc/. a
253 * settings file that the person changed stays at an upgrade or a remove.
254 */
255bool rat_is_settings(const rat_entry_t *entry);
256
257/** the end of the name that a new settings file gets when the person changed the old one */
258#define RAT_NEW_SUFFIX ".ratnew"
259
260/** true if the base image, or a package other than name, owns path */
261bool rat_owned_by_other(const rat_owners_t *owners, const char *path, const char *name);
262
263/** what is on the disk at the path of a file or link of an installed package */
264typedef enum
265{
266 RAT_DISK_GONE, // nothing is there
267 RAT_DISK_SAME, // the file or link of the record
268 RAT_DISK_CHANGED, // other data, another link target, or another type
269 RAT_DISK_FOLDER, // a folder, which rat never removes as a file
270} rat_disk_t;
271
272/** compare a file or link of a record with the disk */
273rat_disk_t rat_disk_state(const rat_t *rat, const rat_entry_t *entry);
274
275/**
276 * check a detached signature: ECDSA P-256 over the SHA-256 of the data,
277 * in DER. one of the keys in <root>/etc/rat/keys must verify it.
278 * @return 0 when the signature is good, or -1.
279 */
280int rat_signature_check(const rat_t *rat, const void *data, size_t size, const void *signature,
281 size_t signature_size, rat_error_t *err);
282
283/**
284 * read the text of index.conf and check every field. the signature must
285 * be checked before.
286 * @param arch the architecture that the index must be for.
287 * @return 0, or -1.
288 */
289int rat_index_parse(const char *text, const char *arch, rat_index_t *index, rat_error_t *err);
290
291/** release the strings of one package of an index */
292void rat_remote_clear(rat_remote_t *remote);
293
294/**
295 * download a URL into a file. only https and file URLs are used, and a
296 * redirect must go to https. on a failure the file is removed.
297 * @param max_size the largest file that is accepted.
298 * @param progress gets the progress (may be NULL).
299 * @return 0, or -1.
300 */
301int rat_download(const char *url, const char *path, uint64_t max_size, rat_progress_t progress,
302 void *context, rat_error_t *err);
void rat_stage_name(const char *path, char out[RAT_STAGE_NAME_MAX])
the name of a path in the stage folder: 'f' and the SHA-256 of the path
Definition archive.c:608
int rat_package_unpack(const char *path, const char *arch, const char *stage, rat_package_t *package, rat_error_t *err)
read and check a package file, as rat_package_read.
Definition archive.c:519
settings files: sections of "key = value" lines.
struct conf conf_t
Definition conf.h:37
char * rat_files_record_text(const rat_entry_t *entries, int count)
the text of a files record.
Definition db.c:142
int rat_owners_load(rat_t *rat, rat_owners_t *owners, rat_error_t *err)
read base.files and the files records of every installed package.
Definition db.c:291
void rat_owners_free(rat_owners_t *owners)
release the owners
Definition db.c:328
bool rat_owned_by_other(const rat_owners_t *owners, const char *path, const char *name)
true if the base image, or a package other than name, owns path
Definition db.c:364
int rat_read_files_record(const char *path, rat_entry_t **entries_out, int *count_out, rat_error_t *err)
read the record of the files of a package (installed/<name>/files).
Definition db.c:94
const rat_owned_t * rat_owners_find(const rat_owners_t *owners, const char *path, int *count_out)
the owners of a path.
Definition db.c:339
int rat_download(const char *url, const char *path, uint64_t max_size, rat_progress_t progress, void *context, rat_error_t *err)
download a URL into a file.
Definition download.c:69
int rat_index_parse(const char *text, const char *arch, rat_index_t *index, rat_error_t *err)
read the text of index.conf and check every field.
Definition index.c:136
void rat_remote_clear(rat_remote_t *remote)
release the strings of one package of an index
Definition index.c:205
int rat_parse_number(const char *text, uint64_t max, uint64_t *out)
a whole number of decimal digits only, at most max.
Definition info.c:70
bool rat_text_ok(const char *text)
a text field: at most RAT_TEXT_MAX bytes, and no control characters
Definition info.c:53
bool rat_version_ok(const char *version)
a version: 1 to RAT_VERSION_MAX characters of 0-9 a-z A-Z .
Definition info.c:34
int rat_parse_info(const char *text, rat_info_t *info, rat_error_t *err)
fill info from the text of .RATINFO and check its fields.
Definition info.c:168
int rat_info_from_conf(const conf_t *conf, const char *section, const char *where, rat_info_t *info, rat_error_t *err)
fill info from one section of a parsed file and check its fields.
Definition info.c:121
bool rat_txn_pending(const rat_t *rat)
true if the list of an interrupted change is there
Definition txn.c:414
int rat_compare_entries(const void *a, const void *b)
compare two entries by path, for qsort and bsearch
Definition util.c:482
void rat_sort_entries(rat_entry_t *entries, int count)
sort entries by path
Definition util.c:487
rat_disk_t rat_disk_state(const rat_t *rat, const rat_entry_t *entry)
compare a file or link of a record with the disk
Definition remove.c:21
void rat_txn_free(rat_txn_t *txn)
Definition txn.c:43
int rat_mkdirs(const char *path, uint32_t mode)
make a folder and the folders above it that are not there.
Definition util.c:385
void rat_hex(const uint8_t sha[32], char out[65])
the SHA-256 as 64 hexadecimal digits and a '\0'
Definition util.c:184
int rat_test_stop_after
for the host tests only: when 0 or more, a transaction stops after this number of operations,...
Definition txn.c:36
int rat_write_file(const char *path, const void *data, size_t size, uint32_t mode)
write a file so that a crash leaves the old file or the new one: to <path>.new, fsync,...
Definition util.c:342
char * rat_strdup(const char *s)
a copy of a string, or NULL when there is no memory
Definition util.c:46
int void rat_say(rat_note_t note, void *context, const char *format,...) __attribute__((format(printf
give a note to the caller (printf format); nothing when note is NULL
int rat_unhex(const char *text, uint8_t sha[32])
read 64 hexadecimal digits.
Definition util.c:209
bool rat_is_settings(const rat_entry_t *entry)
true if a path of a package is a settings file: a file under etc/.
Definition util.c:495
const char * rat_check_path(const char *path, char out[RAT_PATH_MAX+1])
check a path of a package and make it the normal form: a "./" at the start and a '/' at the end are r...
Definition util.c:74
int rat_sha256_file(const char *path, uint8_t sha[32], uint64_t *size_out)
the SHA-256 and size of a file.
Definition util.c:224
const char * rat_check_link(const char *path, const char *target)
check the target of a symbolic link at path: relative, and inside the package root when it is resolve...
Definition util.c:130
int rat_remove_tree(const char *path)
remove a file, a link, or a folder with everything in it.
Definition util.c:418
int rat_signature_check(const rat_t *rat, const void *data, size_t size, const void *signature, size_t signature_size, rat_error_t *err)
check a detached signature: ECDSA P-256 over the SHA-256 of the data, in DER.
Definition signature.c:63
int rat_recover(rat_t *rat, rat_error_t *err)
finish the transaction of an earlier rat that stopped, or remove its stage folder.
Definition txn.c:357
void rat_entry_clear(rat_entry_t *entry)
release the members of an entry
Definition util.c:465
void rat_txn_init(rat_txn_t *txn)
Definition txn.c:38
rat_disk_t
what is on the disk at the path of a file or link of an installed package
Definition internal.h:265
@ RAT_DISK_SAME
Definition internal.h:267
@ RAT_DISK_CHANGED
Definition internal.h:268
@ RAT_DISK_GONE
Definition internal.h:266
@ RAT_DISK_FOLDER
Definition internal.h:269
int int rat_txn_commit(rat_t *rat, rat_txn_t *txn, const char *note, rat_error_t *err)
write the list and do it, then remove the list and the stage folder.
Definition txn.c:317
int rat_fail(rat_error_t *err, const char *format,...) __attribute__((format(printf
set the message of err (printf format).
char * rat_read_file(const char *path, size_t max, size_t *size_out)
read a whole file into memory, with a '\0' after it.
Definition util.c:261
int rat_txn_add(rat_txn_t *txn, const char *format,...) __attribute__((format(printf
add one operation (printf format, without the ' ').
int void char * rat_path(const rat_t *rat, const char *rel)
a path on the root: <root>/<rel>.
Definition util.c:57
#define RAT_STAGE_NAME_MAX
the size of a name in the stage folder: 'f', 64 hex digits, '\0'
Definition internal.h:33
unsigned int uint32_t
Definition fb.h:17
unsigned char uint8_t
Definition fb.h:18
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
buffered streams and formatted input/output.
int printf(const char *fmt,...) __attribute__((format(printf
formatted output.
librat: the package manager of ginnOS.
#define RAT_ERROR_MAX
Definition rat.h:32
#define RAT_PATH_MAX
Definition rat.h:31
void(*) rat_progress_t(void *context, const char *file, uint64_t done, uint64_t total)
a function that gets the progress of a download.
Definition rat.h:130
void(*) rat_note_t(void *context, const char *text)
a function that gets the notes of a change (one line, without ' ')
Definition rat.h:123
struct rat rat_t
an open root folder
Definition rat.h:120
ELF32 file header.
Definition elf.h:51
a folder, file or symbolic link of a package
Definition rat.h:53
a message about a failure
Definition rat.h:39
the index of the repository, after its signature was checked
Definition rat.h:95
the fields of .RATINFO
Definition rat.h:64
the owners of all paths: the base image and the installed packages
Definition internal.h:221
const char * owner
Definition internal.h:223
char type
Definition internal.h:224
const char * path
Definition internal.h:222
Definition internal.h:228
rat_owned_t * items
Definition internal.h:229
char ** strings
Definition internal.h:231
int count
Definition internal.h:230
int string_count
Definition internal.h:232
a package file that was read and checked
Definition rat.h:77
a package of the index of the repository
Definition rat.h:86
a list of operations that is being made
Definition internal.h:167
char * text
Definition internal.h:168
size_t capacity
Definition internal.h:170
size_t used
Definition internal.h:169
Definition internal.h:36
char * root
Definition internal.h:37
int lock_fd
Definition internal.h:38
bool writable
Definition internal.h:39
bool pending
Definition internal.h:40
char recovered[RAT_ERROR_MAX]
Definition internal.h:41