/* bufio.c - buffered io access */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2008 Free Software Foundation, Inc.
*
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
GRUB_MOD_LICENSE ("GPLv3+");
struct grub_bufio
{
char buffer[0];
};
{
if (! file)
return 0;
if (size == 0)
else if (size > GRUB_BUFIO_MAX_SIZE)
if (! bufio)
{
return 0;
}
bufio->buffer_len = 0;
return file;
}
{
if (! io)
return 0;
if (! file)
{
return 0;
}
return file;
}
static grub_ssize_t
{
/* First part: use whatever we already have in the buffer. */
{
grub_size_t n;
if (n > len)
n = len;
len -= n;
res += n;
buf += n;
}
if (len == 0)
return res;
/* Need to read some more. */
/* Now read between file->offset + res and bufio->buffer_at. */
{
if (really_read < 0)
return -1;
len -= really_read;
buf += really_read;
res += really_read;
/* Partial read. File ended unexpectedly. Save the last chunk in buffer.
*/
{
bufio->buffer_len);
return res;
}
}
/* Read into buffer. */
bufio->block_size);
if (really_read < 0)
return -1;
return res;
}
static grub_err_t
{
return grub_errno;
}
{
.name = "bufio",
.dir = 0,
.open = 0,
.read = grub_bufio_read,
.label = 0,
.next = 0
};