Support new --append option.
[rsync/rsync.git] / sender.c
1 /*
2    Copyright (C) Andrew Tridgell 1996
3    Copyright (C) Paul Mackerras 1996
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include "rsync.h"
21
22 extern int verbose;
23 extern int do_xfers;
24 extern int am_server;
25 extern int am_daemon;
26 extern int log_before_transfer;
27 extern int log_format_has_i;
28 extern int daemon_log_format_has_i;
29 extern int csum_length;
30 extern int append_mode;
31 extern int io_error;
32 extern int allowed_lull;
33 extern int protocol_version;
34 extern int remove_sent_files;
35 extern int updating_basis_file;
36 extern int make_backups;
37 extern int do_progress;
38 extern int inplace;
39 extern int batch_fd;
40 extern int write_batch;
41 extern struct stats stats;
42 extern struct file_list *the_file_list;
43 extern char *log_format;
44
45
46 /**
47  * @file
48  *
49  * The sender gets checksums from the generator, calculates deltas,
50  * and transmits them to the receiver.  The sender process runs on the
51  * machine holding the source files.
52  **/
53
54 /**
55  * Receive the checksums for a buffer
56  **/
57 static struct sum_struct *receive_sums(int f)
58 {
59         struct sum_struct *s;
60         int32 i;
61         int lull_mod = allowed_lull * 5;
62         OFF_T offset = 0;
63
64         if (!(s = new(struct sum_struct)))
65                 out_of_memory("receive_sums");
66
67         read_sum_head(f, s);
68
69         s->sums = NULL;
70
71         if (verbose > 3) {
72                 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
73                         (double)s->count, (long)s->blength, (long)s->remainder);
74         }
75
76         if (append_mode) {
77                 s->flength = (OFF_T)s->count * s->blength;
78                 if (s->remainder)
79                         s->flength -= s->blength - s->remainder;
80                 return s;
81         }
82
83         if (s->count == 0)
84                 return(s);
85
86         if (!(s->sums = new_array(struct sum_buf, s->count)))
87                 out_of_memory("receive_sums");
88
89         for (i = 0; i < s->count; i++) {
90                 s->sums[i].sum1 = read_int(f);
91                 read_buf(f, s->sums[i].sum2, s->s2length);
92
93                 s->sums[i].offset = offset;
94                 s->sums[i].flags = 0;
95
96                 if (i == s->count-1 && s->remainder != 0)
97                         s->sums[i].len = s->remainder;
98                 else
99                         s->sums[i].len = s->blength;
100                 offset += s->sums[i].len;
101
102                 if (allowed_lull && !(i % lull_mod))
103                         maybe_send_keepalive();
104
105                 if (verbose > 3) {
106                         rprintf(FINFO,
107                                 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
108                                 i, s->sums[i].len, (double)s->sums[i].offset,
109                                 s->sums[i].sum1);
110                 }
111         }
112
113         s->flength = offset;
114
115         return s;
116 }
117
118 void successful_send(int ndx)
119 {
120         char fname[MAXPATHLEN];
121         struct file_struct *file;
122         unsigned int offset;
123
124         if (ndx < 0 || ndx >= the_file_list->count)
125                 return;
126
127         file = the_file_list->files[ndx];
128         /* The generator might tell us about symlinks we didn't send. */
129         if (!(file->flags & FLAG_SENT) && !S_ISLNK(file->mode))
130                 return;
131         if (file->dir.root) {
132                 offset = stringjoin(fname, sizeof fname,
133                                     file->dir.root, "/", NULL);
134         } else
135                 offset = 0;
136         f_name_to(file, fname + offset);
137         if (remove_sent_files && do_unlink(fname) == 0 && verbose > 1) {
138                 rprintf(FINFO, "sender removed %s\n",
139                         safe_fname(fname + offset));
140         }
141 }
142
143 static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
144                                 uchar fnamecmp_type, char *buf, int len)
145 {
146         write_int(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 }
155
156 /* This is also used by receive.c with f_out = -1. */
157 int read_item_attrs(int f_in, int f_out, int ndx, uchar *type_ptr,
158                     char *buf, int *len_ptr)
159 {
160         int len;
161         uchar fnamecmp_type = FNAMECMP_FNAME;
162         int iflags = protocol_version >= 29 ? read_shortint(f_in)
163                    : ITEM_TRANSFER | ITEM_MISSING_DATA;
164
165         /* Handle the new keep-alive (no-op) packet. */
166         if (ndx == the_file_list->count && iflags == ITEM_IS_NEW)
167                 ;
168         else if (ndx < 0 || ndx >= the_file_list->count) {
169                 rprintf(FERROR, "Invalid file index: %d (count=%d) [%s]\n",
170                         ndx, the_file_list->count, who_am_i());
171                 exit_cleanup(RERR_PROTOCOL);
172         } else if (iflags == ITEM_IS_NEW) {
173                 rprintf(FERROR, "Invalid itemized flag word: %x [%s]\n",
174                         iflags, who_am_i());
175                 exit_cleanup(RERR_PROTOCOL);
176         }
177
178         if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
179                 fnamecmp_type = read_byte(f_in);
180         *type_ptr = fnamecmp_type;
181
182         if (iflags & ITEM_XNAME_FOLLOWS) {
183                 if ((len = read_vstring(f_in, buf, MAXPATHLEN)) < 0)
184                         exit_cleanup(RERR_PROTOCOL);
185         } else {
186                 *buf = '\0';
187                 len = -1;
188         }
189         *len_ptr = len;
190
191         if (iflags & ITEM_TRANSFER) {
192                 if (!S_ISREG(the_file_list->files[ndx]->mode)) {
193                         rprintf(FERROR,
194                                 "received request to transfer non-regular file: %d [%s]\n",
195                                 ndx, who_am_i());
196                         exit_cleanup(RERR_PROTOCOL);
197                 }
198         } else if (f_out >= 0) {
199                 write_ndx_and_attrs(f_out, ndx, iflags,
200                                     fnamecmp_type, buf, len);
201         }
202
203         return iflags;
204 }
205
206 void send_files(struct file_list *flist, int f_out, int f_in)
207 {
208         int fd = -1;
209         struct sum_struct *s;
210         struct map_struct *mbuf = NULL;
211         STRUCT_STAT st;
212         char *fname2, fname[MAXPATHLEN];
213         char xname[MAXPATHLEN];
214         uchar fnamecmp_type;
215         int iflags, xlen;
216         struct file_struct *file;
217         int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
218         struct stats initial_stats;
219         int save_make_backups = make_backups;
220         int itemizing = am_daemon ? daemon_log_format_has_i
221                       : !am_server && log_format_has_i;
222         int f_xfer = write_batch < 0 ? batch_fd : f_out;
223         int i, j;
224
225         if (verbose > 2)
226                 rprintf(FINFO, "send_files starting\n");
227
228         while (1) {
229                 unsigned int offset;
230
231                 i = read_int(f_in);
232                 if (i == -1) {
233                         if (++phase > max_phase)
234                                 break;
235                         csum_length = SUM_LENGTH;
236                         if (verbose > 2)
237                                 rprintf(FINFO, "send_files phase=%d\n", phase);
238                         write_int(f_out, -1);
239                         /* For inplace: redo phase turns off the backup
240                          * flag so that we do a regular inplace send. */
241                         make_backups = 0;
242                         append_mode = 0;
243                         continue;
244                 }
245
246                 iflags = read_item_attrs(f_in, f_out, i, &fnamecmp_type,
247                                          xname, &xlen);
248                 if (iflags == ITEM_IS_NEW) /* no-op packet */
249                         continue;
250
251                 file = flist->files[i];
252                 if (file->dir.root) {
253                         /* N.B. We're sure that this fits, so offset is OK. */
254                         offset = strlcpy(fname, file->dir.root, sizeof fname);
255                         if (!offset || fname[offset-1] != '/')
256                                 fname[offset++] = '/';
257                 } else
258                         offset = 0;
259                 fname2 = f_name_to(file, fname + offset);
260
261                 if (verbose > 2)
262                         rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
263
264                 if (!(iflags & ITEM_TRANSFER)) {
265                         maybe_log_item(file, iflags, itemizing, xname);
266                         continue;
267                 }
268                 if (phase == 2) {
269                         rprintf(FERROR,
270                                 "got transfer request in phase 2 [%s]\n",
271                                 who_am_i());
272                         exit_cleanup(RERR_PROTOCOL);
273                 }
274
275                 updating_basis_file = inplace && (protocol_version >= 29
276                         ? fnamecmp_type == FNAMECMP_FNAME : !make_backups);
277
278                 stats.current_file_index = i;
279                 stats.num_transferred_files++;
280                 stats.total_transferred_size += file->length;
281
282                 if (!do_xfers) { /* log the transfer */
283                         if (!am_server && log_format)
284                                 log_item(file, &stats, iflags, NULL);
285                         write_ndx_and_attrs(f_out, i, iflags, fnamecmp_type,
286                                             xname, xlen);
287                         continue;
288                 }
289
290                 initial_stats = stats;
291
292                 if (!(s = receive_sums(f_in))) {
293                         io_error |= IOERR_GENERAL;
294                         rprintf(FERROR, "receive_sums failed\n");
295                         return;
296                 }
297
298                 fd = do_open(fname, O_RDONLY, 0);
299                 if (fd == -1) {
300                         if (errno == ENOENT) {
301                                 enum logcode c = am_daemon
302                                     && protocol_version < 28 ? FERROR
303                                                              : FINFO;
304                                 io_error |= IOERR_VANISHED;
305                                 rprintf(c, "file has vanished: %s\n",
306                                         full_fname(fname));
307                         } else {
308                                 io_error |= IOERR_GENERAL;
309                                 rsyserr(FERROR, errno,
310                                         "send_files failed to open %s",
311                                         full_fname(fname));
312                         }
313                         free_sums(s);
314                         continue;
315                 }
316
317                 /* map the local file */
318                 if (do_fstat(fd, &st) != 0) {
319                         io_error |= IOERR_GENERAL;
320                         rsyserr(FERROR, errno, "fstat failed");
321                         free_sums(s);
322                         close(fd);
323                         return;
324                 }
325
326                 if (st.st_size) {
327                         int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
328                         mbuf = map_file(fd, st.st_size, read_size, s->blength);
329                 } else
330                         mbuf = NULL;
331
332                 if (verbose > 2) {
333                         rprintf(FINFO, "send_files mapped %s of size %.0f\n",
334                                 safe_fname(fname), (double)st.st_size);
335                 }
336
337                 write_ndx_and_attrs(f_out, i, iflags, fnamecmp_type,
338                                     xname, xlen);
339                 write_sum_head(f_xfer, s);
340
341                 if (verbose > 2) {
342                         rprintf(FINFO, "calling match_sums %s\n",
343                                 safe_fname(fname));
344                 }
345
346                 if (log_before_transfer)
347                         log_item(file, &initial_stats, iflags, NULL);
348                 else if (!am_server && verbose && do_progress)
349                         rprintf(FINFO, "%s\n", safe_fname(fname2));
350
351                 set_compression(fname);
352
353                 match_sums(f_xfer, s, mbuf, st.st_size);
354                 if (do_progress)
355                         end_progress(st.st_size);
356
357                 if (!log_before_transfer)
358                         log_item(file, &initial_stats, iflags, NULL);
359
360                 if (mbuf) {
361                         j = unmap_file(mbuf);
362                         if (j) {
363                                 io_error |= IOERR_GENERAL;
364                                 rsyserr(FERROR, j,
365                                         "read errors mapping %s",
366                                         full_fname(fname));
367                         }
368                 }
369                 close(fd);
370
371                 free_sums(s);
372
373                 if (verbose > 2) {
374                         rprintf(FINFO, "sender finished %s\n",
375                                 safe_fname(fname));
376                 }
377
378                 /* Flag that we actually sent this entry. */
379                 file->flags |= FLAG_SENT;
380         }
381         make_backups = save_make_backups;
382
383         if (verbose > 2)
384                 rprintf(FINFO, "send files finished\n");
385
386         match_report();
387
388         write_int(f_out, -1);
389 }