Skip to content

Commit

Permalink
Changes needed to make leech packaging work
Browse files Browse the repository at this point in the history
Ticket: CFE-4379
Changelog: Title
Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
  • Loading branch information
larsewi committed Apr 18, 2024
1 parent bded12b commit 512ab49
Show file tree
Hide file tree
Showing 19 changed files with 255 additions and 183 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/acceptance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Bootstrap project
run: ./bootstrap.sh
- name: Configure project
run: ./configure --enable-debug
run: ./configure --enable-debug=yes --with-csv-module=yes --with-psql-module=yes
- name: Compile project
run: make
- name: Run acceptance tests
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/c.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Bootstrap project
run: ./bootstrap.sh
- name: Configure project with gcc compiler
run: ./configure CC=gcc --enable-debug
run: ./configure CC=gcc --enable-debug=yes --with-csv-module --with-psql-module
- name: Compile project
run: make
- name: Run tests
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Bootstrap project
run: ./bootstrap.sh
- name: Configure project with g++ compiler
run: ./configure CC=g++ --enable-debug
run: ./configure CC=g++ --enable-debug=yes --with-psql-module=yes --with-csv-module=yes
- name: Compile project
run: make
- name: Run tests
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Bootstrap project
run: ./bootstrap.sh
- name: Configure project
run: ./configure --enable-debug
run: ./configure --enable-debug=yes --with-csv-module=yes --with-psql-module=yes
- name: Compile project
run: make
- name: Run pytest
Expand Down
7 changes: 6 additions & 1 deletion bin/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
#include "common.h"
#include "diff.h"
#include "history.h"
#include "libpq-fe.h"
#include "patch.h"
#include "rebase.h"

#ifdef HAVE_LIBPQ
#include "libpq-fe.h"
#endif // HAVE_LIBPQ

enum OPTION_VALUE {
OPTION_WORKDIR = 1,
OPTION_INFORM,
Expand Down Expand Up @@ -95,7 +98,9 @@ int main(int argc, char *argv[]) {
* doesn't find any references to it. Adding a reference to one of the symbols
* here, seems to fix the issue.
*/
#ifdef HAVE_LIBPQ
PQlibVersion();
#endif // HAVE_LIBPQ

int opt;
while ((opt = getopt_long(argc, argv, "+", OPTIONS, NULL)) != -1) {
Expand Down
12 changes: 11 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ AC_CHECK_FUNCS([memmove memset strchr strspn strdup strerror strndup mkdir rmdir
# Configure arguments.
AC_ARG_ENABLE(debug, AS_HELP_STRING([--enable-debug], [enable debugging]),
[debug=$enableval], [debug=no])
AC_ARG_WITH([psql-module], AS_HELP_STRING([--with-psql-module], [build PostgreSQL module]))
AC_ARG_WITH([csv-module], AS_HELP_STRING([--with-csv-module], [build CSV module]))

# Automake conditionals.
AM_CONDITIONAL([NDEBUG], [test x"$debug" = x"no"])
AM_CONDITIONAL([PSQL_MODULE], [test x"$psql-module" = x"yes"])
AM_CONDITIONAL([CSV_MODULE], [test x"$csv-module" = x"yes"])

# Config constants.
AC_DEFINE([LCH_DEFAULT_MAX_CHAIN_LENGTH], 2048, [Default max chain length used in block garbage collector])
Expand All @@ -49,7 +53,13 @@ AC_DEFINE([LCH_LIST_CAPACITY], 256, [Initial list capacity allocated by leech])

# pkg-check modules.
PKG_CHECK_MODULES([CHECK], [check])
PKG_CHECK_MODULES([PSQL], [libpq])
AS_IF([test "x$with_psql_module" = "xyes"], [
PKG_CHECK_MODULES([PSQL], [libpq],
[AC_DEFINE([HAVE_LIBPQ], [1],
[Define to 1 if you have the <libpq-fe.h> header file.]
)
])
])

AC_CONFIG_FILES([Makefile lib/Makefile bin/Makefile tests/Makefile])
AC_OUTPUT
21 changes: 13 additions & 8 deletions lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ AM_CPPFLAGS = -include config.h

include_HEADERS = $(top_builddir)/lib/leech.h
lib_LTLIBRARIES = libleech.la
pkglib_LTLIBRARIES = leech_csv.la leech_psql.la

leech_csv_la_SOURCES = leech_csv.c
leech_csv_la_LDFLAGS = -avoid-version -module -shared -export-dynamic

leech_psql_la_SOURCES = leech_psql.c
leech_psql_la_LDFLAGS = -avoid-version -module -shared -export-dynamic
leech_psql_la_CFLAGS = @PSQL_CFLAGS@

pkglib_LTLIBRARIES =
if CSV_MODULE
pkglib_LTLIBRARIES += leech_csv.la
leech_csv_la_SOURCES = leech_csv.c
leech_csv_la_LDFLAGS = -avoid-version -module -shared -export-dynamic
endif
if PSQL_MODULE
pkglib_LTLIBRARIES += leech_psql.la
leech_psql_la_SOURCES = leech_psql.c
leech_psql_la_LDFLAGS = -avoid-version -module -shared -export-dynamic
leech_psql_la_CFLAGS = @PSQL_CFLAGS@
endif

libleech_la_SOURCES = leech.c \
block.h block.c \
buffer.h buffer.c \
Expand Down
1 change: 1 addition & 0 deletions lib/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <limits.h>
#include <time.h>

#include "definitions.h"
#include "files.h"
#include "head.h"
#include "logger.h"
Expand Down
76 changes: 2 additions & 74 deletions lib/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,11 @@
#include <stdbool.h>
#include <stdlib.h>

typedef struct LCH_Buffer LCH_Buffer;
#include "leech.h"

/**
* @brief create a byte buffer.
* @note buffer is always null terminated and must be freed with
* LCH_BufferDestroy.
* @return byte buffer.
* Put private LCH_Buffer functions here:
*/
LCH_Buffer *LCH_BufferCreate(void);

/**
* @brief append a byte to the buffer.
* @param[in] buffer buffer.
* @param[in] byte byte to append.
* @return false in case of error.
*/
bool LCH_BufferAppend(LCH_Buffer *buffer, char byte);

/**
* @brief format- and print string to byte buffer.
* @note buffer capacity is expanded if need be.
* @param[in] buffer buffer.
* @param[in] format format string.
* @return false in case of failure.
*/
bool LCH_BufferPrintFormat(LCH_Buffer *buffer, const char *format, ...);

/**
* @brief get length of buffer.
* @param[in] buffer buffer.
* @return length of buffer excluding the terminating null byte.
*/
size_t LCH_BufferLength(const LCH_Buffer *buffer);

/**
* @brief destroy buffer.
* @note noop if buffer is NULL.
* @param[in] buffer buffer.
*/
void LCH_BufferDestroy(void *buffer);

/**
* @brief get buffer.
* @param [in] buffer buffer.
* @return pointer to internal buffer.
*/
const char *LCH_BufferData(const LCH_Buffer *buffer);

/**
* @brief allocate memory in buffer.
Expand Down Expand Up @@ -93,40 +51,10 @@ bool LCH_BufferHexToBytes(LCH_Buffer *bytes, const LCH_Buffer *hex);
*/
bool LCH_BufferUnicodeToUTF8(LCH_Buffer *const buffer, const char *in);

void LCH_BufferChop(LCH_Buffer *const buffer, size_t offset);

/**
* @brief Get pointer to internal buffer and destroy surrounding data structure.
* @param buffer Buffer.
* @return Pointer to internal char buffer.
* @note Returned buffer must be freed with free(3). If you don't want to
* destroy the surrounding data structure, you can use LCH_BufferData()
* instead.
*/
char *LCH_BufferToString(LCH_Buffer *buffer);

/**
* @brief Create a buffer containing string.
* @param str String content of created buffer.
* @return Buffer containing string.
* @note String must be terminated by the NULL-byte.
*/
LCH_Buffer *LCH_BufferFromString(const char *str);

const LCH_Buffer *LCH_BufferStaticFromString(const char *str);

bool LCH_BufferReadFile(LCH_Buffer *buffer, const char *filename);

bool LCH_BufferWriteFile(const LCH_Buffer *buffer, const char *filename);

void LCH_BufferTrim(LCH_Buffer *buffer, char ch);

bool LCH_BufferAppendBuffer(LCH_Buffer *buffer, const LCH_Buffer *append);

bool LCH_BufferEqual(const LCH_Buffer *self, const LCH_Buffer *other);

int LCH_BufferCompare(const LCH_Buffer *self, const LCH_Buffer *other);

LCH_Buffer *LCH_BufferDuplicate(const LCH_Buffer *buffer);

#endif // _LEECH_BUFFER_H
3 changes: 0 additions & 3 deletions lib/definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,4 @@
#define LCH_PATH_SEP '/'
#endif // _WIN32

typedef int (*LCH_CompareFn)(const void *, const void *);
typedef void *(*LCH_DuplicateFn)(const void *);

#endif // _LEECH_DEFINITIONS_H
36 changes: 20 additions & 16 deletions lib/leech.c
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,7 @@ LCH_Buffer *LCH_Rebase(const char *const work_dir) {
return buffer;
}

static bool HistoryAppendRecord(
const LCH_Instance *const instance,
static bool HistoryAppendRecord(const LCH_Instance *const instance,
const char *const table_id,
const LCH_Json *const history,
const char *const block_id,
Expand Down Expand Up @@ -650,8 +649,10 @@ static bool HistoryAppendRecord(
return false;
}

const LCH_TableInfo *const table_info = LCH_InstanceGetTable(instance, table_id);
const LCH_List *const subsidiary_names = LCH_TableInfoGetSubsidiaryFields(table_info);
const LCH_TableInfo *const table_info =
LCH_InstanceGetTable(instance, table_id);
const LCH_List *const subsidiary_names =
LCH_TableInfoGetSubsidiaryFields(table_info);

const size_t num_fields = LCH_ListLength(subsidiary_fields);
assert(num_fields == LCH_ListLength(subsidiary_names));
Expand Down Expand Up @@ -744,8 +745,8 @@ static bool HistoryFindRecord(const LCH_Instance *const instance,

if (timestamp >= to) {
// Continue without recording history (yet)
if (!HistoryFindRecord(instance, history, table_id, primary_key, parent_id, from,
to)) {
if (!HistoryFindRecord(instance, history, table_id, primary_key, parent_id,
from, to)) {
LCH_JsonDestroy(block);
return false;
}
Expand All @@ -768,7 +769,7 @@ static bool HistoryFindRecord(const LCH_Instance *const instance,
return false;
}

{ // Skip tables that does not match table identifier
{ // Skip tables that does not match table identifier
const LCH_Buffer *const key = LCH_BufferStaticFromString("id");
const LCH_Buffer *const tid = LCH_JsonObjectGetString(delta, key);
if (tid == NULL) {
Expand Down Expand Up @@ -807,8 +808,8 @@ static bool HistoryFindRecord(const LCH_Instance *const instance,
return false;
}

if (!HistoryAppendRecord(instance, table_id, history, block_id, timestamp, "insert",
subsidiary_value)) {
if (!HistoryAppendRecord(instance, table_id, history, block_id, timestamp,
"insert", subsidiary_value)) {
LCH_JsonDestroy(block);
return false;
}
Expand All @@ -820,8 +821,8 @@ static bool HistoryFindRecord(const LCH_Instance *const instance,
return false;
}

if (!HistoryAppendRecord(instance, table_id, history, block_id, timestamp, "delete",
subsidiary_value)) {
if (!HistoryAppendRecord(instance, table_id, history, block_id, timestamp,
"delete", subsidiary_value)) {
LCH_JsonDestroy(block);
return false;
}
Expand All @@ -833,15 +834,16 @@ static bool HistoryFindRecord(const LCH_Instance *const instance,
return false;
}

if (!HistoryAppendRecord(instance, table_id, history, block_id, timestamp, "update",
subsidiary_value)) {
if (!HistoryAppendRecord(instance, table_id, history, block_id, timestamp,
"update", subsidiary_value)) {
LCH_JsonDestroy(block);
return false;
}
}
}

if (!HistoryFindRecord(instance, history, table_id, primary_key, parent_id, from, to)) {
if (!HistoryFindRecord(instance, history, table_id, primary_key, parent_id,
from, to)) {
LCH_JsonDestroy(block);
return false;
}
Expand Down Expand Up @@ -884,7 +886,8 @@ LCH_Buffer *LCH_History(const char *const work_dir, const char *const table_id,
{
const LCH_TableInfo *const table_info =
LCH_InstanceGetTable(instance, table_id);
const LCH_List *const primary_names = LCH_TableInfoGetPrimaryFields(table_info);
const LCH_List *const primary_names =
LCH_TableInfoGetPrimaryFields(table_info);

LCH_Json *const primary = LCH_JsonObjectCreate();
if (primary == NULL) {
Expand Down Expand Up @@ -968,7 +971,8 @@ LCH_Buffer *LCH_History(const char *const work_dir, const char *const table_id,
return NULL;
}

if (!HistoryFindRecord(instance, history, table_id, primary, block_id, from, to)) {
if (!HistoryFindRecord(instance, history, table_id, primary, block_id, from,
to)) {
LCH_BufferDestroy(primary);
free(block_id);
LCH_BufferDestroy(response);
Expand Down
Loading

0 comments on commit 512ab49

Please sign in to comment.