Another try at socklen_t: just check for it, and otherwise use int.
[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
97         if (verbose > 2)
98                 rprintf(FINFO,"send_files starting\n");
99
100         while (1) {
101                 int offset=0;
102
103                 i = read_int(f_in);
104                 if (i == -1) {
105                         if (phase==0 && remote_version >= 13) {
106                                 phase++;
107                                 csum_length = SUM_LENGTH;
108                                 write_int(f_out,-1);
109                                 if (verbose > 2)
110                                         rprintf(FINFO,"send_files phase=%d\n",phase);
111                                 continue;
112                         }
113                         break;
114                 }
115
116                 if (i < 0 || i >= flist->count) {
117                         rprintf(FERROR,"Invalid file index %d (count=%d)\n", 
118                                 i, flist->count);
119                         exit_cleanup(RERR_PROTOCOL);
120                 }
121
122                 file = flist->files[i];
123
124                 stats.num_transferred_files++;
125                 stats.total_transferred_size += file->length;
126
127                 fname[0] = 0;
128                 if (file->basedir) {
129                         strlcpy(fname,file->basedir,MAXPATHLEN);
130                         if (strlen(fname) == MAXPATHLEN-1) {
131                                 io_error = 1;
132                                 rprintf(FERROR, "send_files failed on long-named directory %s\n",
133                                         fname);
134                                 return;
135                         }
136                         strlcat(fname,"/",MAXPATHLEN);
137                         offset = strlen(file->basedir)+1;
138                 }
139                 strlcat(fname,f_name(file),MAXPATHLEN);
140           
141                 if (verbose > 2) 
142                         rprintf(FINFO,"send_files(%d,%s)\n",i,fname);
143           
144                 if (dry_run) {  
145                         if (!am_server) {
146                                 log_transfer(file, fname+offset);
147                         }
148                         write_int(f_out,i);
149                         continue;
150                 }
151
152                 initial_stats = stats;
153
154                 s = receive_sums(f_in);
155                 if (!s) {
156                         io_error = 1;
157                         rprintf(FERROR,"receive_sums failed\n");
158                         return;
159                 }
160           
161                 fd = do_open(fname, O_RDONLY, 0);
162                 if (fd == -1) {
163                         io_error = 1;
164                         rprintf(FERROR,"send_files failed to open %s: %s\n",
165                                 fname,strerror(errno));
166                         free_sums(s);
167                         continue;
168                 }
169           
170                 /* map the local file */
171                 if (do_fstat(fd,&st) != 0) {
172                         io_error = 1;
173                         rprintf(FERROR,"fstat failed : %s\n",strerror(errno));
174                         free_sums(s);
175                         close(fd);
176                         return;
177                 }
178           
179                 if (st.st_size > 0) {
180                         buf = map_file(fd,st.st_size);
181                 } else {
182                         buf = NULL;
183                 }
184           
185                 if (verbose > 2)
186                         rprintf(FINFO,"send_files mapped %s of size %.0f\n",
187                                 fname,(double)st.st_size);
188
189                 write_int(f_out,i);
190           
191                 write_int(f_out,s->count);
192                 write_int(f_out,s->n);
193                 write_int(f_out,s->remainder);
194           
195                 if (verbose > 2)
196                         rprintf(FINFO,"calling match_sums %s\n",fname);
197           
198                 if (!am_server) {
199                         log_transfer(file, fname+offset);
200                 }
201
202                 set_compression(fname);
203           
204                 match_sums(f_out,s,buf,st.st_size);
205
206                 log_send(file, &initial_stats);
207                   
208                 if (buf) unmap_file(buf);
209                 close(fd);
210           
211                 free_sums(s);
212           
213                 if (verbose > 2)
214                         rprintf(FINFO,"sender finished %s\n",fname);
215         }
216
217         if (verbose > 2)
218                 rprintf(FINFO,"send files finished\n");
219
220         match_report();
221
222         write_int(f_out,-1);
223 }
224
225
226
227
228