1
/* bufio.c - buffered io access */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2008 Free Software Foundation, Inc.
6
* GRUB is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* GRUB is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
21
#include <grub/types.h>
23
#include <grub/misc.h>
25
#include <grub/bufio.h>
27
#define GRUB_BUFIO_DEF_SIZE 8192
28
#define GRUB_BUFIO_MAX_SIZE 1048576
33
grub_size_t block_size;
34
grub_size_t buffer_len;
37
typedef struct grub_bufio *grub_bufio_t;
39
static struct grub_fs grub_bufio_fs;
42
grub_bufio_open (grub_file_t io, int size)
45
grub_bufio_t bufio = 0;
47
file = (grub_file_t) grub_malloc (sizeof (*file));
52
size = GRUB_BUFIO_DEF_SIZE;
53
else if (size > GRUB_BUFIO_MAX_SIZE)
54
size = GRUB_BUFIO_MAX_SIZE;
56
if ((size < 0) || ((unsigned) size > io->size))
57
size = ((io->size > GRUB_BUFIO_MAX_SIZE) ? GRUB_BUFIO_MAX_SIZE :
60
bufio = grub_malloc (sizeof (struct grub_bufio) + size);
68
bufio->block_size = size;
69
bufio->buffer_len = 0;
71
file->device = io->device;
73
file->size = io->size;
76
file->fs = &grub_bufio_fs;
77
file->not_easly_seekable = io->not_easly_seekable;
83
grub_buffile_open (const char *name, int size)
87
io = grub_file_open (name);
91
file = grub_bufio_open (io, size);
102
grub_bufio_read (grub_file_t file, char *buf, grub_size_t len)
104
grub_size_t res = len;
105
grub_bufio_t bufio = file->data;
108
if ((file->offset >= bufio->file->offset) &&
109
(file->offset < bufio->file->offset + bufio->buffer_len))
113
pos = file->offset - bufio->file->offset;
114
n = bufio->buffer_len - pos;
118
grub_memcpy (buf, &bufio->buffer[pos], n);
124
bufio->file->offset += bufio->buffer_len;
129
bufio->file->offset = grub_divmod64 (file->offset, bufio->block_size,
131
bufio->file->offset *= bufio->block_size;
134
if (pos + len >= bufio->block_size)
140
bufio->file->fs->read (bufio->file, bufio->buffer,
145
n = bufio->block_size - pos;
146
grub_memcpy (buf, &bufio->buffer[pos], n);
149
bufio->file->offset += bufio->block_size;
153
while (len >= bufio->block_size)
155
bufio->file->fs->read (bufio->file, buf, bufio->block_size);
159
len -= bufio->block_size;
160
buf += bufio->block_size;
161
bufio->file->offset += bufio->block_size;
166
bufio->buffer_len = 0;
171
bufio->buffer_len = bufio->file->size - bufio->file->offset;
172
if (bufio->buffer_len > bufio->block_size)
173
bufio->buffer_len = bufio->block_size;
175
bufio->file->fs->read (bufio->file, bufio->buffer, bufio->buffer_len);
179
grub_memcpy (buf, &bufio->buffer[pos], len);
185
grub_bufio_close (grub_file_t file)
187
grub_bufio_t bufio = file->data;
189
grub_file_close (bufio->file);
197
static struct grub_fs grub_bufio_fs =
202
.read = grub_bufio_read,
203
.close = grub_bufio_close,