added some debugging code
[rsync/rsync.git] / io.c
... / ...
CommitLineData
1/*
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20/*
21 Utilities used in rsync
22
23 tridge, June 1996
24 */
25#include "rsync.h"
26
27static int64 total_written;
28static int64 total_read;
29
30extern int verbose;
31extern int sparse_files;
32extern int io_timeout;
33
34int64 write_total(void)
35{
36 return total_written;
37}
38
39int64 read_total(void)
40{
41 return total_read;
42}
43
44static int buffer_f_in = -1;
45
46void setup_nonblocking(int f_in,int f_out)
47{
48 set_blocking(f_out,0);
49 buffer_f_in = f_in;
50}
51
52
53static char *read_buffer;
54static char *read_buffer_p;
55static int read_buffer_len;
56static int read_buffer_size;
57
58
59/* This function was added to overcome a deadlock problem when using
60 * ssh. It looks like we can't allow our receive queue to get full or
61 * ssh will clag up. Uggh. */
62static void read_check(int f)
63{
64 int n;
65
66 if (f == -1) return;
67
68 if (read_buffer_len == 0) {
69 read_buffer_p = read_buffer;
70 }
71
72 if ((n=num_waiting(f)) <= 0)
73 return;
74
75 /* things could deteriorate if we read in really small chunks */
76 if (n < 10) n = 1024;
77
78 if (read_buffer_p != read_buffer) {
79 memmove(read_buffer,read_buffer_p,read_buffer_len);
80 read_buffer_p = read_buffer;
81 }
82
83 if (n > (read_buffer_size - read_buffer_len)) {
84 read_buffer_size += n;
85 if (!read_buffer)
86 read_buffer = (char *)malloc(read_buffer_size);
87 else
88 read_buffer = (char *)realloc(read_buffer,read_buffer_size);
89 if (!read_buffer) out_of_memory("read check");
90 read_buffer_p = read_buffer;
91 }
92
93 n = read(f,read_buffer+read_buffer_len,n);
94 if (n > 0) {
95 read_buffer_len += n;
96 }
97}
98
99static time_t last_io;
100
101
102static void check_timeout(void)
103{
104 time_t t;
105
106 if (!io_timeout) return;
107
108 if (!last_io) {
109 last_io = time(NULL);
110 return;
111 }
112
113 t = time(NULL);
114
115 if (last_io && io_timeout && (t-last_io)>io_timeout) {
116 fprintf(FERROR,"read timeout after %d second - exiting\n",
117 (int)(t-last_io));
118 exit_cleanup(1);
119 }
120}
121
122static int readfd(int fd,char *buffer,int N)
123{
124 int ret;
125 int total=0;
126 struct timeval tv;
127
128 if (read_buffer_len < N)
129 read_check(buffer_f_in);
130
131 while (total < N) {
132 if (read_buffer_len > 0 && buffer_f_in == fd) {
133 ret = MIN(read_buffer_len,N-total);
134 memcpy(buffer+total,read_buffer_p,ret);
135 read_buffer_p += ret;
136 read_buffer_len -= ret;
137 continue;
138 }
139
140 while ((ret = read(fd,buffer + total,N-total)) == -1) {
141 fd_set fds;
142
143 if (errno != EAGAIN && errno != EWOULDBLOCK)
144 return -1;
145 FD_ZERO(&fds);
146 FD_SET(fd, &fds);
147 tv.tv_sec = io_timeout;
148 tv.tv_usec = 0;
149
150 if (select(fd+1, &fds, NULL, NULL, &tv) != 1) {
151 check_timeout();
152 }
153 }
154
155 if (ret <= 0)
156 return total;
157 total += ret;
158 }
159
160 if (io_timeout)
161 last_io = time(NULL);
162 return total;
163}
164
165
166int read_int(int f)
167{
168 int ret;
169 char b[4];
170 if ((ret=readfd(f,b,4)) != 4) {
171 if (verbose > 1)
172 fprintf(FERROR,"(%d) Error reading %d bytes : %s\n",
173 getpid(),4,ret==-1?strerror(errno):"EOF");
174 exit_cleanup(1);
175 }
176 total_read += 4;
177 return IVAL(b,0);
178}
179
180int64 read_longint(int f)
181{
182 extern int remote_version;
183 int64 ret;
184 char b[8];
185 ret = read_int(f);
186
187 if (ret != -1) return ret;
188
189#ifndef HAVE_LONGLONG
190 fprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
191 exit_cleanup(1);
192#else
193 if (remote_version >= 16) {
194 if ((ret=readfd(f,b,8)) != 8) {
195 if (verbose > 1)
196 fprintf(FERROR,"(%d) Error reading %d bytes : %s\n",
197 getpid(),8,ret==-1?strerror(errno):"EOF");
198 exit_cleanup(1);
199 }
200 total_read += 8;
201 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
202 }
203#endif
204
205 return ret;
206}
207
208void read_buf(int f,char *buf,int len)
209{
210 int ret;
211 if ((ret=readfd(f,buf,len)) != len) {
212 if (verbose > 1)
213 fprintf(FERROR,"(%d) Error reading %d bytes : %s\n",
214 getpid(),len,ret==-1?strerror(errno):"EOF");
215 exit_cleanup(1);
216 }
217 total_read += len;
218}
219
220unsigned char read_byte(int f)
221{
222 unsigned char c;
223 read_buf(f,(char *)&c,1);
224 return c;
225}
226
227
228static char last_byte;
229static int last_sparse;
230
231int sparse_end(int f)
232{
233 if (last_sparse) {
234 lseek(f,-1,SEEK_CUR);
235 return (write(f,&last_byte,1) == 1 ? 0 : -1);
236 }
237 last_sparse = 0;
238 return 0;
239}
240
241int write_sparse(int f,char *buf,int len)
242{
243 int l1=0,l2=0;
244 int ret;
245
246 if (!sparse_files)
247 return write(f,buf,len);
248
249 for (l1=0;l1<len && buf[l1]==0;l1++) ;
250 for (l2=0;l2<(len-l1) && buf[len-(l2+1)]==0;l2++) ;
251
252 last_byte = buf[len-1];
253
254 if (l1 == len || l2 > 0)
255 last_sparse=1;
256
257 if (l1 > 0)
258 lseek(f,l1,SEEK_CUR);
259
260 if (l1 == len)
261 return len;
262
263 if ((ret=write(f,buf+l1,len-(l1+l2))) != len-(l1+l2)) {
264 if (ret == -1 || ret == 0) return ret;
265 return (l1+ret);
266 }
267
268 if (l2 > 0)
269 lseek(f,l2,SEEK_CUR);
270
271 return len;
272}
273
274
275static int writefd(int fd,char *buf,int len)
276{
277 int total = 0;
278 fd_set w_fds, r_fds;
279 int fd_count, count, got_select=0;
280 struct timeval tv;
281
282 if (buffer_f_in == -1)
283 return write(fd,buf,len);
284
285 while (total < len) {
286 int ret = write(fd,buf+total,len-total);
287
288 if (ret == 0) return total;
289
290 if (ret == -1 && !(errno == EWOULDBLOCK || errno == EAGAIN))
291 return -1;
292
293 if (ret == -1 && got_select) {
294 /* hmmm, we got a write select on the fd and then failed to write.
295 Why doesn't that mean that the fd is dead? It doesn't on some
296 systems it seems (eg. IRIX) */
297 u_sleep(1000);
298#if 0
299 fprintf(FERROR,"write exception\n");
300 exit_cleanup(1);
301#endif
302 }
303
304 got_select = 0;
305
306
307 if (ret == -1) {
308 read_check(buffer_f_in);
309
310 fd_count = fd+1;
311 FD_ZERO(&w_fds);
312 FD_ZERO(&r_fds);
313 FD_SET(fd,&w_fds);
314 if (buffer_f_in != -1) {
315 FD_SET(buffer_f_in,&r_fds);
316 if (buffer_f_in > fd)
317 fd_count = buffer_f_in+1;
318 }
319
320 tv.tv_sec = BLOCKING_TIMEOUT;
321 tv.tv_usec = 0;
322 count = select(fd_count,buffer_f_in == -1? NULL: &r_fds,
323 &w_fds,NULL,&tv);
324 if (count == -1 && errno != EINTR) {
325 if (verbose > 1)
326 fprintf(FERROR,"select error: %s\n", strerror(errno));
327 exit_cleanup(1);
328 }
329
330 if (count == 0) {
331 check_timeout();
332 continue;
333 }
334
335 if (FD_ISSET(fd, &w_fds)) {
336 got_select = 1;
337 }
338 } else {
339 total += ret;
340 }
341 }
342
343 if (io_timeout)
344 last_io = time(NULL);
345
346 return total;
347}
348
349
350
351void write_int(int f,int x)
352{
353 int ret;
354 char b[4];
355 SIVAL(b,0,x);
356 if ((ret=writefd(f,b,4)) != 4) {
357 fprintf(FERROR,"write_int failed : %s\n",
358 ret==-1?strerror(errno):"EOF");
359 exit_cleanup(1);
360 }
361 total_written += 4;
362}
363
364void write_longint(int f, int64 x)
365{
366 extern int remote_version;
367 char b[8];
368 int ret;
369
370 if (remote_version < 16 || x <= 0x7FFFFFFF) {
371 write_int(f, (int)x);
372 return;
373 }
374
375 write_int(f, -1);
376 SIVAL(b,0,(x&0xFFFFFFFF));
377 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
378
379 if ((ret=writefd(f,b,8)) != 8) {
380 fprintf(FERROR,"write_longint failed : %s\n",
381 ret==-1?strerror(errno):"EOF");
382 exit_cleanup(1);
383 }
384 total_written += 8;
385}
386
387void write_buf(int f,char *buf,int len)
388{
389 int ret;
390 if ((ret=writefd(f,buf,len)) != len) {
391 fprintf(FERROR,"write_buf failed : %s\n",
392 ret==-1?strerror(errno):"EOF");
393 exit_cleanup(1);
394 }
395 total_written += len;
396}
397
398
399void write_byte(int f,unsigned char c)
400{
401 write_buf(f,(char *)&c,1);
402}
403
404void write_flush(int f)
405{
406}
407
408