Some misc. improvements (I hope).
[rsync/rsync.git] / sender.c
... / ...
CommitLineData
1/*
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4
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.
9
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.
14
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;
23extern int dry_run;
24extern int am_server;
25extern int am_daemon;
26extern int log_before_transfer;
27extern int log_format_has_i;
28extern int daemon_log_format_has_i;
29extern int csum_length;
30extern int io_error;
31extern int protocol_version;
32extern int remove_sent_files;
33extern int updating_basis_file;
34extern int make_backups;
35extern int do_progress;
36extern int inplace;
37extern struct stats stats;
38extern struct file_list *the_file_list;
39extern char *log_format;
40
41
42/**
43 * @file
44 *
45 * The sender gets checksums from the generator, calculates deltas,
46 * and transmits them to the receiver. The sender process runs on the
47 * machine holding the source files.
48 **/
49
50/**
51 * Receive the checksums for a buffer
52 **/
53static struct sum_struct *receive_sums(int f)
54{
55 struct sum_struct *s;
56 int32 i;
57 OFF_T offset = 0;
58
59 if (!(s = new(struct sum_struct)))
60 out_of_memory("receive_sums");
61
62 read_sum_head(f, s);
63
64 s->sums = NULL;
65
66 if (verbose > 3) {
67 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
68 (double)s->count, (long)s->blength, (long)s->remainder);
69 }
70
71 if (s->count == 0)
72 return(s);
73
74 if (!(s->sums = new_array(struct sum_buf, s->count)))
75 out_of_memory("receive_sums");
76
77 for (i = 0; i < s->count; i++) {
78 s->sums[i].sum1 = read_int(f);
79 read_buf(f, s->sums[i].sum2, s->s2length);
80
81 s->sums[i].offset = offset;
82 s->sums[i].flags = 0;
83
84 if (i == s->count-1 && s->remainder != 0)
85 s->sums[i].len = s->remainder;
86 else
87 s->sums[i].len = s->blength;
88 offset += s->sums[i].len;
89
90 if (verbose > 3) {
91 rprintf(FINFO,
92 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
93 i, s->sums[i].len, (double)s->sums[i].offset,
94 s->sums[i].sum1);
95 }
96 }
97
98 s->flength = offset;
99
100 return s;
101}
102
103void successful_send(int ndx)
104{
105 char fname[MAXPATHLEN];
106 struct file_struct *file;
107 unsigned int offset;
108
109 if (ndx < 0 || ndx >= the_file_list->count)
110 return;
111
112 file = the_file_list->files[ndx];
113 /* The generator might tell us about symlinks we didn't send. */
114 if (!(file->flags & FLAG_SENT) && !S_ISLNK(file->mode))
115 return;
116 if (file->dir.root) {
117 offset = stringjoin(fname, sizeof fname,
118 file->dir.root, "/", NULL);
119 } else
120 offset = 0;
121 f_name_to(file, fname + offset);
122 if (remove_sent_files && do_unlink(fname) == 0 && verbose > 1) {
123 rprintf(FINFO, "sender removed %s\n",
124 safe_fname(fname + offset));
125 }
126}
127
128static void write_item_attrs(int f_out, int ndx, int iflags,
129 uchar fnamecmp_type, char *buf, int len)
130{
131 write_int(f_out, ndx);
132 if (protocol_version < 29)
133 return;
134 write_shortint(f_out, iflags);
135 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
136 write_byte(f_out, fnamecmp_type);
137 if (iflags & ITEM_XNAME_FOLLOWS) {
138 if (len < 0)
139 len = strlen(buf);
140 write_vstring(f_out, buf, len);
141 }
142}
143
144/* This is also used by receive.c with f_out = -1. */
145int read_item_attrs(int f_in, int f_out, int ndx, uchar *type_ptr,
146 char *buf, int *len_ptr)
147{
148 int len;
149 uchar fnamecmp_type = FNAMECMP_FNAME;
150 int iflags = protocol_version >= 29 ? read_shortint(f_in)
151 : ITEM_TRANSFER | ITEM_MISSING_DATA;
152 int isave = iflags; /* XXX remove soon */
153
154 /* Handle the new keep-alive (no-op) packet. */
155 if (ndx == the_file_list->count && iflags == ITEM_IS_NEW)
156 ;
157 else if (ndx < 0 || ndx >= the_file_list->count) {
158 rprintf(FERROR, "Invalid file index %d (count=%d) [%s]\n",
159 ndx, the_file_list->count, who_am_i());
160 exit_cleanup(RERR_PROTOCOL);
161 } else if (iflags == ITEM_IS_NEW) {
162 rprintf(FERROR, "Invalid itemized flag word [%s]\n",
163 who_am_i());
164 exit_cleanup(RERR_PROTOCOL);
165 }
166
167 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
168 fnamecmp_type = read_byte(f_in);
169 *type_ptr = fnamecmp_type;
170
171 if (iflags & ITEM_XNAME_FOLLOWS) {
172 if ((len = read_vstring(f_in, buf, MAXPATHLEN)) < 0)
173 exit_cleanup(RERR_PROTOCOL);
174 } else {
175 *buf = '\0';
176 len = -1;
177 }
178 *len_ptr = len;
179
180 /* XXX Temporary backward compatibility when talking to 2.6.4pre[12] */
181 if (protocol_version >= 29 && iflags & ITEM_TRANSFER
182 && !S_ISREG(the_file_list->files[ndx]->mode)) {
183 iflags &= ~ITEM_TRANSFER;
184 iflags |= ITEM_LOCAL_CHANGE;
185 }
186
187 if (iflags & ITEM_TRANSFER) {
188 if (!S_ISREG(the_file_list->files[ndx]->mode)) {
189 rprintf(FERROR,
190 "received index of non-regular file: %d [%s]\n",
191 ndx, who_am_i());
192 exit_cleanup(RERR_PROTOCOL);
193 }
194 } else if (f_out >= 0) {
195 write_item_attrs(f_out, ndx, isave /*XXX iflags */,
196 fnamecmp_type, buf, len);
197 }
198
199 return iflags;
200}
201
202void send_files(struct file_list *flist, int f_out, int f_in)
203{
204 int fd = -1;
205 struct sum_struct *s;
206 struct map_struct *mbuf = NULL;
207 STRUCT_STAT st;
208 char *fname2, fname[MAXPATHLEN];
209 char xname[MAXPATHLEN];
210 uchar fnamecmp_type;
211 int iflags, xlen;
212 struct file_struct *file;
213 int phase = 0;
214 struct stats initial_stats;
215 int save_make_backups = make_backups;
216 int itemizing = am_daemon ? daemon_log_format_has_i
217 : !am_server && log_format_has_i;
218 int i, j;
219
220 if (verbose > 2)
221 rprintf(FINFO, "send_files starting\n");
222
223 while (1) {
224 unsigned int offset;
225
226 i = read_int(f_in);
227 if (i == -1) {
228 if (phase == 0) {
229 phase++;
230 csum_length = SUM_LENGTH;
231 write_int(f_out, -1);
232 if (verbose > 2)
233 rprintf(FINFO, "send_files phase=%d\n", phase);
234 /* For inplace: redo phase turns off the backup
235 * flag so that we do a regular inplace send. */
236 make_backups = 0;
237 continue;
238 }
239 break;
240 }
241
242 iflags = read_item_attrs(f_in, f_out, i, &fnamecmp_type,
243 xname, &xlen);
244 if (iflags == ITEM_IS_NEW) /* no-op packet */
245 continue;
246
247 file = flist->files[i];
248 if (file->dir.root) {
249 /* N.B. We're sure that this fits, so offset is OK. */
250 offset = strlcpy(fname, file->dir.root, sizeof fname);
251 if (!offset || fname[offset-1] != '/')
252 fname[offset++] = '/';
253 } else
254 offset = 0;
255 fname2 = f_name_to(file, fname + offset);
256
257 if (verbose > 2)
258 rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
259
260 if (!(iflags & ITEM_TRANSFER)) {
261 maybe_log_item(file, iflags, itemizing, xname);
262 continue;
263 }
264
265 updating_basis_file = inplace && (protocol_version >= 29
266 ? fnamecmp_type == FNAMECMP_FNAME : !make_backups);
267
268 stats.current_file_index = i;
269 stats.num_transferred_files++;
270 stats.total_transferred_size += file->length;
271
272 if (dry_run) { /* log the transfer */
273 if (!am_server && log_format)
274 log_item(file, &stats, iflags, NULL);
275 write_item_attrs(f_out, i, iflags, fnamecmp_type,
276 xname, xlen);
277 continue;
278 }
279
280 initial_stats = stats;
281
282 if (!(s = receive_sums(f_in))) {
283 io_error |= IOERR_GENERAL;
284 rprintf(FERROR, "receive_sums failed\n");
285 return;
286 }
287
288 fd = do_open(fname, O_RDONLY, 0);
289 if (fd == -1) {
290 if (errno == ENOENT) {
291 enum logcode c = am_daemon
292 && protocol_version < 28 ? FERROR
293 : FINFO;
294 io_error |= IOERR_VANISHED;
295 rprintf(c, "file has vanished: %s\n",
296 full_fname(fname));
297 } else {
298 io_error |= IOERR_GENERAL;
299 rsyserr(FERROR, errno,
300 "send_files failed to open %s",
301 full_fname(fname));
302 }
303 free_sums(s);
304 continue;
305 }
306
307 /* map the local file */
308 if (do_fstat(fd, &st) != 0) {
309 io_error |= IOERR_GENERAL;
310 rsyserr(FERROR, errno, "fstat failed");
311 free_sums(s);
312 close(fd);
313 return;
314 }
315
316 if (st.st_size) {
317 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
318 mbuf = map_file(fd, st.st_size, read_size, s->blength);
319 } else
320 mbuf = NULL;
321
322 if (verbose > 2) {
323 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
324 safe_fname(fname), (double)st.st_size);
325 }
326
327 write_item_attrs(f_out, i, iflags, fnamecmp_type, xname, xlen);
328 write_sum_head(f_out, s);
329
330 if (verbose > 2) {
331 rprintf(FINFO, "calling match_sums %s\n",
332 safe_fname(fname));
333 }
334
335 if (log_before_transfer)
336 log_item(file, &initial_stats, iflags, NULL);
337 else if (!am_server && verbose && do_progress)
338 rprintf(FINFO, "%s\n", safe_fname(fname2));
339
340 set_compression(fname);
341
342 match_sums(f_out, s, mbuf, st.st_size);
343 if (do_progress)
344 end_progress(st.st_size);
345
346 if (!log_before_transfer)
347 log_item(file, &initial_stats, iflags, NULL);
348
349 if (mbuf) {
350 j = unmap_file(mbuf);
351 if (j) {
352 io_error |= IOERR_GENERAL;
353 rsyserr(FERROR, j,
354 "read errors mapping %s",
355 full_fname(fname));
356 }
357 }
358 close(fd);
359
360 free_sums(s);
361
362 if (verbose > 2) {
363 rprintf(FINFO, "sender finished %s\n",
364 safe_fname(fname));
365 }
366
367 /* Flag that we actually sent this entry. */
368 file->flags |= FLAG_SENT;
369 }
370 make_backups = save_make_backups;
371
372 if (verbose > 2)
373 rprintf(FINFO, "send files finished\n");
374
375 match_report();
376
377 write_int(f_out, -1);
378}