don't show basedir in printed path (a aesthetic change only)
[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 off_t total_written;
28static off_t total_read;
29
30extern int verbose;
31extern int sparse_files;
32
33off_t write_total(void)
34{
35 return total_written;
36}
37
38off_t read_total(void)
39{
40 return total_read;
41}
42
43static int buffer_f_in = -1;
44
45void setup_nonblocking(int f_in,int f_out)
46{
47 set_blocking(f_out,0);
48 buffer_f_in = f_in;
49}
50
51
52static char *read_buffer;
53static char *read_buffer_p;
54static int read_buffer_len;
55static int read_buffer_size;
56
57
58/* This function was added to overcome a deadlock problem when using
59 * ssh. It looks like we can't allow our receive queue to get full or
60 * ssh will clag up. Uggh. */
61static void read_check(int f)
62{
63 int n;
64
65 if (f == -1) return;
66
67 if (read_buffer_len == 0) {
68 read_buffer_p = read_buffer;
69 }
70
71 if ((n=num_waiting(f)) <= 0)
72 return;
73
74 /* things could deteriorate if we read in really small chunks */
75 if (n < 10) n = 1024;
76
77 if (read_buffer_p != read_buffer) {
78 memmove(read_buffer,read_buffer_p,read_buffer_len);
79 read_buffer_p = read_buffer;
80 }
81
82 if (n > (read_buffer_size - read_buffer_len)) {
83 read_buffer_size += n;
84 if (!read_buffer)
85 read_buffer = (char *)malloc(read_buffer_size);
86 else
87 read_buffer = (char *)realloc(read_buffer,read_buffer_size);
88 if (!read_buffer) out_of_memory("read check");
89 read_buffer_p = read_buffer;
90 }
91
92 n = read(f,read_buffer+read_buffer_len,n);
93 if (n > 0) {
94 read_buffer_len += n;
95 }
96}
97
98
99static int readfd(int fd,char *buffer,int N)
100{
101 int ret;
102 int total=0;
103
104 if (read_buffer_len < N)
105 read_check(buffer_f_in);
106
107 while (total < N)
108 {
109 if (read_buffer_len > 0 && buffer_f_in == fd) {
110 ret = MIN(read_buffer_len,N-total);
111 memcpy(buffer+total,read_buffer_p,ret);
112 read_buffer_p += ret;
113 read_buffer_len -= ret;
114 } else {
115 while ((ret = read(fd,buffer + total,N - total)) == -1) {
116 fd_set fds;
117
118 if (errno != EAGAIN && errno != EWOULDBLOCK)
119 return -1;
120 FD_ZERO(&fds);
121 FD_SET(fd, &fds);
122 select(fd+1, &fds, NULL, NULL, NULL);
123 }
124 }
125
126 if (ret <= 0)
127 return total;
128 total += ret;
129 }
130 return total;
131}
132
133
134int read_int(int f)
135{
136 int ret;
137 char b[4];
138 if ((ret=readfd(f,b,4)) != 4) {
139 if (verbose > 1)
140 fprintf(FERROR,"(%d) Error reading %d bytes : %s\n",
141 getpid(),4,ret==-1?strerror(errno):"EOF");
142 exit_cleanup(1);
143 }
144 total_read += 4;
145 return IVAL(b,0);
146}
147
148off_t read_longint(int f)
149{
150 extern int remote_version;
151 off_t ret;
152 char b[8];
153 ret = read_int(f);
154 if (ret == -1 && remote_version >= 16) {
155 if (sizeof(off_t) <= 4) {
156 fprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
157 exit_cleanup(1);
158 }
159 if ((ret=readfd(f,b,8)) != 8) {
160 if (verbose > 1)
161 fprintf(FERROR,"(%d) Error reading %d bytes : %s\n",
162 getpid(),8,ret==-1?strerror(errno):"EOF");
163 exit_cleanup(1);
164 }
165 total_read += 8;
166 ret = IVAL(b,0) | (((off_t)IVAL(b,4))<<32);
167 }
168 return ret;
169}
170
171void read_buf(int f,char *buf,int len)
172{
173 int ret;
174 if ((ret=readfd(f,buf,len)) != len) {
175 if (verbose > 1)
176 fprintf(FERROR,"(%d) Error reading %d bytes : %s\n",
177 getpid(),len,ret==-1?strerror(errno):"EOF");
178 exit_cleanup(1);
179 }
180 total_read += len;
181}
182
183unsigned char read_byte(int f)
184{
185 unsigned char c;
186 read_buf(f,(char *)&c,1);
187 return c;
188}
189
190
191static char last_byte;
192static int last_sparse;
193
194int sparse_end(int f)
195{
196 if (last_sparse) {
197 lseek(f,-1,SEEK_CUR);
198 return (write(f,&last_byte,1) == 1 ? 0 : -1);
199 }
200 last_sparse = 0;
201 return 0;
202}
203
204int write_sparse(int f,char *buf,int len)
205{
206 int l1=0,l2=0;
207 int ret;
208
209 if (!sparse_files)
210 return write(f,buf,len);
211
212 for (l1=0;l1<len && buf[l1]==0;l1++) ;
213 for (l2=0;l2<(len-l1) && buf[len-(l2+1)]==0;l2++) ;
214
215 last_byte = buf[len-1];
216
217 if (l1 == len || l2 > 0)
218 last_sparse=1;
219
220 if (l1 > 0)
221 lseek(f,l1,SEEK_CUR);
222
223 if (l1 == len)
224 return len;
225
226 if ((ret=write(f,buf+l1,len-(l1+l2))) != len-(l1+l2)) {
227 if (ret == -1 || ret == 0) return ret;
228 return (l1+ret);
229 }
230
231 if (l2 > 0)
232 lseek(f,l2,SEEK_CUR);
233
234 return len;
235}
236
237
238static int writefd(int fd,char *buf,int len)
239{
240 int total = 0;
241 fd_set w_fds, r_fds;
242 int fd_count, count, got_select=0;
243 struct timeval tv;
244
245 if (buffer_f_in == -1)
246 return write(fd,buf,len);
247
248 while (total < len) {
249 int ret = write(fd,buf+total,len-total);
250
251 if (ret == 0) return total;
252
253 if (ret == -1 && !(errno == EWOULDBLOCK || errno == EAGAIN))
254 return -1;
255
256 if (ret == -1 && got_select) {
257 /* hmmm, we got a write select on the fd and then failed to write.
258 Why doesn't that mean that the fd is dead? It doesn't on some
259 systems it seems (eg. IRIX) */
260 u_sleep(1000);
261#if 0
262 fprintf(FERROR,"write exception\n");
263 exit_cleanup(1);
264#endif
265 }
266
267 got_select = 0;
268
269
270 if (ret == -1) {
271 read_check(buffer_f_in);
272
273 fd_count = fd+1;
274 FD_ZERO(&w_fds);
275 FD_ZERO(&r_fds);
276 FD_SET(fd,&w_fds);
277 if (buffer_f_in != -1) {
278 FD_SET(buffer_f_in,&r_fds);
279 if (buffer_f_in > fd)
280 fd_count = buffer_f_in+1;
281 }
282
283 tv.tv_sec = BLOCKING_TIMEOUT;
284 tv.tv_usec = 0;
285 count = select(fd_count,buffer_f_in == -1? NULL: &r_fds,
286 &w_fds,NULL,&tv);
287 if (count == -1 && errno != EINTR) {
288 if (verbose > 1)
289 fprintf(FERROR,"select error: %s\n", strerror(errno));
290 exit_cleanup(1);
291 }
292
293 if (count == 0) continue;
294
295 if (FD_ISSET(fd, &w_fds)) {
296 got_select = 1;
297 }
298 } else {
299 total += ret;
300 }
301 }
302
303 return total;
304}
305
306
307
308void write_int(int f,int x)
309{
310 int ret;
311 char b[4];
312 SIVAL(b,0,x);
313 if ((ret=writefd(f,b,4)) != 4) {
314 fprintf(FERROR,"write_int failed : %s\n",
315 ret==-1?strerror(errno):"EOF");
316 exit_cleanup(1);
317 }
318 total_written += 4;
319}
320
321void write_longint(int f, off_t x)
322{
323 extern int remote_version;
324 char b[8];
325 int ret;
326
327 if (remote_version < 16 || x <= 0x7FFFFFFF) {
328 write_int(f, (int)x);
329 return;
330 }
331
332 write_int(f, -1);
333 SIVAL(b,0,(x&0xFFFFFFFF));
334 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
335
336 if ((ret=writefd(f,b,8)) != 8) {
337 fprintf(FERROR,"write_longint failed : %s\n",
338 ret==-1?strerror(errno):"EOF");
339 exit_cleanup(1);
340 }
341 total_written += 8;
342}
343
344void write_buf(int f,char *buf,int len)
345{
346 int ret;
347 if ((ret=writefd(f,buf,len)) != len) {
348 fprintf(FERROR,"write_buf failed : %s\n",
349 ret==-1?strerror(errno):"EOF");
350 exit_cleanup(1);
351 }
352 total_written += len;
353}
354
355
356void write_byte(int f,unsigned char c)
357{
358 write_buf(f,(char *)&c,1);
359}
360
361void write_flush(int f)
362{
363}
364
365