2005-12-01 22:14:27 +00:00
|
|
|
/*
|
|
|
|
*
|
2006-09-15 14:55:39 +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"
|
2008-04-20 16:20:54 +00:00
|
|
|
#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));
|
|
|
|
|
2008-04-20 16:20:54 +00:00
|
|
|
static unsigned long seek_block(stream_FILE *_file)
|
2005-12-01 22:14:27 +00:00
|
|
|
{
|
2008-04-20 16:20:54 +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;
|
|
|
|
}
|
|
|
|
|
2008-04-20 16:20:54 +00:00
|
|
|
size_t container_read(stream_FILE *_file, void *ptr, size_t size)
|
2005-12-01 22:14:27 +00:00
|
|
|
{
|
2008-04-20 16:20:54 +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;
|
|
|
|
|
2008-04-20 16:20:54 +00:00
|
|
|
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;
|
|
|
|
}
|