librsync  2.3.3
whole.c
1/*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 *
3 * librsync -- the library for network deltas
4 *
5 * Copyright 2000, 2001, 2014, 2015 by Martin Pool <mbp@sourcefrog.net>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 /*=
23 | Is it possible that software is not
24 | like anything else, that it is meant
25 | to be discarded: that the whole point
26 | is to always see it as a soap bubble?
27 | -- Alan Perlis
28 */
29
30#include <stdio.h>
31#include <string.h>
32#include "librsync.h"
33#include "whole.h"
34#include "sumset.h"
35#include "job.h"
36#include "buf.h"
37#include "librsync_export.h"
38
39/** Whole file IO buffer sizes. */
40LIBRSYNC_EXPORT int rs_inbuflen = 0, rs_outbuflen = 0;
41
42rs_result rs_whole_run(rs_job_t *job, FILE *in_file, FILE *out_file,
43 int inbuflen, int outbuflen)
44{
45 rs_buffers_t buf;
46 rs_result result;
47 rs_filebuf_t *in_fb = NULL, *out_fb = NULL;
48
49 /* Override buffer sizes if rs_inbuflen or rs_outbuflen are set. */
50 inbuflen = rs_inbuflen ? rs_inbuflen : inbuflen;
51 outbuflen = rs_outbuflen ? rs_outbuflen : outbuflen;
52 if (in_file)
53 in_fb = rs_filebuf_new(in_file, inbuflen);
54 if (out_file)
55 out_fb = rs_filebuf_new(out_file, outbuflen);
56 result =
57 rs_job_drive(job, &buf, in_fb ? rs_infilebuf_fill : NULL, in_fb,
58 out_fb ? rs_outfilebuf_drain : NULL, out_fb);
59 if (in_fb)
60 rs_filebuf_free(in_fb);
61 if (out_fb)
62 rs_filebuf_free(out_fb);
63 return result;
64}
65
66rs_result rs_sig_file(FILE *old_file, FILE *sig_file, size_t block_len,
67 size_t strong_len, rs_magic_number sig_magic,
68 rs_stats_t *stats)
69{
70 rs_job_t *job;
71 rs_result r;
72 rs_long_t old_fsize = rs_file_size(old_file);
73
74 if ((r =
75 rs_sig_args(old_fsize, &sig_magic, &block_len,
76 &strong_len)) != RS_DONE)
77 return r;
78 job = rs_sig_begin(block_len, strong_len, sig_magic);
79 /* Size inbuf for 4 blocks, outbuf for header + 4 blocksums. */
80 r = rs_whole_run(job, old_file, sig_file, 4 * (int)block_len,
81 12 + 4 * (4 + (int)strong_len));
82 if (stats)
83 memcpy(stats, &job->stats, sizeof *stats);
84 rs_job_free(job);
85
86 return r;
87}
88
89rs_result rs_loadsig_file(FILE *sig_file, rs_signature_t **sumset,
90 rs_stats_t *stats)
91{
92 rs_job_t *job;
93 rs_result r;
94
95 job = rs_loadsig_begin(sumset);
96 /* Set filesize used to estimate signature size. */
97 job->sig_fsize = rs_file_size(sig_file);
98 /* Size inbuf for 1024x 16 byte blocksums. */
99 r = rs_whole_run(job, sig_file, NULL, 1024 * 16, 0);
100 if (stats)
101 memcpy(stats, &job->stats, sizeof *stats);
102 rs_job_free(job);
103
104 return r;
105}
106
107rs_result rs_delta_file(rs_signature_t *sig, FILE *new_file, FILE *delta_file,
108 rs_stats_t *stats)
109{
110 rs_job_t *job;
111 rs_result r;
112
113 job = rs_delta_begin(sig);
114 /* Size inbuf for 4*(CMD + 1 block), outbuf for 4*CMD. */
115 r = rs_whole_run(job, new_file, delta_file,
116 4 * (MAX_DELTA_CMD + sig->block_len), 4 * MAX_DELTA_CMD);
117 if (stats)
118 memcpy(stats, &job->stats, sizeof *stats);
119 rs_job_free(job);
120 return r;
121}
122
123rs_result rs_patch_file(FILE *basis_file, FILE *delta_file, FILE *new_file,
124 rs_stats_t *stats)
125{
126 rs_job_t *job;
127 rs_result r;
128
129 job = rs_patch_begin(rs_file_copy_cb, basis_file);
130 /* Default size inbuf 1*CMD and outbuf 4*CMD. */
131 r = rs_whole_run(job, delta_file, new_file, MAX_DELTA_CMD,
132 4 * MAX_DELTA_CMD);
133 if (stats)
134 memcpy(stats, &job->stats, sizeof *stats);
135 rs_job_free(job);
136 return r;
137}
Buffers that map between stdio file streams and librsync streams.
rs_job_t * rs_delta_begin(rs_signature_t *sig)
Prepare to compute a streaming delta.
Definition: delta.c:395
Generic state-machine interface.
#define MAX_DELTA_CMD
Max length of a singled delta command is including command bytes.
Definition: job.h:44
Public header for librsync.
LIBRSYNC_EXPORT rs_job_t * rs_patch_begin(rs_copy_cb *copy_cb, void *copy_arg)
Apply a delta to a basis file to recreate the new file.
Definition: patch.c:220
LIBRSYNC_EXPORT rs_job_t * rs_sig_begin(size_t block_len, size_t strong_len, rs_magic_number sig_magic)
Start generating a signature.
Definition: mksum.c:109
LIBRSYNC_EXPORT rs_result rs_sig_file(FILE *old_file, FILE *sig_file, size_t block_len, size_t strong_len, rs_magic_number sig_magic, rs_stats_t *stats)
Generate the signature of a basis file, and write it out to another.
Definition: whole.c:66
LIBRSYNC_EXPORT rs_result rs_sig_args(rs_long_t old_fsize, rs_magic_number *magic, size_t *block_len, size_t *strong_len)
Get or check signature arguments for a given file size.
Definition: sumset.c:112
LIBRSYNC_EXPORT rs_job_t * rs_loadsig_begin(rs_signature_t **)
Read a signature from a file into an rs_signature structure in memory.
Definition: readsums.c:134
LIBRSYNC_EXPORT rs_result rs_delta_file(rs_signature_t *, FILE *new_file, FILE *delta_file, rs_stats_t *)
Generate a delta between a signature and a new file into a delta file.
Definition: whole.c:107
LIBRSYNC_EXPORT rs_result rs_loadsig_file(FILE *sig_file, rs_signature_t **sumset, rs_stats_t *stats)
Load signatures from a signature file into memory.
Definition: whole.c:89
LIBRSYNC_EXPORT rs_result rs_patch_file(FILE *basis_file, FILE *delta_file, FILE *new_file, rs_stats_t *)
Apply a patch, relative to a basis, into a new file.
Definition: whole.c:123
rs_result
Return codes from nonblocking rsync operations.
Definition: librsync.h:180
@ RS_DONE
Completed successfully.
Definition: librsync.h:181
rs_magic_number
A uint32 magic number, emitted in bigendian/network order at the start of librsync files.
Definition: librsync.h:65
LIBRSYNC_EXPORT int rs_inbuflen
Buffer sizes for file IO.
Definition: whole.c:40
Description of input and output buffers.
Definition: librsync.h:328
Definition: buf.c:36
The contents of this structure are private.
Definition: job.h:47
rs_long_t sig_fsize
The size of the signature file if available.
Definition: job.h:69
rs_stats_t stats
Encoding statistics.
Definition: job.h:93
Signature of a whole file.
Definition: sumset.h:44
int block_len
The block length.
Definition: sumset.h:46
Performance statistics from a librsync encoding or decoding operation.
Definition: librsync.h:210
The rs_signature class implementation of a file signature.
Whole-file API driver functions.