1 /* file.c - file I/O functions */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2006,2007 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_malloc (sizeof (*file
));
82 file
->device
= device
;
87 if (device
->disk
&& file_name
[0] != '/')
88 /* This is a block list. */
89 file
->fs
= &grub_fs_blocklist
;
92 file
->fs
= grub_fs_probe (device
);
97 if ((file
->fs
->open
) (file
, file_name
) != GRUB_ERR_NONE
)
104 grub_device_close (device
);
106 /* if (net) grub_net_close (net); */
114 grub_file_read (grub_file_t file
, char *buf
, grub_size_t len
)
118 if (len
== 0 || len
> file
->size
- file
->offset
)
119 len
= file
->size
- file
->offset
;
121 /* Prevent an overflow. */
122 if ((grub_ssize_t
) len
< 0)
128 res
= (file
->fs
->read
) (file
, buf
, len
);
136 grub_file_close (grub_file_t file
)
139 (file
->fs
->close
) (file
);
142 grub_device_close (file
->device
);
148 grub_file_seek (grub_file_t file
, grub_off_t offset
)
152 if (offset
> file
->size
)
154 grub_error (GRUB_ERR_OUT_OF_RANGE
,
155 "attempt to seek outside of the file");
160 file
->offset
= offset
;