From 871dde4911077e559fc7ac3876fee6ea7d1a0b59 Mon Sep 17 00:00:00 2001 From: Klaas Freitag Date: Mon, 27 Feb 2012 12:18:02 +0100 Subject: [PATCH] Added ability to limit the csync run to the local tree only. That gives apps the chance to use the efficient treewalk of csync to get information on the tree. Signed-off-by: Andreas Schneider --- src/csync.c | 87 +++++++++++++++++++++++++++++---------------- src/csync.h | 18 ++++++++++ src/csync_private.h | 1 + 3 files changed, 76 insertions(+), 30 deletions(-) diff --git a/src/csync.c b/src/csync.c index 3c9977cdc..3b0474f29 100644 --- a/src/csync.c +++ b/src/csync.c @@ -116,6 +116,7 @@ int csync_create(CSYNC **csync, const char *local, const char *remote) { ctx->options.max_time_difference = MAX_TIME_DIFFERENCE; ctx->options.unix_extensions = 0; ctx->options.with_conflict_copys=false; + ctx->options.local_only_mode = false; ctx->pwd.uid = getuid(); ctx->pwd.euid = geteuid(); @@ -261,7 +262,7 @@ int csync_init(CSYNC *ctx) { ctx->local.type = LOCAL_REPLICA; /* check for uri */ - if (csync_fnmatch("*://*", ctx->remote.uri, 0) == 0) { + if ( !ctx->options.local_only_mode && csync_fnmatch("*://*", ctx->remote.uri, 0) == 0) { size_t len; len = strstr(ctx->remote.uri, "://") - ctx->remote.uri; /* get protocol */ @@ -294,23 +295,25 @@ retry_vio_init: ctx->remote.type = LOCAL_REPLICA; } - 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); - rc = -1; - goto out; - } else if (timediff < 0) { - CSYNC_LOG(CSYNC_LOG_PRIORITY_FATAL, "Synchronisation is not possible!"); - rc = -1; - goto out; - } + 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); + rc = -1; + goto out; + } else if (timediff < 0) { + CSYNC_LOG(CSYNC_LOG_PRIORITY_FATAL, "Synchronisation is not possible!"); + rc = -1; + goto out; + } - if (csync_unix_extensions(ctx) < 0) { - CSYNC_LOG(CSYNC_LOG_PRIORITY_FATAL, "Could not detect filesystem type."); - rc = -1; - goto out; + if (csync_unix_extensions(ctx) < 0) { + CSYNC_LOG(CSYNC_LOG_PRIORITY_FATAL, "Could not detect filesystem type."); + rc = -1; + goto out; + } } if (c_rbtree_create(&ctx->local.tree, _key_cmp, _data_cmp) < 0) { @@ -365,24 +368,25 @@ int csync_update(CSYNC *ctx) { } /* update detection for remote replica */ - csync_gettime(&start); - ctx->current = REMOTE_REPLCIA; - ctx->replica = ctx->remote.type; + 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); + rc = csync_ftw(ctx, ctx->remote.uri, csync_walker, MAX_DEPTH); - csync_gettime(&finish); + 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(); + 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) { - return -1; + if (rc < 0) { + return -1; + } } - ctx->status |= CSYNC_STATUS_UPDATE; return 0; @@ -734,4 +738,27 @@ int csync_enable_conflictcopys(CSYNC* ctx){ return 0; } +int csync_set_local_only( CSYNC *ctx, bool local_only ) { + if (ctx == NULL) { + return -1; + } + + if (ctx->status & CSYNC_STATUS_INIT) { + fprintf(stderr, "This function must be called before initialization."); + return -1; + } + + ctx->options.local_only_mode=local_only; + + return 0; +} + +bool csync_get_local_only( CSYNC *ctx ) { + if (ctx == NULL) { + return -1; + } + + return ctx->options.local_only_mode; +} + /* vim: set ts=8 sw=2 et cindent: */ diff --git a/src/csync.h b/src/csync.h index bd641f633..3aeed5f64 100644 --- a/src/csync.h +++ b/src/csync.h @@ -31,6 +31,8 @@ #ifndef _CSYNC_H #define _CSYNC_H +#include + #ifdef __cplusplus extern "C" { #endif @@ -283,6 +285,22 @@ const char *csync_get_statedb_file(CSYNC *ctx); */ int csync_enable_conflictcopys(CSYNC *ctx); +/** + * @brief Flag to tell csync that only a local run is intended. Call before csync_init + * + * @param local_only Bool flag to indicate local only mode. + * + * @return 0 on success, less than 0 if an error occured. + */ +int csync_set_local_only( CSYNC *ctx, bool local_only ); + +/** + * @brief Retrieve the flag to tell csync that only a local run is intended. + * + * @return 1: stay local only, 0: local and remote mode + */ +bool csync_get_local_only( CSYNC *ctx ); + /* Used for special modes or debugging */ int csync_get_status(CSYNC *ctx); diff --git a/src/csync_private.h b/src/csync_private.h index 051bfa38e..26cce1889 100644 --- a/src/csync_private.h +++ b/src/csync_private.h @@ -117,6 +117,7 @@ struct csync_s { int unix_extensions; char *config_dir; bool with_conflict_copys; + bool local_only_mode; } options; struct {