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