librsync  2.3.0
stream.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
8  * modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License 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  | Programming languages should be designed not
24  | by piling feature on top of feature, but by
25  | removing the weaknesses and restrictions that
26  | make additional features appear necessary.
27  | -- Revised^5 Report on Scheme
28  */
29 
30 /** \file stream.c
31  * Manage librsync streams of IO.
32  *
33  * See \sa scoop.c and \sa tube.c for related code for input and output
34  * respectively.
35  *
36  * OK, so I'll admit IO here is a little complex. The most important player
37  * here is the stream, which is an object for managing filter operations. It
38  * has both input and output sides, both of which is just a (pointer,len) pair
39  * into a buffer provided by the client. The code controlling the stream
40  * handles however much data it wants, and the client provides or accepts
41  * however much is convenient.
42  *
43  * At the same time as being friendly to the client, we also try to be very
44  * friendly to the internal code. It wants to be able to ask for arbitrary
45  * amounts of input or output and get it without having to keep track of
46  * partial completion. So there are functions which either complete, or queue
47  * whatever was not sent and return RS_BLOCKED.
48  *
49  * The output buffer is a little more clever than simply a data buffer. Instead
50  * it knows that we can send either literal data, or data copied through from
51  * the input of the stream.
52  *
53  * In buf.c you will find functions that then map buffers onto stdio files.
54  *
55  * So on return from an encoding function, either the input or the output or
56  * possibly both will have no more bytes available.
57  *
58  * librsync never does IO or memory allocation, but relies on the caller. This
59  * is very nice for integration, but means that we have to be fairly flexible
60  * as to when we can `read' or `write' stuff internally.
61  *
62  * librsync basically does two types of IO. It reads network integers of
63  * various lengths which encode command and control information such as
64  * versions and signatures. It also does bulk data transfer.
65  *
66  * IO of network integers is internally buffered, because higher levels of the
67  * code need to see them transmitted atomically: it's no good to read half of a
68  * uint32. So there is a small and fixed length internal buffer which
69  * accumulates these. Unlike previous versions of the library, we don't require
70  * that the caller hold the start until the whole thing has arrived, which
71  * guarantees that we can always make progress.
72  *
73  * On each call into a stream iterator, it should begin by trying to flush
74  * output. This may well use up all the remaining stream space, in which case
75  * nothing else can be done.
76  *
77  * \todo Kill this file and move the vestigial code remaining closer to where
78  * it's used. */
79 
80 #include "config.h"
81 #include <assert.h>
82 #include <stdlib.h>
83 #include <string.h>
84 #include "librsync.h"
85 #include "stream.h"
86 #include "trace.h"
87 
88 /** Copy up to \p max_len bytes from input of \b stream to its output.
89  *
90  * \return the number of bytes actually copied, which may be less than LEN if
91  * there is not enough space in one or the other stream.
92  *
93  * This always does the copy immediately. Most functions should call
94  * rs_tube_copy() to cause the copy to happen gradually as space becomes
95  * available. */
96 int rs_buffers_copy(rs_buffers_t *stream, int max_len)
97 {
98  int len = max_len;
99 
100  assert(len > 0);
101 
102  if ((unsigned)len > stream->avail_in) {
103  rs_trace("copy limited to " FMT_SIZE " available input bytes",
104  stream->avail_in);
105  len = stream->avail_in;
106  }
107 
108  if ((unsigned)len > stream->avail_out) {
109  rs_trace("copy limited to " FMT_SIZE " available output bytes",
110  stream->avail_out);
111  len = stream->avail_out;
112  }
113 
114  if (!len)
115  return 0;
116  /* rs_trace("stream copied chunk of %d bytes", len); */
117 
118  memcpy(stream->next_out, stream->next_in, len);
119 
120  stream->next_out += len;
121  stream->avail_out -= len;
122 
123  stream->next_in += len;
124  stream->avail_in -= len;
125 
126  return len;
127 }
128 
129 /** Assert input is empty or output is full.
130  *
131  * Whenever a stream processing function exits, it should have done so because
132  * it has either consumed all the input or has filled the output buffer. This
133  * function checks that simple postcondition. */
135 {
136  assert(stream->avail_in == 0 || stream->avail_out == 0);
137 }
rs_buffers_s::avail_in
size_t avail_in
Number of bytes available at next_in.
Definition: librsync.h:336
rs_buffers_copy
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
trace.h
librsync.h
rs_buffers_s::next_in
char * next_in
Next input byte.
Definition: librsync.h:328
rs_buffers_check_exit
void rs_buffers_check_exit(rs_buffers_t const *stream)
Assert input is empty or output is full.
Definition: stream.c:134
rs_buffers_s::next_out
char * next_out
Next output byte should be put there.
Definition: librsync.h:345
rs_buffers_s::avail_out
size_t avail_out
Remaining free space at next_out.
Definition: librsync.h:351
rs_buffers_s
Description of input and output buffers.
Definition: librsync.h:322