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;
35 extern char *compare_dest;
38 static struct delete_list {
42 static int dlist_len, dlist_alloc_len;
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.
49 static int delete_already_done(struct file_list *flist,int j)
54 if (link_stat(f_name(flist->files[j]), &st)) return 1;
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)
65 static void add_delete_entry(struct file_struct *file)
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");
73 delete_list[dlist_len].dev = file->dev;
74 delete_list[dlist_len].inode = file->inode;
78 rprintf(FINFO,"added %s to delete list\n", f_name(file));
81 static void delete_one(struct file_struct *f)
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));
87 rprintf(FINFO,"deleting %s\n",f_name(f));
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));
94 rprintf(FINFO,"deleting directory %s\n",f_name(f));
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)
107 struct file_list *local_file_list;
115 rprintf(FINFO,"IO error encountered - skipping file deletion\n");
119 for (j=0;j<flist->count;j++) {
120 if (!S_ISDIR(flist->files[j]->mode) ||
121 !(flist->files[j]->flags & FLAG_DELETE)) continue;
123 if (remote_version < 19 &&
124 delete_already_done(flist, j)) continue;
126 name = strdup(f_name(flist->files[j]));
128 if (!(local_file_list = send_file_list(-1,1,&name))) {
134 rprintf(FINFO,"deleting in %s\n", name);
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]);
145 flist_free(local_file_list);
151 static int get_tmpname(char *fnametmp, char *fname)
157 f = strrchr(fname,'/');
162 if (strlen(tmpdir)+strlen(f)+10 > MAXPATHLEN) {
163 rprintf(FERROR,"filename too long\n");
166 slprintf(fnametmp,MAXPATHLEN-1, "%s/.%s.XXXXXX",tmpdir,f);
170 f = strrchr(fname,'/');
172 if (strlen(fname)+9 > MAXPATHLEN) {
173 rprintf(FERROR,"filename too long\n");
179 slprintf(fnametmp,MAXPATHLEN-1,"%s/.%s.XXXXXX",
183 slprintf(fnametmp,MAXPATHLEN-1,".%s.XXXXXX",fname);
190 static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname,
193 int i,n,remainder,len,count;
197 static char file_sum1[MD4_SUM_LENGTH];
198 static char file_sum2[MD4_SUM_LENGTH];
201 count = read_int(f_in);
203 remainder = read_int(f_in);
207 for (i=recv_token(f_in,&data); i != 0; i=recv_token(f_in,&data)) {
209 show_progress(offset, total_size);
212 extern int cleanup_got_literal;
215 rprintf(FINFO,"data recv %d at %d\n",
219 stats.literal_data += i;
220 cleanup_got_literal = 1;
224 if (fd != -1 && write_file(fd,data,i) != i) {
225 rprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
235 if (i == count-1 && remainder != 0)
238 stats.matched_data += len;
241 rprintf(FINFO,"chunk[%d] of size %d at %d offset=%d\n",
242 i,len,(int)offset2,(int)offset);
244 map = map_ptr(buf,offset2,len);
249 if (fd != -1 && write_file(fd,map,len) != len) {
250 rprintf(FERROR,"write failed on %s : %s\n",
251 fname,strerror(errno));
259 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
260 rprintf(FERROR,"write failed on %s : %s\n",
261 fname,strerror(errno));
267 if (remote_version >= 14) {
268 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
270 rprintf(FINFO,"got file_sum\n");
273 memcmp(file_sum1,file_sum2,MD4_SUM_LENGTH) != 0) {
282 int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
287 char fnametmp[MAXPATHLEN];
289 char fnamecmpbuf[MAXPATHLEN];
290 struct map_struct *buf;
292 struct file_struct *file;
297 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
300 if (recurse && delete_mode && !local_name && flist->count>0) {
309 if (phase==0 && remote_version >= 13) {
311 csum_length = SUM_LENGTH;
313 rprintf(FINFO,"recv_files phase=%d\n",phase);
320 if (i < 0 || i >= flist->count) {
321 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
326 file = flist->files[i];
327 fname = f_name(file);
329 stats.num_transferred_files++;
330 stats.total_transferred_size += file->length;
337 log_transfer(file, fname);
343 rprintf(FINFO,"recv_files(%s)\n",fname);
348 fd1 = open(fnamecmp,O_RDONLY);
350 if ((fd1 == -1) && (compare_dest != NULL)) {
351 /* try the file at compare_dest instead */
352 slprintf(fnamecmpbuf,MAXPATHLEN-1,"%s/%s",
354 fnamecmp = fnamecmpbuf;
355 fd1 = open(fnamecmp,O_RDONLY);
358 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
359 rprintf(FERROR,"fstat %s : %s\n",fnamecmp,strerror(errno));
360 receive_data(f_in,NULL,-1,NULL,file->length);
365 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
366 rprintf(FERROR,"%s : not a regular file (recv_files)\n",fnamecmp);
367 receive_data(f_in,NULL,-1,NULL,file->length);
372 if (fd1 != -1 && st.st_size > 0) {
373 buf = map_file(fd1,st.st_size);
375 rprintf(FINFO,"recv mapped %s of size %d\n",fnamecmp,(int)st.st_size);
380 if (!get_tmpname(fnametmp,fname)) {
381 if (buf) unmap_file(buf);
382 if (fd1 != -1) close(fd1);
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);
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
399 fd2 = do_open(fnametmp,O_WRONLY|O_CREAT|O_EXCL,
400 file->mode & ACCESSPERMS);
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);
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);
415 cleanup_set(fnametmp, fname, file, buf, fd1, fd2);
418 log_transfer(file, fname);
424 recv_ok = receive_data(f_in,buf,fd2,fname,file->length);
426 if (buf) unmap_file(buf);
433 rprintf(FINFO,"renaming %s to %s\n",fnametmp,fname);
435 finish_transfer(fname, fnametmp, file);
440 if (csum_length == SUM_LENGTH) {
441 rprintf(FERROR,"ERROR: file corruption in %s. File changed during transfer?\n",
445 rprintf(FINFO,"redoing %s(%d)\n",fname,i);
451 if (preserve_hard_links)
452 do_hard_links(flist);
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);
463 rprintf(FINFO,"recv_files finished\n");