added a --force option.
[rsync/rsync.git] / io.c
CommitLineData
720b47f2
AT
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
3a6a366f
AT
27static off_t total_written;
28static off_t total_read;
720b47f2
AT
29
30extern int verbose;
dc5ddbcc 31extern int sparse_files;
720b47f2 32
3a6a366f 33off_t write_total(void)
720b47f2
AT
34{
35 return total_written;
36}
37
3a6a366f 38off_t read_total(void)
720b47f2
AT
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
3a6a366f
AT
52static char *read_buffer;
53static char *read_buffer_p;
54static int read_buffer_len;
55static int read_buffer_size;
720b47f2
AT
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
05c629f7
AT
65 if (f == -1) return;
66
720b47f2
AT
67 if (read_buffer_len == 0) {
68 read_buffer_p = read_buffer;
69 }
70
71 if ((n=num_waiting(f)) <= 0)
72 return;
73
05c629f7
AT
74 /* things could deteriorate if we read in really small chunks */
75 if (n < 10) n = 1024;
76
720b47f2
AT
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;
05c629f7
AT
103
104 if (read_buffer_len < N)
105 read_check(buffer_f_in);
720b47f2
AT
106
107 while (total < N)
108 {
05c629f7 109 if (read_buffer_len > 0 && buffer_f_in == fd) {
720b47f2
AT
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 {
7f28dbee
PM
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 }
720b47f2
AT
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{
4fe159a8 136 int ret;
720b47f2 137 char b[4];
4fe159a8 138 if ((ret=readfd(f,b,4)) != 4) {
720b47f2 139 if (verbose > 1)
9e31c482
AT
140 fprintf(FERROR,"(%d) Error reading %d bytes : %s\n",
141 getpid(),4,ret==-1?strerror(errno):"EOF");
34ccb63e 142 exit_cleanup(1);
720b47f2
AT
143 }
144 total_read += 4;
145 return IVAL(b,0);
146}
147
3a6a366f
AT
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
720b47f2
AT
171void read_buf(int f,char *buf,int len)
172{
4fe159a8
AT
173 int ret;
174 if ((ret=readfd(f,buf,len)) != len) {
720b47f2 175 if (verbose > 1)
9e31c482
AT
176 fprintf(FERROR,"(%d) Error reading %d bytes : %s\n",
177 getpid(),len,ret==-1?strerror(errno):"EOF");
34ccb63e 178 exit_cleanup(1);
720b47f2
AT
179 }
180 total_read += len;
181}
182
182dca5c
AT
183unsigned char read_byte(int f)
184{
d89322c4
AT
185 unsigned char c;
186 read_buf(f,(char *)&c,1);
187 return c;
182dca5c 188}
720b47f2 189
7bec6a5c 190
3a6a366f
AT
191static char last_byte;
192static int last_sparse;
7bec6a5c
AT
193
194int sparse_end(int f)
195{
7bec6a5c
AT
196 if (last_sparse) {
197 lseek(f,-1,SEEK_CUR);
198 return (write(f,&last_byte,1) == 1 ? 0 : -1);
199 }
d5ee1f8e 200 last_sparse = 0;
7bec6a5c
AT
201 return 0;
202}
203
204int write_sparse(int f,char *buf,int len)
205{
dc5ddbcc
AT
206 int l1=0,l2=0;
207 int ret;
7bec6a5c 208
dc5ddbcc
AT
209 if (!sparse_files)
210 return write(f,buf,len);
7bec6a5c 211
dc5ddbcc
AT
212 for (l1=0;l1<len && buf[l1]==0;l1++) ;
213 for (l2=0;l2<(len-l1) && buf[len-(l2+1)]==0;l2++) ;
7bec6a5c
AT
214
215 last_byte = buf[len-1];
7bec6a5c 216
dc5ddbcc
AT
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)
7bec6a5c 224 return len;
7bec6a5c 225
dc5ddbcc
AT
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);
7bec6a5c 233
dc5ddbcc 234 return len;
7bec6a5c
AT
235}
236
720b47f2
AT
237
238static int writefd(int fd,char *buf,int len)
239{
240 int total = 0;
05c629f7 241 fd_set w_fds, r_fds;
e92338c8 242 int fd_count, count, got_select=0;
58d433ab 243 struct timeval tv;
720b47f2
AT
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
4fe159a8
AT
253 if (ret == -1 && !(errno == EWOULDBLOCK || errno == EAGAIN))
254 return -1;
720b47f2 255
e92338c8 256 if (ret == -1 && got_select) {
97d6916e
AT
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) */
feaa89c4 260 u_sleep(1000);
97d6916e 261#if 0
e92338c8
AT
262 fprintf(FERROR,"write exception\n");
263 exit_cleanup(1);
97d6916e 264#endif
e92338c8
AT
265 }
266
9a52223b
AT
267 got_select = 0;
268
269
720b47f2
AT
270 if (ret == -1) {
271 read_check(buffer_f_in);
272
05c629f7
AT
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 }
e92338c8 282
58d433ab
AT
283 tv.tv_sec = BLOCKING_TIMEOUT;
284 tv.tv_usec = 0;
e92338c8
AT
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 }
720b47f2
AT
298 } else {
299 total += ret;
300 }
301 }
302
303 return total;
304}
305
306
307
308void write_int(int f,int x)
309{
4fe159a8 310 int ret;
720b47f2
AT
311 char b[4];
312 SIVAL(b,0,x);
4fe159a8 313 if ((ret=writefd(f,b,4)) != 4) {
dc5ddbcc 314 fprintf(FERROR,"write_int failed : %s\n",
4fe159a8 315 ret==-1?strerror(errno):"EOF");
34ccb63e 316 exit_cleanup(1);
720b47f2
AT
317 }
318 total_written += 4;
319}
320
3a6a366f
AT
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
720b47f2
AT
344void write_buf(int f,char *buf,int len)
345{
4fe159a8
AT
346 int ret;
347 if ((ret=writefd(f,buf,len)) != len) {
dc5ddbcc 348 fprintf(FERROR,"write_buf failed : %s\n",
4fe159a8 349 ret==-1?strerror(errno):"EOF");
34ccb63e 350 exit_cleanup(1);
720b47f2
AT
351 }
352 total_written += len;
353}
354
355
182dca5c
AT
356void write_byte(int f,unsigned char c)
357{
358 write_buf(f,(char *)&c,1);
359}
360
720b47f2
AT
361void write_flush(int f)
362{
363}
364
365