From d867229ba0fcc7f829047b7caaa2166685cadf6e Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 5 Apr 1998 06:43:38 +0000 Subject: [PATCH] handle sparse files more efficiently --- io.c | 75 ++++++++++++++++++++++++++++++++++++--------------------- rsync.c | 4 +-- rsync.h | 2 +- 3 files changed, 51 insertions(+), 30 deletions(-) diff --git a/io.c b/io.c index 1b2fb221..95177c4e 100644 --- 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 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 cf57296a..e359adc7 100644 --- 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 a669276b..9f517ade 100644 --- 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) -- 2.34.1