2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
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.
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.
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.
24 extern int delete_mode;
25 extern int remote_version;
26 extern int csum_length;
27 extern struct stats stats;
30 extern int relative_paths;
31 extern int preserve_hard_links;
32 extern int cvs_exclude;
37 static struct delete_list {
41 static int dlist_len, dlist_alloc_len;
44 /* yuck! This function wouldn't have been necessary if I had the sorting
45 algorithm right. Unfortunately fixing the sorting algorithm would introduce
46 a backward incompatibility as file list indexes are sent over the link.
48 static int delete_already_done(struct file_list *flist,int j)
53 if (link_stat(f_name(flist->files[j]), &st)) return 1;
55 for (i=0;i<dlist_len;i++) {
56 if (st.st_ino == delete_list[i].inode &&
57 st.st_dev == delete_list[i].dev)
64 static void add_delete_entry(struct file_struct *file)
66 if (dlist_len == dlist_alloc_len) {
67 dlist_alloc_len += 1024;
68 delete_list = (struct delete_list *)Realloc(delete_list, sizeof(delete_list[0])*dlist_alloc_len);
69 if (!delete_list) out_of_memory("add_delete_entry");
72 delete_list[dlist_len].dev = file->dev;
73 delete_list[dlist_len].inode = file->inode;
77 rprintf(FINFO,"added %s to delete list\n", f_name(file));
80 static void delete_one(struct file_struct *f)
82 if (!S_ISDIR(f->mode)) {
83 if (do_unlink(f_name(f)) != 0) {
84 rprintf(FERROR,"unlink %s : %s\n",f_name(f),strerror(errno));
86 rprintf(FINFO,"deleting %s\n",f_name(f));
89 if (do_rmdir(f_name(f)) != 0) {
90 if (errno != ENOTEMPTY && errno != EEXIST)
91 rprintf(FERROR,"rmdir %s : %s\n",f_name(f),strerror(errno));
93 rprintf(FINFO,"deleting directory %s\n",f_name(f));
101 /* this deletes any files on the receiving side that are not present
102 on the sending side. For version 1.6.4 I have changed the behaviour
103 to match more closely what most people seem to expect of this option */
104 static void delete_files(struct file_list *flist)
106 struct file_list *local_file_list;
114 rprintf(FINFO,"IO error encountered - skipping file deletion\n");
118 for (j=0;j<flist->count;j++) {
119 if (!S_ISDIR(flist->files[j]->mode) ||
120 !(flist->files[j]->flags & FLAG_DELETE)) continue;
122 if (remote_version < 19 &&
123 delete_already_done(flist, j)) continue;
125 name = strdup(f_name(flist->files[j]));
127 if (!(local_file_list = send_file_list(-1,1,&name))) {
133 rprintf(FINFO,"deleting in %s\n", name);
135 for (i=local_file_list->count-1;i>=0;i--) {
136 if (!local_file_list->files[i]->basename) continue;
137 if (remote_version < 19 &&
138 S_ISDIR(local_file_list->files[i]->mode))
139 add_delete_entry(local_file_list->files[i]);
140 if (-1 == flist_find(flist,local_file_list->files[i])) {
141 delete_one(local_file_list->files[i]);
144 flist_free(local_file_list);
150 static int get_tmpname(char *fnametmp, char *fname)
156 f = strrchr(fname,'/');
161 if (strlen(tmpdir)+strlen(f)+10 > MAXPATHLEN) {
162 rprintf(FERROR,"filename too long\n");
165 slprintf(fnametmp,MAXPATHLEN-1, "%s/.%s.XXXXXX",tmpdir,f);
169 f = strrchr(fname,'/');
171 if (strlen(fname)+9 > MAXPATHLEN) {
172 rprintf(FERROR,"filename too long\n");
178 slprintf(fnametmp,MAXPATHLEN-1,"%s/.%s.XXXXXX",
182 slprintf(fnametmp,MAXPATHLEN-1,".%s.XXXXXX",fname);
189 static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname,
192 int i,n,remainder,len,count;
196 static char file_sum1[MD4_SUM_LENGTH];
197 static char file_sum2[MD4_SUM_LENGTH];
200 count = read_int(f_in);
202 remainder = read_int(f_in);
206 for (i=recv_token(f_in,&data); i != 0; i=recv_token(f_in,&data)) {
208 show_progress(offset, total_size);
211 extern int cleanup_got_literal;
214 rprintf(FINFO,"data recv %d at %d\n",
218 stats.literal_data += i;
219 cleanup_got_literal = 1;
223 if (fd != -1 && write_file(fd,data,i) != i) {
224 rprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
234 if (i == count-1 && remainder != 0)
237 stats.matched_data += len;
240 rprintf(FINFO,"chunk[%d] of size %d at %d offset=%d\n",
241 i,len,(int)offset2,(int)offset);
243 map = map_ptr(buf,offset2,len);
248 if (fd != -1 && write_file(fd,map,len) != len) {
249 rprintf(FERROR,"write failed on %s : %s\n",
250 fname,strerror(errno));
258 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
259 rprintf(FERROR,"write failed on %s : %s\n",
260 fname,strerror(errno));
266 if (remote_version >= 14) {
267 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
269 rprintf(FINFO,"got file_sum\n");
272 memcmp(file_sum1,file_sum2,MD4_SUM_LENGTH) != 0) {
281 int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
286 char fnametmp[MAXPATHLEN];
287 struct map_struct *buf;
289 struct file_struct *file;
294 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
297 if (recurse && delete_mode && !local_name && flist->count>0) {
306 if (phase==0 && remote_version >= 13) {
308 csum_length = SUM_LENGTH;
310 rprintf(FINFO,"recv_files phase=%d\n",phase);
317 if (i < 0 || i >= flist->count) {
318 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
323 file = flist->files[i];
324 fname = f_name(file);
326 stats.num_transferred_files++;
327 stats.total_transferred_size += file->length;
333 if (!am_server && verbose)
334 rprintf(FINFO,"%s\n",fname);
339 rprintf(FINFO,"recv_files(%s)\n",fname);
342 fd1 = open(fname,O_RDONLY);
344 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
345 rprintf(FERROR,"fstat %s : %s\n",fname,strerror(errno));
346 receive_data(f_in,NULL,-1,NULL,file->length);
351 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
352 rprintf(FERROR,"%s : not a regular file (recv_files)\n",fname);
353 receive_data(f_in,NULL,-1,NULL,file->length);
358 if (fd1 != -1 && st.st_size > 0) {
359 buf = map_file(fd1,st.st_size);
361 rprintf(FINFO,"recv mapped %s of size %d\n",fname,(int)st.st_size);
366 if (!get_tmpname(fnametmp,fname)) {
367 if (buf) unmap_file(buf);
368 if (fd1 != -1) close(fd1);
372 if (NULL == do_mktemp(fnametmp)) {
373 rprintf(FERROR,"mktemp %s failed\n",fnametmp);
374 receive_data(f_in,buf,-1,NULL,file->length);
375 if (buf) unmap_file(buf);
376 if (fd1 != -1) close(fd1);
380 /* we initially set the perms without the
381 setuid/setgid bits to ensure that there is no race
382 condition. They are then correctly updated after
383 the lchown. Thanks to snabb@epipe.fi for pointing
385 fd2 = do_open(fnametmp,O_WRONLY|O_CREAT|O_EXCL,
386 file->mode & ACCESSPERMS);
388 if (fd2 == -1 && relative_paths && errno == ENOENT &&
389 create_directory_path(fnametmp) == 0) {
390 fd2 = do_open(fnametmp,O_WRONLY|O_CREAT|O_EXCL,
391 file->mode & ACCESSPERMS);
394 rprintf(FERROR,"open %s : %s\n",fnametmp,strerror(errno));
395 receive_data(f_in,buf,-1,NULL,file->length);
396 if (buf) unmap_file(buf);
397 if (fd1 != -1) close(fd1);
401 cleanup_set(fnametmp, fname, file, buf, fd1, fd2);
403 if (!am_server && verbose)
404 rprintf(FINFO,"%s\n",fname);
407 recv_ok = receive_data(f_in,buf,fd2,fname,file->length);
409 if (buf) unmap_file(buf);
416 rprintf(FINFO,"renaming %s to %s\n",fnametmp,fname);
418 finish_transfer(fname, fnametmp, file);
423 if (csum_length == SUM_LENGTH) {
424 rprintf(FERROR,"ERROR: file corruption in %s. File changed during transfer?\n",
428 rprintf(FINFO,"redoing %s(%d)\n",fname,i);
434 if (preserve_hard_links)
435 do_hard_links(flist);
437 /* now we need to fix any directory permissions that were
438 modified during the transfer */
439 for (i = 0; i < flist->count; i++) {
440 file = flist->files[i];
441 if (!file->basename || !S_ISDIR(file->mode)) continue;
442 recv_generator(f_name(file),flist,i,-1);
446 rprintf(FINFO,"recv_files finished\n");