2008-05-15 15:50:34 +04: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>
|
2008-05-15 15:50:34 +04: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-05-15 15:50:34 +04:00
|
|
|
*
|
2013-07-23 19:31:55 +04:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
2008-05-15 15:50:34 +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-05-15 15:50:34 +04: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-05-15 15:50:34 +04:00
|
|
|
*/
|
|
|
|
|
2014-01-15 15:20:03 +04:00
|
|
|
#include "config_csync.h"
|
2012-03-02 19:47:34 +04:00
|
|
|
|
2008-05-15 15:50:34 +04:00
|
|
|
#include "csync_private.h"
|
|
|
|
#include "csync_reconcile.h"
|
|
|
|
#include "csync_util.h"
|
2012-08-02 17:58:54 +04:00
|
|
|
#include "csync_statedb.h"
|
2013-01-04 23:45:10 +04:00
|
|
|
#include "csync_rename.h"
|
2012-08-02 17:58:54 +04:00
|
|
|
#include "c_jhash.h"
|
2008-05-15 15:50:34 +04:00
|
|
|
|
|
|
|
#define CSYNC_LOG_CATEGORY_NAME "csync.reconciler"
|
|
|
|
#include "csync_log.h"
|
|
|
|
|
2013-07-31 15:12:10 +04:00
|
|
|
#include "inttypes.h"
|
|
|
|
|
2012-11-08 17:52:14 +04:00
|
|
|
#define ACCEPTED_TIME_DIFF 5
|
2012-11-20 20:33:25 +04:00
|
|
|
#define ONE_HOUR 3600
|
|
|
|
|
2014-04-03 19:42:08 +04:00
|
|
|
|
|
|
|
/* Check if a file is ignored because one parent is ignored.
|
|
|
|
* return the node of the ignored directoy if it's the case, or NULL if it is not ignored */
|
|
|
|
static c_rbnode_t *_csync_check_ignored(c_rbtree_t *tree, const char *path, int pathlen) {
|
|
|
|
uint64_t h = 0;
|
|
|
|
c_rbnode_t *node = NULL;
|
|
|
|
|
|
|
|
/* compute the size of the parent directory */
|
|
|
|
int parentlen = pathlen - 1;
|
|
|
|
while (parentlen > 0 && path[parentlen] != '/') {
|
|
|
|
parentlen--;
|
|
|
|
}
|
|
|
|
if (parentlen <= 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
h = c_jhash64((uint8_t *) path, parentlen, 0);
|
|
|
|
node = c_rbtree_find(tree, &h);
|
|
|
|
if (node) {
|
|
|
|
csync_file_stat_t *n = (csync_file_stat_t*)node->data;
|
|
|
|
if (n->instruction == CSYNC_INSTRUCTION_IGNORE) {
|
|
|
|
/* Yes, we are ignored */
|
|
|
|
return node;
|
|
|
|
} else {
|
|
|
|
/* Not ignored */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Try if the parent itself is ignored */
|
|
|
|
return _csync_check_ignored(tree, path, parentlen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-03 11:49:12 +04:00
|
|
|
/*
|
|
|
|
* We merge replicas at the file level. The merged replica contains the
|
|
|
|
* superset of files that are on the local machine and server copies of
|
|
|
|
* the replica. In the case where the same file is in both the local
|
|
|
|
* and server copy, the file that was modified most recently is used.
|
|
|
|
* This means that new files are not deleted, and updated versions of
|
|
|
|
* existing files are not overwritten.
|
|
|
|
*
|
|
|
|
* When a file is updated, the merge algorithm compares the destination
|
|
|
|
* file with the the source file. If the destination file is newer
|
|
|
|
* (timestamp is newer), it is not overwritten. If both files, on the
|
|
|
|
* source and the destination, have been changed, the newer file wins.
|
|
|
|
*/
|
2008-06-02 17:11:45 +04:00
|
|
|
static int _csync_merge_algorithm_visitor(void *obj, void *data) {
|
2012-08-23 18:33:41 +04:00
|
|
|
csync_file_stat_t *cur = NULL;
|
|
|
|
csync_file_stat_t *other = NULL;
|
|
|
|
csync_file_stat_t *tmp = NULL;
|
|
|
|
uint64_t h = 0;
|
|
|
|
int len = 0;
|
2012-08-02 17:58:54 +04:00
|
|
|
|
2012-08-23 18:33:41 +04:00
|
|
|
CSYNC *ctx = NULL;
|
|
|
|
c_rbtree_t *tree = NULL;
|
|
|
|
c_rbnode_t *node = NULL;
|
2008-05-15 15:50:34 +04:00
|
|
|
|
2012-08-23 18:33:41 +04:00
|
|
|
cur = (csync_file_stat_t *) obj;
|
|
|
|
ctx = (CSYNC *) data;
|
2008-05-15 15:50:34 +04:00
|
|
|
|
2012-08-23 18:33:41 +04:00
|
|
|
/* we need the opposite tree! */
|
|
|
|
switch (ctx->current) {
|
2008-05-15 15:50:34 +04:00
|
|
|
case LOCAL_REPLICA:
|
2012-08-23 18:33:41 +04:00
|
|
|
tree = ctx->remote.tree;
|
|
|
|
break;
|
2012-10-27 21:27:14 +04:00
|
|
|
case REMOTE_REPLICA:
|
2012-08-23 18:33:41 +04:00
|
|
|
tree = ctx->local.tree;
|
2008-05-15 15:50:34 +04:00
|
|
|
break;
|
2012-08-23 18:33:41 +04:00
|
|
|
default:
|
2008-05-15 15:50:34 +04:00
|
|
|
break;
|
2012-08-23 18:33:41 +04:00
|
|
|
}
|
2012-08-02 17:58:54 +04:00
|
|
|
|
2012-08-23 18:33:41 +04:00
|
|
|
node = c_rbtree_find(tree, &cur->phash);
|
2013-01-04 23:45:10 +04:00
|
|
|
|
2013-10-30 20:34:13 +04:00
|
|
|
if (!node) {
|
2013-01-04 23:45:10 +04:00
|
|
|
/* Check the renamed path as well. */
|
|
|
|
char *renamed_path = csync_rename_adjust_path(ctx, cur->path);
|
|
|
|
if (!c_streq(renamed_path, cur->path)) {
|
|
|
|
len = strlen( renamed_path );
|
|
|
|
h = c_jhash64((uint8_t *) renamed_path, len, 0);
|
|
|
|
node = c_rbtree_find(tree, &h);
|
|
|
|
}
|
|
|
|
SAFE_FREE(renamed_path);
|
|
|
|
}
|
2014-04-03 19:42:08 +04:00
|
|
|
if (!node) {
|
|
|
|
/* Check if it is ignored */
|
|
|
|
node = _csync_check_ignored(tree, cur->path, cur->pathlen);
|
|
|
|
/* If it is ignored, other->instruction will be IGNORE so this one will also be ignored */
|
|
|
|
}
|
2013-01-05 14:32:24 +04:00
|
|
|
|
2012-08-23 18:33:41 +04:00
|
|
|
/* file only found on current replica */
|
|
|
|
if (node == NULL) {
|
|
|
|
switch(cur->instruction) {
|
|
|
|
/* file has been modified */
|
|
|
|
case CSYNC_INSTRUCTION_EVAL:
|
|
|
|
cur->instruction = CSYNC_INSTRUCTION_NEW;
|
|
|
|
break;
|
|
|
|
/* file has been removed on the opposite replica */
|
|
|
|
case CSYNC_INSTRUCTION_NONE:
|
|
|
|
cur->instruction = CSYNC_INSTRUCTION_REMOVE;
|
|
|
|
break;
|
2013-11-14 20:05:58 +04:00
|
|
|
case CSYNC_INSTRUCTION_EVAL_RENAME:
|
2012-08-23 18:33:41 +04:00
|
|
|
if(ctx->current == LOCAL_REPLICA ) {
|
|
|
|
/* use the old name to find the "other" node */
|
2014-03-19 19:22:25 +04:00
|
|
|
tmp = csync_statedb_get_stat_by_inode(ctx, cur->inode);
|
2013-11-05 21:03:51 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "Finding opposite temp through inode %" PRIu64 ": %s",
|
|
|
|
cur->inode, tmp ? "true":"false");
|
2013-10-30 20:34:13 +04:00
|
|
|
} else if( ctx->current == REMOTE_REPLICA ) {
|
2014-03-19 19:22:25 +04:00
|
|
|
tmp = csync_statedb_get_stat_by_file_id(ctx, cur->file_id);
|
2013-11-05 21:03:51 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "Finding opposite temp through file ID %s: %s",
|
|
|
|
cur->file_id, tmp ? "true":"false");
|
2013-10-30 20:34:13 +04:00
|
|
|
} else {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Unknown replica...");
|
2012-08-03 19:48:44 +04:00
|
|
|
}
|
2013-11-05 21:03:51 +04:00
|
|
|
|
2013-10-30 20:34:13 +04:00
|
|
|
if( tmp ) {
|
|
|
|
if( tmp->path ) {
|
|
|
|
len = strlen( tmp->path );
|
|
|
|
h = c_jhash64((uint8_t *) tmp->path, len, 0);
|
2014-02-12 16:44:32 +04:00
|
|
|
/* First, check that the file is NOT in our tree (another file with the same name was added) */
|
|
|
|
node = c_rbtree_find(ctx->current == REMOTE_REPLICA ? ctx->remote.tree : ctx->local.tree, &h);
|
|
|
|
if (node) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "Origin found in our tree : %s", tmp->path);
|
|
|
|
} else {
|
|
|
|
/* Find the temporar file in the other tree. */
|
|
|
|
node = c_rbtree_find(tree, &h);
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "PHash of temporary opposite (%s): %" PRIu64 " %s",
|
|
|
|
tmp->path , h, node ? "found": "not found" );
|
|
|
|
if (node) {
|
|
|
|
other = (csync_file_stat_t*)node->data;
|
|
|
|
} else {
|
|
|
|
/* the renamed file could not be found in the opposite tree. That is because it
|
|
|
|
* is not longer existing there, maybe because it was renamed or deleted.
|
|
|
|
* The journal is cleaned up later after propagation.
|
|
|
|
*/
|
|
|
|
}
|
2013-11-05 21:03:51 +04:00
|
|
|
}
|
2013-10-30 20:34:13 +04:00
|
|
|
}
|
2013-11-05 21:03:51 +04:00
|
|
|
|
2013-10-30 20:34:13 +04:00
|
|
|
if(!other) {
|
|
|
|
cur->instruction = CSYNC_INSTRUCTION_NEW;
|
|
|
|
} else if (other->instruction == CSYNC_INSTRUCTION_NONE
|
|
|
|
|| cur->type == CSYNC_FTW_TYPE_DIR) {
|
|
|
|
other->instruction = CSYNC_INSTRUCTION_RENAME;
|
|
|
|
other->destpath = c_strdup( cur->path );
|
2013-11-14 16:42:42 +04:00
|
|
|
if( !c_streq(cur->file_id, "") ) {
|
2013-11-14 14:30:51 +04:00
|
|
|
csync_vio_set_file_id( other->file_id, cur->file_id );
|
|
|
|
}
|
2014-05-06 14:55:54 +04:00
|
|
|
other->inode = cur->inode;
|
2013-10-30 20:34:13 +04:00
|
|
|
cur->instruction = CSYNC_INSTRUCTION_NONE;
|
|
|
|
} else if (other->instruction == CSYNC_INSTRUCTION_REMOVE) {
|
|
|
|
other->instruction = CSYNC_INSTRUCTION_RENAME;
|
|
|
|
other->destpath = c_strdup( cur->path );
|
2013-11-14 14:30:51 +04:00
|
|
|
|
2013-11-14 16:42:42 +04:00
|
|
|
if( !c_streq(cur->file_id, "") ) {
|
2013-11-14 14:30:51 +04:00
|
|
|
csync_vio_set_file_id( other->file_id, cur->file_id );
|
|
|
|
}
|
2014-05-06 14:55:54 +04:00
|
|
|
other->inode = cur->inode;
|
2013-10-30 20:34:13 +04:00
|
|
|
cur->instruction = CSYNC_INSTRUCTION_NONE;
|
2013-11-08 13:08:49 +04:00
|
|
|
} else if (other->instruction == CSYNC_INSTRUCTION_NEW) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "OOOO=> NEW detected in other tree!");
|
|
|
|
cur->instruction = CSYNC_INSTRUCTION_CONFLICT;
|
2013-10-30 20:34:13 +04:00
|
|
|
} else {
|
|
|
|
cur->instruction = CSYNC_INSTRUCTION_NONE;
|
|
|
|
other->instruction = CSYNC_INSTRUCTION_SYNC;
|
|
|
|
}
|
2014-02-12 16:44:32 +04:00
|
|
|
csync_file_stat_free(tmp);
|
2013-10-30 20:34:13 +04:00
|
|
|
}
|
|
|
|
|
2012-08-23 18:33:41 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2012-08-02 17:58:54 +04:00
|
|
|
}
|
2012-08-23 18:33:41 +04:00
|
|
|
} else {
|
2013-05-22 18:15:09 +04:00
|
|
|
bool is_equal_files = false;
|
2012-08-23 18:33:41 +04:00
|
|
|
/*
|
2008-05-15 15:50:34 +04:00
|
|
|
* file found on the other replica
|
|
|
|
*/
|
2012-08-23 18:33:41 +04:00
|
|
|
other = (csync_file_stat_t *) node->data;
|
|
|
|
|
|
|
|
switch (cur->instruction) {
|
2013-11-14 20:05:58 +04:00
|
|
|
case CSYNC_INSTRUCTION_EVAL_RENAME:
|
2012-12-12 16:53:19 +04:00
|
|
|
/* If the file already exist on the other side, we have a conflict.
|
|
|
|
Abort the rename and consider it is a new file. */
|
|
|
|
cur->instruction = CSYNC_INSTRUCTION_NEW;
|
|
|
|
/* fall trough */
|
2013-05-07 12:48:44 +04:00
|
|
|
/* file on current replica is changed or new */
|
|
|
|
case CSYNC_INSTRUCTION_EVAL:
|
2012-08-23 18:33:41 +04:00
|
|
|
case CSYNC_INSTRUCTION_NEW:
|
2014-04-28 18:57:56 +04:00
|
|
|
// This operation is usually a no-op and will by default return false
|
|
|
|
if (csync_file_locked_or_open(ctx->local.uri, cur->path)) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "[Reconciler] IGNORING file %s/%s since it is locked / open", ctx->local.uri, cur->path);
|
|
|
|
cur->instruction = CSYNC_INSTRUCTION_ERROR;
|
|
|
|
if (cur->error_status == CSYNC_STATUS_OK) // don't overwrite error
|
|
|
|
cur->error_status = CYSNC_STATUS_FILE_LOCKED_OR_OPEN;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
//CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "[Reconciler] not ignoring file %s/%s", ctx->local.uri, cur->path);
|
|
|
|
}
|
2012-08-23 18:33:41 +04:00
|
|
|
switch (other->instruction) {
|
2013-05-07 12:48:44 +04:00
|
|
|
/* file on other replica is changed or new */
|
2012-08-23 18:33:41 +04:00
|
|
|
case CSYNC_INSTRUCTION_NEW:
|
2013-05-07 12:48:44 +04:00
|
|
|
case CSYNC_INSTRUCTION_EVAL:
|
2013-05-22 18:09:54 +04:00
|
|
|
if (other->type == CSYNC_VIO_FILE_TYPE_DIRECTORY &&
|
|
|
|
cur->type == CSYNC_VIO_FILE_TYPE_DIRECTORY) {
|
|
|
|
is_equal_files = (other->modtime == cur->modtime);
|
|
|
|
} else {
|
2013-05-24 17:43:42 +04:00
|
|
|
is_equal_files = ((other->size == cur->size) && (other->modtime == cur->modtime));
|
2013-05-22 18:09:54 +04:00
|
|
|
}
|
|
|
|
if (is_equal_files) {
|
2013-05-07 12:48:44 +04:00
|
|
|
/* The files are considered equal. */
|
2014-04-18 20:27:27 +04:00
|
|
|
cur->instruction = CSYNC_INSTRUCTION_NONE;
|
2012-08-23 18:33:41 +04:00
|
|
|
other->instruction = CSYNC_INSTRUCTION_NONE;
|
2012-08-15 17:32:10 +04:00
|
|
|
|
2014-06-03 13:14:07 +04:00
|
|
|
/* update DB with new etag from remote */
|
|
|
|
if (ctx->current == LOCAL_REPLICA) {
|
|
|
|
other->should_update_etag = true;
|
|
|
|
} else {
|
|
|
|
cur->should_update_etag = true;
|
|
|
|
}
|
2013-05-07 12:48:44 +04:00
|
|
|
} else if(ctx->current == REMOTE_REPLICA) {
|
2012-08-23 18:33:41 +04:00
|
|
|
cur->instruction = CSYNC_INSTRUCTION_CONFLICT;
|
2013-05-07 12:48:44 +04:00
|
|
|
other->instruction = CSYNC_INSTRUCTION_NONE;
|
2012-08-23 18:33:41 +04:00
|
|
|
} else {
|
|
|
|
cur->instruction = CSYNC_INSTRUCTION_NONE;
|
2013-05-07 12:48:44 +04:00
|
|
|
other->instruction = CSYNC_INSTRUCTION_CONFLICT;
|
2012-08-23 18:33:41 +04:00
|
|
|
}
|
2013-05-07 12:48:44 +04:00
|
|
|
|
2012-08-23 18:33:41 +04:00
|
|
|
break;
|
|
|
|
/* file on the other replica has not been modified */
|
|
|
|
case CSYNC_INSTRUCTION_NONE:
|
|
|
|
cur->instruction = CSYNC_INSTRUCTION_SYNC;
|
|
|
|
break;
|
2013-04-09 13:12:17 +04:00
|
|
|
case CSYNC_INSTRUCTION_IGNORE:
|
|
|
|
cur->instruction = CSYNC_INSTRUCTION_IGNORE;
|
|
|
|
break;
|
2012-08-23 18:33:41 +04:00
|
|
|
default:
|
|
|
|
break;
|
2008-05-15 21:20:01 +04:00
|
|
|
}
|
2012-08-23 18:33:41 +04:00
|
|
|
default:
|
2008-05-15 21:20:01 +04:00
|
|
|
break;
|
|
|
|
}
|
2012-08-23 18:33:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//hide instruction NONE messages when log level is set to debug,
|
|
|
|
//only show these messages on log level trace
|
|
|
|
if(cur->instruction ==CSYNC_INSTRUCTION_NONE)
|
|
|
|
{
|
|
|
|
if(cur->type == CSYNC_FTW_TYPE_DIR)
|
|
|
|
{
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE,
|
|
|
|
"%-20s dir: %s",
|
|
|
|
csync_instruction_str(cur->instruction),
|
|
|
|
cur->path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE,
|
|
|
|
"%-20s file: %s",
|
|
|
|
csync_instruction_str(cur->instruction),
|
|
|
|
cur->path);
|
2008-05-15 15:50:34 +04:00
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:33:41 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if(cur->type == CSYNC_FTW_TYPE_DIR)
|
|
|
|
{
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG,
|
|
|
|
"%-20s dir: %s",
|
|
|
|
csync_instruction_str(cur->instruction),
|
|
|
|
cur->path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG,
|
|
|
|
"%-20s file: %s",
|
|
|
|
csync_instruction_str(cur->instruction),
|
|
|
|
cur->path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2008-05-15 15:50:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int csync_reconcile_updates(CSYNC *ctx) {
|
2012-02-04 15:26:27 +04:00
|
|
|
int rc;
|
2008-05-15 15:50:34 +04:00
|
|
|
c_rbtree_t *tree = NULL;
|
|
|
|
|
|
|
|
switch (ctx->current) {
|
|
|
|
case LOCAL_REPLICA:
|
|
|
|
tree = ctx->local.tree;
|
|
|
|
break;
|
2012-10-19 16:23:20 +04:00
|
|
|
case REMOTE_REPLICA:
|
2008-05-15 15:50:34 +04:00
|
|
|
tree = ctx->remote.tree;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-06-02 17:11:45 +04:00
|
|
|
rc = c_rbtree_walk(tree, (void *) ctx, _csync_merge_algorithm_visitor);
|
2013-03-13 14:57:06 +04:00
|
|
|
if( rc < 0 ) {
|
|
|
|
ctx->status_code = CSYNC_STATUS_RECONCILE_ERROR;
|
|
|
|
}
|
2012-02-04 15:26:27 +04:00
|
|
|
return rc;
|
2008-05-15 15:50:34 +04:00
|
|
|
}
|
|
|
|
|
2009-05-13 12:12:07 +04:00
|
|
|
/* vim: set ts=8 sw=2 et cindent: */
|