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