added resend logic
[rsync/rsync.git] / rsync.c
diff --git a/rsync.c b/rsync.c
index 5ae3695..647af67 100644 (file)
--- a/rsync.c
+++ b/rsync.c
@@ -26,6 +26,8 @@ extern int am_server;
 extern int always_checksum;
 extern time_t starttime;
 
+extern int remote_version;
+
 extern char *backup_suffix;
 
 extern int block_size;
@@ -81,7 +83,7 @@ static void send_sums(struct sum_struct *s,int f_out)
 
   generate approximately one checksum every n bytes
   */
-static struct sum_struct *generate_sums(char *buf,off_t len,int n)
+static struct sum_struct *generate_sums(struct map_struct *buf,off_t len,int n)
 {
   int i;
   struct sum_struct *s;
@@ -253,14 +255,14 @@ void recv_generator(char *fname,struct file_list *flist,int i,int f_out)
 {  
   int fd;
   struct stat st;
-  char *buf;
+  struct map_struct *buf;
   struct sum_struct *s;
   char sum[SUM_LENGTH];
   int statret;
   struct file_struct *file = &flist->files[i];
 
   if (verbose > 2)
-    fprintf(FERROR,"recv_generator(%s)\n",fname);
+    fprintf(FERROR,"recv_generator(%s,%d)\n",fname,i);
 
   statret = lstat(fname,&st);
 
@@ -381,38 +383,49 @@ void recv_generator(char *fname,struct file_list *flist,int i,int f_out)
   }
 
   if (verbose > 3)
-    fprintf(FERROR,"mapped %s of size %d\n",fname,(int)st.st_size);
+    fprintf(FERROR,"gen mapped %s of size %d\n",fname,(int)st.st_size);
 
   s = generate_sums(buf,st.st_size,block_size);
 
+  if (verbose > 2)
+    fprintf(FERROR,"sending sums for %d\n",i);
+
   write_int(f_out,i);
   send_sums(s,f_out);
   write_flush(f_out);
 
   close(fd);
-  unmap_file(buf,st.st_size);
+  unmap_file(buf);
 
   free_sums(s);
 }
 
 
 
-static void receive_data(int f_in,char *buf,int fd,char *fname)
+static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname)
 {
   int i,n,remainder,len,count;
   off_t offset = 0;
   off_t offset2;
+  char *data;
+  static char file_sum1[SUM_LENGTH];
+  static char file_sum2[SUM_LENGTH];
+  char *map=NULL;
 
   count = read_int(f_in);
   n = read_int(f_in);
   remainder = read_int(f_in);
 
-  for (i=read_int(f_in); i != 0; i=read_int(f_in)) {
+  sum_init();
+
+  for (i=recv_token(f_in,&data); i != 0; i=recv_token(f_in,&data)) {
     if (i > 0) {
       if (verbose > 3)
        fprintf(FERROR,"data recv %d at %d\n",i,(int)offset);
 
-      if (read_write(f_in,fd,i) != i) {
+      sum_update(data,i);
+
+      if (write_sparse(fd,data,i) != i) {
        fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
        exit_cleanup(1);
       }
@@ -428,7 +441,11 @@ static void receive_data(int f_in,char *buf,int fd,char *fname)
        fprintf(FERROR,"chunk[%d] of size %d at %d offset=%d\n",
                i,len,(int)offset2,(int)offset);
 
-      if (write_sparse(fd,map_ptr(buf,offset2,len),len) != len) {
+      map = map_ptr(buf,offset2,len);
+
+      sum_update(map,len);
+
+      if (write_sparse(fd,map,len) != len) {
        fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
        exit_cleanup(1);
       }
@@ -440,6 +457,17 @@ static void receive_data(int f_in,char *buf,int fd,char *fname)
     fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
     exit_cleanup(1);
   }
+
+  sum_end(file_sum1);
+
+  if (remote_version >= 14) {
+    read_buf(f_in,file_sum2,SUM_LENGTH);
+    if (verbose > 2)
+      fprintf(FERROR,"got file_sum\n");
+    if (memcmp(file_sum1,file_sum2,SUM_LENGTH) != 0)
+      return 0;
+  }
+  return 1;
 }
 
 
@@ -497,15 +525,17 @@ void sig_int(void)
 }
 
 
-int recv_files(int f_in,struct file_list *flist,char *local_name)
+int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
 {  
   int fd1,fd2;
   struct stat st;
   char *fname;
   char fnametmp[MAXPATHLEN];
-  char *buf;
+  struct map_struct *buf;
   int i;
   struct file_struct *file;
+  int phase=0;
+  int recv_ok;
 
   if (verbose > 2) {
     fprintf(FERROR,"recv_files(%d) starting\n",flist->count);
@@ -516,9 +546,20 @@ int recv_files(int f_in,struct file_list *flist,char *local_name)
   }
 
   while (1) 
-    {
+    {      
       i = read_int(f_in);
-      if (i == -1) break;
+      if (i == -1) {
+       if (phase==0 && remote_version >= 13) {
+         phase++;
+         csum_length = SUM_LENGTH;
+         if (verbose > 2)
+           fprintf(FERROR,"recv_files phase=%d\n",phase);
+         write_int(f_gen,-1);
+         write_flush(f_gen);
+         continue;
+       }
+       break;
+      }
 
       file = &flist->files[i];
       fname = file->name;
@@ -552,13 +593,12 @@ int recv_files(int f_in,struct file_list *flist,char *local_name)
 
       if (fd1 != -1 && st.st_size > 0) {
        buf = map_file(fd1,st.st_size);
+       if (verbose > 2)
+         fprintf(FERROR,"recv mapped %s of size %d\n",fname,(int)st.st_size);
       } else {
        buf = NULL;
       }
 
-      if (verbose > 2)
-       fprintf(FERROR,"mapped %s of size %d\n",fname,(int)st.st_size);
-
       /* open tmp file */
       sprintf(fnametmp,"%s.XXXXXX",fname);
       if (NULL == mktemp(fnametmp)) {
@@ -577,10 +617,10 @@ int recv_files(int f_in,struct file_list *flist,char *local_name)
        printf("%s\n",fname);
 
       /* recv file data */
-      receive_data(f_in,buf,fd2,fname);
+      recv_ok = receive_data(f_in,buf,fd2,fname);
 
       if (fd1 != -1) {
-       unmap_file(buf,st.st_size);
+       unmap_file(buf);
        close(fd1);
       }
       close(fd2);
@@ -606,6 +646,12 @@ int recv_files(int f_in,struct file_list *flist,char *local_name)
       cleanup_fname = NULL;
 
       set_perms(fname,file,NULL,0);
+
+      if (!recv_ok) {
+       if (verbose > 1)
+         fprintf(FERROR,"redoing %s(%d)\n",fname,i);
+       write_int(f_gen,i);
+      }
     }
 
   if (verbose > 2)
@@ -620,12 +666,13 @@ off_t send_files(struct file_list *flist,int f_out,int f_in)
 { 
   int fd;
   struct sum_struct *s;
-  char *buf;
+  struct map_struct *buf;
   struct stat st;
   char fname[MAXPATHLEN];  
   off_t total=0;
   int i;
   struct file_struct *file;
+  int phase = 0;
 
   if (verbose > 2)
     fprintf(FERROR,"send_files starting\n");
@@ -635,7 +682,18 @@ off_t send_files(struct file_list *flist,int f_out,int f_in)
   while (1) 
     {
       i = read_int(f_in);
-      if (i == -1) break;
+      if (i == -1) {
+       if (phase==0 && remote_version >= 13) {
+         phase++;
+         csum_length = SUM_LENGTH;
+         write_int(f_out,-1);
+         write_flush(f_out);
+         if (verbose > 2)
+           fprintf(FERROR,"send_files phase=%d\n",phase);
+         continue;
+       }
+       break;
+      }
 
       file = &flist->files[i];
 
@@ -700,7 +758,7 @@ off_t send_files(struct file_list *flist,int f_out,int f_in)
       match_sums(f_out,s,buf,st.st_size);
       write_flush(f_out);
       
-      unmap_file(buf,st.st_size);
+      unmap_file(buf);
       close(fd);
 
       free_sums(s);
@@ -724,9 +782,10 @@ off_t send_files(struct file_list *flist,int f_out,int f_in)
 
 
 
-void generate_files(int f,struct file_list *flist,char *local_name)
+void generate_files(int f,struct file_list *flist,char *local_name,int f_recv)
 {
   int i;
+  int phase=0;
 
   if (verbose > 2)
     fprintf(FERROR,"generator starting pid=%d count=%d\n",
@@ -747,8 +806,33 @@ void generate_files(int f,struct file_list *flist,char *local_name)
     recv_generator(local_name?local_name:file->name,
                   flist,i,f);
   }
+
+  phase++;
+  csum_length = SUM_LENGTH;
+  ignore_times=1;
+
+  if (verbose > 2)
+    fprintf(FERROR,"generate_files phase=%d\n",phase);
+
   write_int(f,-1);
   write_flush(f);
+
+  if (remote_version >= 13) {
+    for (i=read_int(f_recv); i != -1; i=read_int(f_recv)) {
+      struct file_struct *file = &flist->files[i];
+      recv_generator(local_name?local_name:file->name,
+                    flist,i,f);    
+    }
+
+    phase++;
+    if (verbose > 2)
+      fprintf(FERROR,"generate_files phase=%d\n",phase);
+
+    write_int(f,-1);
+    write_flush(f);
+  }
+
+
   if (verbose > 2)
     fprintf(FERROR,"generator wrote %d\n",write_total());
 }