librsync  2.0.2
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 A somewhat elastic but fairly small buffer for data passing
33  * through a stream.
34  *
35  * In most cases the iter can adjust to send just as much data will fit. In
36  * some cases that would be too complicated, because it has to transmit an
37  * integer or something similar. So in that case we stick whatever won't fit
38  * into a small buffer.
39  *
40  * A tube can contain some literal data to go out (typically command bytes),
41  * and also an instruction to copy data from the stream's input or from some
42  * other location. Both literal data and a copy command can be queued at the
43  * same time, but only in that order and at most one of each.
44  *
45  * \todo As an optimization, write it directly to the stream if possible. But
46  * for simplicity don't do that yet.
47  *
48  * \todo I think our current copy code will lock up if the application only
49  * ever calls us with either input or output buffers, and not both. So I guess
50  * in that case we might need to copy into some temporary buffer space, and
51  * then back out again later. */
52 
53 #include "config.h"
54 
55 #include <assert.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <stdio.h>
59 
60 #include "librsync.h"
61 #include "trace.h"
62 #include "util.h"
63 #include "job.h"
64 #include "stream.h"
65 
66 static void rs_tube_catchup_write(rs_job_t *job)
67 {
68  rs_buffers_t *stream = job->stream;
69  int len, remain;
70 
71  len = job->write_len;
72  assert(len > 0);
73 
74  assert(len > 0);
75  if ((size_t)len > stream->avail_out)
76  len = stream->avail_out;
77 
78  if (!stream->avail_out) {
79  rs_trace("no output space available");
80  return;
81  }
82 
83  memcpy(stream->next_out, job->write_buf, len);
84  stream->next_out += len;
85  stream->avail_out -= len;
86 
87  remain = job->write_len - len;
88  rs_trace("transmitted %d write bytes from tube, %d remain to be sent", len,
89  remain);
90 
91  if (remain > 0) {
92  /* Still something left in the tube... */
93  memmove(job->write_buf, job->write_buf + len, remain);
94  } else {
95  assert(remain == 0);
96  }
97 
98  job->write_len = remain;
99 }
100 
101 /** Execute a copy command, taking data from the scoop.
102  *
103  * \sa rs_tube_catchup_copy() */
105 {
106  size_t this_len;
107  rs_buffers_t *stream = job->stream;
108 
109  this_len =
110  job->copy_len < job->scoop_avail ? job->copy_len : job->scoop_avail;
111  if (this_len > stream->avail_out) {
112  this_len = stream->avail_out;
113  }
114  memcpy(stream->next_out, job->scoop_next, this_len);
115 
116  stream->next_out += this_len;
117  stream->avail_out -= this_len;
118 
119  job->scoop_avail -= this_len;
120  job->scoop_next += this_len;
121 
122  job->copy_len -= this_len;
123 
124  rs_trace("caught up on " FMT_SIZE " copied bytes from scoop, " FMT_SIZE
125  " remain there, " FMT_LONG " remain to be copied", this_len,
126  job->scoop_avail, job->copy_len);
127 }
128 
129 /** Catch up on an outstanding copy command.
130  *
131  * Takes data from the scoop, and the input (in that order), and writes as much
132  * as will fit to the output, up to the limit of the outstanding copy. */
134 {
135  assert(job->write_len == 0);
136  assert(job->copy_len > 0);
137 
138  /* If there's data in the scoop, send that first. */
139  if (job->scoop_avail && job->copy_len) {
141  }
142  /* If there's more to copy and we emptied the scoop, send input. */
143  if (job->copy_len && !job->scoop_avail) {
144  size_t this_copy = rs_buffers_copy(job->stream, job->copy_len);
145  job->copy_len -= this_copy;
146  rs_trace("copied " FMT_SIZE " bytes from input buffer, " FMT_LONG
147  " remain to be copied", this_copy, job->copy_len);
148  }
149 }
150 
151 /** Put whatever will fit from the tube into the output of the stream.
152  *
153  * \return RS_DONE if the tube is now empty and ready to accept another
154  * command, RS_BLOCKED if there is still stuff waiting to go out. */
156 {
157  if (job->write_len) {
158  rs_tube_catchup_write(job);
159  if (job->write_len)
160  return RS_BLOCKED;
161  }
162 
163  if (job->copy_len) {
165  if (job->copy_len) {
166  if (job->stream->eof_in && !job->stream->avail_in
167  && !job->scoop_avail) {
168  rs_error
169  ("reached end of file while copying literal data through buffers");
170  return RS_INPUT_ENDED;
171  }
172  return RS_BLOCKED;
173  }
174  }
175  return RS_DONE;
176 }
177 
178 /* Check whether there is data in the tube waiting to go out.
179 
180  \return true if the previous command has finished doing all its output. */
181 int rs_tube_is_idle(rs_job_t const *job)
182 {
183  return job->write_len == 0 && job->copy_len == 0;
184 }
185 
186 /** Queue up a request to copy through \p len bytes from the input to the
187  * output of the stream.
188  *
189  * The data is copied from the scoop (if there is anything there) or from the
190  * input, on the next call to rs_tube_write().
191  *
192  * We can only accept this request if there is no copy command already pending.
193  *
194  * \todo Try to do the copy immediately, and return a result. Then, people can
195  * try to continue if possible. Is this really required? Callers can just go
196  * out and back in again after flushing the tube. */
197 void rs_tube_copy(rs_job_t *job, int len)
198 {
199  assert(job->copy_len == 0);
200 
201  job->copy_len = len;
202 }
203 
204 /** Push some data into the tube for storage.
205  *
206  * The tube's never supposed to get very big, so this will just pop loudly if
207  * you do that.
208  *
209  * We can't accept write data if there's already a copy command in the tube,
210  * because the write data comes out first. */
211 void rs_tube_write(rs_job_t *job, const void *buf, size_t len)
212 {
213  assert(job->copy_len == 0);
214 
215  if (len > sizeof(job->write_buf) - job->write_len) {
216  rs_fatal("tube popped when trying to write " FMT_SIZE " bytes!", len);
217  }
218 
219  memcpy(job->write_buf + job->write_len, buf, len);
220  job->write_len += len;
221 }
Description of input and output buffers.
Definition: librsync.h:300
logging functions.
rs_long_t copy_len
If copy_len is >0, then that much data should be copied through from the input.
Definition: job.h:89
rs_byte_t write_buf[36]
If USED is >0, then buf contains that much write data to be sent out.
Definition: job.h:84
size_t avail_out
Remaining free space at next_out.
Definition: librsync.h:329
static void rs_tube_catchup_copy(rs_job_t *job)
Catch up on an outstanding copy command.
Definition: tube.c:133
char * next_out
Next output byte should be put there.
Definition: librsync.h:323
size_t avail_in
Number of bytes available at next_in.
Definition: librsync.h:314
Public header for librsync.
static void rs_tube_copy_from_scoop(rs_job_t *job)
Execute a copy command, taking data from the scoop.
Definition: tube.c:104
void rs_tube_copy(rs_job_t *job, int len)
Queue up a request to copy through len bytes from the input to the output of the stream.
Definition: tube.c:197
int rs_tube_catchup(rs_job_t *job)
Put whatever will fit from the tube into the output of the stream.
Definition: tube.c:155
Unexpected end of input file, perhaps due to a truncated file or dropped network connection.
Definition: librsync.h:171
Blocked waiting for more data.
Definition: librsync.h:163
int rs_buffers_copy(rs_buffers_t *stream, int max_len)
Copy up to max_len bytes from input of stream to its output.
Definition: stream.c:99
int eof_in
True if there is no more data after this.
Definition: librsync.h:317
Completed successfully.
Definition: librsync.h:162
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:211
of this structure are private.
Definition: job.h:26