2009-10-26 Robert Millan <rmh.grub@aybabtu.com>
[grub2/phcoder/solaris.git] / io / bufio.c
blob92f29279e360fd437cb88f6c3dd38fb4cb9c5ed4
1 /* bufio.c - buffered io access */
2 /*
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/>.
20 #include <grub/err.h>
21 #include <grub/types.h>
22 #include <grub/mm.h>
23 #include <grub/misc.h>
24 #include <grub/fs.h>
25 #include <grub/bufio.h>
27 #define GRUB_BUFIO_DEF_SIZE 8192
28 #define GRUB_BUFIO_MAX_SIZE 1048576
30 struct grub_bufio
32 grub_file_t file;
33 grub_size_t block_size;
34 grub_size_t buffer_len;
35 char buffer[0];
37 typedef struct grub_bufio *grub_bufio_t;
39 static struct grub_fs grub_bufio_fs;
41 grub_file_t
42 grub_bufio_open (grub_file_t io, int size)
44 grub_file_t file;
45 grub_bufio_t bufio = 0;
47 file = (grub_file_t) grub_malloc (sizeof (*file));
48 if (! file)
49 return 0;
51 if (size == 0)
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 :
58 io->size);
60 bufio = grub_malloc (sizeof (struct grub_bufio) + size);
61 if (! bufio)
63 grub_free (file);
64 return 0;
67 bufio->file = io;
68 bufio->block_size = size;
69 bufio->buffer_len = 0;
71 file->device = io->device;
72 file->offset = 0;
73 file->size = io->size;
74 file->data = bufio;
75 file->read_hook = 0;
76 file->fs = &grub_bufio_fs;
78 return file;
81 grub_file_t
82 grub_buffile_open (const char *name, int size)
84 grub_file_t io, file;
86 io = grub_file_open (name);
87 if (! io)
88 return 0;
90 file = grub_bufio_open (io, size);
91 if (! file)
93 grub_file_close (io);
94 return 0;
97 return file;
100 static grub_ssize_t
101 grub_bufio_read (grub_file_t file, char *buf, grub_size_t len)
103 grub_size_t res = len;
104 grub_bufio_t bufio = file->data;
105 grub_uint32_t pos;
107 if ((file->offset >= bufio->file->offset) &&
108 (file->offset < bufio->file->offset + bufio->buffer_len))
110 grub_size_t n;
112 pos = file->offset - bufio->file->offset;
113 n = bufio->buffer_len - pos;
114 if (n > len)
115 n = len;
117 grub_memcpy (buf, &bufio->buffer[pos], n);
118 len -= n;
119 if (! len)
120 return res;
122 buf += n;
123 bufio->file->offset += bufio->buffer_len;
124 pos = 0;
126 else
128 bufio->file->offset = grub_divmod64 (file->offset, bufio->block_size,
129 &pos);
130 bufio->file->offset *= bufio->block_size;
133 if (pos + len >= bufio->block_size)
135 if (pos)
137 grub_size_t n;
139 bufio->file->fs->read (bufio->file, bufio->buffer,
140 bufio->block_size);
141 if (grub_errno)
142 return -1;
144 n = bufio->block_size - pos;
145 grub_memcpy (buf, &bufio->buffer[pos], n);
146 len -= n;
147 buf += n;
148 bufio->file->offset += bufio->block_size;
149 pos = 0;
152 while (len >= bufio->block_size)
154 bufio->file->fs->read (bufio->file, buf, bufio->block_size);
155 if (grub_errno)
156 return -1;
158 len -= bufio->block_size;
159 buf += bufio->block_size;
160 bufio->file->offset += bufio->block_size;
163 if (! len)
165 bufio->buffer_len = 0;
166 return res;
170 bufio->buffer_len = bufio->file->size - bufio->file->offset;
171 if (bufio->buffer_len > bufio->block_size)
172 bufio->buffer_len = bufio->block_size;
174 bufio->file->fs->read (bufio->file, bufio->buffer, bufio->buffer_len);
175 if (grub_errno)
176 return -1;
178 grub_memcpy (buf, &bufio->buffer[pos], len);
180 return res;
183 static grub_err_t
184 grub_bufio_close (grub_file_t file)
186 grub_bufio_t bufio = file->data;
188 grub_file_close (bufio->file);
189 grub_free (bufio);
191 file->device = 0;
193 return grub_errno;
196 static struct grub_fs grub_bufio_fs =
198 .name = "bufio",
199 .dir = 0,
200 .open = 0,
201 .read = grub_bufio_read,
202 .close = grub_bufio_close,
203 .label = 0,
204 .next = 0