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