librsync  2.3.1
patch.c
1 /*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
2  *
3  * librsync -- the library for network deltas
4  *
5  * Copyright (C) 2000, 2001 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  | This is Tranquility Base.
24  */
25 
26 #include "config.h"
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include "librsync.h"
31 #include "job.h"
32 #include "netint.h"
33 #include "stream.h"
34 #include "command.h"
35 #include "prototab.h"
36 #include "trace.h"
37 
38 static rs_result rs_patch_s_cmdbyte(rs_job_t *);
39 static rs_result rs_patch_s_params(rs_job_t *);
40 static rs_result rs_patch_s_run(rs_job_t *);
41 static rs_result rs_patch_s_literal(rs_job_t *);
42 static rs_result rs_patch_s_copy(rs_job_t *);
43 static rs_result rs_patch_s_copying(rs_job_t *);
44 
45 /** State of trying to read the first byte of a command. Once we've taken that
46  * in, we can know how much data to read to get the arguments. */
47 static rs_result rs_patch_s_cmdbyte(rs_job_t *job)
48 {
49  rs_result result;
50 
51  if ((result = rs_suck_byte(job, &job->op)) != RS_DONE)
52  return result;
53 
54  job->cmd = &rs_prototab[job->op];
55 
56  rs_trace("got command %#04x (%s), len_1=" FMT_SIZE "", job->op,
57  rs_op_kind_name(job->cmd->kind), job->cmd->len_1);
58 
59  if (job->cmd->len_1)
60  job->statefn = rs_patch_s_params;
61  else {
62  job->param1 = job->cmd->immediate;
63  job->statefn = rs_patch_s_run;
64  }
65 
66  return RS_RUNNING;
67 }
68 
69 /** Called after reading a command byte to pull in its parameters and then
70  * setup to execute the command. */
71 static rs_result rs_patch_s_params(rs_job_t *job)
72 {
73  rs_result result;
74  int len = job->cmd->len_1 + job->cmd->len_2;
75  void *p;
76 
77  assert(len);
78 
79  result = rs_scoop_readahead(job, len, &p);
80  if (result != RS_DONE)
81  return result;
82 
83  /* we now must have LEN bytes buffered */
84  result = rs_suck_netint(job, &job->param1, job->cmd->len_1);
85  /* shouldn't fail, since we already checked */
86  assert(result == RS_DONE);
87 
88  if (job->cmd->len_2) {
89  result = rs_suck_netint(job, &job->param2, job->cmd->len_2);
90  assert(result == RS_DONE);
91  }
92 
93  job->statefn = rs_patch_s_run;
94 
95  return RS_RUNNING;
96 }
97 
98 /** Called when we've read in the whole command and we need to execute it. */
99 static rs_result rs_patch_s_run(rs_job_t *job)
100 {
101  rs_trace("running command %#04x", job->op);
102 
103  switch (job->cmd->kind) {
104  case RS_KIND_LITERAL:
105  job->statefn = rs_patch_s_literal;
106  return RS_RUNNING;
107 
108  case RS_KIND_END:
109  return RS_DONE;
110  /* so we exit here; trying to continue causes an error */
111 
112  case RS_KIND_COPY:
113  job->statefn = rs_patch_s_copy;
114  return RS_RUNNING;
115 
116  default:
117  rs_error("bogus command %#04x", job->op);
118  return RS_CORRUPT;
119  }
120 }
121 
122 /** Called when trying to copy through literal data. */
123 static rs_result rs_patch_s_literal(rs_job_t *job)
124 {
125  rs_long_t len = job->param1;
126 
127  rs_trace("LITERAL(len=" FMT_LONG ")", len);
128 
129  if (len < 0) {
130  rs_error("invalid length=" FMT_LONG " on LITERAL command", len);
131  return RS_CORRUPT;
132  }
133 
134  job->stats.lit_cmds++;
135  job->stats.lit_bytes += len;
136  job->stats.lit_cmdbytes += 1 + job->cmd->len_1;
137 
138  rs_tube_copy(job, len);
139 
140  job->statefn = rs_patch_s_cmdbyte;
141  return RS_RUNNING;
142 }
143 
144 static rs_result rs_patch_s_copy(rs_job_t *job)
145 {
146  rs_long_t where, len;
147  rs_stats_t *stats;
148 
149  where = job->param1;
150  len = job->param2;
151 
152  rs_trace("COPY(where=" FMT_LONG ", len=" FMT_LONG ")", where, len);
153 
154  if (len < 0) {
155  rs_error("invalid length=" FMT_LONG " on COPY command", len);
156  return RS_CORRUPT;
157  }
158 
159  if (where < 0) {
160  rs_error("invalid where=" FMT_LONG " on COPY command", where);
161  return RS_CORRUPT;
162  }
163 
164  job->basis_pos = where;
165  job->basis_len = len;
166 
167  stats = &job->stats;
168 
169  stats->copy_cmds++;
170  stats->copy_bytes += len;
171  stats->copy_cmdbytes += 1 + job->cmd->len_1 + job->cmd->len_2;
172 
173  job->statefn = rs_patch_s_copying;
174  return RS_RUNNING;
175 }
176 
177 /** Called when we're executing a COPY command and waiting for all the data to
178  * be retrieved from the callback. */
179 static rs_result rs_patch_s_copying(rs_job_t *job)
180 {
181  rs_result result;
182  size_t desired_len, len;
183  void *ptr;
184  rs_buffers_t *buffs = job->stream;
185 
186  /* copy only as much as will fit in the output buffer, so that we don't
187  have to block or store the input. */
188  desired_len = len =
189  (buffs->avail_out < job->basis_len) ? buffs->avail_out : job->basis_len;
190 
191  if (!len)
192  return RS_BLOCKED;
193 
194  rs_trace("copy " FMT_SIZE " bytes from basis at offset " FMT_LONG "", len,
195  job->basis_pos);
196 
197  ptr = buffs->next_out;
198 
199  result = (job->copy_cb) (job->copy_arg, job->basis_pos, &len, &ptr);
200  if (result != RS_DONE)
201  return result;
202  else
203  rs_trace("copy callback returned %s", rs_strerror(result));
204 
205  rs_trace("got " FMT_SIZE " bytes back from basis callback", len);
206 
207  if (len > desired_len) {
208  rs_trace("warning: copy_cb returned more than the requested length.");
209  len = desired_len;
210  }
211 
212  /* copy back to out buffer only if the callback has used its own buffer */
213  if (ptr != buffs->next_out)
214  memcpy(buffs->next_out, ptr, len);
215 
216  buffs->next_out += len;
217  buffs->avail_out -= len;
218 
219  job->basis_pos += len;
220  job->basis_len -= len;
221 
222  if (!job->basis_len) {
223  /* Done! */
224  job->statefn = rs_patch_s_cmdbyte;
225  }
226 
227  return RS_RUNNING;
228 }
229 
230 /** Called while we're trying to read the header of the patch. */
231 static rs_result rs_patch_s_header(rs_job_t *job)
232 {
233  int v;
234  rs_result result;
235 
236  if ((result = rs_suck_n4(job, &v)) != RS_DONE)
237  return result;
238 
239  if (v != RS_DELTA_MAGIC) {
240  rs_error("got magic number %#x rather than expected value %#x", v,
242  return RS_BAD_MAGIC;
243  } else
244  rs_trace("got patch magic %#x", v);
245 
246  job->statefn = rs_patch_s_cmdbyte;
247 
248  return RS_RUNNING;
249 }
250 
251 rs_job_t *rs_patch_begin(rs_copy_cb * copy_cb, void *copy_arg)
252 {
253  rs_job_t *job = rs_job_new("patch", rs_patch_s_header);
254 
255  job->copy_cb = copy_cb;
256  job->copy_arg = copy_arg;
257 
258  rs_mdfour_begin(&job->output_md4);
259 
260  return job;
261 }
rs_result
rs_result
Return codes from nonblocking rsync operations.
Definition: librsync.h:180
rs_op_kind_name
Definition: command.h:44
trace.h
RS_DONE
@ RS_DONE
Completed successfully.
Definition: librsync.h:181
rs_job::param1
rs_long_t param1
Lengths of expected parameters.
Definition: job.h:66
rs_job::statefn
rs_result(* statefn)(rs_job_t *)
Callback for each processing step.
Definition: job.h:35
rs_job::basis_pos
rs_long_t basis_pos
Copy from the basis position.
Definition: job.h:92
librsync.h
rs_job::op
unsigned char op
Command byte currently being processed, if any.
Definition: job.h:57
rs_job
The contents of this structure are private.
Definition: job.h:26
RS_RUNNING
@ RS_RUNNING
The job is still running, and not yet finished or blocked.
Definition: librsync.h:183
command.h
rs_strerror
LIBRSYNC_EXPORT char const * rs_strerror(rs_result r)
Return an English description of a rs_result value.
Definition: msg.c:46
RS_DELTA_MAGIC
@ RS_DELTA_MAGIC
A delta file.
Definition: librsync.h:71
rs_stats::lit_cmds
int lit_cmds
Number of literal commands.
Definition: librsync.h:213
rs_job::stats
rs_stats_t stats
Encoding statistics.
Definition: job.h:72
rs_stats::lit_bytes
rs_long_t lit_bytes
Number of literal bytes.
Definition: librsync.h:214
RS_CORRUPT
@ RS_CORRUPT
Unbelievable value in stream.
Definition: librsync.h:198
rs_patch_begin
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:251
RS_BLOCKED
@ RS_BLOCKED
Blocked waiting for more data.
Definition: librsync.h:182
rs_scoop_readahead
rs_result rs_scoop_readahead(rs_job_t *job, size_t len, void **ptr)
Read from scoop without advancing.
Definition: scoop.c:148
rs_buffers_s::next_out
char * next_out
Next output byte should be put there.
Definition: librsync.h:345
rs_buffers_s::avail_out
size_t avail_out
Remaining free space at next_out.
Definition: librsync.h:351
rs_copy_cb
rs_result rs_copy_cb(void *opaque, rs_long_t pos, size_t *len, void **buf)
Callback used to retrieve parts of the basis file.
Definition: librsync.h:493
prototab.h
rs_stats
Performance statistics from a librsync encoding or decoding operation.
Definition: librsync.h:210
RS_BAD_MAGIC
@ RS_BAD_MAGIC
Bad magic number at start of stream.
Definition: librsync.h:193
rs_job::copy_cb
rs_copy_cb * copy_cb
Callback used to copy data from the basis into the output.
Definition: job.h:95
rs_stats::lit_cmdbytes
rs_long_t lit_cmdbytes
Number of bytes used in literal command headers.
Definition: librsync.h:215
rs_tube_copy
void rs_tube_copy(rs_job_t *job, int len)
Queue up a request to copy through len bytes from the input to the output of the stream.
Definition: tube.c:178
rs_buffers_s
Description of input and output buffers.
Definition: librsync.h:322