$treeview $search $mathjax $extrastylesheet
librsync
2.0.2
$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 Manage librsync streams of IO. 00031 * 00032 * See \sa scoop.c and \sa tube.c for related code for input and output 00033 * respectively. 00034 * 00035 * OK, so I'll admit IO here is a little complex. The most important player 00036 * here is the stream, which is an object for managing filter operations. It 00037 * has both input and output sides, both of which is just a (pointer,len) pair 00038 * into a buffer provided by the client. The code controlling the stream 00039 * handles however much data it wants, and the client provides or accepts 00040 * however much is convenient. 00041 * 00042 * At the same time as being friendly to the client, we also try to be very 00043 * friendly to the internal code. It wants to be able to ask for arbitrary 00044 * amounts of input or output and get it without having to keep track of 00045 * partial completion. So there are functions which either complete, or queue 00046 * whatever was not sent and return RS_BLOCKED. 00047 * 00048 * The output buffer is a little more clever than simply a data buffer. Instead 00049 * it knows that we can send either literal data, or data copied through from 00050 * the input of the stream. 00051 * 00052 * In buf.c you will find functions that then map buffers onto stdio files. 00053 * 00054 * So on return from an encoding function, either the input or the output or 00055 * possibly both will have no more bytes available. 00056 * 00057 * librsync never does IO or memory allocation, but relies on the caller. This 00058 * is very nice for integration, but means that we have to be fairly flexible 00059 * as to when we can `read' or `write' stuff internally. 00060 * 00061 * librsync basically does two types of IO. It reads network integers of 00062 * various lengths which encode command and control information such as 00063 * versions and signatures. It also does bulk data transfer. 00064 * 00065 * IO of network integers is internally buffered, because higher levels of the 00066 * code need to see them transmitted atomically: it's no good to read half of a 00067 * uint32. So there is a small and fixed length internal buffer which 00068 * accumulates these. Unlike previous versions of the library, we don't require 00069 * that the caller hold the start until the whole thing has arrived, which 00070 * guarantees that we can always make progress. 00071 * 00072 * On each call into a stream iterator, it should begin by trying to flush 00073 * output. This may well use up all the remaining stream space, in which case 00074 * nothing else can be done. 00075 * 00076 * \todo Kill this file and move the vestigial code remaining closer to where 00077 * it's used. */ 00078 00079 #include "config.h" 00080 00081 #include <assert.h> 00082 #include <stdlib.h> 00083 #include <string.h> 00084 #include <stdio.h> 00085 00086 #include "librsync.h" 00087 #include "stream.h" 00088 #include "util.h" 00089 #include "trace.h" 00090 00091 /** Copy up to \p max_len bytes from input of \b stream to its output. 00092 * 00093 * \return the number of bytes actually copied, which may be less than LEN if 00094 * there is not enough space in one or the other stream. 00095 * 00096 * This always does the copy immediately. Most functions should call 00097 * rs_tube_copy() to cause the copy to happen gradually as space becomes 00098 * available. */ 00099 int rs_buffers_copy(rs_buffers_t *stream, int max_len) 00100 { 00101 int len = max_len; 00102 00103 assert(len > 0); 00104 00105 if ((unsigned)len > stream->avail_in) { 00106 rs_trace("copy limited to " FMT_SIZE " available input bytes", 00107 stream->avail_in); 00108 len = stream->avail_in; 00109 } 00110 00111 if ((unsigned)len > stream->avail_out) { 00112 rs_trace("copy limited to " FMT_SIZE " available output bytes", 00113 stream->avail_out); 00114 len = stream->avail_out; 00115 } 00116 00117 if (!len) 00118 return 0; 00119 /* rs_trace("stream copied chunk of %d bytes", len); */ 00120 00121 memcpy(stream->next_out, stream->next_in, len); 00122 00123 stream->next_out += len; 00124 stream->avail_out -= len; 00125 00126 stream->next_in += len; 00127 stream->avail_in -= len; 00128 00129 return len; 00130 } 00131 00132 /** Assert input is empty or output is full. 00133 * 00134 * Whenever a stream processing function exits, it should have done so because 00135 * it has either consumed all the input or has filled the output buffer. This 00136 * function checks that simple postcondition. */ 00137 void rs_buffers_check_exit(rs_buffers_t const *stream) 00138 { 00139 assert(stream->avail_in == 0 || stream->avail_out == 0); 00140 }