2008-05-05 12:35:38 +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-05-05 12:35:38 +04:00
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "csync_time.h"
|
|
|
|
#include "vio/csync_vio.h"
|
|
|
|
|
|
|
|
#define CSYNC_LOG_CATEGORY_NAME "csync.time"
|
|
|
|
#include "csync_log.h"
|
|
|
|
|
2012-02-04 17:24:22 +04:00
|
|
|
#ifdef HAVE_CLOCK_GETTIME
|
|
|
|
# ifdef _POSIX_MONOTONIC_CLOCK
|
|
|
|
# define CSYNC_CLOCK CLOCK_MONOTONIC
|
|
|
|
# else
|
|
|
|
# define CSYNC_CLOCK CLOCK_REALTIME
|
|
|
|
# endif
|
2012-02-04 16:09:07 +04:00
|
|
|
#endif
|
2012-04-15 18:39:33 +04:00
|
|
|
static enum csync_error_codes_e _csync_time_errno;
|
2012-02-04 16:09:07 +04:00
|
|
|
|
|
|
|
int csync_gettime(struct timespec *tp)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_CLOCK_GETTIME
|
2012-02-04 17:24:22 +04:00
|
|
|
return clock_gettime(CSYNC_CLOCK, tp);
|
2012-02-04 16:09:07 +04:00
|
|
|
#else
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
if (gettimeofday(&tv, NULL) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tp->tv_sec = tv.tv_sec;
|
|
|
|
tp->tv_nsec = tv.tv_usec * 1000;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef CSYNC_CLOCK
|
|
|
|
|
2008-05-05 12:35:38 +04:00
|
|
|
/* check time difference between the replicas */
|
|
|
|
time_t csync_timediff(CSYNC *ctx) {
|
|
|
|
time_t timediff = -1;
|
2008-07-18 13:35:02 +04:00
|
|
|
char errbuf[256] = {0};
|
2008-05-05 12:48:34 +04:00
|
|
|
char *luri = NULL;
|
|
|
|
char *ruri = NULL;
|
2008-05-05 12:35:38 +04:00
|
|
|
csync_vio_handle_t *fp = NULL;
|
|
|
|
csync_vio_file_stat_t *st = NULL;
|
2008-05-13 18:05:11 +04:00
|
|
|
csync_vio_handle_t *dp = NULL;
|
|
|
|
|
2012-04-15 18:39:33 +04:00
|
|
|
_csync_time_errno = CSYNC_ERR_NONE;
|
|
|
|
|
2008-05-13 18:05:11 +04:00
|
|
|
/* try to open remote dir to get auth */
|
|
|
|
ctx->replica = ctx->remote.type;
|
|
|
|
dp = csync_vio_opendir(ctx, ctx->remote.uri);
|
|
|
|
if (dp == NULL) {
|
2008-06-13 20:44:15 +04:00
|
|
|
/*
|
|
|
|
* To prevent problems especially with pam_csync we shouldn't try to create the
|
|
|
|
* remote directory here. Just fail!
|
|
|
|
*/
|
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_FATAL,
|
|
|
|
"Access dienied to remote uri: %s - %s",
|
|
|
|
ctx->remote.uri,
|
2012-03-07 19:54:17 +04:00
|
|
|
errbuf);
|
2012-04-15 18:39:33 +04:00
|
|
|
_csync_time_errno = CSYNC_ERR_ACCESS_FAILED;
|
2008-06-13 20:44:15 +04:00
|
|
|
return -1;
|
2008-05-13 18:05:11 +04:00
|
|
|
}
|
|
|
|
csync_vio_closedir(ctx, dp);
|
2008-05-05 12:35:38 +04:00
|
|
|
|
2012-06-18 18:20:28 +04:00
|
|
|
if (asprintf(&luri, "%s/.csync_timediff.ctmp", ctx->local.uri) < 0) {
|
2008-05-05 12:35:38 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-06-18 18:20:28 +04:00
|
|
|
if (asprintf(&ruri, "%s/.csync_timediff.ctmp", ctx->remote.uri) < 0) {
|
2008-05-05 12:48:34 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2008-05-05 12:35:38 +04:00
|
|
|
/* create temporary file on local */
|
|
|
|
ctx->replica = ctx->local.type;
|
|
|
|
fp = csync_vio_creat(ctx, luri, 0644);
|
|
|
|
if (fp == NULL) {
|
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_FATAL,
|
|
|
|
"Unable to create temporary file: %s - %s",
|
|
|
|
luri,
|
2012-03-07 19:54:17 +04:00
|
|
|
errbuf);
|
2012-04-15 18:39:33 +04:00
|
|
|
_csync_time_errno = CSYNC_ERR_LOCAL_CREATE;
|
2008-05-05 12:35:38 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
csync_vio_close(ctx, fp);
|
|
|
|
|
|
|
|
/* Get the modification time */
|
|
|
|
st = csync_vio_file_stat_new();
|
|
|
|
if (csync_vio_stat(ctx, luri, st) < 0) {
|
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_FATAL,
|
|
|
|
"Synchronisation is not possible! %s - %s",
|
|
|
|
luri,
|
2012-03-07 19:54:17 +04:00
|
|
|
errbuf);
|
2012-04-15 18:39:33 +04:00
|
|
|
_csync_time_errno = CSYNC_ERR_LOCAL_STAT;
|
2008-05-05 12:35:38 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
timediff = st->mtime;
|
|
|
|
csync_vio_file_stat_destroy(st);
|
2008-05-06 17:41:52 +04:00
|
|
|
st = NULL;
|
2008-05-05 12:35:38 +04:00
|
|
|
|
|
|
|
/* create temporary file on remote replica */
|
|
|
|
ctx->replica = ctx->remote.type;
|
2008-05-13 18:05:11 +04:00
|
|
|
|
|
|
|
fp = csync_vio_creat(ctx, ruri, 0644);
|
2008-05-05 12:35:38 +04:00
|
|
|
if (fp == NULL) {
|
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_FATAL,
|
|
|
|
"Unable to create temporary file: %s - %s",
|
|
|
|
ruri,
|
2012-03-07 19:54:17 +04:00
|
|
|
errbuf);
|
2012-04-15 18:39:33 +04:00
|
|
|
_csync_time_errno = CSYNC_ERR_REMOTE_CREATE;
|
2008-05-05 12:35:38 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
csync_vio_close(ctx, fp);
|
|
|
|
|
|
|
|
/* Get the modification time */
|
|
|
|
st = csync_vio_file_stat_new();
|
2008-05-13 18:05:11 +04:00
|
|
|
if (csync_vio_stat(ctx, ruri, st) < 0) {
|
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_FATAL,
|
|
|
|
"Synchronisation is not possible! %s - %s",
|
|
|
|
ruri,
|
2012-03-07 19:54:17 +04:00
|
|
|
errbuf);
|
2012-04-15 18:39:33 +04:00
|
|
|
_csync_time_errno = CSYNC_ERR_REMOTE_STAT;
|
2008-05-05 12:35:38 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* calc time difference */
|
2012-03-07 19:16:59 +04:00
|
|
|
timediff = llabs(timediff - st->mtime);
|
2008-05-05 12:35:38 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Time difference: %ld seconds", timediff);
|
|
|
|
|
|
|
|
out:
|
|
|
|
csync_vio_file_stat_destroy(st);
|
2008-05-15 19:26:26 +04:00
|
|
|
|
|
|
|
ctx->replica = ctx->local.type;
|
2008-05-05 12:35:38 +04:00
|
|
|
csync_vio_unlink(ctx, luri);
|
|
|
|
SAFE_FREE(luri);
|
2008-05-15 19:26:26 +04:00
|
|
|
|
|
|
|
ctx->replica = ctx->remote.type;
|
2008-05-05 12:35:38 +04:00
|
|
|
csync_vio_unlink(ctx, ruri);
|
|
|
|
SAFE_FREE(ruri);
|
2008-05-15 19:26:26 +04:00
|
|
|
|
2008-05-05 12:35:38 +04:00
|
|
|
return timediff;
|
|
|
|
}
|
|
|
|
|
2012-04-15 18:39:33 +04:00
|
|
|
CSYNC_ERROR_CODE csync_time_errno(void) {
|
|
|
|
return _csync_time_errno;
|
|
|
|
}
|
|
|
|
|
2009-05-13 12:12:07 +04:00
|
|
|
/* vim: set ts=8 sw=2 et cindent: */
|