Distributed File System 1
Distributed file system in C
Loading...
Searching...
No Matches
lib.h
Go to the documentation of this file.
1/* lib/lib.h
2 *
3 * General library stuff.
4 *
5 */
6#ifndef LIB_H_
7#define LIB_H_
8
9#ifdef __cplusplus
10extern "C" {
11#endif
12
13/* ----- Includes ----- */
14
15#include <stdint.h>
16#include <stdlib.h>
17#include <assert.h>
18#include <stdio.h>
19#include <string.h>
20#include <errno.h>
21#include <stdbool.h>
22#include "list.h"
23#include "log.h" // This may appear unused but it is used in the macros
24
25/* ----- Macros ----- */
26
29#define try(a) do { \
30 if ((a) < 0) { \
31 log(FATAL, "%s:%s:%d %s\n", __FILE__, __func__, __LINE__, strerror(errno)); \
32 exit (EXIT_FAILURE); \
33 } \
34 } while(0)
35
36
39#define exists(a) do { \
40 if ((a) == NULL) { \
41 log(FATAL, "%s:%s:%d Null pointer encountered, %s\n", __FILE__, __func__, __LINE__, strerror(errno)); \
42 exit (EXIT_FAILURE); \
43 } \
44 } while(0)
45
47#define TODO do { \
48 log(ERROR, "%s:%s:%d Not yet implemented\n", __FILE__, __func__, __LINE__); \
49 exit(EXIT_FAILURE); \
50} while(0)
51
53#define expect(cond, ...) do { \
54 if (!(cond)) { \
55 fprintf(stderr, __VA_ARGS__); \
56 exit(EXIT_FAILURE); \
57 } \
58} while (0)
59
61#define KB4 0x1000
62
64#define CHUNK_SIZE 0x400
65
66
67/* ----- Types ----- */
68
70typedef uint8_t Byte;
71
73typedef const char* Str;
74
76typedef struct {
78 size_t len;
79} SafeStr;
80
89Byte* copy(Byte* src, size_t nbytes);
90
92typedef struct {
93 size_t len;
95} Buffer;
96
98
99
100typedef struct {
101 FILE* fptr;
102 size_t position;
103 size_t size;
104 size_t len;
105 Byte buf[CHUNK_SIZE];
107
108/* ----- Functions ----- */
109
123bool mkdir_p(SafeStr fpath, mode_t mode);
124
135LazyBuffer open(Str fpath);
136
149
157
163
170
175ListBuffer ListBuffer_deserialize(Buffer buf);
176
182Buffer to_buffer(const void* thing, size_t nbytes);
183
188Buffer uint8_t_serialize(uint8_t n);
189
194uint8_t uint8_t_deserialize(Buffer buf);
195
200Buffer uint16_t_serialize(uint16_t n);
201
206uint16_t uint16_t_deserialize(Buffer buf);
207
212Buffer uint32_t_serialize(uint32_t n);
213
218uint32_t uint32_t_deserialize(Buffer buf);
219
224Buffer atob(Str str);
225
231Buffer validate_str(Buffer str, size_t max_len);
232
239
246size_t safe_strlen(Str str, size_t max_len);
247
252void Buffer_deinit(Buffer kod);
253
258void Buffer_write(Buffer buf, FILE* f);
259
264Buffer Buffer_read(FILE* f);
265
272
278
283Buffer size_t_serialize(size_t a);
284
289size_t size_t_deserialize(Buffer buf);
290
297Buffer concat(Buffer fst, Buffer snd);
298
304bool Str_eq(Str lhs, Str rhs);
305
311SafeStr atoss(Str str);
312
318size_t min(size_t l, size_t r);
319
320#ifdef __cplusplus
321}
322#endif
323
324#endif // LIB_H_
const char * Str
String type.
Definition lib.h:73
uint16_t uint16_t_deserialize(Buffer buf)
Deserialize a uint16_t from a Buffer.
Definition lib.c:271
LazyBuffer next_chunk(LazyBuffer b)
Reads the next chunk of the file, if it can.
Definition lib.c:328
Buffer uint32_t_serialize(uint32_t n)
Serialize a uint32_t to a Buffer.
Definition lib.c:284
Buffer atob(Str str)
Converts a CString to a Byte Buffer.
Definition lib.c:138
bool mkdir_p(SafeStr fpath, mode_t mode)
Recursively create directories.
Definition lib.c:32
Buffer validate_str(Buffer str, size_t max_len)
Validates that a string received through a Socket is correct.
Definition lib.c:108
Byte * copy(Byte *src, size_t nbytes)
Byte by Byte copy.
Definition lib.c:86
bool Str_eq(Str lhs, Str rhs)
Compares two strings.
Definition lib.c:209
Buffer Buffer_serialize(Buffer buf)
Serialize a Buffer to a Buffer.
Definition lib.c:213
size_t size_t_deserialize(Buffer buf)
Deserializes a size_t length.
Definition lib.c:175
Buffer concat(Buffer fst, Buffer snd)
Concatenate two Buffer's.
Definition lib.c:188
LazyBuffer open(Str fpath)
Open a LazyBuffer.
Definition lib.c:313
Buffer Buffer_read(FILE *f)
Reads data from a file.
Definition lib.c:65
uint8_t Byte
Simple Byte type. Guaranteed to be exactly 8 bits.
Definition lib.h:70
LazyBuffer
Definition lib.h:106
size_t safe_strlen(Str str, size_t max_len)
Does an strlen using a max size.
Definition lib.c:132
void Buffer_write(Buffer buf, FILE *f)
Writes data from a Buffer to a file.
Definition lib.c:60
Buffer bufcpy(Buffer in)
Make a copy of a Buffer.
Definition lib.c:96
Buffer SafeStr_serialize(SafeStr str)
Serializes a string.
Definition lib.c:145
SafeStr atoss(Str str)
Convert a string literal to a safe string WARNING: DO NOT USE THIS TO CONVERT THINGS THAT ARE NOT STR...
Definition lib.c:301
SafeStr SafeStr_deserialize(Buffer buf)
Deserializes a string from a Buffer.
Definition lib.c:149
Buffer uint16_t_serialize(uint16_t n)
Serialize a uint16_t to a Buffer.
Definition lib.c:267
void Buffer_deinit(Buffer kod)
Deinitializes a Buffer.
Definition lib.c:56
Buffer Buffer_deserialize(Buffer buf)
Deserialize a Buffer from a Buffer.
Definition lib.c:219
uint8_t uint8_t_deserialize(Buffer buf)
Deserialize a uint8_t from a Buffer.
Definition lib.c:253
Buffer size_t_serialize(size_t a)
Serializes a size_t length.
Definition lib.c:164
Buffer to_buffer(const void *thing, size_t nbytes)
Convert a thing to a buffer.
Definition lib.c:237
size_t min(size_t l, size_t r)
Simply returns the minimum of the two.
Definition lib.c:308
Buffer ListBuffer_serialize(ListBuffer list)
Serialize a List of Buffer's to a Buffer.
uint32_t uint32_t_deserialize(Buffer buf)
Deserialize a uint32_t from a Buffer.
Definition lib.c:288
#define CHUNK_SIZE
un solo kilobyte
Definition lib.h:64
Buffer uint8_t_serialize(uint8_t n)
Serialize a uint8_t to a Buffer.
Definition lib.c:249
ListBuffer ListBuffer_deserialize(Buffer buf)
Deserialize a ListBuffer from a Buffer.
Definition lib.c:18
#define DefList(type)
Definition list.h:93
void list(Enchufe cliente)
Definition meta-data.c:14
Byte buffer.
Definition lib.h:92
size_t len
length of buffer
Definition lib.h:93
Byte * buf
underlying buffer
Definition lib.h:94
Safe String type.
Definition lib.h:76
Str str
the underlying buffer for the string
Definition lib.h:77
size_t len
the string's actual length
Definition lib.h:78