nextcloud-desktop/src/csync.c

961 lines
22 KiB
C
Raw Normal View History

2008-02-27 20:56:47 +03:00
/*
* libcsync -- a library to sync a directory with another
*
* Copyright (c) 2008 by Andreas Schneider <mail@cynapses.org>
2008-02-27 20:56:47 +03:00
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
2008-02-27 20:56:47 +03:00
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
2008-02-27 20:56:47 +03:00
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2008-02-27 20:56:47 +03:00
*/
2012-03-02 19:47:34 +04:00
#include "config.h"
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
2008-04-28 14:40:32 +04:00
2008-02-27 20:56:47 +03:00
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdbool.h>
2008-02-27 20:56:47 +03:00
#include "c_lib.h"
#include "csync_private.h"
2008-03-06 19:43:58 +03:00
#include "csync_config.h"
#include "csync_exclude.h"
#include "csync_lock.h"
#include "csync_statedb.h"
#include "csync_time.h"
#include "csync_util.h"
2012-03-02 16:38:39 +04:00
#include "csync_misc.h"
2008-02-27 20:56:47 +03:00
#include "csync_update.h"
2008-05-15 15:50:34 +04:00
#include "csync_reconcile.h"
#include "csync_propagate.h"
#include "vio/csync_vio.h"
2008-02-27 20:56:47 +03:00
#define CSYNC_LOG_CATEGORY_NAME "csync.api"
#include "csync_log.h"
static enum csync_error_codes_e _csync_errno;
2008-02-27 20:56:47 +03:00
static int _key_cmp(const void *key, const void *data) {
uint64_t a;
csync_file_stat_t *b;
2008-06-27 20:01:19 +04:00
a = *(uint64_t *) (key);
b = (csync_file_stat_t *) data;
if (a < b->phash) {
return -1;
} else if (a > b->phash) {
return 1;
}
return 0;
}
static int _data_cmp(const void *key, const void *data) {
csync_file_stat_t *a, *b;
a = (csync_file_stat_t *) key;
b = (csync_file_stat_t *) data;
if (a->phash < b->phash) {
return -1;
} else if (a->phash > b->phash) {
return 1;
}
return 0;
}
int csync_create(CSYNC **csync, const char *local, const char *remote) {
2008-02-27 20:56:47 +03:00
CSYNC *ctx;
2008-04-28 14:35:29 +04:00
size_t len = 0;
2012-03-02 16:38:39 +04:00
char *home;
int rc;
2008-02-27 20:56:47 +03:00
_csync_errno = CSYNC_ERR_NONE;
2008-02-27 20:56:47 +03:00
ctx = c_malloc(sizeof(CSYNC));
if (ctx == NULL) {
_csync_errno = CSYNC_ERR_MEM;
return -1;
}
2008-04-28 14:35:29 +04:00
/* remove trailing slashes */
len = strlen(local);
while(len > 0 && local[len - 1] == '/') --len;
ctx->local.uri = c_strndup(local, len);
if (ctx->local.uri == NULL) {
_csync_errno = CSYNC_ERR_MEM;
return -1;
}
2008-04-28 14:35:29 +04:00
/* remove trailing slashes */
len = strlen(remote);
while(len > 0 && remote[len - 1] == '/') --len;
ctx->remote.uri = c_strndup(remote, len);
if (ctx->remote.uri == NULL) {
SAFE_FREE(ctx->remote.uri);
_csync_errno = CSYNC_ERR_MEM;
2008-02-27 20:56:47 +03:00
return -1;
}
ctx->options.max_depth = MAX_DEPTH;
ctx->options.max_time_difference = MAX_TIME_DIFFERENCE;
2009-01-19 13:32:58 +03:00
ctx->options.unix_extensions = 0;
ctx->options.with_conflict_copys=false;
ctx->options.local_only_mode = false;
2008-02-27 20:56:47 +03:00
ctx->pwd.uid = getuid();
ctx->pwd.euid = geteuid();
2012-03-02 16:38:39 +04:00
home = csync_get_user_home_dir();
if (home == NULL) {
SAFE_FREE(ctx->local.uri);
SAFE_FREE(ctx->remote.uri);
SAFE_FREE(ctx);
errno = ENOMEM;
_csync_errno = CSYNC_ERR_MEM;
2012-03-02 16:38:39 +04:00
return -1;
}
rc = asprintf(&ctx->options.config_dir, "%s/%s", home, CSYNC_CONF_DIR);
SAFE_FREE(home);
if (rc < 0) {
SAFE_FREE(ctx->local.uri);
SAFE_FREE(ctx->remote.uri);
2008-02-27 20:56:47 +03:00
SAFE_FREE(ctx);
errno = ENOMEM;
_csync_errno = CSYNC_ERR_MEM;
2008-02-27 20:56:47 +03:00
return -1;
}
*csync = ctx;
return 0;
}
int csync_init(CSYNC *ctx) {
int rc;
time_t timediff = -1;
2008-02-27 20:56:47 +03:00
char *log = NULL;
char *exclude = NULL;
2008-02-27 20:56:47 +03:00
char *lock = NULL;
2008-03-06 19:43:58 +03:00
char *config = NULL;
#ifndef _WIN32
char errbuf[256] = {0};
#endif
_csync_errno = CSYNC_ERR_NONE;
if (ctx == NULL) {
_csync_errno = CSYNC_ERR_UNSPEC;
2008-02-27 20:56:47 +03:00
errno = EBADF;
return -1;
}
/* Do not initialize twice */
if (ctx->status & CSYNC_STATUS_INIT) {
2008-02-27 20:56:47 +03:00
return 1;
}
/* load log file */
if (csync_log_init() < 0) {
_csync_errno = CSYNC_ERR_LOG;
2008-02-27 20:56:47 +03:00
fprintf(stderr, "csync_init: logger init failed\n");
return -1;
}
/* create dir if it doesn't exist */
if (! c_isdir(ctx->options.config_dir)) {
c_mkdirs(ctx->options.config_dir, 0700);
}
if (asprintf(&log, "%s/%s", ctx->options.config_dir, CSYNC_LOG_FILE) < 0) {
_csync_errno = CSYNC_ERR_UNSPEC;
2008-02-27 20:56:47 +03:00
rc = -1;
goto out;
}
/* load log if it exists */
if (c_isfile(log)) {
csync_log_load(log);
} else {
2012-02-22 15:14:34 +04:00
#ifndef _WIN32
2008-03-06 19:43:58 +03:00
if (c_copy(SYSCONFDIR "/csync/" CSYNC_LOG_FILE, log, 0644) == 0) {
2008-02-27 20:56:47 +03:00
csync_log_load(log);
}
2012-02-22 15:14:34 +04:00
#endif
2008-02-27 20:56:47 +03:00
}
/* create lock file */
if (asprintf(&lock, "%s/%s", ctx->options.config_dir, CSYNC_LOCK_FILE) < 0) {
_csync_errno = CSYNC_ERR_UNSPEC;
2008-02-27 20:56:47 +03:00
rc = -1;
goto out;
}
2012-02-22 15:01:30 +04:00
#ifndef _WIN32
2008-02-27 20:56:47 +03:00
if (csync_lock(lock) < 0) {
_csync_errno = CSYNC_ERR_LOCK;
2008-02-27 20:56:47 +03:00
rc = -1;
goto out;
}
2012-02-22 15:01:30 +04:00
#endif
2008-02-27 20:56:47 +03:00
2008-03-06 19:43:58 +03:00
/* load config file */
if (asprintf(&config, "%s/%s", ctx->options.config_dir, CSYNC_CONF_FILE) < 0) {
rc = -1;
goto out;
}
if (csync_config_load(ctx, config) < 0) {
CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN, "Could not load config file %s, using defaults.", config);
2008-03-06 19:43:58 +03:00
}
2008-02-27 20:56:47 +03:00
2012-02-22 15:14:34 +04:00
#ifndef _WIN32
/* load global exclude list */
if (asprintf(&exclude, "%s/csync/%s", SYSCONFDIR, CSYNC_EXCLUDE_FILE) < 0) {
_csync_errno = CSYNC_ERR_UNSPEC;
rc = -1;
goto out;
}
if (csync_exclude_load(ctx, exclude) < 0) {
strerror_r(errno, errbuf, sizeof(errbuf));
CSYNC_LOG(CSYNC_LOG_PRIORITY_INFO, "Could not load %s - %s", exclude,
errbuf);
}
SAFE_FREE(exclude);
/* load exclude list */
if (asprintf(&exclude, "%s/%s", ctx->options.config_dir, CSYNC_EXCLUDE_FILE) < 0) {
_csync_errno = CSYNC_ERR_UNSPEC;
rc = -1;
goto out;
}
if (csync_exclude_load(ctx, exclude) < 0) {
strerror_r(errno, errbuf, sizeof(errbuf));
CSYNC_LOG(CSYNC_LOG_PRIORITY_INFO, "Could not load %s - %s", exclude,
errbuf);
}
2012-02-22 15:14:34 +04:00
#endif
2008-02-27 20:56:47 +03:00
2008-07-09 12:10:00 +04:00
/* create/load statedb */
if (! csync_is_statedb_disabled(ctx)) {
uint64_t h = csync_create_statedb_hash(ctx);
2008-07-09 12:10:00 +04:00
if (asprintf(&ctx->statedb.file, "%s/csync_statedb_%llu.db",
2008-06-27 20:52:09 +04:00
ctx->options.config_dir, (long long unsigned int) h) < 0) {
_csync_errno = CSYNC_ERR_UNSPEC;
rc = -1;
goto out;
}
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Remote replica: %s", ctx->remote.uri);
2008-07-09 12:10:00 +04:00
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Statedb: %s", ctx->statedb.file);
2008-02-27 20:56:47 +03:00
2008-07-09 12:10:00 +04:00
if (csync_statedb_load(ctx, ctx->statedb.file) < 0) {
_csync_errno = CSYNC_ERR_STATEDB_LOAD;
rc = -1;
goto out;
}
2008-02-27 20:56:47 +03:00
}
2008-04-28 16:08:07 +04:00
ctx->local.type = LOCAL_REPLICA;
2008-04-28 14:40:32 +04:00
/* check for uri */
if ( !ctx->options.local_only_mode && csync_fnmatch("*://*", ctx->remote.uri, 0) == 0) {
2008-04-28 14:40:32 +04:00
size_t len;
len = strstr(ctx->remote.uri, "://") - ctx->remote.uri;
/* get protocol */
if (len > 0) {
char *module = NULL;
/* module name */
module = c_strndup(ctx->remote.uri, len);
if (module == NULL) {
_csync_errno = CSYNC_ERR_MODULE;
2008-04-28 14:40:32 +04:00
rc = -1;
goto out;
}
/* load module */
retry_vio_init:
2008-04-28 16:08:07 +04:00
rc = csync_vio_init(ctx, module, NULL);
if (rc < 0) {
len = strlen(module);
if (module[len-1] == 's') {
module[len-1] = '\0';
goto retry_vio_init;
}
SAFE_FREE(module);
2008-04-28 14:40:32 +04:00
goto out;
}
SAFE_FREE(module);
2008-04-28 16:08:07 +04:00
ctx->remote.type = REMOTE_REPLCIA;
2008-04-28 14:40:32 +04:00
}
2008-04-28 16:08:07 +04:00
} else {
ctx->remote.type = LOCAL_REPLICA;
}
2008-03-20 12:34:58 +03:00
if( !ctx->options.local_only_mode ) {
timediff = csync_timediff(ctx);
if (timediff > ctx->options.max_time_difference) {
CSYNC_LOG(CSYNC_LOG_PRIORITY_FATAL,
"Clock skew detected. The time difference is greater than %d seconds!",
ctx->options.max_time_difference);
_csync_errno = CSYNC_ERR_TIMESKEW;
rc = -1;
goto out;
} else if (timediff < 0) {
CSYNC_LOG(CSYNC_LOG_PRIORITY_FATAL, "Synchronisation is not possible!");
_csync_errno = CSYNC_ERR_TIMESKEW;
rc = -1;
goto out;
}
if (csync_unix_extensions(ctx) < 0) {
CSYNC_LOG(CSYNC_LOG_PRIORITY_FATAL, "Could not detect filesystem type.");
_csync_errno = CSYNC_ERR_FILESYSTEM;
rc = -1;
goto out;
}
}
if (c_rbtree_create(&ctx->local.tree, _key_cmp, _data_cmp) < 0) {
_csync_errno = CSYNC_ERR_TREE;
rc = -1;
goto out;
}
if (c_rbtree_create(&ctx->remote.tree, _key_cmp, _data_cmp) < 0) {
_csync_errno = CSYNC_ERR_TREE;
rc = -1;
goto out;
}
ctx->status = CSYNC_STATUS_INIT;
2008-02-27 20:56:47 +03:00
rc = 0;
out:
SAFE_FREE(log);
SAFE_FREE(lock);
SAFE_FREE(exclude);
SAFE_FREE(config);
2008-02-27 20:56:47 +03:00
return rc;
}
int csync_update(CSYNC *ctx) {
int rc = -1;
2008-05-15 21:18:41 +04:00
struct timespec start, finish;
_csync_errno = CSYNC_ERR_NONE;
if (ctx == NULL) {
errno = EBADF;
_csync_errno = CSYNC_ERR_PARAM;
return -1;
}
csync_memstat_check();
/* update detection for local replica */
2012-02-04 16:24:53 +04:00
csync_gettime(&start);
2008-04-28 16:08:07 +04:00
ctx->current = LOCAL_REPLICA;
ctx->replica = ctx->local.type;
rc = csync_ftw(ctx, ctx->local.uri, csync_walker, MAX_DEPTH);
2012-02-04 16:24:53 +04:00
csync_gettime(&finish);
2008-05-15 21:18:41 +04:00
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG,
2009-04-22 15:41:46 +04:00
"Update detection for local replica took %.2f seconds walking %zu files.",
c_secdiff(finish, start), c_rbtree_size(ctx->local.tree));
csync_memstat_check();
if (rc < 0) {
_csync_errno = CSYNC_ERR_TREE;
return -1;
}
/* update detection for remote replica */
if( ! ctx->options.local_only_mode ) {
csync_gettime(&start);
ctx->current = REMOTE_REPLCIA;
ctx->replica = ctx->remote.type;
rc = csync_ftw(ctx, ctx->remote.uri, csync_walker, MAX_DEPTH);
csync_gettime(&finish);
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG,
"Update detection for remote replica took %.2f seconds "
"walking %zu files.",
c_secdiff(finish, start), c_rbtree_size(ctx->remote.tree));
csync_memstat_check();
if (rc < 0) {
_csync_errno = CSYNC_ERR_TREE;
return -1;
}
}
ctx->status |= CSYNC_STATUS_UPDATE;
return 0;
}
2008-05-15 15:50:34 +04:00
int csync_reconcile(CSYNC *ctx) {
int rc = -1;
2008-05-15 21:18:41 +04:00
struct timespec start, finish;
2008-05-15 15:50:34 +04:00
_csync_errno = CSYNC_ERR_NONE;
2008-05-15 15:50:34 +04:00
if (ctx == NULL) {
_csync_errno = CSYNC_ERR_PARAM;
2008-05-15 15:50:34 +04:00
errno = EBADF;
return -1;
}
/* Reconciliation for local replica */
2012-02-04 16:24:53 +04:00
csync_gettime(&start);
2008-05-15 21:42:03 +04:00
2008-05-15 15:50:34 +04:00
ctx->current = LOCAL_REPLICA;
ctx->replica = ctx->local.type;
rc = csync_reconcile_updates(ctx);
2012-02-04 16:24:53 +04:00
csync_gettime(&finish);
2008-05-15 21:42:03 +04:00
2008-05-15 15:50:34 +04:00
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG,
2009-04-22 15:41:46 +04:00
"Reconciliation for local replica took %.2f seconds visiting %zu files.",
c_secdiff(finish, start), c_rbtree_size(ctx->local.tree));
2008-05-15 15:50:34 +04:00
if (rc < 0) {
_csync_errno = CSYNC_ERR_RECONCILE;
2008-05-15 15:50:34 +04:00
return -1;
}
/* Reconciliation for local replica */
2012-02-04 16:24:53 +04:00
csync_gettime(&start);
2008-05-15 21:42:03 +04:00
2008-05-15 15:50:34 +04:00
ctx->current = REMOTE_REPLCIA;
ctx->replica = ctx->remote.type;
rc = csync_reconcile_updates(ctx);
2012-02-04 16:24:53 +04:00
csync_gettime(&finish);
2008-05-15 21:42:03 +04:00
2008-05-15 15:50:34 +04:00
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG,
2009-04-22 15:41:46 +04:00
"Reconciliation for remote replica took %.2f seconds visiting %zu files.",
c_secdiff(finish, start), c_rbtree_size(ctx->remote.tree));
2008-05-15 15:50:34 +04:00
if (rc < 0) {
_csync_errno = CSYNC_ERR_RECONCILE;
2008-05-15 15:50:34 +04:00
return -1;
}
ctx->status |= CSYNC_STATUS_RECONCILE;
2008-05-15 15:50:34 +04:00
return 0;
}
int csync_propagate(CSYNC *ctx) {
int rc = -1;
struct timespec start, finish;
_csync_errno = CSYNC_ERR_NONE;
if (ctx == NULL) {
_csync_errno = CSYNC_ERR_PARAM;
errno = EBADF;
return -1;
}
/* Reconciliation for local replica */
2012-02-04 16:24:53 +04:00
csync_gettime(&start);
ctx->current = LOCAL_REPLICA;
ctx->replica = ctx->local.type;
2008-12-14 01:29:16 +03:00
rc = csync_propagate_files(ctx);
2012-02-04 16:24:53 +04:00
csync_gettime(&finish);
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG,
2009-04-22 15:41:46 +04:00
"Propagation for local replica took %.2f seconds visiting %zu files.",
c_secdiff(finish, start), c_rbtree_size(ctx->local.tree));
if (rc < 0) {
_csync_errno = CSYNC_ERR_PROPAGATE;
return -1;
}
/* Reconciliation for local replica */
2012-02-04 16:24:53 +04:00
csync_gettime(&start);
ctx->current = REMOTE_REPLCIA;
ctx->replica = ctx->remote.type;
2008-12-14 01:29:16 +03:00
rc = csync_propagate_files(ctx);
2012-02-04 16:24:53 +04:00
csync_gettime(&finish);
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG,
2009-04-22 15:41:46 +04:00
"Propagation for remote replica took %.2f seconds visiting %zu files.",
c_secdiff(finish, start), c_rbtree_size(ctx->remote.tree));
if (rc < 0) {
_csync_errno = CSYNC_ERR_PROPAGATE;
return -1;
}
ctx->status |= CSYNC_STATUS_PROPAGATE;
return 0;
}
/*
* local visitor which calls the user visitor with repacked stat info.
*/
static int _csync_treewalk_visitor( void *obj, void *data ) {
csync_file_stat_t *cur = NULL;
CSYNC *ctx = NULL;
c_rbtree_visit_func *visitor = NULL;
_csync_treewalk_context *twctx = NULL;
TREE_WALK_FILE trav;
_csync_errno = CSYNC_ERR_NONE;
cur = (csync_file_stat_t *) obj;
ctx = (CSYNC *) data;
if( ctx )
twctx = (_csync_treewalk_context*) ctx->userdata;
if( twctx->instruction_filter > 0 && !(twctx->instruction_filter & cur->instruction) ) {
return 0;
}
if(cur && twctx) {
visitor = (c_rbtree_visit_func*)(twctx->user_visitor);
if(visitor) {
trav.path = cur->path;
trav.modtime = cur->modtime;
trav.uid = cur->uid;
trav.gid = cur->gid;
trav.mode = cur->mode;
trav.type = cur->type;
trav.instruction = cur->instruction;
return (*visitor)(&trav, twctx->userdata);
}
}
_csync_errno = CSYNC_ERR_TREE;
return -1;
}
/*
* treewalk function, called from its wrappers below.
*
* it encapsulates the user visitor function, the filter and the userdata
* into a treewalk_context structure and calls the rb treewalk function,
* which calls the local _csync_treewalk_visitor in this module.
* The user visitor is called from there.
*/
static int _csync_walk_tree(CSYNC *ctx, c_rbtree_t *tree, csync_treewalk_visit_func *visitor, int filter)
{
_csync_treewalk_context tw_ctx;
int rc = -1;
_csync_errno = CSYNC_ERR_NONE;
if( !(visitor && tree && ctx)) {
_csync_errno = CSYNC_ERR_PARAM;
return rc;
}
tw_ctx.userdata = ctx->userdata;
tw_ctx.user_visitor = visitor;
tw_ctx.instruction_filter = filter;
ctx->userdata = &tw_ctx;
rc = c_rbtree_walk(tree, (void*) ctx, _csync_treewalk_visitor);
if( rc < 0 )
_csync_errno = CSYNC_ERR_TREE;
ctx->userdata = tw_ctx.userdata;
return rc;
}
/*
* wrapper function for treewalk on the remote tree
*/
int csync_walk_remote_tree(CSYNC *ctx, csync_treewalk_visit_func *visitor, int filter)
{
c_rbtree_t *tree = NULL;
int rc = -1;
_csync_errno = CSYNC_ERR_NONE;
if(ctx) {
tree = ctx->remote.tree;
}
rc = _csync_walk_tree(ctx, tree, visitor, filter);
if(rc < 0)
_csync_errno = CSYNC_ERR_TREE;
return rc;
}
/*
* wrapper function for treewalk on the local tree
*/
int csync_walk_local_tree(CSYNC *ctx, csync_treewalk_visit_func *visitor, int filter)
{
c_rbtree_t *tree = NULL;
int rc = -1;
_csync_errno = CSYNC_ERR_NONE;
if(ctx) {
tree = ctx->local.tree;
}
rc = _csync_walk_tree(ctx, tree, visitor, filter);
if(rc < 0)
_csync_errno = CSYNC_ERR_TREE;
return rc;
}
static void _tree_destructor(void *data) {
csync_file_stat_t *freedata = NULL;
_csync_errno = CSYNC_ERR_NONE;
freedata = (csync_file_stat_t *) data;
SAFE_FREE(freedata);
}
2008-02-27 20:56:47 +03:00
int csync_destroy(CSYNC *ctx) {
2008-05-15 21:18:41 +04:00
struct timespec start, finish;
2008-02-27 20:56:47 +03:00
char *lock = NULL;
char errbuf[256] = {0};
2008-04-29 13:22:06 +04:00
int jwritten = 0;
2008-02-27 20:56:47 +03:00
_csync_errno = CSYNC_ERR_NONE;
2008-04-26 12:45:38 +04:00
if (ctx == NULL) {
_csync_errno = CSYNC_ERR_PARAM;
errno = EBADF;
return -1;
}
csync_vio_shutdown(ctx);
2008-07-09 12:10:00 +04:00
/* if we have a statedb */
if (ctx->statedb.db != NULL) {
2008-06-18 12:41:49 +04:00
/* 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 {
2012-02-04 16:24:53 +04:00
csync_gettime(&start);
2008-07-09 12:10:00 +04:00
/* write the statedb to disk */
if (csync_statedb_write(ctx) == 0) {
jwritten = 1;
2012-02-04 16:24:53 +04:00
csync_gettime(&finish);
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG,
2008-07-09 12:10:00 +04:00
"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));
2008-07-09 12:10:00 +04:00
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "Unable to write statedb: %s",
errbuf);
}
2008-04-29 13:22:06 +04:00
}
}
2008-07-09 12:10:00 +04:00
csync_statedb_close(ctx, ctx->statedb.file, jwritten);
2008-02-27 20:56:47 +03:00
}
2008-06-18 12:41:49 +04:00
/* clear exclude list */
csync_exclude_destroy(ctx);
2012-02-22 15:01:30 +04:00
#ifndef _WIN32
2008-06-18 12:41:49 +04:00
/* remove the lock file */
2008-02-27 20:56:47 +03:00
if (asprintf(&lock, "%s/%s", ctx->options.config_dir, CSYNC_LOCK_FILE) > 0) {
csync_lock_remove(lock);
}
2012-02-22 15:01:30 +04:00
#endif
2008-02-27 20:56:47 +03:00
2008-06-18 12:41:49 +04:00
/* stop logging */
2008-02-27 20:56:47 +03:00
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);
}
2008-06-18 12:41:49 +04:00
/* 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);
2008-02-27 20:56:47 +03:00
SAFE_FREE(ctx->options.config_dir);
2008-07-09 12:10:00 +04:00
SAFE_FREE(ctx->statedb.file);
2008-02-27 20:56:47 +03:00
SAFE_FREE(ctx);
SAFE_FREE(lock);
return 0;
}
2009-03-26 12:40:16 +03:00
/* Check if csync is the required version or get the version string. */
const char *csync_version(int req_version) {
_csync_errno = CSYNC_ERR_NONE;
2009-03-26 12:40:16 +03:00
if (req_version <= LIBCSYNC_VERSION_INT) {
return CSYNC_STRINGIFY(LIBCSYNC_VERSION);
}
return NULL;
2008-02-27 20:56:47 +03:00
}
int csync_add_exclude_list(CSYNC *ctx, const char *path) {
_csync_errno = CSYNC_ERR_NONE;
2008-05-20 18:31:10 +04:00
if (ctx == NULL || path == NULL) {
return -1;
}
return csync_exclude_load(ctx, path);
}
2009-03-26 13:24:34 +03:00
const char *csync_get_config_dir(CSYNC *ctx) {
_csync_errno = CSYNC_ERR_NONE;
if (ctx == NULL) {
return NULL;
}
return ctx->options.config_dir;
}
int csync_set_config_dir(CSYNC *ctx, const char *path) {
_csync_errno = CSYNC_ERR_NONE;
if (ctx == NULL || path == NULL) {
_csync_errno = CSYNC_ERR_PARAM;
return -1;
}
SAFE_FREE(ctx->options.config_dir);
ctx->options.config_dir = c_strdup(path);
if (ctx->options.config_dir == NULL) {
_csync_errno = CSYNC_ERR_MEM;
return -1;
}
return 0;
}
2008-07-09 12:10:00 +04:00
int csync_enable_statedb(CSYNC *ctx) {
_csync_errno = CSYNC_ERR_NONE;
if (ctx == NULL) {
_csync_errno = CSYNC_ERR_MEM;
return -1;
}
2008-06-24 19:39:46 +04:00
if (ctx->status & CSYNC_STATUS_INIT) {
_csync_errno = CSYNC_ERR_UNSPEC;
2008-06-24 19:39:46 +04:00
fprintf(stderr, "This function must be called before initialization.");
return -1;
}
2008-07-09 12:10:00 +04:00
ctx->statedb.disabled = 0;
return 0;
}
2008-07-09 12:10:00 +04:00
int csync_disable_statedb(CSYNC *ctx) {
_csync_errno = CSYNC_ERR_NONE;
if (ctx == NULL) {
_csync_errno = CSYNC_ERR_PARAM;
return -1;
}
2008-06-24 19:39:46 +04:00
if (ctx->status & CSYNC_STATUS_INIT) {
_csync_errno = CSYNC_ERR_UNSPEC;
2008-06-24 19:39:46 +04:00
fprintf(stderr, "This function must be called before initialization.");
return -1;
}
2008-07-09 12:10:00 +04:00
ctx->statedb.disabled = 1;
return 0;
}
2008-07-09 12:10:00 +04:00
int csync_is_statedb_disabled(CSYNC *ctx) {
_csync_errno = CSYNC_ERR_NONE;
if (ctx == NULL) {
_csync_errno = CSYNC_ERR_PARAM;
2008-05-21 16:46:42 +04:00
return -1;
}
2008-07-09 12:10:00 +04:00
return ctx->statedb.disabled;
}
2008-06-24 15:36:27 +04:00
int csync_set_auth_callback(CSYNC *ctx, csync_auth_callback cb) {
_csync_errno = CSYNC_ERR_NONE;
if (ctx == NULL || cb == NULL) {
_csync_errno = CSYNC_ERR_PARAM;
return -1;
}
if (ctx->status & CSYNC_STATUS_INIT) {
fprintf(stderr, "This function must be called before initialization.");
_csync_errno = CSYNC_ERR_UNSPEC;
return -1;
}
ctx->auth_callback = cb;
return 0;
}
2009-03-26 13:24:34 +03:00
const char *csync_get_statedb_file(CSYNC *ctx) {
_csync_errno = CSYNC_ERR_NONE;
if (ctx == NULL) {
_csync_errno = CSYNC_ERR_PARAM;
return NULL;
}
2008-07-09 12:10:00 +04:00
return c_strdup(ctx->statedb.file);
}
void *csync_get_userdata(CSYNC *ctx) {
_csync_errno = CSYNC_ERR_NONE;
if (ctx == NULL) {
_csync_errno = CSYNC_ERR_PARAM;
return NULL;
}
return ctx->userdata;
}
int csync_set_userdata(CSYNC *ctx, void *userdata) {
_csync_errno = CSYNC_ERR_NONE;
if (ctx == NULL) {
_csync_errno = CSYNC_ERR_PARAM;
return -1;
}
ctx->userdata = userdata;
return 0;
}
2008-06-24 15:36:27 +04:00
csync_auth_callback csync_get_auth_callback(CSYNC *ctx) {
_csync_errno = CSYNC_ERR_NONE;
if (ctx == NULL) {
_csync_errno = CSYNC_ERR_PARAM;
return NULL;
}
return ctx->auth_callback;
}
int csync_set_status(CSYNC *ctx, int status) {
_csync_errno = CSYNC_ERR_NONE;
if (ctx == NULL || status < 0) {
_csync_errno = CSYNC_ERR_PARAM;
return -1;
}
ctx->status = status;
return 0;
}
int csync_get_status(CSYNC *ctx) {
_csync_errno = CSYNC_ERR_NONE;
2008-06-24 13:13:56 +04:00
if (ctx == NULL) {
_csync_errno = CSYNC_ERR_PARAM;
2008-06-24 13:13:56 +04:00
return -1;
}
return ctx->status;
}
int csync_enable_conflictcopys(CSYNC* ctx){
_csync_errno = CSYNC_ERR_NONE;
if (ctx == NULL) {
_csync_errno = CSYNC_ERR_PARAM;
return -1;
}
if (ctx->status & CSYNC_STATUS_INIT) {
fprintf(stderr, "This function must be called before initialization.");
_csync_errno = CSYNC_ERR_UNSPEC;
return -1;
}
ctx->options.with_conflict_copys=true;
return 0;
}
int csync_set_local_only( CSYNC *ctx, bool local_only ) {
_csync_errno = CSYNC_ERR_NONE;
if (ctx == NULL) {
_csync_errno = CSYNC_ERR_PARAM;
return -1;
}
if (ctx->status & CSYNC_STATUS_INIT) {
fprintf(stderr, "This function must be called before initialization.");
_csync_errno = CSYNC_ERR_UNSPEC;
return -1;
}
ctx->options.local_only_mode=local_only;
return 0;
}
bool csync_get_local_only( CSYNC *ctx ) {
_csync_errno = CSYNC_ERR_NONE;
if (ctx == NULL) {
_csync_errno = CSYNC_ERR_PARAM;
return -1;
}
return ctx->options.local_only_mode;
}
CSYNC_ERROR_CODE csync_errno(void) {
return _csync_errno;
}
2009-05-13 12:12:07 +04:00
/* vim: set ts=8 sw=2 et cindent: */