handle sparse files more efficiently
authorAndrew Tridgell <tridge@samba.org>
Sun, 5 Apr 1998 06:43:38 +0000 (06:43 +0000)
committerAndrew Tridgell <tridge@samba.org>
Sun, 5 Apr 1998 06:43:38 +0000 (06:43 +0000)
io.c
rsync.c
rsync.h

diff --git a/io.c b/io.c
index 1b2fb22..95177c4 100644 (file)
--- a/io.c
+++ b/io.c
@@ -237,45 +237,66 @@ static int last_sparse;
 
 int sparse_end(int f)
 {
-  if (last_sparse) {
-    lseek(f,-1,SEEK_CUR);
-    return (write(f,&last_byte,1) == 1 ? 0 : -1);
-  }
-  last_sparse = 0;
-  return 0;
+       if (last_sparse) {
+               lseek(f,-1,SEEK_CUR);
+               return (write(f,&last_byte,1) == 1 ? 0 : -1);
+       }
+       last_sparse = 0;
+       return 0;
 }
 
-int write_sparse(int f,char *buf,int len)
+
+static int write_sparse(int f,char *buf,int len)
 {
-  int l1=0,l2=0;
-  int ret;
+       int l1=0,l2=0;
+       int ret;
 
-  if (!sparse_files) 
-    return write(f,buf,len);
+       for (l1=0;l1<len && buf[l1]==0;l1++) ;
+       for (l2=0;l2<(len-l1) && buf[len-(l2+1)]==0;l2++) ;
 
-  for (l1=0;l1<len && buf[l1]==0;l1++) ;
-  for (l2=0;l2<(len-l1) && buf[len-(l2+1)]==0;l2++) ;
+       last_byte = buf[len-1];
 
-  last_byte = buf[len-1];
+       if (l1 == len || l2 > 0)
+               last_sparse=1;
 
-  if (l1 == len || l2 > 0)
-    last_sparse=1;
+       if (l1 > 0)
+               lseek(f,l1,SEEK_CUR);  
 
-  if (l1 > 0)
-    lseek(f,l1,SEEK_CUR);  
+       if (l1 == len) 
+               return len;
 
-  if (l1 == len) 
-    return len;
+       if ((ret=write(f,buf+l1,len-(l1+l2))) != len-(l1+l2)) {
+               if (ret == -1 || ret == 0) return ret;
+               return (l1+ret);
+       }
 
-  if ((ret=write(f,buf+l1,len-(l1+l2))) != len-(l1+l2)) {
-    if (ret == -1 || ret == 0) return ret;
-    return (l1+ret);
-  }
+       if (l2 > 0)
+               lseek(f,l2,SEEK_CUR);
+       
+       return len;
+}
+
+
+
+int write_file(int f,char *buf,int len)
+{
+       int ret = 0;
 
-  if (l2 > 0)
-    lseek(f,l2,SEEK_CUR);
+       if (!sparse_files) 
+               return write(f,buf,len);
 
-  return len;
+       while (len>0) {
+               int len1 = MIN(len, SPARSE_WRITE_SIZE);
+               int r1 = write_sparse(f, buf, len1);
+               if (r1 <= 0) {
+                       if (ret > 0) return ret;
+                       return r1;
+               }
+               len -= r1;
+               buf += r1;
+               ret += r1;
+       }
+       return ret;
 }
 
 
diff --git a/rsync.c b/rsync.c
index cf57296..e359adc 100644 (file)
--- a/rsync.c
+++ b/rsync.c
@@ -561,7 +561,7 @@ static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname)
 
       sum_update(data,i);
 
-      if (fd != -1 && write_sparse(fd,data,i) != i) {
+      if (fd != -1 && write_file(fd,data,i) != i) {
        fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
        exit_cleanup(1);
       }
@@ -582,7 +582,7 @@ static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname)
       see_token(map, len);
       sum_update(map,len);
 
-      if (fd != -1 && write_sparse(fd,map,len) != len) {
+      if (fd != -1 && write_file(fd,map,len) != len) {
        fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
        exit_cleanup(1);
       }
diff --git a/rsync.h b/rsync.h
index a669276..9f517ad 100644 (file)
--- a/rsync.h
+++ b/rsync.h
@@ -43,7 +43,7 @@
 #define MIN_PROTOCOL_VERSION 11
 #define MAX_PROTOCOL_VERSION 30
 
-#define SPARSE_WRITE_SIZE (4*1024)
+#define SPARSE_WRITE_SIZE (1024)
 #define WRITE_SIZE (32*1024)
 #define CHUNK_SIZE (32*1024)
 #define MAX_MAP_SIZE (4*1024*1024)