X-Git-Url: https://mattmccutchen.net/rsync/rsync.git/blobdiff_plain/6ba9279fb2c52280cedaaca94783caf44ae9d324..a0b65b1805e323ffb8b439d8cf2a2ba162503cfc:/io.c diff --git a/io.c b/io.c index b5ceff12..95177c4e 100644 --- a/io.c +++ b/io.c @@ -134,6 +134,7 @@ static int readfd(int fd,char *buffer,int N) memcpy(buffer+total,read_buffer_p,ret); read_buffer_p += ret; read_buffer_len -= ret; + total += ret; continue; } @@ -217,6 +218,12 @@ void read_buf(int f,char *buf,int len) total_read += len; } +void read_sbuf(int f,char *buf,int len) +{ + read_buf(f,buf,len); + buf[len] = 0; +} + unsigned char read_byte(int f) { unsigned char c; @@ -230,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 (l2 > 0) + lseek(f,l2,SEEK_CUR); + + 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 (l2 > 0) - lseek(f,l2,SEEK_CUR); - return len; +int write_file(int f,char *buf,int len) +{ + int ret = 0; + + if (!sparse_files) + return write(f,buf,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; }