static int _merge_and_write_statedb(CSYNC *ctx) { struct timespec start, finish; char errbuf[256] = {0}; int jwritten = 0; int rc = 0; /* 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); ctx->status_code = CSYNC_STATUS_MERGE_FILETREE_ERROR; rc = -1; } else { csync_gettime(&start); /* write the statedb to disk */ rc = csync_statedb_write(ctx, ctx->statedb.db); if (rc == 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); ctx->status_code = CSYNC_STATUS_STATEDB_WRITE_ERROR; rc = -1; } } } rc = csync_statedb_close(ctx->statedb.file, ctx->statedb.db, jwritten); ctx->statedb.db = NULL; if (rc < 0) { CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "ERR: closing of statedb failed."); } } return rc; }
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; }