1c81ec1dc03453d7e1faa4c317ba9ac671193c06
[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-2007 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 version 2 as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21
22 #include "rsync.h"
23
24 extern int verbose;
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 allowed_lull;
36 extern int protocol_version;
37 extern int remove_source_files;
38 extern int updating_basis_file;
39 extern int make_backups;
40 extern int do_progress;
41 extern int inplace;
42 extern int batch_fd;
43 extern int write_batch;
44 extern struct stats stats;
45 extern struct file_list *cur_flist, *first_flist;
46
47 /**
48  * @file
49  *
50  * The sender gets checksums from the generator, calculates deltas,
51  * and transmits them to the receiver.  The sender process runs on the
52  * machine holding the source files.
53  **/
54
55 /**
56  * Receive the checksums for a buffer
57  **/
58 static struct sum_struct *receive_sums(int f)
59 {
60         struct sum_struct *s;
61         int32 i;
62         int lull_mod = allowed_lull * 5;
63         OFF_T offset = 0;
64
65         if (!(s = new(struct sum_struct)))
66                 out_of_memory("receive_sums");
67
68         read_sum_head(f, s);
69
70         s->sums = NULL;
71
72         if (verbose > 3) {
73                 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
74                         (double)s->count, (long)s->blength, (long)s->remainder);
75         }
76
77         if (append_mode > 0) {
78                 s->flength = (OFF_T)s->count * s->blength;
79                 if (s->remainder)
80                         s->flength -= s->blength - s->remainder;
81                 return s;
82         }
83
84         if (s->count == 0)
85                 return(s);
86
87         if (!(s->sums = new_array(struct sum_buf, s->count)))
88                 out_of_memory("receive_sums");
89
90         for (i = 0; i < s->count; i++) {
91                 s->sums[i].sum1 = read_int(f);
92                 read_buf(f, s->sums[i].sum2, s->s2length);
93
94                 s->sums[i].offset = offset;
95                 s->sums[i].flags = 0;
96
97                 if (i == s->count-1 && s->remainder != 0)
98                         s->sums[i].len = s->remainder;
99                 else
100                         s->sums[i].len = s->blength;
101                 offset += s->sums[i].len;
102
103                 if (allowed_lull && !(i % lull_mod))
104                         maybe_send_keepalive();
105
106                 if (verbose > 3) {
107                         rprintf(FINFO,
108                                 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
109                                 i, s->sums[i].len, (double)s->sums[i].offset,
110                                 s->sums[i].sum1);
111                 }
112         }
113
114         s->flength = offset;
115
116         return s;
117 }
118
119 void successful_send(int ndx)
120 {
121         char fname[MAXPATHLEN];
122         struct file_struct *file;
123         struct file_list *flist;
124
125         if (!remove_source_files)
126                 return;
127
128         if (!(flist = flist_for_ndx(ndx))) {
129                 rprintf(FERROR,
130                         "INTERNAL ERROR: unable to find flist for item %d\n",
131                         ndx);
132                 return;
133         }
134
135         file = flist->files[ndx - flist->ndx_start];
136         if (!push_flist_dir(F_ROOTDIR(file), -1))
137                 return;
138         f_name(file, fname);
139
140         if (do_unlink(fname) == 0) {
141                 if (verbose > 1)
142                         rprintf(FINFO, "sender removed %s\n", fname);
143         } else
144                 rsyserr(FERROR, errno, "sender failed to remove %s", fname);
145 }
146
147 void write_ndx_and_attrs(int f_out, int ndx, int iflags,
148                          uchar fnamecmp_type, char *buf, int len)
149 {
150         write_ndx(f_out, ndx);
151         if (protocol_version < 29)
152                 return;
153         write_shortint(f_out, iflags);
154         if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
155                 write_byte(f_out, fnamecmp_type);
156         if (iflags & ITEM_XNAME_FOLLOWS)
157                 write_vstring(f_out, buf, len);
158 }
159
160 void send_files(int f_in, int f_out)
161 {
162         int fd = -1;
163         struct sum_struct *s;
164         struct map_struct *mbuf = NULL;
165         STRUCT_STAT st;
166         char fname[MAXPATHLEN], xname[MAXPATHLEN];
167         const char *path, *slash;
168         uchar fnamecmp_type;
169         int iflags, xlen;
170         struct file_struct *file;
171         int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
172         struct stats initial_stats;
173         int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
174         enum logcode log_code = log_before_transfer ? FLOG : FINFO;
175         int f_xfer = write_batch < 0 ? batch_fd : f_out;
176         int ndx, j;
177
178         if (verbose > 2)
179                 rprintf(FINFO, "send_files starting\n");
180
181         while (1) {
182                 if (inc_recurse)
183                         send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
184
185                 /* This call also sets cur_flist. */
186                 ndx = read_ndx_and_attrs(f_in, f_out, &iflags,
187                                          &fnamecmp_type, xname, &xlen);
188                 if (ndx == NDX_DONE) {
189                         if (inc_recurse && first_flist) {
190                                 flist_free(first_flist);
191                                 if (first_flist) {
192                                         write_ndx(f_out, NDX_DONE);
193                                         continue;
194                                 }
195                         }
196                         if (++phase > max_phase)
197                                 break;
198                         if (verbose > 2)
199                                 rprintf(FINFO, "send_files phase=%d\n", phase);
200                         write_ndx(f_out, NDX_DONE);
201                         continue;
202                 }
203
204                 file = cur_flist->files[ndx - cur_flist->ndx_start];
205                 if (F_ROOTDIR(file)) {
206                         path = F_ROOTDIR(file);
207                         slash = "/";
208                 } else {
209                         path = slash = "";
210                 }
211                 if (!push_flist_dir(F_ROOTDIR(file), -1))
212                         continue;
213                 f_name(file, fname);
214
215                 if (verbose > 2)
216                         rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
217
218                 if (!(iflags & ITEM_TRANSFER)) {
219                         maybe_log_item(file, iflags, itemizing, xname);
220                         continue;
221                 }
222                 if (phase == 2) {
223                         rprintf(FERROR,
224                                 "got transfer request in phase 2 [%s]\n",
225                                 who_am_i());
226                         exit_cleanup(RERR_PROTOCOL);
227                 }
228
229                 if (file->flags & FLAG_FILE_SENT) {
230                         if (csum_length == SHORT_SUM_LENGTH) {
231                                 /* For inplace: redo phase turns off the backup
232                                  * flag so that we do a regular inplace send. */
233                                 make_backups = -make_backups;
234                                 append_mode = -append_mode;
235                                 csum_length = SUM_LENGTH;
236                         }
237                 } else {
238                         if (csum_length != SHORT_SUM_LENGTH) {
239                                 make_backups = -make_backups;
240                                 append_mode = -append_mode;
241                                 csum_length = SHORT_SUM_LENGTH;
242                         }
243                 }
244
245                 updating_basis_file = inplace && (protocol_version >= 29
246                         ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0);
247
248                 stats.current_file_index = ndx;
249                 stats.num_transferred_files++;
250                 stats.total_transferred_size += F_LENGTH(file);
251
252                 if (!do_xfers) { /* log the transfer */
253                         log_item(FCLIENT, file, &stats, iflags, NULL);
254                         write_ndx_and_attrs(f_out, ndx, iflags, fnamecmp_type,
255                                             xname, xlen);
256                         continue;
257                 }
258
259                 initial_stats = stats;
260
261                 if (!(s = receive_sums(f_in))) {
262                         io_error |= IOERR_GENERAL;
263                         rprintf(FERROR, "receive_sums failed\n");
264                         exit_cleanup(RERR_PROTOCOL);
265                 }
266
267                 fd = do_open(fname, O_RDONLY, 0);
268                 if (fd == -1) {
269                         if (errno == ENOENT) {
270                                 enum logcode c = am_daemon
271                                     && protocol_version < 28 ? FERROR
272                                                              : FINFO;
273                                 io_error |= IOERR_VANISHED;
274                                 rprintf(c, "file has vanished: %s\n",
275                                         full_fname(fname));
276                         } else {
277                                 io_error |= IOERR_GENERAL;
278                                 rsyserr(FERROR, errno,
279                                         "send_files failed to open %s",
280                                         full_fname(fname));
281                         }
282                         free_sums(s);
283                         if (protocol_version >= 30)
284                                 send_msg_int(MSG_NO_SEND, ndx);
285                         continue;
286                 }
287
288                 /* map the local file */
289                 if (do_fstat(fd, &st) != 0) {
290                         io_error |= IOERR_GENERAL;
291                         rsyserr(FERROR, errno, "fstat failed");
292                         free_sums(s);
293                         close(fd);
294                         exit_cleanup(RERR_PROTOCOL);
295                 }
296
297                 if (st.st_size) {
298                         int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
299                         mbuf = map_file(fd, st.st_size, read_size, s->blength);
300                 } else
301                         mbuf = NULL;
302
303                 if (verbose > 2) {
304                         rprintf(FINFO, "send_files mapped %s%s%s of size %.0f\n",
305                                 path,slash,fname, (double)st.st_size);
306                 }
307
308                 write_ndx_and_attrs(f_out, ndx, iflags, fnamecmp_type,
309                                     xname, xlen);
310                 write_sum_head(f_xfer, s);
311
312                 if (verbose > 2)
313                         rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
314
315                 if (log_before_transfer)
316                         log_item(FCLIENT, file, &initial_stats, iflags, NULL);
317                 else if (!am_server && verbose && do_progress)
318                         rprintf(FCLIENT, "%s\n", fname);
319
320                 set_compression(fname);
321
322                 match_sums(f_xfer, s, mbuf, st.st_size);
323                 if (do_progress)
324                         end_progress(st.st_size);
325
326                 log_item(log_code, file, &initial_stats, iflags, NULL);
327
328                 if (mbuf) {
329                         j = unmap_file(mbuf);
330                         if (j) {
331                                 io_error |= IOERR_GENERAL;
332                                 rsyserr(FERROR, j,
333                                         "read errors mapping %s",
334                                         full_fname(fname));
335                         }
336                 }
337                 close(fd);
338
339                 free_sums(s);
340
341                 if (verbose > 2)
342                         rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
343
344                 /* Flag that we actually sent this entry. */
345                 file->flags |= FLAG_FILE_SENT;
346         }
347         if (make_backups < 0)
348                 make_backups = -make_backups;
349
350         if (verbose > 2)
351                 rprintf(FINFO, "send files finished\n");
352
353         match_report();
354
355         write_ndx(f_out, NDX_DONE);
356 }