- document the rsync:// URL format
[rsync/rsync.git] / receiver.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 recurse;
24extern int delete_mode;
25extern int remote_version;
26extern int csum_length;
27extern struct stats stats;
28extern int dry_run;
29extern int am_server;
30extern int relative_paths;
31extern int preserve_hard_links;
32extern int cvs_exclude;
33extern int io_error;
34extern char *tmpdir;
375a4556 35extern char *compare_dest;
2f03f956
AT
36
37
38static struct delete_list {
39 dev_t dev;
40 INO_T inode;
41} *delete_list;
42static int dlist_len, dlist_alloc_len;
43
44
45/* yuck! This function wouldn't have been necessary if I had the sorting
46 algorithm right. Unfortunately fixing the sorting algorithm would introduce
47 a backward incompatibility as file list indexes are sent over the link.
48*/
49static int delete_already_done(struct file_list *flist,int j)
50{
51 int i;
52 STRUCT_STAT st;
53
54 if (link_stat(f_name(flist->files[j]), &st)) return 1;
55
56 for (i=0;i<dlist_len;i++) {
57 if (st.st_ino == delete_list[i].inode &&
58 st.st_dev == delete_list[i].dev)
59 return 1;
60 }
61
62 return 0;
63}
64
65static void add_delete_entry(struct file_struct *file)
66{
67 if (dlist_len == dlist_alloc_len) {
68 dlist_alloc_len += 1024;
69 delete_list = (struct delete_list *)Realloc(delete_list, sizeof(delete_list[0])*dlist_alloc_len);
70 if (!delete_list) out_of_memory("add_delete_entry");
71 }
72
73 delete_list[dlist_len].dev = file->dev;
74 delete_list[dlist_len].inode = file->inode;
75 dlist_len++;
76
77 if (verbose > 3)
78 rprintf(FINFO,"added %s to delete list\n", f_name(file));
79}
80
81static void delete_one(struct file_struct *f)
82{
83 if (!S_ISDIR(f->mode)) {
84 if (do_unlink(f_name(f)) != 0) {
85 rprintf(FERROR,"unlink %s : %s\n",f_name(f),strerror(errno));
86 } else if (verbose) {
87 rprintf(FINFO,"deleting %s\n",f_name(f));
88 }
89 } else {
90 if (do_rmdir(f_name(f)) != 0) {
91 if (errno != ENOTEMPTY && errno != EEXIST)
92 rprintf(FERROR,"rmdir %s : %s\n",f_name(f),strerror(errno));
93 } else if (verbose) {
94 rprintf(FINFO,"deleting directory %s\n",f_name(f));
95 }
96 }
97}
98
99
100
101
102/* this deletes any files on the receiving side that are not present
103 on the sending side. For version 1.6.4 I have changed the behaviour
104 to match more closely what most people seem to expect of this option */
105static void delete_files(struct file_list *flist)
106{
107 struct file_list *local_file_list;
108 int i, j;
109 char *name;
110
111 if (cvs_exclude)
112 add_cvs_excludes();
113
114 if (io_error) {
115 rprintf(FINFO,"IO error encountered - skipping file deletion\n");
116 return;
117 }
118
119 for (j=0;j<flist->count;j++) {
120 if (!S_ISDIR(flist->files[j]->mode) ||
121 !(flist->files[j]->flags & FLAG_DELETE)) continue;
122
123 if (remote_version < 19 &&
124 delete_already_done(flist, j)) continue;
125
126 name = strdup(f_name(flist->files[j]));
127
128 if (!(local_file_list = send_file_list(-1,1,&name))) {
129 free(name);
130 continue;
131 }
132
133 if (verbose > 1)
134 rprintf(FINFO,"deleting in %s\n", name);
135
136 for (i=local_file_list->count-1;i>=0;i--) {
137 if (!local_file_list->files[i]->basename) continue;
138 if (remote_version < 19 &&
139 S_ISDIR(local_file_list->files[i]->mode))
140 add_delete_entry(local_file_list->files[i]);
141 if (-1 == flist_find(flist,local_file_list->files[i])) {
142 delete_one(local_file_list->files[i]);
143 }
144 }
145 flist_free(local_file_list);
146 free(name);
147 }
148}
149
150
151static int get_tmpname(char *fnametmp, char *fname)
152{
153 char *f;
154
155 /* open tmp file */
156 if (tmpdir) {
157 f = strrchr(fname,'/');
158 if (f == NULL)
159 f = fname;
160 else
161 f++;
162 if (strlen(tmpdir)+strlen(f)+10 > MAXPATHLEN) {
163 rprintf(FERROR,"filename too long\n");
164 return 0;
165 }
166 slprintf(fnametmp,MAXPATHLEN-1, "%s/.%s.XXXXXX",tmpdir,f);
167 return 1;
168 }
169
170 f = strrchr(fname,'/');
171
172 if (strlen(fname)+9 > MAXPATHLEN) {
173 rprintf(FERROR,"filename too long\n");
174 return 0;
175 }
176
177 if (f) {
178 *f = 0;
179 slprintf(fnametmp,MAXPATHLEN-1,"%s/.%s.XXXXXX",
180 fname,f+1);
181 *f = '/';
182 } else {
183 slprintf(fnametmp,MAXPATHLEN-1,".%s.XXXXXX",fname);
184 }
185
186 return 1;
187}
188
189
190static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname,
191 OFF_T total_size)
192{
193 int i,n,remainder,len,count;
194 OFF_T offset = 0;
195 OFF_T offset2;
196 char *data;
197 static char file_sum1[MD4_SUM_LENGTH];
198 static char file_sum2[MD4_SUM_LENGTH];
199 char *map=NULL;
200
201 count = read_int(f_in);
202 n = read_int(f_in);
203 remainder = read_int(f_in);
204
205 sum_init();
206
207 for (i=recv_token(f_in,&data); i != 0; i=recv_token(f_in,&data)) {
208
209 show_progress(offset, total_size);
210
211 if (i > 0) {
212 extern int cleanup_got_literal;
213
214 if (verbose > 3) {
215 rprintf(FINFO,"data recv %d at %d\n",
216 i,(int)offset);
217 }
218
219 stats.literal_data += i;
220 cleanup_got_literal = 1;
221
222 sum_update(data,i);
223
224 if (fd != -1 && write_file(fd,data,i) != i) {
225 rprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
226 exit_cleanup(1);
227 }
228 offset += i;
229 continue;
230 }
231
232 i = -(i+1);
233 offset2 = i*n;
234 len = n;
235 if (i == count-1 && remainder != 0)
236 len = remainder;
237
238 stats.matched_data += len;
239
240 if (verbose > 3)
241 rprintf(FINFO,"chunk[%d] of size %d at %d offset=%d\n",
242 i,len,(int)offset2,(int)offset);
243
244 map = map_ptr(buf,offset2,len);
245
246 see_token(map, len);
247 sum_update(map,len);
248
249 if (fd != -1 && write_file(fd,map,len) != len) {
250 rprintf(FERROR,"write failed on %s : %s\n",
251 fname,strerror(errno));
252 exit_cleanup(1);
253 }
254 offset += len;
255 }
256
257 end_progress();
258
259 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
260 rprintf(FERROR,"write failed on %s : %s\n",
261 fname,strerror(errno));
262 exit_cleanup(1);
263 }
264
265 sum_end(file_sum1);
266
267 if (remote_version >= 14) {
268 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
269 if (verbose > 2) {
270 rprintf(FINFO,"got file_sum\n");
271 }
272 if (fd != -1 &&
273 memcmp(file_sum1,file_sum2,MD4_SUM_LENGTH) != 0) {
274 return 0;
275 }
276 }
277 return 1;
278}
279
280
281
282int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
283{
284 int fd1,fd2;
285 STRUCT_STAT st;
286 char *fname;
287 char fnametmp[MAXPATHLEN];
375a4556
DD
288 char *fnamecmp;
289 char fnamecmpbuf[MAXPATHLEN];
2f03f956
AT
290 struct map_struct *buf;
291 int i;
292 struct file_struct *file;
293 int phase=0;
294 int recv_ok;
11a5a3c7 295
2f03f956
AT
296 if (verbose > 2) {
297 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
298 }
299
300 if (recurse && delete_mode && !local_name && flist->count>0) {
301 delete_files(flist);
302 }
303
304 while (1) {
305 cleanup_disable();
306
307 i = read_int(f_in);
308 if (i == -1) {
309 if (phase==0 && remote_version >= 13) {
310 phase++;
311 csum_length = SUM_LENGTH;
312 if (verbose > 2)
313 rprintf(FINFO,"recv_files phase=%d\n",phase);
314 write_int(f_gen,-1);
315 continue;
316 }
317 break;
318 }
319
320 if (i < 0 || i >= flist->count) {
321 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
322 i, flist->count);
323 exit_cleanup(1);
324 }
325
326 file = flist->files[i];
327 fname = f_name(file);
328
329 stats.num_transferred_files++;
330 stats.total_transferred_size += file->length;
331
332 if (local_name)
333 fname = local_name;
334
335 if (dry_run) {
11a5a3c7
AT
336 if (!am_server) {
337 log_transfer(file, fname);
338 }
2f03f956
AT
339 continue;
340 }
341
342 if (verbose > 2)
343 rprintf(FINFO,"recv_files(%s)\n",fname);
344
375a4556
DD
345 fnamecmp = fname;
346
2f03f956 347 /* open the file */
375a4556
DD
348 fd1 = open(fnamecmp,O_RDONLY);
349
350 if ((fd1 == -1) && (compare_dest != NULL)) {
351 /* try the file at compare_dest instead */
352 slprintf(fnamecmpbuf,MAXPATHLEN-1,"%s/%s",
353 compare_dest,fname);
354 fnamecmp = fnamecmpbuf;
355 fd1 = open(fnamecmp,O_RDONLY);
356 }
2f03f956
AT
357
358 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
375a4556 359 rprintf(FERROR,"fstat %s : %s\n",fnamecmp,strerror(errno));
2f03f956
AT
360 receive_data(f_in,NULL,-1,NULL,file->length);
361 close(fd1);
362 continue;
363 }
364
365 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
375a4556 366 rprintf(FERROR,"%s : not a regular file (recv_files)\n",fnamecmp);
2f03f956
AT
367 receive_data(f_in,NULL,-1,NULL,file->length);
368 close(fd1);
369 continue;
370 }
371
372 if (fd1 != -1 && st.st_size > 0) {
373 buf = map_file(fd1,st.st_size);
374 if (verbose > 2)
375a4556 375 rprintf(FINFO,"recv mapped %s of size %d\n",fnamecmp,(int)st.st_size);
2f03f956
AT
376 } else {
377 buf = NULL;
378 }
379
380 if (!get_tmpname(fnametmp,fname)) {
381 if (buf) unmap_file(buf);
382 if (fd1 != -1) close(fd1);
383 continue;
384 }
385
386 if (NULL == do_mktemp(fnametmp)) {
387 rprintf(FERROR,"mktemp %s failed\n",fnametmp);
388 receive_data(f_in,buf,-1,NULL,file->length);
389 if (buf) unmap_file(buf);
390 if (fd1 != -1) close(fd1);
391 continue;
392 }
393
394 /* we initially set the perms without the
395 setuid/setgid bits to ensure that there is no race
396 condition. They are then correctly updated after
397 the lchown. Thanks to snabb@epipe.fi for pointing
398 this out */
399 fd2 = do_open(fnametmp,O_WRONLY|O_CREAT|O_EXCL,
400 file->mode & ACCESSPERMS);
401
402 if (fd2 == -1 && relative_paths && errno == ENOENT &&
403 create_directory_path(fnametmp) == 0) {
404 fd2 = do_open(fnametmp,O_WRONLY|O_CREAT|O_EXCL,
405 file->mode & ACCESSPERMS);
406 }
407 if (fd2 == -1) {
408 rprintf(FERROR,"open %s : %s\n",fnametmp,strerror(errno));
409 receive_data(f_in,buf,-1,NULL,file->length);
410 if (buf) unmap_file(buf);
411 if (fd1 != -1) close(fd1);
412 continue;
413 }
414
c6b81a98 415 cleanup_set(fnametmp, fname, file, buf, fd1, fd2);
2f03f956 416
11a5a3c7
AT
417 if (!am_server) {
418 log_transfer(file, fname);
419 }
420
421 log_recv(file);
2f03f956
AT
422
423 /* recv file data */
424 recv_ok = receive_data(f_in,buf,fd2,fname,file->length);
425
426 if (buf) unmap_file(buf);
427 if (fd1 != -1) {
428 close(fd1);
429 }
430 close(fd2);
431
432 if (verbose > 2)
433 rprintf(FINFO,"renaming %s to %s\n",fnametmp,fname);
434
435 finish_transfer(fname, fnametmp, file);
c6b81a98 436
2f03f956 437 cleanup_disable();
c6b81a98 438
2f03f956
AT
439 if (!recv_ok) {
440 if (csum_length == SUM_LENGTH) {
441 rprintf(FERROR,"ERROR: file corruption in %s. File changed during transfer?\n",
442 fname);
443 } else {
444 if (verbose > 1)
445 rprintf(FINFO,"redoing %s(%d)\n",fname,i);
446 write_int(f_gen,i);
447 }
448 }
449 }
450
451 if (preserve_hard_links)
452 do_hard_links(flist);
453
454 /* now we need to fix any directory permissions that were
455 modified during the transfer */
456 for (i = 0; i < flist->count; i++) {
457 file = flist->files[i];
458 if (!file->basename || !S_ISDIR(file->mode)) continue;
459 recv_generator(f_name(file),flist,i,-1);
460 }
461
462 if (verbose > 2)
463 rprintf(FINFO,"recv_files finished\n");
464
465 return 0;
466}
467