if rsync fails to update the group of a file but nothing else then
[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
71c46176
AT
27static int64 total_written;
28static int64 total_read;
720b47f2
AT
29
30extern int verbose;
dc5ddbcc 31extern int sparse_files;
720b47f2 32
71c46176 33int64 write_total(void)
720b47f2
AT
34{
35 return total_written;
36}
37
71c46176 38int64 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
71c46176 148int64 read_longint(int f)
3a6a366f
AT
149{
150 extern int remote_version;
71c46176 151 int64 ret;
3a6a366f
AT
152 char b[8];
153 ret = read_int(f);
71c46176
AT
154
155 if (ret != -1) return ret;
156
157#ifndef HAVE_LONGLONG
158 fprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
159 exit_cleanup(1);
160#else
161 if (remote_version >= 16) {
3a6a366f
AT
162 if ((ret=readfd(f,b,8)) != 8) {
163 if (verbose > 1)
164 fprintf(FERROR,"(%d) Error reading %d bytes : %s\n",
165 getpid(),8,ret==-1?strerror(errno):"EOF");
166 exit_cleanup(1);
167 }
168 total_read += 8;
71c46176 169 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
3a6a366f 170 }
71c46176
AT
171#endif
172
3a6a366f
AT
173 return ret;
174}
175
720b47f2
AT
176void read_buf(int f,char *buf,int len)
177{
4fe159a8
AT
178 int ret;
179 if ((ret=readfd(f,buf,len)) != len) {
720b47f2 180 if (verbose > 1)
9e31c482
AT
181 fprintf(FERROR,"(%d) Error reading %d bytes : %s\n",
182 getpid(),len,ret==-1?strerror(errno):"EOF");
34ccb63e 183 exit_cleanup(1);
720b47f2
AT
184 }
185 total_read += len;
186}
187
182dca5c
AT
188unsigned char read_byte(int f)
189{
d89322c4
AT
190 unsigned char c;
191 read_buf(f,(char *)&c,1);
192 return c;
182dca5c 193}
720b47f2 194
7bec6a5c 195
3a6a366f
AT
196static char last_byte;
197static int last_sparse;
7bec6a5c
AT
198
199int sparse_end(int f)
200{
7bec6a5c
AT
201 if (last_sparse) {
202 lseek(f,-1,SEEK_CUR);
203 return (write(f,&last_byte,1) == 1 ? 0 : -1);
204 }
d5ee1f8e 205 last_sparse = 0;
7bec6a5c
AT
206 return 0;
207}
208
209int write_sparse(int f,char *buf,int len)
210{
dc5ddbcc
AT
211 int l1=0,l2=0;
212 int ret;
7bec6a5c 213
dc5ddbcc
AT
214 if (!sparse_files)
215 return write(f,buf,len);
7bec6a5c 216
dc5ddbcc
AT
217 for (l1=0;l1<len && buf[l1]==0;l1++) ;
218 for (l2=0;l2<(len-l1) && buf[len-(l2+1)]==0;l2++) ;
7bec6a5c
AT
219
220 last_byte = buf[len-1];
7bec6a5c 221
dc5ddbcc
AT
222 if (l1 == len || l2 > 0)
223 last_sparse=1;
224
225 if (l1 > 0)
226 lseek(f,l1,SEEK_CUR);
227
228 if (l1 == len)
7bec6a5c 229 return len;
7bec6a5c 230
dc5ddbcc
AT
231 if ((ret=write(f,buf+l1,len-(l1+l2))) != len-(l1+l2)) {
232 if (ret == -1 || ret == 0) return ret;
233 return (l1+ret);
234 }
235
236 if (l2 > 0)
237 lseek(f,l2,SEEK_CUR);
7bec6a5c 238
dc5ddbcc 239 return len;
7bec6a5c
AT
240}
241
720b47f2
AT
242
243static int writefd(int fd,char *buf,int len)
244{
245 int total = 0;
05c629f7 246 fd_set w_fds, r_fds;
e92338c8 247 int fd_count, count, got_select=0;
58d433ab 248 struct timeval tv;
720b47f2
AT
249
250 if (buffer_f_in == -1)
251 return write(fd,buf,len);
252
253 while (total < len) {
254 int ret = write(fd,buf+total,len-total);
255
256 if (ret == 0) return total;
257
4fe159a8
AT
258 if (ret == -1 && !(errno == EWOULDBLOCK || errno == EAGAIN))
259 return -1;
720b47f2 260
e92338c8 261 if (ret == -1 && got_select) {
97d6916e
AT
262 /* hmmm, we got a write select on the fd and then failed to write.
263 Why doesn't that mean that the fd is dead? It doesn't on some
264 systems it seems (eg. IRIX) */
feaa89c4 265 u_sleep(1000);
97d6916e 266#if 0
e92338c8
AT
267 fprintf(FERROR,"write exception\n");
268 exit_cleanup(1);
97d6916e 269#endif
e92338c8
AT
270 }
271
9a52223b
AT
272 got_select = 0;
273
274
720b47f2
AT
275 if (ret == -1) {
276 read_check(buffer_f_in);
277
05c629f7
AT
278 fd_count = fd+1;
279 FD_ZERO(&w_fds);
280 FD_ZERO(&r_fds);
281 FD_SET(fd,&w_fds);
282 if (buffer_f_in != -1) {
283 FD_SET(buffer_f_in,&r_fds);
284 if (buffer_f_in > fd)
285 fd_count = buffer_f_in+1;
286 }
e92338c8 287
58d433ab
AT
288 tv.tv_sec = BLOCKING_TIMEOUT;
289 tv.tv_usec = 0;
e92338c8
AT
290 count = select(fd_count,buffer_f_in == -1? NULL: &r_fds,
291 &w_fds,NULL,&tv);
292 if (count == -1 && errno != EINTR) {
293 if (verbose > 1)
294 fprintf(FERROR,"select error: %s\n", strerror(errno));
295 exit_cleanup(1);
296 }
297
298 if (count == 0) continue;
299
300 if (FD_ISSET(fd, &w_fds)) {
301 got_select = 1;
302 }
720b47f2
AT
303 } else {
304 total += ret;
305 }
306 }
307
308 return total;
309}
310
311
312
313void write_int(int f,int x)
314{
4fe159a8 315 int ret;
720b47f2
AT
316 char b[4];
317 SIVAL(b,0,x);
4fe159a8 318 if ((ret=writefd(f,b,4)) != 4) {
dc5ddbcc 319 fprintf(FERROR,"write_int failed : %s\n",
4fe159a8 320 ret==-1?strerror(errno):"EOF");
34ccb63e 321 exit_cleanup(1);
720b47f2
AT
322 }
323 total_written += 4;
324}
325
71c46176 326void write_longint(int f, int64 x)
3a6a366f
AT
327{
328 extern int remote_version;
329 char b[8];
330 int ret;
331
332 if (remote_version < 16 || x <= 0x7FFFFFFF) {
333 write_int(f, (int)x);
334 return;
335 }
336
337 write_int(f, -1);
338 SIVAL(b,0,(x&0xFFFFFFFF));
339 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
340
341 if ((ret=writefd(f,b,8)) != 8) {
342 fprintf(FERROR,"write_longint failed : %s\n",
343 ret==-1?strerror(errno):"EOF");
344 exit_cleanup(1);
345 }
346 total_written += 8;
347}
348
720b47f2
AT
349void write_buf(int f,char *buf,int len)
350{
4fe159a8
AT
351 int ret;
352 if ((ret=writefd(f,buf,len)) != len) {
dc5ddbcc 353 fprintf(FERROR,"write_buf failed : %s\n",
4fe159a8 354 ret==-1?strerror(errno):"EOF");
34ccb63e 355 exit_cleanup(1);
720b47f2
AT
356 }
357 total_written += len;
358}
359
360
182dca5c
AT
361void write_byte(int f,unsigned char c)
362{
363 write_buf(f,(char *)&c,1);
364}
365
720b47f2
AT
366void write_flush(int f)
367{
368}
369
370