patch from Alberto Accomazzi <aaccomazzi@cfa.harvard.edu> to add
[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(RERR_FILEIO);
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(RERR_FILEIO);
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(RERR_FILEIO);
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         extern struct stats stats;              
296         struct stats initial_stats;
297
298         if (verbose > 2) {
299                 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
300         }
301
302         if (recurse && delete_mode && !local_name && flist->count>0) {
303                 delete_files(flist);
304         }
305
306         while (1) {      
307                 cleanup_disable();
308
309                 i = read_int(f_in);
310                 if (i == -1) {
311                         if (phase==0 && remote_version >= 13) {
312                                 phase++;
313                                 csum_length = SUM_LENGTH;
314                                 if (verbose > 2)
315                                         rprintf(FINFO,"recv_files phase=%d\n",phase);
316                                 write_int(f_gen,-1);
317                                 continue;
318                         }
319                         break;
320                 }
321
322                 if (i < 0 || i >= flist->count) {
323                         rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n", 
324                                 i, flist->count);
325                         exit_cleanup(RERR_PROTOCOL);
326                 }
327
328                 file = flist->files[i];
329                 fname = f_name(file);
330
331                 stats.num_transferred_files++;
332                 stats.total_transferred_size += file->length;
333
334                 if (local_name)
335                         fname = local_name;
336
337                 if (dry_run) {
338                         if (!am_server) {
339                                 log_transfer(file, fname);
340                         }
341                         continue;
342                 }
343
344                 initial_stats = stats;
345
346                 if (verbose > 2)
347                         rprintf(FINFO,"recv_files(%s)\n",fname);
348
349                 fnamecmp = fname;
350
351                 /* open the file */  
352                 fd1 = open(fnamecmp,O_RDONLY);
353
354                 if ((fd1 == -1) && (compare_dest != NULL)) {
355                         /* try the file at compare_dest instead */
356                         slprintf(fnamecmpbuf,MAXPATHLEN-1,"%s/%s",
357                                                 compare_dest,fname);
358                         fnamecmp = fnamecmpbuf;
359                         fd1 = open(fnamecmp,O_RDONLY);
360                 }
361
362                 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
363                         rprintf(FERROR,"fstat %s : %s\n",fnamecmp,strerror(errno));
364                         receive_data(f_in,NULL,-1,NULL,file->length);
365                         close(fd1);
366                         continue;
367                 }
368
369                 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
370                         rprintf(FERROR,"%s : not a regular file (recv_files)\n",fnamecmp);
371                         receive_data(f_in,NULL,-1,NULL,file->length);
372                         close(fd1);
373                         continue;
374                 }
375
376                 if (fd1 != -1 && st.st_size > 0) {
377                         buf = map_file(fd1,st.st_size);
378                         if (verbose > 2)
379                                 rprintf(FINFO,"recv mapped %s of size %d\n",fnamecmp,(int)st.st_size);
380                 } else {
381                         buf = NULL;
382                 }
383
384                 if (!get_tmpname(fnametmp,fname)) {
385                         if (buf) unmap_file(buf);
386                         if (fd1 != -1) close(fd1);
387                         continue;
388                 }
389
390                 if (NULL == do_mktemp(fnametmp)) {
391                         rprintf(FERROR,"mktemp %s failed\n",fnametmp);
392                         receive_data(f_in,buf,-1,NULL,file->length);
393                         if (buf) unmap_file(buf);
394                         if (fd1 != -1) close(fd1);
395                         continue;
396                 }
397
398                 /* we initially set the perms without the
399                    setuid/setgid bits to ensure that there is no race
400                    condition. They are then correctly updated after
401                    the lchown. Thanks to snabb@epipe.fi for pointing
402                    this out */
403                 fd2 = do_open(fnametmp,O_WRONLY|O_CREAT|O_EXCL,
404                               file->mode & ACCESSPERMS);
405
406                 if (fd2 == -1 && relative_paths && errno == ENOENT && 
407                     create_directory_path(fnametmp) == 0) {
408                         fd2 = do_open(fnametmp,O_WRONLY|O_CREAT|O_EXCL,
409                                       file->mode & ACCESSPERMS);
410                 }
411                 if (fd2 == -1) {
412                         rprintf(FERROR,"open %s : %s\n",fnametmp,strerror(errno));
413                         receive_data(f_in,buf,-1,NULL,file->length);
414                         if (buf) unmap_file(buf);
415                         if (fd1 != -1) close(fd1);
416                         continue;
417                 }
418       
419                 cleanup_set(fnametmp, fname, file, buf, fd1, fd2);
420
421                 if (!am_server) {
422                         log_transfer(file, fname);
423                 }
424
425                 /* recv file data */
426                 recv_ok = receive_data(f_in,buf,fd2,fname,file->length);
427
428                 log_recv(file, &initial_stats);
429                 
430                 if (buf) unmap_file(buf);
431                 if (fd1 != -1) {
432                         close(fd1);
433                 }
434                 close(fd2);
435                 
436                 if (verbose > 2)
437                         rprintf(FINFO,"renaming %s to %s\n",fnametmp,fname);
438
439                 finish_transfer(fname, fnametmp, file);
440
441                 cleanup_disable();
442                                 
443                 if (!recv_ok) {
444                         if (csum_length == SUM_LENGTH) {
445                                 rprintf(FERROR,"ERROR: file corruption in %s. File changed during transfer?\n",
446                                         fname);
447                         } else {
448                                 if (verbose > 1)
449                                         rprintf(FINFO,"redoing %s(%d)\n",fname,i);
450                                 write_int(f_gen,i);
451                         }
452                 }
453         }
454
455         if (preserve_hard_links)
456                 do_hard_links(flist);
457
458         /* now we need to fix any directory permissions that were 
459            modified during the transfer */
460         for (i = 0; i < flist->count; i++) {
461                 file = flist->files[i];
462                 if (!file->basename || !S_ISDIR(file->mode)) continue;
463                 recv_generator(f_name(file),flist,i,-1);
464         }
465
466         if (verbose > 2)
467                 rprintf(FINFO,"recv_files finished\n");
468         
469         return 0;
470 }
471