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