Log items that are unchanged if verbose > 1.
[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 log_before_transfer;
24 extern int itemize_changes;
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 (protocol_version >= 29) {
148                         iflags = read_shortint(f_in);
149                         if (!(iflags & ITEM_UPDATING) || !S_ISREG(file->mode)) {
150                                 if (am_server) {
151                                         write_int(f_out, i);
152                                         write_shortint(f_out, iflags);
153                                 } else if (itemize_changes || verbose > 1
154                                     || iflags & ITEM_UPDATING
155                                     || (S_ISDIR(file->mode)
156                                      && iflags & ITEM_REPORT_TIME))
157                                         log_send(file, &stats, iflags);
158                                 continue;
159                         }
160                 } else
161                         iflags = ITEM_UPDATING | ITEM_MISSING_DATA;
162
163                 if (inplace && protocol_version >= 29) {
164                         uchar fnamecmp_type = read_byte(f_in);
165                         updating_basis_file = fnamecmp_type == FNAMECMP_FNAME;
166                 } else
167                         updating_basis_file = inplace && !make_backups;
168
169                 if (!S_ISREG(file->mode)) {
170                         rprintf(FERROR, "[%s] got index of non-regular file: %d\n",
171                                 who_am_i(), i);
172                         exit_cleanup(RERR_PROTOCOL);
173                 }
174
175                 stats.current_file_index = i;
176                 stats.num_transferred_files++;
177                 stats.total_transferred_size += file->length;
178
179                 if (file->dir.root) {
180                         /* N.B. We're sure that this fits, so offset is OK. */
181                         offset = strlcpy(fname, file->dir.root, sizeof fname);
182                         if (!offset || fname[offset-1] != '/')
183                                 fname[offset++] = '/';
184                 } else
185                         offset = 0;
186                 fname2 = f_name_to(file, fname + offset);
187
188                 if (verbose > 2)
189                         rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
190
191                 if (dry_run) { /* log the transfer */
192                         if (!am_server && log_format)
193                                 log_send(file, &stats, iflags);
194                         write_int(f_out, i);
195                         if (protocol_version >= 29)
196                                 write_shortint(f_out, iflags);
197                         continue;
198                 }
199
200                 initial_stats = stats;
201
202                 if (!(s = receive_sums(f_in))) {
203                         io_error |= IOERR_GENERAL;
204                         rprintf(FERROR, "receive_sums failed\n");
205                         return;
206                 }
207
208                 fd = do_open(fname, O_RDONLY, 0);
209                 if (fd == -1) {
210                         if (errno == ENOENT) {
211                                 enum logcode c = am_daemon
212                                     && protocol_version < 28 ? FERROR
213                                                              : FINFO;
214                                 io_error |= IOERR_VANISHED;
215                                 rprintf(c, "file has vanished: %s\n",
216                                         full_fname(fname));
217                         } else {
218                                 io_error |= IOERR_GENERAL;
219                                 rsyserr(FERROR, errno,
220                                         "send_files failed to open %s",
221                                         full_fname(fname));
222                         }
223                         free_sums(s);
224                         continue;
225                 }
226
227                 /* map the local file */
228                 if (do_fstat(fd, &st) != 0) {
229                         io_error |= IOERR_GENERAL;
230                         rsyserr(FERROR, errno, "fstat failed");
231                         free_sums(s);
232                         close(fd);
233                         return;
234                 }
235
236                 if (st.st_size) {
237                         int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
238                         mbuf = map_file(fd, st.st_size, read_size, s->blength);
239                 } else
240                         mbuf = NULL;
241
242                 if (verbose > 2) {
243                         rprintf(FINFO, "send_files mapped %s of size %.0f\n",
244                                 safe_fname(fname), (double)st.st_size);
245                 }
246
247                 write_int(f_out, i);
248                 if (protocol_version >= 29)
249                         write_shortint(f_out, iflags);
250                 write_sum_head(f_out, s);
251
252                 if (verbose > 2) {
253                         rprintf(FINFO, "calling match_sums %s\n",
254                                 safe_fname(fname));
255                 }
256
257                 if (log_before_transfer)
258                         log_send(file, &initial_stats, iflags);
259                 else if (!am_server && verbose && do_progress)
260                         rprintf(FINFO, "%s\n", safe_fname(fname2));
261
262                 set_compression(fname);
263
264                 match_sums(f_out, s, mbuf, st.st_size);
265                 if (!log_before_transfer)
266                         log_send(file, &initial_stats, iflags);
267
268                 if (mbuf) {
269                         j = unmap_file(mbuf);
270                         if (j) {
271                                 io_error |= IOERR_GENERAL;
272                                 rsyserr(FERROR, j,
273                                         "read errors mapping %s",
274                                         full_fname(fname));
275                         }
276                 }
277                 close(fd);
278
279                 free_sums(s);
280
281                 if (verbose > 2) {
282                         rprintf(FINFO, "sender finished %s\n",
283                                 safe_fname(fname));
284                 }
285         }
286         make_backups = save_make_backups;
287
288         if (verbose > 2)
289                 rprintf(FINFO, "send files finished\n");
290
291         match_report();
292
293         write_int(f_out, -1);
294 }