EMILE/libcontainer/container_read.c

90 lines
1.8 KiB
C
Raw Normal View History

2005-12-01 22:14:27 +00:00
/*
*
* (c) 2005 Laurent Vivier <Laurent@lvivier.info>
2005-12-01 22:14:27 +00:00
*
*/
#include <sys/types.h>
#include <string.h>
2005-12-05 19:59:43 +00:00
#include <stdio.h>
2005-12-01 22:14:27 +00:00
#include "libcontainer.h"
#include "container.h"
2005-12-01 22:14:27 +00:00
2005-12-05 19:59:43 +00:00
extern void error(char *x) __attribute__ ((noreturn));
static unsigned long seek_block(stream_FILE *_file)
2005-12-01 22:14:27 +00:00
{
container_FILE *file = (container_FILE*)_file;
2005-12-01 22:14:27 +00:00
struct emile_container *container = file->container;
ssize_t current;
int i;
unsigned long offset = file->offset;
2005-12-02 00:48:11 +00:00
int block_size = file->device->get_blocksize(file->device->data);
2005-12-01 22:14:27 +00:00
2005-12-05 19:59:43 +00:00
for (i = 0, current = 0;
container->blocks[i].offset != 0; i++)
2005-12-01 22:14:27 +00:00
{
int extent_size = block_size *
container->blocks[i].count;
if ( (current <= offset) && (offset < current + extent_size) )
{
return container->blocks[i].offset +
(offset - current) / block_size;
}
current += extent_size;
}
return 0;
}
size_t container_read(stream_FILE *_file, void *ptr, size_t size)
2005-12-01 22:14:27 +00:00
{
container_FILE *file = (container_FILE*)_file;
2005-12-01 22:14:27 +00:00
int err;
ssize_t read = 0;
int part;
2005-12-02 00:48:11 +00:00
int block_size = file->device->get_blocksize(file->device->data);
2005-12-01 22:14:27 +00:00
while (size != 0)
{
2005-12-05 19:59:43 +00:00
unsigned long block_nb;
int block_offset;
if (file->offset >= file->container->size)
return read;
block_nb = seek_block(_file);
2005-12-05 19:59:43 +00:00
block_offset = file->offset % block_size;
if (block_nb == 0)
error("BUG in libcontainer !!!");
2005-12-01 22:14:27 +00:00
if (block_nb != file->current_block)
{
2005-12-02 00:48:11 +00:00
err = file->device->read_sector(
file->device->data,
2005-12-01 22:14:27 +00:00
block_nb,
file->buffer,
block_size);
if (err == -1)
return -1;
file->current_block = block_nb;
}
part = block_size - block_offset;
if (part > size)
part = size;
memcpy(ptr, file->buffer + block_offset, part);
size -= part;
ptr = (char*)ptr + part;
file->offset += part;
read += part;
}
return read;
}