static void check_c_rbtree_insert_delete(void **state) { c_rbtree_t *tree = NULL; c_rbnode_t *node = NULL; test_t *testdata = NULL; int rc; (void) state; /* unused */ rc = c_rbtree_create(&tree, key_cmp, data_cmp); assert_int_equal(rc, 0); testdata = malloc(sizeof(test_t)); testdata->key = 42; rc = c_rbtree_insert(tree, (void *) testdata); assert_int_equal(rc, 0); node = c_rbtree_head(tree); assert_non_null(node); testdata = c_rbtree_node_data(node); SAFE_FREE(testdata); rc = c_rbtree_node_delete(node); assert_int_equal(rc, 0); c_rbtree_free(tree); }
static void teardown(void **state) { c_rbtree_t *tree = *state; c_rbtree_destroy(tree, destructor); c_rbtree_free(tree); *state = NULL; }
static void check_c_rbtree_free_null(void **state) { int rc; (void) state; /* unused */ rc = c_rbtree_free(NULL); assert_int_equal(rc, -1); }
/* reset all the list to empty. * used by csync_commit and csync_destroy */ static void _csync_clean_ctx(CSYNC *ctx) { /* 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); } csync_rename_destroy(ctx); /* free memory */ c_rbtree_free(ctx->local.tree); c_rbtree_free(ctx->remote.tree); SAFE_FREE(ctx->statedb.file); SAFE_FREE(ctx->remote.root_perms); }
static void check_c_rbtree_dup(void **state) { c_rbtree_t *tree = *state; c_rbtree_t *duptree = NULL; int rc = -1; duptree = c_rbtree_dup(tree); assert_non_null(duptree); rc = c_rbtree_check_sanity(duptree); assert_int_equal(rc, 0); c_rbtree_free(duptree); }
static void check_c_rbtree_create_free(void **state) { c_rbtree_t *tree = NULL; int rc; (void) state; /* unused */ rc = c_rbtree_create(&tree, key_cmp, data_cmp); assert_int_equal(rc, 0); assert_int_equal(tree->size, 0); rc = c_rbtree_free(tree); assert_int_equal(rc, 0); }
/* reset all the list to empty. * used by csync_commit and csync_destroy */ static void _csync_clean_ctx(CSYNC *ctx) { c_list_t * walk; /* 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); } csync_rename_destroy(ctx); for (walk = c_list_last(ctx->local.ignored_cleanup); walk != NULL; walk = c_list_prev(walk)) { SAFE_FREE(walk->data); } for (walk = c_list_last(ctx->remote.ignored_cleanup); walk != NULL; walk = c_list_prev(walk)) { SAFE_FREE(walk->data); } /* free memory */ c_rbtree_free(ctx->local.tree); c_list_free(ctx->local.list); c_list_free(ctx->local.ignored_cleanup); c_rbtree_free(ctx->remote.tree); c_list_free(ctx->remote.list); c_list_free(ctx->remote.ignored_cleanup); ctx->remote.list = 0; ctx->local.list = 0; ctx->remote.ignored_cleanup = 0; ctx->local.ignored_cleanup = 0; SAFE_FREE(ctx->statedb.file); }
int csync_destroy(CSYNC *ctx) { struct timespec start, finish; char *lock = NULL; char errbuf[256] = {0}; int jwritten = 0; if (ctx == NULL) { errno = EBADF; return -1; } ctx->error_code = CSYNC_ERR_NONE; csync_vio_shutdown(ctx); /* if we have a statedb */ if (ctx->statedb.db != NULL) { /* and we have successfully synchronized */ if (ctx->status >= CSYNC_STATUS_DONE) { /* merge trees */ if (csync_merge_file_trees(ctx) < 0) { strerror_r(errno, errbuf, sizeof(errbuf)); CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "Unable to merge trees: %s", errbuf); } else { csync_gettime(&start); /* write the statedb to disk */ if (csync_statedb_write(ctx) == 0) { jwritten = 1; csync_gettime(&finish); CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Writing the statedb of %zu files to disk took %.2f seconds", c_rbtree_size(ctx->local.tree), c_secdiff(finish, start)); } else { strerror_r(errno, errbuf, sizeof(errbuf)); CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "Unable to write statedb: %s", errbuf); } } } csync_statedb_close(ctx, ctx->statedb.file, jwritten); } /* clear exclude list */ csync_exclude_destroy(ctx); #ifndef _WIN32 /* remove the lock file */ if (asprintf(&lock, "%s/%s", ctx->options.config_dir, CSYNC_LOCK_FILE) > 0) { csync_lock_remove(lock); } #endif /* stop logging */ csync_log_fini(); /* 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); SAFE_FREE(ctx->local.uri); SAFE_FREE(ctx->remote.uri); SAFE_FREE(ctx->options.config_dir); SAFE_FREE(ctx->statedb.file); SAFE_FREE(ctx); SAFE_FREE(lock); return 0; }
int csync_destroy(CSYNC *ctx) { char *lock = NULL; int rc; if (ctx == NULL) { errno = EBADF; return -1; } ctx->status_code = CSYNC_STATUS_OK; csync_vio_shutdown(ctx); rc = _merge_and_write_statedb(ctx); if (rc < 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 */ csync_exclude_destroy(ctx); #ifndef _WIN32 /* remove the lock file */ rc = asprintf(&lock, "%s/%s", ctx->options.config_dir, CSYNC_LOCK_FILE); if (rc > 0) { csync_lock_remove(lock); } #endif /* destroy the rbtrees */ rc = c_rbtree_size(ctx->local.tree); if (rc > 0) { c_rbtree_destroy(ctx->local.tree, _tree_destructor); } rc = c_rbtree_size(ctx->remote.tree); if (rc > 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); SAFE_FREE(ctx->local.uri); SAFE_FREE(ctx->remote.uri); SAFE_FREE(ctx->options.config_dir); SAFE_FREE(ctx->statedb.file); SAFE_FREE(ctx->error_string); #ifdef WITH_ICONV c_close_iconv(); #endif SAFE_FREE(ctx); SAFE_FREE(lock); return 0; }
int csync_commit(CSYNC *ctx) { int rc = 0; if (ctx == NULL) { return -1; } ctx->status_code = CSYNC_STATUS_OK; rc = _merge_and_write_statedb(ctx); if (rc < 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? */ } rc = csync_vio_commit(ctx); if (rc < 0) { CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "commit failed: %s", ctx->error_string ? ctx->error_string : ""); goto out; } /* destroy the rbtrees */ rc = c_rbtree_size(ctx->local.tree); if (rc > 0) { c_rbtree_destroy(ctx->local.tree, _tree_destructor); } rc = c_rbtree_size(ctx->remote.tree); if (rc > 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); ctx->local.list = 0; ctx->remote.list = 0; /* create/load statedb */ rc = csync_is_statedb_disabled(ctx); if (rc == 0) { if (ctx->statedb.file == NULL) { 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); rc = csync_statedb_load(ctx, ctx->statedb.file, &ctx->statedb.db); if (rc < 0) { ctx->status_code = CSYNC_STATUS_STATEDB_LOAD_ERROR; goto out; } } /* Create new trees */ rc = c_rbtree_create(&ctx->local.tree, _key_cmp, _data_cmp); if (rc < 0) { ctx->status_code = CSYNC_STATUS_TREE_ERROR; goto out; } rc = c_rbtree_create(&ctx->remote.tree, _key_cmp, _data_cmp); if (rc < 0) { ctx->status_code = CSYNC_STATUS_TREE_ERROR; goto out; } /* reset the progress */ ctx->progress.file_count = 0; ctx->progress.current_file_no = 0; ctx->progress.byte_sum = 0; ctx->progress.byte_current = 0; ctx->status = CSYNC_STATUS_INIT; SAFE_FREE(ctx->error_string); rc = 0; out: return rc; }