~ubuntu-branches/ubuntu/trusty/grub2/trusty

« back to all changes in this revision

Viewing changes to grub-core/kern/emu/hostfs.c

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2014-01-16 15:18:04 UTC
  • mfrom: (17.6.38 experimental)
  • Revision ID: package-import@ubuntu.com-20140116151804-3foouk7fpqcq3sxx
Tags: 2.02~beta2-2
* Convert patch handling to git-dpm.
* Add bi-endian support to ELF parser (Tomohiro B Berry).
* Adjust restore_mkdevicemap.patch to mark get_kfreebsd_version as static,
  to appease "gcc -Werror=missing-prototypes".
* Cherry-pick from upstream:
  - Change grub-macbless' manual page section to 8.
* Install grub-glue-efi, grub-macbless, grub-render-label, and
  grub-syslinux2cfg.
* grub-shell: Pass -no-pad to xorriso when building floppy images.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 *  You should have received a copy of the GNU General Public License
17
17
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
18
18
 */
 
19
 
 
20
#include <config-util.h>
 
21
 
19
22
#define _BSD_SOURCE
20
23
#include <grub/fs.h>
21
24
#include <grub/file.h>
27
30
#include <grub/emu/hostdisk.h>
28
31
#include <grub/i18n.h>
29
32
 
30
 
#include <dirent.h>
31
33
#include <stdio.h>
32
34
#include <errno.h>
33
 
 
34
 
 
35
 
/* dirent.d_type is a BSD extension, not part of POSIX */
36
 
#include <sys/stat.h>
37
35
#include <string.h>
38
36
 
39
37
static int
41
39
{
42
40
  int len1 = strlen(path);
43
41
  int len2 = strlen(name);
 
42
  int ret;
44
43
 
45
 
  char pathname[len1 + 1 + len2 + 1 + 13];
 
44
  char *pathname = xmalloc (len1 + 1 + len2 + 1 + 13);
46
45
  strcpy (pathname, path);
47
46
 
48
47
  /* Avoid UNC-path "//name" on Cygwin.  */
51
50
 
52
51
  strcat (pathname, name);
53
52
 
54
 
  struct stat st;
55
 
  if (stat (pathname, &st))
56
 
    return 0;
57
 
  return S_ISDIR (st.st_mode);
 
53
  ret = grub_util_is_directory (pathname);
 
54
  free (pathname);
 
55
  return ret;
58
56
}
59
57
 
60
58
struct grub_hostfs_data
61
59
{
62
60
  char *filename;
63
 
  FILE *f;
 
61
  grub_util_fd_t f;
64
62
};
65
63
 
66
64
static grub_err_t
67
65
grub_hostfs_dir (grub_device_t device, const char *path,
68
 
                 int (*hook) (const char *filename,
69
 
                              const struct grub_dirhook_info *info))
 
66
                 grub_fs_dir_hook_t hook, void *hook_data)
70
67
{
71
 
  DIR *dir;
 
68
  grub_util_fd_dir_t dir;
72
69
 
73
70
  /* Check if the disk is our dummy disk.  */
74
71
  if (grub_strcmp (device->disk->name, "host"))
75
72
    return grub_error (GRUB_ERR_BAD_FS, "not a hostfs");
76
73
 
77
 
  dir = opendir (path);
 
74
  dir = grub_util_fd_opendir (path);
78
75
  if (! dir)
79
76
    return grub_error (GRUB_ERR_BAD_FILENAME,
80
77
                       N_("can't open `%s': %s"), path,
81
 
                       strerror (errno));
 
78
                       grub_util_fd_strerror ());
82
79
 
83
80
  while (1)
84
81
    {
85
 
      struct dirent *de;
 
82
      grub_util_fd_dirent_t de;
86
83
      struct grub_dirhook_info info;
87
84
      grub_memset (&info, 0, sizeof (info));
88
85
 
89
 
      de = readdir (dir);
 
86
      de = grub_util_fd_readdir (dir);
90
87
      if (! de)
91
88
        break;
92
89
 
93
90
      info.dir = !! is_dir (path, de->d_name);
94
 
      hook (de->d_name, &info);
 
91
      hook (de->d_name, &info, hook_data);
95
92
 
96
93
    }
97
94
 
98
 
  closedir (dir);
 
95
  grub_util_fd_closedir (dir);
99
96
 
100
97
  return GRUB_ERR_NONE;
101
98
}
104
101
static grub_err_t
105
102
grub_hostfs_open (struct grub_file *file, const char *name)
106
103
{
107
 
  FILE *f;
 
104
  grub_util_fd_t f;
108
105
  struct grub_hostfs_data *data;
109
106
 
110
 
  f = fopen (name, "rb");
111
 
  if (! f)
 
107
  f = grub_util_fd_open (name, GRUB_UTIL_FD_O_RDONLY);
 
108
  if (! GRUB_UTIL_FD_IS_VALID (f))
112
109
    return grub_error (GRUB_ERR_BAD_FILENAME,
113
110
                       N_("can't open `%s': %s"), name,
114
111
                       strerror (errno));
115
112
  data = grub_malloc (sizeof (*data));
116
113
  if (!data)
117
114
    {
118
 
      fclose (f);
 
115
      grub_util_fd_close (f);
119
116
      return grub_errno;
120
117
    }
121
118
  data->filename = grub_strdup (name);
122
119
  if (!data->filename)
123
120
    {
124
121
      grub_free (data);
125
 
      fclose (f);
 
122
      grub_util_fd_close (f);
126
123
      return grub_errno;
127
124
    }
128
125
 
130
127
 
131
128
  file->data = data;
132
129
 
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
 
130
  file->size = grub_util_get_fd_size (f, name, NULL);
138
131
 
139
132
  return GRUB_ERR_NONE;
140
133
}
145
138
  struct grub_hostfs_data *data;
146
139
 
147
140
  data = file->data;
148
 
  if (fseeko (data->f, file->offset, SEEK_SET) != 0)
 
141
  if (grub_util_fd_seek (data->f, file->offset) != 0)
149
142
    {
150
143
      grub_error (GRUB_ERR_OUT_OF_RANGE, N_("cannot seek `%s': %s"),
151
 
                  data->filename, strerror (errno));
 
144
                  data->filename, grub_util_fd_strerror ());
152
145
      return -1;
153
146
    }
154
147
 
155
 
  unsigned int s = fread (buf, 1, len, data->f);
 
148
  unsigned int s = grub_util_fd_read (data->f, buf, len);
156
149
  if (s != len)
157
150
    grub_error (GRUB_ERR_FILE_READ_ERROR, N_("cannot read `%s': %s"),
158
 
                data->filename, strerror (errno));
 
151
                data->filename, grub_util_fd_strerror ());
159
152
 
160
153
  return (signed) s;
161
154
}
166
159
  struct grub_hostfs_data *data;
167
160
 
168
161
  data = file->data;
169
 
  fclose (data->f);
 
162
  grub_util_fd_close (data->f);
170
163
  grub_free (data->filename);
171
164
  grub_free (data);
172
165