$treeview $search $mathjax $extrastylesheet
librsync
2.3.0
$projectbrief
|
$projectbrief
|
$searchbox |
00001 /*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- 00002 * 00003 * librsync -- dynamic caching and delta update in HTTP 00004 * 00005 * Copyright (C) 2000, 2001 by Martin Pool <mbp@sourcefrog.net> 00006 * 00007 * This program is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public License 00009 * as published by the Free Software Foundation; either version 2.1 of 00010 * the License, or (at your option) any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, but 00013 * WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00020 */ 00021 00022 /*= 00023 | Programming languages should be designed not 00024 | by piling feature on top of feature, but by 00025 | removing the weaknesses and restrictions that 00026 | make additional features appear necessary. 00027 | -- Revised^5 Report on Scheme 00028 */ 00029 00030 /** \file stream.c 00031 * Manage librsync streams of IO. 00032 * 00033 * See \sa scoop.c and \sa tube.c for related code for input and output 00034 * respectively. 00035 * 00036 * OK, so I'll admit IO here is a little complex. The most important player 00037 * here is the stream, which is an object for managing filter operations. It 00038 * has both input and output sides, both of which is just a (pointer,len) pair 00039 * into a buffer provided by the client. The code controlling the stream 00040 * handles however much data it wants, and the client provides or accepts 00041 * however much is convenient. 00042 * 00043 * At the same time as being friendly to the client, we also try to be very 00044 * friendly to the internal code. It wants to be able to ask for arbitrary 00045 * amounts of input or output and get it without having to keep track of 00046 * partial completion. So there are functions which either complete, or queue 00047 * whatever was not sent and return RS_BLOCKED. 00048 * 00049 * The output buffer is a little more clever than simply a data buffer. Instead 00050 * it knows that we can send either literal data, or data copied through from 00051 * the input of the stream. 00052 * 00053 * In buf.c you will find functions that then map buffers onto stdio files. 00054 * 00055 * So on return from an encoding function, either the input or the output or 00056 * possibly both will have no more bytes available. 00057 * 00058 * librsync never does IO or memory allocation, but relies on the caller. This 00059 * is very nice for integration, but means that we have to be fairly flexible 00060 * as to when we can `read' or `write' stuff internally. 00061 * 00062 * librsync basically does two types of IO. It reads network integers of 00063 * various lengths which encode command and control information such as 00064 * versions and signatures. It also does bulk data transfer. 00065 * 00066 * IO of network integers is internally buffered, because higher levels of the 00067 * code need to see them transmitted atomically: it's no good to read half of a 00068 * uint32. So there is a small and fixed length internal buffer which 00069 * accumulates these. Unlike previous versions of the library, we don't require 00070 * that the caller hold the start until the whole thing has arrived, which 00071 * guarantees that we can always make progress. 00072 * 00073 * On each call into a stream iterator, it should begin by trying to flush 00074 * output. This may well use up all the remaining stream space, in which case 00075 * nothing else can be done. 00076 * 00077 * \todo Kill this file and move the vestigial code remaining closer to where 00078 * it's used. */ 00079 00080 #include "config.h" 00081 #include <assert.h> 00082 #include <stdlib.h> 00083 #include <string.h> 00084 #include "librsync.h" 00085 #include "stream.h" 00086 #include "trace.h" 00087 00088 /** Copy up to \p max_len bytes from input of \b stream to its output. 00089 * 00090 * \return the number of bytes actually copied, which may be less than LEN if 00091 * there is not enough space in one or the other stream. 00092 * 00093 * This always does the copy immediately. Most functions should call 00094 * rs_tube_copy() to cause the copy to happen gradually as space becomes 00095 * available. */ 00096 int rs_buffers_copy(rs_buffers_t *stream, int max_len) 00097 { 00098 int len = max_len; 00099 00100 assert(len > 0); 00101 00102 if ((unsigned)len > stream->avail_in) { 00103 rs_trace("copy limited to " FMT_SIZE " available input bytes", 00104 stream->avail_in); 00105 len = stream->avail_in; 00106 } 00107 00108 if ((unsigned)len > stream->avail_out) { 00109 rs_trace("copy limited to " FMT_SIZE " available output bytes", 00110 stream->avail_out); 00111 len = stream->avail_out; 00112 } 00113 00114 if (!len) 00115 return 0; 00116 /* rs_trace("stream copied chunk of %d bytes", len); */ 00117 00118 memcpy(stream->next_out, stream->next_in, len); 00119 00120 stream->next_out += len; 00121 stream->avail_out -= len; 00122 00123 stream->next_in += len; 00124 stream->avail_in -= len; 00125 00126 return len; 00127 } 00128 00129 /** Assert input is empty or output is full. 00130 * 00131 * Whenever a stream processing function exits, it should have done so because 00132 * it has either consumed all the input or has filled the output buffer. This 00133 * function checks that simple postcondition. */ 00134 void rs_buffers_check_exit(rs_buffers_t const *stream) 00135 { 00136 assert(stream->avail_in == 0 || stream->avail_out == 0); 00137 }