-rw-r--r-- | rsync/whole.c | 180 |
1 files changed, 180 insertions, 0 deletions
diff --git a/rsync/whole.c b/rsync/whole.c new file mode 100644 index 0000000..153d402 --- a/dev/null +++ b/rsync/whole.c | |||
@@ -0,0 +1,180 @@ | |||
1 | /*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- | ||
2 | * | ||
3 | * librsync -- the library for network deltas | ||
4 | * $Id$ | ||
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 | | Is it possible that software is not | ||
25 | | like anything else, that it is meant | ||
26 | | to be discarded: that the whole point | ||
27 | | is to always see it as a soap bubble? | ||
28 | | -- Alan Perlis | ||
29 | */ | ||
30 | |||
31 | |||
32 | |||
33 | #include <config_rsync.h> | ||
34 | |||
35 | #include <assert.h> | ||
36 | #include <stdlib.h> | ||
37 | #include <unistd.h> | ||
38 | #include <stdio.h> | ||
39 | #include <string.h> | ||
40 | #include <errno.h> | ||
41 | |||
42 | #include <rsync.h> | ||
43 | |||
44 | #include "trace.h" | ||
45 | #include "fileutil.h" | ||
46 | #include "sumset.h" | ||
47 | #include "job.h" | ||
48 | #include "buf.h" | ||
49 | #include "whole.h" | ||
50 | #include "util.h" | ||
51 | |||
52 | /** | ||
53 | * Run a job continuously, with input to/from the two specified files. | ||
54 | * The job should already be set up, and must be free by the caller | ||
55 | * after return. | ||
56 | * | ||
57 | * Buffers of ::rs_inbuflen and ::rs_outbuflen are allocated for | ||
58 | * temporary storage. | ||
59 | * | ||
60 | * \param in_file Source of input bytes, or NULL if the input buffer | ||
61 | * should not be filled. | ||
62 | * | ||
63 | * \return RS_DONE if the job completed, or otherwise an error result. | ||
64 | */ | ||
65 | rs_result | ||
66 | rs_whole_run(rs_job_t *job, FILE *in_file, FILE *out_file) | ||
67 | { | ||
68 | rs_buffers_t buf; | ||
69 | rs_result result; | ||
70 | rs_filebuf_t *in_fb = NULL, *out_fb = NULL; | ||
71 | |||
72 | if (in_file) | ||
73 | in_fb = rs_filebuf_new(in_file, rs_inbuflen); | ||
74 | |||
75 | if (out_file) | ||
76 | out_fb = rs_filebuf_new(out_file, rs_outbuflen); | ||
77 | |||
78 | result = rs_job_drive(job, &buf, | ||
79 | in_fb ? rs_infilebuf_fill : NULL, in_fb, | ||
80 | out_fb ? rs_outfilebuf_drain : NULL, out_fb); | ||
81 | |||
82 | if (in_fb) | ||
83 | rs_filebuf_free(in_fb); | ||
84 | |||
85 | if (out_fb) | ||
86 | rs_filebuf_free(out_fb); | ||
87 | |||
88 | return result; | ||
89 | } | ||
90 | |||
91 | |||
92 | |||
93 | /** | ||
94 | * Generate the signature of a basis file, and write it out to | ||
95 | * another. | ||
96 | * | ||
97 | * \param new_block_len block size for signature generation, in bytes | ||
98 | * | ||
99 | * \param strong_len truncated length of strong checksums, in bytes | ||
100 | * | ||
101 | * \sa rs_sig_begin() | ||
102 | */ | ||
103 | rs_result | ||
104 | rs_sig_file(FILE *old_file, FILE *sig_file, size_t new_block_len, | ||
105 | size_t strong_len, rs_stats_t *stats) | ||
106 | { | ||
107 | rs_job_t *job; | ||
108 | rs_result r; | ||
109 | |||
110 | job = rs_sig_begin(new_block_len, strong_len); | ||
111 | r = rs_whole_run(job, old_file, sig_file); | ||
112 | if (stats) | ||
113 | memcpy(stats, &job->stats, sizeof *stats); | ||
114 | rs_job_free(job); | ||
115 | |||
116 | return r; | ||
117 | } | ||
118 | |||
119 | |||
120 | /** | ||
121 | * Load signatures from a signature file into memory. Return a | ||
122 | * pointer to the newly allocated structure in SUMSET. | ||
123 | * | ||
124 | * \sa rs_readsig_begin() | ||
125 | */ | ||
126 | rs_result | ||
127 | rs_loadsig_file(FILE *sig_file, rs_signature_t **sumset, rs_stats_t *stats) | ||
128 | { | ||
129 | rs_job_t *job; | ||
130 | rs_result r; | ||
131 | |||
132 | job = rs_loadsig_begin(sumset); | ||
133 | r = rs_whole_run(job, sig_file, NULL); | ||
134 | if (stats) | ||
135 | memcpy(stats, &job->stats, sizeof *stats); | ||
136 | rs_job_free(job); | ||
137 | |||
138 | return r; | ||
139 | } | ||
140 | |||
141 | |||
142 | |||
143 | rs_result | ||
144 | rs_delta_file(rs_signature_t *sig, FILE *new_file, FILE *delta_file, | ||
145 | rs_stats_t *stats) | ||
146 | { | ||
147 | rs_job_t *job; | ||
148 | rs_result r; | ||
149 | |||
150 | job = rs_delta_begin(sig); | ||
151 | |||
152 | r = rs_whole_run(job, new_file, delta_file); | ||
153 | |||
154 | if (stats) | ||
155 | memcpy(stats, &job->stats, sizeof *stats); | ||
156 | |||
157 | rs_job_free(job); | ||
158 | |||
159 | return r; | ||
160 | } | ||
161 | |||
162 | |||
163 | |||
164 | rs_result rs_patch_file(FILE *basis_file, FILE *delta_file, FILE *new_file, | ||
165 | rs_stats_t *stats) | ||
166 | { | ||
167 | rs_job_t *job; | ||
168 | rs_result r; | ||
169 | |||
170 | job = rs_patch_begin(rs_file_copy_cb, basis_file); | ||
171 | |||
172 | r = rs_whole_run(job, delta_file, new_file); | ||
173 | |||
174 | if (stats) | ||
175 | memcpy(stats, &job->stats, sizeof *stats); | ||
176 | |||
177 | rs_job_free(job); | ||
178 | |||
179 | return r; | ||
180 | } | ||