librsync  2.3.1
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"
55 #include <assert.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include "librsync.h"
59 #include "job.h"
60 #include "stream.h"
61 #include "trace.h"
62 
63 static void rs_tube_catchup_write(rs_job_t *job)
64 {
65  rs_buffers_t *stream = job->stream;
66  int len = job->write_len;
67 
68  assert(len > 0);
69  if ((size_t)len > stream->avail_out)
70  len = stream->avail_out;
71  if (!stream->avail_out) {
72  rs_trace("no output space available");
73  return;
74  }
75  memcpy(stream->next_out, job->write_buf, len);
76  stream->next_out += len;
77  stream->avail_out -= len;
78  job->write_len -= len;
79  if (job->write_len > 0) {
80  /* Still something left in the tube, shuffle it to the front. */
81  memmove(job->write_buf, job->write_buf + len, job->write_len);
82  }
83  rs_trace("wrote %d bytes from tube, %d remaining", len, job->write_len);
84 }
85 
86 /** Execute a copy command, taking data from the scoop.
87  *
88  * \sa rs_tube_catchup_copy() */
90 {
91  rs_buffers_t *stream = job->stream;
92  size_t len = job->copy_len;
93 
94  assert(len > 0);
95  if (len > job->scoop_avail)
96  len = job->scoop_avail;
97  if (len > stream->avail_out)
98  len = stream->avail_out;
99  memcpy(stream->next_out, job->scoop_next, len);
100  stream->next_out += len;
101  stream->avail_out -= len;
102  job->scoop_avail -= len;
103  job->scoop_next += len;
104  job->copy_len -= len;
105  rs_trace("caught up on " FMT_SIZE " copied bytes from scoop, " FMT_SIZE
106  " remain there, " FMT_LONG " remain to be copied", len,
107  job->scoop_avail, job->copy_len);
108 }
109 
110 /** Catch up on an outstanding copy command.
111  *
112  * Takes data from the scoop, and the input (in that order), and writes as much
113  * as will fit to the output, up to the limit of the outstanding copy. */
115 {
116  assert(job->write_len == 0);
117  assert(job->copy_len > 0);
118 
119  /* If there's data in the scoop, send that first. */
120  if (job->scoop_avail && job->copy_len) {
122  }
123  /* If there's more to copy and we emptied the scoop, send input. */
124  if (job->copy_len && !job->scoop_avail) {
125  size_t this_copy = rs_buffers_copy(job->stream, job->copy_len);
126  job->copy_len -= this_copy;
127  rs_trace("copied " FMT_SIZE " bytes from input buffer, " FMT_LONG
128  " remain to be copied", this_copy, job->copy_len);
129  }
130 }
131 
132 /** Put whatever will fit from the tube into the output of the stream.
133  *
134  * \return RS_DONE if the tube is now empty and ready to accept another
135  * command, RS_BLOCKED if there is still stuff waiting to go out. */
137 {
138  if (job->write_len) {
139  rs_tube_catchup_write(job);
140  if (job->write_len)
141  return RS_BLOCKED;
142  }
143 
144  if (job->copy_len) {
146  if (job->copy_len) {
147  if (job->stream->eof_in && !job->stream->avail_in
148  && !job->scoop_avail) {
149  rs_error
150  ("reached end of file while copying literal data through buffers");
151  return RS_INPUT_ENDED;
152  }
153  return RS_BLOCKED;
154  }
155  }
156  return RS_DONE;
157 }
158 
159 /* Check whether there is data in the tube waiting to go out.
160 
161  \return true if the previous command has finished doing all its output. */
162 int rs_tube_is_idle(rs_job_t const *job)
163 {
164  return job->write_len == 0 && job->copy_len == 0;
165 }
166 
167 /** Queue up a request to copy through \p len bytes from the input to the
168  * output of the stream.
169  *
170  * The data is copied from the scoop (if there is anything there) or from the
171  * input, on the next call to rs_tube_write().
172  *
173  * We can only accept this request if there is no copy command already pending.
174  *
175  * \todo Try to do the copy immediately, and return a result. Then, people can
176  * try to continue if possible. Is this really required? Callers can just go
177  * out and back in again after flushing the tube. */
178 void rs_tube_copy(rs_job_t *job, int len)
179 {
180  assert(job->copy_len == 0);
181 
182  job->copy_len = len;
183 }
184 
185 /** Push some data into the tube for storage.
186  *
187  * The tube's never supposed to get very big, so this will just pop loudly if
188  * you do that.
189  *
190  * We can't accept write data if there's already a copy command in the tube,
191  * because the write data comes out first. */
192 void rs_tube_write(rs_job_t *job, const void *buf, size_t len)
193 {
194  assert(job->copy_len == 0);
195  assert(len <= sizeof(job->write_buf) - job->write_len);
196 
197  memcpy(job->write_buf + job->write_len, buf, len);
198  job->write_len += len;
199 }
Description of input and output buffers.
Definition: librsync.h:322
logging functions.
rs_long_t copy_len
If copy_len is &gt;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 &gt;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:351
static void rs_tube_catchup_copy(rs_job_t *job)
Catch up on an outstanding copy command.
Definition: tube.c:114
char * next_out
Next output byte should be put there.
Definition: librsync.h:345
size_t avail_in
Number of bytes available at next_in.
Definition: librsync.h:336
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:89
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:178
int rs_tube_catchup(rs_job_t *job)
Put whatever will fit from the tube into the output of the stream.
Definition: tube.c:136
Unexpected end of input file, perhaps due to a truncated file or dropped network connection.
Definition: librsync.h:190
Blocked waiting for more data.
Definition: librsync.h:182
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:96
int eof_in
True if there is no more data after this.
Definition: librsync.h:339
Completed successfully.
Definition: librsync.h:181
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:192
The contents of this structure are private.
Definition: job.h:26