File: container_read.c

package info (click to toggle)
emile 0.10-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,716 kB
  • ctags: 2,737
  • sloc: ansic: 18,908; makefile: 726; asm: 622; sh: 2
file content (86 lines) | stat: -rw-r--r-- 1,693 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 *
 * (c) 2005 Laurent Vivier <LaurentVivier@wanadoo.fr>
 *
 */

#include <sys/types.h>
#include <string.h>
#include <stdio.h>

#include "libcontainer.h"

extern void error(char *x) __attribute__ ((noreturn));

static unsigned long seek_block(container_FILE *file)
{
	struct emile_container *container = file->container;
	ssize_t current;
	int i;
	unsigned long offset = file->offset;
	int block_size = file->device->get_blocksize(file->device->data);

	for (i = 0, current = 0;
	     container->blocks[i].offset != 0; i++)
	{
		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;
}

ssize_t container_read(container_FILE *file, void *ptr, size_t size)
{
	int err;
	ssize_t read = 0;
	int part;
	int block_size = file->device->get_blocksize(file->device->data);

	while (size != 0)
	{
		unsigned long block_nb;
		int block_offset;

		if (file->offset >= file->container->size)
			return read;

		block_nb = seek_block(file);
		block_offset = file->offset % block_size;

		if (block_nb == 0)
			error("BUG in libcontainer !!!");

		if (block_nb != file->current_block)
		{
			err = file->device->read_sector(
					file->device->data,
					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;
}