- Fixed a problem with the setting of the --recurse option from the
[rsync/rsync.git] / sender.c
CommitLineData
e6e3f12f 1/*
2f03f956
AT
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
e6e3f12f 4
2f03f956
AT
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
e6e3f12f 9
2f03f956
AT
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
e6e3f12f 14
2f03f956
AT
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20#include "rsync.h"
21
22extern int verbose;
2f03f956
AT
23extern int csum_length;
24extern struct stats stats;
25extern int io_error;
26extern int dry_run;
27extern int am_server;
d6631cf3
WD
28extern int am_daemon;
29extern int protocol_version;
53f8519a 30extern int updating_basis_file;
8b115ac8 31extern int make_backups;
53f8519a 32extern int inplace;
a3221d2a 33extern struct stats stats;
2f03f956
AT
34
35
0f9c48b1
MP
36/**
37 * @file
38 *
39 * The sender gets checksums from the generator, calculates deltas,
40 * and transmits them to the receiver. The sender process runs on the
41 * machine holding the source files.
42 **/
fc0257c9 43
ce8149b6
MP
44/**
45 * Receive the checksums for a buffer
46 **/
2f03f956
AT
47static struct sum_struct *receive_sums(int f)
48{
49 struct sum_struct *s;
50 int i;
51 OFF_T offset = 0;
52
a3221d2a
WD
53 if (!(s = new(struct sum_struct)))
54 out_of_memory("receive_sums");
2f03f956 55
fc0257c9
S
56 read_sum_head(f, s);
57
2f03f956
AT
58 s->sums = NULL;
59
da9d12f5 60 if (verbose > 3) {
6c495e0d
WD
61 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
62 (double)s->count, (long)s->blength, (long)s->remainder);
da9d12f5 63 }
2f03f956 64
e6e3f12f 65 if (s->count == 0)
2f03f956
AT
66 return(s);
67
a3221d2a
WD
68 if (!(s->sums = new_array(struct sum_buf, s->count)))
69 out_of_memory("receive_sums");
2f03f956 70
a3221d2a 71 for (i = 0; i < (int)s->count; i++) {
2f03f956 72 s->sums[i].sum1 = read_int(f);
e6e3f12f 73 read_buf(f, s->sums[i].sum2, s->s2length);
2f03f956
AT
74
75 s->sums[i].offset = offset;
a3221d2a 76 s->sums[i].flags = 0;
2f03f956 77
a3221d2a 78 if (i == (int)s->count-1 && s->remainder != 0)
2f03f956 79 s->sums[i].len = s->remainder;
a3221d2a 80 else
fc0257c9 81 s->sums[i].len = s->blength;
2f03f956
AT
82 offset += s->sums[i].len;
83
a3221d2a
WD
84 if (verbose > 3) {
85 rprintf(FINFO,
86 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
87 i, s->sums[i].len, (double)s->sums[i].offset,
88 s->sums[i].sum1);
89 }
2f03f956
AT
90 }
91
92 s->flength = offset;
93
94 return s;
95}
96
97
98
e6e3f12f
S
99void send_files(struct file_list *flist, int f_out, int f_in)
100{
c1659c79 101 int fd = -1;
2f03f956 102 struct sum_struct *s;
41cc97ae 103 struct map_struct *mbuf = NULL;
2f03f956 104 STRUCT_STAT st;
ecc81fce 105 char *fname2, fname[MAXPATHLEN];
2f03f956
AT
106 int i;
107 struct file_struct *file;
108 int phase = 0;
1b7c47cb 109 struct stats initial_stats;
8b115ac8 110 int save_make_backups = make_backups;
64c3523a 111 int j;
2f03f956
AT
112
113 if (verbose > 2)
e6e3f12f 114 rprintf(FINFO, "send_files starting\n");
2f03f956 115
2f03f956 116 while (1) {
3fef5364 117 unsigned int offset;
2f03f956
AT
118
119 i = read_int(f_in);
120 if (i == -1) {
e6e3f12f 121 if (phase == 0) {
2f03f956
AT
122 phase++;
123 csum_length = SUM_LENGTH;
e6e3f12f 124 write_int(f_out, -1);
2f03f956 125 if (verbose > 2)
e6e3f12f 126 rprintf(FINFO, "send_files phase=%d\n", phase);
9715c589
WD
127 /* For inplace: redo phase turns off the backup
128 * flag so that we do a regular inplace send. */
8b115ac8 129 make_backups = 0;
2f03f956
AT
130 continue;
131 }
132 break;
133 }
134
135 if (i < 0 || i >= flist->count) {
e6e3f12f 136 rprintf(FERROR, "Invalid file index %d (count=%d)\n",
2f03f956 137 i, flist->count);
65417579 138 exit_cleanup(RERR_PROTOCOL);
2f03f956
AT
139 }
140
b951e023
WD
141 if (inplace && protocol_version >= 29) {
142 uchar fnamecmp_type = read_byte(f_in);
143 updating_basis_file = fnamecmp_type == FNAMECMP_FNAME;
144 } else
145 updating_basis_file = inplace && !make_backups;
146
2f03f956
AT
147 file = flist->files[i];
148
97feb557 149 stats.current_file_index = i;
2f03f956
AT
150 stats.num_transferred_files++;
151 stats.total_transferred_size += file->length;
152
2b136663 153 if (file->dir.root) {
421c2a24 154 /* N.B. We're sure that this fits, so offset is OK. */
2b136663 155 offset = strlcpy(fname, file->dir.root, sizeof fname);
421c2a24
WD
156 if (!offset || fname[offset-1] != '/')
157 fname[offset++] = '/';
3fef5364
WD
158 } else
159 offset = 0;
ecc81fce 160 fname2 = f_name_to(file, fname + offset);
e6e3f12f
S
161
162 if (verbose > 2)
163 rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
164
165 if (dry_run) {
ecc81fce
WD
166 if (!am_server && verbose) /* log the transfer */
167 rprintf(FINFO, "%s\n", safe_fname(fname2));
e6e3f12f 168 write_int(f_out, i);
2f03f956
AT
169 continue;
170 }
171
1b7c47cb
AT
172 initial_stats = stats;
173
efa95a18 174 if (!(s = receive_sums(f_in))) {
06c28400 175 io_error |= IOERR_GENERAL;
e6e3f12f 176 rprintf(FERROR, "receive_sums failed\n");
2f03f956
AT
177 return;
178 }
76f79ba7 179
b9f592fb
WD
180 fd = do_open(fname, O_RDONLY, 0);
181 if (fd == -1) {
182 if (errno == ENOENT) {
183 enum logcode c = am_daemon
184 && protocol_version < 28 ? FERROR
185 : FINFO;
186 io_error |= IOERR_VANISHED;
187 rprintf(c, "file has vanished: %s\n",
188 full_fname(fname));
189 } else {
06c28400 190 io_error |= IOERR_GENERAL;
b9f592fb
WD
191 rsyserr(FERROR, errno,
192 "send_files failed to open %s",
193 full_fname(fname));
41cc97ae 194 }
b9f592fb
WD
195 free_sums(s);
196 continue;
197 }
e6e3f12f 198
b9f592fb
WD
199 /* map the local file */
200 if (do_fstat(fd, &st) != 0) {
201 io_error |= IOERR_GENERAL;
202 rsyserr(FERROR, errno, "fstat failed");
203 free_sums(s);
204 close(fd);
205 return;
206 }
11a5a3c7 207
96d910c7 208 if (st.st_size) {
9b919d59
WD
209 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
210 mbuf = map_file(fd, st.st_size, read_size, s->blength);
96d910c7
WD
211 } else
212 mbuf = NULL;
6902ed17 213
b9f592fb
WD
214 if (verbose > 2) {
215 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
ecc81fce 216 safe_fname(fname), (double)st.st_size);
6902ed17 217 }
e6e3f12f 218
b9f592fb
WD
219 write_int(f_out, i);
220 write_sum_head(f_out, s);
221
ecc81fce
WD
222 if (verbose > 2) {
223 rprintf(FINFO, "calling match_sums %s\n",
224 safe_fname(fname));
11a5a3c7 225 }
83fff1aa 226
ecc81fce
WD
227 if (!am_server && verbose) /* log the transfer */
228 rprintf(FINFO, "%s\n", safe_fname(fname2));
229
83fff1aa 230 set_compression(fname);
1b7c47cb 231
b9f592fb
WD
232 match_sums(f_out, s, mbuf, st.st_size);
233 log_send(file, &initial_stats);
6902ed17 234
b9f592fb
WD
235 if (mbuf) {
236 j = unmap_file(mbuf);
237 if (j) {
238 io_error |= IOERR_GENERAL;
239 rsyserr(FERROR, j,
240 "read errors mapping %s",
241 full_fname(fname));
6a7cc46c 242 }
6902ed17 243 }
b9f592fb 244 close(fd);
e6e3f12f 245
2f03f956 246 free_sums(s);
e6e3f12f 247
ecc81fce
WD
248 if (verbose > 2) {
249 rprintf(FINFO, "sender finished %s\n",
250 safe_fname(fname));
251 }
2f03f956 252 }
8b115ac8 253 make_backups = save_make_backups;
2f03f956
AT
254
255 if (verbose > 2)
e6e3f12f 256 rprintf(FINFO, "send files finished\n");
2f03f956
AT
257
258 match_report();
259
e6e3f12f 260 write_int(f_out, -1);
2f03f956 261}