The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
rat.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file rat.h
5 * @brief librat: the package manager of ginnOS. the rat command and other
6 * programs install, remove and inspect packages through it.
7 *
8 * a package is a ustar archive, compressed with gzip, with the name
9 * <name>-<version>-<release>-<arch>.rat. its first member is .RATINFO (a
10 * libconf file with a [package] section); the other members are folders,
11 * files and symbolic links, with paths relative to the root folder.
12 *
13 * every call works on a root folder: "/" on a running system, or another
14 * folder (a test, or an image that is not running). the database is in
15 * <root>/var/lib/rat. a change (install, remove) is a transaction: rat
16 * checks everything and unpacks into a stage folder first, then writes a
17 * list of operations and does them. if the change stops (a power cut),
18 * the next rat_open for writing does the rest of the list.
19 *
20 * a function that can fail returns 0 or -1. on -1, the rat_error_t has a
21 * message for a person, without "rat:" at its start.
22 */
23
24#include <stdbool.h>
25#include <stddef.h>
26#include <stdint.h>
27
28#define RAT_NAME_MAX 32 // the longest package name
29#define RAT_VERSION_MAX 32 // the longest version
30#define RAT_ARCH_MAX 7 // "i686", "x86_64" or "any"
31#define RAT_PATH_MAX 255 // the longest path in a package
32#define RAT_ERROR_MAX 512
33
34/** the database folder, relative to the root */
35#define RAT_DB_DIR "var/lib/rat"
36
37/** a message about a failure */
38typedef struct
39{
42
43/** the types of the members of a package */
44typedef enum
45{
50
51/** a folder, file or symbolic link of a package */
52typedef struct
53{
54 char *path; // relative to the root, without a '/' at the start or end
55 char type; // a rat_entry_type_t
56 uint32_t mode; // the permission bits (0 to 0777)
57 uint64_t size; // RAT_ENTRY_FILE: the size in bytes
58 uint8_t sha256[32]; // RAT_ENTRY_FILE: the SHA-256 of the data
59 char *target; // RAT_ENTRY_LINK: the target of the link
61
62/** the fields of .RATINFO */
63typedef struct
64{
65 char name[RAT_NAME_MAX + 1];
67 long release;
68 char arch[RAT_ARCH_MAX + 1];
70 char *licence;
71 uint64_t installed_size; // bytes, the sum of the file sizes at most
72 char *installed; // an installed package: the UTC time of the install
74
75/** a package file that was read and checked */
76typedef struct
77{
79 rat_entry_t *entries; // sorted by path
80 int count;
81 char *ratinfo; // the text of .RATINFO
83
84/** a package of the index of the repository */
85typedef struct
86{
87 rat_info_t info; // installed is NULL
88 char *file; // the file name: <name>-<version>-<release>-<arch>.rat
89 uint64_t size; // the size of the file in bytes
90 uint8_t sha256[32]; // the SHA-256 of the file
92
93/** the index of the repository, after its signature was checked */
94typedef struct
95{
96 rat_remote_t *packages; // sorted by name
97 int count;
98 uint64_t sequence; // a larger number for each new index
99 char generated[11]; // the day the index was made: "YYYY-MM-DD"
100 int64_t generated_days; // the same day, as days from 1970-01-01
102
103/** what rat_update did */
104typedef struct
105{
106 uint64_t old_sequence; // the sequence of the index before, or 0
107 uint64_t sequence; // the sequence of the new index
108 int count; // the packages in the new index
109 char generated[11]; // the day the new index was made
111
112/** an installed package that the index has in a newer version */
113typedef struct
114{
115 rat_info_t installed; // the installed version
116 const rat_remote_t *remote; // the newer version, in the index
118
119/** an open root folder */
120typedef struct rat rat_t;
121
122/** a function that gets the notes of a change (one line, without '\n') */
123typedef void (*rat_note_t)(void *context, const char *text);
124
125/**
126 * a function that gets the progress of a download.
127 * @param file the name of the file.
128 * @param total the size in bytes, or 0 while it is not known.
129 */
130typedef void (*rat_progress_t)(void *context, const char *file, uint64_t done, uint64_t total);
131
132/** rat_install_file: replace files that exist and that no package owns */
133#define RAT_FORCE 0x1
134
135/** rat_install_file: replace an installed package with an older version */
136#define RAT_DOWNGRADE 0x2
137
138/**
139 * open a root folder.
140 * @param root the root folder; "/" on a running system.
141 * @param write true to change packages: takes the lock (one writer at a
142 * time), and finishes or removes an interrupted change. false for
143 * queries: no lock.
144 * @return the handle, or NULL (err has the reason).
145 */
146rat_t *rat_open(const char *root, bool write, rat_error_t *err);
147
148/** close the handle and release the lock. */
149void rat_close(rat_t *rat);
150
151/**
152 * what rat_open did with an interrupted change.
153 * @return a message, or NULL if there was none.
154 */
155const char *rat_recovered(const rat_t *rat);
156
157/**
158 * true if a handle opened for queries saw an interrupted change, which
159 * the next rat_open for writing finishes. the queries can then show a
160 * state between the old one and the new one.
161 */
162bool rat_pending(const rat_t *rat);
163
164/**
165 * read and check a package file. nothing is written.
166 * @param arch the architecture the package must have ("any" also
167 * matches), or NULL for every architecture.
168 * @return 0, or -1 if the file is not a correct package.
169 */
170int rat_package_read(const char *path, const char *arch, rat_package_t *package,
171 rat_error_t *err);
172
173/** release the memory of a package (not the struct itself). */
174void rat_package_free(rat_package_t *package);
175
176/**
177 * install a package file. when an older version of the package is
178 * installed, upgrade it: the files of the old version that the new one
179 * does not have go, in the same change. the same version is refused.
180 * @param flags RAT_FORCE, RAT_DOWNGRADE (accept an older version than
181 * the installed one), or 0.
182 * @param note gets a line for each thing the person must know (may be NULL).
183 * @return 0, or -1 with nothing changed.
184 */
185int rat_install_file(rat_t *rat, const char *path, unsigned flags, rat_note_t note,
186 void *context, rat_error_t *err);
187
188/**
189 * remove an installed package: its files, its links, and its folders that
190 * are then empty and that the base or another package does not use.
191 * @param note gets a line for each file that was changed after the install.
192 * @return 0, or -1 with nothing changed.
193 */
194int rat_remove(rat_t *rat, const char *name, rat_note_t note, void *context,
195 rat_error_t *err);
196
197/**
198 * the installed packages, sorted by name.
199 * @param infos_out gets an array to free with rat_infos_free.
200 * @return 0, or -1.
201 */
202int rat_list(rat_t *rat, rat_info_t **infos_out, int *count_out, rat_error_t *err);
203
204/**
205 * the fields of an installed package.
206 * @return 0, or -1 (also when it is not installed).
207 */
208int rat_info(rat_t *rat, const char *name, rat_info_t *info_out, rat_error_t *err);
209
210/**
211 * the folders, files and links of an installed package, sorted by path.
212 * @param entries_out gets an array to free with rat_entries_free.
213 * @return 0, or -1.
214 */
215int rat_files(rat_t *rat, const char *name, rat_entry_t **entries_out, int *count_out,
216 rat_error_t *err);
217
218/**
219 * which package owns a path.
220 * @param path an absolute path on the root, as "/usr/bin/lua".
221 * @param name_out gets the package name, or "" when the base image owns
222 * the path.
223 * @return 0, or -1 when nothing owns it.
224 */
225int rat_owner(rat_t *rat, const char *path, char name_out[RAT_NAME_MAX + 1], rat_error_t *err);
226
227/**
228 * compare the files of an installed package with the record: a missing
229 * file, another type, other permissions, other data.
230 * @param note gets one line for each difference.
231 * @param problems_out gets the number of differences.
232 * @return 0, or -1 (the package is not installed).
233 */
234int rat_verify(rat_t *rat, const char *name, rat_note_t note, void *context, int *problems_out,
235 rat_error_t *err);
236
237/**
238 * true if the root folder is the root image in RAM (from /proc/mounts):
239 * a change is then lost at the next boot. always false for a root other
240 * than "/".
241 */
242bool rat_root_in_ram(const rat_t *rat);
243
244/** release the strings of an info (not the struct itself). */
245void rat_info_free(rat_info_t *info);
246
247/** release an array from rat_list. */
248void rat_infos_free(rat_info_t *infos, int count);
249
250/** release an array from rat_files. */
251void rat_entries_free(rat_entry_t *entries, int count);
252
253/**
254 * check a package name: 1 to RAT_NAME_MAX characters of a-z, 0-9, '-',
255 * '+' and '.', the first a letter or a digit.
256 */
257bool rat_name_ok(const char *name);
258
259/**
260 * compare two versions of a package: first the versions, with the rules
261 * of Debian (digit runs as numbers, '~' before everything), then the
262 * releases.
263 * @return -1 when a is older than b, 0 when they are the same, 1 when a
264 * is newer.
265 */
266int rat_version_compare(const char *version_a, long release_a, const char *version_b,
267 long release_b);
268
269/** release the memory of an index (not the struct itself). */
270void rat_index_free(rat_index_t *index);
271
272/**
273 * a package of the index by name.
274 * @return the package, or NULL if the index does not have it.
275 */
276const rat_remote_t *rat_index_find(const rat_index_t *index, const char *name);
277
278/**
279 * true if word is in the name or the description of the package, without
280 * a difference between capitals and small letters.
281 */
282bool rat_remote_matches(const rat_remote_t *remote, const char *word);
283
284/**
285 * the names in the index that are near a name that it does not have, for
286 * a "did you mean" message: one or two letters different, or a name that
287 * contains it.
288 * @param names_out gets pointers into the index.
289 * @return the number of names, at most max.
290 */
291int rat_index_similar(const rat_index_t *index, const char *name, const char **names_out,
292 int max);
293
294/**
295 * get the index of the repository (the url for this architecture in
296 * <root>/etc/rat/repos.conf) and its signature, check them, and keep
297 * them. an index with a bad signature, or older than the one rat has, is
298 * refused and the old one stays.
299 * @param progress gets the progress of the download (may be NULL).
300 * @return 0, or -1.
301 */
302int rat_update(rat_t *rat, rat_progress_t progress, void *context, rat_update_t *result_out,
303 rat_error_t *err);
304
305/**
306 * read the index that rat_update kept, and check its signature again.
307 * @param index gets the index; free it with rat_index_free.
308 * @return 0, or -1 (also when there is no index yet).
309 */
311
312/**
313 * install or upgrade a package of the index: download it into the
314 * cache, check its size and SHA-256 against the index, then install it as
315 * rat_install_file does. the file leaves the cache after the install.
316 * a package that has the version of the index is refused before the
317 * download.
318 * @param flags RAT_FORCE, RAT_DOWNGRADE, or 0.
319 * @param note gets the notes of the install (may be NULL).
320 * @param progress gets the progress of the download (may be NULL).
321 * @param context goes to note and to progress.
322 * @return 0, or -1 with nothing changed.
323 */
324int rat_install_name(rat_t *rat, const char *name, unsigned flags, rat_note_t note,
325 rat_progress_t progress, void *context, rat_error_t *err);
326
327/**
328 * the installed packages that the index has in a newer version, sorted
329 * by name. a package that is not in the index is not in the list.
330 * @param items_out gets an array to free with rat_upgradables_free; its
331 * remote pointers point into the index, so free it first.
332 * @return 0, or -1.
333 */
334int rat_upgradable(rat_t *rat, const rat_index_t *index, rat_upgradable_t **items_out,
335 int *count_out, rat_error_t *err);
336
337/** release an array from rat_upgradable. */
338void rat_upgradables_free(rat_upgradable_t *items, int count);
339
340/**
341 * remove the downloaded files of the cache.
342 * @param freed_out gets the bytes that were freed.
343 * @return 0, or -1.
344 */
345int rat_clean(rat_t *rat, uint64_t *freed_out, rat_error_t *err);
void rat_package_free(rat_package_t *package)
release the memory of a package (not the struct itself).
Definition archive.c:600
int rat_package_read(const char *path, const char *arch, rat_package_t *package, rat_error_t *err)
read and check a package file.
Definition archive.c:594
uint8_t flags
Definition idt.c:3
unsigned int uint32_t
Definition fb.h:17
unsigned char uint8_t
Definition fb.h:18
NULL, size_t, ptrdiff_t, offsetof.
integer types of fixed width.
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__INT64_TYPE__ int64_t
Definition stdint.h:16
#define RAT_ERROR_MAX
Definition rat.h:32
int rat_update(rat_t *rat, rat_progress_t progress, void *context, rat_update_t *result_out, rat_error_t *err)
get the index of the repository (the url for this architecture in <root>/etc/rat/repos....
Definition repo.c:219
void rat_info_free(rat_info_t *info)
release the strings of an info (not the struct itself).
Definition info.c:181
void rat_infos_free(rat_info_t *infos, int count)
release an array from rat_list.
Definition info.c:191
void rat_close(rat_t *rat)
close the handle and release the lock.
Definition open.c:81
int rat_install_name(rat_t *rat, const char *name, unsigned flags, rat_note_t note, rat_progress_t progress, void *context, rat_error_t *err)
install or upgrade a package of the index: download it into the cache, check its size and SHA-256 aga...
Definition repo.c:377
#define RAT_ARCH_MAX
Definition rat.h:30
const char * rat_recovered(const rat_t *rat)
what rat_open did with an interrupted change.
Definition open.c:95
bool rat_name_ok(const char *name)
check a package name: 1 to RAT_NAME_MAX characters of a-z, 0-9, '-', '+' and '.
Definition info.c:15
int rat_upgradable(rat_t *rat, const rat_index_t *index, rat_upgradable_t **items_out, int *count_out, rat_error_t *err)
the installed packages that the index has in a newer version, sorted by name.
Definition repo.c:458
int rat_clean(rat_t *rat, uint64_t *freed_out, rat_error_t *err)
remove the downloaded files of the cache.
Definition repo.c:504
int rat_install_file(rat_t *rat, const char *path, unsigned flags, rat_note_t note, void *context, rat_error_t *err)
install a package file.
Definition install.c:421
bool rat_pending(const rat_t *rat)
true if a handle opened for queries saw an interrupted change, which the next rat_open for writing fi...
Definition open.c:100
int rat_owner(rat_t *rat, const char *path, char name_out[RAT_NAME_MAX+1], rat_error_t *err)
which package owns a path.
Definition query.c:144
int rat_files(rat_t *rat, const char *name, rat_entry_t **entries_out, int *count_out, rat_error_t *err)
the folders, files and links of an installed package, sorted by path.
Definition query.c:54
const rat_remote_t * rat_index_find(const rat_index_t *index, const char *name)
a package of the index by name.
Definition index.c:223
int rat_info(rat_t *rat, const char *name, rat_info_t *info_out, rat_error_t *err)
the fields of an installed package.
Definition query.c:24
int rat_list(rat_t *rat, rat_info_t **infos_out, int *count_out, rat_error_t *err)
the installed packages, sorted by name.
Definition query.c:80
int rat_index_load(rat_t *rat, rat_index_t *index, rat_error_t *err)
read the index that rat_update kept, and check its signature again.
Definition repo.c:138
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
bool rat_root_in_ram(const rat_t *rat)
true if the root folder is the root image in RAM (from /proc/mounts): a change is then lost at the ne...
Definition open.c:105
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
int rat_verify(rat_t *rat, const char *name, rat_note_t note, void *context, int *problems_out, rat_error_t *err)
compare the files of an installed package with the record: a missing file, another type,...
Definition query.c:239
#define RAT_VERSION_MAX
Definition rat.h:29
struct rat rat_t
an open root folder
Definition rat.h:120
int rat_version_compare(const char *version_a, long release_a, const char *version_b, long release_b)
compare two versions of a package: first the versions, with the rules of Debian (digit runs as number...
Definition version.c:89
void rat_index_free(rat_index_t *index)
release the memory of an index (not the struct itself).
Definition index.c:212
rat_entry_type_t
the types of the members of a package
Definition rat.h:45
@ RAT_ENTRY_LINK
Definition rat.h:48
@ RAT_ENTRY_DIR
Definition rat.h:47
@ RAT_ENTRY_FILE
Definition rat.h:46
void rat_upgradables_free(rat_upgradable_t *items, int count)
release an array from rat_upgradable.
Definition repo.c:495
#define RAT_NAME_MAX
Definition rat.h:28
int rat_remove(rat_t *rat, const char *name, rat_note_t note, void *context, rat_error_t *err)
remove an installed package: its files, its links, and its folders that are then empty and that the b...
Definition remove.c:97
int rat_index_similar(const rat_index_t *index, const char *name, const char **names_out, int max)
the names in the index that are near a name that it does not have, for a "did you mean" message: one ...
Definition index.c:304
void rat_entries_free(rat_entry_t *entries, int count)
release an array from rat_files.
Definition util.c:473
rat_t * rat_open(const char *root, bool write, rat_error_t *err)
open a root folder.
Definition open.c:16
bool rat_remote_matches(const rat_remote_t *remote, const char *word)
true if word is in the name or the description of the package, without a difference between capitals ...
Definition index.c:266
bool, true and false.
an open-file description.
Definition file.h:64
a folder, file or symbolic link of a package
Definition rat.h:53
char * target
Definition rat.h:59
uint32_t mode
Definition rat.h:56
uint64_t size
Definition rat.h:57
char * path
Definition rat.h:54
uint8_t sha256[32]
Definition rat.h:58
char type
Definition rat.h:55
a message about a failure
Definition rat.h:39
char message[RAT_ERROR_MAX]
Definition rat.h:40
the index of the repository, after its signature was checked
Definition rat.h:95
char generated[11]
Definition rat.h:99
rat_remote_t * packages
Definition rat.h:96
int64_t generated_days
Definition rat.h:100
int count
Definition rat.h:97
uint64_t sequence
Definition rat.h:98
the fields of .RATINFO
Definition rat.h:64
char arch[RAT_ARCH_MAX+1]
Definition rat.h:68
long release
Definition rat.h:67
char * licence
Definition rat.h:70
char version[RAT_VERSION_MAX+1]
Definition rat.h:66
char name[RAT_NAME_MAX+1]
Definition rat.h:65
char * description
Definition rat.h:69
char * installed
Definition rat.h:72
uint64_t installed_size
Definition rat.h:71
a package file that was read and checked
Definition rat.h:77
rat_entry_t * entries
Definition rat.h:79
int count
Definition rat.h:80
rat_info_t info
Definition rat.h:78
char * ratinfo
Definition rat.h:81
a package of the index of the repository
Definition rat.h:86
rat_info_t info
Definition rat.h:87
uint8_t sha256[32]
Definition rat.h:90
uint64_t size
Definition rat.h:89
char * file
Definition rat.h:88
what rat_update did
Definition rat.h:105
uint64_t old_sequence
Definition rat.h:106
uint64_t sequence
Definition rat.h:107
int count
Definition rat.h:108
char generated[11]
Definition rat.h:109
an installed package that the index has in a newer version
Definition rat.h:114
rat_info_t installed
Definition rat.h:115
const rat_remote_t * remote
Definition rat.h:116
Definition internal.h:36
char * root
Definition internal.h:37
ssize_t write(int fd, const void *buf, size_t count)
write bytes to a file descriptor.