nextcloud-desktop/client/csync_client.c

257 lines
5.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.
*
* vim: ts=2 sw=2 et cindent
*/
#include <argp.h>
2008-02-27 20:56:47 +03:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
2008-02-27 20:56:47 +03:00
#include <csync.h>
#include "csync_auth.h"
enum {
KEY_DUMMY = 129,
KEY_EXCLUDE_FILE,
2008-07-09 12:10:00 +04:00
KEY_CREATE_STATEDB,
};
2008-05-05 15:53:07 +04:00
const char *argp_program_version = "csync commandline client 0.42";
2008-02-27 20:56:47 +03:00
const char *argp_program_bug_address = "<csync-devel@csync.org>";
/* Program documentation. */
static char doc[] = "csync -- a user level file synchronizer";
/* A description of the arguments we accept. */
static char args_doc[] = "SOURCE DESTINATION";
/* The options we understand. */
static struct argp_option options[] = {
2008-05-20 18:33:34 +04:00
{
2008-07-09 12:10:00 +04:00
.name = "disable-statedb",
2008-06-24 19:39:46 +04:00
.key = 'd',
2008-05-20 18:33:34 +04:00
.arg = NULL,
.flags = 0,
2008-07-09 12:10:00 +04:00
.doc = "Disable the usage and creation of a statedb.",
2008-05-20 18:33:34 +04:00
.group = 0
},
2008-05-07 12:55:08 +04:00
{
.name = "update",
.key = 'u',
.arg = NULL,
.flags = 0,
.doc = "Run only the update detection",
.group = 0
},
{
.name = "reconcile",
.key = 'r',
.arg = NULL,
.flags = 0,
2008-05-21 17:11:26 +04:00
.doc = "Run update detection and reconcilation",
2008-05-07 12:55:08 +04:00
.group = 0
},
{
2008-07-09 12:10:00 +04:00
.name = "create-statedb",
.key = KEY_CREATE_STATEDB,
2008-05-07 12:55:08 +04:00
.arg = NULL,
.flags = 0,
2008-07-09 12:10:00 +04:00
.doc = "Run update detection and write the statedb (TESTING ONLY!)",
.group = 0
},
{
.name = "exclude-file",
.key = KEY_EXCLUDE_FILE,
.arg = "<file>",
.flags = 0,
.doc = "Add an additional exclude file",
2008-05-07 12:55:08 +04:00
.group = 0
},
2008-02-27 20:56:47 +03:00
{NULL, 0, 0, 0, NULL, 0}
};
/* 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;
};
/* Parse a single option. */
static error_t parse_opt (int key, char *arg, struct argp_state *state) {
/* Get the input argument from argp_parse, which we
* know is a pointer to our arguments structure.
*/
struct argument_s *arguments = state->input;
switch (key) {
case 'u':
2008-07-09 12:10:00 +04:00
arguments->create_statedb = 0;
arguments->update = 1;
2008-02-27 20:56:47 +03:00
arguments->reconcile = 0;
arguments->propagate = 0;
break;
case 'r':
2008-07-09 12:10:00 +04:00
arguments->create_statedb = 0;
arguments->update = 1;
arguments->reconcile = 1;
2008-02-27 20:56:47 +03:00
arguments->propagate = 0;
2008-05-07 12:55:08 +04:00
break;
case KEY_EXCLUDE_FILE:
arguments->exclude_file = strdup(arg);
break;
2008-06-24 19:39:46 +04:00
case 'd':
2008-07-09 12:10:00 +04:00
arguments->disable_statedb = 1;
break;
2008-07-09 12:10:00 +04:00
case KEY_CREATE_STATEDB:
arguments->create_statedb = 1;
2008-06-24 19:39:46 +04:00
arguments->update = 1;
arguments->reconcile = 0;
arguments->propagate = 0;
break;
2008-02-27 20:56:47 +03:00
case ARGP_KEY_ARG:
if (state->arg_num >= 2) {
/* Too many arguments. */
argp_usage (state);
}
arguments->args[state->arg_num] = arg;
break;
case ARGP_KEY_END:
if (state->arg_num < 2) {
/* Not enough arguments. */
argp_usage (state);
}
break;
default:
return ARGP_ERR_UNKNOWN;
}
2008-05-07 12:55:08 +04:00
2008-02-27 20:56:47 +03:00
return 0;
}
static void csync_auth_fn(char *usr, size_t usrlen, char *pwd, size_t pwdlen) {
char tmp[256] = {0};
/* get username */
snprintf(tmp, 255, "Username: [%s] ", usr);
csync_text_prompt(tmp, tmp, 255);
if (tmp[strlen(tmp) - 1] == '\n') {
tmp[strlen(tmp) - 1] = '\0';
}
if (tmp[0] != '\0') {
strncpy(usr, tmp, usrlen - 1);
}
/* get password */
csync_password_prompt("Password: ", pwd, pwdlen, 0);
}
2008-02-27 20:56:47 +03:00
/* Our argp parser. */
static struct argp argp = {options, parse_opt, args_doc, doc, NULL, NULL, NULL};
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;
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;
/*
* Parse our arguments; every option seen by parse_opt will
* be reflected in arguments.
*/
argp_parse (&argp, argc, argv, 0, 0, &arguments);
if (csync_create(&csync, arguments.args[0], arguments.args[1]) < 0) {
2008-02-27 20:56:47 +03:00
fprintf(stderr, "csync_create: failed\n");
exit(1);
}
csync_set_auth_callback(csync, csync_auth_fn);
2008-07-09 12:10:00 +04:00
if (arguments.disable_statedb) {
csync_disable_statedb(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) {
2008-05-21 16:46:42 +04:00
fprintf(stderr, "csync_add_exclude_list - %s: %s\n", arguments.exclude_file,
strerror(errno));
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
}