static ssize_t read_lba(int fd, uint64_t lba, void *buffer, size_t bytes) { int sector_size = get_sector_size(fd); off_t offset = lba * sector_size; uint64_t lastlba; ssize_t bytesread; lseek(fd, offset, SEEK_SET); bytesread = read(fd, buffer, bytes); lastlba = last_lba(fd); if (!lastlba) return bytesread; /* Kludge. This is necessary to read/write the last block of an odd-sized disk, until Linux 2.5.x kernel fixes. This is only used by gpt.c, and only to read one sector, so we don't have to be fancy. */ if (!bytesread && !(lastlba & 1) && lba == lastlba) { bytesread = read_lastoddsector(fd, lba, buffer, bytes); } return bytesread; }
static ssize_t read_lba(int fd, uint64_t lba, void *buffer, size_t bytes) { int sector_size = get_sector_size(fd); off_t offset = lba * sector_size; ssize_t bytesread; void *iobuf; size_t iobuf_size; int rc; off_t new_offset; iobuf_size = lcm(bytes, sector_size); rc = posix_memalign(&iobuf, sector_size, iobuf_size); if (rc) return rc; memset(iobuf, 0, bytes); new_offset = lseek(fd, offset, SEEK_SET); if (new_offset == (off_t)-1) { free(iobuf); return 0; } bytesread = read(fd, iobuf, iobuf_size); memcpy(buffer, iobuf, bytes); free(iobuf); /* Kludge. This is necessary to read/write the last block of an odd-sized disk, until Linux 2.5.x kernel fixes. This is only used by gpt.c, and only to read one sector, so we don't have to be fancy. */ if (!bytesread && !(last_lba(fd) & 1) && lba == last_lba(fd)) { bytesread = read_lastoddsector(fd, lba, buffer, bytes); } return bytesread; }