| 337 | } |
| 338 | |
| 339 | static int get_packet_data(int fd, char **src_buf, size_t *src_size, |
| 340 | void *dst, size_t size, int options) |
| 341 | { |
| 342 | size_t bytes_read; |
| 343 | |
| 344 | if (fd >= 0 && src_buf && *src_buf) |
| 345 | BUG("multiple sources given to packet_read"); |
| 346 | |
| 347 | /* Read up to "size" bytes from our source, whatever it is. */ |
| 348 | if (src_buf && *src_buf) { |
| 349 | bytes_read = size < *src_size ? size : *src_size; |
| 350 | memcpy(dst, *src_buf, bytes_read); |
| 351 | *src_buf += bytes_read; |
| 352 | *src_size -= bytes_read; |
| 353 | } else { |
| 354 | ssize_t ret = read_in_full(fd, dst, size); |
| 355 | if (ret < 0) { |
| 356 | if (options & PACKET_READ_GENTLE_ON_READ_ERROR) |
| 357 | return error_errno(_("read error")); |
| 358 | die_errno(_("read error")); |
| 359 | } |
| 360 | |
| 361 | bytes_read = (size_t) ret; |
| 362 | } |
| 363 | |
| 364 | /* And complain if we didn't get enough bytes to satisfy the read. */ |
| 365 | if (bytes_read != size) { |
| 366 | if (options & PACKET_READ_GENTLE_ON_EOF) |
| 367 | return -1; |
| 368 | |
| 369 | if (options & PACKET_READ_GENTLE_ON_READ_ERROR) |
| 370 | return error(_("the remote end hung up unexpectedly")); |
| 371 | die(_("the remote end hung up unexpectedly")); |
| 372 | } |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | int packet_length(const char lenbuf_hex[4], size_t size) |
| 378 | { |
no test coverage detected