#include <sys/stat.h>
#include <assert.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <err.h>
#include "extern.h"
int
rsync_client(const struct opts *opts, int fd, const struct fargs *f)
{
struct sess sess;
int rc = 1;
if (pledge("stdio unix rpath wpath cpath dpath fattr chown getpw unveil",
NULL) == -1)
err(ERR_IPC, "pledge");
memset(&sess, 0, sizeof(struct sess));
sess.opts = opts;
sess.lver = RSYNC_PROTOCOL;
if (!io_write_int(&sess, fd, sess.lver)) {
ERRX1("io_write_int");
goto out;
} else if (!io_read_int(&sess, fd, &sess.rver)) {
ERRX1("io_read_int");
goto out;
} else if (!io_read_int(&sess, fd, &sess.seed)) {
ERRX1("io_read_int");
goto out;
}
if (sess.rver < sess.lver) {
ERRX("remote protocol %d is older than our own %d: unsupported",
sess.rver, sess.lver);
rc = 2;
goto out;
}
LOG2("client detected client version %d, server version %d, seed %d",
sess.lver, sess.rver, sess.seed);
sess.mplex_reads = 1;
if (f->mode != FARGS_RECEIVER) {
LOG2("client starting sender: %s",
f->host == NULL ? "(local)" : f->host);
if (!rsync_sender(&sess, fd, fd, f->sourcesz,
f->sources)) {
ERRX1("rsync_sender");
goto out;
}
} else {
LOG2("client starting receiver: %s",
f->host == NULL ? "(local)" : f->host);
if (!rsync_receiver(&sess, fd, fd, f->sink)) {
ERRX1("rsync_receiver");
goto out;
}
}
#if 0
if (io_read_check(&sess, fd))
WARNX("data remains in read pipe");
#endif
rc = 0;
out:
return rc;
}