~hamo/ubuntu/precise/grub2/grub2.hi_res

« back to all changes in this revision

Viewing changes to util/hostfs.c

  • Committer: Bazaar Package Importer
  • Author(s): Robert Millan
  • Date: 2007-11-01 13:18:51 UTC
  • mto: (17.3.1 squeeze) (1.9.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 15.
  • Revision ID: james.westby@ubuntu.com-20071101131851-63uqsb4dax2h1cbm
Tags: upstream-1.95+20071101
ImportĀ upstreamĀ versionĀ 1.95+20071101

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* hostfs.c - Dummy filesystem to provide access to the hosts filesystem  */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2007  Free Software Foundation, Inc.
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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/>.
 
18
 */
 
19
 
 
20
#include <grub/fs.h>
 
21
#include <grub/file.h>
 
22
#include <grub/disk.h>
 
23
#include <grub/misc.h>
 
24
 
 
25
#include <dirent.h>
 
26
#include <stdio.h>
 
27
 
 
28
static grub_err_t
 
29
grub_hostfs_dir (grub_device_t device, const char *path, 
 
30
                 int (*hook) (const char *filename, int dir))
 
31
{
 
32
  DIR *dir;
 
33
 
 
34
  /* Check if the disk is our dummy disk.  */
 
35
  if (grub_strcmp (device->disk->name, "host"))
 
36
    return grub_error (GRUB_ERR_BAD_FS, "not a hostfs");
 
37
 
 
38
  dir = opendir (path);
 
39
  if (! dir)
 
40
    return grub_error (GRUB_ERR_BAD_FILENAME,
 
41
                       "can't open the hostfs directory `%s'", path);
 
42
 
 
43
  while (1)
 
44
    {
 
45
      struct dirent *de;
 
46
 
 
47
      de = readdir (dir);
 
48
      if (! de)
 
49
        break;
 
50
 
 
51
      hook (de->d_name, de->d_type == DT_DIR);
 
52
    }
 
53
 
 
54
  closedir (dir);
 
55
 
 
56
  return GRUB_ERR_NONE;
 
57
}
 
58
 
 
59
/* Open a file named NAME and initialize FILE.  */
 
60
static grub_err_t
 
61
grub_hostfs_open (struct grub_file *file, const char *name)
 
62
{
 
63
  FILE *f;
 
64
 
 
65
  f = fopen (name, "r");
 
66
  if (! f)
 
67
    return grub_error (GRUB_ERR_BAD_FILENAME,
 
68
                       "can't open `%s'", name);
 
69
  file->data = f;
 
70
 
 
71
  fseek (f, 0, SEEK_END);
 
72
  file->size = ftell (f);
 
73
  fseek (f, 0, SEEK_SET);
 
74
 
 
75
  return GRUB_ERR_NONE;
 
76
}
 
77
 
 
78
static grub_ssize_t
 
79
grub_hostfs_read (grub_file_t file, char *buf, grub_size_t len)
 
80
{
 
81
  FILE *f;
 
82
 
 
83
  f = (FILE *) file->data;
 
84
  int s= fread (buf, 1, len, f);
 
85
 
 
86
  return s;
 
87
}
 
88
 
 
89
static grub_err_t
 
90
grub_hostfs_close (grub_file_t file)
 
91
{
 
92
  FILE *f;
 
93
 
 
94
  f = (FILE *) file->data;
 
95
  fclose (f);
 
96
 
 
97
  return GRUB_ERR_NONE;
 
98
}
 
99
 
 
100
static grub_err_t
 
101
grub_hostfs_label (grub_device_t device __attribute ((unused)),
 
102
                   char **label __attribute ((unused)))
 
103
{
 
104
  return GRUB_ERR_NONE;
 
105
}
 
106
 
 
107
static struct grub_fs grub_hostfs_fs =
 
108
  {
 
109
    .name = "hostfs",
 
110
    .dir = grub_hostfs_dir,
 
111
    .open = grub_hostfs_open,
 
112
    .read = grub_hostfs_read,
 
113
    .close = grub_hostfs_close,
 
114
    .label = grub_hostfs_label,
 
115
    .next = 0
 
116
  };
 
117
 
 
118
 
 
119
 
 
120
void
 
121
grub_hostfs_init (void)
 
122
{
 
123
  grub_fs_register (&grub_hostfs_fs);
 
124
}
 
125
 
 
126
void
 
127
grub_hostfs_fini (void)
 
128
{
 
129
  grub_fs_unregister (&grub_hostfs_fs);
 
130
}