Add understanding of RSYNC_PROTECT_ARGS environment var.
[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 BOOL we_send_keepalive_messages;
47 extern struct stats stats;
48 extern struct file_list *cur_flist, *first_flist, *dir_flist;
49
50 BOOL extra_flist_sending_enabled;
51
52 /**
53  * @file
54  *
55  * The sender gets checksums from the generator, calculates deltas,
56  * and transmits them to the receiver.  The sender process runs on the
57  * machine holding the source files.
58  **/
59
60 /**
61  * Receive the checksums for a buffer
62  **/
63 static struct sum_struct *receive_sums(int f)
64 {
65         struct sum_struct *s;
66         int32 i;
67         int lull_mod = allowed_lull * 5;
68         OFF_T offset = 0;
69
70         if (!(s = new(struct sum_struct)))
71                 out_of_memory("receive_sums");
72
73         read_sum_head(f, s);
74
75         s->sums = NULL;
76
77         if (DEBUG_GTE(DELTASUM, 3)) {
78                 rprintf(FINFO, "count=%s n=%ld rem=%ld\n",
79                         big_num(s->count), (long)s->blength, (long)s->remainder);
80         }
81
82         if (append_mode > 0) {
83                 s->flength = (OFF_T)s->count * s->blength;
84                 if (s->remainder)
85                         s->flength -= s->blength - s->remainder;
86                 return s;
87         }
88
89         if (s->count == 0)
90                 return(s);
91
92         if (!(s->sums = new_array(struct sum_buf, s->count)))
93                 out_of_memory("receive_sums");
94
95         for (i = 0; i < s->count; i++) {
96                 s->sums[i].sum1 = read_int(f);
97                 read_buf(f, s->sums[i].sum2, s->s2length);
98
99                 s->sums[i].offset = offset;
100                 s->sums[i].flags = 0;
101
102                 if (i == s->count-1 && s->remainder != 0)
103                         s->sums[i].len = s->remainder;
104                 else
105                         s->sums[i].len = s->blength;
106                 offset += s->sums[i].len;
107
108                 if (we_send_keepalive_messages && !(i % lull_mod))
109                         maybe_send_keepalive(time(NULL), True);
110
111                 if (DEBUG_GTE(DELTASUM, 3)) {
112                         rprintf(FINFO,
113                                 "chunk[%d] len=%d offset=%s sum1=%08x\n",
114                                 i, s->sums[i].len, big_num(s->sums[i].offset),
115                                 s->sums[i].sum1);
116                 }
117         }
118
119         s->flength = offset;
120
121         return s;
122 }
123
124 void successful_send(int ndx)
125 {
126         char fname[MAXPATHLEN];
127         struct file_struct *file;
128         struct file_list *flist;
129
130         if (!remove_source_files)
131                 return;
132
133         flist = flist_for_ndx(ndx, "successful_send");
134         file = flist->files[ndx - flist->ndx_start];
135         if (!change_pathname(file, NULL, 0))
136                 return;
137         f_name(file, fname);
138
139         if (do_unlink(fname) == 0) {
140                 if (INFO_GTE(REMOVE, 1))
141                         rprintf(FINFO, "sender removed %s\n", fname);
142         } else
143                 rsyserr(FERROR, errno, "sender failed to remove %s", fname);
144 }
145
146 static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
147                                 const char *fname, struct file_struct *file,
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 #ifdef SUPPORT_XATTRS
159         if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers)
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                         recv_xattr_request(file, f_in);
242 #endif
243
244                 if (!(iflags & ITEM_TRANSFER)) {
245                         maybe_log_item(file, iflags, itemizing, xname);
246                         write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
247                                             fnamecmp_type, xname, xlen);
248                         if (iflags & ITEM_IS_NEW) {
249                                 stats.created_files++;
250                                 if (S_ISREG(file->mode)) {
251                                         /* Nothing further to count. */
252                                 } else if (S_ISDIR(file->mode))
253                                         stats.created_dirs++;
254 #ifdef SUPPORT_LINKS
255                                 else if (S_ISLNK(file->mode))
256                                         stats.created_symlinks++;
257 #endif
258                                 else if (IS_DEVICE(file->mode))
259                                         stats.created_devices++;
260                                 else
261                                         stats.created_specials++;
262                         }
263                         continue;
264                 }
265                 if (phase == 2) {
266                         rprintf(FERROR,
267                                 "got transfer request in phase 2 [%s]\n",
268                                 who_am_i());
269                         exit_cleanup(RERR_PROTOCOL);
270                 }
271
272                 if (file->flags & FLAG_FILE_SENT) {
273                         if (csum_length == SHORT_SUM_LENGTH) {
274                                 /* For inplace: redo phase turns off the backup
275                                  * flag so that we do a regular inplace send. */
276                                 make_backups = -make_backups;
277                                 append_mode = -append_mode;
278                                 csum_length = SUM_LENGTH;
279                         }
280                 } else {
281                         if (csum_length != SHORT_SUM_LENGTH) {
282                                 make_backups = -make_backups;
283                                 append_mode = -append_mode;
284                                 csum_length = SHORT_SUM_LENGTH;
285                         }
286                         if (iflags & ITEM_IS_NEW)
287                                 stats.created_files++;
288                 }
289
290                 updating_basis_file = inplace && (protocol_version >= 29
291                         ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0);
292
293                 if (!am_server && INFO_GTE(PROGRESS, 1))
294                         set_current_file_index(file, ndx);
295                 stats.xferred_files++;
296                 stats.total_transferred_size += F_LENGTH(file);
297
298                 if (!log_before_transfer)
299                         remember_initial_stats();
300
301                 if (!do_xfers) { /* log the transfer */
302                         log_item(FCLIENT, file, iflags, NULL);
303                         write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
304                                             fnamecmp_type, xname, xlen);
305                         continue;
306                 }
307
308                 if (!(s = receive_sums(f_in))) {
309                         io_error |= IOERR_GENERAL;
310                         rprintf(FERROR_XFER, "receive_sums failed\n");
311                         exit_cleanup(RERR_PROTOCOL);
312                 }
313
314                 fd = do_open(fname, O_RDONLY, 0);
315                 if (fd == -1) {
316                         if (errno == ENOENT) {
317                                 enum logcode c = am_daemon
318                                     && protocol_version < 28 ? FERROR
319                                                              : FWARNING;
320                                 io_error |= IOERR_VANISHED;
321                                 rprintf(c, "file has vanished: %s\n",
322                                         full_fname(fname));
323                         } else {
324                                 io_error |= IOERR_GENERAL;
325                                 rsyserr(FERROR_XFER, errno,
326                                         "send_files failed to open %s",
327                                         full_fname(fname));
328                         }
329                         free_sums(s);
330                         if (protocol_version >= 30)
331                                 send_msg_int(MSG_NO_SEND, ndx);
332                         continue;
333                 }
334
335                 /* map the local file */
336                 if (do_fstat(fd, &st) != 0) {
337                         io_error |= IOERR_GENERAL;
338                         rsyserr(FERROR_XFER, errno, "fstat failed");
339                         free_sums(s);
340                         close(fd);
341                         exit_cleanup(RERR_FILEIO);
342                 }
343
344                 if (st.st_size) {
345                         int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
346                         mbuf = map_file(fd, st.st_size, read_size, s->blength);
347                 } else
348                         mbuf = NULL;
349
350                 if (DEBUG_GTE(DELTASUM, 2)) {
351                         rprintf(FINFO, "send_files mapped %s%s%s of size %s\n",
352                                 path,slash,fname, big_num(st.st_size));
353                 }
354
355                 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
356                                     fnamecmp_type, xname, xlen);
357                 write_sum_head(f_xfer, s);
358
359                 if (DEBUG_GTE(DELTASUM, 2))
360                         rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
361
362                 if (log_before_transfer)
363                         log_item(FCLIENT, file, iflags, NULL);
364                 else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
365                         rprintf(FCLIENT, "%s\n", fname);
366
367                 set_compression(fname);
368
369                 match_sums(f_xfer, s, mbuf, st.st_size);
370                 if (INFO_GTE(PROGRESS, 1))
371                         end_progress(st.st_size);
372
373                 log_item(log_code, file, iflags, NULL);
374
375                 if (mbuf) {
376                         j = unmap_file(mbuf);
377                         if (j) {
378                                 io_error |= IOERR_GENERAL;
379                                 rsyserr(FERROR_XFER, j,
380                                         "read errors mapping %s",
381                                         full_fname(fname));
382                         }
383                 }
384                 close(fd);
385
386                 free_sums(s);
387
388                 if (DEBUG_GTE(SEND, 1))
389                         rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
390
391                 /* Flag that we actually sent this entry. */
392                 file->flags |= FLAG_FILE_SENT;
393         }
394         if (make_backups < 0)
395                 make_backups = -make_backups;
396
397         if (io_error != save_io_error && protocol_version >= 30)
398                 send_msg_int(MSG_IO_ERROR, io_error);
399
400         if (DEBUG_GTE(SEND, 1))
401                 rprintf(FINFO, "send files finished\n");
402
403         match_report();
404
405         write_ndx(f_out, NDX_DONE);
406 }