The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
rat.h File Reference

librat: the package manager of ginnOS. More...

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

Go to the source code of this file.

Data Structures

struct  rat_error_t
 a message about a failure More...
struct  rat_entry_t
 a folder, file or symbolic link of a package More...
struct  rat_info_t
 the fields of .RATINFO More...
struct  rat_package_t
 a package file that was read and checked More...
struct  rat_remote_t
 a package of the index of the repository More...
struct  rat_index_t
 the index of the repository, after its signature was checked More...
struct  rat_update_t
 what rat_update did More...
struct  rat_upgradable_t
 an installed package that the index has in a newer version More...

Macros

#define RAT_NAME_MAX   32
#define RAT_VERSION_MAX   32
#define RAT_ARCH_MAX   7
#define RAT_PATH_MAX   255
#define RAT_ERROR_MAX   512
#define RAT_DB_DIR   "var/lib/rat"
 the database folder, relative to the root
#define RAT_FORCE   0x1
 rat_install_file: replace files that exist and that no package owns
#define RAT_DOWNGRADE   0x2
 rat_install_file: replace an installed package with an older version

Typedefs

typedef struct rat rat_t
 an open root folder
typedef void(*) rat_note_t(void *context, const char *text)
 a function that gets the notes of a change (one line, without '
')
typedef void(*) rat_progress_t(void *context, const char *file, uint64_t done, uint64_t total)
 a function that gets the progress of a download.

Enumerations

enum  rat_entry_type_t { RAT_ENTRY_FILE = 'f' , RAT_ENTRY_DIR = 'd' , RAT_ENTRY_LINK = 'l' }
 the types of the members of a package More...

Functions

rat_t * rat_open (const char *root, bool write, rat_error_t *err)
 open a root folder.
void rat_close (rat_t *rat)
 close the handle and release the lock.
const char * rat_recovered (const rat_t *rat)
 what rat_open did with an interrupted change.
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 finishes.
int rat_package_read (const char *path, const char *arch, rat_package_t *package, rat_error_t *err)
 read and check a package file.
void rat_package_free (rat_package_t *package)
 release the memory of a package (not the struct itself).
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.
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 base or another package does not use.
int rat_list (rat_t *rat, rat_info_t **infos_out, int *count_out, rat_error_t *err)
 the installed packages, sorted by name.
int rat_info (rat_t *rat, const char *name, rat_info_t *info_out, rat_error_t *err)
 the fields of an installed package.
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.
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.
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, other permissions, other data.
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 next boot.
void rat_info_free (rat_info_t *info)
 release the strings of an info (not the struct itself).
void rat_infos_free (rat_info_t *infos, int count)
 release an array from rat_list.
void rat_entries_free (rat_entry_t *entries, int count)
 release an array from rat_files.
bool rat_name_ok (const char *name)
 check a package name: 1 to RAT_NAME_MAX characters of a-z, 0-9, '-', '+' and '.
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 numbers, '~' before everything), then the releases.
void rat_index_free (rat_index_t *index)
 release the memory of an index (not the struct itself).
const rat_remote_t * rat_index_find (const rat_index_t *index, const char *name)
 a package of the index by name.
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 and small letters.
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 or two letters different, or a name that contains it.
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.conf) and its signature, check them, and keep them.
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.
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 against the index, then install it as rat_install_file does.
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.
void rat_upgradables_free (rat_upgradable_t *items, int count)
 release an array from rat_upgradable.
int rat_clean (rat_t *rat, uint64_t *freed_out, rat_error_t *err)
 remove the downloaded files of the cache.

Detailed Description

librat: the package manager of ginnOS.

the rat command and other programs install, remove and inspect packages through it.

a package is a ustar archive, compressed with gzip, with the name <name>-<version>-<release>-<arch>.rat. its first member is .RATINFO (a libconf file with a [package] section); the other members are folders, files and symbolic links, with paths relative to the root folder.

every call works on a root folder: "/" on a running system, or another folder (a test, or an image that is not running). the database is in <root>/var/lib/rat. a change (install, remove) is a transaction: rat checks everything and unpacks into a stage folder first, then writes a list of operations and does them. if the change stops (a power cut), the next rat_open for writing does the rest of the list.

a function that can fail returns 0 or -1. on -1, the rat_error_t has a message for a person, without "rat:" at its start.

Macro Definition Documentation

◆ RAT_NAME_MAX

#define RAT_NAME_MAX   32

◆ RAT_VERSION_MAX

#define RAT_VERSION_MAX   32

◆ RAT_ARCH_MAX

#define RAT_ARCH_MAX   7

◆ RAT_PATH_MAX

#define RAT_PATH_MAX   255

◆ RAT_ERROR_MAX

#define RAT_ERROR_MAX   512

◆ RAT_DB_DIR

#define RAT_DB_DIR   "var/lib/rat"

the database folder, relative to the root

◆ RAT_FORCE

#define RAT_FORCE   0x1

rat_install_file: replace files that exist and that no package owns

◆ RAT_DOWNGRADE

#define RAT_DOWNGRADE   0x2

rat_install_file: replace an installed package with an older version

Typedef Documentation

◆ rat_t

typedef struct rat rat_t

an open root folder

◆ rat_note_t

typedef void(*) rat_note_t(void *context, const char *text)

a function that gets the notes of a change (one line, without '
')

◆ rat_progress_t

typedef void(*) rat_progress_t(void *context, const char *file, uint64_t done, uint64_t total)

a function that gets the progress of a download.

Parameters
filethe name of the file.
totalthe size in bytes, or 0 while it is not known.

Enumeration Type Documentation

◆ rat_entry_type_t

the types of the members of a package

Enumerator
RAT_ENTRY_FILE 
RAT_ENTRY_DIR 
RAT_ENTRY_LINK 

Function Documentation

◆ rat_open()

rat_t * rat_open ( const char * root,
bool write,
rat_error_t * err )

open a root folder.

Parameters
rootthe root folder; "/" on a running system.
writetrue to change packages: takes the lock (one writer at a time), and finishes or removes an interrupted change. false for queries: no lock.
Returns
the handle, or NULL (err has the reason).

◆ rat_close()

void rat_close ( rat_t * rat)

close the handle and release the lock.

◆ rat_recovered()

const char * rat_recovered ( const rat_t * rat)

what rat_open did with an interrupted change.

Returns
a message, or NULL if there was none.

◆ rat_pending()

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 finishes.

the queries can then show a state between the old one and the new one.

◆ rat_package_read()

int rat_package_read ( const char * path,
const char * arch,
rat_package_t * package,
rat_error_t * err )

read and check a package file.

nothing is written.

Parameters
archthe architecture the package must have ("any" also matches), or NULL for every architecture.
Returns
0, or -1 if the file is not a correct package.

◆ rat_package_free()

void rat_package_free ( rat_package_t * package)

release the memory of a package (not the struct itself).

◆ rat_install_file()

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.

when an older version of the package is installed, upgrade it: the files of the old version that the new one does not have go, in the same change. the same version is refused.

Parameters
flagsRAT_FORCE, RAT_DOWNGRADE (accept an older version than the installed one), or 0.
notegets a line for each thing the person must know (may be NULL).
Returns
0, or -1 with nothing changed.

◆ rat_remove()

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 base or another package does not use.

Parameters
notegets a line for each file that was changed after the install.
Returns
0, or -1 with nothing changed.

◆ rat_list()

int rat_list ( rat_t * rat,
rat_info_t ** infos_out,
int * count_out,
rat_error_t * err )

the installed packages, sorted by name.

Parameters
infos_outgets an array to free with rat_infos_free.
Returns
0, or -1.

◆ rat_info()

int rat_info ( rat_t * rat,
const char * name,
rat_info_t * info_out,
rat_error_t * err )

the fields of an installed package.

Returns
0, or -1 (also when it is not installed).

◆ rat_files()

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.

Parameters
entries_outgets an array to free with rat_entries_free.
Returns
0, or -1.

◆ rat_owner()

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.

Parameters
pathan absolute path on the root, as "/usr/bin/lua".
name_outgets the package name, or "" when the base image owns the path.
Returns
0, or -1 when nothing owns it.

◆ rat_verify()

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, other permissions, other data.

Parameters
notegets one line for each difference.
problems_outgets the number of differences.
Returns
0, or -1 (the package is not installed).

◆ rat_root_in_ram()

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 next boot.

always false for a root other than "/".

◆ rat_info_free()

void rat_info_free ( rat_info_t * info)

release the strings of an info (not the struct itself).

◆ rat_infos_free()

void rat_infos_free ( rat_info_t * infos,
int count )

release an array from rat_list.

◆ rat_entries_free()

void rat_entries_free ( rat_entry_t * entries,
int count )

release an array from rat_files.

◆ rat_name_ok()

bool rat_name_ok ( const char * name)

check a package name: 1 to RAT_NAME_MAX characters of a-z, 0-9, '-', '+' and '.

', the first a letter or a digit.

◆ rat_version_compare()

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 numbers, '~' before everything), then the releases.

Returns
-1 when a is older than b, 0 when they are the same, 1 when a is newer.

◆ rat_index_free()

void rat_index_free ( rat_index_t * index)

release the memory of an index (not the struct itself).

◆ rat_index_find()

const rat_remote_t * rat_index_find ( const rat_index_t * index,
const char * name )

a package of the index by name.

Returns
the package, or NULL if the index does not have it.

◆ rat_remote_matches()

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 and small letters.

◆ rat_index_similar()

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 or two letters different, or a name that contains it.

Parameters
names_outgets pointers into the index.
Returns
the number of names, at most max.

◆ rat_update()

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.conf) and its signature, check them, and keep them.

an index with a bad signature, or older than the one rat has, is refused and the old one stays.

Parameters
progressgets the progress of the download (may be NULL).
Returns
0, or -1.

◆ rat_index_load()

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.

Parameters
indexgets the index; free it with rat_index_free.
Returns
0, or -1 (also when there is no index yet).

◆ rat_install_name()

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 against the index, then install it as rat_install_file does.

the file leaves the cache after the install. a package that has the version of the index is refused before the download.

Parameters
flagsRAT_FORCE, RAT_DOWNGRADE, or 0.
notegets the notes of the install (may be NULL).
progressgets the progress of the download (may be NULL).
contextgoes to note and to progress.
Returns
0, or -1 with nothing changed.

◆ rat_upgradable()

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.

a package that is not in the index is not in the list.

Parameters
items_outgets an array to free with rat_upgradables_free; its remote pointers point into the index, so free it first.
Returns
0, or -1.

◆ rat_upgradables_free()

void rat_upgradables_free ( rat_upgradable_t * items,
int count )

release an array from rat_upgradable.

◆ rat_clean()

int rat_clean ( rat_t * rat,
uint64_t * freed_out,
rat_error_t * err )

remove the downloaded files of the cache.

Parameters
freed_outgets the bytes that were freed.
Returns
0, or -1.