Detect and report when open or opendir succeed but read and
[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
29
30 /**
31  * @file
32  *
33  * The sender gets checksums from the generator, calculates deltas,
34  * and transmits them to the receiver.  The sender process runs on the
35  * machine holding the source files.
36  **/
37
38
39 void read_sum_head(int f, struct sum_struct *sum)
40 {
41         extern int protocol_version;
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         sum->remainder = read_int(f);
50 }
51
52 /**
53  * Receive the checksums for a buffer
54  **/
55 static struct sum_struct *receive_sums(int f)
56 {
57         struct sum_struct *s;
58         int i;
59         OFF_T offset = 0;
60
61         s = (struct sum_struct *)malloc(sizeof(*s));
62         if (!s) out_of_memory("receive_sums");
63
64         read_sum_head(f, s);
65
66         s->sums = NULL;
67
68         if (verbose > 3)
69                 rprintf(FINFO, "count=%ld n=%ld rem=%ld\n",
70                         (long) s->count, (long) s->blength,
71                         (long) s->remainder);
72
73         if (s->count == 0)
74                 return(s);
75
76         s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
77         if (!s->sums) out_of_memory("receive_sums");
78
79         for (i = 0; i < (int) s->count; i++) {
80                 s->sums[i].sum1 = read_int(f);
81                 read_buf(f, s->sums[i].sum2, s->s2length);
82
83                 s->sums[i].offset = offset;
84                 s->sums[i].i = i;
85
86                 if (i == (int) s->count-1 && s->remainder != 0) {
87                         s->sums[i].len = s->remainder;
88                 } else {
89                         s->sums[i].len = s->blength;
90                 }
91                 offset += s->sums[i].len;
92
93                 if (verbose > 3)
94                         rprintf(FINFO, "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
95                                 i, s->sums[i].len, (double)s->sums[i].offset, s->sums[i].sum1);
96         }
97
98         s->flength = offset;
99
100         return s;
101 }
102
103
104
105 void send_files(struct file_list *flist, int f_out, int f_in)
106 {
107         int fd = -1;
108         struct sum_struct *s;
109         struct map_struct *buf = NULL;
110         STRUCT_STAT st;
111         char fname[MAXPATHLEN];
112         int i;
113         struct file_struct *file;
114         int phase = 0;
115         extern struct stats stats;
116         struct stats initial_stats;
117         extern int write_batch;   /* dw */
118         extern int read_batch;    /* dw */
119         int checksums_match;   /* dw */
120         int buff_len;  /* dw */
121         char buff[CHUNK_SIZE];    /* dw */
122         int j;   /* dw */
123         int done;   /* dw */
124
125         if (verbose > 2)
126                 rprintf(FINFO, "send_files starting\n");
127
128         while (1) {
129                 int offset = 0;
130
131                 i = read_int(f_in);
132                 if (i == -1) {
133                         if (phase == 0) {
134                                 phase++;
135                                 csum_length = SUM_LENGTH;
136                                 write_int(f_out, -1);
137                                 if (verbose > 2)
138                                         rprintf(FINFO, "send_files phase=%d\n", phase);
139                                 continue;
140                         }
141                         break;
142                 }
143
144                 if (i < 0 || i >= flist->count) {
145                         rprintf(FERROR, "Invalid file index %d (count=%d)\n",
146                                 i, flist->count);
147                         exit_cleanup(RERR_PROTOCOL);
148                 }
149
150                 file = flist->files[i];
151
152                 stats.num_transferred_files++;
153                 stats.total_transferred_size += file->length;
154
155                 fname[0] = 0;
156                 if (file->basedir) {
157                         strlcpy(fname, file->basedir, MAXPATHLEN);
158                         if (strlen(fname) == MAXPATHLEN-1) {
159                                 io_error = 1;
160                                 rprintf(FERROR, "send_files failed on long-named directory %s\n",
161                                         full_fname(fname));
162                                 return;
163                         }
164                         strlcat(fname, "/", MAXPATHLEN);
165                         offset = strlen(file->basedir)+1;
166                 }
167                 strlcat(fname, f_name(file), MAXPATHLEN);
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 transfer */
174                                 rprintf(FINFO, "%s\n", fname+offset);
175                         }
176                         write_int(f_out, i);
177                         continue;
178                 }
179
180                 initial_stats = stats;
181
182                 s = receive_sums(f_in);
183                 if (!s) {
184                         io_error = 1;
185                         rprintf(FERROR, "receive_sums failed\n");
186                         return;
187                 }
188
189                 if (write_batch)
190                         write_batch_csum_info(&i, flist->count, s);
191
192                 if (!read_batch) {
193                         fd = do_open(fname, O_RDONLY, 0);
194                         if (fd == -1) {
195                                 io_error = 1;
196                                 rprintf(FERROR, "send_files failed to open %s: %s\n",
197                                         full_fname(fname), strerror(errno));
198                                 free_sums(s);
199                                 continue;
200                         }
201
202                         /* map the local file */
203                         if (do_fstat(fd, &st) != 0) {
204                                 io_error = 1;
205                                 rprintf(FERROR, "fstat failed: %s\n", strerror(errno));
206                                 free_sums(s);
207                                 close(fd);
208                                 return;
209                         }
210
211                         if (st.st_size > 0) {
212                                 buf = map_file(fd, st.st_size);
213                         } else {
214                                 buf = NULL;
215                         }
216
217                         if (verbose > 2)
218                                 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
219                                         fname, (double)st.st_size);
220
221                         write_int(f_out, i);
222
223                         if (write_batch)
224                                 write_batch_delta_file((char *)&i, sizeof(i));
225
226                         write_sum_head(f_out, s);
227                 }
228
229                 if (verbose > 2 && !read_batch)
230                         rprintf(FINFO, "calling match_sums %s\n", fname);
231
232                 if (!am_server && verbose) {    /* log transfer */
233                         rprintf(FINFO, "%s\n", fname+offset);
234                 }
235
236                 set_compression(fname);
237
238                 if (read_batch) { /* dw */
239                         /* read checksums originally computed on sender side */
240                         read_batch_csum_info(i, s, &checksums_match);
241                         if (checksums_match) {
242                                 read_batch_delta_file( (char *) &j, sizeof(int) );
243                                 if (j != i) {    /* if flist index entries don't match*/
244                                         rprintf(FINFO, "index mismatch in send_files\n");
245                                         rprintf(FINFO, "read index = %d flist ndx = %d\n", j, i);
246                                         close_batch_delta_file();
247                                         close_batch_csums_file();
248                                         exit_cleanup(1);
249                                 } else {
250                                         write_int(f_out, j);
251                                         write_sum_head(f_out, s);
252                                         done = 0;
253                                         while (!done) {
254                                                 read_batch_delta_file( (char *) &buff_len, sizeof(int) );
255                                                 write_int(f_out, buff_len);
256                                                 if (buff_len == 0) {
257                                                         done = 1;
258                                                 } else {
259                                                         if (buff_len > 0) {
260                                                                 read_batch_delta_file(buff, buff_len);
261                                                                 write_buf(f_out, buff, buff_len);
262                                                         }
263                                                 }
264                                         }  /* end while  */
265                                         read_batch_delta_file( buff, MD4_SUM_LENGTH);
266                                         write_buf(f_out, buff, MD4_SUM_LENGTH);
267
268                                 }  /* j=i */
269                         } else {  /* not checksum match */
270                                 rprintf (FINFO, "readbatch & checksums don't match\n");
271                                 rprintf (FINFO, "filename=%s is being skipped\n", fname);
272                                 continue;
273                         }
274                 } else  {
275                         match_sums(f_out, s, buf, st.st_size);
276                         log_send(file, &initial_stats);
277                 }
278
279                 if (!read_batch) { /* dw */
280                         if (buf) {
281                                 j = unmap_file(buf);
282                                 if (j) {
283                                         io_error = 1;
284                                         rprintf(FERROR,
285                                             "read errors mapping %s: (%d) %s\n",
286                                             full_fname(fname),
287                                             j,
288                                             strerror(j));
289                                 }
290                         }
291                         close(fd);
292                 }
293
294                 free_sums(s);
295
296                 if (verbose > 2)
297                         rprintf(FINFO, "sender finished %s\n", fname);
298         }
299
300         if (verbose > 2)
301                 rprintf(FINFO, "send files finished\n");
302
303         match_report();
304
305         write_int(f_out, -1);
306         if (write_batch || read_batch) { /* dw */
307                 close_batch_csums_file();
308                 close_batch_delta_file();
309         }
310
311 }
312
313
314
315
316