- while (total < size) {
- int n = MIN(size-total,bufsize);
- read_buf(fd_in,buf,n);
- if (write(fd_out,buf,n) != n)
- return total;
- total += n;
- }
- return total;
+
+static int write_sparse(int f,char *buf,int len)
+{
+ int l1=0,l2=0;
+ int ret;
+
+ 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];
+
+ if (l1 == len || l2 > 0)
+ last_sparse=1;
+
+ if (l1 > 0)
+ do_lseek(f,l1,SEEK_CUR);
+
+ 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)
+ do_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;