2008-04-23 14:12:48 +04:00
|
|
|
/*
|
|
|
|
* libcsync -- a library to sync a directory with another
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 by Andreas Schneider <mail@cynapses.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; 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"
|
|
|
|
|
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>
|
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"
|
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"
|
|
|
|
|
2009-05-25 13:33:18 +04:00
|
|
|
static int _csync_detect_update(CSYNC *ctx, const char *file,
|
|
|
|
const csync_vio_file_stat_t *fs, const int type) {
|
2008-05-27 16:31:57 +04:00
|
|
|
uint64_t h = 0;
|
|
|
|
size_t len = 0;
|
2008-06-27 20:01:19 +04:00
|
|
|
size_t size = 0;
|
2008-04-28 18:50:25 +04:00
|
|
|
const char *path = NULL;
|
|
|
|
csync_file_stat_t *st = NULL;
|
2008-04-30 18:24:50 +04:00
|
|
|
csync_file_stat_t *tmp = NULL;
|
2008-04-26 12:42:20 +04:00
|
|
|
|
2008-04-23 14:12:48 +04:00
|
|
|
if ((file == NULL) || (fs == NULL)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-08-01 17:57:19 +04:00
|
|
|
path = file;
|
2008-04-28 18:50:25 +04:00
|
|
|
switch (ctx->current) {
|
|
|
|
case LOCAL_REPLICA:
|
2008-08-01 17:57:19 +04:00
|
|
|
if (strlen(path) <= strlen(ctx->local.uri)) {
|
|
|
|
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)) {
|
|
|
|
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;
|
|
|
|
return -1;
|
2008-04-28 18:50:25 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
len = strlen(path);
|
2008-04-26 12:42:20 +04:00
|
|
|
|
2008-04-28 18:50:25 +04:00
|
|
|
h = c_jhash64((uint8_t *) path, len, 0);
|
2008-06-27 20:01:19 +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-28 18:50:25 +04:00
|
|
|
if (st == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2008-06-27 20:52:09 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "file: %s - hash %llu, st size: %zu",
|
|
|
|
path, (long long unsigned int) h, 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;
|
|
|
|
|
2008-04-29 11:26:42 +04:00
|
|
|
/* check hardlink count */
|
|
|
|
if (type == CSYNC_FTW_TYPE_FILE && fs->nlink > 1) {
|
2008-04-28 18:50:25 +04:00
|
|
|
st->instruction = CSYNC_INSTRUCTION_IGNORE;
|
|
|
|
goto out;
|
2008-04-23 14:12:48 +04:00
|
|
|
}
|
|
|
|
|
2008-05-09 15:34:46 +04:00
|
|
|
/* Update detection */
|
2008-07-09 12:10:00 +04:00
|
|
|
if (csync_get_statedb_exists(ctx)) {
|
|
|
|
tmp = csync_statedb_get_stat_by_hash(ctx, h);
|
2008-09-03 11:46:19 +04:00
|
|
|
if (tmp && tmp->phash == h) {
|
|
|
|
/* we have an update! */
|
|
|
|
if (fs->mtime > tmp->modtime) {
|
|
|
|
st->instruction = CSYNC_INSTRUCTION_EVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
st->instruction = CSYNC_INSTRUCTION_NONE;
|
|
|
|
} else {
|
2008-04-30 18:24:50 +04:00
|
|
|
/* check if the file has been renamed */
|
|
|
|
if (ctx->current == LOCAL_REPLICA) {
|
2012-10-19 22:21:14 +04:00
|
|
|
free(tmp);
|
2008-07-09 12:10:00 +04:00
|
|
|
tmp = csync_statedb_get_stat_by_inode(ctx, fs->inode);
|
2008-09-03 11:46:19 +04:00
|
|
|
if (tmp && tmp->inode == fs->inode) {
|
2008-06-02 20:06:17 +04:00
|
|
|
/* inode found so the file has been renamed */
|
|
|
|
st->instruction = CSYNC_INSTRUCTION_RENAME;
|
|
|
|
goto out;
|
2008-09-03 11:46:19 +04:00
|
|
|
} else {
|
|
|
|
/* file not found in statedb */
|
|
|
|
st->instruction = CSYNC_INSTRUCTION_NEW;
|
|
|
|
goto out;
|
2008-04-30 18:24:50 +04:00
|
|
|
}
|
|
|
|
}
|
2008-07-09 12:10:00 +04:00
|
|
|
/* remote and file not found in statedb */
|
2008-04-30 18:24:50 +04:00
|
|
|
st->instruction = CSYNC_INSTRUCTION_NEW;
|
|
|
|
}
|
2008-04-29 11:27:49 +04:00
|
|
|
} else {
|
|
|
|
st->instruction = CSYNC_INSTRUCTION_NEW;
|
|
|
|
}
|
2008-04-28 18:50:25 +04:00
|
|
|
|
|
|
|
out:
|
2008-04-30 18:24:50 +04:00
|
|
|
SAFE_FREE(tmp);
|
2008-04-28 18:50:25 +04:00
|
|
|
st->inode = fs->inode;
|
|
|
|
st->mode = fs->mode;
|
2008-05-16 19:30:22 +04:00
|
|
|
st->size = fs->size;
|
2008-06-02 20:03:24 +04:00
|
|
|
st->modtime = fs->mtime;
|
2008-04-28 18:50:25 +04:00
|
|
|
st->uid = fs->uid;
|
|
|
|
st->gid = fs->gid;
|
2008-04-29 11:07:30 +04:00
|
|
|
st->nlink = fs->nlink;
|
2008-04-28 18:50:25 +04:00
|
|
|
st->type = type;
|
|
|
|
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-05-25 13:33:18 +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) {
|
2008-04-23 14:12:48 +04:00
|
|
|
switch (flag) {
|
|
|
|
case CSYNC_FTW_FLAG_FILE:
|
2008-05-26 19:09:42 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "file: %s", file);
|
|
|
|
|
2008-06-02 17:11:45 +04:00
|
|
|
return _csync_detect_update(ctx, file, fs, CSYNC_FTW_TYPE_FILE);
|
2008-05-26 19:09:42 +04:00
|
|
|
break;
|
2008-04-23 14:12:48 +04:00
|
|
|
case CSYNC_FTW_FLAG_SLINK:
|
2008-05-26 19:09:42 +04:00
|
|
|
/* FIXME: implement support for symlinks, see csync_propagate.c too */
|
|
|
|
#if 0
|
|
|
|
if (ctx->options.sync_symbolic_links) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "symlink: %s", file);
|
|
|
|
|
2008-06-02 17:11:45 +04:00
|
|
|
return _csync_detect_update(ctx, file, fs, CSYNC_FTW_TYPE_SLINK);
|
2008-04-23 14:12:48 +04:00
|
|
|
}
|
2008-05-26 19:09:42 +04:00
|
|
|
#endif
|
|
|
|
break;
|
2008-04-23 14:12:48 +04:00
|
|
|
case CSYNC_FTW_FLAG_DIR: /* enter directory */
|
2008-05-15 22:00:01 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "directory: %s", file);
|
2008-04-23 14:12:48 +04:00
|
|
|
|
2008-06-02 17:11:45 +04:00
|
|
|
return _csync_detect_update(ctx, file, fs, CSYNC_FTW_TYPE_DIR);
|
2008-04-23 14:12:48 +04:00
|
|
|
case CSYNC_FTW_FLAG_NSTAT: /* not statable file */
|
|
|
|
case CSYNC_FTW_FLAG_DNR:
|
|
|
|
case CSYNC_FTW_FLAG_DP:
|
|
|
|
case CSYNC_FTW_FLAG_SLN:
|
|
|
|
break;
|
2008-05-13 18:06:11 +04:00
|
|
|
default:
|
|
|
|
break;
|
2008-04-23 14:12:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2008-04-26 12:42:20 +04:00
|
|
|
|
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-07-18 13:35:02 +04:00
|
|
|
char errbuf[256] = {0};
|
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;
|
2008-04-23 14:12:48 +04:00
|
|
|
csync_vio_file_stat_t *fs = NULL;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
if (uri[0] == '\0') {
|
|
|
|
errno = ENOENT;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((dh = csync_vio_opendir(ctx, uri)) == NULL) {
|
|
|
|
/* permission denied */
|
|
|
|
if (errno == EACCES) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
2012-03-07 19:54:17 +04:00
|
|
|
strerror_r(errno, errbuf, sizeof(errbuf));
|
2008-07-18 13:35:02 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR,
|
|
|
|
"opendir failed for %s - %s",
|
|
|
|
uri,
|
2012-03-07 19:54:17 +04:00
|
|
|
errbuf);
|
2008-04-23 14:12:48 +04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2008-04-23 14:12:48 +04:00
|
|
|
int flag;
|
|
|
|
|
2008-04-26 12:42:20 +04:00
|
|
|
d_name = dirent->name;
|
2008-04-23 14:12:48 +04:00
|
|
|
if (d_name == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* skip "." and ".." */
|
|
|
|
if (d_name[0] == '.' && (d_name[1] == '\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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (asprintf(&filename, "%s/%s", uri, d_name) < 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
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2008-06-28 22:44:54 +04:00
|
|
|
/* Create relative path for checking the exclude list */
|
|
|
|
switch (ctx->current) {
|
|
|
|
case LOCAL_REPLICA:
|
|
|
|
path = filename + strlen(ctx->local.uri) + 1;
|
|
|
|
break;
|
2012-10-19 16:23:20 +04:00
|
|
|
case REMOTE_REPLICA:
|
2008-06-28 22:44:54 +04:00
|
|
|
path = filename + strlen(ctx->remote.uri) + 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if file is excluded */
|
|
|
|
if (csync_excluded(ctx, path)) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "%s excluded", path);
|
2013-02-07 15:52:25 +04:00
|
|
|
SAFE_FREE(filename);
|
2008-06-28 22:44:54 +04:00
|
|
|
csync_vio_file_stat_destroy(dirent);
|
|
|
|
dirent = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-04-23 14:12:48 +04:00
|
|
|
fs = csync_vio_file_stat_new();
|
|
|
|
if (csync_vio_stat(ctx, filename, fs) == 0) {
|
2008-06-16 19:48:48 +04:00
|
|
|
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;
|
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;
|
|
|
|
}
|
|
|
|
|
2008-05-15 22:00:01 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "walk: %s", filename);
|
2008-04-23 14:12:48 +04:00
|
|
|
|
|
|
|
/* Call walker function for each file */
|
|
|
|
rc = fn(ctx, filename, fs, flag);
|
|
|
|
csync_vio_file_stat_destroy(fs);
|
|
|
|
|
|
|
|
if (rc < 0) {
|
|
|
|
csync_vio_closedir(ctx, dh);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flag == CSYNC_FTW_FLAG_DIR && depth) {
|
|
|
|
rc = csync_ftw(ctx, filename, fn, depth - 1);
|
|
|
|
if (rc < 0) {
|
|
|
|
csync_vio_closedir(ctx, dh);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
csync_vio_closedir(ctx, dh);
|
|
|
|
|
|
|
|
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:
|
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: */
|