Fails, succeeds, or partial read due to EOF (returns num read) return value: -1: unrecoverable error : number of bytes read
| 57 | // -1: unrecoverable error |
| 58 | // <n>: number of bytes read |
| 59 | ssize_t Utils::readAll(int fd, void *buf, size_t count) |
| 60 | { |
| 61 | ssize_t rc; |
| 62 | char *ptr = (char *)buf; |
| 63 | size_t num_read = 0; |
| 64 | |
| 65 | for (num_read = 0; num_read < count;) { |
| 66 | rc = read(fd, ptr + num_read, count - num_read); |
| 67 | if (rc == -1) { |
| 68 | if (errno == EINTR) { |
| 69 | continue; |
| 70 | } else { |
| 71 | LOG(LL_ERROR, LCF_FILEIO, "Read at address %p failed with errno %d", ptr + num_read, errno); |
| 72 | return -1; |
| 73 | } |
| 74 | } else if (rc == 0) { |
| 75 | break; |
| 76 | } else { // else rc > 0 |
| 77 | num_read += rc; |
| 78 | } |
| 79 | } |
| 80 | return num_read; |
| 81 | } |
| 82 | |
| 83 | /* This function detects if the given page is zero pages or not. There is |
| 84 | * scope of improving this function using some optimizations. |