librsync  2.3.3
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 <assert.h>
55#include <stdlib.h>
56#include <string.h>
57#include "librsync.h"
58#include "job.h"
59#include "scoop.h"
60#include "trace.h"
61
62static void rs_tube_catchup_write(rs_job_t *job)
63{
64 rs_buffers_t *stream = job->stream;
65 size_t len = job->write_len;
66
67 assert(len > 0);
68 if (len > stream->avail_out)
69 len = stream->avail_out;
70 if (len) {
71 memcpy(stream->next_out, job->write_buf, len);
72 stream->next_out += len;
73 stream->avail_out -= len;
74 job->write_len -= len;
75 if (job->write_len > 0)
76 /* Still something left in the tube, shuffle it to the front. */
77 memmove(job->write_buf, job->write_buf + len, job->write_len);
78 }
79 rs_trace("wrote " FMT_SIZE " bytes from tube, " FMT_SIZE " left to write",
80 len, job->write_len);
81}
82
83/** Catch up on an outstanding copy command.
84 *
85 * Takes data from the scoop and writes as much as will fit to the output, up
86 * to the limit of the outstanding copy. */
88{
89 assert(job->write_len == 0);
90 assert(job->copy_len > 0);
91 rs_buffers_t *stream = job->stream;
92 size_t copy_len = job->copy_len;
93 size_t avail_in = rs_scoop_avail(job);
94 size_t avail_out = stream->avail_out;
95 size_t len, ilen;
96 void *next;
97
98 if (copy_len > avail_in)
99 copy_len = avail_in;
100 if (copy_len > avail_out)
101 copy_len = avail_out;
102 len = copy_len;
103 for (next = rs_scoop_iterbuf(job, &len, &ilen); ilen > 0;
104 next = rs_scoop_nextbuf(job, &len, &ilen)) {
105 memcpy(stream->next_out, next, ilen);
106 stream->next_out += ilen;
107 stream->avail_out -= ilen;
108 job->copy_len -= ilen;
109 }
110 rs_trace("copied " FMT_SIZE " bytes from scoop, " FMT_SIZE
111 " left in scoop, " FMT_SIZE " left to copy", copy_len,
112 rs_scoop_avail(job), job->copy_len);
113}
114
115/** Put whatever will fit from the tube into the output of the stream.
116 *
117 * \return RS_DONE if the tube is now empty and ready to accept another
118 * command, RS_BLOCKED if there is still stuff waiting to go out. */
120{
121 if (job->write_len) {
122 rs_tube_catchup_write(job);
123 if (job->write_len)
124 return RS_BLOCKED;
125 }
126
127 if (job->copy_len) {
129 if (job->copy_len) {
130 if (rs_scoop_eof(job)) {
131 rs_error("reached end of file while copying data");
132 return RS_INPUT_ENDED;
133 }
134 return RS_BLOCKED;
135 }
136 }
137 return RS_DONE;
138}
139
140/* Check whether there is data in the tube waiting to go out.
141
142 \return true if the previous command has finished doing all its output. */
143int rs_tube_is_idle(rs_job_t const *job)
144{
145 return job->write_len == 0 && job->copy_len == 0;
146}
147
148/** Queue up a request to copy through \p len bytes from the input to the
149 * output of the stream.
150 *
151 * The data is copied from the scoop (if there is anything there) or from the
152 * input, on the next call to rs_tube_write().
153 *
154 * We can only accept this request if there is no copy command already pending.
155 *
156 * \todo Try to do the copy immediately, and return a result. Then, people can
157 * try to continue if possible. Is this really required? Callers can just go
158 * out and back in again after flushing the tube. */
159void rs_tube_copy(rs_job_t *job, size_t len)
160{
161 assert(job->copy_len == 0);
162
163 job->copy_len = len;
164}
165
166/** Push some data into the tube for storage.
167 *
168 * The tube's never supposed to get very big, so this will just pop loudly if
169 * you do that.
170 *
171 * We can't accept write data if there's already a copy command in the tube,
172 * because the write data comes out first. */
173void rs_tube_write(rs_job_t *job, const void *buf, size_t len)
174{
175 assert(job->copy_len == 0);
176 assert(len <= sizeof(job->write_buf) - job->write_len);
177
178 memcpy(job->write_buf + job->write_len, buf, len);
179 job->write_len += len;
180}
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:173
static void rs_tube_catchup_copy(rs_job_t *job)
Catch up on an outstanding copy command.
Definition: tube.c:87
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:119
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:159