Split code to generate "rwx-----" strings into lib/permstring.c so it
[rsync/rsync.git] / sender.c
CommitLineData
2f03f956
AT
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
22extern int verbose;
23extern int remote_version;
24extern int csum_length;
25extern struct stats stats;
26extern int io_error;
27extern int dry_run;
28extern int am_server;
29
30
31/*
32 receive the checksums for a buffer
33 */
34static 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)
5f808dfb
AT
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);
2f03f956
AT
75 }
76
77 s->flength = offset;
78
79 return s;
80}
81
82
83
84void send_files(struct file_list *flist,int f_out,int f_in)
85{
c1659c79 86 int fd = -1;
2f03f956 87 struct sum_struct *s;
c1659c79 88 struct map_struct *buf = NULL;
2f03f956
AT
89 STRUCT_STAT st;
90 char fname[MAXPATHLEN];
91 int i;
92 struct file_struct *file;
93 int phase = 0;
1b7c47cb
AT
94 extern struct stats stats;
95 struct stats initial_stats;
6902ed17 96 extern int write_batch; /* dw */
6902ed17
MP
97 extern int read_batch; /* dw */
98 int checksums_match; /* dw */
99 int buff_len; /* dw */
100 char buff[CHUNK_SIZE]; /* dw */
101 int j; /* dw */
102 int done; /* dw */
2f03f956
AT
103
104 if (verbose > 2)
105 rprintf(FINFO,"send_files starting\n");
106
2f03f956
AT
107 while (1) {
108 int offset=0;
109
110 i = read_int(f_in);
111 if (i == -1) {
112 if (phase==0 && remote_version >= 13) {
113 phase++;
114 csum_length = SUM_LENGTH;
115 write_int(f_out,-1);
116 if (verbose > 2)
117 rprintf(FINFO,"send_files phase=%d\n",phase);
118 continue;
119 }
120 break;
121 }
122
123 if (i < 0 || i >= flist->count) {
124 rprintf(FERROR,"Invalid file index %d (count=%d)\n",
125 i, flist->count);
65417579 126 exit_cleanup(RERR_PROTOCOL);
2f03f956
AT
127 }
128
129 file = flist->files[i];
130
131 stats.num_transferred_files++;
132 stats.total_transferred_size += file->length;
133
134 fname[0] = 0;
135 if (file->basedir) {
37f9805d 136 strlcpy(fname,file->basedir,MAXPATHLEN);
2f03f956
AT
137 if (strlen(fname) == MAXPATHLEN-1) {
138 io_error = 1;
139 rprintf(FERROR, "send_files failed on long-named directory %s\n",
140 fname);
141 return;
142 }
37f9805d 143 strlcat(fname,"/",MAXPATHLEN);
2f03f956
AT
144 offset = strlen(file->basedir)+1;
145 }
37f9805d 146 strlcat(fname,f_name(file),MAXPATHLEN);
2f03f956
AT
147
148 if (verbose > 2)
149 rprintf(FINFO,"send_files(%d,%s)\n",i,fname);
150
151 if (dry_run) {
11a5a3c7
AT
152 if (!am_server) {
153 log_transfer(file, fname+offset);
154 }
2f03f956
AT
155 write_int(f_out,i);
156 continue;
157 }
158
1b7c47cb
AT
159 initial_stats = stats;
160
2f03f956 161 s = receive_sums(f_in);
6902ed17
MP
162 if (write_batch) /* dw */
163 write_batch_csum_info(&i,flist->count,s);
2f03f956
AT
164 if (!s) {
165 io_error = 1;
166 rprintf(FERROR,"receive_sums failed\n");
167 return;
168 }
169
6902ed17 170 if (!read_batch) {
c1659c79
MP
171 fd = do_open(fname, O_RDONLY, 0);
172 if (fd == -1) {
173 io_error = 1;
174 rprintf(FERROR,"send_files failed to open %s: %s\n",
175 fname,strerror(errno));
176 free_sums(s);
177 continue;
178 }
2f03f956 179
c1659c79
MP
180 /* map the local file */
181 if (do_fstat(fd,&st) != 0) {
182 io_error = 1;
183 rprintf(FERROR,"fstat failed : %s\n",strerror(errno));
184 free_sums(s);
185 close(fd);
186 return;
187 }
2f03f956 188
c1659c79
MP
189 if (st.st_size > 0) {
190 buf = map_file(fd,st.st_size);
191 } else {
192 buf = NULL;
193 }
2f03f956 194
c1659c79
MP
195 if (verbose > 2)
196 rprintf(FINFO,"send_files mapped %s of size %.0f\n",
197 fname,(double)st.st_size);
11a5a3c7 198
c1659c79 199 write_int(f_out,i);
2f03f956 200
c1659c79
MP
201 if (write_batch)
202 write_batch_delta_file((char *)&i,sizeof(i));
6902ed17 203
c1659c79
MP
204 write_int(f_out,s->count);
205 write_int(f_out,s->n);
206 write_int(f_out,s->remainder);
6902ed17 207 }
2f03f956
AT
208
209 if (verbose > 2)
6902ed17
MP
210 if (!read_batch)
211 rprintf(FINFO,"calling match_sums %s\n",fname);
2f03f956 212
11a5a3c7
AT
213 if (!am_server) {
214 log_transfer(file, fname+offset);
215 }
83fff1aa
AT
216
217 set_compression(fname);
1b7c47cb 218
6902ed17
MP
219 if (read_batch) { /* dw */
220 /* read checksums originally computed on sender side */
221 read_batch_csum_info(i, s, &checksums_match);
222 if (checksums_match) {
223 read_batch_delta_file( (char *) &j, sizeof(int) );
224 if (j != i) { /* if flist index entries don't match*/
225 rprintf(FINFO,"index mismatch in send_files\n");
226 rprintf(FINFO,"read index = %d flist ndx = %d\n",j,i);
227 close_batch_delta_file();
228 close_batch_csums_file();
229 exit_cleanup(1);
230 }
231 else {
232 write_int(f_out,j);
233 write_int(f_out,s->count);
234 write_int(f_out,s->n);
235 write_int(f_out,s->remainder);
236 done=0;
237 while (!done) {
238 read_batch_delta_file( (char *) &buff_len, sizeof(int) );
239 write_int(f_out,buff_len);
240 if (buff_len == 0) {
241 done = 1;
242 }
243 else {
244 if (buff_len > 0) {
245 read_batch_delta_file(buff, buff_len);
246 write_buf(f_out,buff,buff_len);
247 }
248 }
249 } /* end while */
250 read_batch_delta_file( buff, MD4_SUM_LENGTH);
251 write_buf(f_out, buff, MD4_SUM_LENGTH);
252
253 } /* j=i */
254 } else { /* not checksum match */
07e95008
MP
255 rprintf (FINFO,"readbatch & checksums don't match\n");
256 rprintf (FINFO,"filename=%s is being skipped\n",
257 fname);
6902ed17
MP
258 continue;
259 }
260 } else {
261 match_sums(f_out,s,buf,st.st_size);
262 log_send(file, &initial_stats);
263 }
264
265 if (!read_batch) { /* dw */
266 if (buf) unmap_file(buf);
267 close(fd);
268 }
2f03f956
AT
269
270 free_sums(s);
271
272 if (verbose > 2)
273 rprintf(FINFO,"sender finished %s\n",fname);
274 }
275
276 if (verbose > 2)
277 rprintf(FINFO,"send files finished\n");
278
279 match_report();
280
281 write_int(f_out,-1);
6902ed17
MP
282 if (write_batch || read_batch) { /* dw */
283 close_batch_csums_file();
284 close_batch_delta_file();
285 }
286
2f03f956
AT
287}
288
289
290
291
292