| 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 | |
| 27 | static int total_written = 0; |
| 28 | static int total_read = 0; |
| 29 | |
| 30 | extern int verbose; |
| 31 | extern int sparse_files; |
| 32 | |
| 33 | int write_total(void) |
| 34 | { |
| 35 | return total_written; |
| 36 | } |
| 37 | |
| 38 | int read_total(void) |
| 39 | { |
| 40 | return total_read; |
| 41 | } |
| 42 | |
| 43 | static int buffer_f_in = -1; |
| 44 | |
| 45 | void setup_nonblocking(int f_in,int f_out) |
| 46 | { |
| 47 | set_blocking(f_out,0); |
| 48 | buffer_f_in = f_in; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | static char *read_buffer = NULL; |
| 53 | static char *read_buffer_p = NULL; |
| 54 | static int read_buffer_len = 0; |
| 55 | static int read_buffer_size = 0; |
| 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. */ |
| 61 | static 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 | |
| 99 | static 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 | |
| 134 | int 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 | |
| 148 | void read_buf(int f,char *buf,int len) |
| 149 | { |
| 150 | int ret; |
| 151 | if ((ret=readfd(f,buf,len)) != len) { |
| 152 | if (verbose > 1) |
| 153 | fprintf(FERROR,"(%d) Error reading %d bytes : %s\n", |
| 154 | getpid(),len,ret==-1?strerror(errno):"EOF"); |
| 155 | exit_cleanup(1); |
| 156 | } |
| 157 | total_read += len; |
| 158 | } |
| 159 | |
| 160 | unsigned char read_byte(int f) |
| 161 | { |
| 162 | unsigned char c; |
| 163 | read_buf(f,(char *)&c,1); |
| 164 | return c; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | static char last_byte=0; |
| 169 | static int last_sparse = 0; |
| 170 | |
| 171 | int sparse_end(int f) |
| 172 | { |
| 173 | if (last_sparse) { |
| 174 | lseek(f,-1,SEEK_CUR); |
| 175 | return (write(f,&last_byte,1) == 1 ? 0 : -1); |
| 176 | } |
| 177 | last_sparse = 0; |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | int write_sparse(int f,char *buf,int len) |
| 182 | { |
| 183 | int l1=0,l2=0; |
| 184 | int ret; |
| 185 | |
| 186 | if (!sparse_files) |
| 187 | return write(f,buf,len); |
| 188 | |
| 189 | for (l1=0;l1<len && buf[l1]==0;l1++) ; |
| 190 | for (l2=0;l2<(len-l1) && buf[len-(l2+1)]==0;l2++) ; |
| 191 | |
| 192 | last_byte = buf[len-1]; |
| 193 | |
| 194 | if (l1 == len || l2 > 0) |
| 195 | last_sparse=1; |
| 196 | |
| 197 | if (l1 > 0) |
| 198 | lseek(f,l1,SEEK_CUR); |
| 199 | |
| 200 | if (l1 == len) |
| 201 | return len; |
| 202 | |
| 203 | if ((ret=write(f,buf+l1,len-(l1+l2))) != len-(l1+l2)) { |
| 204 | if (ret == -1 || ret == 0) return ret; |
| 205 | return (l1+ret); |
| 206 | } |
| 207 | |
| 208 | if (l2 > 0) |
| 209 | lseek(f,l2,SEEK_CUR); |
| 210 | |
| 211 | return len; |
| 212 | } |
| 213 | |
| 214 | int read_write(int fd_in,int fd_out,int size) |
| 215 | { |
| 216 | static char *buf=NULL; |
| 217 | int bufsize = sparse_files?SPARSE_WRITE_SIZE:WRITE_SIZE; |
| 218 | int total=0; |
| 219 | |
| 220 | if (!buf) { |
| 221 | buf = (char *)malloc(bufsize); |
| 222 | if (!buf) out_of_memory("read_write"); |
| 223 | } |
| 224 | |
| 225 | while (total < size) { |
| 226 | int n = MIN(size-total,bufsize); |
| 227 | read_buf(fd_in,buf,n); |
| 228 | if (write_sparse(fd_out,buf,n) != n) |
| 229 | return total; |
| 230 | total += n; |
| 231 | } |
| 232 | return total; |
| 233 | } |
| 234 | |
| 235 | |
| 236 | static int writefd(int fd,char *buf,int len) |
| 237 | { |
| 238 | int total = 0; |
| 239 | fd_set w_fds, r_fds; |
| 240 | int fd_count, count, got_select=0; |
| 241 | struct timeval tv; |
| 242 | |
| 243 | if (buffer_f_in == -1) |
| 244 | return write(fd,buf,len); |
| 245 | |
| 246 | while (total < len) { |
| 247 | int ret = write(fd,buf+total,len-total); |
| 248 | |
| 249 | if (ret == 0) return total; |
| 250 | |
| 251 | if (ret == -1 && !(errno == EWOULDBLOCK || errno == EAGAIN)) |
| 252 | return -1; |
| 253 | |
| 254 | if (ret == -1 && got_select) { |
| 255 | /* hmmm, we got a write select on the fd and then failed to write. |
| 256 | Why doesn't that mean that the fd is dead? It doesn't on some |
| 257 | systems it seems (eg. IRIX) */ |
| 258 | #if 0 |
| 259 | fprintf(FERROR,"write exception\n"); |
| 260 | exit_cleanup(1); |
| 261 | #endif |
| 262 | } |
| 263 | |
| 264 | got_select = 0; |
| 265 | |
| 266 | |
| 267 | if (ret == -1) { |
| 268 | read_check(buffer_f_in); |
| 269 | |
| 270 | fd_count = fd+1; |
| 271 | FD_ZERO(&w_fds); |
| 272 | FD_ZERO(&r_fds); |
| 273 | FD_SET(fd,&w_fds); |
| 274 | if (buffer_f_in != -1) { |
| 275 | FD_SET(buffer_f_in,&r_fds); |
| 276 | if (buffer_f_in > fd) |
| 277 | fd_count = buffer_f_in+1; |
| 278 | } |
| 279 | |
| 280 | tv.tv_sec = BLOCKING_TIMEOUT; |
| 281 | tv.tv_usec = 0; |
| 282 | count = select(fd_count,buffer_f_in == -1? NULL: &r_fds, |
| 283 | &w_fds,NULL,&tv); |
| 284 | if (count == -1 && errno != EINTR) { |
| 285 | if (verbose > 1) |
| 286 | fprintf(FERROR,"select error: %s\n", strerror(errno)); |
| 287 | exit_cleanup(1); |
| 288 | } |
| 289 | |
| 290 | if (count == 0) continue; |
| 291 | |
| 292 | if (FD_ISSET(fd, &w_fds)) { |
| 293 | got_select = 1; |
| 294 | } |
| 295 | } else { |
| 296 | total += ret; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | return total; |
| 301 | } |
| 302 | |
| 303 | |
| 304 | |
| 305 | void write_int(int f,int x) |
| 306 | { |
| 307 | int ret; |
| 308 | char b[4]; |
| 309 | SIVAL(b,0,x); |
| 310 | if ((ret=writefd(f,b,4)) != 4) { |
| 311 | fprintf(FERROR,"write_int failed : %s\n", |
| 312 | ret==-1?strerror(errno):"EOF"); |
| 313 | exit_cleanup(1); |
| 314 | } |
| 315 | total_written += 4; |
| 316 | } |
| 317 | |
| 318 | void write_buf(int f,char *buf,int len) |
| 319 | { |
| 320 | int ret; |
| 321 | if ((ret=writefd(f,buf,len)) != len) { |
| 322 | fprintf(FERROR,"write_buf failed : %s\n", |
| 323 | ret==-1?strerror(errno):"EOF"); |
| 324 | exit_cleanup(1); |
| 325 | } |
| 326 | total_written += len; |
| 327 | } |
| 328 | |
| 329 | |
| 330 | void write_byte(int f,unsigned char c) |
| 331 | { |
| 332 | write_buf(f,(char *)&c,1); |
| 333 | } |
| 334 | |
| 335 | void write_flush(int f) |
| 336 | { |
| 337 | } |
| 338 | |
| 339 | |