Line data Source code
1 : /*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 : *
3 : * librsync -- the library for network deltas
4 : * $Id: buf.c,v 1.22 2003/12/16 00:10:55 abo Exp $
5 : *
6 : * Copyright (C) 2000, 2001 by Martin Pool <mbp@samba.org>
7 : *
8 : * This program is free software; you can redistribute it and/or
9 : * modify it under the terms of the GNU Lesser General Public License
10 : * as published by the Free Software Foundation; either version 2.1 of
11 : * the License, or (at your option) any later version.
12 : *
13 : * This program is distributed in the hope that it will be useful, but
14 : * WITHOUT ANY WARRANTY; without even the implied warranty of
15 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 : * Lesser General Public License for more details.
17 : *
18 : * You should have received a copy of the GNU Lesser General Public
19 : * License along with this program; if not, write to the Free Software
20 : * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 : */
22 :
23 : /*
24 : | Pick a window, Jimmy, you're leaving.
25 : | -- Martin Schwenke, regularly
26 : */
27 :
28 :
29 : /*
30 : * buf.c -- Buffers that map between stdio file streams and librsync
31 : * streams. As the stream consumes input and produces output, it is
32 : * refilled from appropriate input and output FILEs. A dynamically
33 : * allocated buffer of configurable size is used as an intermediary.
34 : */
35 :
36 : #include "../burp.h"
37 : #include "rs_buf.h"
38 : #include "../cmd.h"
39 : #include "../alloc.h"
40 : #include "../asfd.h"
41 : #include "../async.h"
42 : #include "../handy.h"
43 : #include "../iobuf.h"
44 : #include "../log.h"
45 :
46 : /* use fseeko instead of fseek for long file support if we have it */
47 : #ifdef HAVE_FSEEKO
48 : #define fseek fseeko
49 : #endif
50 :
51 24 : rs_filebuf_t *rs_filebuf_new(BFILE *bfd,
52 : struct fzp *fzp,
53 : struct asfd *asfd,
54 : size_t buf_len,
55 : size_t data_len)
56 : {
57 24 : rs_filebuf_t *pf=NULL;
58 24 : if(!(pf=(struct rs_filebuf *)calloc_w(1,
59 24 : sizeof(struct rs_filebuf), __func__))
60 24 : || !(pf->buf=(char *)calloc_w(1, buf_len, __func__)))
61 : goto error;
62 23 : pf->buf_len=buf_len;
63 23 : pf->fzp=fzp;
64 23 : pf->bfd=bfd;
65 23 : pf->bytes=0;
66 23 : pf->data_len=data_len;
67 23 : if(data_len>0)
68 9 : pf->do_known_byte_count=1;
69 : else
70 14 : pf->do_known_byte_count=0;
71 23 : if(!MD5_Init(&(pf->md5)))
72 : {
73 0 : logp("MD5_Init() failed\n");
74 0 : goto error;
75 : }
76 23 : pf->asfd=asfd;
77 23 : return pf;
78 : error:
79 1 : rs_filebuf_free(&pf);
80 1 : return NULL;
81 : }
82 :
83 118050 : void rs_filebuf_free(rs_filebuf_t **fb)
84 : {
85 236100 : if(!fb || !*fb) return;
86 23 : free_w(&((*fb)->buf));
87 23 : free_v((void **)fb);
88 : }
89 :
90 : /*
91 : * If the stream has no more data available, read some from F into
92 : * BUF, and let the stream use that. On return, SEEN_EOF is true if
93 : * the end of file has passed into the stream.
94 : */
95 14 : rs_result rs_infilebuf_fill(rs_job_t *job, rs_buffers_t *buf, void *opaque)
96 : {
97 14 : int len=0;
98 14 : rs_filebuf_t *fb=(rs_filebuf_t *) opaque;
99 :
100 : //logp("rs_infilebuf_fill\n");
101 :
102 : /* This is only allowed if either the buf has no input buffer
103 : * yet, or that buffer could possibly be BUF. */
104 14 : if(buf->next_in)
105 : {
106 : //logp("infilebuf avail_in %d buf_len %d\n",
107 : // buf->avail_in, fb->buf_len);
108 5 : if(buf->avail_in > fb->buf_len)
109 : {
110 : logp("buf->avail_in > fb->buf_len (%lu > %lu) in %s\n",
111 : (unsigned long)buf->avail_in,
112 1 : (unsigned long)fb->buf_len, __func__);
113 1 : return RS_IO_ERROR;
114 : }
115 4 : if(buf->next_in < fb->buf)
116 : {
117 1 : logp("buf->next_in < fb->buf in %s\n", __func__);
118 1 : return RS_IO_ERROR;
119 : }
120 3 : if(buf->next_in > fb->buf + fb->buf_len)
121 : {
122 : logp("buf->next_in > fb->buf + fb->buf_len in %s\n",
123 1 : __func__);
124 1 : return RS_IO_ERROR;
125 : }
126 : }
127 : else
128 : {
129 9 : if(buf->avail_in)
130 : {
131 : logp("buf->avail_in is %lu in %s\n",
132 1 : (unsigned long)buf->avail_in, __func__);
133 1 : return RS_IO_ERROR;
134 : }
135 : }
136 :
137 10 : if(buf->eof_in)
138 : return RS_DONE;
139 :
140 9 : if(buf->avail_in)
141 : /* Still some data remaining. Perhaps we should read
142 : anyhow? */
143 : return RS_DONE;
144 :
145 9 : if(fb->asfd)
146 : {
147 : static struct iobuf *rbuf=NULL;
148 4 : rbuf=fb->asfd->rbuf;
149 :
150 4 : switch(rbuf->cmd)
151 : {
152 : case CMD_APPEND:
153 2 : memcpy(fb->buf, rbuf->buf, rbuf->len);
154 2 : len=rbuf->len;
155 : break;
156 : case CMD_END_FILE:
157 2 : buf->eof_in=1;
158 2 : return RS_DONE;
159 : default:
160 0 : iobuf_log_unexpected(rbuf, __func__);
161 0 : return RS_IO_ERROR;
162 : }
163 : }
164 5 : else if(fb->bfd)
165 : {
166 2 : if(fb->do_known_byte_count)
167 : {
168 0 : if(fb->data_len>0)
169 : {
170 : len=fb->bfd->read(fb->bfd, fb->buf,
171 0 : min(fb->buf_len, fb->data_len));
172 0 : fb->data_len-=len;
173 : }
174 : else
175 : {
176 : // We have already read as much data as the VSS
177 : // header told us to, so set len=0 in order to
178 : // finish up.
179 : len=0;
180 : }
181 : }
182 : else
183 2 : len=fb->bfd->read(fb->bfd, fb->buf, fb->buf_len);
184 2 : if(len==0)
185 : {
186 : //logp("bread: eof\n");
187 2 : buf->eof_in=1;
188 2 : return RS_DONE;
189 : }
190 0 : else if(len<0)
191 : {
192 0 : logp("rs_infilebuf_fill: error in bread\n");
193 0 : return RS_IO_ERROR;
194 : }
195 : //logp("bread: ok: %d\n", len);
196 0 : fb->bytes+=len;
197 0 : if(!MD5_Update(&(fb->md5), fb->buf, len))
198 : {
199 0 : logp("rs_infilebuf_fill: MD5_Update() failed\n");
200 0 : return RS_IO_ERROR;
201 : }
202 : }
203 3 : else if(fb->fzp)
204 : {
205 2 : if((len=fzp_read(fb->fzp, fb->buf, fb->buf_len))<=0)
206 : {
207 : // This will happen if file size is a multiple of
208 : // input block len.
209 2 : if(fzp_eof(fb->fzp))
210 : {
211 2 : buf->eof_in=1;
212 2 : return RS_DONE;
213 : }
214 : else
215 : {
216 0 : logp("rs_infilebuf_fill: got return %d when trying to read\n", len);
217 0 : return RS_IO_ERROR;
218 : }
219 : }
220 0 : fb->bytes+=len;
221 0 : if(!MD5_Update(&(fb->md5), fb->buf, len))
222 : {
223 0 : logp("rs_infilebuf_fill: MD5_Update() failed\n");
224 0 : return RS_IO_ERROR;
225 : }
226 : }
227 :
228 3 : buf->avail_in = len;
229 3 : buf->next_in = fb->buf;
230 :
231 3 : return RS_DONE;
232 : }
233 :
234 : /*
235 : * The buf is already using BUF for an output buffer, and probably
236 : * contains some buffered output now. Write this out to F, and reset
237 : * the buffer cursor.
238 : */
239 15 : rs_result rs_outfilebuf_drain(rs_job_t *job, rs_buffers_t *buf, void *opaque)
240 : {
241 15 : rs_filebuf_t *fb=(rs_filebuf_t *)opaque;
242 : size_t wlen;
243 :
244 : //logp("in rs_outfilebuf_drain\n");
245 :
246 : /* This is only allowed if either the buf has no output buffer
247 : * yet, or that buffer could possibly be BUF. */
248 15 : if(!buf->next_out)
249 : {
250 6 : if(buf->avail_out)
251 : {
252 : logp("buf->avail_out is %lu in %s\n",
253 1 : (unsigned long)buf->avail_out, __func__);
254 1 : return RS_IO_ERROR;
255 : }
256 5 : buf->next_out = fb->buf;
257 5 : buf->avail_out = fb->buf_len;
258 5 : return RS_DONE;
259 : }
260 :
261 9 : if(buf->avail_out > fb->buf_len)
262 : {
263 : logp("buf->avail_out > fb->buf_len (%lu > %lu) in %s\n",
264 : (unsigned long)buf->avail_out,
265 1 : (unsigned long)fb->buf_len, __func__);
266 1 : return RS_IO_ERROR;
267 : }
268 8 : if(buf->next_out < fb->buf)
269 : {
270 : logp("buf->next_out < fb->buf (%p < %p) in %s\n",
271 1 : buf->next_out, fb->buf, __func__);
272 1 : return RS_IO_ERROR;
273 : }
274 7 : if(buf->next_out > fb->buf + fb->buf_len)
275 : {
276 : logp("buf->next_out > fb->buf + fb->buf_len in %s\n",
277 1 : __func__);
278 1 : return RS_IO_ERROR;
279 : }
280 :
281 6 : if((wlen=buf->next_out-fb->buf)>0)
282 : {
283 : //logp("wlen: %d\n", wlen);
284 6 : if(fb->asfd)
285 : {
286 6 : size_t w=wlen;
287 : struct iobuf wbuf;
288 6 : iobuf_set(&wbuf, CMD_APPEND, fb->buf, wlen);
289 6 : switch(fb->asfd->append_all_to_write_buffer(
290 : fb->asfd, &wbuf))
291 : {
292 : case APPEND_OK: break;
293 0 : case APPEND_BLOCKED: return RS_BLOCKED;
294 : case APPEND_ERROR:
295 0 : default: return RS_IO_ERROR;
296 : }
297 6 : fb->bytes+=w;
298 : }
299 : else
300 : {
301 0 : size_t result=0;
302 0 : result=fzp_write(fb->fzp, fb->buf, wlen);
303 0 : if(wlen!=result)
304 : {
305 : logp("error draining buf to file: %s",
306 0 : strerror(errno));
307 0 : return RS_IO_ERROR;
308 : }
309 : }
310 : }
311 :
312 6 : buf->next_out = fb->buf;
313 6 : buf->avail_out = fb->buf_len;
314 :
315 6 : return RS_DONE;
316 : }
317 :
318 14 : static rs_result rs_async_drive(rs_job_t *job, rs_buffers_t *rsbuf,
319 : rs_driven_cb in_cb, void *in_opaque,
320 : rs_driven_cb out_cb, void *out_opaque)
321 : {
322 : rs_result result;
323 : rs_result iores;
324 :
325 14 : if(!rsbuf->eof_in && in_cb)
326 : {
327 8 : iores=in_cb(job, rsbuf, in_opaque);
328 8 : if(iores!=RS_DONE) return iores;
329 : }
330 :
331 14 : result=rs_job_iter(job, rsbuf);
332 14 : if(result!=RS_DONE && result!=RS_BLOCKED)
333 : return result;
334 :
335 14 : if(out_cb)
336 : {
337 10 : iores=(out_cb)(job, rsbuf, out_opaque);
338 10 : if(iores!=RS_DONE) return iores;
339 : }
340 :
341 14 : return result;
342 : }
343 :
344 14 : rs_result rs_async(rs_job_t *job, rs_buffers_t *rsbuf,
345 : rs_filebuf_t *infb, rs_filebuf_t *outfb)
346 : {
347 : return rs_async_drive(job, rsbuf,
348 : infb ? rs_infilebuf_fill : NULL, infb,
349 14 : outfb ? rs_outfilebuf_drain : NULL, outfb);
350 : }
351 :
352 0 : static rs_result rs_whole_gzrun(
353 : rs_job_t *job, struct fzp *in_file, struct fzp *out_file)
354 : {
355 : rs_buffers_t buf;
356 : rs_result result;
357 0 : rs_filebuf_t *in_fb=NULL;
358 0 : rs_filebuf_t *out_fb=NULL;
359 :
360 0 : if(in_file)
361 : in_fb=rs_filebuf_new(NULL,
362 0 : in_file, NULL, ASYNC_BUF_LEN, -1);
363 0 : if(out_file)
364 : out_fb=rs_filebuf_new(NULL,
365 0 : out_file, NULL, ASYNC_BUF_LEN, -1);
366 :
367 : result=rs_job_drive(job, &buf,
368 : in_fb ? rs_infilebuf_fill : NULL, in_fb,
369 0 : out_fb ? rs_outfilebuf_drain : NULL, out_fb);
370 :
371 0 : rs_filebuf_free(&in_fb);
372 0 : rs_filebuf_free(&out_fb);
373 0 : return result;
374 : }
375 :
376 0 : rs_result rs_patch_gzfile(struct fzp *basis_file,
377 : struct fzp *delta_file, struct fzp *new_file)
378 : {
379 : rs_job_t *job;
380 : rs_result r;
381 :
382 : // FIX THIS: Seems wrong to just pick out basis_file->fp.
383 : // Should probably pass a fp into rs_patch_gzfile.
384 : // Or, much better would be to investigate whether basis_file always
385 : // needs to be a FILE *. If I copy rs_file_copy_cb from librsync, and
386 : // rewrite it to use a struct fzp, maybe the callers to rs_patch_gzfile
387 : // do not need to mess around inflating files when they do not have
388 : // to.
389 0 : job=rs_patch_begin(rs_file_copy_cb, basis_file->fp);
390 0 : r=rs_whole_gzrun(job, delta_file, new_file);
391 0 : rs_job_free(job);
392 :
393 0 : return r;
394 : }
395 :
396 0 : rs_result rs_sig_gzfile(struct fzp *old_file, struct fzp *sig_file,
397 : size_t new_block_len, size_t strong_len,
398 : struct conf **confs)
399 : {
400 : rs_job_t *job;
401 : rs_result r;
402 : job=
403 : rs_sig_begin(new_block_len, strong_len
404 : #ifndef RS_DEFAULT_STRONG_LEN
405 : , rshash_to_magic_number(
406 : get_e_rshash(confs[OPT_RSHASH]))
407 : #endif
408 0 : );
409 :
410 0 : r=rs_whole_gzrun(job, old_file, sig_file);
411 0 : rs_job_free(job);
412 :
413 0 : return r;
414 : }
415 :
416 0 : rs_result rs_delta_gzfile(rs_signature_t *sig, struct fzp *new_file,
417 : struct fzp *delta_file)
418 : {
419 : rs_job_t *job;
420 : rs_result r;
421 :
422 0 : job=rs_delta_begin(sig);
423 0 : r=rs_whole_gzrun(job, new_file, delta_file);
424 0 : rs_job_free(job);
425 :
426 0 : return r;
427 : }
428 :
429 : #ifndef RS_DEFAULT_STRONG_LEN
430 : rs_magic_number rshash_to_magic_number(enum rshash r)
431 : {
432 : switch(r)
433 : {
434 : case RSHASH_BLAKE2: return RS_BLAKE2_SIG_MAGIC;
435 : default: return RS_MD4_SIG_MAGIC;
436 : }
437 : }
438 : #endif
439 :
440 0 : rs_result rs_loadsig_fzp(struct fzp *fzp,
441 : rs_signature_t **sig, rs_stats_t *stats)
442 : {
443 0 : return rs_loadsig_file(fzp->fp, sig, stats);
444 : }
|