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

the parts of librat that its source files share. More...

#include "conf.h"
#include "rat.h"
#include <stdio.h>

Go to the source code of this file.

Data Structures

struct  rat
struct  rat_txn_t
 a list of operations that is being made More...
struct  rat_owned_t
 the owners of all paths: the base image and the installed packages More...
struct  rat_owners_t

Macros

#define RAT_MACHINE_ARCH   "i686"
 the architecture of this machine, as packages name it
#define RAT_ENTRIES_MAX   65536
 the most members in a package
#define RAT_TEXT_MAX   512
 the longest description and licence
#define RAT_KEYS_DIR   "etc/rat/keys"
 the folder of the public keys that sign the index, relative to the root
#define RAT_RATINFO_MAX   65536
 the largest .RATINFO
#define RAT_STAGE_NAME_MAX   66
 the size of a name in the stage folder: 'f', 64 hex digits, '\0'
#define RAT_NEW_SUFFIX   ".ratnew"
 the end of the name that a new settings file gets when the person changed the old one

Enumerations

enum  rat_disk_t { RAT_DISK_GONE , RAT_DISK_SAME , RAT_DISK_CHANGED , RAT_DISK_FOLDER }
 what is on the disk at the path of a file or link of an installed package More...

Functions

int rat_fail (rat_error_t *err, const char *format,...) __attribute__((format(printf
 set the message of err (printf format).
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 void char * rat_path (const rat_t *rat, const char *rel)
 a path on the root: <root>/<rel>.
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 removed.
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 resolved from the folder of the link.
void rat_hex (const uint8_t sha[32], char out[65])
 the SHA-256 as 64 hexadecimal digits and a '\0'
int rat_unhex (const char *text, uint8_t sha[32])
 read 64 hexadecimal digits.
int rat_sha256_file (const char *path, uint8_t sha[32], uint64_t *size_out)
 the SHA-256 and size of a file.
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.
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, then rename.
int rat_mkdirs (const char *path, uint32_t mode)
 make a folder and the folders above it that are not there.
int rat_remove_tree (const char *path)
 remove a file, a link, or a folder with everything in it.
char * rat_strdup (const char *s)
 a copy of a string, or NULL when there is no memory
void rat_entry_clear (rat_entry_t *entry)
 release the members of an entry
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.
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.
bool rat_version_ok (const char *version)
 a version: 1 to RAT_VERSION_MAX characters of 0-9 a-z A-Z .
bool rat_text_ok (const char *text)
 a text field: at most RAT_TEXT_MAX bytes, and no control characters
int rat_parse_number (const char *text, uint64_t max, uint64_t *out)
 a whole number of decimal digits only, at most max.
void rat_sort_entries (rat_entry_t *entries, int count)
 sort entries by path
int rat_compare_entries (const void *a, const void *b)
 compare two entries by path, for qsort and bsearch
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.
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
void rat_txn_init (rat_txn_t *txn)
void rat_txn_free (rat_txn_t *txn)
int rat_txn_add (rat_txn_t *txn, const char *format,...) __attribute__((format(printf
 add one operation (printf format, without the '
').
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.
bool rat_txn_pending (const rat_t *rat)
 true if the list of an interrupted change is there
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).
char * rat_files_record_text (const rat_entry_t *entries, int count)
 the text of a files record.
int rat_recover (rat_t *rat, rat_error_t *err)
 finish the transaction of an earlier rat that stopped, or remove its stage folder.
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.
void rat_owners_free (rat_owners_t *owners)
 release the owners
const rat_owned_t * rat_owners_find (const rat_owners_t *owners, const char *path, int *count_out)
 the owners of a path.
bool rat_is_settings (const rat_entry_t *entry)
 true if a path of a package is a settings file: a file under etc/.
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
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
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.
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.
void rat_remote_clear (rat_remote_t *remote)
 release the strings of one package of an index
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.

Variables

int rat_test_stop_after
 for the host tests only: when 0 or more, a transaction stops after this number of operations, as if the power went off.

Detailed Description

the parts of librat that its source files share.

Macro Definition Documentation

◆ RAT_MACHINE_ARCH

#define RAT_MACHINE_ARCH   "i686"

the architecture of this machine, as packages name it

◆ RAT_ENTRIES_MAX

#define RAT_ENTRIES_MAX   65536

the most members in a package

◆ RAT_TEXT_MAX

#define RAT_TEXT_MAX   512

the longest description and licence

◆ RAT_KEYS_DIR

#define RAT_KEYS_DIR   "etc/rat/keys"

the folder of the public keys that sign the index, relative to the root

◆ RAT_RATINFO_MAX

#define RAT_RATINFO_MAX   65536

the largest .RATINFO

◆ RAT_STAGE_NAME_MAX

#define RAT_STAGE_NAME_MAX   66

the size of a name in the stage folder: 'f', 64 hex digits, '\0'

◆ RAT_NEW_SUFFIX

#define RAT_NEW_SUFFIX   ".ratnew"

the end of the name that a new settings file gets when the person changed the old one

Enumeration Type Documentation

◆ rat_disk_t

enum rat_disk_t

what is on the disk at the path of a file or link of an installed package

Enumerator
RAT_DISK_GONE 
RAT_DISK_SAME 
RAT_DISK_CHANGED 
RAT_DISK_FOLDER 

Function Documentation

◆ rat_fail()

int rat_fail ( rat_error_t * err,
const char * format,
... )

set the message of err (printf format).

Returns
-1, so a caller can write "return rat_fail(err, ...)".

◆ rat_say()

int void rat_say ( rat_note_t note,
void * context,
const char * format,
... )

give a note to the caller (printf format); nothing when note is NULL

◆ rat_path()

int void char * rat_path ( const rat_t * rat,
const char * rel )

a path on the root: <root>/<rel>.

Returns
a string to free, or NULL (no memory).

◆ rat_check_path()

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

Parameters
outgets the normal form (RAT_PATH_MAX + 1 bytes).
Returns
NULL if the path is good, else the rule that it breaks.

◆ rat_check_link()

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 resolved from the folder of the link.

Returns
NULL if the target is good, else the rule that it breaks.

◆ rat_hex()

void rat_hex ( const uint8_t sha[32],
char out[65] )

the SHA-256 as 64 hexadecimal digits and a '\0'

◆ rat_unhex()

int rat_unhex ( const char * text,
uint8_t sha[32] )

read 64 hexadecimal digits.

Returns
0, or -1

◆ rat_sha256_file()

int rat_sha256_file ( const char * path,
uint8_t sha[32],
uint64_t * size_out )

the SHA-256 and size of a file.

Returns
0, or -1 with errno set.

◆ rat_read_file()

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.

Parameters
maxthe largest size that is read.
Returns
a buffer to free, or NULL with errno set (EFBIG past max).

◆ rat_write_file()

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, then rename.

Returns
0, or -1 with errno set.

◆ rat_mkdirs()

int rat_mkdirs ( const char * path,
uint32_t mode )

make a folder and the folders above it that are not there.

Returns
0, or -1 with errno set.

◆ rat_remove_tree()

int rat_remove_tree ( const char * path)

remove a file, a link, or a folder with everything in it.

it never follows a symbolic link. a path that is not there is not an error.

Returns
0, or -1 with errno set.

◆ rat_strdup()

char * rat_strdup ( const char * s)

a copy of a string, or NULL when there is no memory

◆ rat_entry_clear()

void rat_entry_clear ( rat_entry_t * entry)

release the members of an entry

◆ rat_parse_info()

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.

Returns
0, or -1.

◆ rat_info_from_conf()

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.

Parameters
wherethe place for messages: ".RATINFO" or "index.conf [lua]".
Returns
0, or -1.

◆ rat_version_ok()

bool rat_version_ok ( const char * version)

a version: 1 to RAT_VERSION_MAX characters of 0-9 a-z A-Z .

  • ~ _

◆ rat_text_ok()

bool rat_text_ok ( const char * text)

a text field: at most RAT_TEXT_MAX bytes, and no control characters

◆ rat_parse_number()

int rat_parse_number ( const char * text,
uint64_t max,
uint64_t * out )

a whole number of decimal digits only, at most max.

Returns
0, or -1.

◆ rat_sort_entries()

void rat_sort_entries ( rat_entry_t * entries,
int count )

sort entries by path

◆ rat_compare_entries()

int rat_compare_entries ( const void * a,
const void * b )

compare two entries by path, for qsort and bsearch

◆ rat_package_unpack()

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.

with a stage folder, also write each file and link there, under the name that rat_stage_name gives for its path.

Parameters
stagethe stage folder (it must exist), or NULL.

◆ rat_stage_name()

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

◆ rat_txn_init()

void rat_txn_init ( rat_txn_t * txn)

◆ rat_txn_free()

void rat_txn_free ( rat_txn_t * txn)

◆ rat_txn_add()

int rat_txn_add ( rat_txn_t * txn,
const char * format,
... )

add one operation (printf format, without the '
').

Returns
0, or -1 (no memory).

◆ rat_txn_commit()

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.

Parameters
notea line that tells what the change is ("install lua 5.4.9-1").
Returns
0, or -1. after -1 the list can still be there: the next rat_open for writing finishes it.

◆ rat_txn_pending()

bool rat_txn_pending ( const rat_t * rat)

true if the list of an interrupted change is there

◆ rat_read_files_record()

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

Returns
0, or -1.

◆ rat_files_record_text()

char * rat_files_record_text ( const rat_entry_t * entries,
int count )

the text of a files record.

Returns
a string to free, or NULL (no memory).

◆ rat_recover()

int rat_recover ( rat_t * rat,
rat_error_t * err )

finish the transaction of an earlier rat that stopped, or remove its stage folder.

rat->recovered gets a message when there was one.

Returns
0, or -1.

◆ rat_owners_load()

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.

Returns
0, or -1.

◆ rat_owners_free()

void rat_owners_free ( rat_owners_t * owners)

release the owners

◆ rat_owners_find()

const rat_owned_t * rat_owners_find ( const rat_owners_t * owners,
const char * path,
int * count_out )

the owners of a path.

Parameters
count_outgets how many there are (0 when none).
Returns
the first of them, or NULL.

◆ rat_is_settings()

bool rat_is_settings ( const rat_entry_t * entry)

true if a path of a package is a settings file: a file under etc/.

a settings file that the person changed stays at an upgrade or a remove.

◆ rat_owned_by_other()

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

◆ rat_disk_state()

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

◆ rat_signature_check()

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.

one of the keys in <root>/etc/rat/keys must verify it.

Returns
0 when the signature is good, or -1.

◆ rat_index_parse()

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.

the signature must be checked before.

Parameters
archthe architecture that the index must be for.
Returns
0, or -1.

◆ rat_remote_clear()

void rat_remote_clear ( rat_remote_t * remote)

release the strings of one package of an index

◆ rat_download()

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.

only https and file URLs are used, and a redirect must go to https. on a failure the file is removed.

Parameters
max_sizethe largest file that is accepted.
progressgets the progress (may be NULL).
Returns
0, or -1.

Variable Documentation

◆ rat_test_stop_after

int rat_test_stop_after
extern

for the host tests only: when 0 or more, a transaction stops after this number of operations, as if the power went off.

-1 normally.