Changed some "rsync" commands into proper "$RSYNC" commands.
[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-2008 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 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 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, *dir_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 (DEBUG_GTE(DELTASUM, 3)) {
74                 rprintf(FINFO, "count=%s n=%ld rem=%ld\n",
75                         big_num(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 (DEBUG_GTE(DELTASUM, 3)) {
108                         rprintf(FINFO,
109                                 "chunk[%d] len=%d offset=%s sum1=%08x\n",
110                                 i, s->sums[i].len, big_num(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         flist = flist_for_ndx(ndx, "successful_send");
130         file = flist->files[ndx - flist->ndx_start];
131         if (!change_pathname(file, NULL, 0))
132                 return;
133         f_name(file, fname);
134
135         if (do_unlink(fname) == 0) {
136                 if (INFO_GTE(REMOVE, 1))
137                         rprintf(FINFO, "sender removed %s\n", fname);
138         } else
139                 rsyserr(FERROR, errno, "sender failed to remove %s", fname);
140 }
141
142 static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
143                                 const char *fname, struct file_struct *file,
144                                 uchar fnamecmp_type, char *buf, int len)
145 {
146         write_ndx(f_out, ndx);
147         if (protocol_version < 29)
148                 return;
149         write_shortint(f_out, iflags);
150         if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
151                 write_byte(f_out, fnamecmp_type);
152         if (iflags & ITEM_XNAME_FOLLOWS)
153                 write_vstring(f_out, buf, len);
154 #ifdef SUPPORT_XATTRS
155         if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && !dry_run)
156                 send_xattr_request(fname, file, f_out);
157 #endif
158 }
159
160 void send_files(int f_in, int f_out)
161 {
162         int fd = -1;
163         struct sum_struct *s;
164         struct map_struct *mbuf = NULL;
165         STRUCT_STAT st;
166         char fname[MAXPATHLEN], xname[MAXPATHLEN];
167         const char *path, *slash;
168         uchar fnamecmp_type;
169         int iflags, xlen;
170         struct file_struct *file;
171         int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
172         struct stats initial_stats;
173         int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
174         enum logcode log_code = log_before_transfer ? FLOG : FINFO;
175         int f_xfer = write_batch < 0 ? batch_fd : f_out;
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, 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, 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 && !dry_run)
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                 if (!am_server && INFO_GTE(PROGRESS, 1))
266                         set_current_file_index(file, ndx);
267                 stats.num_transferred_files++;
268                 stats.total_transferred_size += F_LENGTH(file);
269
270                 if (!do_xfers) { /* log the transfer */
271                         log_item(FCLIENT, file, &stats, iflags, NULL);
272                         write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
273                                             fnamecmp_type, xname, xlen);
274                         continue;
275                 }
276
277                 initial_stats = stats;
278
279                 if (!(s = receive_sums(f_in))) {
280                         io_error |= IOERR_GENERAL;
281                         rprintf(FERROR_XFER, "receive_sums failed\n");
282                         exit_cleanup(RERR_PROTOCOL);
283                 }
284
285                 fd = do_open(fname, O_RDONLY, 0);
286                 if (fd == -1) {
287                         if (errno == ENOENT) {
288                                 enum logcode c = am_daemon
289                                     && protocol_version < 28 ? FERROR
290                                                              : FWARNING;
291                                 io_error |= IOERR_VANISHED;
292                                 rprintf(c, "file has vanished: %s\n",
293                                         full_fname(fname));
294                         } else {
295                                 io_error |= IOERR_GENERAL;
296                                 rsyserr(FERROR_XFER, errno,
297                                         "send_files failed to open %s",
298                                         full_fname(fname));
299                         }
300                         free_sums(s);
301                         if (protocol_version >= 30)
302                                 send_msg_int(MSG_NO_SEND, ndx);
303                         continue;
304                 }
305
306                 /* map the local file */
307                 if (do_fstat(fd, &st) != 0) {
308                         io_error |= IOERR_GENERAL;
309                         rsyserr(FERROR_XFER, errno, "fstat failed");
310                         free_sums(s);
311                         close(fd);
312                         exit_cleanup(RERR_PROTOCOL);
313                 }
314
315                 if (st.st_size) {
316                         int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
317                         mbuf = map_file(fd, st.st_size, read_size, s->blength);
318                 } else
319                         mbuf = NULL;
320
321                 if (DEBUG_GTE(DELTASUM, 2)) {
322                         rprintf(FINFO, "send_files mapped %s%s%s of size %s\n",
323                                 path,slash,fname, big_num(st.st_size));
324                 }
325
326                 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
327                                     fnamecmp_type, xname, xlen);
328                 write_sum_head(f_xfer, s);
329
330                 if (DEBUG_GTE(DELTASUM, 2))
331                         rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
332
333                 if (log_before_transfer)
334                         log_item(FCLIENT, file, &initial_stats, iflags, NULL);
335                 else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
336                         rprintf(FCLIENT, "%s\n", fname);
337
338                 set_compression(fname);
339
340                 match_sums(f_xfer, s, mbuf, st.st_size);
341                 if (INFO_GTE(PROGRESS, 1))
342                         end_progress(st.st_size);
343
344                 log_item(log_code, file, &initial_stats, iflags, NULL);
345
346                 if (mbuf) {
347                         j = unmap_file(mbuf);
348                         if (j) {
349                                 io_error |= IOERR_GENERAL;
350                                 rsyserr(FERROR_XFER, j,
351                                         "read errors mapping %s",
352                                         full_fname(fname));
353                         }
354                 }
355                 close(fd);
356
357                 free_sums(s);
358
359                 if (DEBUG_GTE(SEND, 1))
360                         rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
361
362                 /* Flag that we actually sent this entry. */
363                 file->flags |= FLAG_FILE_SENT;
364         }
365         if (make_backups < 0)
366                 make_backups = -make_backups;
367
368         if (DEBUG_GTE(SEND, 1))
369                 rprintf(FINFO, "send files finished\n");
370
371         match_report();
372
373         write_ndx(f_out, NDX_DONE);
374 }