mirror of https://github.com/xqemu/xqemu.git
parallels: use pread
Use pread instead of lseek + read in preparation of using the qemu block API. Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
parent
b76b6e95b8
commit
9d8b88f68c
|
@ -83,7 +83,7 @@ static int parallels_open(BlockDriverState *bs, const char *filename, int flags)
|
||||||
|
|
||||||
s->fd = fd;
|
s->fd = fd;
|
||||||
|
|
||||||
if (read(fd, &ph, sizeof(ph)) != sizeof(ph))
|
if (pread(fd, &ph, sizeof(ph), 0) != sizeof(ph))
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
if (memcmp(ph.magic, HEADER_MAGIC, 16) ||
|
if (memcmp(ph.magic, HEADER_MAGIC, 16) ||
|
||||||
|
@ -93,14 +93,11 @@ static int parallels_open(BlockDriverState *bs, const char *filename, int flags)
|
||||||
|
|
||||||
bs->total_sectors = le32_to_cpu(ph.nb_sectors);
|
bs->total_sectors = le32_to_cpu(ph.nb_sectors);
|
||||||
|
|
||||||
if (lseek(s->fd, 64, SEEK_SET) != 64)
|
|
||||||
goto fail;
|
|
||||||
|
|
||||||
s->tracks = le32_to_cpu(ph.tracks);
|
s->tracks = le32_to_cpu(ph.tracks);
|
||||||
|
|
||||||
s->catalog_size = le32_to_cpu(ph.catalog_entries);
|
s->catalog_size = le32_to_cpu(ph.catalog_entries);
|
||||||
s->catalog_bitmap = qemu_malloc(s->catalog_size * 4);
|
s->catalog_bitmap = qemu_malloc(s->catalog_size * 4);
|
||||||
if (read(s->fd, s->catalog_bitmap, s->catalog_size * 4) !=
|
if (pread(s->fd, s->catalog_bitmap, s->catalog_size * 4, 64) !=
|
||||||
s->catalog_size * 4)
|
s->catalog_size * 4)
|
||||||
goto fail;
|
goto fail;
|
||||||
for (i = 0; i < s->catalog_size; i++)
|
for (i = 0; i < s->catalog_size; i++)
|
||||||
|
@ -114,28 +111,18 @@ fail:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int seek_to_sector(BlockDriverState *bs, int64_t sector_num)
|
static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
|
||||||
{
|
{
|
||||||
BDRVParallelsState *s = bs->opaque;
|
BDRVParallelsState *s = bs->opaque;
|
||||||
uint32_t index, offset;
|
uint32_t index, offset;
|
||||||
uint64_t position;
|
|
||||||
|
|
||||||
index = sector_num / s->tracks;
|
index = sector_num / s->tracks;
|
||||||
offset = sector_num % s->tracks;
|
offset = sector_num % s->tracks;
|
||||||
|
|
||||||
// not allocated
|
/* not allocated */
|
||||||
if ((index > s->catalog_size) || (s->catalog_bitmap[index] == 0))
|
if ((index > s->catalog_size) || (s->catalog_bitmap[index] == 0))
|
||||||
return -1;
|
return -1;
|
||||||
|
return (uint64_t)(s->catalog_bitmap[index] + offset) * 512;
|
||||||
position = (uint64_t)(s->catalog_bitmap[index] + offset) * 512;
|
|
||||||
|
|
||||||
// fprintf(stderr, "sector: %llx index=%x offset=%x pointer=%x position=%x\n",
|
|
||||||
// sector_num, index, offset, s->catalog_bitmap[index], position);
|
|
||||||
|
|
||||||
if (lseek(s->fd, position, SEEK_SET) != position)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int parallels_read(BlockDriverState *bs, int64_t sector_num,
|
static int parallels_read(BlockDriverState *bs, int64_t sector_num,
|
||||||
|
@ -144,11 +131,13 @@ static int parallels_read(BlockDriverState *bs, int64_t sector_num,
|
||||||
BDRVParallelsState *s = bs->opaque;
|
BDRVParallelsState *s = bs->opaque;
|
||||||
|
|
||||||
while (nb_sectors > 0) {
|
while (nb_sectors > 0) {
|
||||||
if (!seek_to_sector(bs, sector_num)) {
|
int64_t position = seek_to_sector(bs, sector_num);
|
||||||
if (read(s->fd, buf, 512) != 512)
|
if (position >= 0) {
|
||||||
return -1;
|
if (pread(s->fd, buf, 512, position) != 512)
|
||||||
} else
|
return -1;
|
||||||
|
} else {
|
||||||
memset(buf, 0, 512);
|
memset(buf, 0, 512);
|
||||||
|
}
|
||||||
nb_sectors--;
|
nb_sectors--;
|
||||||
sector_num++;
|
sector_num++;
|
||||||
buf += 512;
|
buf += 512;
|
||||||
|
|
Loading…
Reference in New Issue