file.c 5.12 KB
Newer Older
okuji's avatar
okuji committed
1 2
/* file.c - file I/O functions */
/*
3
 *  GRUB  --  GRand Unified Bootloader
4
 *  Copyright (C) 2002,2006,2007,2009  Free Software Foundation, Inc.
okuji's avatar
okuji committed
5
 *
6
 *  GRUB is free software: you can redistribute it and/or modify
okuji's avatar
okuji committed
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
okuji's avatar
okuji committed
9 10
 *  (at your option) any later version.
 *
11
 *  GRUB is distributed in the hope that it will be useful,
okuji's avatar
okuji committed
12 13 14 15 16
 *  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
17
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
okuji's avatar
okuji committed
18 19
 */

20 21 22
#include <grub/misc.h>
#include <grub/err.h>
#include <grub/file.h>
23
#include <grub/net.h>
24 25 26
#include <grub/mm.h>
#include <grub/fs.h>
#include <grub/device.h>
27
#include <grub/i18n.h>
okuji's avatar
okuji committed
28

29
void (*EXPORT_VAR (grub_grubnet_fini)) (void);
30

31 32 33
grub_file_filter_t grub_file_filters_all[GRUB_FILE_FILTER_MAX];
grub_file_filter_t grub_file_filters_enabled[GRUB_FILE_FILTER_MAX];

okuji's avatar
okuji committed
34 35
/* Get the device part of the filename NAME. It is enclosed by parentheses.  */
char *
36
grub_file_get_device_name (const char *name)
okuji's avatar
okuji committed
37 38 39
{
  if (name[0] == '(')
    {
40
      char *p = grub_strchr (name, ')');
okuji's avatar
okuji committed
41
      char *ret;
42

okuji's avatar
okuji committed
43 44
      if (! p)
	{
45
	  grub_error (GRUB_ERR_BAD_FILENAME, N_("missing `%c' symbol"), ')');
okuji's avatar
okuji committed
46 47 48
	  return 0;
	}

49
      ret = (char *) grub_malloc (p - name);
okuji's avatar
okuji committed
50 51
      if (! ret)
	return 0;
52

53
      grub_memcpy (ret, name + 1, p - name - 1);
okuji's avatar
okuji committed
54 55 56 57 58 59 60
      ret[p - name - 1] = '\0';
      return ret;
    }

  return 0;
}

61 62
grub_file_t
grub_file_open (const char *name)
okuji's avatar
okuji committed
63
{
64 65
  grub_device_t device = 0;
  grub_file_t file = 0, last_file = 0;
okuji's avatar
okuji committed
66
  char *device_name;
67
  const char *file_name;
68
  grub_file_filter_id_t filter;
okuji's avatar
okuji committed
69

70
  device_name = grub_file_get_device_name (name);
71
  if (grub_errno)
72
    goto fail;
okuji's avatar
okuji committed
73 74

  /* Get the file part of NAME.  */
75
  file_name = (name[0] == '(') ? grub_strchr (name, ')') : NULL;
okuji's avatar
okuji committed
76 77 78
  if (file_name)
    file_name++;
  else
79
    file_name = name;
okuji's avatar
okuji committed
80

81 82
  device = grub_device_open (device_name);
  grub_free (device_name);
okuji's avatar
okuji committed
83 84
  if (! device)
    goto fail;
85

86
  file = (grub_file_t) grub_zalloc (sizeof (*file));
okuji's avatar
okuji committed
87 88
  if (! file)
    goto fail;
89

okuji's avatar
okuji committed
90
  file->device = device;
91

92 93 94 95 96
  /* In case of relative pathnames and non-Unix systems (like Windows)
   * name of host files may not start with `/'. Blocklists for host files
   * are meaningless as well (for a start, host disk does not allow any direct
   * access - it is just a marker). So skip host disk in this case.
   */
97 98 99 100 101
  if (device->disk && file_name[0] != '/'
#if defined(GRUB_UTIL) || defined(GRUB_MACHINE_EMU)
      && grub_strcmp (device->disk->name, "host")
#endif
     )
okuji's avatar
okuji committed
102
    /* This is a block list.  */
103
    file->fs = &grub_fs_blocklist;
okuji's avatar
okuji committed
104 105
  else
    {
106
      file->fs = grub_fs_probe (device);
okuji's avatar
okuji committed
107 108 109 110
      if (! file->fs)
	goto fail;
    }

111
  if ((file->fs->open) (file, file_name) != GRUB_ERR_NONE)
okuji's avatar
okuji committed
112 113
    goto fail;

114 115 116
  file->name = grub_strdup (name);
  grub_errno = GRUB_ERR_NONE;

117 118 119 120 121
  for (filter = 0; file && filter < ARRAY_SIZE (grub_file_filters_enabled);
       filter++)
    if (grub_file_filters_enabled[filter])
      {
	last_file = file;
122
	file = grub_file_filters_enabled[filter] (file, name);
123 124 125 126 127 128 129
      }
  if (!file)
    grub_file_close (last_file);
    
  grub_memcpy (grub_file_filters_enabled, grub_file_filters_all,
	       sizeof (grub_file_filters_enabled));

okuji's avatar
okuji committed
130 131 132 133
  return file;

 fail:
  if (device)
134
    grub_device_close (device);
okuji's avatar
okuji committed
135

136
  /* if (net) grub_net_close (net);  */
okuji's avatar
okuji committed
137

138
  grub_free (file);
139

140 141 142
  grub_memcpy (grub_file_filters_enabled, grub_file_filters_all,
	       sizeof (grub_file_filters_enabled));

okuji's avatar
okuji committed
143 144 145
  return 0;
}

146 147
grub_disk_read_hook_t grub_file_progress_hook;

148
grub_ssize_t
149
grub_file_read (grub_file_t file, void *buf, grub_size_t len)
okuji's avatar
okuji committed
150
{
151
  grub_ssize_t res;
152 153
  grub_disk_read_hook_t read_hook;
  void *read_hook_data;
154

155 156 157
  if (file->offset > file->size)
    {
      grub_error (GRUB_ERR_OUT_OF_RANGE,
158
		  N_("attempt to read past the end of file"));
159 160 161
      return -1;
    }

162 163 164 165
  if (len == 0)
    return 0;

  if (len > file->size - file->offset)
okuji's avatar
okuji committed
166 167
    len = file->size - file->offset;

168 169 170
  /* Prevent an overflow.  */
  if ((grub_ssize_t) len < 0)
    len >>= 1;
171

okuji's avatar
okuji committed
172 173
  if (len == 0)
    return 0;
174 175 176 177 178 179 180 181
  read_hook = file->read_hook;
  read_hook_data = file->read_hook_data;
  if (!file->read_hook)
    {
      file->read_hook = grub_file_progress_hook;
      file->read_hook_data = file;
      file->progress_offset = file->offset;
    }
182
  res = (file->fs->read) (file, buf, len);
183 184
  file->read_hook = read_hook;
  file->read_hook_data = read_hook_data;
okuji's avatar
okuji committed
185 186 187 188 189 190
  if (res > 0)
    file->offset += res;

  return res;
}

191 192
grub_err_t
grub_file_close (grub_file_t file)
okuji's avatar
okuji committed
193 194 195 196
{
  if (file->fs->close)
    (file->fs->close) (file);

197 198
  if (file->device)
    grub_device_close (file->device);
199
  grub_free (file->name);
200 201
  grub_free (file);
  return grub_errno;
okuji's avatar
okuji committed
202 203
}

204 205
grub_off_t
grub_file_seek (grub_file_t file, grub_off_t offset)
okuji's avatar
okuji committed
206
{
207
  grub_off_t old;
okuji's avatar
okuji committed
208

209
  if (offset > file->size)
okuji's avatar
okuji committed
210
    {
211
      grub_error (GRUB_ERR_OUT_OF_RANGE,
212
		  N_("attempt to seek outside of the file"));
okuji's avatar
okuji committed
213 214
      return -1;
    }
215
  
okuji's avatar
okuji committed
216 217
  old = file->offset;
  file->offset = offset;
218
    
okuji's avatar
okuji committed
219 220
  return old;
}