Added csync_commit to be able to reuse the csync context.

This commit is contained in:
Klaas Freitag 2013-04-20 12:07:20 +03:00 committed by Andreas Schneider
parent 85b565fcbe
commit 8b65e8fbf3
3 changed files with 106 additions and 13 deletions

View file

@ -629,20 +629,11 @@ static void _tree_destructor(void *data) {
SAFE_FREE(freedata);
}
int csync_destroy(CSYNC *ctx) {
static int _merge_and_write_statedb(CSYNC *ctx) {
struct timespec start, finish;
char *lock = NULL;
char errbuf[256] = {0};
int jwritten = 0;
if (ctx == NULL) {
errno = EBADF;
return -1;
}
ctx->status_code = CSYNC_STATUS_OK;
csync_vio_shutdown(ctx);
int rc = 0;
/* if we have a statedb */
if (ctx->statedb.db != NULL) {
@ -654,6 +645,7 @@ int csync_destroy(CSYNC *ctx) {
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "Unable to merge trees: %s",
errbuf);
ctx->status_code = CSYNC_STATUS_MERGE_FILETREE_ERROR;
rc = -1;
} else {
csync_gettime(&start);
/* write the statedb to disk */
@ -668,10 +660,102 @@ int csync_destroy(CSYNC *ctx) {
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "Unable to write statedb: %s",
errbuf);
ctx->status_code = CSYNC_STATUS_STATEDB_WRITE_ERROR;
rc = -1;
}
}
}
csync_statedb_close(ctx, ctx->statedb.file, jwritten);
if (csync_statedb_close(ctx, ctx->statedb.file, jwritten) < 0) {
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "ERR: closing of statedb failed.");
rc = -1;
}
}
return rc;
}
int csync_commit(CSYNC *ctx) {
int rc = 0;
ctx->status_code = CSYNC_STATUS_OK;
if (_merge_and_write_statedb(ctx) < 0) {
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Merge and Write database failed!");
if (ctx->status_code == CSYNC_STATUS_OK) {
ctx->status_code = CSYNC_STATUS_STATEDB_WRITE_ERROR;
}
rc = 1; /* Set to soft error. */
/* The other steps happen anyway, what else can we do? */
}
/* destroy the rbtrees */
if (c_rbtree_size(ctx->local.tree) > 0) {
c_rbtree_destroy(ctx->local.tree, _tree_destructor);
}
if (c_rbtree_size(ctx->remote.tree) > 0) {
c_rbtree_destroy(ctx->remote.tree, _tree_destructor);
}
/* free memory */
c_rbtree_free(ctx->local.tree);
c_list_free(ctx->local.list);
c_rbtree_free(ctx->remote.tree);
c_list_free(ctx->remote.list);
/* create/load statedb */
if (! csync_is_statedb_disabled(ctx)) {
if(!ctx->statedb.file) {
rc = asprintf(&ctx->statedb.file, "%s/.csync_journal.db",
ctx->local.uri);
if (rc < 0) {
ctx->status_code = CSYNC_STATUS_MEMORY_ERROR;
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Failed to assemble statedb file name.");
goto out;
}
}
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Journal: %s", ctx->statedb.file);
if (csync_statedb_load(ctx, ctx->statedb.file) < 0) {
ctx->status_code = CSYNC_STATUS_STATEDB_LOAD_ERROR;
rc = -1;
goto out;
}
}
/* Create new trees */
if (c_rbtree_create(&ctx->local.tree, _key_cmp, _data_cmp) < 0) {
ctx->status_code = CSYNC_STATUS_TREE_ERROR;
rc = -1;
goto out;
}
if (c_rbtree_create(&ctx->remote.tree, _key_cmp, _data_cmp) < 0) {
ctx->status_code = CSYNC_STATUS_TREE_ERROR;
rc = -1;
goto out;
}
ctx->status = CSYNC_STATUS_INIT;
out:
return rc;
}
int csync_destroy(CSYNC *ctx) {
char *lock = NULL;
if (ctx == NULL) {
errno = EBADF;
return -1;
}
ctx->status_code = CSYNC_STATUS_OK;
csync_vio_shutdown(ctx);
if (_merge_and_write_statedb(ctx) < 0) {
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "destroy: Merge and Write database failed!");
if (ctx->status_code == CSYNC_STATUS_OK) {
ctx->status_code = CSYNC_STATUS_STATEDB_WRITE_ERROR;
}
/* The other steps happen anyway, what else can we do? */
}
/* clear exclude list */
@ -810,7 +894,6 @@ int csync_set_auth_callback(CSYNC *ctx, csync_auth_callback cb) {
fprintf(stderr, "This function must be called before initialization.");
return -1;
}
ctx->callbacks.auth_function = cb;
return 0;

View file

@ -245,6 +245,15 @@ int csync_reconcile(CSYNC *ctx);
*/
int csync_propagate(CSYNC *ctx);
/**
* @brief Commit the sync results to journal
*
* @param ctx The context to commit.
*
* @return 0 on success, less than 0 if an error occured.
*/
int csync_commit(CSYNC *ctx);
/**
* @brief Destroy the csync context
*

View file

@ -164,6 +164,7 @@ CSYNC_STATUS csync_errno_to_status(int error, CSYNC_STATUS default_status)
CSYNC_ERR_LOG,
CSYNC_ERR_LOCK,
CSYNC_ERR_STATEDB_LOAD,
CSYNC_ERR_STATEDB_WRITE,
CSYNC_ERR_MODULE,
CSYNC_ERR_TIMESKEW,
CSYNC_ERR_FILESYSTEM,