198 lines
4.8 KiB
C
198 lines
4.8 KiB
C
/* hostfs.c - Dummy filesystem to provide access to the hosts filesystem */
|
||
/*
|
||
* VasEBoot -- GRand Unified Bootloader
|
||
* Copyright (C) 2007,2008,2009,2010 Free Software Foundation, Inc.
|
||
*
|
||
* VasEBoot is free software: you can redistribute it and/or modify
|
||
* it under the terms of the GNU General Public License as published by
|
||
* the Free Software Foundation, either version 3 of the License, or
|
||
* (at your option) any later version.
|
||
*
|
||
* VasEBoot is distributed in the hope that it will be useful,
|
||
* 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
|
||
* along with VasEBoot. If not, see <http://www.gnu.org/licenses/>.
|
||
*/
|
||
|
||
#include <config-util.h>
|
||
|
||
#include <VasEBoot/fs.h>
|
||
#include <VasEBoot/file.h>
|
||
#include <VasEBoot/disk.h>
|
||
#include <VasEBoot/misc.h>
|
||
#include <VasEBoot/mm.h>
|
||
#include <VasEBoot/dl.h>
|
||
#include <VasEBoot/util/misc.h>
|
||
#include <VasEBoot/emu/hostdisk.h>
|
||
#include <VasEBoot/i18n.h>
|
||
|
||
#include <stdio.h>
|
||
#include <errno.h>
|
||
#include <string.h>
|
||
|
||
static int
|
||
is_dir (const char *path, const char *name)
|
||
{
|
||
int len1 = strlen(path);
|
||
int len2 = strlen(name);
|
||
int ret;
|
||
|
||
char *pathname = xmalloc (len1 + 1 + len2 + 1 + 13);
|
||
strcpy (pathname, path);
|
||
|
||
/* Avoid UNC-path "//name" on Cygwin. */
|
||
if (len1 > 0 && pathname[len1 - 1] != '/')
|
||
strcat (pathname, "/");
|
||
|
||
strcat (pathname, name);
|
||
|
||
ret = VasEBoot_util_is_directory (pathname);
|
||
free (pathname);
|
||
return ret;
|
||
}
|
||
|
||
struct VasEBoot_hostfs_data
|
||
{
|
||
char *filename;
|
||
VasEBoot_util_fd_t f;
|
||
};
|
||
|
||
static VasEBoot_err_t
|
||
VasEBoot_hostfs_dir (VasEBoot_device_t device, const char *path,
|
||
VasEBoot_fs_dir_hook_t hook, void *hook_data)
|
||
{
|
||
VasEBoot_util_fd_dir_t dir;
|
||
|
||
/* Check if the disk is our dummy disk. */
|
||
if (VasEBoot_strcmp (device->disk->name, "host"))
|
||
return VasEBoot_error (VasEBoot_ERR_BAD_FS, "not a hostfs");
|
||
|
||
dir = VasEBoot_util_fd_opendir (path);
|
||
if (! dir)
|
||
return VasEBoot_error (VasEBoot_ERR_BAD_FILENAME,
|
||
N_("can't open `%s': %s"), path,
|
||
VasEBoot_util_fd_strerror ());
|
||
|
||
while (1)
|
||
{
|
||
VasEBoot_util_fd_dirent_t de;
|
||
struct VasEBoot_dirhook_info info;
|
||
VasEBoot_memset (&info, 0, sizeof (info));
|
||
|
||
de = VasEBoot_util_fd_readdir (dir);
|
||
if (! de)
|
||
break;
|
||
|
||
info.dir = !! is_dir (path, de->d_name);
|
||
hook (de->d_name, &info, hook_data);
|
||
|
||
}
|
||
|
||
VasEBoot_util_fd_closedir (dir);
|
||
|
||
return VasEBoot_ERR_NONE;
|
||
}
|
||
|
||
/* Open a file named NAME and initialize FILE. */
|
||
static VasEBoot_err_t
|
||
VasEBoot_hostfs_open (struct VasEBoot_file *file, const char *name)
|
||
{
|
||
VasEBoot_util_fd_t f;
|
||
struct VasEBoot_hostfs_data *data;
|
||
|
||
f = VasEBoot_util_fd_open (name, VasEBoot_UTIL_FD_O_RDONLY);
|
||
if (! VasEBoot_UTIL_FD_IS_VALID (f))
|
||
return VasEBoot_error (VasEBoot_ERR_BAD_FILENAME,
|
||
N_("can't open `%s': %s"), name,
|
||
strerror (errno));
|
||
data = VasEBoot_malloc (sizeof (*data));
|
||
if (!data)
|
||
{
|
||
VasEBoot_util_fd_close (f);
|
||
return VasEBoot_errno;
|
||
}
|
||
data->filename = VasEBoot_strdup (name);
|
||
if (!data->filename)
|
||
{
|
||
VasEBoot_free (data);
|
||
VasEBoot_util_fd_close (f);
|
||
return VasEBoot_errno;
|
||
}
|
||
|
||
data->f = f;
|
||
|
||
file->data = data;
|
||
|
||
file->size = VasEBoot_util_get_fd_size (f, name, NULL);
|
||
|
||
return VasEBoot_ERR_NONE;
|
||
}
|
||
|
||
static VasEBoot_ssize_t
|
||
VasEBoot_hostfs_read (VasEBoot_file_t file, char *buf, VasEBoot_size_t len)
|
||
{
|
||
struct VasEBoot_hostfs_data *data;
|
||
|
||
data = file->data;
|
||
if (VasEBoot_util_fd_seek (data->f, file->offset) != 0)
|
||
{
|
||
VasEBoot_error (VasEBoot_ERR_OUT_OF_RANGE, N_("cannot seek `%s': %s"),
|
||
data->filename, VasEBoot_util_fd_strerror ());
|
||
return -1;
|
||
}
|
||
|
||
unsigned int s = VasEBoot_util_fd_read (data->f, buf, len);
|
||
if (s != len)
|
||
VasEBoot_error (VasEBoot_ERR_FILE_READ_ERROR, N_("cannot read `%s': %s"),
|
||
data->filename, VasEBoot_util_fd_strerror ());
|
||
|
||
return (signed) s;
|
||
}
|
||
|
||
static VasEBoot_err_t
|
||
VasEBoot_hostfs_close (VasEBoot_file_t file)
|
||
{
|
||
struct VasEBoot_hostfs_data *data;
|
||
|
||
data = file->data;
|
||
VasEBoot_util_fd_close (data->f);
|
||
VasEBoot_free (data->filename);
|
||
VasEBoot_free (data);
|
||
|
||
return VasEBoot_ERR_NONE;
|
||
}
|
||
|
||
static VasEBoot_err_t
|
||
VasEBoot_hostfs_label (VasEBoot_device_t device __attribute ((unused)),
|
||
char **label __attribute ((unused)))
|
||
{
|
||
*label = 0;
|
||
return VasEBoot_ERR_NONE;
|
||
}
|
||
|
||
static struct VasEBoot_fs VasEBoot_hostfs_fs =
|
||
{
|
||
.name = "hostfs",
|
||
.dir = VasEBoot_hostfs_dir,
|
||
.open = VasEBoot_hostfs_open,
|
||
.read = VasEBoot_hostfs_read,
|
||
.close = VasEBoot_hostfs_close,
|
||
.label = VasEBoot_hostfs_label,
|
||
.next = 0
|
||
};
|
||
|
||
|
||
|
||
VasEBoot_MOD_INIT(hostfs)
|
||
{
|
||
VasEBoot_fs_register (&VasEBoot_hostfs_fs);
|
||
}
|
||
|
||
VasEBoot_MOD_FINI(hostfs)
|
||
{
|
||
VasEBoot_fs_unregister (&VasEBoot_hostfs_fs);
|
||
}
|