Distributed File System 1
Distributed file system in C
Loading...
Searching...
No Matches
lib.c File Reference
#include "lib.h"
#include "log.h"
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

Functions

 ImplList (Buffer)
ListBuffer ListBuffer_deserialize (Buffer buf)
 Deserialize a ListBuffer from a Buffer.
bool mkdir_p (SafeStr fpath, mode_t mode)
 Recursively create directories.
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.
Bytecopy (Byte *src, size_t nbytes)
 Byte by Byte copy.
Buffer bufcpy (Buffer src)
 Allocates new memory from src and returns a buffer to that memory.
Buffer validate_str (Buffer str, size_t max_len)
 Checks whether the buffer contains a valid string and that the size provided matches the size of that string.
size_t safe_strlen (Str str, size_t max_len)
 Does an strlen using a max size.
Buffer atob (const char *str)
 Converts a CString to a Byte Buffer.
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 l, Buffer r)
 Concatenate two Buffer's.
bool Str_eq (Str l, Str r)
 Compares two strings.
Buffer Buffer_serialize (Buffer buf)
 Serialize a Buffer to a Buffer.
Buffer Buffer_deserialize (Buffer buf)
 Deserialize a Buffer 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.
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.
LazyBuffer open (Str fpath)
 Open a LazyBuffer.
LazyBuffer next_chunk (LazyBuffer b)
 Reads the next chunk of the file, if it can.

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)

Allocates new memory from src and returns a buffer to that memory.

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

◆ ImplList()

ImplList ( 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

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

Checks whether the buffer contains a valid string and that the size provided matches the size of that string.

Validates that a string received through a Socket is correct.