204933ff21d80dbac37288b721755e0d0b57589a
[rsync/rsync.git] / receiver.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 recurse;
24 extern int delete_mode;
25 extern int remote_version;
26 extern int csum_length;
27 extern struct stats stats;
28 extern int dry_run;
29 extern int am_server;
30 extern int relative_paths;
31 extern int preserve_hard_links;
32 extern int cvs_exclude;
33 extern int io_error;
34 extern char *tmpdir;
35 extern char *compare_dest;
36
37
38 static struct delete_list {
39         dev_t dev;
40         INO_T inode;
41 } *delete_list;
42 static 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 */
49 static 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
65 static 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
81 static 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 */
105 static 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
151 static 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
190 static 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
282 int 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];
288         char *fnamecmp;
289         char fnamecmpbuf[MAXPATHLEN];
290         struct map_struct *buf;
291         int i;
292         struct file_struct *file;
293         int phase=0;
294         int recv_ok;
295         
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) {
336                         if (!am_server && verbose)
337                                 rprintf(FINFO,"%s\n",fname);
338                         continue;
339                 }
340
341                 if (verbose > 2)
342                         rprintf(FINFO,"recv_files(%s)\n",fname);
343
344                 fnamecmp = fname;
345
346                 /* open the file */  
347                 fd1 = open(fnamecmp,O_RDONLY);
348
349                 if ((fd1 == -1) && (compare_dest != NULL)) {
350                         /* try the file at compare_dest instead */
351                         slprintf(fnamecmpbuf,MAXPATHLEN-1,"%s/%s",
352                                                 compare_dest,fname);
353                         fnamecmp = fnamecmpbuf;
354                         fd1 = open(fnamecmp,O_RDONLY);
355                 }
356
357                 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
358                         rprintf(FERROR,"fstat %s : %s\n",fnamecmp,strerror(errno));
359                         receive_data(f_in,NULL,-1,NULL,file->length);
360                         close(fd1);
361                         continue;
362                 }
363
364                 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
365                         rprintf(FERROR,"%s : not a regular file (recv_files)\n",fnamecmp);
366                         receive_data(f_in,NULL,-1,NULL,file->length);
367                         close(fd1);
368                         continue;
369                 }
370
371                 if (fd1 != -1 && st.st_size > 0) {
372                         buf = map_file(fd1,st.st_size);
373                         if (verbose > 2)
374                                 rprintf(FINFO,"recv mapped %s of size %d\n",fnamecmp,(int)st.st_size);
375                 } else {
376                         buf = NULL;
377                 }
378
379                 if (!get_tmpname(fnametmp,fname)) {
380                         if (buf) unmap_file(buf);
381                         if (fd1 != -1) close(fd1);
382                         continue;
383                 }
384
385                 if (NULL == do_mktemp(fnametmp)) {
386                         rprintf(FERROR,"mktemp %s failed\n",fnametmp);
387                         receive_data(f_in,buf,-1,NULL,file->length);
388                         if (buf) unmap_file(buf);
389                         if (fd1 != -1) close(fd1);
390                         continue;
391                 }
392
393                 /* we initially set the perms without the
394                    setuid/setgid bits to ensure that there is no race
395                    condition. They are then correctly updated after
396                    the lchown. Thanks to snabb@epipe.fi for pointing
397                    this out */
398                 fd2 = do_open(fnametmp,O_WRONLY|O_CREAT|O_EXCL,
399                               file->mode & ACCESSPERMS);
400
401                 if (fd2 == -1 && relative_paths && errno == ENOENT && 
402                     create_directory_path(fnametmp) == 0) {
403                         fd2 = do_open(fnametmp,O_WRONLY|O_CREAT|O_EXCL,
404                                       file->mode & ACCESSPERMS);
405                 }
406                 if (fd2 == -1) {
407                         rprintf(FERROR,"open %s : %s\n",fnametmp,strerror(errno));
408                         receive_data(f_in,buf,-1,NULL,file->length);
409                         if (buf) unmap_file(buf);
410                         if (fd1 != -1) close(fd1);
411                         continue;
412                 }
413       
414                 cleanup_set(fnametmp, fname, file, buf, fd1, fd2);
415
416                 if (!am_server && verbose)
417                         rprintf(FINFO,"%s\n",fname);
418                 
419                 /* recv file data */
420                 recv_ok = receive_data(f_in,buf,fd2,fname,file->length);
421                 
422                 if (buf) unmap_file(buf);
423                 if (fd1 != -1) {
424                         close(fd1);
425                 }
426                 close(fd2);
427                 
428                 if (verbose > 2)
429                         rprintf(FINFO,"renaming %s to %s\n",fnametmp,fname);
430
431                 finish_transfer(fname, fnametmp, file);
432
433                 cleanup_disable();
434                                 
435                 if (!recv_ok) {
436                         if (csum_length == SUM_LENGTH) {
437                                 rprintf(FERROR,"ERROR: file corruption in %s. File changed during transfer?\n",
438                                         fname);
439                         } else {
440                                 if (verbose > 1)
441                                         rprintf(FINFO,"redoing %s(%d)\n",fname,i);
442                                 write_int(f_gen,i);
443                         }
444                 }
445         }
446
447         if (preserve_hard_links)
448                 do_hard_links(flist);
449
450         /* now we need to fix any directory permissions that were 
451            modified during the transfer */
452         for (i = 0; i < flist->count; i++) {
453                 file = flist->files[i];
454                 if (!file->basename || !S_ISDIR(file->mode)) continue;
455                 recv_generator(f_name(file),flist,i,-1);
456         }
457
458         if (verbose > 2)
459                 rprintf(FINFO,"recv_files finished\n");
460         
461         return 0;
462 }
463