- We now accept an itemized-changes flag byte over the socket if we're
[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 itemize_changes;
24 extern int log_before_transfer;
25 extern int csum_length;
26 extern struct stats stats;
27 extern int io_error;
28 extern int dry_run;
29 extern int am_server;
30 extern int am_daemon;
31 extern int protocol_version;
32 extern int updating_basis_file;
33 extern int make_backups;
34 extern int do_progress;
35 extern int inplace;
36 extern char *log_format;
37 extern struct stats stats;
38
39
40 /**
41  * @file
42  *
43  * The sender gets checksums from the generator, calculates deltas,
44  * and transmits them to the receiver.  The sender process runs on the
45  * machine holding the source files.
46  **/
47
48 /**
49  * Receive the checksums for a buffer
50  **/
51 static struct sum_struct *receive_sums(int f)
52 {
53         struct sum_struct *s;
54         int32 i;
55         OFF_T offset = 0;
56
57         if (!(s = new(struct sum_struct)))
58                 out_of_memory("receive_sums");
59
60         read_sum_head(f, s);
61
62         s->sums = NULL;
63
64         if (verbose > 3) {
65                 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
66                         (double)s->count, (long)s->blength, (long)s->remainder);
67         }
68
69         if (s->count == 0)
70                 return(s);
71
72         if (!(s->sums = new_array(struct sum_buf, s->count)))
73                 out_of_memory("receive_sums");
74
75         for (i = 0; i < s->count; i++) {
76                 s->sums[i].sum1 = read_int(f);
77                 read_buf(f, s->sums[i].sum2, s->s2length);
78
79                 s->sums[i].offset = offset;
80                 s->sums[i].flags = 0;
81
82                 if (i == s->count-1 && s->remainder != 0)
83                         s->sums[i].len = s->remainder;
84                 else
85                         s->sums[i].len = s->blength;
86                 offset += s->sums[i].len;
87
88                 if (verbose > 3) {
89                         rprintf(FINFO,
90                                 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
91                                 i, s->sums[i].len, (double)s->sums[i].offset,
92                                 s->sums[i].sum1);
93                 }
94         }
95
96         s->flength = offset;
97
98         return s;
99 }
100
101
102
103 void send_files(struct file_list *flist, int f_out, int f_in)
104 {
105         int fd = -1;
106         struct sum_struct *s;
107         struct map_struct *mbuf = NULL;
108         STRUCT_STAT st;
109         char *fname2, fname[MAXPATHLEN];
110         int iflags;
111         struct file_struct *file;
112         int phase = 0;
113         struct stats initial_stats;
114         int save_make_backups = make_backups;
115         int i, j;
116
117         if (verbose > 2)
118                 rprintf(FINFO, "send_files starting\n");
119
120         while (1) {
121                 unsigned int offset;
122
123                 i = read_int(f_in);
124                 if (i == -1) {
125                         if (phase == 0) {
126                                 phase++;
127                                 csum_length = SUM_LENGTH;
128                                 write_int(f_out, -1);
129                                 if (verbose > 2)
130                                         rprintf(FINFO, "send_files phase=%d\n", phase);
131                                 /* For inplace: redo phase turns off the backup
132                                  * flag so that we do a regular inplace send. */
133                                 make_backups = 0;
134                                 continue;
135                         }
136                         break;
137                 }
138
139                 if (i < 0 || i >= flist->count) {
140                         rprintf(FERROR, "Invalid file index %d (count=%d)\n",
141                                 i, flist->count);
142                         exit_cleanup(RERR_PROTOCOL);
143                 }
144
145                 file = flist->files[i];
146
147                 if (itemize_changes) {
148                         iflags = read_byte(f_in);
149                         if (!(iflags & ITEM_UPDATING) || !S_ISREG(file->mode)) {
150                                 if (am_server) {
151                                         write_int(f_out, i);
152                                         write_byte(f_out, iflags);
153                                 } else
154                                         log_send(file, &stats, iflags);
155                                 continue;
156                         }
157                 } else
158                         iflags = ITEM_UPDATING | ITEM_MISSING_DATA;
159
160                 if (inplace && protocol_version >= 29) {
161                         uchar fnamecmp_type = read_byte(f_in);
162                         updating_basis_file = fnamecmp_type == FNAMECMP_FNAME;
163                 } else
164                         updating_basis_file = inplace && !make_backups;
165
166                 if (!S_ISREG(file->mode)) {
167                         rprintf(FERROR, "[%s] got index of non-regular file: %d\n",
168                                 who_am_i(), i);
169                         exit_cleanup(RERR_PROTOCOL);
170                 }
171
172                 stats.current_file_index = i;
173                 stats.num_transferred_files++;
174                 stats.total_transferred_size += file->length;
175
176                 if (file->dir.root) {
177                         /* N.B. We're sure that this fits, so offset is OK. */
178                         offset = strlcpy(fname, file->dir.root, sizeof fname);
179                         if (!offset || fname[offset-1] != '/')
180                                 fname[offset++] = '/';
181                 } else
182                         offset = 0;
183                 fname2 = f_name_to(file, fname + offset);
184
185                 if (verbose > 2)
186                         rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
187
188                 if (dry_run) { /* log the transfer */
189                         if (!am_server && verbose && !log_format)
190                                 rprintf(FINFO, "%s\n", safe_fname(fname2));
191                         else if (!am_server)
192                                 log_send(file, &stats, iflags);
193                         write_int(f_out, i);
194                         if (itemize_changes)
195                                 write_byte(f_out, iflags);
196                         continue;
197                 }
198
199                 initial_stats = stats;
200
201                 if (!(s = receive_sums(f_in))) {
202                         io_error |= IOERR_GENERAL;
203                         rprintf(FERROR, "receive_sums failed\n");
204                         return;
205                 }
206
207                 fd = do_open(fname, O_RDONLY, 0);
208                 if (fd == -1) {
209                         if (errno == ENOENT) {
210                                 enum logcode c = am_daemon
211                                     && protocol_version < 28 ? FERROR
212                                                              : FINFO;
213                                 io_error |= IOERR_VANISHED;
214                                 rprintf(c, "file has vanished: %s\n",
215                                         full_fname(fname));
216                         } else {
217                                 io_error |= IOERR_GENERAL;
218                                 rsyserr(FERROR, errno,
219                                         "send_files failed to open %s",
220                                         full_fname(fname));
221                         }
222                         free_sums(s);
223                         continue;
224                 }
225
226                 /* map the local file */
227                 if (do_fstat(fd, &st) != 0) {
228                         io_error |= IOERR_GENERAL;
229                         rsyserr(FERROR, errno, "fstat failed");
230                         free_sums(s);
231                         close(fd);
232                         return;
233                 }
234
235                 if (st.st_size) {
236                         int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
237                         mbuf = map_file(fd, st.st_size, read_size, s->blength);
238                 } else
239                         mbuf = NULL;
240
241                 if (verbose > 2) {
242                         rprintf(FINFO, "send_files mapped %s of size %.0f\n",
243                                 safe_fname(fname), (double)st.st_size);
244                 }
245
246                 write_int(f_out, i);
247                 if (itemize_changes)
248                         write_byte(f_out, iflags);
249                 write_sum_head(f_out, s);
250
251                 if (verbose > 2) {
252                         rprintf(FINFO, "calling match_sums %s\n",
253                                 safe_fname(fname));
254                 }
255
256                 if (log_before_transfer)
257                         log_send(file, &initial_stats, iflags);
258                 else if (!am_server && verbose && (!log_format || do_progress))
259                         rprintf(FINFO, "%s\n", safe_fname(fname2));
260
261                 set_compression(fname);
262
263                 match_sums(f_out, s, mbuf, st.st_size);
264                 if (!log_before_transfer)
265                         log_send(file, &initial_stats, iflags);
266
267                 if (mbuf) {
268                         j = unmap_file(mbuf);
269                         if (j) {
270                                 io_error |= IOERR_GENERAL;
271                                 rsyserr(FERROR, j,
272                                         "read errors mapping %s",
273                                         full_fname(fname));
274                         }
275                 }
276                 close(fd);
277
278                 free_sums(s);
279
280                 if (verbose > 2) {
281                         rprintf(FINFO, "sender finished %s\n",
282                                 safe_fname(fname));
283                 }
284         }
285         make_backups = save_make_backups;
286
287         if (verbose > 2)
288                 rprintf(FINFO, "send files finished\n");
289
290         match_report();
291
292         write_int(f_out, -1);
293 }