librsync  2.0.1
emit.c
1 /*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
2  *
3  * librsync -- dynamic caching and delta update in HTTP
4  *
5  * Copyright (C) 2000, 2001, 2004 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 /*
24  * TODO: Pluggable encoding formats:
25  *
26  * - gdiff-style
27  * - rsync 24
28  * - ed (text)
29  * - Delta HTTP
30  */
31 
32 
33 #include "config.h"
34 
35 #include <assert.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 
39 #include "librsync.h"
40 #include "command.h"
41 #include "trace.h"
42 #include "emit.h"
43 #include "prototab.h"
44 #include "netint.h"
45 #include "sumset.h"
46 #include "job.h"
47 
48 
49 /*
50  * Write the magic for the start of a delta.
51  */
52 void
53 rs_emit_delta_header(rs_job_t *job)
54 {
55  rs_trace("emit DELTA magic");
56  rs_squirt_n4(job, RS_DELTA_MAGIC);
57 }
58 
59 
60 
61 /* Write a LITERAL command. */
62 void
63 rs_emit_literal_cmd(rs_job_t *job, int len)
64 {
65  int cmd;
66  int param_len;
67 
68  switch (param_len = rs_int_len(len)) {
69  case 1:
70  cmd = RS_OP_LITERAL_N1;
71  break;
72  case 2:
73  cmd = RS_OP_LITERAL_N2;
74  break;
75  case 4:
76  cmd = RS_OP_LITERAL_N4;
77  break;
78  default:
79  rs_fatal("What?");
80  }
81 
82  rs_trace("emit LITERAL_N%d(len=%d), cmd_byte=%#x", param_len, len, cmd);
83  rs_squirt_byte(job, cmd);
84  rs_squirt_netint(job, len, param_len);
85 
86  job->stats.lit_cmds++;
87  job->stats.lit_bytes += len;
88  job->stats.lit_cmdbytes += 1 + param_len;
89 }
90 
91 
92 /** Write a COPY command for given offset and length.
93  *
94  * There is a choice of variable-length encodings, depending on the
95  * size of representation for the parameters. */
96 void
97 rs_emit_copy_cmd(rs_job_t *job, rs_long_t where, rs_long_t len)
98 {
99  int cmd;
100  rs_stats_t *stats = &job->stats;
101  const int where_bytes = rs_int_len(where);
102  const int len_bytes = rs_int_len(len);
103 
104  /* Commands ascend (1,1), (1,2), ... (8, 8) */
105  if (where_bytes == 8)
106  cmd = RS_OP_COPY_N8_N1;
107  else if (where_bytes == 4)
108  cmd = RS_OP_COPY_N4_N1;
109  else if (where_bytes == 2)
110  cmd = RS_OP_COPY_N2_N1;
111  else if (where_bytes == 1)
112  cmd = RS_OP_COPY_N1_N1;
113  else {
114  rs_fatal("can't encode copy command with where_bytes=%d",
115  where_bytes);
116  }
117 
118  if (len_bytes == 1)
119  ;
120  else if (len_bytes == 2)
121  cmd += 1;
122  else if (len_bytes == 4)
123  cmd += 2;
124  else if (len_bytes == 8)
125  cmd += 3;
126  else {
127  rs_fatal("can't encode copy command with len_bytes=%d",
128  len_bytes);
129  }
130 
131  rs_trace("emit COPY_N%d_N%d(where=" PRINTF_FORMAT_U64
132  ", len=" PRINTF_FORMAT_U64 "), cmd_byte=%#x",
133  where_bytes, len_bytes, PRINTF_CAST_U64(where), PRINTF_CAST_U64(len), cmd);
134  rs_squirt_byte(job, cmd);
135  rs_squirt_netint(job, where, where_bytes);
136  rs_squirt_netint(job, len, len_bytes);
137 
138  stats->copy_cmds++;
139  stats->copy_bytes += len;
140  stats->copy_cmdbytes += 1 + where_bytes + len_bytes;
141 
142  /* TODO: All the stats */
143 }
144 
145 
146 /** Write an END command. */
147 void
148 rs_emit_end_cmd(rs_job_t *job)
149 {
150  int cmd = RS_OP_END;
151 
152  rs_trace("emit END, cmd_byte=%#x", cmd);
153  rs_squirt_byte(job, cmd);
154 }
rs_long_t lit_cmdbytes
Number of bytes used in literal command headers.
Definition: librsync.h:241
long long rs_long_t
A long integer type that can handle the largest file offsets.
A delta file.
Definition: librsync.h:84
rs_stats_t stats
Encoding statistics.
Definition: job.h:75
Public header for librsync.
int lit_cmds
Number of literal commands.
Definition: librsync.h:239
rs_long_t lit_bytes
Number of literal bytes.
Definition: librsync.h:240
Performance statistics from a librsync encoding or decoding operation.
Definition: librsync.h:236
The contents of this structure are private.
Definition: job.h:29