Distributed File System 1
Distributed file system in C
Loading...
Searching...
No Matches
lib.h File Reference
#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include "list.h"
#include "log.h"

Go to the source code of this file.

Classes

struct  SafeStr
 Safe String type. More...
struct  Buffer
 Byte buffer. More...

Macros

#define try(a)
 Macro para probar si un numero es negativo.
#define exists(a)
 Macro para detectar si un pointer es NULL.
#define TODO
 Macro for marking parts that have not been implemented.
#define expect(cond, ...)
 Macro para crashear el programa si una condicion no se cumple.
#define KB4   0x1000
 cuatro kilobytes
#define CHUNK_SIZE   0x400
 un solo kilobyte

Typedefs

typedef uint8_t Byte
 Simple Byte type. Guaranteed to be exactly 8 bits.
typedef const char * Str
 String type.

Functions

Bytecopy (Byte *src, size_t nbytes)
 Byte by Byte copy.
 DefList (Buffer) typedef struct
 This is the buffer that lazily opens the file.
bool mkdir_p (SafeStr fpath, mode_t mode)
 Recursively create directories.
LazyBuffer open (Str fpath)
 Open a LazyBuffer.
LazyBuffer next_chunk (LazyBuffer b)
 Reads the next chunk of the file, if it can.
Buffer Buffer_serialize (Buffer buf)
 Serialize a Buffer to a Buffer.
Buffer Buffer_deserialize (Buffer buf)
 Deserialize a Buffer from a Buffer.
Buffer ListBuffer_serialize (ListBuffer list)
 Serialize a List of Buffer's to a Buffer.
ListBuffer ListBuffer_deserialize (Buffer buf)
 Deserialize a ListBuffer from a Buffer.
Buffer to_buffer (const void *thing, size_t nbytes)
 Convert a thing to a buffer.
Buffer uint8_t_serialize (uint8_t n)
 Serialize a uint8_t to a Buffer.
uint8_t uint8_t_deserialize (Buffer buf)
 Deserialize a uint8_t from a Buffer.
Buffer uint16_t_serialize (uint16_t n)
 Serialize a uint16_t to a Buffer.
uint16_t uint16_t_deserialize (Buffer buf)
 Deserialize a uint16_t from a Buffer.
Buffer uint32_t_serialize (uint32_t n)
 Serialize a uint32_t to a Buffer.
uint32_t uint32_t_deserialize (Buffer buf)
 Deserialize a uint32_t from a Buffer.
Buffer atob (Str str)
 Converts a CString to a Byte Buffer.
Buffer validate_str (Buffer str, size_t max_len)
 Validates that a string received through a Socket is correct.
Buffer bufcpy (Buffer in)
 Make a copy of a Buffer.
size_t safe_strlen (Str str, size_t max_len)
 Does an strlen using a max size.
void Buffer_deinit (Buffer kod)
 Deinitializes a Buffer.
void Buffer_write (Buffer buf, FILE *f)
 Writes data from a Buffer to a file.
Buffer Buffer_read (FILE *f)
 Reads data from a file.
Buffer SafeStr_serialize (SafeStr str)
 Serializes a string.
SafeStr SafeStr_deserialize (Buffer buf)
 Deserializes a string from a Buffer.
Buffer size_t_serialize (size_t a)
 Serializes a size_t length.
size_t size_t_deserialize (Buffer buf)
 Deserializes a size_t length.
Buffer concat (Buffer fst, Buffer snd)
 Concatenate two Buffer's.
bool Str_eq (Str lhs, Str rhs)
 Compares two strings.
SafeStr atoss (Str str)
 Convert a string literal to a safe string WARNING: DO NOT USE THIS TO CONVERT THINGS THAT ARE NOT STRING LITERALS!
size_t min (size_t l, size_t r)
 Simply returns the minimum of the two.

Variables

 LazyBuffer

Macro Definition Documentation

◆ CHUNK_SIZE

#define CHUNK_SIZE   0x400

un solo kilobyte

◆ exists

#define exists ( a)
Value:
do { \
if ((a) == NULL) { \
log(FATAL, "%s:%s:%d Null pointer encountered, %s\n", __FILE__, __func__, __LINE__, strerror(errno)); \
exit (EXIT_FAILURE); \
} \
} while(0)
@ FATAL
Definition log.h:20

Macro para detectar si un pointer es NULL.

Usa esta funcion si prefieres crashear el programa cuando encuetras un puntero NULL.

◆ expect

#define expect ( cond,
... )
Value:
do { \
if (!(cond)) { \
fprintf(stderr, __VA_ARGS__); \
exit(EXIT_FAILURE); \
} \
} while (0)

Macro para crashear el programa si una condicion no se cumple.

◆ KB4

#define KB4   0x1000

cuatro kilobytes

◆ TODO

#define TODO
Value:
do { \
log(ERROR, "%s:%s:%d Not yet implemented\n", __FILE__, __func__, __LINE__); \
exit(EXIT_FAILURE); \
} while(0)
@ ERROR
Definition log.h:19

Macro for marking parts that have not been implemented.

◆ try

#define try ( a)
Value:
do { \
if ((a) < 0) { \
log(FATAL, "%s:%s:%d %s\n", __FILE__, __func__, __LINE__, strerror(errno)); \
exit (EXIT_FAILURE); \
} \
} while(0)

Macro para probar si un numero es negativo.

En general, esta libreria prefiere crashear el programa que dejar que el usuario arregle un error.

Typedef Documentation

◆ Byte

typedef uint8_t Byte

Simple Byte type. Guaranteed to be exactly 8 bits.

◆ Str

typedef const char* Str

String type.

Function Documentation

◆ atob()

Buffer atob ( Str str)

Converts a CString to a Byte Buffer.

Parameters
strthe string to convert
Returns
the Buffer the string represents

◆ atoss()

SafeStr atoss ( Str str)

Convert a string literal to a safe string WARNING: DO NOT USE THIS TO CONVERT THINGS THAT ARE NOT STRING LITERALS!

Parameters
strthe string literal that you want to convert
Returns
a safe string representing the string literal

◆ bufcpy()

Buffer bufcpy ( Buffer src)

Make a copy of a Buffer.

Caller must free memory.

Parameters
inthe Buffer to copy
Returns
a copy of the Buffer

Make a copy of a Buffer.

The user must free that memory.

◆ Buffer_deinit()

void Buffer_deinit ( Buffer kod)

Deinitializes a Buffer.

You could also just use free.

Parameters
kodBuffer to deinitialize

◆ Buffer_deserialize()

Buffer Buffer_deserialize ( Buffer buf)

Deserialize a Buffer from a Buffer.

Parameters
bufthe Buffer containing that Buffer
Returns
the Buffer inside that Buffer

◆ Buffer_read()

Buffer Buffer_read ( FILE * f)

Reads data from a file.

Parameters
fa FILE* to read from
Returns
the Buffer that that file has

◆ Buffer_serialize()

Buffer Buffer_serialize ( Buffer buf)

Serialize a Buffer to a Buffer.

This basically just includes the Buffer's contents along with the size of the Buffer.

Parameters
bufthe Buffer that you want to serialize
Returns
a Buffer representing that Buffer

◆ Buffer_write()

void Buffer_write ( Buffer buf,
FILE * f )

Writes data from a Buffer to a file.

Parameters
bufthe Buffer to write from
fa FILE* to write to

◆ concat()

Buffer concat ( Buffer fst,
Buffer snd )

Concatenate two Buffer's.

INFO: Memory is automatically free'd for buffers.

Parameters
fstthe Buffer that comes first
sndthe Buffer that comes second
Returns
a Buffer containing the data in both Buffer's

◆ copy()

Byte * copy ( Byte * src,
size_t nbytes )

Byte by Byte copy.

WARNING: DO NOT USE THIS TO COPY STRINGS. IT WILL NOT COPY THE NULL TERMINATOR AND YOU WILL HAVE A HEAP BUFFER OVERFLOW. Caller must free memory.

Parameters
srcthe bytes to copy from
nbytesthe number of bytes the src has
Returns
a pointer to the new bytes

◆ DefList()

DefList ( Buffer )

This is the buffer that lazily opens the file.

< the FILE* of the file

< the position in the file

< the total size of the file

< the length of the current buffer

< the current buffer

◆ ListBuffer_deserialize()

ListBuffer ListBuffer_deserialize ( Buffer buf)

Deserialize a ListBuffer from a Buffer.

Parameters
bufthe Buffer containing that ListBuffer
Returns
the ListBuffer inside that Buffer

◆ ListBuffer_serialize()

Buffer ListBuffer_serialize ( ListBuffer list)

Serialize a List of Buffer's to a Buffer.

This basically just concatenates all the Buffer's.

Parameters
listthe ListBuffer that you want to serialize
Returns
a Buffer representing that ListBuffer

◆ min()

size_t min ( size_t l,
size_t r )

Simply returns the minimum of the two.

Parameters
lthe lhs
rthe rhs
Returns
the smaller of l and r

◆ mkdir_p()

bool mkdir_p ( SafeStr fpath,
mode_t mode )

Recursively create directories.

The following code:

mkdir_p(fpath, mode);
bool mkdir_p(SafeStr fpath, mode_t mode)
Recursively create directories.
Definition lib.c:32

is equivalent to the following Shell code:

mkdir -p -m mode fpath
Parameters
fpaththe filepath where you want to create a directory
modethe mode in which you want to create the directory
Returns
A boolean value telling you whether the directory was created correctly or not.

◆ next_chunk()

LazyBuffer next_chunk ( LazyBuffer b)

Reads the next chunk of the file, if it can.

In the case where the entire file was read, the LazyBuffer::len will be 0. I'm not sure in what case you wouldn't want to do it this way, but you probably always want to use this like this:

b = next_chunk(b);
LazyBuffer next_chunk(LazyBuffer b)
Reads the next chunk of the file, if it can.
Definition lib.c:328
Parameters
bthe LazyBuffer that you want to get the next chunk for
Returns
a LazyBuffer with the next chunk in its LazyBuffer::buf

◆ open()

LazyBuffer open ( Str fpath)

Open a LazyBuffer.

INFO: This does not fill the LazyBuffer with anything. Consider it similar to just executing:

fopen(fpath);
Parameters
fpaththe file path to the file that you want to open
Returns
a LazyBuffer with the

◆ safe_strlen()

size_t safe_strlen ( Str str,
size_t max_len )

Does an strlen using a max size.

Avoids buffer overflows.

Parameters
strthe string to look at
max_lenthe maximum length that the string can have
Returns
the size of the string

◆ SafeStr_deserialize()

SafeStr SafeStr_deserialize ( Buffer buf)

Deserializes a string from a Buffer.

Parameters
bufBuffer to deserialize from
Returns
a safe string from the Buffer

◆ SafeStr_serialize()

Buffer SafeStr_serialize ( SafeStr str)

Serializes a string.

INFO: ONLY SERIALIZE STRINGS WITH THIS.

Parameters
stra safe string to serialize
Returns
a Buffer containing that string

◆ size_t_deserialize()

size_t size_t_deserialize ( Buffer buf)

Deserializes a size_t length.

Parameters
bufthe Buffer to deserialize from
Returns
the size

◆ size_t_serialize()

Buffer size_t_serialize ( size_t a)

Serializes a size_t length.

Parameters
athe size to serialize
Returns
a Buffer containing the size

◆ Str_eq()

bool Str_eq ( Str lhs,
Str rhs )

Compares two strings.

Parameters
lhsleft-hand side string
rhsright-hand side string
Returns
whether the strings are equal or not

◆ to_buffer()

Buffer to_buffer ( const void * thing,
size_t nbytes )

Convert a thing to a buffer.

This can be used for types whose size is known at compile-time.

Parameters
thinga pointer to the thing that you want to serialize
Returns
a Buffer containing that thing

◆ uint16_t_deserialize()

uint16_t uint16_t_deserialize ( Buffer buf)

Deserialize a uint16_t from a Buffer.

Parameters
bufthe Buffer containing that uint16_t
Returns
the uint16_t inside that Buffer

◆ uint16_t_serialize()

Buffer uint16_t_serialize ( uint16_t n)

Serialize a uint16_t to a Buffer.

Parameters
commandthe uint16_t that you want to serialize
Returns
a Buffer representing that uint16_t

◆ uint32_t_deserialize()

uint32_t uint32_t_deserialize ( Buffer buf)

Deserialize a uint32_t from a Buffer.

Parameters
bufthe Buffer containing that uint32_t
Returns
the uint32_t inside that Buffer

◆ uint32_t_serialize()

Buffer uint32_t_serialize ( uint32_t n)

Serialize a uint32_t to a Buffer.

Parameters
commandthe uint32_t that you want to serialize
Returns
a Buffer representing that uint32_t

◆ uint8_t_deserialize()

uint8_t uint8_t_deserialize ( Buffer buf)

Deserialize a uint8_t from a Buffer.

Parameters
bufthe Buffer containing that uint8_t
Returns
the uint8_t inside that Buffer

◆ uint8_t_serialize()

Buffer uint8_t_serialize ( uint8_t n)

Serialize a uint8_t to a Buffer.

Parameters
commandthe uint8_t that you want to serialize
Returns
a Buffer representing that uint8_t

◆ validate_str()

Buffer validate_str ( Buffer str,
size_t max_len )

Validates that a string received through a Socket is correct.

Parameters
stra Buffer with the string
max_lenthe maximum size of the string
Returns
a Buffer containing the string

Validates that a string received through a Socket is correct.

Variable Documentation

◆ LazyBuffer

LazyBuffer