Fix %b and %c so that they count per-transfer bytes again.
[rsync/rsync.git] / sender.c
1 /*
2  * Routines only used by the sending process.
3  *
4  * Copyright (C) 1996 Andrew Tridgell
5  * Copyright (C) 1996 Paul Mackerras
6  * Copyright (C) 2003-2009 Wayne Davison
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, visit the http://fsf.org website.
20  */
21
22 #include "rsync.h"
23 #include "inums.h"
24
25 extern int do_xfers;
26 extern int am_server;
27 extern int am_daemon;
28 extern int inc_recurse;
29 extern int log_before_transfer;
30 extern int stdout_format_has_i;
31 extern int logfile_format_has_i;
32 extern int csum_length;
33 extern int append_mode;
34 extern int io_error;
35 extern int flist_eof;
36 extern int allowed_lull;
37 extern int preserve_xattrs;
38 extern int protocol_version;
39 extern int remove_source_files;
40 extern int updating_basis_file;
41 extern int make_backups;
42 extern int inplace;
43 extern int batch_fd;
44 extern int write_batch;
45 extern struct stats stats;
46 extern struct file_list *cur_flist, *first_flist, *dir_flist;
47
48 BOOL extra_flist_sending_enabled;
49
50 /**
51  * @file
52  *
53  * The sender gets checksums from the generator, calculates deltas,
54  * and transmits them to the receiver.  The sender process runs on the
55  * machine holding the source files.
56  **/
57
58 /**
59  * Receive the checksums for a buffer
60  **/
61 static struct sum_struct *receive_sums(int f)
62 {
63         struct sum_struct *s;
64         int32 i;
65         int lull_mod = allowed_lull * 5;
66         OFF_T offset = 0;
67
68         if (!(s = new(struct sum_struct)))
69                 out_of_memory("receive_sums");
70
71         read_sum_head(f, s);
72
73         s->sums = NULL;
74
75         if (DEBUG_GTE(DELTASUM, 3)) {
76                 rprintf(FINFO, "count=%s n=%ld rem=%ld\n",
77                         big_num(s->count), (long)s->blength, (long)s->remainder);
78         }
79
80         if (append_mode > 0) {
81                 s->flength = (OFF_T)s->count * s->blength;
82                 if (s->remainder)
83                         s->flength -= s->blength - s->remainder;
84                 return s;
85         }
86
87         if (s->count == 0)
88                 return(s);
89
90         if (!(s->sums = new_array(struct sum_buf, s->count)))
91                 out_of_memory("receive_sums");
92
93         for (i = 0; i < s->count; i++) {
94                 s->sums[i].sum1 = read_int(f);
95                 read_buf(f, s->sums[i].sum2, s->s2length);
96
97                 s->sums[i].offset = offset;
98                 s->sums[i].flags = 0;
99
100                 if (i == s->count-1 && s->remainder != 0)
101                         s->sums[i].len = s->remainder;
102                 else
103                         s->sums[i].len = s->blength;
104                 offset += s->sums[i].len;
105
106                 if (allowed_lull && !(i % lull_mod))
107                         maybe_send_keepalive();
108
109                 if (DEBUG_GTE(DELTASUM, 3)) {
110                         rprintf(FINFO,
111                                 "chunk[%d] len=%d offset=%s sum1=%08x\n",
112                                 i, s->sums[i].len, big_num(s->sums[i].offset),
113                                 s->sums[i].sum1);
114                 }
115         }
116
117         s->flength = offset;
118
119         return s;
120 }
121
122 void successful_send(int ndx)
123 {
124         char fname[MAXPATHLEN];
125         struct file_struct *file;
126         struct file_list *flist;
127
128         if (!remove_source_files)
129                 return;
130
131         flist = flist_for_ndx(ndx, "successful_send");
132         file = flist->files[ndx - flist->ndx_start];
133         if (!change_pathname(file, NULL, 0))
134                 return;
135         f_name(file, fname);
136
137         if (do_unlink(fname) == 0) {
138                 if (INFO_GTE(REMOVE, 1))
139                         rprintf(FINFO, "sender removed %s\n", fname);
140         } else
141                 rsyserr(FERROR, errno, "sender failed to remove %s", fname);
142 }
143
144 static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
145                                 const char *fname, struct file_struct *file,
146                                 uchar fnamecmp_type, char *buf, int len)
147 {
148         write_ndx(f_out, ndx);
149         if (protocol_version < 29)
150                 return;
151         write_shortint(f_out, iflags);
152         if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
153                 write_byte(f_out, fnamecmp_type);
154         if (iflags & ITEM_XNAME_FOLLOWS)
155                 write_vstring(f_out, buf, len);
156 #ifdef SUPPORT_XATTRS
157         if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers)
158                 send_xattr_request(fname, file, f_out);
159 #endif
160 }
161
162 void send_files(int f_in, int f_out)
163 {
164         int fd = -1;
165         struct sum_struct *s;
166         struct map_struct *mbuf = NULL;
167         STRUCT_STAT st;
168         char fname[MAXPATHLEN], xname[MAXPATHLEN];
169         const char *path, *slash;
170         uchar fnamecmp_type;
171         int iflags, xlen;
172         struct file_struct *file;
173         int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
174         int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
175         enum logcode log_code = log_before_transfer ? FLOG : FINFO;
176         int f_xfer = write_batch < 0 ? batch_fd : f_out;
177         int save_io_error = io_error;
178         int ndx, j;
179
180         if (DEBUG_GTE(SEND, 1))
181                 rprintf(FINFO, "send_files starting\n");
182
183         while (1) {
184                 if (inc_recurse) {
185                         send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
186                         extra_flist_sending_enabled = !flist_eof;
187                 }
188
189                 /* This call also sets cur_flist. */
190                 ndx = read_ndx_and_attrs(f_in, f_out, &iflags, &fnamecmp_type,
191                                          xname, &xlen);
192                 extra_flist_sending_enabled = False;
193
194                 if (ndx == NDX_DONE) {
195                         if (!am_server && INFO_GTE(PROGRESS, 2) && cur_flist) {
196                                 set_current_file_index(NULL, 0);
197                                 end_progress(0);
198                         }
199                         if (inc_recurse && first_flist) {
200                                 flist_free(first_flist);
201                                 if (first_flist) {
202                                         write_ndx(f_out, NDX_DONE);
203                                         continue;
204                                 }
205                         }
206                         if (++phase > max_phase)
207                                 break;
208                         if (DEBUG_GTE(SEND, 1))
209                                 rprintf(FINFO, "send_files phase=%d\n", phase);
210                         write_ndx(f_out, NDX_DONE);
211                         continue;
212                 }
213
214                 if (inc_recurse)
215                         send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
216
217                 if (ndx - cur_flist->ndx_start >= 0)
218                         file = cur_flist->files[ndx - cur_flist->ndx_start];
219                 else
220                         file = dir_flist->files[cur_flist->parent_ndx];
221                 if (F_PATHNAME(file)) {
222                         path = F_PATHNAME(file);
223                         slash = "/";
224                 } else {
225                         path = slash = "";
226                 }
227                 if (!change_pathname(file, NULL, 0))
228                         continue;
229                 f_name(file, fname);
230
231                 if (DEBUG_GTE(SEND, 1))
232                         rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
233
234 #ifdef SUPPORT_XATTRS
235                 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers)
236                         recv_xattr_request(file, f_in);
237 #endif
238
239                 if (!(iflags & ITEM_TRANSFER)) {
240                         maybe_log_item(file, iflags, itemizing, xname);
241                         write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
242                                             fnamecmp_type, xname, xlen);
243                         if (iflags & ITEM_IS_NEW) {
244                                 stats.created_files++;
245                                 if (S_ISREG(file->mode)) {
246                                         /* Nothing further to count. */
247                                 } else if (S_ISDIR(file->mode))
248                                         stats.created_dirs++;
249 #ifdef SUPPORT_LINKS
250                                 else if (S_ISLNK(file->mode))
251                                         stats.created_symlinks++;
252 #endif
253                                 else if (IS_DEVICE(file->mode))
254                                         stats.created_devices++;
255                                 else
256                                         stats.created_specials++;
257                         }
258                         continue;
259                 }
260                 if (phase == 2) {
261                         rprintf(FERROR,
262                                 "got transfer request in phase 2 [%s]\n",
263                                 who_am_i());
264                         exit_cleanup(RERR_PROTOCOL);
265                 }
266
267                 if (file->flags & FLAG_FILE_SENT) {
268                         if (csum_length == SHORT_SUM_LENGTH) {
269                                 /* For inplace: redo phase turns off the backup
270                                  * flag so that we do a regular inplace send. */
271                                 make_backups = -make_backups;
272                                 append_mode = -append_mode;
273                                 csum_length = SUM_LENGTH;
274                         }
275                 } else {
276                         if (csum_length != SHORT_SUM_LENGTH) {
277                                 make_backups = -make_backups;
278                                 append_mode = -append_mode;
279                                 csum_length = SHORT_SUM_LENGTH;
280                         }
281                         if (iflags & ITEM_IS_NEW)
282                                 stats.created_files++;
283                 }
284
285                 updating_basis_file = inplace && (protocol_version >= 29
286                         ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0);
287
288                 if (!am_server && INFO_GTE(PROGRESS, 1))
289                         set_current_file_index(file, ndx);
290                 stats.xferred_files++;
291                 stats.total_transferred_size += F_LENGTH(file);
292
293                 if (!log_before_transfer)
294                         remember_initial_stats();
295
296                 if (!do_xfers) { /* log the transfer */
297                         log_item(FCLIENT, file, iflags, NULL);
298                         write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
299                                             fnamecmp_type, xname, xlen);
300                         continue;
301                 }
302
303                 if (!(s = receive_sums(f_in))) {
304                         io_error |= IOERR_GENERAL;
305                         rprintf(FERROR_XFER, "receive_sums failed\n");
306                         exit_cleanup(RERR_PROTOCOL);
307                 }
308
309                 fd = do_open(fname, O_RDONLY, 0);
310                 if (fd == -1) {
311                         if (errno == ENOENT) {
312                                 enum logcode c = am_daemon
313                                     && protocol_version < 28 ? FERROR
314                                                              : FWARNING;
315                                 io_error |= IOERR_VANISHED;
316                                 rprintf(c, "file has vanished: %s\n",
317                                         full_fname(fname));
318                         } else {
319                                 io_error |= IOERR_GENERAL;
320                                 rsyserr(FERROR_XFER, errno,
321                                         "send_files failed to open %s",
322                                         full_fname(fname));
323                         }
324                         free_sums(s);
325                         if (protocol_version >= 30)
326                                 send_msg_int(MSG_NO_SEND, ndx);
327                         continue;
328                 }
329
330                 /* map the local file */
331                 if (do_fstat(fd, &st) != 0) {
332                         io_error |= IOERR_GENERAL;
333                         rsyserr(FERROR_XFER, errno, "fstat failed");
334                         free_sums(s);
335                         close(fd);
336                         exit_cleanup(RERR_PROTOCOL);
337                 }
338
339                 if (st.st_size) {
340                         int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
341                         mbuf = map_file(fd, st.st_size, read_size, s->blength);
342                 } else
343                         mbuf = NULL;
344
345                 if (DEBUG_GTE(DELTASUM, 2)) {
346                         rprintf(FINFO, "send_files mapped %s%s%s of size %s\n",
347                                 path,slash,fname, big_num(st.st_size));
348                 }
349
350                 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
351                                     fnamecmp_type, xname, xlen);
352                 write_sum_head(f_xfer, s);
353
354                 if (DEBUG_GTE(DELTASUM, 2))
355                         rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
356
357                 if (log_before_transfer)
358                         log_item(FCLIENT, file, iflags, NULL);
359                 else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
360                         rprintf(FCLIENT, "%s\n", fname);
361
362                 set_compression(fname);
363
364                 match_sums(f_xfer, s, mbuf, st.st_size);
365                 if (INFO_GTE(PROGRESS, 1))
366                         end_progress(st.st_size);
367
368                 log_item(log_code, file, iflags, NULL);
369
370                 if (mbuf) {
371                         j = unmap_file(mbuf);
372                         if (j) {
373                                 io_error |= IOERR_GENERAL;
374                                 rsyserr(FERROR_XFER, j,
375                                         "read errors mapping %s",
376                                         full_fname(fname));
377                         }
378                 }
379                 close(fd);
380
381                 free_sums(s);
382
383                 if (DEBUG_GTE(SEND, 1))
384                         rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
385
386                 /* Flag that we actually sent this entry. */
387                 file->flags |= FLAG_FILE_SENT;
388         }
389         if (make_backups < 0)
390                 make_backups = -make_backups;
391
392         if (io_error != save_io_error && protocol_version >= 30)
393                 send_msg_int(MSG_IO_ERROR, io_error);
394
395         if (DEBUG_GTE(SEND, 1))
396                 rprintf(FINFO, "send files finished\n");
397
398         match_report();
399
400         write_ndx(f_out, NDX_DONE);
401 }