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