patch from Alberto Accomazzi <aaccomazzi@cfa.harvard.edu> to add
[rsync/rsync.git] / receiver.c
index 03912ad..189aa41 100644 (file)
@@ -32,6 +32,7 @@ extern int preserve_hard_links;
 extern int cvs_exclude;
 extern int io_error;
 extern char *tmpdir;
+extern char *compare_dest;
 
 
 static struct delete_list {
@@ -222,7 +223,7 @@ static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname,
 
                        if (fd != -1 && write_file(fd,data,i) != i) {
                                rprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
-                               exit_cleanup(1);
+                               exit_cleanup(RERR_FILEIO);
                        }
                        offset += i;
                        continue;
@@ -248,7 +249,7 @@ static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname,
                if (fd != -1 && write_file(fd,map,len) != len) {
                        rprintf(FERROR,"write failed on %s : %s\n",
                                fname,strerror(errno));
-                       exit_cleanup(1);
+                       exit_cleanup(RERR_FILEIO);
                }
                offset += len;
        }
@@ -258,7 +259,7 @@ static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname,
        if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
                rprintf(FERROR,"write failed on %s : %s\n",
                        fname,strerror(errno));
-               exit_cleanup(1);
+               exit_cleanup(RERR_FILEIO);
        }
 
        sum_end(file_sum1);
@@ -284,12 +285,16 @@ int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
        STRUCT_STAT st;
        char *fname;
        char fnametmp[MAXPATHLEN];
+       char *fnamecmp;
+       char fnamecmpbuf[MAXPATHLEN];
        struct map_struct *buf;
        int i;
        struct file_struct *file;
        int phase=0;
        int recv_ok;
-       
+       extern struct stats stats;              
+       struct stats initial_stats;
+
        if (verbose > 2) {
                rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
        }
@@ -317,7 +322,7 @@ int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
                if (i < 0 || i >= flist->count) {
                        rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n", 
                                i, flist->count);
-                       exit_cleanup(1);
+                       exit_cleanup(RERR_PROTOCOL);
                }
 
                file = flist->files[i];
@@ -330,26 +335,39 @@ int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
                        fname = local_name;
 
                if (dry_run) {
-                       if (!am_server && verbose)
-                               rprintf(FINFO,"%s\n",fname);
+                       if (!am_server) {
+                               log_transfer(file, fname);
+                       }
                        continue;
                }
 
+               initial_stats = stats;
+
                if (verbose > 2)
                        rprintf(FINFO,"recv_files(%s)\n",fname);
 
+               fnamecmp = fname;
+
                /* open the file */  
-               fd1 = open(fname,O_RDONLY);
+               fd1 = open(fnamecmp,O_RDONLY);
+
+               if ((fd1 == -1) && (compare_dest != NULL)) {
+                       /* try the file at compare_dest instead */
+                       slprintf(fnamecmpbuf,MAXPATHLEN-1,"%s/%s",
+                                               compare_dest,fname);
+                       fnamecmp = fnamecmpbuf;
+                       fd1 = open(fnamecmp,O_RDONLY);
+               }
 
                if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
-                       rprintf(FERROR,"fstat %s : %s\n",fname,strerror(errno));
+                       rprintf(FERROR,"fstat %s : %s\n",fnamecmp,strerror(errno));
                        receive_data(f_in,NULL,-1,NULL,file->length);
                        close(fd1);
                        continue;
                }
 
                if (fd1 != -1 && !S_ISREG(st.st_mode)) {
-                       rprintf(FERROR,"%s : not a regular file (recv_files)\n",fname);
+                       rprintf(FERROR,"%s : not a regular file (recv_files)\n",fnamecmp);
                        receive_data(f_in,NULL,-1,NULL,file->length);
                        close(fd1);
                        continue;
@@ -358,7 +376,7 @@ int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
                if (fd1 != -1 && st.st_size > 0) {
                        buf = map_file(fd1,st.st_size);
                        if (verbose > 2)
-                               rprintf(FINFO,"recv mapped %s of size %d\n",fname,(int)st.st_size);
+                               rprintf(FINFO,"recv mapped %s of size %d\n",fnamecmp,(int)st.st_size);
                } else {
                        buf = NULL;
                }
@@ -398,13 +416,16 @@ int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
                        continue;
                }
       
-               cleanup_set(fnametmp, fname, file);
+               cleanup_set(fnametmp, fname, file, buf, fd1, fd2);
+
+               if (!am_server) {
+                       log_transfer(file, fname);
+               }
 
-               if (!am_server && verbose)
-                       rprintf(FINFO,"%s\n",fname);
-               
                /* recv file data */
                recv_ok = receive_data(f_in,buf,fd2,fname,file->length);
+
+               log_recv(file, &initial_stats);
                
                if (buf) unmap_file(buf);
                if (fd1 != -1) {
@@ -416,9 +437,9 @@ int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
                        rprintf(FINFO,"renaming %s to %s\n",fnametmp,fname);
 
                finish_transfer(fname, fnametmp, file);
-               
+
                cleanup_disable();
-               
+                               
                if (!recv_ok) {
                        if (csum_length == SUM_LENGTH) {
                                rprintf(FERROR,"ERROR: file corruption in %s. File changed during transfer?\n",