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(struct 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 : sizeof(struct rs_filebuf), __func__))
60 23 : || !(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 119266 : void rs_filebuf_free(rs_filebuf_t **fb)
84 : {
85 238532 : 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(__attribute__ ((unused)) rs_job_t *job,
96 : rs_buffers_t *buf, void *opaque)
97 : {
98 14 : int len=0;
99 14 : rs_filebuf_t *fb=(rs_filebuf_t *) opaque;
100 :
101 : //logp("rs_infilebuf_fill\n");
102 :
103 : /* This is only allowed if either the buf has no input buffer
104 : * yet, or that buffer could possibly be BUF. */
105 14 : if(buf->next_in)
106 : {
107 : //logp("infilebuf avail_in %d buf_len %d\n",
108 : // buf->avail_in, fb->buf_len);
109 5 : if(buf->avail_in > fb->buf_len)
110 : {
111 1 : logp("buf->avail_in > fb->buf_len (%lu > %lu) in %s\n",
112 : (unsigned long)buf->avail_in,
113 : (unsigned long)fb->buf_len, __func__);
114 1 : return RS_IO_ERROR;
115 : }
116 4 : if(buf->next_in < fb->buf)
117 : {
118 1 : logp("buf->next_in < fb->buf in %s\n", __func__);
119 1 : return RS_IO_ERROR;
120 : }
121 3 : if(buf->next_in > fb->buf + fb->buf_len)
122 : {
123 1 : logp("buf->next_in > fb->buf + fb->buf_len in %s\n",
124 : __func__);
125 1 : return RS_IO_ERROR;
126 : }
127 : }
128 : else
129 : {
130 9 : if(buf->avail_in)
131 : {
132 1 : logp("buf->avail_in is %lu in %s\n",
133 : (unsigned long)buf->avail_in, __func__);
134 1 : return RS_IO_ERROR;
135 : }
136 : }
137 :
138 10 : if(buf->eof_in)
139 : return RS_DONE;
140 :
141 9 : if(buf->avail_in)
142 : /* Still some data remaining. Perhaps we should read
143 : anyhow? */
144 : return RS_DONE;
145 :
146 9 : if(fb->asfd)
147 : {
148 : static struct iobuf *rbuf=NULL;
149 4 : rbuf=fb->asfd->rbuf;
150 :
151 4 : switch(rbuf->cmd)
152 : {
153 : case CMD_APPEND:
154 2 : memcpy(fb->buf, rbuf->buf, rbuf->len);
155 2 : len=rbuf->len;
156 : break;
157 : case CMD_END_FILE:
158 2 : buf->eof_in=1;
159 2 : return RS_DONE;
160 : default:
161 0 : iobuf_log_unexpected(rbuf, __func__);
162 0 : return RS_IO_ERROR;
163 : }
164 : }
165 5 : else if(fb->bfd)
166 : {
167 2 : if(fb->do_known_byte_count)
168 : {
169 0 : if(fb->data_len>0)
170 : {
171 0 : len=fb->bfd->read(fb->bfd, fb->buf,
172 0 : min(fb->buf_len, fb->data_len));
173 0 : fb->data_len-=len;
174 : }
175 : else
176 : {
177 : // We have already read as much data as the VSS
178 : // header told us to, so set len=0 in order to
179 : // finish up.
180 : len=0;
181 : }
182 : }
183 : else
184 2 : len=fb->bfd->read(fb->bfd, fb->buf, fb->buf_len);
185 2 : if(len==0)
186 : {
187 : //logp("bread: eof\n");
188 2 : buf->eof_in=1;
189 2 : return RS_DONE;
190 : }
191 0 : else if(len<0)
192 : {
193 0 : logp("rs_infilebuf_fill: error in bread\n");
194 0 : return RS_IO_ERROR;
195 : }
196 : //logp("bread: ok: %d\n", len);
197 0 : fb->bytes+=len;
198 0 : if(!MD5_Update(&(fb->md5), fb->buf, len))
199 : {
200 0 : logp("rs_infilebuf_fill: MD5_Update() failed\n");
201 0 : return RS_IO_ERROR;
202 : }
203 : }
204 3 : else if(fb->fzp)
205 : {
206 2 : if((len=fzp_read(fb->fzp, fb->buf, fb->buf_len))<=0)
207 : {
208 : // This will happen if file size is a multiple of
209 : // input block len.
210 2 : if(fzp_eof(fb->fzp))
211 : {
212 2 : buf->eof_in=1;
213 2 : return RS_DONE;
214 : }
215 : else
216 : {
217 0 : logp("rs_infilebuf_fill: got return %d when trying to read\n", len);
218 0 : return RS_IO_ERROR;
219 : }
220 : }
221 0 : fb->bytes+=len;
222 0 : if(!MD5_Update(&(fb->md5), fb->buf, len))
223 : {
224 0 : logp("rs_infilebuf_fill: MD5_Update() failed\n");
225 0 : return RS_IO_ERROR;
226 : }
227 : }
228 :
229 3 : buf->avail_in = len;
230 3 : buf->next_in = fb->buf;
231 :
232 3 : return RS_DONE;
233 : }
234 :
235 : /*
236 : * The buf is already using BUF for an output buffer, and probably
237 : * contains some buffered output now. Write this out to F, and reset
238 : * the buffer cursor.
239 : */
240 15 : rs_result rs_outfilebuf_drain(__attribute__ ((unused)) rs_job_t *job,
241 : rs_buffers_t *buf, void *opaque)
242 : {
243 15 : rs_filebuf_t *fb=(rs_filebuf_t *)opaque;
244 : size_t wlen;
245 :
246 : //logp("in rs_outfilebuf_drain\n");
247 :
248 : /* This is only allowed if either the buf has no output buffer
249 : * yet, or that buffer could possibly be BUF. */
250 15 : if(!buf->next_out)
251 : {
252 6 : if(buf->avail_out)
253 : {
254 1 : logp("buf->avail_out is %lu in %s\n",
255 : (unsigned long)buf->avail_out, __func__);
256 1 : return RS_IO_ERROR;
257 : }
258 5 : buf->next_out = fb->buf;
259 5 : buf->avail_out = fb->buf_len;
260 5 : return RS_DONE;
261 : }
262 :
263 9 : if(buf->avail_out > fb->buf_len)
264 : {
265 1 : logp("buf->avail_out > fb->buf_len (%lu > %lu) in %s\n",
266 : (unsigned long)buf->avail_out,
267 : (unsigned long)fb->buf_len, __func__);
268 1 : return RS_IO_ERROR;
269 : }
270 8 : if(buf->next_out < fb->buf)
271 : {
272 1 : logp("buf->next_out < fb->buf (%p < %p) in %s\n",
273 : buf->next_out, fb->buf, __func__);
274 1 : return RS_IO_ERROR;
275 : }
276 7 : if(buf->next_out > fb->buf + fb->buf_len)
277 : {
278 1 : logp("buf->next_out > fb->buf + fb->buf_len in %s\n",
279 : __func__);
280 1 : return RS_IO_ERROR;
281 : }
282 :
283 6 : if((wlen=buf->next_out-fb->buf)>0)
284 : {
285 : //logp("wlen: %d\n", wlen);
286 6 : if(fb->asfd)
287 : {
288 6 : size_t w=wlen;
289 : struct iobuf wbuf;
290 6 : iobuf_set(&wbuf, CMD_APPEND, fb->buf, wlen);
291 6 : switch(fb->asfd->append_all_to_write_buffer(
292 : fb->asfd, &wbuf))
293 : {
294 : case APPEND_OK: break;
295 0 : case APPEND_BLOCKED: return RS_BLOCKED;
296 : case APPEND_ERROR:
297 0 : default: return RS_IO_ERROR;
298 : }
299 6 : fb->bytes+=w;
300 : }
301 : else
302 : {
303 0 : size_t result=0;
304 0 : result=fzp_write(fb->fzp, fb->buf, wlen);
305 0 : if(wlen!=result)
306 : {
307 0 : logp("error draining buf to file: %s",
308 0 : strerror(errno));
309 0 : return RS_IO_ERROR;
310 : }
311 : }
312 : }
313 :
314 6 : buf->next_out = fb->buf;
315 6 : buf->avail_out = fb->buf_len;
316 :
317 6 : return RS_DONE;
318 : }
319 :
320 14 : static rs_result rs_async_drive(rs_job_t *job, rs_buffers_t *rsbuf,
321 : rs_driven_cb in_cb, void *in_opaque,
322 : rs_driven_cb out_cb, void *out_opaque)
323 : {
324 : rs_result result;
325 : rs_result iores;
326 :
327 14 : if(!rsbuf->eof_in && in_cb)
328 : {
329 8 : iores=in_cb(job, rsbuf, in_opaque);
330 8 : if(iores!=RS_DONE) return iores;
331 : }
332 :
333 14 : result=rs_job_iter(job, rsbuf);
334 14 : if(result!=RS_DONE && result!=RS_BLOCKED)
335 : return result;
336 :
337 14 : if(out_cb)
338 : {
339 10 : iores=(out_cb)(job, rsbuf, out_opaque);
340 10 : if(iores!=RS_DONE) return iores;
341 : }
342 :
343 14 : return result;
344 : }
345 :
346 14 : rs_result rs_async(rs_job_t *job, rs_buffers_t *rsbuf,
347 : rs_filebuf_t *infb, rs_filebuf_t *outfb)
348 : {
349 14 : return rs_async_drive(job, rsbuf,
350 : infb ? rs_infilebuf_fill : NULL, infb,
351 : outfb ? rs_outfilebuf_drain : NULL, outfb);
352 : }
353 :
354 0 : static rs_result rs_whole_gzrun(
355 : rs_job_t *job, struct fzp *in_file, struct fzp *out_file)
356 : {
357 : rs_buffers_t buf;
358 : rs_result result;
359 0 : rs_filebuf_t *in_fb=NULL;
360 0 : rs_filebuf_t *out_fb=NULL;
361 :
362 0 : if(in_file)
363 0 : in_fb=rs_filebuf_new(NULL,
364 : in_file, NULL, ASYNC_BUF_LEN, -1);
365 0 : if(out_file)
366 0 : out_fb=rs_filebuf_new(NULL,
367 : out_file, NULL, ASYNC_BUF_LEN, -1);
368 :
369 0 : result=rs_job_drive(job, &buf,
370 0 : in_fb ? rs_infilebuf_fill : NULL, in_fb,
371 0 : out_fb ? rs_outfilebuf_drain : NULL, out_fb);
372 :
373 0 : rs_filebuf_free(&in_fb);
374 0 : rs_filebuf_free(&out_fb);
375 0 : return result;
376 : }
377 :
378 0 : rs_result rs_patch_gzfile(struct fzp *basis_file,
379 : struct fzp *delta_file, struct fzp *new_file)
380 : {
381 : rs_job_t *job;
382 : rs_result r;
383 :
384 : // FIX THIS: Seems wrong to just pick out basis_file->fp.
385 : // Should probably pass a fp into rs_patch_gzfile.
386 : // Or, much better would be to investigate whether basis_file always
387 : // needs to be a FILE *. If I copy rs_file_copy_cb from librsync, and
388 : // rewrite it to use a struct fzp, maybe the callers to rs_patch_gzfile
389 : // do not need to mess around inflating files when they do not have
390 : // to.
391 0 : job=rs_patch_begin(rs_file_copy_cb, basis_file->fp);
392 0 : r=rs_whole_gzrun(job, delta_file, new_file);
393 0 : rs_job_free(job);
394 :
395 0 : return r;
396 : }
397 :
398 0 : rs_result rs_sig_gzfile(struct fzp *old_file, struct fzp *sig_file,
399 : size_t new_block_len, size_t strong_len,
400 : struct conf **confs)
401 : {
402 : rs_job_t *job;
403 : rs_result r;
404 0 : job=
405 : rs_sig_begin(new_block_len, strong_len
406 : #ifndef RS_DEFAULT_STRONG_LEN
407 : , rshash_to_magic_number(
408 : get_e_rshash(confs[OPT_RSHASH]))
409 : #endif
410 : );
411 :
412 0 : r=rs_whole_gzrun(job, old_file, sig_file);
413 0 : rs_job_free(job);
414 :
415 0 : return r;
416 : }
417 :
418 0 : rs_result rs_delta_gzfile(rs_signature_t *sig, struct fzp *new_file,
419 : struct fzp *delta_file)
420 : {
421 : rs_job_t *job;
422 : rs_result r;
423 :
424 0 : job=rs_delta_begin(sig);
425 0 : r=rs_whole_gzrun(job, new_file, delta_file);
426 0 : rs_job_free(job);
427 :
428 0 : return r;
429 : }
430 :
431 : #ifndef RS_DEFAULT_STRONG_LEN
432 : rs_magic_number rshash_to_magic_number(enum rshash r)
433 : {
434 : switch(r)
435 : {
436 : case RSHASH_BLAKE2: return RS_BLAKE2_SIG_MAGIC;
437 : default: return RS_MD4_SIG_MAGIC;
438 : }
439 : }
440 : #endif
441 :
442 0 : rs_result rs_loadsig_fzp(struct fzp *fzp, rs_signature_t **sig)
443 : {
444 : rs_result r;
445 : rs_job_t *job;
446 :
447 0 : job=rs_loadsig_begin(sig);
448 0 : r=rs_whole_gzrun(job, fzp, NULL);
449 0 : rs_job_free(job);
450 :
451 0 : return r;
452 : }
|