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