2008-02-27 20:56:47 +03:00
|
|
|
/*
|
|
|
|
* libcsync -- a library to sync a directory with another
|
|
|
|
*
|
2013-07-23 19:31:55 +04:00
|
|
|
* Copyright (c) 2008-2013 by Andreas Schneider <asn@cryptomilk.org>
|
|
|
|
* Copyright (c) 2012-2013 by Klaas Freitag <freitag@owncloud.com>
|
2008-02-27 20:56:47 +03:00
|
|
|
*
|
2013-07-23 19:31:55 +04:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2008-02-27 20:56:47 +03:00
|
|
|
*
|
2013-07-23 19:31:55 +04:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
2008-07-10 12:25:12 +04:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2013-07-23 19:31:55 +04:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2008-02-27 20:56:47 +03:00
|
|
|
*
|
2013-07-23 19:31:55 +04:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; 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"
|
|
|
|
|
2008-06-09 19:19:12 +04:00
|
|
|
#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>
|
2008-04-26 12:43:22 +04:00
|
|
|
#include <time.h>
|
2008-11-13 16:08:26 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
2011-04-06 18:43:04 +04:00
|
|
|
#include <stdbool.h>
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2013-07-12 13:25:49 +04:00
|
|
|
#ifdef HAVE_ICONV_H
|
2012-12-07 16:02:46 +04:00
|
|
|
#include <iconv.h>
|
|
|
|
#endif
|
2013-07-12 13:25:49 +04:00
|
|
|
#ifdef HAVE_SYS_ICONV_H
|
|
|
|
#include <sys/iconv.h>
|
|
|
|
#endif
|
2012-12-07 16:02:46 +04:00
|
|
|
|
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"
|
2008-03-25 18:22:51 +03:00
|
|
|
#include "csync_exclude.h"
|
2008-04-28 18:49:21 +04:00
|
|
|
#include "csync_lock.h"
|
2008-07-09 11:57:19 +04:00
|
|
|
#include "csync_statedb.h"
|
2008-05-05 12:48:05 +04:00
|
|
|
#include "csync_time.h"
|
2008-04-28 18:49:21 +04:00
|
|
|
#include "csync_util.h"
|
2012-03-02 16:38:39 +04:00
|
|
|
#include "csync_misc.h"
|
2012-12-07 16:02:46 +04:00
|
|
|
#include "std/c_private.h"
|
2013-04-26 12:45:14 +04:00
|
|
|
|
2008-04-23 14:12:48 +04:00
|
|
|
#include "csync_update.h"
|
2008-05-15 15:50:34 +04:00
|
|
|
#include "csync_reconcile.h"
|
2008-04-23 14:12:48 +04:00
|
|
|
|
2008-04-22 17:58:06 +04:00
|
|
|
#include "vio/csync_vio.h"
|
|
|
|
|
2008-02-27 20:56:47 +03:00
|
|
|
#include "csync_log.h"
|
2013-01-04 23:45:10 +04:00
|
|
|
#include "csync_rename.h"
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2008-06-02 17:11:45 +04:00
|
|
|
static int _key_cmp(const void *key, const void *data) {
|
2008-04-28 15:42:10 +04:00
|
|
|
uint64_t a;
|
|
|
|
csync_file_stat_t *b;
|
|
|
|
|
2008-06-27 20:01:19 +04:00
|
|
|
a = *(uint64_t *) (key);
|
2008-04-28 15:42:10 +04:00
|
|
|
b = (csync_file_stat_t *) data;
|
|
|
|
|
|
|
|
if (a < b->phash) {
|
|
|
|
return -1;
|
|
|
|
} else if (a > b->phash) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-02 17:11:45 +04:00
|
|
|
static int _data_cmp(const void *key, const void *data) {
|
2008-04-28 15:42:10 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-04-22 19:23:26 +04:00
|
|
|
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
|
|
|
|
|
|
|
ctx = c_malloc(sizeof(CSYNC));
|
|
|
|
if (ctx == NULL) {
|
2008-04-22 19:23:26 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-08-18 19:26:45 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_OK;
|
2012-04-17 14:31:27 +04:00
|
|
|
|
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);
|
2008-04-22 19:23:26 +04:00
|
|
|
if (ctx->local.uri == NULL) {
|
2013-08-18 19:26:45 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_MEMORY_ERROR;
|
2012-10-19 22:22:52 +04:00
|
|
|
free(ctx);
|
2008-04-22 19:23:26 +04:00
|
|
|
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);
|
2008-04-22 19:23:26 +04:00
|
|
|
if (ctx->remote.uri == NULL) {
|
2013-08-18 19:26:45 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_MEMORY_ERROR;
|
2012-10-19 22:22:52 +04:00
|
|
|
free(ctx);
|
2008-02-27 20:56:47 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_OK;
|
2008-02-27 20:56:47 +03:00
|
|
|
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;
|
2011-04-06 18:43:04 +04:00
|
|
|
ctx->options.with_conflict_copys=false;
|
2012-02-27 15:18:02 +04:00
|
|
|
ctx->options.local_only_mode = false;
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2008-11-13 16:08:26 +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;
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_MEMORY_ERROR;
|
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) {
|
2008-04-22 19:23:26 +04:00
|
|
|
SAFE_FREE(ctx->local.uri);
|
|
|
|
SAFE_FREE(ctx->remote.uri);
|
2008-02-27 20:56:47 +03:00
|
|
|
SAFE_FREE(ctx);
|
|
|
|
errno = ENOMEM;
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_MEMORY_ERROR;
|
2008-02-27 20:56:47 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-04-20 14:06:59 +04:00
|
|
|
ctx->local.list = 0;
|
|
|
|
ctx->remote.list = 0;
|
2013-03-11 22:55:07 +04:00
|
|
|
ctx->current_fs = NULL;
|
2013-04-20 14:06:59 +04:00
|
|
|
|
2013-05-08 17:28:26 +04:00
|
|
|
ctx->abort = false;
|
|
|
|
|
2008-02-27 20:56:47 +03:00
|
|
|
*csync = ctx;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int csync_init(CSYNC *ctx) {
|
|
|
|
int rc;
|
2008-05-05 12:48:05 +04:00
|
|
|
time_t timediff = -1;
|
2008-03-25 18:22:51 +03:00
|
|
|
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;
|
2008-07-18 13:35:02 +04:00
|
|
|
char errbuf[256] = {0};
|
2013-08-18 18:21:18 +04:00
|
|
|
|
2008-02-29 13:41:15 +03:00
|
|
|
if (ctx == NULL) {
|
2008-02-27 20:56:47 +03:00
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_OK;
|
|
|
|
|
2008-02-27 20:56:47 +03:00
|
|
|
/* Do not initialize twice */
|
2008-05-27 16:15:44 +04:00
|
|
|
if (ctx->status & CSYNC_STATUS_INIT) {
|
2008-02-27 20:56:47 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create dir if it doesn't exist */
|
|
|
|
if (! c_isdir(ctx->options.config_dir)) {
|
|
|
|
c_mkdirs(ctx->options.config_dir, 0700);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create lock file */
|
2013-05-04 18:10:11 +04:00
|
|
|
if (asprintf(&lock, "%s/%s", ctx->local.uri, CSYNC_LOCK_FILE) < 0) {
|
2008-02-27 20:56:47 +03:00
|
|
|
rc = -1;
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_MEMORY_ERROR;
|
2008-02-27 20:56:47 +03:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-03-01 12:59:55 +04:00
|
|
|
if (csync_lock(lock) < 0) {
|
2008-02-27 20:56:47 +03:00
|
|
|
rc = -1;
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_NO_LOCK;
|
2008-02-27 20:56:47 +03:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2008-03-06 19:43:58 +03:00
|
|
|
/* load config file */
|
|
|
|
if (asprintf(&config, "%s/%s", ctx->options.config_dir, CSYNC_CONF_FILE) < 0) {
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_MEMORY_ERROR;
|
2008-03-06 19:43:58 +03:00
|
|
|
rc = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-07-29 01:19:00 +04:00
|
|
|
rc = csync_config_parse_file(ctx, config);
|
|
|
|
if (rc < 0) {
|
2012-03-18 19:16:46 +04:00
|
|
|
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
|
2008-03-25 18:22:51 +03:00
|
|
|
/* load global exclude list */
|
2012-10-26 18:11:34 +04:00
|
|
|
if (asprintf(&exclude, "%s/ocsync/%s", SYSCONFDIR, CSYNC_EXCLUDE_FILE) < 0) {
|
2008-03-25 18:22:51 +03:00
|
|
|
rc = -1;
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_MEMORY_ERROR;
|
2008-03-25 18:22:51 +03:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (csync_exclude_load(ctx, exclude) < 0) {
|
2013-08-02 17:19:41 +04:00
|
|
|
C_STRERROR(errno, errbuf, sizeof(errbuf));
|
2012-10-27 18:05:16 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN, "Could not load %s - %s", exclude,
|
2012-03-07 19:46:12 +04:00
|
|
|
errbuf);
|
2008-03-25 18:22:51 +03:00
|
|
|
}
|
|
|
|
SAFE_FREE(exclude);
|
2012-11-07 20:10:56 +04:00
|
|
|
#endif
|
2008-03-25 18:22:51 +03:00
|
|
|
/* load exclude list */
|
2009-04-22 15:41:46 +04:00
|
|
|
if (asprintf(&exclude, "%s/%s", ctx->options.config_dir,
|
|
|
|
CSYNC_EXCLUDE_FILE) < 0) {
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_MEMORY_ERROR;
|
2008-03-25 18:22:51 +03:00
|
|
|
rc = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (csync_exclude_load(ctx, exclude) < 0) {
|
2013-08-02 17:19:41 +04:00
|
|
|
C_STRERROR(errno, errbuf, sizeof(errbuf));
|
2013-02-06 17:56:42 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_INFO, "Could not load %s - %s", exclude,
|
2012-03-07 19:46:12 +04:00
|
|
|
errbuf);
|
2008-03-25 18:22:51 +03:00
|
|
|
}
|
2012-11-07 20:10:56 +04: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 */
|
2012-02-27 15:18:02 +04:00
|
|
|
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) {
|
|
|
|
rc = -1;
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_MEMORY_ERROR;
|
2008-04-28 14:40:32 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* load module */
|
2012-02-13 17:33:23 +04:00
|
|
|
retry_vio_init:
|
2008-04-28 16:08:07 +04:00
|
|
|
rc = csync_vio_init(ctx, module, NULL);
|
|
|
|
if (rc < 0) {
|
2012-02-13 17:33:23 +04:00
|
|
|
len = strlen(module);
|
|
|
|
|
2012-05-15 18:15:27 +04:00
|
|
|
if (len > 0 && module[len-1] == 's') {
|
2012-02-22 18:17:35 +04:00
|
|
|
module[len-1] = '\0';
|
2012-02-13 17:33:23 +04:00
|
|
|
goto retry_vio_init;
|
|
|
|
}
|
2012-04-19 18:12:05 +04:00
|
|
|
/* Now vio init finally failed which means a module could not be found. */
|
2012-06-18 12:43:07 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_FATAL,
|
|
|
|
"The csync module %s could not be loaded.", module);
|
2012-02-13 17:33:23 +04:00
|
|
|
SAFE_FREE(module);
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_NO_MODULE;
|
2008-04-28 14:40:32 +04:00
|
|
|
goto out;
|
|
|
|
}
|
2012-02-13 17:33:23 +04:00
|
|
|
SAFE_FREE(module);
|
2012-10-19 16:23:20 +04:00
|
|
|
ctx->remote.type = REMOTE_REPLICA;
|
2008-04-28 14:40:32 +04:00
|
|
|
}
|
2008-04-28 16:08:07 +04:00
|
|
|
} else {
|
|
|
|
ctx->remote.type = LOCAL_REPLICA;
|
2008-04-22 17:58:06 +04:00
|
|
|
}
|
2008-03-20 12:34:58 +03:00
|
|
|
|
2012-07-11 18:03:31 +04:00
|
|
|
if(!ctx->options.local_only_mode) {
|
2012-07-04 18:32:17 +04:00
|
|
|
if(ctx->module.capabilities.time_sync_required) {
|
2012-02-27 15:18:02 +04:00
|
|
|
timediff = csync_timediff(ctx);
|
|
|
|
if (timediff > ctx->options.max_time_difference) {
|
2012-02-27 15:18:02 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_FATAL,
|
|
|
|
"Clock skew detected. The time difference is greater than %d seconds!",
|
|
|
|
ctx->options.max_time_difference);
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_TIMESKEW;
|
2012-02-27 15:18:02 +04:00
|
|
|
rc = -1;
|
|
|
|
goto out;
|
2012-02-27 15:18:02 +04:00
|
|
|
} else if (timediff < 0) {
|
2012-02-27 15:18:02 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_FATAL, "Synchronisation is not possible!");
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_TIMESKEW;
|
2012-02-27 15:18:02 +04:00
|
|
|
rc = -1;
|
|
|
|
goto out;
|
2012-02-27 15:18:02 +04:00
|
|
|
}
|
2012-07-11 18:03:31 +04:00
|
|
|
} else {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "Module does not need time synchronization.");
|
2012-07-04 18:32:17 +04:00
|
|
|
}
|
2012-07-11 18:03:31 +04:00
|
|
|
|
2012-07-04 18:32:17 +04:00
|
|
|
if(ctx->module.capabilities.unix_extensions == -1) { /* detect */
|
2012-02-27 15:18:02 +04:00
|
|
|
if (csync_unix_extensions(ctx) < 0) {
|
2012-02-27 15:18:02 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_FATAL, "Could not detect filesystem type.");
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_FILESYSTEM_UNKNOWN;
|
2012-02-27 15:18:02 +04:00
|
|
|
rc = -1;
|
|
|
|
goto out;
|
2012-02-27 15:18:02 +04:00
|
|
|
}
|
2012-07-04 18:32:17 +04:00
|
|
|
} else {
|
|
|
|
/* The module specifies the value for the unix_extensions. */
|
|
|
|
ctx->options.unix_extensions = ctx->module.capabilities.unix_extensions;
|
|
|
|
}
|
2009-01-13 17:50:44 +03:00
|
|
|
}
|
|
|
|
|
2013-02-06 21:38:46 +04:00
|
|
|
if (ctx->options.timeout)
|
|
|
|
csync_vio_set_property(ctx, "timeout", &ctx->options.timeout);
|
|
|
|
|
2008-06-02 17:11:45 +04:00
|
|
|
if (c_rbtree_create(&ctx->local.tree, _key_cmp, _data_cmp) < 0) {
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_TREE_ERROR;
|
2008-04-28 15:42:10 +04:00
|
|
|
rc = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2008-06-02 17:11:45 +04:00
|
|
|
if (c_rbtree_create(&ctx->remote.tree, _key_cmp, _data_cmp) < 0) {
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_TREE_ERROR;
|
2008-04-28 15:42:10 +04:00
|
|
|
rc = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2008-05-27 16:15:44 +04:00
|
|
|
ctx->status = CSYNC_STATUS_INIT;
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2013-08-18 20:55:32 +04:00
|
|
|
csync_lock_remove(lock);
|
2013-05-04 18:10:11 +04:00
|
|
|
|
2013-03-26 13:05:15 +04:00
|
|
|
csync_set_module_property(ctx, "csync_context", ctx);
|
|
|
|
|
2012-06-20 12:40:40 +04:00
|
|
|
/* initialize random generator */
|
|
|
|
srand(time(NULL));
|
|
|
|
|
2008-02-27 20:56:47 +03:00
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
SAFE_FREE(lock);
|
2008-03-25 18:22:51 +03:00
|
|
|
SAFE_FREE(exclude);
|
2008-03-07 13:07:12 +03:00
|
|
|
SAFE_FREE(config);
|
2008-02-27 20:56:47 +03:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2008-04-23 14:12:48 +04:00
|
|
|
int csync_update(CSYNC *ctx) {
|
2008-05-15 14:03:05 +04:00
|
|
|
int rc = -1;
|
2008-05-15 21:18:41 +04:00
|
|
|
struct timespec start, finish;
|
2013-05-04 18:10:11 +04:00
|
|
|
char *lock = NULL;
|
|
|
|
|
2008-04-26 12:43:22 +04:00
|
|
|
if (ctx == NULL) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2013-08-18 19:26:45 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_OK;
|
2008-04-26 12:43:22 +04:00
|
|
|
|
2013-05-04 18:10:11 +04:00
|
|
|
/* try to create lock file */
|
|
|
|
if (asprintf(&lock, "%s/%s", ctx->local.uri, CSYNC_LOCK_FILE) < 0) {
|
2013-08-18 19:26:45 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_MEMORY_ERROR;
|
2013-05-04 18:10:11 +04:00
|
|
|
rc = -1;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2013-08-18 20:55:32 +04:00
|
|
|
if (csync_lock(lock) < 0) {
|
2013-08-18 19:26:45 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_NO_LOCK;
|
2013-05-04 18:10:11 +04:00
|
|
|
rc = -1;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2013-05-13 19:45:12 +04:00
|
|
|
SAFE_FREE(lock);
|
2013-08-05 16:58:43 +04:00
|
|
|
|
|
|
|
/* create/load statedb */
|
|
|
|
if (! csync_is_statedb_disabled(ctx)) {
|
|
|
|
rc = asprintf(&ctx->statedb.file, "%s/.csync_journal.db",
|
|
|
|
ctx->local.uri);
|
|
|
|
if (rc < 0) {
|
2013-08-18 18:21:18 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_MEMORY_ERROR;
|
2013-08-05 16:58:43 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Journal: %s", ctx->statedb.file);
|
|
|
|
|
2013-08-18 20:55:32 +04:00
|
|
|
if (csync_statedb_load(ctx, ctx->statedb.file, &ctx->statedb.db) < 0) {
|
2013-08-18 18:21:18 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_STATEDB_LOAD_ERROR;
|
2013-08-05 16:58:43 +04:00
|
|
|
rc = -1;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_OK;
|
|
|
|
|
2013-03-01 12:59:55 +04:00
|
|
|
csync_memstat_check();
|
2008-04-28 18:49:21 +04:00
|
|
|
|
2008-05-15 14:03:05 +04:00
|
|
|
/* 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;
|
2008-05-15 14:03:05 +04:00
|
|
|
|
|
|
|
rc = csync_ftw(ctx, ctx->local.uri, csync_walker, MAX_DEPTH);
|
2013-06-06 11:34:54 +04:00
|
|
|
if (rc < 0) {
|
2013-08-18 18:21:18 +04:00
|
|
|
if(ctx->status_code == CSYNC_STATUS_OK)
|
2013-08-18 19:26:45 +04:00
|
|
|
ctx->status_code = csync_errno_to_status(errno, CSYNC_STATUS_UPDATE_ERROR);
|
2013-06-06 11:34:54 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2008-05-15 14:03:05 +04:00
|
|
|
|
2012-02-04 16:24:53 +04:00
|
|
|
csync_gettime(&finish);
|
2008-05-15 21:18:41 +04:00
|
|
|
|
2008-04-26 12:43:22 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG,
|
2013-03-12 20:01:39 +04:00
|
|
|
"Update detection for local replica took %.2f seconds walking %zu files.",
|
|
|
|
c_secdiff(finish, start), c_rbtree_size(ctx->local.tree));
|
2013-03-01 12:59:55 +04:00
|
|
|
csync_memstat_check();
|
2008-04-26 12:43:22 +04:00
|
|
|
|
2008-05-15 14:03:05 +04:00
|
|
|
if (rc < 0) {
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_TREE_ERROR;
|
2008-05-15 14:03:05 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update detection for remote replica */
|
2012-02-27 15:18:02 +04:00
|
|
|
if( ! ctx->options.local_only_mode ) {
|
2013-03-12 20:01:39 +04:00
|
|
|
csync_gettime(&start);
|
|
|
|
ctx->current = REMOTE_REPLICA;
|
|
|
|
ctx->replica = ctx->remote.type;
|
2008-05-15 14:03:05 +04:00
|
|
|
|
2013-03-12 20:01:39 +04:00
|
|
|
rc = csync_ftw(ctx, ctx->remote.uri, csync_walker, MAX_DEPTH);
|
2013-08-18 18:21:18 +04:00
|
|
|
if (rc < 0) {
|
|
|
|
if(ctx->status_code == CSYNC_STATUS_OK)
|
2013-08-18 19:26:45 +04:00
|
|
|
ctx->status_code = csync_errno_to_status(errno, CSYNC_STATUS_UPDATE_ERROR);
|
2013-08-18 18:21:18 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2008-05-15 14:03:05 +04:00
|
|
|
|
2008-04-23 14:12:48 +04:00
|
|
|
|
2013-03-12 20:01:39 +04:00
|
|
|
csync_gettime(&finish);
|
2008-05-15 14:03:05 +04:00
|
|
|
|
2013-03-12 20:01:39 +04:00
|
|
|
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();
|
2008-05-15 14:03:05 +04:00
|
|
|
}
|
2008-05-27 16:15:44 +04:00
|
|
|
ctx->status |= CSYNC_STATUS_UPDATE;
|
2008-04-29 13:20:52 +04:00
|
|
|
|
2008-04-23 14:12:48 +04:00
|
|
|
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
|
|
|
|
|
|
|
if (ctx == NULL) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_OK;
|
2008-05-15 15:50:34 +04:00
|
|
|
|
|
|
|
/* 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) {
|
2013-04-06 20:47:08 +04:00
|
|
|
if (!CSYNC_STATUS_IS_OK(ctx->status_code)) {
|
2013-08-18 19:26:45 +04:00
|
|
|
ctx->status_code = csync_errno_to_status( errno, CSYNC_STATUS_RECONCILE_ERROR );
|
2013-04-06 20:47:08 +04:00
|
|
|
}
|
|
|
|
return -1;
|
2008-05-15 15:50:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Reconciliation for local replica */
|
2012-02-04 16:24:53 +04:00
|
|
|
csync_gettime(&start);
|
2008-05-15 21:42:03 +04:00
|
|
|
|
2012-10-19 16:23:20 +04:00
|
|
|
ctx->current = REMOTE_REPLICA;
|
2008-05-15 15:50:34 +04:00
|
|
|
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) {
|
2013-04-06 20:47:08 +04:00
|
|
|
if (!CSYNC_STATUS_IS_OK(ctx->status_code)) {
|
2013-08-18 19:26:45 +04:00
|
|
|
ctx->status_code = csync_errno_to_status(errno, CSYNC_STATUS_RECONCILE_ERROR );
|
2013-04-06 20:47:08 +04:00
|
|
|
}
|
|
|
|
return -1;
|
2008-05-15 15:50:34 +04:00
|
|
|
}
|
|
|
|
|
2008-05-27 16:15:44 +04:00
|
|
|
ctx->status |= CSYNC_STATUS_RECONCILE;
|
2008-05-15 15:50:34 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-19 19:05:23 +04:00
|
|
|
/*
|
|
|
|
* local visitor which calls the user visitor with repacked stat info.
|
|
|
|
*/
|
2012-04-17 14:31:27 +04:00
|
|
|
static int _csync_treewalk_visitor(void *obj, void *data) {
|
2012-12-15 19:34:31 +04:00
|
|
|
int rc = 0;
|
2012-03-30 17:33:13 +04:00
|
|
|
csync_file_stat_t *cur = NULL;
|
|
|
|
CSYNC *ctx = NULL;
|
|
|
|
c_rbtree_visit_func *visitor = NULL;
|
2012-03-19 19:05:23 +04:00
|
|
|
_csync_treewalk_context *twctx = NULL;
|
|
|
|
TREE_WALK_FILE trav;
|
|
|
|
|
2012-10-28 14:31:25 +04:00
|
|
|
cur = (csync_file_stat_t *) obj;
|
|
|
|
ctx = (CSYNC *) data;
|
|
|
|
|
2013-04-08 11:11:25 +04:00
|
|
|
if (ctx == NULL) {
|
2013-04-05 17:05:44 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2012-12-03 20:32:08 +04:00
|
|
|
|
2013-04-08 11:11:25 +04:00
|
|
|
if (obj == NULL || data == NULL) {
|
2013-08-18 19:26:45 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_PARAM_ERROR;
|
2012-04-17 14:31:27 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2013-08-18 19:26:45 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_OK;
|
2012-04-17 14:31:27 +04:00
|
|
|
|
2012-10-27 17:10:16 +04:00
|
|
|
twctx = (_csync_treewalk_context*) ctx->callbacks.userdata;
|
2012-10-19 21:20:45 +04:00
|
|
|
if (twctx == NULL) {
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_PARAM_ERROR;
|
2012-10-19 21:20:45 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2012-04-17 14:31:27 +04:00
|
|
|
|
2012-10-19 21:20:45 +04:00
|
|
|
if (twctx->instruction_filter > 0 &&
|
|
|
|
!(twctx->instruction_filter & cur->instruction) ) {
|
2012-03-19 19:05:23 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-19 21:20:45 +04:00
|
|
|
visitor = (c_rbtree_visit_func*)(twctx->user_visitor);
|
|
|
|
if (visitor != NULL) {
|
2013-10-25 15:13:39 +04:00
|
|
|
trav.path = cur->path;
|
|
|
|
trav.size = cur->size;
|
|
|
|
trav.modtime = cur->modtime;
|
|
|
|
trav.uid = cur->uid;
|
|
|
|
trav.gid = cur->gid;
|
|
|
|
trav.mode = cur->mode;
|
|
|
|
trav.type = cur->type;
|
|
|
|
trav.instruction = cur->instruction;
|
|
|
|
trav.rename_path = cur->destpath;
|
2013-11-13 17:29:31 +04:00
|
|
|
trav.etag = cur->etag;
|
2013-10-25 15:13:39 +04:00
|
|
|
trav.file_id = cur->file_id;
|
|
|
|
|
2013-03-28 16:04:13 +04:00
|
|
|
trav.error_string = cur->error_string;
|
2013-11-15 16:52:41 +04:00
|
|
|
trav.should_update_etag = cur->should_update_etag;
|
2012-10-19 21:20:45 +04:00
|
|
|
|
2012-12-15 19:34:31 +04:00
|
|
|
rc = (*visitor)(&trav, twctx->userdata);
|
|
|
|
cur->instruction = trav.instruction;
|
2013-11-13 17:29:31 +04:00
|
|
|
if (trav.etag != cur->etag) {
|
|
|
|
SAFE_FREE(cur->etag);
|
|
|
|
cur->etag = c_strdup(trav.etag);
|
2013-05-03 21:03:55 +04:00
|
|
|
}
|
2012-12-15 19:34:31 +04:00
|
|
|
return rc;
|
2012-03-19 19:05:23 +04:00
|
|
|
}
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_PARAM_ERROR;
|
2012-03-19 19:05:23 +04:00
|
|
|
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;
|
2012-04-17 14:31:27 +04:00
|
|
|
|
|
|
|
if (ctx == NULL) {
|
2013-08-18 18:21:18 +04:00
|
|
|
errno = EBADF;
|
2013-07-10 16:42:12 +04:00
|
|
|
return rc;
|
2012-04-17 14:31:27 +04:00
|
|
|
}
|
|
|
|
|
2013-07-10 16:42:12 +04:00
|
|
|
if (visitor == NULL || tree == NULL) {
|
|
|
|
ctx->status_code = CSYNC_STATUS_PARAM_ERROR;
|
|
|
|
return rc;
|
2012-03-30 17:33:13 +04:00
|
|
|
}
|
|
|
|
|
2012-10-27 17:10:16 +04:00
|
|
|
tw_ctx.userdata = ctx->callbacks.userdata;
|
2012-03-19 19:05:23 +04:00
|
|
|
tw_ctx.user_visitor = visitor;
|
|
|
|
tw_ctx.instruction_filter = filter;
|
|
|
|
|
2012-10-27 17:10:16 +04:00
|
|
|
ctx->callbacks.userdata = &tw_ctx;
|
2012-03-19 19:05:23 +04:00
|
|
|
|
|
|
|
rc = c_rbtree_walk(tree, (void*) ctx, _csync_treewalk_visitor);
|
2012-12-17 20:22:24 +04:00
|
|
|
if( rc < 0 ) {
|
2013-08-18 18:21:18 +04:00
|
|
|
if( ctx->status_code == CSYNC_STATUS_OK )
|
2013-08-18 19:26:45 +04:00
|
|
|
ctx->status_code = csync_errno_to_status(errno, CSYNC_STATUS_TREE_ERROR);
|
2012-12-17 20:22:24 +04:00
|
|
|
}
|
2012-10-27 17:10:16 +04:00
|
|
|
ctx->callbacks.userdata = tw_ctx.userdata;
|
2012-03-19 19:05:23 +04:00
|
|
|
|
|
|
|
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;
|
2012-03-30 17:33:13 +04:00
|
|
|
int rc = -1;
|
2012-03-19 19:05:23 +04:00
|
|
|
|
2013-08-18 18:21:18 +04:00
|
|
|
if(ctx != NULL) {
|
2013-07-10 17:04:56 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_OK;
|
2012-03-19 19:05:23 +04:00
|
|
|
tree = ctx->remote.tree;
|
|
|
|
}
|
2012-04-17 14:31:27 +04:00
|
|
|
|
|
|
|
/* all error handling in the called function */
|
2012-03-30 17:33:13 +04:00
|
|
|
rc = _csync_walk_tree(ctx, tree, visitor, filter);
|
|
|
|
return rc;
|
2012-03-19 19:05:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
2012-03-30 17:33:13 +04:00
|
|
|
int rc = -1;
|
2012-03-19 19:05:23 +04:00
|
|
|
|
2013-07-10 17:04:56 +04:00
|
|
|
if (ctx != NULL) {
|
|
|
|
ctx->status_code = CSYNC_STATUS_OK;
|
2012-03-19 19:05:23 +04:00
|
|
|
tree = ctx->local.tree;
|
|
|
|
}
|
2012-04-17 14:31:27 +04:00
|
|
|
|
|
|
|
/* all error handling in the called function */
|
2012-03-30 17:33:13 +04:00
|
|
|
rc = _csync_walk_tree(ctx, tree, visitor, filter);
|
|
|
|
return rc;
|
2012-03-19 19:05:23 +04:00
|
|
|
}
|
|
|
|
|
2008-06-02 17:11:45 +04:00
|
|
|
static void _tree_destructor(void *data) {
|
2008-04-28 15:42:10 +04:00
|
|
|
csync_file_stat_t *freedata = NULL;
|
|
|
|
|
|
|
|
freedata = (csync_file_stat_t *) data;
|
2013-07-02 20:25:17 +04:00
|
|
|
csync_file_stat_free(freedata);
|
2008-04-28 15:42:10 +04:00
|
|
|
}
|
|
|
|
|
2013-07-26 14:37:54 +04:00
|
|
|
/* 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;
|
2013-08-16 22:20:20 +04:00
|
|
|
|
|
|
|
SAFE_FREE(ctx->statedb.file);
|
2013-07-26 14:37:54 +04:00
|
|
|
}
|
|
|
|
|
2013-04-20 12:51:27 +04:00
|
|
|
int csync_commit(CSYNC *ctx) {
|
|
|
|
int rc = 0;
|
|
|
|
|
2013-04-22 19:05:13 +04:00
|
|
|
if (ctx == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-04-20 12:51:27 +04:00
|
|
|
|
2013-04-20 13:07:20 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_OK;
|
2013-05-08 17:28:26 +04:00
|
|
|
|
2013-10-31 14:14:21 +04:00
|
|
|
if (ctx->statedb.db != NULL
|
|
|
|
&& csync_statedb_close(ctx->statedb.file, ctx->statedb.db, 0) < 0) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "ERR: closing of statedb failed.");
|
|
|
|
rc = -1;
|
|
|
|
}
|
2013-12-03 17:08:18 +04:00
|
|
|
ctx->statedb.db = NULL;
|
2013-10-31 14:14:21 +04:00
|
|
|
|
2013-04-23 18:49:06 +04:00
|
|
|
rc = csync_vio_commit(ctx);
|
|
|
|
if (rc < 0) {
|
2013-04-20 12:51:27 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "commit failed: %s",
|
|
|
|
ctx->error_string ? ctx->error_string : "");
|
2013-04-24 16:35:19 +04:00
|
|
|
goto out;
|
2013-04-20 12:51:27 +04:00
|
|
|
}
|
2013-04-20 12:51:27 +04:00
|
|
|
|
2013-10-31 13:05:15 +04:00
|
|
|
_csync_clean_ctx(ctx);
|
|
|
|
|
|
|
|
ctx->remote.read_from_db = 0;
|
|
|
|
|
2013-04-20 12:51:27 +04:00
|
|
|
/* Create new trees */
|
2013-04-23 18:49:06 +04:00
|
|
|
rc = c_rbtree_create(&ctx->local.tree, _key_cmp, _data_cmp);
|
|
|
|
if (rc < 0) {
|
2013-04-20 13:07:20 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_TREE_ERROR;
|
2013-04-20 12:51:27 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-04-23 18:49:06 +04:00
|
|
|
rc = c_rbtree_create(&ctx->remote.tree, _key_cmp, _data_cmp);
|
|
|
|
if (rc < 0) {
|
2013-04-20 13:07:20 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_TREE_ERROR;
|
2013-04-20 12:51:27 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-05-06 19:14:17 +04:00
|
|
|
/* reset the progress */
|
2013-07-25 17:36:46 +04:00
|
|
|
ctx->overall_progress.file_count = 0;
|
|
|
|
ctx->overall_progress.current_file_no = 0;
|
|
|
|
ctx->overall_progress.byte_sum = 0;
|
|
|
|
ctx->overall_progress.byte_current = 0;
|
2013-05-06 19:14:17 +04:00
|
|
|
|
2013-04-20 12:51:27 +04:00
|
|
|
ctx->status = CSYNC_STATUS_INIT;
|
2013-04-22 12:22:20 +04:00
|
|
|
SAFE_FREE(ctx->error_string);
|
2013-04-22 17:39:43 +04:00
|
|
|
|
2013-04-24 16:35:19 +04:00
|
|
|
rc = 0;
|
2013-05-04 18:10:11 +04:00
|
|
|
|
2013-04-23 18:49:06 +04:00
|
|
|
out:
|
2013-04-20 12:51:27 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int csync_destroy(CSYNC *ctx) {
|
|
|
|
char *lock = NULL;
|
2013-04-23 18:49:06 +04:00
|
|
|
int rc;
|
2013-04-20 12:51:27 +04:00
|
|
|
|
|
|
|
if (ctx == NULL) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2013-04-20 13:07:20 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_OK;
|
2013-04-20 12:51:27 +04:00
|
|
|
|
2013-10-31 14:14:21 +04:00
|
|
|
if (ctx->statedb.db != NULL
|
|
|
|
&& csync_statedb_close(ctx->statedb.file, ctx->statedb.db, 0) < 0) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "ERR: closing of statedb failed.");
|
|
|
|
rc = -1;
|
|
|
|
}
|
2013-12-03 17:08:18 +04:00
|
|
|
ctx->statedb.db = NULL;
|
2013-10-31 14:14:21 +04:00
|
|
|
|
2013-04-20 12:51:27 +04:00
|
|
|
csync_vio_shutdown(ctx);
|
|
|
|
|
2013-07-15 16:22:04 +04:00
|
|
|
/* destroy exclude list */
|
2008-03-26 13:11:20 +03:00
|
|
|
csync_exclude_destroy(ctx);
|
|
|
|
|
2008-06-18 12:41:49 +04:00
|
|
|
/* remove the lock file */
|
2013-04-23 18:49:06 +04:00
|
|
|
rc = asprintf(&lock, "%s/%s", ctx->options.config_dir, CSYNC_LOCK_FILE);
|
|
|
|
if (rc > 0) {
|
2013-03-01 12:59:55 +04:00
|
|
|
csync_lock_remove(lock);
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2013-07-26 14:37:54 +04:00
|
|
|
_csync_clean_ctx(ctx);
|
2013-01-04 23:45:10 +04:00
|
|
|
|
2008-04-23 15:05:40 +04:00
|
|
|
SAFE_FREE(ctx->local.uri);
|
|
|
|
SAFE_FREE(ctx->remote.uri);
|
2008-02-27 20:56:47 +03:00
|
|
|
SAFE_FREE(ctx->options.config_dir);
|
2012-12-14 19:48:47 +04:00
|
|
|
SAFE_FREE(ctx->error_string);
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-12-07 16:02:46 +04:00
|
|
|
#ifdef WITH_ICONV
|
|
|
|
c_close_iconv();
|
|
|
|
#endif
|
|
|
|
|
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) {
|
|
|
|
if (req_version <= LIBCSYNC_VERSION_INT) {
|
|
|
|
return CSYNC_STRINGIFY(LIBCSYNC_VERSION);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2008-05-20 17:50:02 +04:00
|
|
|
int csync_add_exclude_list(CSYNC *ctx, const char *path) {
|
2008-05-20 18:31:10 +04:00
|
|
|
if (ctx == NULL || path == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-05-20 17:50:02 +04:00
|
|
|
return csync_exclude_load(ctx, path);
|
|
|
|
}
|
|
|
|
|
2013-07-15 16:22:04 +04:00
|
|
|
void csync_clear_exclude_list(CSYNC *ctx)
|
|
|
|
{
|
|
|
|
csync_exclude_clear(ctx);
|
|
|
|
}
|
|
|
|
|
2009-03-26 13:24:34 +03:00
|
|
|
const char *csync_get_config_dir(CSYNC *ctx) {
|
2008-05-20 18:33:03 +04:00
|
|
|
if (ctx == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx->options.config_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
int csync_set_config_dir(CSYNC *ctx, const char *path) {
|
|
|
|
if (ctx == NULL || path == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SAFE_FREE(ctx->options.config_dir);
|
|
|
|
ctx->options.config_dir = c_strdup(path);
|
|
|
|
if (ctx->options.config_dir == NULL) {
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_MEMORY_ERROR;
|
2008-05-20 18:33:03 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-09 12:10:00 +04:00
|
|
|
int csync_enable_statedb(CSYNC *ctx) {
|
2008-06-24 15:36:47 +04:00
|
|
|
if (ctx == NULL) {
|
2008-05-20 18:33:03 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_OK;
|
2008-05-20 18:33:03 +04:00
|
|
|
|
2008-06-24 19:39:46 +04:00
|
|
|
if (ctx->status & CSYNC_STATUS_INIT) {
|
|
|
|
fprintf(stderr, "This function must be called before initialization.");
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_CSYNC_STATUS_ERROR;
|
2008-06-24 19:39:46 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-07-09 12:10:00 +04:00
|
|
|
ctx->statedb.disabled = 0;
|
2008-05-20 18:33:03 +04:00
|
|
|
|
2008-06-24 15:36:47 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2008-05-20 18:33:03 +04:00
|
|
|
|
2008-07-09 12:10:00 +04:00
|
|
|
int csync_disable_statedb(CSYNC *ctx) {
|
2008-06-24 15:36:47 +04:00
|
|
|
if (ctx == NULL) {
|
2008-05-20 18:33:03 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_OK;
|
2008-05-20 18:33:03 +04:00
|
|
|
|
2008-06-24 19:39:46 +04:00
|
|
|
if (ctx->status & CSYNC_STATUS_INIT) {
|
|
|
|
fprintf(stderr, "This function must be called before initialization.");
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_CSYNC_STATUS_ERROR;
|
2008-06-24 19:39:46 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-07-09 12:10:00 +04:00
|
|
|
ctx->statedb.disabled = 1;
|
2008-06-24 15:36:47 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-09 12:10:00 +04:00
|
|
|
int csync_is_statedb_disabled(CSYNC *ctx) {
|
2008-06-24 15:36:47 +04:00
|
|
|
if (ctx == NULL) {
|
2008-05-21 16:46:42 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2008-07-09 12:10:00 +04:00
|
|
|
return ctx->statedb.disabled;
|
2008-05-20 18:33:03 +04:00
|
|
|
}
|
|
|
|
|
2008-06-24 15:36:27 +04:00
|
|
|
int csync_set_auth_callback(CSYNC *ctx, csync_auth_callback cb) {
|
2008-05-20 18:33:03 +04:00
|
|
|
if (ctx == NULL || cb == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-05-27 16:15:44 +04:00
|
|
|
if (ctx->status & CSYNC_STATUS_INIT) {
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_CSYNC_STATUS_ERROR;
|
2008-05-20 17:50:02 +04:00
|
|
|
fprintf(stderr, "This function must be called before initialization.");
|
2008-05-20 18:33:03 +04:00
|
|
|
return -1;
|
2008-05-13 18:04:29 +04:00
|
|
|
}
|
2012-10-27 17:10:16 +04:00
|
|
|
ctx->callbacks.auth_function = cb;
|
2008-05-20 18:33:03 +04:00
|
|
|
|
|
|
|
return 0;
|
2008-05-13 15:38:03 +04:00
|
|
|
}
|
|
|
|
|
2009-03-26 13:24:34 +03:00
|
|
|
const char *csync_get_statedb_file(CSYNC *ctx) {
|
2008-06-02 19:02:22 +04:00
|
|
|
if (ctx == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_OK;
|
2008-06-02 19:02:22 +04:00
|
|
|
|
2008-07-09 12:10:00 +04:00
|
|
|
return c_strdup(ctx->statedb.file);
|
2008-06-02 19:02:22 +04:00
|
|
|
}
|
|
|
|
|
2009-03-26 13:09:46 +03:00
|
|
|
void *csync_get_userdata(CSYNC *ctx) {
|
|
|
|
if (ctx == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-10-27 17:10:16 +04:00
|
|
|
return ctx->callbacks.userdata;
|
2009-03-26 13:09:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int csync_set_userdata(CSYNC *ctx, void *userdata) {
|
|
|
|
if (ctx == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-10-27 17:10:16 +04:00
|
|
|
ctx->callbacks.userdata = userdata;
|
2009-03-26 13:09:46 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-24 15:36:27 +04:00
|
|
|
csync_auth_callback csync_get_auth_callback(CSYNC *ctx) {
|
2008-05-20 18:33:03 +04:00
|
|
|
if (ctx == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-10-27 17:10:16 +04:00
|
|
|
return ctx->callbacks.auth_function;
|
2012-10-27 17:15:59 +04:00
|
|
|
}
|
|
|
|
|
2008-05-20 18:33:03 +04:00
|
|
|
int csync_set_status(CSYNC *ctx, int status) {
|
|
|
|
if (ctx == NULL || status < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-05-05 13:09:16 +04:00
|
|
|
ctx->status = status;
|
2008-05-20 18:33:03 +04:00
|
|
|
|
|
|
|
return 0;
|
2008-05-05 13:09:16 +04:00
|
|
|
}
|
|
|
|
|
2013-11-25 01:17:36 +04:00
|
|
|
CSYNC_STATUS csync_get_status(CSYNC *ctx) {
|
2008-06-24 13:13:56 +04:00
|
|
|
if (ctx == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-11-25 01:17:36 +04:00
|
|
|
return ctx->status_code;
|
2008-05-05 13:09:16 +04:00
|
|
|
}
|
|
|
|
|
2011-04-06 18:43:04 +04:00
|
|
|
int csync_enable_conflictcopys(CSYNC* ctx){
|
|
|
|
if (ctx == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->status & CSYNC_STATUS_INIT) {
|
|
|
|
fprintf(stderr, "This function must be called before initialization.");
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_CSYNC_STATUS_ERROR;
|
2011-04-06 18:43:04 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->options.with_conflict_copys=true;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-17 14:31:27 +04:00
|
|
|
int csync_set_local_only(CSYNC *ctx, bool local_only) {
|
2012-02-27 15:18:02 +04:00
|
|
|
if (ctx == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-08-18 18:21:18 +04:00
|
|
|
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_OK;
|
2012-02-27 15:18:02 +04:00
|
|
|
|
|
|
|
if (ctx->status & CSYNC_STATUS_INIT) {
|
2013-08-18 18:21:18 +04:00
|
|
|
fprintf(stderr, "csync_set_local_only: This function must be called before initialization.");
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_CSYNC_STATUS_ERROR;
|
2012-02-27 15:18:02 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->options.local_only_mode=local_only;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-17 14:31:27 +04:00
|
|
|
bool csync_get_local_only(CSYNC *ctx) {
|
2012-02-27 15:18:02 +04:00
|
|
|
if (ctx == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-03-12 20:01:39 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_OK;
|
2012-02-27 15:18:02 +04:00
|
|
|
|
|
|
|
return ctx->options.local_only_mode;
|
|
|
|
}
|
|
|
|
|
2013-03-13 17:46:08 +04:00
|
|
|
const char *csync_get_status_string(CSYNC *ctx)
|
2012-12-04 16:42:16 +04:00
|
|
|
{
|
2013-03-13 17:46:08 +04:00
|
|
|
return csync_vio_get_status_string(ctx);
|
2012-12-04 16:42:16 +04:00
|
|
|
}
|
|
|
|
|
2012-12-07 16:02:46 +04:00
|
|
|
#ifdef WITH_ICONV
|
|
|
|
int csync_set_iconv_codec(const char *from)
|
|
|
|
{
|
|
|
|
c_close_iconv();
|
|
|
|
|
|
|
|
if (from != NULL) {
|
|
|
|
c_setup_iconv(from);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-05-08 19:33:50 +04:00
|
|
|
void csync_request_abort(CSYNC *ctx)
|
2013-05-08 17:28:26 +04:00
|
|
|
{
|
2013-05-16 19:37:30 +04:00
|
|
|
if (ctx != NULL) {
|
2013-05-08 17:28:26 +04:00
|
|
|
ctx->abort = true;
|
2013-05-16 19:37:30 +04:00
|
|
|
}
|
2013-05-08 17:28:26 +04:00
|
|
|
}
|
|
|
|
|
2013-05-08 19:33:50 +04:00
|
|
|
void csync_resume(CSYNC *ctx)
|
|
|
|
{
|
2013-05-16 19:37:30 +04:00
|
|
|
if (ctx != NULL) {
|
2013-05-08 19:33:50 +04:00
|
|
|
ctx->abort = false;
|
2013-05-16 19:37:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int csync_abort_requested(CSYNC *ctx)
|
|
|
|
{
|
|
|
|
if (ctx != NULL) {
|
|
|
|
return ctx->abort;
|
|
|
|
} else {
|
|
|
|
return (1 == 0);
|
|
|
|
}
|
2013-05-08 19:33:50 +04:00
|
|
|
}
|
|
|
|
|
2013-07-02 20:25:17 +04:00
|
|
|
void csync_file_stat_free(csync_file_stat_t *st)
|
|
|
|
{
|
|
|
|
if (st) {
|
2013-11-13 17:29:31 +04:00
|
|
|
SAFE_FREE(st->etag);
|
2013-07-02 20:25:17 +04:00
|
|
|
SAFE_FREE(st->error_string);
|
|
|
|
SAFE_FREE(st->destpath);
|
|
|
|
SAFE_FREE(st);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-04 16:42:16 +04:00
|
|
|
int csync_set_module_property(CSYNC* ctx, const char* key, void* value)
|
|
|
|
{
|
|
|
|
return csync_vio_set_property(ctx, key, value);
|
|
|
|
}
|
|
|
|
|
2013-07-25 17:36:46 +04:00
|
|
|
int csync_set_progress_callback(CSYNC* ctx, csync_progress_callback cb)
|
2013-05-06 19:14:17 +04:00
|
|
|
{
|
|
|
|
if (ctx == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (cb == NULL ) {
|
2013-05-06 19:14:17 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_PARAM_ERROR;
|
2013-05-06 19:14:17 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-07-25 17:36:46 +04:00
|
|
|
ctx->callbacks.progress_cb = cb;
|
2013-05-06 19:14:17 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_OK;
|
2013-05-06 19:14:17 +04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-25 17:36:46 +04:00
|
|
|
csync_progress_callback csync_get_progress_callback(CSYNC *ctx)
|
2013-05-06 19:14:17 +04:00
|
|
|
{
|
|
|
|
if (ctx == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-25 17:36:46 +04:00
|
|
|
return ctx->callbacks.progress_cb;
|
2013-05-06 19:14:17 +04:00
|
|
|
}
|