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

librat helpers: messages, paths, file data and SHA-256, safe writes, folders. More...

#include "internal.h"
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "sha2.h"

Functions

int rat_fail (rat_error_t *err, const char *format,...)
void rat_say (rat_note_t note, void *context, const char *format,...)
char * rat_strdup (const char *s)
 a copy of a string, or NULL when there is no memory
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.
void rat_entry_clear (rat_entry_t *entry)
 release the members of an entry
void rat_entries_free (rat_entry_t *entries, int count)
 release an array from rat_files.
int rat_compare_entries (const void *a, const void *b)
 compare two entries by path, for qsort and bsearch
void rat_sort_entries (rat_entry_t *entries, int count)
 sort entries by 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/.

Detailed Description

librat helpers: messages, paths, file data and SHA-256, safe writes, folders.

Function Documentation

◆ rat_fail()

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

◆ rat_say()

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

◆ rat_strdup()

char * rat_strdup ( const char * s)

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

◆ rat_path()

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_entry_clear()

void rat_entry_clear ( rat_entry_t * entry)

release the members of an entry

◆ rat_entries_free()

void rat_entries_free ( rat_entry_t * entries,
int count )

release an array from rat_files.

◆ rat_compare_entries()

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

compare two entries by path, for qsort and bsearch

◆ rat_sort_entries()

void rat_sort_entries ( rat_entry_t * entries,
int count )

sort entries by path

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