Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Enhancements

- Add local FIFO transfers on supported Linux builds with `--stream-source`.
Require an exact byte count and EOF before replacing a regular destination.
- Accept `TCP_CONGESTION=NAME` in socket options on supported Linux builds.
Distinguish unknown names, unavailable options and malformed values.
- Add `--placement-log` for local receivers to record verified regular files
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ options. To get a complete list of supported options type:

See the [manpage][0] for more detailed information.

On supported Linux builds, `--stream-source --stream-size=SIZE` copies one
local named pipe into a regular file. The writer must produce exactly `SIZE`
bytes and close the pipe before rsync can publish the file.

For local transfers to an existing directory, `--compare-dest-per-dir` and
`--copy-dest-per-dir` can use files in that tree as bases for missing regular
files. The [option reference](rsync.1.md#options) describes their limits.
Expand Down
6 changes: 6 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,12 @@ dnl io.c and socket.c include <poll.h> unconditionally, so the HEADER has to
dnl be required too: a system that exposes poll() through some other header
dnl would otherwise pass configure and then fail to compile.
AC_CHECK_FUNCS([poll], , [AC_MSG_ERROR([rsync requires poll(); please report the platform to the rsync developers])])
case $host_os in
linux*)
AC_CHECK_HEADERS([linux/magic.h sys/vfs.h])
AC_CHECK_FUNCS([clock_gettime fstatfs])
;;
esac
if test x"$ac_cv_header_poll_h" != x"yes"; then
AC_MSG_ERROR([rsync requires <poll.h>; please report the platform to the rsync developers])
fi
Expand Down
109 changes: 109 additions & 0 deletions fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

#include "rsync.h"
#include "inums.h"
#ifdef SUPPORT_STREAM_SOURCE
#include <poll.h>
#endif

#ifndef ENODATA
#define ENODATA EAGAIN
Expand Down Expand Up @@ -334,6 +337,107 @@ struct map_struct *map_file(int fd, OFF_T len, int32 read_size, int32 blk_size)
}


#ifdef SUPPORT_STREAM_SOURCE
static int64 stream_millis(void)
{
struct timespec now;

if (clock_gettime(CLOCK_MONOTONIC, &now) < 0) {
rsyserr(FERROR, errno, "cannot read monotonic clock for FIFO input");
exit_cleanup(RERR_FILEIO);
}
return (int64)now.tv_sec * 1000 + now.tv_nsec / 1000000;
}

static int32 read_stream(struct map_struct *map, char *buf, int32 len)
{
int64 deadline = stream_millis() + map->stream_timeout * 1000;
struct pollfd pfd = { map->fd, POLLIN, 0 };

/* Linux suppresses POLLHUP on this nonblocking FIFO reader until a
* writer has opened it. Do not interpret a bare read of zero as EOF. */
for (;;) {
int64 remaining = deadline - stream_millis();
int ready;
ssize_t n;

if (remaining <= 0 || (ready = poll(&pfd, 1, (int)remaining)) == 0) {
rprintf(FERROR, "FIFO input timed out after %d seconds waiting for data or EOF\n",
map->stream_timeout);
exit_cleanup(RERR_TIMEOUT);
}
if (ready < 0) {
if (errno == EINTR)
continue;
rsyserr(FERROR, errno, "cannot poll FIFO input");
exit_cleanup(RERR_FILEIO);
}
if (pfd.revents & (POLLERR | POLLNVAL) || !(pfd.revents & (POLLIN | POLLHUP))) {
rprintf(FERROR, "unexpected FIFO poll events %#x\n", (unsigned int)pfd.revents);
exit_cleanup(RERR_FILEIO);
}
n = read(map->fd, buf, len);
if (n >= 0)
return (int32)n;
if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) {
rsyserr(FERROR, errno, "cannot read FIFO input");
exit_cleanup(RERR_FILEIO);
}
}
}

void check_stream_eof(struct map_struct *map)
{
char extra;

if (map->p_fd_offset != map->file_size) {
rprintf(FERROR, "FIFO transfer did not read the declared stream size\n");
exit_cleanup(RERR_FILEIO);
}
if (read_stream(map, &extra, 1) != 0) {
rprintf(FERROR, "FIFO input exceeds --stream-size (%s bytes)\n", big_num(map->file_size));
exit_cleanup(RERR_FILEIO);
}
}

static char *map_stream_ptr(struct map_struct *map, OFF_T offset, int32 len)
{
/* The matcher may look ahead beyond unsent literals. Keep those bytes
* until it has sent and checksummed them rather than seeking backwards. */
const int32 max_window = 4 * MAX_BLOCK_SIZE + 3 * CHUNK_SIZE + ALIGN_BOUNDARY;
OFF_T start = map->stream_keep - ALIGNED_OVERSHOOT(map->stream_keep);
int32 window, kept;

if (start < map->p_offset || start > map->p_fd_offset || offset < start
|| len > map->file_size - offset || offset - start > max_window - len) {
rprintf(FERROR, "FIFO read exceeds the forward input window\n");
exit_cleanup(RERR_FILEIO);
}
window = MAX(map->def_window_size, ALIGNED_LENGTH((int32)(offset - start) + len));
window = (int32)MIN((OFF_T)window, map->file_size - start);
if (window > map->p_size) {
map->p = realloc_array(map->p, char, window);
map->p_size = window;
}
kept = (int32)(map->p_fd_offset - start);
if (kept)
memmove(map->p, map->p + (start - map->p_offset), kept);
while (kept < window) {
int32 n = read_stream(map, map->p + kept, window - kept);
if (!n) {
rprintf(FERROR, "FIFO input ended before --stream-size at %s of %s bytes\n",
big_num(map->p_fd_offset), big_num(map->file_size));
exit_cleanup(RERR_FILEIO);
}
map->p_fd_offset += n;
kept += n;
}
map->p_offset = start;
map->p_len = window;
return map->p + (offset - start);
}
#endif

/* slide the read window in the file */
char *map_ptr(struct map_struct *map, OFF_T offset, int32 len)
{
Expand All @@ -352,6 +456,11 @@ char *map_ptr(struct map_struct *map, OFF_T offset, int32 len)
if (offset >= map->p_offset && offset+len <= map->p_offset+map->p_len)
return map->p + (offset - map->p_offset);

#ifdef SUPPORT_STREAM_SOURCE
if (map->stream_timeout)
return map_stream_ptr(map, offset, len);
#endif

/* nope, we are going to have to do a read. Work out our desired window */
align_fudge = (int32)ALIGNED_OVERSHOOT(offset);
window_start = offset - align_fudge;
Expand Down
19 changes: 18 additions & 1 deletion flist.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ extern char *files_from;
extern char *skip_dir_marker;
extern int one_file_system;
extern int copy_devices;
extern int stream_source;
extern OFF_T stream_size;
extern int copy_dirlinks;
extern int preserve_uid;
extern int preserve_gid;
Expand Down Expand Up @@ -1697,7 +1699,12 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
if (sanitize_paths)
sanitize_path(thisname, thisname, "", 0, SP_DEFAULT);

if (stp && (S_ISDIR(stp->st_mode) || IS_MISSING_FILE(*stp))) {
if (stream_source && am_sender && stp && S_ISFIFO(stp->st_mode)) {
/* The sender pinned this FIFO before advertising its exact byte count. */
st = *stp;
st.st_mode = S_IFREG | (st.st_mode & ACCESSPERMS);
st.st_size = stream_size;
} else if (stp && (S_ISDIR(stp->st_mode) || IS_MISSING_FILE(*stp))) {
/* This is needed to handle a "symlink/." with a --relative
* dir, or a request to delete a specific file. */
st = *stp;
Expand Down Expand Up @@ -2998,6 +3005,11 @@ struct file_list *send_file_list(int f, int argc, char *argv[])
if (fn != fbuf)
memmove(fbuf, fn, len + 1);

#ifdef SUPPORT_STREAM_SOURCE
if (stream_source)
pin_stream_source(fbuf, &st);
else
#endif
if (scan_link_stat(fbuf, &st, copy_dirlinks || name_type != NORMAL_NAME) != 0
|| (name_type != DOTDIR_NAME && is_excluded(fbuf, S_ISDIR(st.st_mode) != 0, SERVER_FILTERS))
|| (relative_paths && path_is_daemon_excluded(fbuf, 1))) {
Expand Down Expand Up @@ -3079,6 +3091,11 @@ struct file_list *send_file_list(int f, int argc, char *argv[])
send_file_name(f, flist, fbuf, &st, flags, NO_FILTERS);
}

if (stream_source && flist->used != 1) {
rprintf(FERROR, "--stream-source requires one readable FIFO source\n");
exit_cleanup(RERR_FILESELECT);
}

if (reenable_multiplex >= 0)
io_start_multiplex_in(reenable_multiplex);

Expand Down
17 changes: 17 additions & 0 deletions generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ extern int alt_dest_type;
extern int per_dir_basis;
extern int per_dir_basis_root_fd;
extern int whole_file;
extern int stream_source;
extern int list_only;
extern int read_batch;
extern int write_batch;
Expand Down Expand Up @@ -2538,6 +2539,11 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
fnamecmp_type = FNAMECMP_FNAME;

if (statret == 0 && !(stype == FT_REG || (write_devices && stype == FT_DEVICE))) {
if (stream_source) {
rprintf(FERROR, "--stream-source will not replace a non-regular destination: %s\n",
full_fname(fname));
exit_cleanup(RERR_FILEIO);
}
if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_FILE) != 0)
goto cleanup;
statret = -1;
Expand Down Expand Up @@ -2628,6 +2634,8 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
goto notify_others;
rsyserr(FERROR_XFER, stat_errno, "recv_generator: failed to stat %s",
full_fname(fname));
if (stream_source)
exit_cleanup(RERR_FILEIO);
goto cleanup;
}

Expand Down Expand Up @@ -2706,6 +2714,15 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
}

/* open the file */
#ifdef SUPPORT_STREAM_SOURCE
if (stream_source) {
fd = open_stream_basis(fnamecmp, &sx.st);
if (fd < 0) {
rsyserr(FERROR, errno, "cannot open regular FIFO transfer basis %s", full_fname(fnamecmp));
exit_cleanup(RERR_FILEIO);
}
} else
#endif
if (fd < 0 && (fd = do_open_checklinks(fnamecmp)) < 0) {
rsyserr(FERROR, errno, "failed to open %s, continuing",
full_fname(fnamecmp));
Expand Down
21 changes: 21 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ extern unsigned int curr_dir_len;
extern int module_id;
extern int rsync_port;
extern int whole_file;
extern int stream_source;
extern struct name_num_item *xfer_sum_nni;
extern int read_batch;
extern int write_batch;
extern int batch_fd;
Expand Down Expand Up @@ -831,6 +833,11 @@ static char *get_local_name(struct file_list *flist, char *dest_path)
* of a single non-directory or the user has requested one via a
* destination path ending in a slash, create one and use mode 1. */
if (file_total > 1 || trailing_slash) {
if (stream_source) {
rprintf(FERROR, "--stream-source requires an existing destination directory: %s\n",
full_fname(dest_path));
exit_cleanup(RERR_FILESELECT);
}
if (trailing_slash)
*cp = '\0'; /* Lop off the final slash (if any). */

Expand Down Expand Up @@ -1401,6 +1408,10 @@ int client_run(int f_in, int f_out, pid_t pid, int argc, char *argv[])

io_set_sock_fds(f_in, f_out);
setup_protocol(f_out,f_in);
if (stream_source && xfer_sum_nni->num == CSUM_NONE) {
rprintf(FERROR, "--stream-source requires a transfer checksum\n");
exit_cleanup(RERR_SYNTAX);
}

/* We set our stderr file handle to blocking because ssh might have
* set it to non-blocking. This can be particularly troublesome if
Expand Down Expand Up @@ -1521,6 +1532,11 @@ static int start_client(int argc, char *argv[])
int ret;
pid_t pid;

if (stream_source && (!argv[0][0] || !argv[1][0])) {
rprintf(FERROR, "--stream-source requires non-empty source and destination arguments\n");
exit_cleanup(RERR_SYNTAX);
}

if (!read_batch) { /* for read_batch, NO source is specified */
char *path = check_for_hostspec(argv[0], &shell_machine, &rsync_port);
if (path) { /* source is remote */
Expand Down Expand Up @@ -1595,6 +1611,11 @@ static int start_client(int argc, char *argv[])
rsync_port = 0;
}

if (stream_source && (!local_server || !am_sender)) {
rprintf(FERROR, "--stream-source requires a local source and destination\n");
exit_cleanup(RERR_SYNTAX);
}

if (placement_log && am_sender && !local_server) {
rprintf(FERROR, "--placement-log requires a local receiver\n");
exit_cleanup(RERR_SYNTAX);
Expand Down
21 changes: 20 additions & 1 deletion match.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ static void matched(int f, struct sum_struct *s, struct map_struct *buf, OFF_T o
{
int32 n = (int32)(offset - last_match); /* max value: block_size (int32) */
int32 j;
int32 token = i;
#ifdef SUPPORT_STREAM_SOURCE
BOOL streaming = buf && buf->stream_timeout;

if (streaming && i == -1)
token = -2;
#endif

if (DEBUG_GTE(DELTASUM, 2) && i >= 0) {
rprintf(FINFO,
Expand All @@ -137,7 +144,7 @@ static void matched(int f, struct sum_struct *s, struct map_struct *buf, OFF_T o
(long)s->sums[i].len, (long)n);
}

send_token(f, i, buf, last_match, n, i < 0 ? 0 : s->sums[i].len);
send_token(f, token, buf, last_match, n, i < 0 ? 0 : s->sums[i].len);
data_transfer += n;

if (i >= 0) {
Expand All @@ -155,6 +162,18 @@ static void matched(int f, struct sum_struct *s, struct map_struct *buf, OFF_T o
else
last_match = offset;

#ifdef SUPPORT_STREAM_SOURCE
if (streaming) {
/* hash_search may inspect the byte at the end of the last match. */
buf->stream_keep = last_match ? last_match - 1 : 0;
if (i == -1) {
check_stream_eof(buf);
send_token(f, -1, buf, offset, 0, 0);
} else if (last_match == buf->file_size)
return; /* Completion is reported only after checking FIFO EOF. */
}
#endif

if (buf && INFO_GTE(PROGRESS, 1))
show_progress(last_match, buf->file_size);
}
Expand Down
Loading
Loading