char mtcp_readchar (int fd) { char c; int rc; do { rc = mtcp_sys_read (fd, &c, 1); } while ( rc == -1 && mtcp_sys_errno == EINTR ); if (rc <= 0) return (0); return (c); }
ssize_t mtcp_read_all(int fd, void *buf, size_t count) { int rc; char *ptr = (char *) buf; size_t num_read = 0; for (num_read = 0; num_read < count;) { rc = mtcp_sys_read (fd, ptr + num_read, count - num_read); if (rc == -1) { if (mtcp_sys_errno == EINTR || mtcp_sys_errno == EAGAIN) continue; else return -1; } else if (rc == 0) break; else // else rc > 0 num_read += rc; } return num_read; }
/** * This function will return the first character of the given file. If the * file is not readable, we will abort. * * @param filename the name of the file to read * @return the first character of the given file */ static char first_char(char *filename) { int fd, rc; char c; fd = mtcp_sys_open(filename, O_RDONLY, 0); if(fd < 0) { MTCP_PRINTF("ERROR: Cannot open file %s\n", filename); mtcp_abort(); } rc = mtcp_sys_read(fd, &c, 1); if(rc != 1) { MTCP_PRINTF("ERROR: Error reading from file %s\n", filename); mtcp_abort(); } mtcp_sys_close(fd); return c; }
void mtcp_readfile(int fd, void *buf, size_t size) { ssize_t rc; size_t ar = 0; int tries = 0; while(ar != size) { rc = mtcp_sys_read(fd, (char*)buf + ar, size - ar); if (rc < 0 && rc > -4096) { /* kernel could return large unsigned int */ MTCP_PRINTF("error %d reading checkpoint\n", mtcp_sys_errno); mtcp_abort(); } else if (rc == 0) { MTCP_PRINTF("only read %u bytes instead of %u from checkpoint file\n", (unsigned)ar, (unsigned)size); if (tries++ >= 10) { MTCP_PRINTF(" failed to read after 10 tries in a row.\n"); mtcp_abort(); } } ar += rc; } }