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>
26 #include <grub/device.h>
27 #include <grub/i18n.h>
29 void (*EXPORT_VAR (grub_grubnet_fini
)) (void);
31 grub_file_filter_t grub_file_filters
[GRUB_FILE_FILTER_MAX
];
33 /* Get the device part of the filename NAME. It is enclosed by parentheses. */
35 grub_file_get_device_name (const char *name
)
39 char *p
= grub_strchr (name
, ')');
44 grub_error (GRUB_ERR_BAD_FILENAME
, N_("missing `%c' symbol"), ')');
48 ret
= (char *) grub_malloc (p
- name
);
52 grub_memcpy (ret
, name
+ 1, p
- name
- 1);
53 ret
[p
- name
- 1] = '\0';
61 grub_file_open (const char *name
, enum grub_file_type type
)
63 grub_device_t device
= 0;
64 grub_file_t file
= 0, last_file
= 0;
66 const char *file_name
;
67 grub_file_filter_id_t filter
;
69 device_name
= grub_file_get_device_name (name
);
73 /* Get the file part of NAME. */
74 file_name
= (name
[0] == '(') ? grub_strchr (name
, ')') : NULL
;
80 device
= grub_device_open (device_name
);
81 grub_free (device_name
);
85 file
= (grub_file_t
) grub_zalloc (sizeof (*file
));
89 file
->device
= device
;
91 /* In case of relative pathnames and non-Unix systems (like Windows)
92 * name of host files may not start with `/'. Blocklists for host files
93 * are meaningless as well (for a start, host disk does not allow any direct
94 * access - it is just a marker). So skip host disk in this case.
96 if (device
->disk
&& file_name
[0] != '/'
97 #if defined(GRUB_UTIL) || defined(GRUB_MACHINE_EMU)
98 && grub_strcmp (device
->disk
->name
, "host")
101 /* This is a block list. */
102 file
->fs
= &grub_fs_blocklist
;
105 file
->fs
= grub_fs_probe (device
);
110 if ((file
->fs
->open
) (file
, file_name
) != GRUB_ERR_NONE
)
113 file
->name
= grub_strdup (name
);
114 grub_errno
= GRUB_ERR_NONE
;
116 for (filter
= 0; file
&& filter
< ARRAY_SIZE (grub_file_filters
);
118 if (grub_file_filters
[filter
])
121 file
= grub_file_filters
[filter
] (file
, type
);
122 if (file
&& file
!= last_file
)
124 file
->name
= grub_strdup (name
);
125 grub_errno
= GRUB_ERR_NONE
;
129 grub_file_close (last_file
);
135 grub_device_close (device
);
137 /* if (net) grub_net_close (net); */
144 grub_disk_read_hook_t grub_file_progress_hook
;
147 grub_file_read (grub_file_t file
, void *buf
, grub_size_t len
)
150 grub_disk_read_hook_t read_hook
;
151 void *read_hook_data
;
153 if (file
->offset
> file
->size
)
155 grub_error (GRUB_ERR_OUT_OF_RANGE
,
156 N_("attempt to read past the end of file"));
163 if (len
> file
->size
- file
->offset
)
164 len
= file
->size
- file
->offset
;
166 /* Prevent an overflow. */
167 if ((grub_ssize_t
) len
< 0)
172 read_hook
= file
->read_hook
;
173 read_hook_data
= file
->read_hook_data
;
174 if (!file
->read_hook
)
176 file
->read_hook
= grub_file_progress_hook
;
177 file
->read_hook_data
= file
;
178 file
->progress_offset
= file
->offset
;
180 res
= (file
->fs
->read
) (file
, buf
, len
);
181 file
->read_hook
= read_hook
;
182 file
->read_hook_data
= read_hook_data
;
190 grub_file_close (grub_file_t file
)
193 (file
->fs
->close
) (file
);
196 grub_device_close (file
->device
);
197 grub_free (file
->name
);
203 grub_file_seek (grub_file_t file
, grub_off_t offset
)
207 if (offset
> file
->size
)
209 grub_error (GRUB_ERR_OUT_OF_RANGE
,
210 N_("attempt to seek outside of the file"));
215 file
->offset
= offset
;