nextcloud-desktop/src/csync_update.c

633 lines
19 KiB
C
Raw Normal View History

/*
* libcsync -- a library to sync a directory with another
*
* Copyright (c) 2008-2013 by Andreas Schneider <asn@cryptomilk.org>
* Copyright (c) 2012-2013 by Klaas Freitag <freitag@owncloud.com>wie
*
* 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.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* 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
*/
2012-03-02 19:47:34 +04:00
#include "config.h"
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <errno.h>
#include <stdio.h>
2008-08-01 17:57:19 +04:00
#include <string.h>
#include <inttypes.h>
#include "c_lib.h"
#include "c_jhash.h"
#include "csync_private.h"
#include "csync_exclude.h"
#include "csync_statedb.h"
#include "csync_update.h"
2008-04-30 18:24:50 +04:00
#include "csync_util.h"
#include "csync_misc.h"
#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"
/* calculate the hash of a given uri */
static uint64_t _hash_of_file(CSYNC *ctx, const char *file) {
const char *path;
int len;
uint64_t h = 0;
if( ctx && file ) {
path = file;
switch (ctx->current) {
case LOCAL_REPLICA:
2008-08-01 17:57:19 +04:00
if (strlen(path) <= strlen(ctx->local.uri)) {
return 0;
2008-08-01 17:57:19 +04:00
}
path += strlen(ctx->local.uri) + 1;
break;
case REMOTE_REPLICA:
2008-08-01 17:57:19 +04:00
if (strlen(path) <= strlen(ctx->remote.uri)) {
return 0;
2008-08-01 17:57:19 +04:00
}
path += strlen(ctx->remote.uri) + 1;
break;
default:
2008-08-01 17:57:19 +04:00
path = NULL;
return 0;
break;
}
len = strlen(path);
h = c_jhash64((uint8_t *) path, len, 0);
}
return h;
}
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;
CSYNC_EXCLUDE_TYPE excluded;
if ((file == NULL) || (fs == NULL)) {
errno = EINVAL;
ctx->status_code = CSYNC_STATUS_PARAM_ERROR;
return -1;
}
2013-03-22 19:29:04 +04:00
path = file;
switch (ctx->current) {
case LOCAL_REPLICA:
2008-08-01 17:57:19 +04:00
if (strlen(path) <= strlen(ctx->local.uri)) {
ctx->status_code = CSYNC_STATUS_PARAM_ERROR;
2008-08-01 17:57:19 +04:00
return -1;
}
path += strlen(ctx->local.uri) + 1;
break;
case REMOTE_REPLICA:
2008-08-01 17:57:19 +04:00
if (strlen(path) <= strlen(ctx->remote.uri)) {
ctx->status_code = CSYNC_STATUS_PARAM_ERROR;
2008-08-01 17:57:19 +04:00
return -1;
}
path += strlen(ctx->remote.uri) + 1;
break;
default:
2008-08-01 17:57:19 +04:00
path = NULL;
ctx->status_code = CSYNC_STATUS_PARAM_ERROR;
2013-03-22 19:29:04 +04:00
return -1;
}
2013-04-03 18:38:35 +04:00
len = strlen(path);
/* Check if file is excluded */
2013-09-12 16:46:48 +04:00
excluded = csync_excluded(ctx, path,type);
if (excluded != CSYNC_NOT_EXCLUDED) {
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "%s excluded (%d)", path, excluded);
if (excluded == CSYNC_FILE_EXCLUDE_AND_REMOVE) {
switch (ctx->current) {
case LOCAL_REPLICA:
ctx->local.ignored_cleanup = c_list_append(ctx->local.ignored_cleanup, c_strdup(path));
break;
case REMOTE_REPLICA:
ctx->remote.ignored_cleanup = c_list_append(ctx->remote.ignored_cleanup, c_strdup(path));
break;
default:
break;
}
2013-08-18 19:51:32 +04:00
return 0;
}
if (excluded == CSYNC_FILE_SILENTLY_EXCLUDED) {
return 0;
}
}
h = _hash_of_file(ctx, file );
if( h == 0 ) {
return -1;
}
2013-03-23 00:48:01 +04:00
size = sizeof(csync_file_stat_t) + len + 1;
2008-06-27 20:01:19 +04:00
st = c_malloc(size);
if (st == NULL) {
ctx->status_code = CSYNC_STATUS_MEMORY_ERROR;
return -1;
}
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "==> file: %s - hash %llu, mtime: %llu, fileId: %s",
path, (unsigned long long ) h, (unsigned long long) fs->mtime, fs->file_id);
2008-06-28 19:14:20 +04:00
/* Set instruction by default to none */
st->instruction = CSYNC_INSTRUCTION_NONE;
st->etag = NULL;
st->child_modified = 0;
2008-06-28 19:14:20 +04:00
/* check hardlink count */
if (type == CSYNC_FTW_TYPE_FILE ) {
if( fs->nlink > 1) {
st->instruction = CSYNC_INSTRUCTION_IGNORE;
goto out;
}
if (fs->mtime == 0) {
tmp = csync_statedb_get_stat_by_hash(ctx->statedb.db, h);
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "file: %s - mtime is zero!", path);
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 );
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-19 14:48:46 +04:00
/* Ignore non statable files and other strange cases. */
if (type == CSYNC_FTW_TYPE_SKIP) {
2013-03-19 14:48:46 +04:00
st->instruction = CSYNC_INSTRUCTION_NONE;
goto out;
}
if (excluded > CSYNC_NOT_EXCLUDED || type == CSYNC_FTW_TYPE_SLINK) {
st->instruction = CSYNC_INSTRUCTION_IGNORE;
goto out;
}
2013-03-19 14:48:46 +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)) {
tmp = csync_statedb_get_stat_by_hash(ctx->statedb.db, h);
2013-09-25 13:34:50 +04:00
2012-09-24 16:56:42 +04:00
if(tmp && tmp->phash == h ) { /* there is an entry in the database */
/* we have an update! */
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "Database entry found, compare: %" PRId64 " <-> %" PRId64 ", etag: %s <-> %s, inode: %" PRId64 " <-> %" PRId64,
((int64_t) fs->mtime), ((int64_t) tmp->modtime), fs->etag, tmp->etag, (uint64_t) fs->inode, (uint64_t) tmp->inode);
if( !fs->etag) {
2012-09-24 16:56:42 +04:00
st->instruction = CSYNC_INSTRUCTION_EVAL;
goto out;
}
if((ctx->current == REMOTE_REPLICA && !c_streq(fs->etag, tmp->etag ))
|| (ctx->current == LOCAL_REPLICA && (fs->mtime != tmp->modtime
#if 0
|| fs->inode != tmp->inode
#endif
))) {
/* Comparison of the local inode is disabled because people reported problems
* on windows with flacky inode values, see github bug #779
*
* The inode needs to be observed because:
* $> echo a > a.txt ; echo b > b.txt
* both files have the same mtime
* sync them.
* $> rm a.txt && mv b.txt a.txt
* makes b.txt appearing as a.txt yet a sync is not performed because
* both have the same modtime as mv does not change that.
*/
2012-09-24 16:56:42 +04:00
st->instruction = CSYNC_INSTRUCTION_EVAL;
goto out;
}
if (type == CSYNC_FTW_TYPE_DIR && ctx->current == REMOTE_REPLICA
&& c_streq(fs->file_id, tmp->file_id)) {
/* If both etag and file id are equal for a directory, read all contents from
* the database. */
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "Reading from database: %s", path);
ctx->remote.read_from_db = true;
}
2012-09-24 16:56:42 +04:00
st->instruction = CSYNC_INSTRUCTION_NONE;
} else {
/* check if it's a file and has been renamed */
2013-01-04 23:45:10 +04:00
if (ctx->current == LOCAL_REPLICA) {
tmp = csync_statedb_get_stat_by_inode(ctx->statedb.db, fs->inode);
if (tmp && tmp->inode == fs->inode && tmp->type == fs->type
&& (tmp->modtime == fs->mtime || fs->type == CSYNC_VIO_FILE_TYPE_DIRECTORY)) {
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "inodes: %" PRId64 " <-> %" PRId64, (uint64_t) tmp->inode, (uint64_t) fs->inode);
2012-09-24 16:56:42 +04:00
/* inode found so the file has been renamed */
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);
}
goto out;
2012-09-24 16:56:42 +04:00
} else {
/* file not found in statedb */
st->instruction = CSYNC_INSTRUCTION_NEW;
goto out;
}
} else {
/* Remote Replica Rename check */
tmp = csync_statedb_get_stat_by_file_id(ctx->statedb.db, fs->file_id);
if(tmp ) { /* tmp existing at all */
if ((tmp->type == CSYNC_FTW_TYPE_DIR && fs->type != CSYNC_VIO_FILE_TYPE_DIRECTORY) ||
(tmp->type == CSYNC_FTW_TYPE_FILE && fs->type != CSYNC_VIO_FILE_TYPE_REGULAR)) {
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "WARN: file types different is not!");
st->instruction = CSYNC_INSTRUCTION_NEW;
goto out;
}
st->instruction = CSYNC_INSTRUCTION_EVAL_RENAME;
if (fs->type == CSYNC_VIO_FILE_TYPE_DIRECTORY) {
csync_rename_record(ctx, tmp->path, path);
} else {
if( !c_streq(tmp->etag, fs->etag) ) {
/* 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;
}
}
goto out;
} else {
/* file not found in statedb */
st->instruction = CSYNC_INSTRUCTION_NEW;
goto out;
}
2008-04-30 18:24:50 +04:00
}
}
} else {
st->instruction = CSYNC_INSTRUCTION_NEW;
}
out:
/* Set the ignored error string. */
if (st->instruction == CSYNC_INSTRUCTION_IGNORE) {
if (excluded == CSYNC_FILE_EXCLUDE_LIST) {
st->error_string = c_strdup("File listed on ignore list.");
} else if (excluded == CSYNC_FILE_EXCLUDE_INVALID_CHAR) {
st->error_string = c_strdup("File contains invalid characters.");
}
}
if (st->instruction != CSYNC_INSTRUCTION_NONE && st->instruction != CSYNC_INSTRUCTION_IGNORE
&& type != CSYNC_FTW_TYPE_DIR) {
st->child_modified = 1;
}
ctx->current_fs = st;
csync_file_stat_free(tmp);
st->inode = fs->inode;
st->mode = fs->mode;
st->size = fs->size;
st->modtime = fs->mtime;
st->uid = fs->uid;
st->gid = fs->gid;
2008-04-29 11:07:30 +04:00
st->nlink = fs->nlink;
st->type = type;
st->etag = NULL;
if( fs->etag ) {
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);
2013-04-08 16:20:38 +04:00
fastout: /* target if the file information is read from database into st */
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);
ctx->status_code = CSYNC_STATUS_TREE_ERROR;
return -1;
}
break;
case REMOTE_REPLICA:
if (c_rbtree_insert(ctx->remote.tree, (void *) st) < 0) {
SAFE_FREE(st);
ctx->status_code = CSYNC_STATUS_TREE_ERROR;
return -1;
}
break;
default:
break;
}
2013-06-05 18:35:48 +04:00
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "file: %s, instruction: %s <<=", st->path,
csync_instruction_str(st->instruction));
return 0;
}
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;
int type = CSYNC_FTW_TYPE_SKIP;
csync_file_stat_t *st = NULL;
uint64_t h;
2013-03-19 14:48:46 +04:00
if (ctx->abort) {
2013-06-19 20:41:30 +04:00
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "Aborted!");
2013-08-18 19:26:45 +04:00
ctx->status_code = CSYNC_STATUS_ABORTED;
return -1;
}
switch (flag) {
case CSYNC_FTW_FLAG_FILE:
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "file: %s", file);
2013-03-19 14:48:46 +04:00
type = CSYNC_FTW_TYPE_FILE;
break;
2013-03-19 14:48:46 +04:00
case CSYNC_FTW_FLAG_DIR: /* enter directory */
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "directory: %s", file);
type = CSYNC_FTW_TYPE_DIR;
2008-05-13 18:06:11 +04:00
break;
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;
}
st = csync_statedb_get_stat_by_hash(ctx->statedb.db, h);
if( !st ) {
return 0;
}
csync_file_stat_free(st);
st = NULL;
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);
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:
return 0;
break;
}
2013-03-19 14:48:46 +04:00
rc = _csync_detect_update(ctx, file, fs, type );
return rc;
}
/* File tree walker */
int csync_ftw(CSYNC *ctx, const char *uri, csync_walker_fn fn,
unsigned int depth) {
char errbuf[256] = {0};
char *filename = NULL;
char *d_name = NULL;
csync_vio_handle_t *dh = NULL;
csync_vio_file_stat_t *dirent = NULL;
csync_vio_file_stat_t *fs = NULL;
csync_file_stat_t *previous_fs = NULL;
int read_from_db = 0;
int rc = 0;
int res = 0;
bool do_read_from_db = (ctx->current == REMOTE_REPLICA && ctx->remote.read_from_db);
if (uri[0] == '\0') {
errno = ENOENT;
ctx->status_code = CSYNC_STATUS_PARAM_ERROR;
goto error;
}
read_from_db = ctx->remote.read_from_db;
if ((dh = csync_vio_opendir(ctx, uri)) == NULL) {
/* permission denied */
ctx->status_code = csync_errno_to_status(errno, CSYNC_STATUS_OPENDIR_ERROR);
if (errno == EACCES) {
return 0;
} else if(errno == EIO ) {
/* Proxy problems (ownCloud) */
2013-08-18 19:26:45 +04:00
ctx->status_code = CSYNC_STATUS_PROXY_ERROR;
goto error;
} else {
C_STRERROR(errno, errbuf, sizeof(errbuf));
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR,
"opendir failed for %s - %s (errno %d)",
uri, errbuf, errno);
goto error;
}
}
while ((dirent = csync_vio_readdir(ctx, dh))) {
const char *path = NULL;
2013-07-25 10:36:23 +04:00
size_t ulen = 0;
int flen;
int flag;
d_name = dirent->name;
if (d_name == NULL) {
ctx->status_code = CSYNC_STATUS_READDIR_ERROR;
goto error;
}
/* skip "." and ".." */
if (d_name[0] == '.' && (d_name[1] == '\0'
|| (d_name[1] == '.' && d_name[2] == '\0'))) {
csync_vio_file_stat_destroy(dirent);
dirent = NULL;
continue;
}
flen = asprintf(&filename, "%s/%s", uri, d_name);
if (flen < 0) {
csync_vio_file_stat_destroy(dirent);
dirent = NULL;
ctx->status_code = CSYNC_STATUS_MEMORY_ERROR;
goto error;
}
/* Create relative path */
switch (ctx->current) {
case LOCAL_REPLICA:
ulen = strlen(ctx->local.uri) + 1;
break;
case REMOTE_REPLICA:
ulen = strlen(ctx->remote.uri) + 1;
break;
default:
break;
}
if (((size_t)flen) < ulen) {
csync_vio_file_stat_destroy(dirent);
dirent = NULL;
ctx->status_code = CSYNC_STATUS_UNSUCCESSFUL;
goto error;
}
path = filename + ulen;
/* skip ".csync_journal.db" and ".csync_journal.db.ctmp" */
if (c_streq(path, ".csync_journal.db")
|| c_streq(path, ".csync_journal.db.ctmp")
|| c_streq(path, ".csync_journal.db.ctmp-journal")
|| c_streq(path, ".csync-progressdatabase")) {
csync_vio_file_stat_destroy(dirent);
dirent = NULL;
SAFE_FREE(filename);
continue;
}
/* == see if really stat has to be called. */
if( do_read_from_db ) {
fs = dirent;
res = 0;
} else {
fs = csync_vio_file_stat_new();
res = csync_vio_stat(ctx, filename, fs);
}
if( res == 0) {
switch (fs->type) {
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;
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:
flag = CSYNC_FTW_FLAG_SPEC;
break;
default:
flag = CSYNC_FTW_FLAG_FILE;
break;
};
} else {
flag = CSYNC_FTW_FLAG_NSTAT;
}
if( ctx->current == LOCAL_REPLICA ) {
char *etag = NULL;
int len = strlen( path );
uint64_t h = c_jhash64((uint8_t *) path, len, 0);
etag = csync_statedb_get_uniqId( ctx, h, fs );
if( etag ) {
SAFE_FREE(fs->etag);
fs->etag = etag;
fs->fields |= CSYNC_VIO_FILE_STAT_FIELDS_ETAG;
}
if( c_streq(etag, "")) {
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Uniq ID from Database is EMPTY: %s", path);
} else {
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Uniq ID from Database: %s -> %s", path, fs->etag ? fs->etag : "<NULL>" );
}
}
previous_fs = ctx->current_fs;
/* Call walker function for each file */
rc = fn(ctx, filename, fs, flag);
/* this function may update ctx->current and ctx->read_from_db */
if (ctx->current_fs && previous_fs && ctx->current_fs->child_modified) {
previous_fs->child_modified = ctx->current_fs->child_modified;
}
if( ! do_read_from_db ) {
csync_vio_file_stat_destroy(fs);
} else {
SAFE_FREE(fs->etag);
}
if (rc < 0) {
if (!CSYNC_STATUS_IS_OK(ctx->status_code)) {
ctx->status_code = CSYNC_STATUS_UPDATE_ERROR;
}
csync_vio_closedir(ctx, dh);
ctx->current_fs = previous_fs;
goto done;
}
if (flag == CSYNC_FTW_FLAG_DIR && depth) {
rc = csync_ftw(ctx, filename, fn, depth - 1);
if (rc < 0) {
ctx->current_fs = previous_fs;
csync_vio_closedir(ctx, dh);
goto done;
}
if (ctx->current_fs && !ctx->current_fs->child_modified
&& ctx->current_fs->instruction == CSYNC_INSTRUCTION_EVAL) {
ctx->current_fs->instruction = CSYNC_INSTRUCTION_NONE;
ctx->current_fs->should_update_etag = true;
}
}
if (ctx->current_fs && (ctx->current_fs->instruction == CSYNC_INSTRUCTION_EVAL ||
ctx->current_fs->instruction == CSYNC_INSTRUCTION_NEW)) {
ctx->current_fs->should_update_etag = true;
}
ctx->current_fs = previous_fs;
ctx->remote.read_from_db = read_from_db;
SAFE_FREE(filename);
csync_vio_file_stat_destroy(dirent);
dirent = NULL;
}
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);
done:
ctx->remote.read_from_db = read_from_db;
csync_vio_file_stat_destroy(dirent);
SAFE_FREE(filename);
return rc;
error:
if (dh != NULL) {
csync_vio_closedir(ctx, dh);
}
ctx->remote.read_from_db = read_from_db;
SAFE_FREE(filename);
return -1;
}
2009-05-13 12:12:07 +04:00
/* vim: set ts=8 sw=2 et cindent: */