*/
#include "rsync.h"
-static off_t total_written;
-static off_t total_read;
+static int64 total_written;
+static int64 total_read;
extern int verbose;
extern int sparse_files;
+extern int io_timeout;
-off_t write_total(void)
+int64 write_total(void)
{
return total_written;
}
-off_t read_total(void)
+int64 read_total(void)
{
return total_read;
}
}
}
+static time_t last_io;
+
+
+static void check_timeout(void)
+{
+ time_t t;
+
+ if (!io_timeout) return;
+
+ if (!last_io) {
+ last_io = time(NULL);
+ return;
+ }
+
+ t = time(NULL);
+
+ if (last_io && io_timeout && (t-last_io)>io_timeout) {
+ fprintf(FERROR,"read timeout after %d second - exiting\n",
+ (int)(t-last_io));
+ exit_cleanup(1);
+ }
+}
static int readfd(int fd,char *buffer,int N)
{
- int ret;
- int total=0;
-
- if (read_buffer_len < N)
- read_check(buffer_f_in);
-
- while (total < N)
- {
- if (read_buffer_len > 0 && buffer_f_in == fd) {
- ret = MIN(read_buffer_len,N-total);
- memcpy(buffer+total,read_buffer_p,ret);
- read_buffer_p += ret;
- read_buffer_len -= ret;
- } else {
- while ((ret = read(fd,buffer + total,N - total)) == -1) {
- fd_set fds;
-
- if (errno != EAGAIN && errno != EWOULDBLOCK)
- return -1;
- FD_ZERO(&fds);
- FD_SET(fd, &fds);
- select(fd+1, &fds, NULL, NULL, NULL);
+ int ret;
+ int total=0;
+ struct timeval tv;
+
+ if (read_buffer_len < N)
+ read_check(buffer_f_in);
+
+ while (total < N) {
+ if (read_buffer_len > 0 && buffer_f_in == fd) {
+ ret = MIN(read_buffer_len,N-total);
+ memcpy(buffer+total,read_buffer_p,ret);
+ read_buffer_p += ret;
+ read_buffer_len -= ret;
+ total += ret;
+ continue;
+ }
+
+ while ((ret = read(fd,buffer + total,N-total)) == -1) {
+ fd_set fds;
+
+ if (errno != EAGAIN && errno != EWOULDBLOCK)
+ return -1;
+ FD_ZERO(&fds);
+ FD_SET(fd, &fds);
+ tv.tv_sec = io_timeout;
+ tv.tv_usec = 0;
+
+ if (select(fd+1, &fds, NULL, NULL, &tv) != 1) {
+ check_timeout();
+ }
+ }
+
+ if (ret <= 0)
+ return total;
+ total += ret;
}
- }
- if (ret <= 0)
+ if (io_timeout)
+ last_io = time(NULL);
return total;
- total += ret;
- }
- return total;
}
return IVAL(b,0);
}
-off_t read_longint(int f)
+int64 read_longint(int f)
{
extern int remote_version;
- off_t ret;
+ int64 ret;
char b[8];
ret = read_int(f);
- if (ret == -1 && remote_version >= 16) {
- if (sizeof(off_t) <= 4) {
- fprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
- exit_cleanup(1);
- }
+
+ if (ret != -1) return ret;
+
+#ifndef HAVE_LONGLONG
+ fprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
+ exit_cleanup(1);
+#else
+ if (remote_version >= 16) {
if ((ret=readfd(f,b,8)) != 8) {
if (verbose > 1)
fprintf(FERROR,"(%d) Error reading %d bytes : %s\n",
exit_cleanup(1);
}
total_read += 8;
- ret = IVAL(b,0) | (((off_t)IVAL(b,4))<<32);
+ ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
}
+#endif
+
return ret;
}
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;
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;
}
exit_cleanup(1);
}
- if (count == 0) continue;
+ if (count == 0) {
+ check_timeout();
+ continue;
+ }
if (FD_ISSET(fd, &w_fds)) {
got_select = 1;
}
}
+ if (io_timeout)
+ last_io = time(NULL);
+
return total;
}
total_written += 4;
}
-void write_longint(int f, off_t x)
+void write_longint(int f, int64 x)
{
extern int remote_version;
char b[8];