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