Add support for transferring & setting nsec time values.
[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 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 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, *dir_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 (DEBUG_GTE(DELTASUM, 3)) {
73                 rprintf(FINFO, "count=%s n=%ld rem=%ld\n",
74                         big_num(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 (DEBUG_GTE(DELTASUM, 3)) {
107                         rprintf(FINFO,
108                                 "chunk[%d] len=%d offset=%s sum1=%08x\n",
109                                 i, s->sums[i].len, big_num(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         flist = flist_for_ndx(ndx, "successful_send");
129         file = flist->files[ndx - flist->ndx_start];
130         if (!change_pathname(file, NULL, 0))
131                 return;
132         f_name(file, fname);
133
134         if (do_unlink(fname) == 0) {
135                 if (INFO_GTE(REMOVE, 1))
136                         rprintf(FINFO, "sender removed %s\n", fname);
137         } else
138                 rsyserr(FERROR, errno, "sender failed to remove %s", fname);
139 }
140
141 static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
142                                 const char *fname, struct file_struct *file,
143                                 uchar fnamecmp_type, char *buf, int len)
144 {
145         write_ndx(f_out, ndx);
146         if (protocol_version < 29)
147                 return;
148         write_shortint(f_out, iflags);
149         if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
150                 write_byte(f_out, fnamecmp_type);
151         if (iflags & ITEM_XNAME_FOLLOWS)
152                 write_vstring(f_out, buf, len);
153 #ifdef SUPPORT_XATTRS
154         if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers)
155                 send_xattr_request(fname, file, f_out);
156 #endif
157 }
158
159 void send_files(int f_in, int f_out)
160 {
161         int fd = -1;
162         struct sum_struct *s;
163         struct map_struct *mbuf = NULL;
164         STRUCT_STAT st;
165         char fname[MAXPATHLEN], xname[MAXPATHLEN];
166         const char *path, *slash;
167         uchar fnamecmp_type;
168         int iflags, xlen;
169         struct file_struct *file;
170         int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
171         struct stats initial_stats;
172         int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
173         enum logcode log_code = log_before_transfer ? FLOG : FINFO;
174         int f_xfer = write_batch < 0 ? batch_fd : f_out;
175         int save_io_error = io_error;
176         int ndx, j;
177
178         if (DEBUG_GTE(SEND, 1))
179                 rprintf(FINFO, "send_files starting\n");
180
181         while (1) {
182                 if (inc_recurse)
183                         send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
184
185                 /* This call also sets cur_flist. */
186                 ndx = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
187                                          xname, &xlen);
188                 if (ndx == NDX_DONE) {
189                         if (!am_server && INFO_GTE(PROGRESS, 2) && cur_flist) {
190                                 set_current_file_index(NULL, 0);
191                                 end_progress(0);
192                         }
193                         if (inc_recurse && first_flist) {
194                                 flist_free(first_flist);
195                                 if (first_flist) {
196                                         write_ndx(f_out, NDX_DONE);
197                                         continue;
198                                 }
199                         }
200                         if (++phase > max_phase)
201                                 break;
202                         if (DEBUG_GTE(SEND, 1))
203                                 rprintf(FINFO, "send_files phase=%d\n", phase);
204                         write_ndx(f_out, NDX_DONE);
205                         continue;
206                 }
207
208                 if (inc_recurse)
209                         send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
210
211                 if (ndx - cur_flist->ndx_start >= 0)
212                         file = cur_flist->files[ndx - cur_flist->ndx_start];
213                 else
214                         file = dir_flist->files[cur_flist->parent_ndx];
215                 if (F_PATHNAME(file)) {
216                         path = F_PATHNAME(file);
217                         slash = "/";
218                 } else {
219                         path = slash = "";
220                 }
221                 if (!change_pathname(file, NULL, 0))
222                         continue;
223                 f_name(file, fname);
224
225                 if (DEBUG_GTE(SEND, 1))
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 && do_xfers)
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                         if (iflags & ITEM_IS_NEW) {
238                                 stats.created_files++;
239                                 if (S_ISREG(file->mode)) {
240                                         /* Nothing further to count. */
241                                 } else if (S_ISDIR(file->mode))
242                                         stats.created_dirs++;
243 #ifdef SUPPORT_LINKS
244                                 else if (S_ISLNK(file->mode))
245                                         stats.created_symlinks++;
246 #endif
247                                 else if (IS_DEVICE(file->mode))
248                                         stats.created_devices++;
249                                 else
250                                         stats.created_specials++;
251                         }
252                         continue;
253                 }
254                 if (phase == 2) {
255                         rprintf(FERROR,
256                                 "got transfer request in phase 2 [%s]\n",
257                                 who_am_i());
258                         exit_cleanup(RERR_PROTOCOL);
259                 }
260
261                 if (file->flags & FLAG_FILE_SENT) {
262                         if (csum_length == SHORT_SUM_LENGTH) {
263                                 /* For inplace: redo phase turns off the backup
264                                  * flag so that we do a regular inplace send. */
265                                 make_backups = -make_backups;
266                                 append_mode = -append_mode;
267                                 csum_length = SUM_LENGTH;
268                         }
269                 } else {
270                         if (csum_length != SHORT_SUM_LENGTH) {
271                                 make_backups = -make_backups;
272                                 append_mode = -append_mode;
273                                 csum_length = SHORT_SUM_LENGTH;
274                         }
275                         if (iflags & ITEM_IS_NEW)
276                                 stats.created_files++;
277                 }
278
279                 updating_basis_file = inplace && (protocol_version >= 29
280                         ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0);
281
282                 if (!am_server && INFO_GTE(PROGRESS, 1))
283                         set_current_file_index(file, ndx);
284                 stats.xferred_files++;
285                 stats.total_transferred_size += F_LENGTH(file);
286
287                 if (!do_xfers) { /* log the transfer */
288                         log_item(FCLIENT, file, &stats, iflags, NULL);
289                         write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
290                                             fnamecmp_type, xname, xlen);
291                         continue;
292                 }
293
294                 initial_stats = stats;
295
296                 if (!(s = receive_sums(f_in))) {
297                         io_error |= IOERR_GENERAL;
298                         rprintf(FERROR_XFER, "receive_sums failed\n");
299                         exit_cleanup(RERR_PROTOCOL);
300                 }
301
302                 fd = do_open(fname, O_RDONLY, 0);
303                 if (fd == -1) {
304                         if (errno == ENOENT) {
305                                 enum logcode c = am_daemon
306                                     && protocol_version < 28 ? FERROR
307                                                              : FWARNING;
308                                 io_error |= IOERR_VANISHED;
309                                 rprintf(c, "file has vanished: %s\n",
310                                         full_fname(fname));
311                         } else {
312                                 io_error |= IOERR_GENERAL;
313                                 rsyserr(FERROR_XFER, errno,
314                                         "send_files failed to open %s",
315                                         full_fname(fname));
316                         }
317                         free_sums(s);
318                         if (protocol_version >= 30)
319                                 send_msg_int(MSG_NO_SEND, ndx);
320                         continue;
321                 }
322
323                 /* map the local file */
324                 if (do_fstat(fd, &st) != 0) {
325                         io_error |= IOERR_GENERAL;
326                         rsyserr(FERROR_XFER, errno, "fstat failed");
327                         free_sums(s);
328                         close(fd);
329                         exit_cleanup(RERR_PROTOCOL);
330                 }
331
332                 if (st.st_size) {
333                         int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
334                         mbuf = map_file(fd, st.st_size, read_size, s->blength);
335                 } else
336                         mbuf = NULL;
337
338                 if (DEBUG_GTE(DELTASUM, 2)) {
339                         rprintf(FINFO, "send_files mapped %s%s%s of size %s\n",
340                                 path,slash,fname, big_num(st.st_size));
341                 }
342
343                 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
344                                     fnamecmp_type, xname, xlen);
345                 write_sum_head(f_xfer, s);
346
347                 if (DEBUG_GTE(DELTASUM, 2))
348                         rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
349
350                 if (log_before_transfer)
351                         log_item(FCLIENT, file, &initial_stats, iflags, NULL);
352                 else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
353                         rprintf(FCLIENT, "%s\n", fname);
354
355                 set_compression(fname);
356
357                 match_sums(f_xfer, s, mbuf, st.st_size);
358                 if (INFO_GTE(PROGRESS, 1))
359                         end_progress(st.st_size);
360
361                 log_item(log_code, file, &initial_stats, iflags, NULL);
362
363                 if (mbuf) {
364                         j = unmap_file(mbuf);
365                         if (j) {
366                                 io_error |= IOERR_GENERAL;
367                                 rsyserr(FERROR_XFER, j,
368                                         "read errors mapping %s",
369                                         full_fname(fname));
370                         }
371                 }
372                 close(fd);
373
374                 free_sums(s);
375
376                 if (DEBUG_GTE(SEND, 1))
377                         rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
378
379                 /* Flag that we actually sent this entry. */
380                 file->flags |= FLAG_FILE_SENT;
381         }
382         if (make_backups < 0)
383                 make_backups = -make_backups;
384
385         if (io_error != save_io_error && protocol_version >= 30)
386                 send_msg_int(MSG_IO_ERROR, io_error);
387
388         if (DEBUG_GTE(SEND, 1))
389                 rprintf(FINFO, "send files finished\n");
390
391         match_report();
392
393         write_ndx(f_out, NDX_DONE);
394 }