librsync  2.3.4
tube.c
Go to the documentation of this file.
1/*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 *
3 * librsync -- dynamic caching and delta update in HTTP
4 *
5 * Copyright (C) 2000, 2001 by Martin Pool <mbp@sourcefrog.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 /*=
23 | Where a calculator on the ENIAC is
24 | equpped with 18,000 vaccuum tubes and
25 | weighs 30 tons, computers in the
26 | future may have only 1,000 vaccuum
27 | tubes and perhaps weigh 1 1/2
28 | tons.
29 | -- Popular Mechanics, March 1949
30 */
31
32/** \file tube.c
33 * A somewhat elastic but fairly small buffer for data passing through a
34 * stream.
35 *
36 * In most cases the iter can adjust to send just as much data will fit. In
37 * some cases that would be too complicated, because it has to transmit an
38 * integer or something similar. So in that case we stick whatever won't fit
39 * into a small buffer.
40 *
41 * A tube can contain some literal data to go out (typically command bytes),
42 * and also an instruction to copy data from the stream's input or from some
43 * other location. Both literal data and a copy command can be queued at the
44 * same time, but only in that order and at most one of each.
45 *
46 * \todo As an optimization, write it directly to the stream if possible. But
47 * for simplicity don't do that yet.
48 *
49 * \todo I think our current copy code will lock up if the application only
50 * ever calls us with either input or output buffers, and not both. So I guess
51 * in that case we might need to copy into some temporary buffer space, and
52 * then back out again later. */
53
54#include "config.h" /* IWYU pragma: keep */
55#include <assert.h>
56#include <stdlib.h>
57#include <string.h>
58#include "librsync.h"
59#include "job.h"
60#include "scoop.h"
61#include "trace.h"
62
63static void rs_tube_catchup_write(rs_job_t *job)
64{
65 rs_buffers_t *stream = job->stream;
66 size_t len = job->write_len;
67
68 assert(len > 0);
69 if (len > stream->avail_out)
70 len = stream->avail_out;
71 if (len) {
72 memcpy(stream->next_out, job->write_buf, len);
73 stream->next_out += len;
74 stream->avail_out -= len;
75 job->write_len -= len;
76 if (job->write_len > 0)
77 /* Still something left in the tube, shuffle it to the front. */
78 memmove(job->write_buf, job->write_buf + len, job->write_len);
79 }
80 rs_trace("wrote " FMT_SIZE " bytes from tube, " FMT_SIZE " left to write",
81 len, job->write_len);
82}
83
84/** Catch up on an outstanding copy command.
85 *
86 * Takes data from the scoop and writes as much as will fit to the output, up
87 * to the limit of the outstanding copy. */
89{
90 assert(job->write_len == 0);
91 assert(job->copy_len > 0);
92 rs_buffers_t *stream = job->stream;
93 size_t copy_len = job->copy_len;
94 size_t avail_in = rs_scoop_avail(job);
95 size_t avail_out = stream->avail_out;
96 size_t len, ilen;
97 void *next;
98
99 if (copy_len > avail_in)
100 copy_len = avail_in;
101 if (copy_len > avail_out)
102 copy_len = avail_out;
103 len = copy_len;
104 for (next = rs_scoop_iterbuf(job, &len, &ilen); ilen > 0;
105 next = rs_scoop_nextbuf(job, &len, &ilen)) {
106 memcpy(stream->next_out, next, ilen);
107 stream->next_out += ilen;
108 stream->avail_out -= ilen;
109 job->copy_len -= ilen;
110 }
111 rs_trace("copied " FMT_SIZE " bytes from scoop, " FMT_SIZE
112 " left in scoop, " FMT_SIZE " left to copy", copy_len,
113 rs_scoop_avail(job), job->copy_len);
114}
115
116/** Put whatever will fit from the tube into the output of the stream.
117 *
118 * \return RS_DONE if the tube is now empty and ready to accept another
119 * command, RS_BLOCKED if there is still stuff waiting to go out. */
121{
122 if (job->write_len) {
123 rs_tube_catchup_write(job);
124 if (job->write_len)
125 return RS_BLOCKED;
126 }
127
128 if (job->copy_len) {
130 if (job->copy_len) {
131 if (rs_scoop_eof(job)) {
132 rs_error("reached end of file while copying data");
133 return RS_INPUT_ENDED;
134 }
135 return RS_BLOCKED;
136 }
137 }
138 return RS_DONE;
139}
140
141/* Check whether there is data in the tube waiting to go out.
142
143 \return true if the previous command has finished doing all its output. */
144int rs_tube_is_idle(rs_job_t const *job)
145{
146 return job->write_len == 0 && job->copy_len == 0;
147}
148
149/** Queue up a request to copy through \p len bytes from the input to the
150 * output of the stream.
151 *
152 * The data is copied from the scoop (if there is anything there) or from the
153 * input, on the next call to rs_tube_write().
154 *
155 * We can only accept this request if there is no copy command already pending.
156 *
157 * \todo Try to do the copy immediately, and return a result. Then, people can
158 * try to continue if possible. Is this really required? Callers can just go
159 * out and back in again after flushing the tube. */
160void rs_tube_copy(rs_job_t *job, size_t len)
161{
162 assert(job->copy_len == 0);
163
164 job->copy_len = len;
165}
166
167/** Push some data into the tube for storage.
168 *
169 * The tube's never supposed to get very big, so this will just pop loudly if
170 * you do that.
171 *
172 * We can't accept write data if there's already a copy command in the tube,
173 * because the write data comes out first. */
174void rs_tube_write(rs_job_t *job, const void *buf, size_t len)
175{
176 assert(job->copy_len == 0);
177 assert(len <= sizeof(job->write_buf) - job->write_len);
178
179 memcpy(job->write_buf + job->write_len, buf, len);
180 job->write_len += len;
181}
Generic state-machine interface.
Public header for librsync.
rs_result
Return codes from nonblocking rsync operations.
Definition: librsync.h:180
@ RS_DONE
Completed successfully.
Definition: librsync.h:181
@ RS_INPUT_ENDED
Unexpected end of input file, perhaps due to a truncated file or dropped network connection.
Definition: librsync.h:190
@ RS_BLOCKED
Blocked waiting for more data.
Definition: librsync.h:182
Manage librsync streams of IO.
static bool rs_scoop_eof(rs_job_t *job)
Test if the scoop has reached eof.
Definition: scoop.h:100
static void * rs_scoop_iterbuf(rs_job_t *job, size_t *len, size_t *ilen)
Iterate through and consume contiguous data buffers in the scoop.
Definition: scoop.h:165
static void * rs_scoop_nextbuf(rs_job_t *job, size_t *len, size_t *ilen)
Get the next iteration of contiguous data buffers from the scoop.
Definition: scoop.h:175
Description of input and output buffers.
Definition: librsync.h:328
size_t avail_out
Remaining free space at next_out.
Definition: librsync.h:357
char * next_out
Next output byte should be put there.
Definition: librsync.h:351
The contents of this structure are private.
Definition: job.h:47
size_t copy_len
If copy_len is >0, then that much data should be copied through from the input.
Definition: job.h:114
rs_byte_t write_buf[36]
If USED is >0, then buf contains that much write data to be sent out.
Definition: job.h:109
logging functions.
void rs_tube_write(rs_job_t *job, const void *buf, size_t len)
Push some data into the tube for storage.
Definition: tube.c:174
static void rs_tube_catchup_copy(rs_job_t *job)
Catch up on an outstanding copy command.
Definition: tube.c:88
rs_result rs_tube_catchup(rs_job_t *job)
Put whatever will fit from the tube into the output of the stream.
Definition: tube.c:120
void rs_tube_copy(rs_job_t *job, size_t len)
Queue up a request to copy through len bytes from the input to the output of the stream.
Definition: tube.c:160