$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 modify 00008 * it under the terms of the GNU Lesser General Public License as published by 00009 * the Free Software Foundation; either version 2.1 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public License 00018 * 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 | Where a calculator on the ENIAC is 00024 | equpped with 18,000 vaccuum tubes and 00025 | weighs 30 tons, computers in the 00026 | future may have only 1,000 vaccuum 00027 | tubes and perhaps weigh 1 1/2 00028 | tons. 00029 | -- Popular Mechanics, March 1949 00030 */ 00031 00032 /** \file tube.c 00033 * A somewhat elastic but fairly small buffer for data passing through a 00034 * stream. 00035 * 00036 * In most cases the iter can adjust to send just as much data will fit. In 00037 * some cases that would be too complicated, because it has to transmit an 00038 * integer or something similar. So in that case we stick whatever won't fit 00039 * into a small buffer. 00040 * 00041 * A tube can contain some literal data to go out (typically command bytes), 00042 * and also an instruction to copy data from the stream's input or from some 00043 * other location. Both literal data and a copy command can be queued at the 00044 * same time, but only in that order and at most one of each. 00045 * 00046 * \todo As an optimization, write it directly to the stream if possible. But 00047 * for simplicity don't do that yet. 00048 * 00049 * \todo I think our current copy code will lock up if the application only 00050 * ever calls us with either input or output buffers, and not both. So I guess 00051 * in that case we might need to copy into some temporary buffer space, and 00052 * then back out again later. */ 00053 00054 #include "config.h" 00055 #include <assert.h> 00056 #include <stdlib.h> 00057 #include <string.h> 00058 #include "librsync.h" 00059 #include "job.h" 00060 #include "stream.h" 00061 #include "trace.h" 00062 00063 static void rs_tube_catchup_write(rs_job_t *job) 00064 { 00065 rs_buffers_t *stream = job->stream; 00066 int len = job->write_len; 00067 00068 assert(len > 0); 00069 if ((size_t)len > stream->avail_out) 00070 len = stream->avail_out; 00071 if (!stream->avail_out) { 00072 rs_trace("no output space available"); 00073 return; 00074 } 00075 memcpy(stream->next_out, job->write_buf, len); 00076 stream->next_out += len; 00077 stream->avail_out -= len; 00078 job->write_len -= len; 00079 if (job->write_len > 0) { 00080 /* Still something left in the tube, shuffle it to the front. */ 00081 memmove(job->write_buf, job->write_buf + len, job->write_len); 00082 } 00083 rs_trace("wrote %d bytes from tube, %d remaining", len, job->write_len); 00084 } 00085 00086 /** Execute a copy command, taking data from the scoop. 00087 * 00088 * \sa rs_tube_catchup_copy() */ 00089 static void rs_tube_copy_from_scoop(rs_job_t *job) 00090 { 00091 rs_buffers_t *stream = job->stream; 00092 size_t len = job->copy_len; 00093 00094 assert(len > 0); 00095 if (len > job->scoop_avail) 00096 len = job->scoop_avail; 00097 if (len > stream->avail_out) 00098 len = stream->avail_out; 00099 memcpy(stream->next_out, job->scoop_next, len); 00100 stream->next_out += len; 00101 stream->avail_out -= len; 00102 job->scoop_avail -= len; 00103 job->scoop_next += len; 00104 job->copy_len -= len; 00105 rs_trace("caught up on " FMT_SIZE " copied bytes from scoop, " FMT_SIZE 00106 " remain there, " FMT_LONG " remain to be copied", len, 00107 job->scoop_avail, job->copy_len); 00108 } 00109 00110 /** Catch up on an outstanding copy command. 00111 * 00112 * Takes data from the scoop, and the input (in that order), and writes as much 00113 * as will fit to the output, up to the limit of the outstanding copy. */ 00114 static void rs_tube_catchup_copy(rs_job_t *job) 00115 { 00116 assert(job->write_len == 0); 00117 assert(job->copy_len > 0); 00118 00119 /* If there's data in the scoop, send that first. */ 00120 if (job->scoop_avail && job->copy_len) { 00121 rs_tube_copy_from_scoop(job); 00122 } 00123 /* If there's more to copy and we emptied the scoop, send input. */ 00124 if (job->copy_len && !job->scoop_avail) { 00125 size_t this_copy = rs_buffers_copy(job->stream, job->copy_len); 00126 job->copy_len -= this_copy; 00127 rs_trace("copied " FMT_SIZE " bytes from input buffer, " FMT_LONG 00128 " remain to be copied", this_copy, job->copy_len); 00129 } 00130 } 00131 00132 /** Put whatever will fit from the tube into the output of the stream. 00133 * 00134 * \return RS_DONE if the tube is now empty and ready to accept another 00135 * command, RS_BLOCKED if there is still stuff waiting to go out. */ 00136 int rs_tube_catchup(rs_job_t *job) 00137 { 00138 if (job->write_len) { 00139 rs_tube_catchup_write(job); 00140 if (job->write_len) 00141 return RS_BLOCKED; 00142 } 00143 00144 if (job->copy_len) { 00145 rs_tube_catchup_copy(job); 00146 if (job->copy_len) { 00147 if (job->stream->eof_in && !job->stream->avail_in 00148 && !job->scoop_avail) { 00149 rs_error 00150 ("reached end of file while copying literal data through buffers"); 00151 return RS_INPUT_ENDED; 00152 } 00153 return RS_BLOCKED; 00154 } 00155 } 00156 return RS_DONE; 00157 } 00158 00159 /* Check whether there is data in the tube waiting to go out. 00160 00161 \return true if the previous command has finished doing all its output. */ 00162 int rs_tube_is_idle(rs_job_t const *job) 00163 { 00164 return job->write_len == 0 && job->copy_len == 0; 00165 } 00166 00167 /** Queue up a request to copy through \p len bytes from the input to the 00168 * output of the stream. 00169 * 00170 * The data is copied from the scoop (if there is anything there) or from the 00171 * input, on the next call to rs_tube_write(). 00172 * 00173 * We can only accept this request if there is no copy command already pending. 00174 * 00175 * \todo Try to do the copy immediately, and return a result. Then, people can 00176 * try to continue if possible. Is this really required? Callers can just go 00177 * out and back in again after flushing the tube. */ 00178 void rs_tube_copy(rs_job_t *job, int len) 00179 { 00180 assert(job->copy_len == 0); 00181 00182 job->copy_len = len; 00183 } 00184 00185 /** Push some data into the tube for storage. 00186 * 00187 * The tube's never supposed to get very big, so this will just pop loudly if 00188 * you do that. 00189 * 00190 * We can't accept write data if there's already a copy command in the tube, 00191 * because the write data comes out first. */ 00192 void rs_tube_write(rs_job_t *job, const void *buf, size_t len) 00193 { 00194 assert(job->copy_len == 0); 00195 assert(len <= sizeof(job->write_buf) - job->write_len); 00196 00197 memcpy(job->write_buf + job->write_len, buf, len); 00198 job->write_len += len; 00199 }