1
/* file.c - file I/O functions */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2002,2006,2007,2009 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/misc.h>
22
#include <grub/file.h>
25
#include <grub/device.h>
27
/* Get the device part of the filename NAME. It is enclosed by parentheses. */
29
grub_file_get_device_name (const char *name)
33
char *p = grub_strchr (name, ')');
38
grub_error (GRUB_ERR_BAD_FILENAME, "missing `)'");
42
ret = (char *) grub_malloc (p - name);
46
grub_memcpy (ret, name + 1, p - name - 1);
47
ret[p - name - 1] = '\0';
55
grub_file_open (const char *name)
62
device_name = grub_file_get_device_name (name);
66
/* Get the file part of NAME. */
67
file_name = grub_strchr (name, ')');
71
file_name = (char *) name;
73
device = grub_device_open (device_name);
74
grub_free (device_name);
78
file = (grub_file_t) grub_zalloc (sizeof (*file));
82
file->device = device;
84
if (device->disk && file_name[0] != '/')
85
/* This is a block list. */
86
file->fs = &grub_fs_blocklist;
89
file->fs = grub_fs_probe (device);
94
if ((file->fs->open) (file, file_name) != GRUB_ERR_NONE)
101
grub_device_close (device);
103
/* if (net) grub_net_close (net); */
111
grub_file_read (grub_file_t file, void *buf, grub_size_t len)
115
if (file->offset > file->size)
117
grub_error (GRUB_ERR_OUT_OF_RANGE,
118
"attempt to read past the end of file");
122
if (len == 0 || len > file->size - file->offset)
123
len = file->size - file->offset;
125
/* Prevent an overflow. */
126
if ((grub_ssize_t) len < 0)
132
res = (file->fs->read) (file, buf, len);
140
grub_file_close (grub_file_t file)
143
(file->fs->close) (file);
146
grub_device_close (file->device);
152
grub_file_seek (grub_file_t file, grub_off_t offset)
156
if (offset > file->size)
158
grub_error (GRUB_ERR_OUT_OF_RANGE,
159
"attempt to seek outside of the file");
164
file->offset = offset;