nextcloud-desktop/client/csync_client.c

254 lines
6.9 KiB
C
Raw Normal View History

2008-02-27 20:56:47 +03:00
/*
* libcsync -- a library to sync a replica with another
*
* Copyright (c) 2006-2007 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.
*/
2008-07-19 14:03:48 +04:00
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
2008-02-27 20:56:47 +03:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
2008-02-27 20:56:47 +03:00
#include <csync.h>
#include <c_string.h>
#include <c_alloc.h>
#include "csync_auth.h"
2012-02-20 21:23:31 +04:00
#include "../src/std/c_private.h"
const char *csync_program_version = "csync commandline client "
2009-03-26 12:40:16 +03:00
CSYNC_STRINGIFY(LIBCSYNC_VERSION);
2008-02-27 20:56:47 +03:00
/* Program documentation. */
static char doc[] = "Usage: csync [OPTION...] LOCAL REMOTE\n\
csync -- a user level file synchronizer which synchronizes the files\n\
at LOCAL with the ones at REMOTE.\n\
\n\
-c, --conflict-copys Create conflict copys if file changed on both\n\
sides.\n\
-d, --disable-statedb Disable the usage and creation of a statedb.\n\
--dry-run This runs only update detection and reconcilation.\n\
\n\
--exclude-file=<file> Add an additional exclude file\n\
--test-statedb Test creation of the statedb. Runs update\n\
detection.\n\
--test-update Test the update detection\n\
-?, --help Give this help list\n\
--usage Give a short usage message\n\
-V, --version Print program version\n\
";
2008-02-27 20:56:47 +03:00
/* The options we understand. */
static const struct option long_options[] =
{
{"exclude-file", required_argument, 0, 0 },
{"disable-statedb", no_argument, 0, 'd' },
{"dry-run", no_argument, 0, 0 },
{"test-statedb", no_argument, 0, 0 },
{"conflict-copies", no_argument, 0, 'c' },
{"test-update", no_argument, 0, 0 },
{"version", no_argument, 0, 'V' },
{"usage", no_argument, 0, 'h' },
{0, 0, 0, 0}
2008-02-27 20:56:47 +03:00
};
/* Used by main to communicate with parse_opt. */
struct argument_s {
char *args[2]; /* SOURCE and DESTINATION */
char *exclude_file;
2008-07-09 12:10:00 +04:00
int disable_statedb;
int create_statedb;
2008-02-27 20:56:47 +03:00
int update;
int reconcile;
int propagate;
bool with_conflict_copys;
2008-02-27 20:56:47 +03:00
};
static void print_version()
{
printf( "%s\n", csync_program_version );
exit(0);
}
static void print_help()
{
printf( "%s\n", doc );
exit(0);
}
2008-05-07 12:55:08 +04:00
static int parse_args(struct argument_s *csync_args, int argc, char **argv)
{
while(optind < argc) {
int c = -1;
struct option *opt = NULL;
int result = getopt_long( argc, argv, "dcVh", long_options, &c );
if( result == -1 ) {
break;
}
switch(result) {
case 'd':
csync_args->disable_statedb = 1;
/* printf("Argument: Disable Statedb\n"); */
break;
case 'c':
csync_args->with_conflict_copys = true;
/* printf("Argument: With conflict copies\n"); */
break;
case 'V':
print_version();
break;
case 'h':
print_help();
break;
case 0:
opt = (struct option*)&(long_options[c]);
if(c_streq(opt->name, "exclude-file")) {
csync_args->exclude_file = c_strdup(optarg);
/* printf("Argument: exclude-file: %s\n", csync_args->exclude_file); */
} else if(c_streq(opt->name, "test-update")) {
csync_args->create_statedb = 0;
csync_args->update = 1;
csync_args->reconcile = 0;
csync_args->propagate = 0;
/* printf("Argument: test-update\n"); */
} else if(c_streq(opt->name, "dry-run")) {
csync_args->create_statedb = 0;
csync_args->update = 1;
csync_args->reconcile = 1;
csync_args->propagate = 0;
/* printf("Argument: dry-run\n" ); */
} else if(c_streq(opt->name, "test-statedb")) {
csync_args->create_statedb = 1;
csync_args->update = 1;
csync_args->reconcile = 0;
csync_args->propagate = 0;
/* printf("Argument: test-statedb\n"); */
} else {
fprintf(stderr, "Argument: No idea what!\n");
}
break;
default:
break;
}
}
return optind;
2008-02-27 20:56:47 +03:00
}
int main(int argc, char **argv) {
2008-05-21 16:46:42 +04:00
int rc = 0;
2008-02-27 20:56:47 +03:00
CSYNC *csync;
char errbuf[256] = {0};
2008-02-27 20:56:47 +03:00
struct argument_s arguments;
/* Default values. */
2008-05-20 18:33:34 +04:00
arguments.exclude_file = NULL;
2008-07-09 12:10:00 +04:00
arguments.disable_statedb = 0;
arguments.create_statedb = 0;
2008-02-27 20:56:47 +03:00
arguments.update = 1;
arguments.reconcile = 1;
arguments.propagate = 1;
arguments.with_conflict_copys = false;
2008-02-27 20:56:47 +03:00
parse_args(&arguments, argc, argv);
/* two options must remain as source and target */
/* printf("ARGC: %d -> optind: %d\n", argc, optind ); */
if( argc - optind < 2 ) {
print_help();
}
2008-02-27 20:56:47 +03:00
if (csync_create(&csync, argv[optind], argv[optind+1]) < 0) {
2008-02-27 20:56:47 +03:00
fprintf(stderr, "csync_create: failed\n");
exit(1);
}
csync_set_auth_callback(csync, csync_getpass);
2008-07-09 12:10:00 +04:00
if (arguments.disable_statedb) {
csync_disable_statedb(csync);
}
if(arguments.with_conflict_copys)
{
csync_enable_conflictcopys(csync);
}
2008-05-20 18:33:34 +04:00
2008-05-21 16:46:42 +04:00
if (csync_init(csync) < 0) {
perror("csync_init");
rc = 1;
goto out;
}
if (arguments.exclude_file != NULL) {
if (csync_add_exclude_list(csync, arguments.exclude_file) < 0) {
fprintf(stderr, "csync_add_exclude_list - %s: %s\n",
arguments.exclude_file,
strerror_r(errno, errbuf, sizeof(errbuf)));
2008-05-21 16:46:42 +04:00
rc = 1;
goto out;
}
}
2008-02-27 20:56:47 +03:00
if (arguments.update) {
2008-05-15 15:50:34 +04:00
if (csync_update(csync) < 0) {
2008-05-21 16:46:42 +04:00
perror("csync_update");
rc = 1;
goto out;
2008-05-15 15:50:34 +04:00
}
2008-02-27 20:56:47 +03:00
}
if (arguments.reconcile) {
2008-05-15 15:50:34 +04:00
if (csync_reconcile(csync) < 0) {
2008-05-21 16:46:42 +04:00
perror("csync_reconcile");
rc = 1;
goto out;
2008-05-15 15:50:34 +04:00
}
2008-02-27 20:56:47 +03:00
}
if (arguments.propagate) {
if (csync_propagate(csync) < 0) {
2008-05-21 16:46:42 +04:00
perror("csync_propagate");
rc = 1;
goto out;
}
2008-02-27 20:56:47 +03:00
}
2008-07-09 12:10:00 +04:00
if (arguments.create_statedb) {
csync_set_status(csync, 0xFFFF);
}
2008-05-21 16:46:42 +04:00
out:
2008-05-15 15:50:34 +04:00
csync_destroy(csync);
2008-05-21 16:46:42 +04:00
return rc;
2008-02-27 20:56:47 +03:00
}
2009-05-13 12:12:07 +04:00
/* vim: set ts=8 sw=2 et cindent: */