From ff7e28f82cd29cd91e429b49834685d4982a5fcc Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Fri, 29 Feb 2008 11:41:15 +0100 Subject: [PATCH] Completly hide the csync structure for the user of the library. So it easier to change the API/ABI. --- client/csync_client.c | 6 ++-- src/csync.c | 23 +++----------- src/csync.h | 54 ++++++--------------------------- src/csync_journal.c | 20 ++++++------ src/csync_private.h | 34 ++++++++++++++++----- tests/csync_tests/check_csync.c | 8 ++--- 6 files changed, 57 insertions(+), 88 deletions(-) diff --git a/client/csync_client.c b/client/csync_client.c index da746a5a5..7d05f27a8 100644 --- a/client/csync_client.c +++ b/client/csync_client.c @@ -108,8 +108,8 @@ int main(int argc, char **argv) { exit(1); } - csync->init(csync); - printf("Version: %s\n", csync->version()); + csync_init(csync); + printf("Version: %s\n", csync_version()); if (arguments.update) { } @@ -120,7 +120,7 @@ int main(int argc, char **argv) { if (arguments.propagate) { } - csync->destroy(csync); + csync_destroy(csync); return 0; } diff --git a/src/csync.c b/src/csync.c index f4895630f..f49d1b770 100644 --- a/src/csync.c +++ b/src/csync.c @@ -44,27 +44,15 @@ int csync_create(CSYNC **csync) { return -1; } - ctx->internal = c_malloc(sizeof(csync_internal_t)); - if (ctx->internal == NULL) { - SAFE_FREE(ctx); - errno = ENOMEM; - return -1; - } - ctx->options.max_depth = MAX_DEPTH; ctx->options.max_time_difference = MAX_TIME_DIFFERENCE; if (asprintf(&ctx->options.config_dir, "%s/%s", getenv("HOME"), CSYNC_CONF_DIR) < 0) { - SAFE_FREE(ctx->internal); SAFE_FREE(ctx); errno = ENOMEM; return -1; } - ctx->init = csync_init; - ctx->destroy = csync_destroy; - ctx->version = csync_version; - *csync = ctx; return 0; } @@ -75,13 +63,13 @@ int csync_init(CSYNC *ctx) { char *journal = NULL; char *lock = NULL; - if (ctx == NULL || ctx->internal == NULL) { + if (ctx == NULL) { errno = EBADF; return -1; } /* Do not initialize twice */ - if (ctx->internal->_initialized) { + if (ctx->initialized) { return 1; } @@ -136,7 +124,7 @@ int csync_init(CSYNC *ctx) { goto out; } - ctx->internal->_initialized = 1; + ctx->initialized = 1; rc = 0; @@ -152,8 +140,8 @@ int csync_destroy(CSYNC *ctx) { /* TODO: write journal */ - if (ctx->internal->_journal) { - sqlite3_close(ctx->internal->_journal); + if (ctx->journal) { + sqlite3_close(ctx->journal); /* TODO if we successfully synchronized, overwrite the original journal */ } @@ -165,7 +153,6 @@ int csync_destroy(CSYNC *ctx) { SAFE_FREE(ctx->options.config_dir); - SAFE_FREE(ctx->internal); SAFE_FREE(ctx); SAFE_FREE(lock); diff --git a/src/csync.h b/src/csync.h index 56601ba94..68c518bd1 100644 --- a/src/csync.h +++ b/src/csync.h @@ -45,19 +45,6 @@ extern "C" { #define CSYNC_VERSION_PATCH 0 #define CSYNC_VERSION_STRING "csync version 0.1.0" -#undef __P -#define __P(protos) protos - -/** - * How deep to scan directories. - */ -#define MAX_DEPTH 50 - -/** - * Maximum time difference between two replicas in seconds - */ -#define MAX_TIME_DIFFERENCE 10 - /* * csync file declarations */ @@ -72,50 +59,27 @@ extern "C" { * Forward declarations */ struct csync_s; typedef struct csync_s CSYNC; -struct csync_config_s; typedef struct csync_config_s csync_config_t; -struct csync_internal_s; typedef struct csync_internal_s csync_internal_t; - -/** - * @brief csync public structure - */ -struct csync_s { - int (*init) __P((CSYNC *)); - int (*update) __P((CSYNC *)); - int (*reconcile) __P((CSYNC *)); - int (*propagate) __P((CSYNC *)); - int (*destroy) __P((CSYNC *)); - const char *(*version) __P((void)); - - struct { - int max_depth; - int max_time_difference; - char *config_dir; - } options; - - csync_internal_t *internal; -}; - /** * @brief Allocate a csync context. * - * @param ctx context variable to allocate + * @param ctx context variable to allocate * - * @return 0 on success, less than 0 if an error occured with errno set. + * @return 0 on success, less than 0 if an error occured with errno set. */ -int csync_create __P((CSYNC **)); +int csync_create(CSYNC **csync); -int csync_init __P((CSYNC *)); +int csync_init(CSYNC *ctx); -int csync_update __P((CSYNC *)); +int csync_update(CSYNC *ctx); -int csync_reconcile __P((CSYNC *)); +int csync_reconcile(CSYNC *ctx); -int csync_propagate __P((CSYNC *)); +int csync_propagate(CSYNC *ctx); -int csync_destroy __P((CSYNC *)); +int csync_destroy(CSYNC *ctx); -const char *csync_version __P((void)); +const char *csync_version(void); #ifdef __cplusplus } diff --git a/src/csync_journal.c b/src/csync_journal.c index d74eb802b..022304f9a 100644 --- a/src/csync_journal.c +++ b/src/csync_journal.c @@ -107,16 +107,16 @@ int csync_journal_load(CSYNC *ctx, const char *journal) { } /* Open the temporary database */ - if (sqlite3_open(journal_tmp, &ctx->internal->_journal) != SQLITE_OK) { + if (sqlite3_open(journal_tmp, &ctx->journal) != SQLITE_OK) { rc = -1; goto out; } if (csync_journal_is_empty(ctx)) { CSYNC_LOG(CSYNC_LOG_PRIORITY_NOTICE, "Journal doesn't exist"); - ctx->internal->_journal_exists = 0; + ctx->journal_exists = 0; } else { - ctx->internal->_journal_exists = 1; + ctx->journal_exists = 1; } out: @@ -146,14 +146,14 @@ c_strlist_t *csync_journal_query(CSYNC *ctx, const char *statement) { usleep(100000); CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "sqlite3_prepare: BUSY counter: %d", busy_count); } - err = sqlite3_prepare(ctx->internal->_journal, statement, -1, &stmt, &tail); + err = sqlite3_prepare(ctx->journal, statement, -1, &stmt, &tail); } while (err == SQLITE_BUSY && busy_count ++ < 120); if (err != SQLITE_OK) { if (err == SQLITE_BUSY) { CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "Gave up waiting for lock to clear"); } - CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN, "sqlite3_compile error: %s - on query %s", sqlite3_errmsg(ctx->internal->_journal), statement); + CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN, "sqlite3_compile error: %s - on query %s", sqlite3_errmsg(ctx->journal), statement); result = c_strlist_new(1); break; } else { @@ -202,7 +202,7 @@ c_strlist_t *csync_journal_query(CSYNC *ctx, const char *statement) { rc = sqlite3_finalize(stmt); if (err != SQLITE_DONE && rc != SQLITE_SCHEMA) { - CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "sqlite_step error: %s - on query: %s", sqlite3_errmsg(ctx->internal->_journal), statement); + CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "sqlite_step error: %s - on query: %s", sqlite3_errmsg(ctx->journal), statement); result = c_strlist_new(1); } @@ -238,14 +238,14 @@ int csync_journal_insert(CSYNC *ctx, const char *statement) { usleep(100000); CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "sqlite3_prepare: BUSY counter: %d", busy_count); } - err = sqlite3_prepare(ctx->internal->_journal, statement, -1, &stmt, &tail); + err = sqlite3_prepare(ctx->journal, statement, -1, &stmt, &tail); } while (err == SQLITE_BUSY && busy_count++ < 120); if (err != SQLITE_OK) { if (err == SQLITE_BUSY) { CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "Gave up waiting for lock to clear"); } - CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "sqlite3_compile error: %s on query %s", sqlite3_errmsg(ctx->internal->_journal), statement); + CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "sqlite3_compile error: %s on query %s", sqlite3_errmsg(ctx->journal), statement); break; } else { busy_count = 0; @@ -277,7 +277,7 @@ int csync_journal_insert(CSYNC *ctx, const char *statement) { rc = sqlite3_finalize(stmt); if (err != SQLITE_DONE && rc != SQLITE_SCHEMA) { - CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "sqlite_step error: %s on insert: %s", sqlite3_errmsg(ctx->internal->_journal), statement); + CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "sqlite_step error: %s on insert: %s", sqlite3_errmsg(ctx->journal), statement); } if (rc == SQLITE_SCHEMA) { @@ -292,6 +292,6 @@ int csync_journal_insert(CSYNC *ctx, const char *statement) { } } while (rc == SQLITE_SCHEMA && retry_count < 10); - return sqlite3_last_insert_rowid(ctx->internal->_journal); + return sqlite3_last_insert_rowid(ctx->journal); } diff --git a/src/csync_private.h b/src/csync_private.h index 0e9e48d37..dcd0de52e 100644 --- a/src/csync_private.h +++ b/src/csync_private.h @@ -33,8 +33,6 @@ #ifndef _CSYNC_PRIVATE_H #define _CSYNC_PRIVATE_H -#define _GNU_SOURCE /* asprintf */ -#include #include #include "c_lib.h" @@ -42,6 +40,16 @@ #include "csync_macros.h" +/** + * How deep to scan directories. + */ +#define MAX_DEPTH 50 + +/** + * Maximum time difference between two replicas in seconds + */ +#define MAX_TIME_DIFFERENCE 10 + /** * Maximum size of a buffer for transfer */ @@ -52,12 +60,22 @@ enum csync_replica_e { REMOTE_REPLCIA }; -struct csync_internal_s { - c_rbtree_t *_local; - c_rbtree_t *_remote; - sqlite3 *_journal; - int _journal_exists; - int _initialized; +/** + * @brief csync public structure + */ +struct csync_s { + c_rbtree_t *local; + c_rbtree_t *remote; + sqlite3 *journal; + + struct { + int max_depth; + int max_time_difference; + char *config_dir; + } options; + + int journal_exists; + int initialized; }; /** diff --git a/tests/csync_tests/check_csync.c b/tests/csync_tests/check_csync.c index eba8227fb..83e8909bd 100644 --- a/tests/csync_tests/check_csync.c +++ b/tests/csync_tests/check_csync.c @@ -22,17 +22,17 @@ START_TEST (csync_create_test) fail_unless(csync->options.max_time_difference == MAX_TIME_DIFFERENCE, NULL); fail_unless(strcmp(csync->options.config_dir, CSYNC_CONF_DIR) > 0, NULL); - csync->destroy(csync); + csync_destroy(csync); } END_TEST START_TEST (csync_init_test) { - fail_unless(csync->init(csync) == 0, NULL); + fail_unless(csync_init(csync) == 0, NULL); - fail_unless(csync->internal->_initialized == 1, NULL); + fail_unless(csync->initialized == 1, NULL); - fail_unless(csync->init(csync) == 1, NULL); + fail_unless(csync_init(csync) == 1, NULL); } END_TEST