Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / kern / emu / hostfs.c
blob3cb089c3fe47218239440b45db01a36ab661f548
1 /* hostfs.c - Dummy filesystem to provide access to the hosts filesystem */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2007,2008,2009,2010 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/>.
19 #define _BSD_SOURCE
20 #include <grub/fs.h>
21 #include <grub/file.h>
22 #include <grub/disk.h>
23 #include <grub/misc.h>
24 #include <grub/mm.h>
25 #include <grub/dl.h>
26 #include <grub/util/misc.h>
27 #include <grub/emu/hostdisk.h>
28 #include <grub/i18n.h>
30 #include <dirent.h>
31 #include <stdio.h>
32 #include <errno.h>
35 /* dirent.d_type is a BSD extension, not part of POSIX */
36 #include <sys/stat.h>
37 #include <string.h>
39 static int
40 is_dir (const char *path, const char *name)
42 int len1 = strlen(path);
43 int len2 = strlen(name);
45 char pathname[len1 + 1 + len2 + 1 + 13];
46 strcpy (pathname, path);
48 /* Avoid UNC-path "//name" on Cygwin. */
49 if (len1 > 0 && pathname[len1 - 1] != '/')
50 strcat (pathname, "/");
52 strcat (pathname, name);
54 struct stat st;
55 if (stat (pathname, &st))
56 return 0;
57 return S_ISDIR (st.st_mode);
60 struct grub_hostfs_data
62 char *filename;
63 FILE *f;
66 static grub_err_t
67 grub_hostfs_dir (grub_device_t device, const char *path,
68 int (*hook) (const char *filename,
69 const struct grub_dirhook_info *info))
71 DIR *dir;
73 /* Check if the disk is our dummy disk. */
74 if (grub_strcmp (device->disk->name, "host"))
75 return grub_error (GRUB_ERR_BAD_FS, "not a hostfs");
77 dir = opendir (path);
78 if (! dir)
79 return grub_error (GRUB_ERR_BAD_FILENAME,
80 N_("can't open `%s': %s"), path,
81 strerror (errno));
83 while (1)
85 struct dirent *de;
86 struct grub_dirhook_info info;
87 grub_memset (&info, 0, sizeof (info));
89 de = readdir (dir);
90 if (! de)
91 break;
93 info.dir = !! is_dir (path, de->d_name);
94 hook (de->d_name, &info);
98 closedir (dir);
100 return GRUB_ERR_NONE;
103 /* Open a file named NAME and initialize FILE. */
104 static grub_err_t
105 grub_hostfs_open (struct grub_file *file, const char *name)
107 FILE *f;
108 struct grub_hostfs_data *data;
110 f = fopen (name, "rb");
111 if (! f)
112 return grub_error (GRUB_ERR_BAD_FILENAME,
113 N_("can't open `%s': %s"), name,
114 strerror (errno));
115 data = grub_malloc (sizeof (*data));
116 if (!data)
118 fclose (f);
119 return grub_errno;
121 data->filename = grub_strdup (name);
122 if (!data->filename)
124 grub_free (data);
125 fclose (f);
126 return grub_errno;
129 data->f = f;
131 file->data = data;
133 #ifdef __MINGW32__
134 file->size = grub_util_get_disk_size (name);
135 #else
136 file->size = grub_util_get_fd_size (fileno (f), name, NULL);
137 #endif
139 return GRUB_ERR_NONE;
142 static grub_ssize_t
143 grub_hostfs_read (grub_file_t file, char *buf, grub_size_t len)
145 struct grub_hostfs_data *data;
147 data = file->data;
148 if (fseeko (data->f, file->offset, SEEK_SET) != 0)
150 grub_error (GRUB_ERR_OUT_OF_RANGE, N_("cannot seek `%s': %s"),
151 data->filename, strerror (errno));
152 return -1;
155 unsigned int s = fread (buf, 1, len, data->f);
156 if (s != len)
157 grub_error (GRUB_ERR_FILE_READ_ERROR, N_("cannot read `%s': %s"),
158 data->filename, strerror (errno));
160 return (signed) s;
163 static grub_err_t
164 grub_hostfs_close (grub_file_t file)
166 struct grub_hostfs_data *data;
168 data = file->data;
169 fclose (data->f);
170 grub_free (data->filename);
171 grub_free (data);
173 return GRUB_ERR_NONE;
176 static grub_err_t
177 grub_hostfs_label (grub_device_t device __attribute ((unused)),
178 char **label __attribute ((unused)))
180 *label = 0;
181 return GRUB_ERR_NONE;
184 static struct grub_fs grub_hostfs_fs =
186 .name = "hostfs",
187 .dir = grub_hostfs_dir,
188 .open = grub_hostfs_open,
189 .read = grub_hostfs_read,
190 .close = grub_hostfs_close,
191 .label = grub_hostfs_label,
192 .next = 0
197 GRUB_MOD_INIT(hostfs)
199 grub_fs_register (&grub_hostfs_fs);
202 GRUB_MOD_FINI(hostfs)
204 grub_fs_unregister (&grub_hostfs_fs);