Skip to content

Commit

Permalink
Merge pull request #44 from larsewi/bogus
Browse files Browse the repository at this point in the history
General improvements and refactoring
  • Loading branch information
larsewi authored Apr 15, 2024
2 parents 8b775db + ffdd296 commit 1767973
Show file tree
Hide file tree
Showing 43 changed files with 816 additions and 996 deletions.
21 changes: 14 additions & 7 deletions bin/patch.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#include <stdio.h>
#include <stdlib.h>

#include "../lib/utils.h"
#include "../lib/buffer.h"
#include "../lib/logger.h"
#include "common.h"

enum OPTION_VALUE {
Expand Down Expand Up @@ -80,20 +81,26 @@ int Patch(const char *const work_dir, int argc, char *argv[]) {
return EXIT_FAILURE;
}

size_t size;
char *const buffer = LCH_FileRead(patch_file, &size);
LCH_Buffer *const buffer = LCH_BufferCreate();
if (buffer == NULL) {
LCH_LOG_ERROR("Failed to load patch file '%s'.", patch_file);
return EXIT_FAILURE;
}

if (!LCH_BufferReadFile(buffer, patch_file)) {
LCH_BufferDestroy(buffer);
return EXIT_FAILURE;
}

const char *const data = LCH_BufferData(buffer);
const size_t size = LCH_BufferLength(buffer);
LCH_LOG_DEBUG("Loaded patch file '%s' %zu Bytes.", patch_file, size);

if (!LCH_Patch(work_dir, uid_field, uid_value, buffer, size)) {
if (!LCH_Patch(work_dir, uid_field, uid_value, data, size)) {
LCH_LOG_ERROR("Failed to apply patch from file '%s'.", patch_file);
free(buffer);
LCH_BufferDestroy(buffer);
return EXIT_FAILURE;
}

free(buffer);
LCH_BufferDestroy(buffer);
return EXIT_SUCCESS;
}
4 changes: 4 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ AM_CONDITIONAL([NDEBUG], [test x"$debug" = x"no"])
# Config constants.
AC_DEFINE([LCH_DEFAULT_MAX_CHAIN_LENGTH], 2048, [Default max chain length used in block garbage collector])
AC_DEFINE([LCH_JSON_PRETTY_INDENT_SIZE], 2, [Indent size used when composing pretty JSON])
AC_DEFINE([LCH_BUFFER_SIZE], 1024, [Initial buffer size allocated by leech])
AC_DEFINE([LCH_DICT_CAPACITY], 256, [Initial dictionary capacity allocated by leech])
AC_DEFINE([LCH_DICT_LOAD_FACTOR], 0.75f, [Initial dictionary capacity allocated by leech])
AC_DEFINE([LCH_LIST_CAPACITY], 256, [Initial list capacity allocated by leech])

# pkg-check modules.
PKG_CHECK_MODULES([CHECK], [check])
Expand Down
2 changes: 2 additions & 0 deletions lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ leech_psql_la_CFLAGS = @PSQL_CFLAGS@
libleech_la_SOURCES = leech.c \
block.h block.c \
buffer.h buffer.c \
files.h files.c \
string_lib.h string_lib.c \
csv.h csv.c \
json.h json.c \
logger.h logger.c \
Expand Down
8 changes: 4 additions & 4 deletions lib/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

#include <assert.h>
#include <limits.h>
#include <string.h>
#include <time.h>

#include "definitions.h"
#include "files.h"
#include "head.h"
#include "logger.h"
#include "string_lib.h"
#include "utils.h"

LCH_Json *LCH_BlockCreate(const char *const parent_id,
Expand Down Expand Up @@ -96,7 +96,7 @@ bool LCH_BlockStore(const LCH_Instance *const instance,
assert(block_id != NULL);

char path[PATH_MAX];
if (!LCH_PathJoin(path, PATH_MAX, 3, work_dir, "blocks", block_id)) {
if (!LCH_FilePathJoin(path, PATH_MAX, 3, work_dir, "blocks", block_id)) {
free(block_id);
LCH_BufferDestroy(json);
return false;
Expand All @@ -121,7 +121,7 @@ bool LCH_BlockStore(const LCH_Instance *const instance,
LCH_Json *LCH_BlockLoad(const char *const work_dir,
const char *const block_id) {
char path[PATH_MAX];
if (!LCH_PathJoin(path, PATH_MAX, 3, work_dir, "blocks", block_id)) {
if (!LCH_FilePathJoin(path, PATH_MAX, 3, work_dir, "blocks", block_id)) {
return NULL;
}

Expand Down
10 changes: 3 additions & 7 deletions lib/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
#include <string.h>
#include <unistd.h>

#include "files.h"
#include "logger.h"
#include "utils.h"

#define INITIAL_CAPACITY 1028

struct LCH_Buffer {
size_t length;
Expand Down Expand Up @@ -60,7 +58,7 @@ static LCH_Buffer *LCH_BufferCreateWithCapacity(size_t capacity) {
}

LCH_Buffer *LCH_BufferCreate(void) {
return LCH_BufferCreateWithCapacity(INITIAL_CAPACITY);
return LCH_BufferCreateWithCapacity(LCH_BUFFER_SIZE);
}

bool LCH_BufferAppend(LCH_Buffer *const self, const char byte) {
Expand Down Expand Up @@ -300,9 +298,7 @@ bool LCH_BufferWriteFile(const LCH_Buffer *buffer, const char *filename) {
assert(buffer != NULL);
assert(filename != NULL);

if (!LCH_CreateParentDirectories(filename)) {
LCH_LOG_ERROR("Failed to create parent directories for file '%s'",
filename);
if (!LCH_FileCreateParentDirectories(filename)) {
return false;
}

Expand Down
22 changes: 14 additions & 8 deletions lib/csv.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include "csv.h"

#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>

#include "logger.h"
#include "utils.h"

/**
* Character values allowed in non-escaped fields
Expand Down Expand Up @@ -304,19 +303,26 @@ LCH_List *LCH_CSVParseTable(const char *str, const size_t size) {
}

LCH_List *LCH_CSVParseFile(const char *const path) {
size_t size = 0;
char *csv = LCH_FileRead(path, &size);
if (csv == NULL) {
LCH_Buffer *const buffer = LCH_BufferCreate();
if (buffer == NULL) {
return NULL;
}

if (!LCH_BufferReadFile(buffer, path)) {
LCH_BufferDestroy(buffer);
return NULL;
}

const char *const csv = LCH_BufferData(buffer);
const size_t size = LCH_BufferLength(buffer);

LCH_List *table = LCH_CSVParseTable(csv, size);
if (table == NULL) {
free(csv);
LCH_BufferDestroy(buffer);
return NULL;
}

free(csv);
LCH_BufferDestroy(buffer);
return table;
}

Expand Down
2 changes: 0 additions & 2 deletions lib/definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#define LCH_MEBIBYTE(n) (n * 1024UL * 1024UL)
#define LCH_GIBIBYTE(n) (n * 1024ULL * 1024ULL * 1024ULL)

#define LCH_BUFFER_SIZE LCH_KIBIBYTE(4)

#define LCH_GENISIS_BLOCK_ID "0000000000000000000000000000000000000000"

#define LCH_LENGTH(x) (sizeof(x) / sizeof(*x))
Expand Down
3 changes: 1 addition & 2 deletions lib/delta.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include "delta.h"

#include <assert.h>
#include <string.h>

#include "utils.h"
#include "logger.h"

LCH_Json *LCH_DeltaCreate(const char *const table_id, const char *const type,
const LCH_Json *const new_state,
Expand Down
7 changes: 2 additions & 5 deletions lib/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
#include <errno.h>
#include <string.h>

#define INITIAL_CAPACITY 64
#define LOAD_FACTOR 0.75f

typedef struct DictElement {
LCH_Buffer *key;
void *value;
Expand All @@ -29,7 +26,7 @@ LCH_Dict *LCH_DictCreate() {
}

self->length = self->in_use = 0;
self->capacity = INITIAL_CAPACITY;
self->capacity = LCH_DICT_CAPACITY;
self->buffer = (DictElement **)calloc(self->capacity, sizeof(DictElement *));

if (self->buffer == NULL) {
Expand Down Expand Up @@ -81,7 +78,7 @@ static size_t ComputeIndex(const LCH_Dict *const dict,
}

static bool EnsureCapacity(LCH_Dict *const dict) {
if (dict->in_use < (dict->capacity * LOAD_FACTOR)) {
if (dict->in_use < (dict->capacity * LCH_DICT_LOAD_FACTOR)) {
return true;
}

Expand Down
1 change: 0 additions & 1 deletion lib/dict.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <stdlib.h>

#include "buffer.h"
#include "definitions.h"
#include "list.h"
#include "logger.h"

Expand Down
154 changes: 154 additions & 0 deletions lib/files.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#include "files.h"

#include <assert.h>
#include <errno.h>
#include <libgen.h>
#include <stdarg.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

#include "definitions.h"
#include "list.h"
#include "logger.h"
#include "string_lib.h"

/******************************************************************************/

bool LCH_FileSize(FILE *file, size_t *size) {
if (fseek(file, 0, SEEK_END) != 0) {
LCH_LOG_ERROR("Failed to seek to end of file: %s", strerror(errno));
return false;
}

long pos = ftell(file);
if (pos < 0) {
LCH_LOG_ERROR("Failed to obtain the current file position indicator: %s",
strerror(errno));
return false;
}
*size = (size_t)pos;

if (fseek(file, 0, SEEK_SET) != 0) {
LCH_LOG_ERROR("Failed to seek to start of file: %s", strerror(errno));
return false;
}

return true;
}

/******************************************************************************/

bool LCH_FileExists(const char *const path) {
struct stat sb;
memset(&sb, 0, sizeof(struct stat));
return stat(path, &sb) == 0;
}

/******************************************************************************/

bool LCH_FileIsRegular(const char *const path) {
struct stat sb;
memset(&sb, 0, sizeof(struct stat));
return (stat(path, &sb) == 0) && ((sb.st_mode & S_IFMT) == S_IFREG);
}

/******************************************************************************/

bool LCH_FileIsDirectory(const char *const path) {
struct stat sb;
memset(&sb, 0, sizeof(struct stat));
return (stat(path, &sb) == 0) && ((sb.st_mode & S_IFMT) == S_IFDIR);
}

/******************************************************************************/

bool LCH_FilePathJoin(char *path, const size_t path_max, const size_t n_items,
...) {
assert(path_max >= 1);

va_list ap;
va_start(ap, n_items);

size_t used = 0;
bool truncated = false;
for (size_t i = 0; i < n_items; i++) {
if (i > 0) {
if (path_max - used < 2) {
truncated = true;
break;
}
path[used++] = LCH_PATH_SEP;
}

char *const sub = va_arg(ap, char *);
const size_t sub_len = strlen(sub);
for (size_t j = 0; j < sub_len; j++) {
if (path_max - used < 2) {
truncated = true;
break;
}
path[used++] = sub[j];
}
}

va_end(ap);
path[used] = '\0';

if (truncated) {
LCH_LOG_ERROR("Failed to join paths: Truncation error.");
return false;
}
return true;
}

/******************************************************************************/

bool LCH_FileDelete(const char *const filename) {
if (unlink(filename) != 0) {
LCH_LOG_ERROR("Failed to delete file '%s': %s", filename, strerror(errno));
return false;
}
return true;
}

bool LCH_FileCreateParentDirectories(const char *const filename) {
assert(filename != NULL);

char fcopy[strlen(filename) + 1];
strcpy(fcopy, filename);
char *parent = dirname(fcopy);

LCH_List *const dirs = LCH_ListCreate();
struct stat sb;

while (stat(parent, &sb) == -1) {
char *const dir = LCH_StringDuplicate(parent);
if (dir == NULL) {
LCH_ListDestroy(dirs);
return false;
}

if (!LCH_ListAppend(dirs, dir, free)) {
free(dir);
LCH_ListDestroy(dirs);
return false;
}

parent = dirname(parent);
}

const size_t num_dirs = LCH_ListLength(dirs);
for (size_t i = num_dirs; i > 0; i--) {
char *const dir = (char *)LCH_ListGet(dirs, i - 1);
if (mkdir(dir, (mode_t)0700) == -1) {
LCH_LOG_ERROR("Failed to create parent directory '%s' for file '%s': %s",
dir, filename, strerror(errno));
LCH_ListDestroy(dirs);
return false;
}
LCH_LOG_VERBOSE("Created directory '%s' with mode %o", dir, (mode_t)0700);
}
LCH_ListDestroy(dirs);
return true;
}
Loading

0 comments on commit 1767973

Please sign in to comment.