2008-04-23 14:12:48 +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>
|
2013-12-11 17:54:34 +04:00
|
|
|
* Copyright (c) 2012-2013 by Klaas Freitag <freitag@owncloud.com>
|
2008-04-23 14:12:48 +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-04-23 14:12:48 +04:00
|
|
|
*
|
2013-07-23 19:31:55 +04:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
2008-04-23 14:12:48 +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-04-23 14:12:48 +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-04-23 14:12:48 +04:00
|
|
|
*/
|
|
|
|
|
2014-01-15 15:20:03 +04:00
|
|
|
#include "config_csync.h"
|
2012-03-02 19:47:34 +04:00
|
|
|
|
2008-04-23 14:12:48 +04:00
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
2008-08-01 17:57:19 +04:00
|
|
|
#include <string.h>
|
2013-07-31 15:12:10 +04:00
|
|
|
#include <inttypes.h>
|
2014-12-01 15:53:30 +03:00
|
|
|
#include <time.h>
|
2015-01-20 20:32:22 +03:00
|
|
|
#include <math.h>
|
2008-04-23 14:12:48 +04:00
|
|
|
|
|
|
|
#include "c_lib.h"
|
2008-04-26 12:42:20 +04:00
|
|
|
#include "c_jhash.h"
|
2008-04-28 18:50:25 +04:00
|
|
|
|
|
|
|
#include "csync_private.h"
|
2008-04-23 14:12:48 +04:00
|
|
|
#include "csync_exclude.h"
|
2008-07-09 11:57:19 +04:00
|
|
|
#include "csync_statedb.h"
|
2008-04-28 18:50:25 +04:00
|
|
|
#include "csync_update.h"
|
2008-04-30 18:24:50 +04:00
|
|
|
#include "csync_util.h"
|
2013-05-09 16:12:26 +04:00
|
|
|
#include "csync_misc.h"
|
2008-04-28 18:50:25 +04:00
|
|
|
|
2008-04-23 14:12:48 +04:00
|
|
|
#include "vio/csync_vio.h"
|
|
|
|
|
|
|
|
#define CSYNC_LOG_CATEGORY_NAME "csync.updater"
|
|
|
|
#include "csync_log.h"
|
2013-01-04 23:45:10 +04:00
|
|
|
#include "csync_rename.h"
|
2008-04-23 14:12:48 +04:00
|
|
|
|
2013-03-19 17:02:12 +04:00
|
|
|
/* calculate the hash of a given uri */
|
|
|
|
static uint64_t _hash_of_file(CSYNC *ctx, const char *file) {
|
|
|
|
const char *path;
|
|
|
|
int len;
|
2008-05-27 16:31:57 +04:00
|
|
|
uint64_t h = 0;
|
2008-04-23 14:12:48 +04:00
|
|
|
|
2013-03-19 17:02:12 +04:00
|
|
|
if( ctx && file ) {
|
|
|
|
path = file;
|
|
|
|
switch (ctx->current) {
|
2008-04-28 18:50:25 +04:00
|
|
|
case LOCAL_REPLICA:
|
2008-08-01 17:57:19 +04:00
|
|
|
if (strlen(path) <= strlen(ctx->local.uri)) {
|
2013-03-19 17:02:12 +04:00
|
|
|
return 0;
|
2008-08-01 17:57:19 +04:00
|
|
|
}
|
|
|
|
path += strlen(ctx->local.uri) + 1;
|
2008-04-28 18:50:25 +04:00
|
|
|
break;
|
2012-10-19 16:23:20 +04:00
|
|
|
case REMOTE_REPLICA:
|
2008-08-01 17:57:19 +04:00
|
|
|
if (strlen(path) <= strlen(ctx->remote.uri)) {
|
2013-03-19 17:02:12 +04:00
|
|
|
return 0;
|
2008-08-01 17:57:19 +04:00
|
|
|
}
|
|
|
|
path += strlen(ctx->remote.uri) + 1;
|
2008-04-28 18:50:25 +04:00
|
|
|
break;
|
|
|
|
default:
|
2008-08-01 17:57:19 +04:00
|
|
|
path = NULL;
|
2013-03-19 17:02:12 +04:00
|
|
|
return 0;
|
2008-04-28 18:50:25 +04:00
|
|
|
break;
|
2013-03-19 17:02:12 +04:00
|
|
|
}
|
|
|
|
len = strlen(path);
|
|
|
|
|
|
|
|
h = c_jhash64((uint8_t *) path, len, 0);
|
2008-04-28 18:50:25 +04:00
|
|
|
}
|
2013-03-19 17:02:12 +04:00
|
|
|
return h;
|
|
|
|
}
|
2008-04-26 12:42:20 +04:00
|
|
|
|
2014-04-24 13:51:48 +04:00
|
|
|
#ifdef NO_RENAME_EXTENSION
|
|
|
|
/* Return true if the two path have the same extension. false otherwise. */
|
|
|
|
static bool _csync_sameextension(const char *p1, const char *p2) {
|
|
|
|
/* Find pointer to the extensions */
|
|
|
|
const char *e1 = strrchr(p1, '.');
|
|
|
|
const char *e2 = strrchr(p2, '.');
|
|
|
|
|
|
|
|
/* If the found extension contains a '/', it is because the . was in the folder name
|
|
|
|
* => no extensions */
|
|
|
|
if (e1 && strchr(e1, '/')) e1 = NULL;
|
|
|
|
if (e2 && strchr(e2, '/')) e2 = NULL;
|
|
|
|
|
|
|
|
/* If none have extension, it is the same extension */
|
|
|
|
if (!e1 && !e2)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* c_streq takes care of the rest */
|
|
|
|
return c_streq(e1, e2);
|
|
|
|
}
|
|
|
|
#endif
|
2013-03-19 17:02:12 +04:00
|
|
|
|
2014-10-20 21:27:02 +04:00
|
|
|
static bool _last_db_return_error(CSYNC* ctx) {
|
|
|
|
return ctx->statedb.lastReturnValue != SQLITE_OK && ctx->statedb.lastReturnValue != SQLITE_DONE && ctx->statedb.lastReturnValue != SQLITE_ROW;
|
|
|
|
}
|
|
|
|
|
2016-01-27 16:11:37 +03:00
|
|
|
/*
|
|
|
|
* This static method is needed because the type members of the two structs use
|
|
|
|
* different enum values. A direct comparion is not neccessarily correct.
|
|
|
|
*
|
|
|
|
* tmp is csync_file_stat_t
|
|
|
|
* fs is csync_vio_file_stat_t with this vio type:
|
|
|
|
* enum csync_vio_file_type_e {
|
|
|
|
* CSYNC_VIO_FILE_TYPE_UNKNOWN,
|
|
|
|
* CSYNC_VIO_FILE_TYPE_REGULAR,
|
|
|
|
* CSYNC_VIO_FILE_TYPE_DIRECTORY,
|
|
|
|
* CSYNC_VIO_FILE_TYPE_FIFO,
|
|
|
|
* CSYNC_VIO_FILE_TYPE_SOCKET,
|
|
|
|
* CSYNC_VIO_FILE_TYPE_CHARACTER_DEVICE,
|
|
|
|
* CSYNC_VIO_FILE_TYPE_BLOCK_DEVICE,
|
|
|
|
* CSYNC_VIO_FILE_TYPE_SYMBOLIC_LINK
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* csync_file_stat_t can be:
|
|
|
|
* CSYNC_FTW_TYPE_SKIP, CSYNC_FTW_TYPE_FILE
|
|
|
|
* CSYNC_FTW_TYPE_DIR, CSYNC_FTW_TYPE_SLINK
|
|
|
|
*/
|
|
|
|
static bool _csync_filetype_different( const csync_file_stat_t *tmp, const csync_vio_file_stat_t *fs)
|
|
|
|
{
|
|
|
|
if( !(tmp && fs)) return false;
|
|
|
|
|
|
|
|
if( tmp->type == CSYNC_FTW_TYPE_SKIP ) return true;
|
|
|
|
|
|
|
|
if( tmp->type == CSYNC_FTW_TYPE_DIR && fs->type != CSYNC_VIO_FILE_TYPE_DIRECTORY )
|
|
|
|
return true;
|
|
|
|
if( tmp->type == CSYNC_FTW_TYPE_FILE && fs->type != CSYNC_VIO_FILE_TYPE_REGULAR )
|
|
|
|
return true;
|
|
|
|
if( tmp->type == CSYNC_FTW_TYPE_SLINK && fs->type != CSYNC_VIO_FILE_TYPE_SYMBOLIC_LINK )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false; // both are NOT different.
|
|
|
|
}
|
2015-01-20 20:32:22 +03:00
|
|
|
|
|
|
|
/* Return true if two mtime are considered equal
|
|
|
|
* We consider mtime that are one hour difference to be equal if they are one hour appart
|
|
|
|
* because on some system (FAT) the date is changing when the daylight saving is changing */
|
|
|
|
static bool _csync_mtime_equal(time_t a, time_t b)
|
|
|
|
{
|
|
|
|
if (a == b)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* 1h of difference +- 1 second because the accuracy of FAT is 2 seconds (#2438) */
|
|
|
|
if (fabs(3600 - fabs(difftime(a, b))) < 2)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-19 17:02:12 +04:00
|
|
|
static int _csync_detect_update(CSYNC *ctx, const char *file,
|
|
|
|
const csync_vio_file_stat_t *fs, const int type) {
|
|
|
|
uint64_t h = 0;
|
|
|
|
size_t len = 0;
|
|
|
|
size_t size = 0;
|
|
|
|
const char *path = NULL;
|
|
|
|
csync_file_stat_t *st = NULL;
|
|
|
|
csync_file_stat_t *tmp = NULL;
|
2013-09-02 19:23:02 +04:00
|
|
|
CSYNC_EXCLUDE_TYPE excluded;
|
2013-03-19 17:02:12 +04:00
|
|
|
|
|
|
|
if ((file == NULL) || (fs == NULL)) {
|
|
|
|
errno = EINVAL;
|
2013-03-13 14:57:06 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_PARAM_ERROR;
|
2013-03-19 17:02:12 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-03-22 19:29:04 +04:00
|
|
|
path = file;
|
|
|
|
switch (ctx->current) {
|
2008-04-28 18:50:25 +04:00
|
|
|
case LOCAL_REPLICA:
|
2008-08-01 17:57:19 +04:00
|
|
|
if (strlen(path) <= strlen(ctx->local.uri)) {
|
2013-03-13 14:57:06 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_PARAM_ERROR;
|
2008-08-01 17:57:19 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
path += strlen(ctx->local.uri) + 1;
|
2008-04-28 18:50:25 +04:00
|
|
|
break;
|
2012-10-19 16:23:20 +04:00
|
|
|
case REMOTE_REPLICA:
|
2008-08-01 17:57:19 +04:00
|
|
|
if (strlen(path) <= strlen(ctx->remote.uri)) {
|
2013-03-13 14:57:06 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_PARAM_ERROR;
|
2008-08-01 17:57:19 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
path += strlen(ctx->remote.uri) + 1;
|
2008-04-28 18:50:25 +04:00
|
|
|
break;
|
|
|
|
default:
|
2008-08-01 17:57:19 +04:00
|
|
|
path = NULL;
|
2013-03-13 14:57:06 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_PARAM_ERROR;
|
2013-03-22 19:29:04 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2013-03-19 17:02:12 +04:00
|
|
|
|
2013-04-03 18:38:35 +04:00
|
|
|
len = strlen(path);
|
|
|
|
|
2015-07-17 17:48:33 +03:00
|
|
|
if (type == CSYNC_FTW_TYPE_SKIP) {
|
|
|
|
excluded =CSYNC_FILE_EXCLUDE_STAT_FAILED;
|
|
|
|
} else {
|
|
|
|
/* Check if file is excluded */
|
2015-08-24 10:47:33 +03:00
|
|
|
excluded = csync_excluded_traversal(ctx->excludes, path, type);
|
2015-07-17 17:48:33 +03:00
|
|
|
}
|
2013-10-01 15:18:06 +04:00
|
|
|
|
2015-07-15 16:01:27 +03:00
|
|
|
if( excluded == CSYNC_NOT_EXCLUDED ) {
|
|
|
|
/* Even if it is not excluded by a pattern, maybe it is to be ignored
|
|
|
|
* because it's a hidden file that should not be synced.
|
|
|
|
* This code should probably be in csync_exclude, but it does not have the fs parameter.
|
|
|
|
* Keep it here for now */
|
|
|
|
if (ctx->ignore_hidden_files && (fs->flags & CSYNC_VIO_FILE_FLAGS_HIDDEN)) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "file excluded because it is a hidden file: %s", path);
|
|
|
|
excluded = CSYNC_FILE_EXCLUDE_HIDDEN;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* File is ignored because it's matched by a user- or system exclude pattern. */
|
2015-07-08 19:06:38 +03:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "%s excluded (%d)", path, excluded);
|
|
|
|
if (excluded == CSYNC_FILE_EXCLUDE_AND_REMOVE) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (excluded == CSYNC_FILE_SILENTLY_EXCLUDED) {
|
|
|
|
return 1;
|
|
|
|
}
|
2013-08-06 20:01:34 +04:00
|
|
|
}
|
|
|
|
|
2015-05-21 15:50:30 +03:00
|
|
|
if (ctx->current == REMOTE_REPLICA && ctx->callbacks.checkSelectiveSyncBlackListHook) {
|
|
|
|
if (ctx->callbacks.checkSelectiveSyncBlackListHook(ctx->callbacks.update_callback_userdata, path)) {
|
2014-09-26 14:35:12 +04:00
|
|
|
return 1;
|
2014-08-28 17:53:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-19 17:02:12 +04:00
|
|
|
h = _hash_of_file(ctx, file );
|
2013-03-21 00:56:09 +04:00
|
|
|
if( h == 0 ) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-03-23 00:48:01 +04:00
|
|
|
size = sizeof(csync_file_stat_t) + len + 1;
|
2008-04-28 18:50:25 +04:00
|
|
|
|
2008-06-27 20:01:19 +04:00
|
|
|
st = c_malloc(size);
|
2008-04-29 11:26:42 +04:00
|
|
|
|
2008-06-28 19:14:20 +04:00
|
|
|
/* Set instruction by default to none */
|
|
|
|
st->instruction = CSYNC_INSTRUCTION_NONE;
|
2013-11-13 17:29:31 +04:00
|
|
|
st->etag = NULL;
|
2013-03-11 22:55:07 +04:00
|
|
|
st->child_modified = 0;
|
2014-08-28 19:16:17 +04:00
|
|
|
st->has_ignored_files = 0;
|
2008-06-28 19:14:20 +04:00
|
|
|
|
2015-07-08 19:06:38 +03:00
|
|
|
/* FIXME: Under which conditions are the following two ifs true and the code
|
|
|
|
* is executed? */
|
2013-03-21 00:56:09 +04:00
|
|
|
if (type == CSYNC_FTW_TYPE_FILE ) {
|
|
|
|
if (fs->mtime == 0) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "file: %s - mtime is zero!", path);
|
2014-10-20 21:27:02 +04:00
|
|
|
|
|
|
|
tmp = csync_statedb_get_stat_by_hash(ctx, h);
|
|
|
|
if(_last_db_return_error(ctx)) {
|
|
|
|
SAFE_FREE(st);
|
2015-06-08 16:31:14 +03:00
|
|
|
SAFE_FREE(tmp);
|
2014-10-20 21:27:02 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_UNSUCCESSFUL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-03-21 00:56:09 +04:00
|
|
|
if (tmp == NULL) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "file: %s - not found in db, IGNORE!", path);
|
|
|
|
st->instruction = CSYNC_INSTRUCTION_IGNORE;
|
|
|
|
} else {
|
|
|
|
SAFE_FREE(st);
|
|
|
|
st = tmp;
|
|
|
|
st->instruction = CSYNC_INSTRUCTION_NONE;
|
2013-06-05 18:35:48 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "file: %s - tmp non zero, mtime %lu", path, st->modtime );
|
2013-03-21 00:56:09 +04:00
|
|
|
tmp = NULL;
|
|
|
|
}
|
2013-04-09 16:41:36 +04:00
|
|
|
goto fastout; /* Skip copying of the etag. That's an important difference to upstream
|
|
|
|
* without etags. */
|
2013-03-21 00:56:09 +04:00
|
|
|
}
|
2008-04-23 14:12:48 +04:00
|
|
|
}
|
|
|
|
|
2013-09-02 19:23:02 +04:00
|
|
|
if (excluded > CSYNC_NOT_EXCLUDED || type == CSYNC_FTW_TYPE_SLINK) {
|
2013-12-06 17:06:38 +04:00
|
|
|
st->instruction = CSYNC_INSTRUCTION_IGNORE;
|
2015-07-09 11:44:12 +03:00
|
|
|
if (ctx->current_fs) {
|
|
|
|
ctx->current_fs->has_ignored_files = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto out;
|
2013-08-06 20:01:34 +04:00
|
|
|
}
|
2013-03-19 14:48:46 +04:00
|
|
|
|
2012-08-09 17:59:08 +04:00
|
|
|
/* Update detection: Check if a database entry exists.
|
|
|
|
* If not, the file is either new or has been renamed. To see if it is
|
|
|
|
* renamed, the db gets queried by the inode of the file as that one
|
|
|
|
* does not change on rename.
|
|
|
|
*/
|
2008-07-09 12:10:00 +04:00
|
|
|
if (csync_get_statedb_exists(ctx)) {
|
2014-03-19 19:22:25 +04:00
|
|
|
tmp = csync_statedb_get_stat_by_hash(ctx, h);
|
2013-09-25 13:34:50 +04:00
|
|
|
|
2014-10-20 21:27:02 +04:00
|
|
|
if(_last_db_return_error(ctx)) {
|
|
|
|
SAFE_FREE(st);
|
|
|
|
ctx->status_code = CSYNC_STATUS_UNSUCCESSFUL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-09-24 16:56:42 +04:00
|
|
|
if(tmp && tmp->phash == h ) { /* there is an entry in the database */
|
|
|
|
/* we have an update! */
|
2014-10-22 19:25:10 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "Database entry found, compare: %" PRId64 " <-> %" PRId64
|
|
|
|
", etag: %s <-> %s, inode: %" PRId64 " <-> %" PRId64
|
2015-07-10 17:48:01 +03:00
|
|
|
", size: %" PRId64 " <-> %" PRId64 ", perms: %s <-> %s, ignore: %d",
|
2014-10-22 19:25:10 +04:00
|
|
|
((int64_t) fs->mtime), ((int64_t) tmp->modtime),
|
|
|
|
fs->etag, tmp->etag, (uint64_t) fs->inode, (uint64_t) tmp->inode,
|
2015-07-10 17:48:01 +03:00
|
|
|
(uint64_t) fs->size, (uint64_t) tmp->size, fs->remotePerm, tmp->remotePerm, tmp->has_ignored_files );
|
2015-11-23 13:53:06 +03:00
|
|
|
if (ctx->current == REMOTE_REPLICA && !c_streq(fs->etag, tmp->etag)) {
|
|
|
|
st->instruction = CSYNC_INSTRUCTION_EVAL;
|
2015-12-22 13:26:07 +03:00
|
|
|
|
|
|
|
// Preserve the EVAL flag later on if the type has changed.
|
2016-01-27 16:11:37 +03:00
|
|
|
if (_csync_filetype_different(tmp, fs)) {
|
2015-12-22 13:26:07 +03:00
|
|
|
st->child_modified = 1;
|
2016-01-27 16:11:37 +03:00
|
|
|
}
|
2015-12-22 13:26:07 +03:00
|
|
|
|
2015-11-23 13:53:06 +03:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (ctx->current == LOCAL_REPLICA &&
|
|
|
|
(!_csync_mtime_equal(fs->mtime, tmp->modtime)
|
|
|
|
// zero size in statedb can happen during migration
|
2015-11-23 14:01:42 +03:00
|
|
|
|| (tmp->size != 0 && fs->size != tmp->size))) {
|
2015-11-23 13:53:06 +03:00
|
|
|
|
|
|
|
if (fs->size == tmp->size && tmp->checksumTypeId) {
|
|
|
|
if (ctx->callbacks.checksum_hook) {
|
2015-11-23 15:44:49 +03:00
|
|
|
st->checksum = ctx->callbacks.checksum_hook(
|
|
|
|
file, tmp->checksumTypeId,
|
2015-11-23 13:53:06 +03:00
|
|
|
ctx->callbacks.checksum_userdata);
|
|
|
|
}
|
2015-11-23 15:44:49 +03:00
|
|
|
bool checksumIdentical = false;
|
|
|
|
if (st->checksum) {
|
|
|
|
st->checksumTypeId = tmp->checksumTypeId;
|
|
|
|
checksumIdentical = strncmp(st->checksum, tmp->checksum, 1000) == 0;
|
|
|
|
}
|
2015-11-23 13:53:06 +03:00
|
|
|
if (checksumIdentical) {
|
|
|
|
st->instruction = CSYNC_INSTRUCTION_NONE;
|
|
|
|
st->should_update_metadata = true;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2016-01-06 12:01:22 +03:00
|
|
|
|
|
|
|
// Preserve the EVAL flag later on if the type has changed.
|
2016-01-27 16:11:37 +03:00
|
|
|
if (_csync_filetype_different(tmp, fs)) {
|
2016-01-06 12:01:22 +03:00
|
|
|
st->child_modified = 1;
|
2016-01-27 16:11:37 +03:00
|
|
|
}
|
2016-01-06 12:01:22 +03:00
|
|
|
|
2012-09-24 16:56:42 +04:00
|
|
|
st->instruction = CSYNC_INSTRUCTION_EVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-06-27 17:26:12 +04:00
|
|
|
bool metadata_differ = (ctx->current == REMOTE_REPLICA && (!c_streq(fs->file_id, tmp->file_id)
|
|
|
|
|| !c_streq(fs->remotePerm, tmp->remotePerm)))
|
|
|
|
|| (ctx->current == LOCAL_REPLICA && fs->inode != tmp->inode);
|
2013-11-14 16:18:29 +04:00
|
|
|
if (type == CSYNC_FTW_TYPE_DIR && ctx->current == REMOTE_REPLICA
|
2015-04-08 16:30:07 +03:00
|
|
|
&& !metadata_differ && ctx->read_remote_from_db) {
|
2013-11-14 16:18:29 +04:00
|
|
|
/* If both etag and file id are equal for a directory, read all contents from
|
2014-04-18 20:27:27 +04:00
|
|
|
* the database.
|
2014-06-24 13:08:38 +04:00
|
|
|
* The metadata comparison ensure that we fetch all the file id or permission when
|
|
|
|
* upgrading owncloud
|
2014-04-18 20:27:27 +04:00
|
|
|
*/
|
2014-03-19 19:22:25 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "Reading from database: %s", path);
|
2013-11-14 16:18:29 +04:00
|
|
|
ctx->remote.read_from_db = true;
|
|
|
|
}
|
2014-06-24 13:08:38 +04:00
|
|
|
if (metadata_differ) {
|
|
|
|
/* file id or permissions has changed. Which means we need to update them in the DB. */
|
2014-08-07 12:14:14 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "Need to update metadata for: %s", path);
|
2015-07-09 16:05:37 +03:00
|
|
|
st->should_update_metadata = true;
|
2014-04-18 20:27:27 +04:00
|
|
|
}
|
2015-07-10 17:48:01 +03:00
|
|
|
/* If it was remembered in the db that the remote dir has ignored files, store
|
|
|
|
* that so that the reconciler can make advantage of.
|
|
|
|
*/
|
|
|
|
if( ctx->current == REMOTE_REPLICA ) {
|
|
|
|
st->has_ignored_files = tmp->has_ignored_files;
|
|
|
|
}
|
2012-09-24 16:56:42 +04:00
|
|
|
st->instruction = CSYNC_INSTRUCTION_NONE;
|
|
|
|
} else {
|
2013-11-27 12:31:13 +04:00
|
|
|
enum csync_vio_file_type_e tmp_vio_type = CSYNC_VIO_FILE_TYPE_UNKNOWN;
|
|
|
|
|
2014-06-20 12:41:01 +04:00
|
|
|
/* tmp might point to malloc mem, so free it here before reusing tmp */
|
|
|
|
SAFE_FREE(tmp);
|
|
|
|
|
2012-09-24 16:56:42 +04:00
|
|
|
/* check if it's a file and has been renamed */
|
2013-01-04 23:45:10 +04:00
|
|
|
if (ctx->current == LOCAL_REPLICA) {
|
2014-05-06 14:55:54 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "Checking for rename based on inode # %" PRId64 "", (uint64_t) fs->inode);
|
|
|
|
|
2014-03-19 19:22:25 +04:00
|
|
|
tmp = csync_statedb_get_stat_by_inode(ctx, fs->inode);
|
2013-11-27 12:31:13 +04:00
|
|
|
|
2014-10-20 21:27:02 +04:00
|
|
|
if(_last_db_return_error(ctx)) {
|
|
|
|
SAFE_FREE(st);
|
|
|
|
ctx->status_code = CSYNC_STATUS_UNSUCCESSFUL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-11-27 12:31:13 +04:00
|
|
|
/* translate the file type between the two stat types csync has. */
|
2016-01-27 16:10:52 +03:00
|
|
|
if( tmp && tmp->type == CSYNC_FTW_TYPE_FILE ) {
|
2013-11-27 12:31:13 +04:00
|
|
|
tmp_vio_type = CSYNC_VIO_FILE_TYPE_REGULAR;
|
2016-01-27 16:10:52 +03:00
|
|
|
} else if( tmp && tmp->type == CSYNC_FTW_TYPE_DIR) {
|
2013-11-27 12:31:13 +04:00
|
|
|
tmp_vio_type = CSYNC_VIO_FILE_TYPE_DIRECTORY;
|
2016-01-27 16:10:52 +03:00
|
|
|
} else if( tmp && tmp->type == CSYNC_FTW_TYPE_SLINK ) {
|
|
|
|
tmp_vio_type = CSYNC_VIO_FILE_TYPE_SYMBOLIC_LINK;
|
2013-11-27 12:31:13 +04:00
|
|
|
} else {
|
|
|
|
tmp_vio_type = CSYNC_VIO_FILE_TYPE_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmp && tmp->inode == fs->inode && tmp_vio_type == fs->type
|
2014-04-24 13:51:48 +04:00
|
|
|
&& (tmp->modtime == fs->mtime || fs->type == CSYNC_VIO_FILE_TYPE_DIRECTORY)
|
|
|
|
#ifdef NO_RENAME_EXTENSION
|
|
|
|
&& _csync_sameextension(tmp->path, path)
|
|
|
|
#endif
|
|
|
|
) {
|
2014-04-22 11:32:58 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "pot rename detected based on inode # %" PRId64 "", (uint64_t) fs->inode);
|
2012-09-24 16:56:42 +04:00
|
|
|
/* inode found so the file has been renamed */
|
2013-11-14 20:05:58 +04:00
|
|
|
st->instruction = CSYNC_INSTRUCTION_EVAL_RENAME;
|
2013-05-22 18:13:27 +04:00
|
|
|
if (fs->type == CSYNC_VIO_FILE_TYPE_DIRECTORY) {
|
2013-01-04 23:45:10 +04:00
|
|
|
csync_rename_record(ctx, tmp->path, path);
|
2013-01-05 14:32:24 +04:00
|
|
|
}
|
2012-08-20 20:11:18 +04:00
|
|
|
goto out;
|
2012-09-24 16:56:42 +04:00
|
|
|
} else {
|
|
|
|
/* file not found in statedb */
|
|
|
|
st->instruction = CSYNC_INSTRUCTION_NEW;
|
2012-08-20 20:11:18 +04:00
|
|
|
goto out;
|
|
|
|
}
|
2013-10-25 15:25:07 +04:00
|
|
|
} else {
|
|
|
|
/* Remote Replica Rename check */
|
2014-03-19 19:22:25 +04:00
|
|
|
tmp = csync_statedb_get_stat_by_file_id(ctx, fs->file_id);
|
2014-10-20 21:27:02 +04:00
|
|
|
|
|
|
|
if(_last_db_return_error(ctx)) {
|
|
|
|
SAFE_FREE(st);
|
|
|
|
ctx->status_code = CSYNC_STATUS_UNSUCCESSFUL;
|
|
|
|
return -1;
|
|
|
|
}
|
2013-10-25 15:25:07 +04:00
|
|
|
if(tmp ) { /* tmp existing at all */
|
2016-01-27 16:11:37 +03:00
|
|
|
if ( _csync_filetype_different(tmp, fs)) {
|
2013-10-30 20:32:12 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "WARN: file types different is not!");
|
2013-10-25 15:25:07 +04:00
|
|
|
st->instruction = CSYNC_INSTRUCTION_NEW;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-12-02 14:25:51 +03:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "remote rename detected based on fileid %s %s", tmp->path, file);
|
2013-11-14 20:05:58 +04:00
|
|
|
st->instruction = CSYNC_INSTRUCTION_EVAL_RENAME;
|
2013-11-08 14:17:43 +04:00
|
|
|
if (fs->type == CSYNC_VIO_FILE_TYPE_DIRECTORY) {
|
|
|
|
csync_rename_record(ctx, tmp->path, path);
|
2013-10-30 20:32:12 +04:00
|
|
|
} else {
|
2013-11-13 17:29:31 +04:00
|
|
|
if( !c_streq(tmp->etag, fs->etag) ) {
|
2013-11-13 17:04:51 +04:00
|
|
|
/* CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "ETags are different!"); */
|
|
|
|
/* File with different etag, don't do a rename, but download the file again */
|
|
|
|
st->instruction = CSYNC_INSTRUCTION_NEW;
|
|
|
|
}
|
2013-10-30 20:32:12 +04:00
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* file not found in statedb */
|
|
|
|
st->instruction = CSYNC_INSTRUCTION_NEW;
|
2015-05-21 15:50:30 +03:00
|
|
|
|
2015-07-27 10:54:20 +03:00
|
|
|
if (fs->type == CSYNC_VIO_FILE_TYPE_DIRECTORY && ctx->current == REMOTE_REPLICA && ctx->callbacks.checkSelectiveSyncNewFolderHook) {
|
|
|
|
if (ctx->callbacks.checkSelectiveSyncNewFolderHook(ctx->callbacks.update_callback_userdata, path)) {
|
|
|
|
SAFE_FREE(st);
|
|
|
|
return 1;
|
2015-05-21 15:50:30 +03:00
|
|
|
}
|
|
|
|
}
|
2013-10-30 20:32:12 +04:00
|
|
|
goto out;
|
2013-10-25 15:25:07 +04:00
|
|
|
}
|
2008-04-30 18:24:50 +04:00
|
|
|
}
|
|
|
|
}
|
2008-04-29 11:27:49 +04:00
|
|
|
} else {
|
2014-10-21 20:09:23 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Unable to open statedb" );
|
2015-06-08 16:31:14 +03:00
|
|
|
SAFE_FREE(st);
|
2014-10-21 20:09:23 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_UNSUCCESSFUL;
|
|
|
|
return -1;
|
2008-04-29 11:27:49 +04:00
|
|
|
}
|
2008-04-28 18:50:25 +04:00
|
|
|
|
|
|
|
out:
|
2013-03-11 22:55:07 +04:00
|
|
|
|
2013-09-02 19:23:02 +04:00
|
|
|
/* Set the ignored error string. */
|
|
|
|
if (st->instruction == CSYNC_INSTRUCTION_IGNORE) {
|
2015-07-09 11:44:12 +03:00
|
|
|
if( type == CSYNC_FTW_TYPE_SLINK ) {
|
|
|
|
st->error_status = CSYNC_STATUS_INDIVIDUAL_IS_SYMLINK; /* Symbolic links are ignored. */
|
|
|
|
} else {
|
|
|
|
if (excluded == CSYNC_FILE_EXCLUDE_LIST) {
|
|
|
|
st->error_status = CSYNC_STATUS_INDIVIDUAL_IGNORE_LIST; /* File listed on ignore list. */
|
|
|
|
} else if (excluded == CSYNC_FILE_EXCLUDE_INVALID_CHAR) {
|
|
|
|
st->error_status = CSYNC_STATUS_INDIVIDUAL_IS_INVALID_CHARS; /* File contains invalid characters. */
|
|
|
|
} else if (excluded == CSYNC_FILE_EXCLUDE_LONG_FILENAME) {
|
|
|
|
st->error_status = CSYNC_STATUS_INDIVIDUAL_EXCLUDE_LONG_FILENAME; /* File name is too long. */
|
|
|
|
} else if (excluded == CSYNC_FILE_EXCLUDE_HIDDEN ) {
|
|
|
|
st->error_status = CSYNC_STATUS_INDIVIDUAL_EXCLUDE_HIDDEN;
|
2015-07-17 17:48:33 +03:00
|
|
|
} else if (excluded == CSYNC_FILE_EXCLUDE_STAT_FAILED) {
|
|
|
|
st->error_status = CSYNC_STATUS_INDIVIDUAL_STAT_FAILED;
|
2015-07-09 11:44:12 +03:00
|
|
|
}
|
|
|
|
}
|
2013-09-02 19:23:02 +04:00
|
|
|
}
|
2013-03-11 22:55:07 +04:00
|
|
|
if (st->instruction != CSYNC_INSTRUCTION_NONE && st->instruction != CSYNC_INSTRUCTION_IGNORE
|
|
|
|
&& type != CSYNC_FTW_TYPE_DIR) {
|
|
|
|
st->child_modified = 1;
|
|
|
|
}
|
|
|
|
ctx->current_fs = st;
|
|
|
|
|
2013-07-02 20:25:17 +04:00
|
|
|
csync_file_stat_free(tmp);
|
2008-04-28 18:50:25 +04:00
|
|
|
st->inode = fs->inode;
|
2013-03-21 00:56:09 +04:00
|
|
|
st->mode = fs->mode;
|
|
|
|
st->size = fs->size;
|
2008-06-02 20:03:24 +04:00
|
|
|
st->modtime = fs->mtime;
|
2013-03-21 00:56:09 +04:00
|
|
|
st->type = type;
|
2013-11-13 17:29:31 +04:00
|
|
|
st->etag = NULL;
|
|
|
|
if( fs->etag ) {
|
2014-01-31 16:32:25 +04:00
|
|
|
SAFE_FREE(st->etag);
|
2013-11-13 17:29:31 +04:00
|
|
|
st->etag = c_strdup(fs->etag);
|
2012-08-30 14:04:14 +04:00
|
|
|
}
|
2013-10-25 15:15:25 +04:00
|
|
|
csync_vio_set_file_id(st->file_id, fs->file_id);
|
2014-06-03 13:50:13 +04:00
|
|
|
if (fs->fields & CSYNC_VIO_FILE_STAT_FIELDS_DIRECTDOWNLOADURL) {
|
|
|
|
SAFE_FREE(st->directDownloadUrl);
|
|
|
|
st->directDownloadUrl = c_strdup(fs->directDownloadUrl);
|
|
|
|
}
|
|
|
|
if (fs->fields & CSYNC_VIO_FILE_STAT_FIELDS_DIRECTDOWNLOADCOOKIES) {
|
|
|
|
SAFE_FREE(st->directDownloadCookies);
|
|
|
|
st->directDownloadCookies = c_strdup(fs->directDownloadCookies);
|
|
|
|
}
|
2014-06-06 17:24:17 +04:00
|
|
|
if (fs->fields & CSYNC_VIO_FILE_STAT_FIELDS_PERM) {
|
2014-06-18 17:56:13 +04:00
|
|
|
strncpy(st->remotePerm, fs->remotePerm, REMOTE_PERM_BUF_SIZE);
|
2014-06-06 17:24:17 +04:00
|
|
|
}
|
2013-04-08 16:20:38 +04:00
|
|
|
|
|
|
|
fastout: /* target if the file information is read from database into st */
|
2008-04-28 18:50:25 +04:00
|
|
|
st->phash = h;
|
|
|
|
st->pathlen = len;
|
|
|
|
memcpy(st->path, (len ? path : ""), len + 1);
|
|
|
|
|
|
|
|
switch (ctx->current) {
|
|
|
|
case LOCAL_REPLICA:
|
|
|
|
if (c_rbtree_insert(ctx->local.tree, (void *) st) < 0) {
|
|
|
|
SAFE_FREE(st);
|
2013-03-13 14:57:06 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_TREE_ERROR;
|
2008-04-28 18:50:25 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
2012-10-19 16:23:20 +04:00
|
|
|
case REMOTE_REPLICA:
|
2008-04-28 18:50:25 +04:00
|
|
|
if (c_rbtree_insert(ctx->remote.tree, (void *) st) < 0) {
|
|
|
|
SAFE_FREE(st);
|
2013-03-13 14:57:06 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_TREE_ERROR;
|
2008-04-28 18:50:25 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-03-19 19:22:25 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "file: %s, instruction: %s <<=", st->path,
|
|
|
|
csync_instruction_str(st->instruction));
|
|
|
|
|
2008-04-28 18:50:25 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-25 13:33:18 +04:00
|
|
|
int csync_walker(CSYNC *ctx, const char *file, const csync_vio_file_stat_t *fs,
|
|
|
|
enum csync_ftw_flags_e flag) {
|
2013-03-19 14:48:46 +04:00
|
|
|
int rc = -1;
|
2013-03-19 17:02:12 +04:00
|
|
|
int type = CSYNC_FTW_TYPE_SKIP;
|
|
|
|
csync_file_stat_t *st = NULL;
|
|
|
|
uint64_t h;
|
2013-03-19 14:48:46 +04:00
|
|
|
|
2013-05-08 17:28:26 +04:00
|
|
|
if (ctx->abort) {
|
2014-03-19 19:22:25 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "Aborted!");
|
2013-08-18 19:26:45 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_ABORTED;
|
2013-05-08 17:28:26 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-04-23 14:12:48 +04:00
|
|
|
switch (flag) {
|
|
|
|
case CSYNC_FTW_FLAG_FILE:
|
2014-09-05 17:23:33 +04:00
|
|
|
if (ctx->current == REMOTE_REPLICA) {
|
2014-09-10 13:08:00 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "file: %s [file_id=%s size=%" PRIu64 "]", file, fs->file_id, fs->size);
|
2014-09-05 17:23:33 +04:00
|
|
|
} else {
|
2014-09-10 13:08:00 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "file: %s [inode=%" PRIu64 " size=%" PRIu64 "]", file, fs->inode, fs->size);
|
2014-09-05 17:23:33 +04:00
|
|
|
}
|
2013-03-19 14:48:46 +04:00
|
|
|
type = CSYNC_FTW_TYPE_FILE;
|
2008-05-26 19:09:42 +04:00
|
|
|
break;
|
2013-03-19 14:48:46 +04:00
|
|
|
case CSYNC_FTW_FLAG_DIR: /* enter directory */
|
2014-09-05 17:23:33 +04:00
|
|
|
if (ctx->current == REMOTE_REPLICA) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "directory: %s [file_id=%s]", file, fs->file_id);
|
|
|
|
} else {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "directory: %s [inode=%" PRIu64 "]", file, fs->inode);
|
|
|
|
}
|
2013-03-19 14:48:46 +04:00
|
|
|
type = CSYNC_FTW_TYPE_DIR;
|
2008-05-13 18:06:11 +04:00
|
|
|
break;
|
2013-03-19 17:02:12 +04:00
|
|
|
case CSYNC_FTW_FLAG_NSTAT: /* not statable file */
|
|
|
|
/* if file was here before and now is not longer stat-able, still
|
|
|
|
* add it to the db, otherwise not. */
|
|
|
|
h = _hash_of_file( ctx, file );
|
|
|
|
if( h == 0 ) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-03-19 19:22:25 +04:00
|
|
|
st = csync_statedb_get_stat_by_hash(ctx, h);
|
2013-03-19 17:02:12 +04:00
|
|
|
if( !st ) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-07-02 20:25:17 +04:00
|
|
|
csync_file_stat_free(st);
|
|
|
|
st = NULL;
|
2013-03-19 17:02:12 +04:00
|
|
|
|
|
|
|
type = CSYNC_FTW_TYPE_SKIP;
|
|
|
|
break;
|
2013-03-19 14:48:46 +04:00
|
|
|
case CSYNC_FTW_FLAG_SLINK:
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "symlink: %s - not supported", file);
|
2013-08-07 14:14:29 +04:00
|
|
|
type = CSYNC_FTW_TYPE_SLINK;
|
|
|
|
break;
|
2013-03-19 14:48:46 +04:00
|
|
|
case CSYNC_FTW_FLAG_DNR:
|
|
|
|
case CSYNC_FTW_FLAG_DP:
|
|
|
|
case CSYNC_FTW_FLAG_SLN:
|
|
|
|
default:
|
2013-03-19 17:02:12 +04:00
|
|
|
return 0;
|
|
|
|
break;
|
2008-04-23 14:12:48 +04:00
|
|
|
}
|
|
|
|
|
2013-03-19 14:48:46 +04:00
|
|
|
rc = _csync_detect_update(ctx, file, fs, type );
|
|
|
|
|
|
|
|
return rc;
|
2008-04-23 14:12:48 +04:00
|
|
|
}
|
2008-04-26 12:42:20 +04:00
|
|
|
|
2014-03-19 19:22:25 +04:00
|
|
|
static bool fill_tree_from_db(CSYNC *ctx, const char *uri)
|
2014-02-07 14:52:37 +04:00
|
|
|
{
|
|
|
|
const char *path = NULL;
|
|
|
|
|
|
|
|
if( strlen(uri) < strlen(ctx->remote.uri)+1) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "name does not contain remote uri!");
|
2014-03-19 19:22:25 +04:00
|
|
|
return false;
|
2014-02-07 14:52:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
path = uri + strlen(ctx->remote.uri)+1;
|
|
|
|
|
|
|
|
if( csync_statedb_get_below_path(ctx, path) < 0 ) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "StateDB could not be read!");
|
2014-03-19 19:22:25 +04:00
|
|
|
return false;
|
2014-02-07 14:52:37 +04:00
|
|
|
}
|
2014-03-19 19:22:25 +04:00
|
|
|
|
|
|
|
return true;
|
2014-02-07 14:52:37 +04:00
|
|
|
}
|
|
|
|
|
2015-11-18 17:28:03 +03:00
|
|
|
/* set the current item to an ignored state.
|
|
|
|
* If the item is set to ignored, the update phase continues, ie. its not a hard error */
|
|
|
|
static bool mark_current_item_ignored( CSYNC *ctx, csync_file_stat_t *previous_fs, CSYNC_STATUS status )
|
|
|
|
{
|
|
|
|
if(!ctx) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->current_fs) {
|
|
|
|
ctx->current_fs->instruction = CSYNC_INSTRUCTION_IGNORE;
|
|
|
|
ctx->current_fs->error_status = status;
|
|
|
|
/* If a directory has ignored files, put the flag on the parent directory as well */
|
|
|
|
if( previous_fs ) {
|
|
|
|
previous_fs->has_ignored_files = true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-04-23 14:12:48 +04:00
|
|
|
/* File tree walker */
|
2009-05-25 13:33:18 +04:00
|
|
|
int csync_ftw(CSYNC *ctx, const char *uri, csync_walker_fn fn,
|
|
|
|
unsigned int depth) {
|
2008-04-23 14:12:48 +04:00
|
|
|
char *filename = NULL;
|
|
|
|
char *d_name = NULL;
|
|
|
|
csync_vio_handle_t *dh = NULL;
|
2008-04-26 12:42:20 +04:00
|
|
|
csync_vio_file_stat_t *dirent = NULL;
|
2013-03-11 22:55:07 +04:00
|
|
|
csync_file_stat_t *previous_fs = NULL;
|
2012-08-23 18:55:39 +04:00
|
|
|
int read_from_db = 0;
|
2008-04-23 14:12:48 +04:00
|
|
|
int rc = 0;
|
2012-08-23 18:55:39 +04:00
|
|
|
int res = 0;
|
|
|
|
|
2012-10-27 21:27:14 +04:00
|
|
|
bool do_read_from_db = (ctx->current == REMOTE_REPLICA && ctx->remote.read_from_db);
|
2008-04-23 14:12:48 +04:00
|
|
|
|
|
|
|
if (uri[0] == '\0') {
|
|
|
|
errno = ENOENT;
|
2013-03-13 14:57:06 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_PARAM_ERROR;
|
2008-04-23 14:12:48 +04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2012-08-23 18:55:39 +04:00
|
|
|
read_from_db = ctx->remote.read_from_db;
|
|
|
|
|
2014-02-07 14:52:37 +04:00
|
|
|
// if the etag of this dir is still the same, its content is restored from the
|
|
|
|
// database.
|
|
|
|
if( do_read_from_db ) {
|
2014-03-19 19:22:25 +04:00
|
|
|
if( ! fill_tree_from_db(ctx, uri) ) {
|
|
|
|
errno = ENOENT;
|
|
|
|
ctx->status_code = CSYNC_STATUS_OPENDIR_ERROR;
|
|
|
|
goto error;
|
|
|
|
}
|
2014-02-07 14:52:37 +04:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2014-12-02 14:25:51 +03:00
|
|
|
const char *uri_for_vio = uri;
|
|
|
|
if (ctx->current == REMOTE_REPLICA) {
|
|
|
|
uri_for_vio += strlen(ctx->remote.uri);
|
|
|
|
if (strlen(uri_for_vio) > 0 && uri_for_vio[0] == '/') {
|
|
|
|
uri_for_vio++; // cut leading slash
|
|
|
|
}
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "URI without fuzz for %s is \"%s\"", uri, uri_for_vio);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((dh = csync_vio_opendir(ctx, uri_for_vio)) == NULL) {
|
|
|
|
if (ctx->abort) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "Aborted!");
|
|
|
|
ctx->status_code = CSYNC_STATUS_ABORTED;
|
|
|
|
goto error;
|
|
|
|
}
|
2014-02-07 14:52:37 +04:00
|
|
|
int asp = 0;
|
|
|
|
/* permission denied */
|
|
|
|
ctx->status_code = csync_errno_to_status(errno, CSYNC_STATUS_OPENDIR_ERROR);
|
|
|
|
if (errno == EACCES) {
|
2015-11-18 17:28:03 +03:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN, "Permission denied.");
|
|
|
|
if (mark_current_item_ignored(ctx, previous_fs, CSYNC_STATUS_PERMISSION_DENIED)) {
|
2015-09-11 19:12:24 +03:00
|
|
|
goto done;
|
2015-09-09 15:12:13 +03:00
|
|
|
}
|
2014-02-07 14:52:37 +04:00
|
|
|
} else if(errno == ENOENT) {
|
|
|
|
asp = asprintf( &ctx->error_string, "%s", uri);
|
|
|
|
if (asp < 0) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "asprintf failed!");
|
|
|
|
}
|
2015-06-04 10:23:58 +03:00
|
|
|
}
|
2015-10-12 18:10:01 +03:00
|
|
|
// 403 Forbidden can be sent by the server if the file firewall is active.
|
|
|
|
// A file or directory should be ignored and sync must continue. See #3490
|
2015-10-12 18:06:49 +03:00
|
|
|
else if(errno == ERRNO_FORBIDDEN) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN, "Directory access Forbidden (File Firewall?)");
|
2015-11-18 17:28:03 +03:00
|
|
|
if( mark_current_item_ignored(ctx, previous_fs, CSYNC_STATUS_FORBIDDEN) ) {
|
2015-10-12 18:06:49 +03:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
/* if current_fs is not defined here, better throw an error */
|
|
|
|
}
|
2015-06-04 10:23:58 +03:00
|
|
|
// The server usually replies with the custom "503 Storage not available"
|
|
|
|
// if some path is temporarily unavailable. But in some cases a standard 503
|
|
|
|
// is returned too. Thus we can't distinguish the two and will treat any
|
|
|
|
// 503 as request to ignore the folder. See #3113 #2884.
|
|
|
|
else if(errno == ERRNO_STORAGE_UNAVAILABLE || errno == ERRNO_SERVICE_UNAVAILABLE) {
|
2015-02-25 10:57:39 +03:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN, "Storage was not available!");
|
2015-11-18 17:28:03 +03:00
|
|
|
if( mark_current_item_ignored(ctx, previous_fs, CSYNC_STATUS_STORAGE_UNAVAILABLE ) ) {
|
2014-10-27 21:21:12 +03:00
|
|
|
goto done;
|
|
|
|
}
|
2015-09-11 19:12:24 +03:00
|
|
|
/* if current_fs is not defined here, better throw an error */
|
2014-02-07 14:52:37 +04:00
|
|
|
} else {
|
2014-05-13 12:39:06 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "opendir failed for %s - errno %d", uri, errno);
|
2013-12-16 12:49:02 +04:00
|
|
|
}
|
2014-02-07 14:52:37 +04:00
|
|
|
goto error;
|
2008-04-23 14:12:48 +04:00
|
|
|
}
|
|
|
|
|
2008-04-26 12:42:20 +04:00
|
|
|
while ((dirent = csync_vio_readdir(ctx, dh))) {
|
2008-06-28 22:44:54 +04:00
|
|
|
const char *path = NULL;
|
2013-07-25 10:36:23 +04:00
|
|
|
size_t ulen = 0;
|
2013-07-24 17:59:18 +04:00
|
|
|
int flen;
|
2008-04-23 14:12:48 +04:00
|
|
|
int flag;
|
|
|
|
|
2015-08-04 23:13:38 +03:00
|
|
|
/* Conversion error */
|
|
|
|
if (dirent->name == NULL && dirent->original_name) {
|
|
|
|
ctx->status_code = CSYNC_STATUS_INVALID_CHARACTERS;
|
|
|
|
ctx->error_string = dirent->original_name; // take ownership
|
|
|
|
dirent->original_name = NULL;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2008-04-26 12:42:20 +04:00
|
|
|
d_name = dirent->name;
|
2008-04-23 14:12:48 +04:00
|
|
|
if (d_name == NULL) {
|
2013-03-13 14:57:06 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_READDIR_ERROR;
|
2008-04-23 14:12:48 +04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* skip "." and ".." */
|
2015-10-26 16:24:05 +03:00
|
|
|
if ( (d_name[0] == '.' && d_name[1] == '\0')
|
|
|
|
|| (d_name[0] == '.' && d_name[1] == '.' && d_name[2] == '\0')) {
|
2008-04-26 12:42:20 +04:00
|
|
|
csync_vio_file_stat_destroy(dirent);
|
2008-06-28 21:49:37 +04:00
|
|
|
dirent = NULL;
|
2008-04-23 14:12:48 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-07-24 17:59:18 +04:00
|
|
|
flen = asprintf(&filename, "%s/%s", uri, d_name);
|
|
|
|
if (flen < 0) {
|
2008-04-26 12:42:20 +04:00
|
|
|
csync_vio_file_stat_destroy(dirent);
|
2008-06-28 21:49:37 +04:00
|
|
|
dirent = NULL;
|
2013-03-13 14:57:06 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_MEMORY_ERROR;
|
2008-04-23 14:12:48 +04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2013-08-06 20:01:34 +04:00
|
|
|
/* Create relative path */
|
2008-06-28 22:44:54 +04:00
|
|
|
switch (ctx->current) {
|
|
|
|
case LOCAL_REPLICA:
|
2013-07-24 17:59:18 +04:00
|
|
|
ulen = strlen(ctx->local.uri) + 1;
|
2008-06-28 22:44:54 +04:00
|
|
|
break;
|
2012-10-19 16:23:20 +04:00
|
|
|
case REMOTE_REPLICA:
|
2013-07-24 17:59:18 +04:00
|
|
|
ulen = strlen(ctx->remote.uri) + 1;
|
2008-06-28 22:44:54 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-07-24 17:59:18 +04:00
|
|
|
if (((size_t)flen) < ulen) {
|
|
|
|
csync_vio_file_stat_destroy(dirent);
|
|
|
|
dirent = NULL;
|
|
|
|
ctx->status_code = CSYNC_STATUS_UNSUCCESSFUL;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
path = filename + ulen;
|
2013-10-01 15:18:36 +04:00
|
|
|
|
2013-08-06 20:01:34 +04:00
|
|
|
/* skip ".csync_journal.db" and ".csync_journal.db.ctmp" */
|
2014-10-21 00:38:10 +04:00
|
|
|
/* Isn't this done via csync_exclude already? */
|
2013-10-01 15:18:36 +04:00
|
|
|
if (c_streq(path, ".csync_journal.db")
|
|
|
|
|| c_streq(path, ".csync_journal.db.ctmp")
|
|
|
|
|| c_streq(path, ".csync_journal.db.ctmp-journal")
|
2014-10-21 00:38:10 +04:00
|
|
|
|| c_streq(path, ".csync-progressdatabase")
|
|
|
|
|| c_streq(path, ".csync_journal.db-shm")
|
2015-01-29 12:10:09 +03:00
|
|
|
|| c_streq(path, ".csync_journal.db-wal")
|
|
|
|
|| c_streq(path, ".csync_journal.db-journal")) {
|
2013-10-01 15:18:36 +04:00
|
|
|
csync_vio_file_stat_destroy(dirent);
|
|
|
|
dirent = NULL;
|
|
|
|
SAFE_FREE(filename);
|
|
|
|
continue;
|
2008-06-28 22:44:54 +04:00
|
|
|
}
|
|
|
|
|
2014-05-28 14:07:27 +04:00
|
|
|
/* Only for the local replica we have to stat(), for the remote one we have all data already */
|
|
|
|
if (ctx->replica == LOCAL_REPLICA) {
|
2015-10-22 03:29:27 +03:00
|
|
|
res = csync_vio_stat(ctx, filename, dirent);
|
2014-05-28 14:07:27 +04:00
|
|
|
} else {
|
|
|
|
res = 0;
|
|
|
|
}
|
2012-08-23 18:55:39 +04:00
|
|
|
|
2015-10-29 13:59:11 +03:00
|
|
|
/* if the filename starts with a . we consider it a hidden file
|
|
|
|
* For windows, the hidden state is also discovered within the vio
|
|
|
|
* local stat function.
|
2015-10-26 16:24:05 +03:00
|
|
|
*/
|
|
|
|
if( d_name[0] == '.' ) {
|
2015-10-28 11:53:06 +03:00
|
|
|
dirent->flags |= CSYNC_VIO_FILE_FLAGS_HIDDEN;
|
2015-10-26 16:24:05 +03:00
|
|
|
}
|
|
|
|
|
2012-08-23 18:55:39 +04:00
|
|
|
if( res == 0) {
|
2015-10-22 03:29:27 +03:00
|
|
|
switch (dirent->type) {
|
2008-06-16 19:48:48 +04:00
|
|
|
case CSYNC_VIO_FILE_TYPE_SYMBOLIC_LINK:
|
|
|
|
flag = CSYNC_FTW_FLAG_SLINK;
|
|
|
|
break;
|
|
|
|
case CSYNC_VIO_FILE_TYPE_DIRECTORY:
|
|
|
|
flag = CSYNC_FTW_FLAG_DIR;
|
|
|
|
break;
|
2008-06-18 13:52:57 +04:00
|
|
|
case CSYNC_VIO_FILE_TYPE_BLOCK_DEVICE:
|
|
|
|
case CSYNC_VIO_FILE_TYPE_CHARACTER_DEVICE:
|
|
|
|
case CSYNC_VIO_FILE_TYPE_SOCKET:
|
|
|
|
flag = CSYNC_FTW_FLAG_SPEC;
|
|
|
|
break;
|
|
|
|
case CSYNC_VIO_FILE_TYPE_FIFO:
|
2008-06-16 19:48:48 +04:00
|
|
|
flag = CSYNC_FTW_FLAG_SPEC;
|
|
|
|
break;
|
2008-06-18 13:52:57 +04:00
|
|
|
default:
|
|
|
|
flag = CSYNC_FTW_FLAG_FILE;
|
|
|
|
break;
|
2008-06-16 19:48:48 +04:00
|
|
|
};
|
2008-04-23 14:12:48 +04:00
|
|
|
} else {
|
|
|
|
flag = CSYNC_FTW_FLAG_NSTAT;
|
|
|
|
}
|
|
|
|
|
2013-03-11 22:55:07 +04:00
|
|
|
previous_fs = ctx->current_fs;
|
|
|
|
|
2008-04-23 14:12:48 +04:00
|
|
|
/* Call walker function for each file */
|
2015-10-22 03:29:27 +03:00
|
|
|
rc = fn(ctx, filename, dirent, flag);
|
2013-11-14 16:18:29 +04:00
|
|
|
/* this function may update ctx->current and ctx->read_from_db */
|
2012-08-23 18:55:39 +04:00
|
|
|
|
2008-04-23 14:12:48 +04:00
|
|
|
if (rc < 0) {
|
2013-11-25 01:18:08 +04:00
|
|
|
if (CSYNC_STATUS_IS_OK(ctx->status_code)) {
|
2013-04-06 20:47:08 +04:00
|
|
|
ctx->status_code = CSYNC_STATUS_UPDATE_ERROR;
|
|
|
|
}
|
2013-03-13 14:57:06 +04:00
|
|
|
|
2013-03-11 22:55:07 +04:00
|
|
|
ctx->current_fs = previous_fs;
|
2014-02-21 22:09:38 +04:00
|
|
|
goto error;
|
2008-04-23 14:12:48 +04:00
|
|
|
}
|
|
|
|
|
2014-09-26 14:35:12 +04:00
|
|
|
if (flag == CSYNC_FTW_FLAG_DIR && depth && rc == 0
|
2013-12-09 13:33:50 +04:00
|
|
|
&& (!ctx->current_fs || ctx->current_fs->instruction != CSYNC_INSTRUCTION_IGNORE)) {
|
2008-04-23 14:12:48 +04:00
|
|
|
rc = csync_ftw(ctx, filename, fn, depth - 1);
|
|
|
|
if (rc < 0) {
|
2013-03-11 22:55:07 +04:00
|
|
|
ctx->current_fs = previous_fs;
|
2014-02-21 22:09:38 +04:00
|
|
|
goto error;
|
2008-04-23 14:12:48 +04:00
|
|
|
}
|
2013-03-11 22:55:07 +04:00
|
|
|
|
|
|
|
if (ctx->current_fs && !ctx->current_fs->child_modified
|
2013-04-17 18:25:52 +04:00
|
|
|
&& ctx->current_fs->instruction == CSYNC_INSTRUCTION_EVAL) {
|
2013-03-11 22:55:07 +04:00
|
|
|
ctx->current_fs->instruction = CSYNC_INSTRUCTION_NONE;
|
2014-12-16 15:02:32 +03:00
|
|
|
if (ctx->current == REMOTE_REPLICA) {
|
2015-07-09 16:05:37 +03:00
|
|
|
ctx->current_fs->should_update_metadata = true;
|
2014-12-16 15:02:32 +03:00
|
|
|
}
|
2013-04-17 18:25:52 +04:00
|
|
|
}
|
2014-08-28 21:33:52 +04:00
|
|
|
|
|
|
|
if (ctx->current_fs && previous_fs && ctx->current_fs->has_ignored_files) {
|
|
|
|
/* If a directory has ignored files, put the flag on the parent directory as well */
|
|
|
|
previous_fs->has_ignored_files = ctx->current_fs->has_ignored_files;
|
|
|
|
}
|
2008-04-23 14:12:48 +04:00
|
|
|
}
|
2013-11-15 16:52:41 +04:00
|
|
|
|
2014-10-21 12:38:21 +04:00
|
|
|
if (ctx->current_fs && previous_fs && ctx->current_fs->child_modified) {
|
|
|
|
/* If a directory has modified files, put the flag on the parent directory as well */
|
|
|
|
previous_fs->child_modified = ctx->current_fs->child_modified;
|
|
|
|
}
|
|
|
|
|
2014-05-26 18:09:39 +04:00
|
|
|
if (flag == CSYNC_FTW_FLAG_DIR && ctx->current_fs
|
|
|
|
&& (ctx->current_fs->instruction == CSYNC_INSTRUCTION_EVAL ||
|
2014-06-27 17:26:12 +04:00
|
|
|
ctx->current_fs->instruction == CSYNC_INSTRUCTION_NEW)) {
|
2015-07-09 16:05:37 +03:00
|
|
|
ctx->current_fs->should_update_metadata = true;
|
2013-11-15 16:52:41 +04:00
|
|
|
}
|
|
|
|
|
2013-03-11 22:55:07 +04:00
|
|
|
ctx->current_fs = previous_fs;
|
2013-11-14 16:18:29 +04:00
|
|
|
ctx->remote.read_from_db = read_from_db;
|
2008-04-23 14:12:48 +04:00
|
|
|
SAFE_FREE(filename);
|
2008-04-26 12:42:20 +04:00
|
|
|
csync_vio_file_stat_destroy(dirent);
|
2008-06-28 21:49:37 +04:00
|
|
|
dirent = NULL;
|
2008-04-23 14:12:48 +04:00
|
|
|
}
|
2012-10-16 13:52:11 +04:00
|
|
|
|
2008-04-23 14:12:48 +04:00
|
|
|
csync_vio_closedir(ctx, dh);
|
2013-06-05 18:35:48 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, " <= Closing walk for %s with read_from_db %d", uri, read_from_db);
|
2012-08-23 18:55:39 +04:00
|
|
|
|
2008-04-23 14:12:48 +04:00
|
|
|
done:
|
2008-04-26 12:42:20 +04:00
|
|
|
csync_vio_file_stat_destroy(dirent);
|
2008-04-23 14:12:48 +04:00
|
|
|
SAFE_FREE(filename);
|
|
|
|
return rc;
|
|
|
|
error:
|
2014-02-21 22:09:38 +04:00
|
|
|
ctx->remote.read_from_db = read_from_db;
|
2012-10-19 22:17:45 +04:00
|
|
|
if (dh != NULL) {
|
|
|
|
csync_vio_closedir(ctx, dh);
|
|
|
|
}
|
2008-04-23 14:12:48 +04:00
|
|
|
SAFE_FREE(filename);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-05-13 12:12:07 +04:00
|
|
|
/* vim: set ts=8 sw=2 et cindent: */
|