2N/A/* hostfs.c - Dummy filesystem to provide access to the hosts filesystem */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2007,2008,2009,2010 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A#define _BSD_SOURCE
2N/A#include <grub/fs.h>
2N/A#include <grub/file.h>
2N/A#include <grub/disk.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/dl.h>
2N/A#include <grub/util/misc.h>
2N/A
2N/A#include <dirent.h>
2N/A#include <stdio.h>
2N/A#include <errno.h>
2N/A
2N/A
2N/A/* dirent.d_type is a BSD extension, not part of POSIX */
2N/A#include <sys/stat.h>
2N/A#include <string.h>
2N/A
2N/Astatic int
2N/Ais_dir (const char *path, const char *name)
2N/A{
2N/A int len1 = strlen(path);
2N/A int len2 = strlen(name);
2N/A
2N/A char pathname[len1 + 1 + len2 + 1 + 13];
2N/A strcpy (pathname, path);
2N/A
2N/A /* Avoid UNC-path "//name" on Cygwin. */
2N/A if (len1 > 0 && pathname[len1 - 1] != '/')
2N/A strcat (pathname, "/");
2N/A
2N/A strcat (pathname, name);
2N/A
2N/A struct stat st;
2N/A if (stat (pathname, &st))
2N/A return 0;
2N/A return S_ISDIR (st.st_mode);
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_hostfs_dir (grub_device_t device, const char *path,
2N/A int (*hook) (const char *filename,
2N/A const struct grub_dirhook_info *info))
2N/A{
2N/A DIR *dir;
2N/A
2N/A /* Check if the disk is our dummy disk. */
2N/A if (grub_strcmp (device->disk->name, "host"))
2N/A return grub_error (GRUB_ERR_BAD_FS, "not a hostfs");
2N/A
2N/A dir = opendir (path);
2N/A if (! dir)
2N/A return grub_error (GRUB_ERR_BAD_FILENAME,
2N/A "can't open the hostfs directory `%s'", path);
2N/A
2N/A while (1)
2N/A {
2N/A struct dirent *de;
2N/A struct grub_dirhook_info info;
2N/A grub_memset (&info, 0, sizeof (info));
2N/A
2N/A de = readdir (dir);
2N/A if (! de)
2N/A break;
2N/A
2N/A info.dir = !! is_dir (path, de->d_name);
2N/A hook (de->d_name, &info);
2N/A
2N/A }
2N/A
2N/A closedir (dir);
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/A/* Open a file named NAME and initialize FILE. */
2N/Astatic grub_err_t
2N/Agrub_hostfs_open (struct grub_file *file, const char *name)
2N/A{
2N/A FILE *f;
2N/A
2N/A f = fopen (name, "rb");
2N/A if (! f)
2N/A return grub_error (GRUB_ERR_BAD_FILENAME,
2N/A "can't open `%s'", name);
2N/A file->data = f;
2N/A
2N/A#ifdef __MINGW32__
2N/A file->size = grub_util_get_disk_size (name);
2N/A#else
2N/A fseeko (f, 0, SEEK_END);
2N/A file->size = ftello (f);
2N/A fseeko (f, 0, SEEK_SET);
2N/A#endif
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/Astatic grub_ssize_t
2N/Agrub_hostfs_read (grub_file_t file, char *buf, grub_size_t len)
2N/A{
2N/A FILE *f;
2N/A
2N/A f = (FILE *) file->data;
2N/A if (fseeko (f, file->offset, SEEK_SET) != 0)
2N/A {
2N/A grub_error (GRUB_ERR_OUT_OF_RANGE, "fseeko: %s", strerror (errno));
2N/A return -1;
2N/A }
2N/A
2N/A unsigned int s = fread (buf, 1, len, f);
2N/A if (s != len)
2N/A grub_error (GRUB_ERR_FILE_READ_ERROR, "fread: %s", strerror (errno));
2N/A
2N/A return (signed) s;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_hostfs_close (grub_file_t file)
2N/A{
2N/A FILE *f;
2N/A
2N/A f = (FILE *) file->data;
2N/A fclose (f);
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_hostfs_label (grub_device_t device __attribute ((unused)),
2N/A char **label __attribute ((unused)))
2N/A{
2N/A *label = 0;
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/Astatic struct grub_fs grub_hostfs_fs =
2N/A {
2N/A .name = "hostfs",
2N/A .dir = grub_hostfs_dir,
2N/A .open = grub_hostfs_open,
2N/A .read = grub_hostfs_read,
2N/A .close = grub_hostfs_close,
2N/A .label = grub_hostfs_label,
2N/A .next = 0
2N/A };
2N/A
2N/A
2N/A
2N/AGRUB_MOD_INIT(hostfs)
2N/A{
2N/A grub_fs_register (&grub_hostfs_fs);
2N/A}
2N/A
2N/AGRUB_MOD_FINI(hostfs)
2N/A{
2N/A grub_fs_unregister (&grub_hostfs_fs);
2N/A}