librsync  2.3.2
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  job->cmd = &rs_prototab[job->op];
54  rs_trace("got command %#04x (%s), len_1=%d, len_2=%d", job->op,
55  rs_op_kind_name(job->cmd->kind), job->cmd->len_1, job->cmd->len_2);
56  if (job->cmd->len_1)
57  job->statefn = rs_patch_s_params;
58  else {
59  job->param1 = job->cmd->immediate;
60  job->statefn = rs_patch_s_run;
61  }
62  return RS_RUNNING;
63 }
64 
65 /** Called after reading a command byte to pull in its parameters and then
66  * setup to execute the command. */
67 static rs_result rs_patch_s_params(rs_job_t *job)
68 {
69  rs_result result;
70  const size_t len = (size_t)(job->cmd->len_1 + job->cmd->len_2);
71  void *p;
72 
73  assert(len);
74  result = rs_scoop_readahead(job, len, &p);
75  if (result != RS_DONE)
76  return result;
77  /* we now must have LEN bytes buffered */
78  result = rs_suck_netint(job, &job->param1, job->cmd->len_1);
79  /* shouldn't fail, since we already checked */
80  assert(result == RS_DONE);
81  if (job->cmd->len_2) {
82  result = rs_suck_netint(job, &job->param2, job->cmd->len_2);
83  assert(result == RS_DONE);
84  }
85  job->statefn = rs_patch_s_run;
86  return RS_RUNNING;
87 }
88 
89 /** Called when we've read in the whole command and we need to execute it. */
90 static rs_result rs_patch_s_run(rs_job_t *job)
91 {
92  rs_trace("running command %#04x", job->op);
93  switch (job->cmd->kind) {
94  case RS_KIND_LITERAL:
95  job->statefn = rs_patch_s_literal;
96  return RS_RUNNING;
97  case RS_KIND_END:
98  return RS_DONE;
99  /* so we exit here; trying to continue causes an error */
100  case RS_KIND_COPY:
101  job->statefn = rs_patch_s_copy;
102  return RS_RUNNING;
103  default:
104  rs_error("bogus command %#04x", job->op);
105  return RS_CORRUPT;
106  }
107 }
108 
109 /** Called when trying to copy through literal data. */
110 static rs_result rs_patch_s_literal(rs_job_t *job)
111 {
112  const rs_long_t len = job->param1;
113  rs_stats_t *stats = &job->stats;
114 
115  rs_trace("LITERAL(length=" FMT_LONG ")", len);
116  if (len <= 0 || len > SIZE_MAX) {
117  rs_error("invalid length=" FMT_LONG " on LITERAL command", len);
118  return RS_CORRUPT;
119  }
120  stats->lit_cmds++;
121  stats->lit_bytes += len;
122  stats->lit_cmdbytes += 1 + job->cmd->len_1;
123  rs_tube_copy(job, (size_t)len);
124  job->statefn = rs_patch_s_cmdbyte;
125  return RS_RUNNING;
126 }
127 
128 static rs_result rs_patch_s_copy(rs_job_t *job)
129 {
130  const rs_long_t pos = job->param1;
131  const rs_long_t len = job->param2;
132  rs_stats_t *stats = &job->stats;
133 
134  rs_trace("COPY(position=" FMT_LONG ", length=" FMT_LONG ")", pos, len);
135  if (len <= 0) {
136  rs_error("invalid length=" FMT_LONG " on COPY command", len);
137  return RS_CORRUPT;
138  }
139  if (pos < 0) {
140  rs_error("invalid position=" FMT_LONG " on COPY command", pos);
141  return RS_CORRUPT;
142  }
143  stats->copy_cmds++;
144  stats->copy_bytes += len;
145  stats->copy_cmdbytes += 1 + job->cmd->len_1 + job->cmd->len_2;
146  job->basis_pos = pos;
147  job->basis_len = len;
148  job->statefn = rs_patch_s_copying;
149  return RS_RUNNING;
150 }
151 
152 /** Called when we're executing a COPY command and waiting for all the data to
153  * be retrieved from the callback. */
154 static rs_result rs_patch_s_copying(rs_job_t *job)
155 {
156  rs_result result;
157  rs_buffers_t *buffs = job->stream;
158  rs_long_t req = job->basis_len;
159  size_t len = buffs->avail_out;
160  void *ptr = buffs->next_out;
161 
162  /* We are blocked if there is no space left to copy into. */
163  if (!len)
164  return RS_BLOCKED;
165  /* Adjust request to min of amount requested and space available. */
166  if (len < req)
167  req = (rs_long_t)len;
168  rs_trace("copy " FMT_LONG " bytes from basis at offset " FMT_LONG "", req,
169  job->basis_pos);
170  len = (size_t)req;
171  result = (job->copy_cb) (job->copy_arg, job->basis_pos, &len, &ptr);
172  if (result != RS_DONE) {
173  rs_trace("copy callback returned %s", rs_strerror(result));
174  return result;
175  }
176  rs_trace("got " FMT_SIZE " bytes back from basis callback", len);
177  /* Actual copied length cannot be greater than requested length. */
178  assert(len <= req);
179  /* Backwards-compatible defensively handle this for NDEBUG builds. */
180  if (len > req) {
181  rs_warn("copy_cb() returned more than the requested length");
182  len = (size_t)req;
183  }
184  /* copy back to out buffer only if the callback has used its own buffer */
185  if (ptr != buffs->next_out)
186  memcpy(buffs->next_out, ptr, len);
187  /* Update buffs and copy for copied data. */
188  buffs->next_out += len;
189  buffs->avail_out -= len;
190  job->basis_pos += (rs_long_t)len;
191  job->basis_len -= (rs_long_t)len;
192  if (!job->basis_len) {
193  /* Nothing left to copy, we are done! */
194  job->statefn = rs_patch_s_cmdbyte;
195  }
196  return RS_RUNNING;
197 }
198 
199 /** Called while we're trying to read the header of the patch. */
200 static rs_result rs_patch_s_header(rs_job_t *job)
201 {
202  int v;
203  rs_result result;
204 
205  if ((result = rs_suck_n4(job, &v)) != RS_DONE)
206  return result;
207  if (v != RS_DELTA_MAGIC) {
208  rs_error("got magic number %#x rather than expected value %#x", v,
210  return RS_BAD_MAGIC;
211  } else
212  rs_trace("got patch magic %#x", v);
213  job->statefn = rs_patch_s_cmdbyte;
214  return RS_RUNNING;
215 }
216 
217 rs_job_t *rs_patch_begin(rs_copy_cb * copy_cb, void *copy_arg)
218 {
219  rs_job_t *job = rs_job_new("patch", rs_patch_s_header);
220 
221  job->copy_cb = copy_cb;
222  job->copy_arg = copy_arg;
223  rs_mdfour_begin(&job->output_md4);
224  return job;
225 }
Types of commands present in the encoding stream.
Public header for librsync.
LIBRSYNC_EXPORT char const * rs_strerror(rs_result r)
Return an English description of a rs_result value.
Definition: msg.c:46
rs_result
Return codes from nonblocking rsync operations.
Definition: librsync.h:180
@ RS_RUNNING
The job is still running, and not yet finished or blocked.
Definition: librsync.h:183
@ RS_DONE
Completed successfully.
Definition: librsync.h:181
@ RS_CORRUPT
Unbelievable value in stream.
Definition: librsync.h:198
@ RS_BAD_MAGIC
Bad magic number at start of stream.
Definition: librsync.h:193
@ RS_BLOCKED
Blocked waiting for more data.
Definition: librsync.h:182
@ RS_DELTA_MAGIC
A delta file.
Definition: librsync.h:71
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:217
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
Delta file commands.
rs_result rs_scoop_readahead(rs_job_t *job, size_t len, void **ptr)
Read from scoop without advancing.
Definition: scoop.c:148
Manage librsync streams of IO.
void rs_tube_copy(rs_job_t *job, size_t len)
Queue up a request to copy through len bytes from the input to the output of the stream.
Definition: tube.c:200
Description of input and output buffers.
Definition: librsync.h:322
size_t avail_out
Remaining free space at next_out.
Definition: librsync.h:351
char * next_out
Next output byte should be put there.
Definition: librsync.h:345
The contents of this structure are private.
Definition: job.h:26
rs_long_t basis_pos
Copy from the basis position.
Definition: job.h:92
unsigned char op
Command byte currently being processed, if any.
Definition: job.h:57
rs_copy_cb * copy_cb
Callback used to copy data from the basis into the output.
Definition: job.h:95
rs_result(* statefn)(rs_job_t *)
Callback for each processing step.
Definition: job.h:35
rs_stats_t stats
Encoding statistics.
Definition: job.h:72
rs_long_t param1
Lengths of expected parameters.
Definition: job.h:66
Performance statistics from a librsync encoding or decoding operation.
Definition: librsync.h:210
int lit_cmds
Number of literal commands.
Definition: librsync.h:213
rs_long_t lit_cmdbytes
Number of bytes used in literal command headers.
Definition: librsync.h:215
rs_long_t lit_bytes
Number of literal bytes.
Definition: librsync.h:214
logging functions.