hostfs.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.
  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. #include <config-util.h>
  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>
  29. #include <stdio.h>
  30. #include <errno.h>
  31. #include <string.h>
  32. static int
  33. is_dir (const char *path, const char *name)
  34. {
  35. int len1 = strlen(path);
  36. int len2 = strlen(name);
  37. int ret;
  38. char *pathname = xmalloc (len1 + 1 + len2 + 1 + 13);
  39. strcpy (pathname, path);
  40. /* Avoid UNC-path "//name" on Cygwin. */
  41. if (len1 > 0 && pathname[len1 - 1] != '/')
  42. strcat (pathname, "/");
  43. strcat (pathname, name);
  44. ret = grub_util_is_directory (pathname);
  45. free (pathname);
  46. return ret;
  47. }
  48. struct grub_hostfs_data
  49. {
  50. char *filename;
  51. grub_util_fd_t f;
  52. };
  53. static grub_err_t
  54. grub_hostfs_dir (grub_device_t device, const char *path,
  55. grub_fs_dir_hook_t hook, void *hook_data)
  56. {
  57. grub_util_fd_dir_t dir;
  58. /* Check if the disk is our dummy disk. */
  59. if (grub_strcmp (device->disk->name, "host"))
  60. return grub_error (GRUB_ERR_BAD_FS, "not a hostfs");
  61. dir = grub_util_fd_opendir (path);
  62. if (! dir)
  63. return grub_error (GRUB_ERR_BAD_FILENAME,
  64. N_("can't open `%s': %s"), path,
  65. grub_util_fd_strerror ());
  66. while (1)
  67. {
  68. grub_util_fd_dirent_t de;
  69. struct grub_dirhook_info info;
  70. grub_memset (&info, 0, sizeof (info));
  71. de = grub_util_fd_readdir (dir);
  72. if (! de)
  73. break;
  74. info.dir = !! is_dir (path, de->d_name);
  75. hook (de->d_name, &info, hook_data);
  76. }
  77. grub_util_fd_closedir (dir);
  78. return GRUB_ERR_NONE;
  79. }
  80. /* Open a file named NAME and initialize FILE. */
  81. static grub_err_t
  82. grub_hostfs_open (struct grub_file *file, const char *name)
  83. {
  84. grub_util_fd_t f;
  85. struct grub_hostfs_data *data;
  86. f = grub_util_fd_open (name, GRUB_UTIL_FD_O_RDONLY);
  87. if (! GRUB_UTIL_FD_IS_VALID (f))
  88. return grub_error (GRUB_ERR_BAD_FILENAME,
  89. N_("can't open `%s': %s"), name,
  90. strerror (errno));
  91. data = grub_malloc (sizeof (*data));
  92. if (!data)
  93. {
  94. grub_util_fd_close (f);
  95. return grub_errno;
  96. }
  97. data->filename = grub_strdup (name);
  98. if (!data->filename)
  99. {
  100. grub_free (data);
  101. grub_util_fd_close (f);
  102. return grub_errno;
  103. }
  104. data->f = f;
  105. file->data = data;
  106. file->size = grub_util_get_fd_size (f, name, NULL);
  107. return GRUB_ERR_NONE;
  108. }
  109. static grub_ssize_t
  110. grub_hostfs_read (grub_file_t file, char *buf, grub_size_t len)
  111. {
  112. struct grub_hostfs_data *data;
  113. data = file->data;
  114. if (grub_util_fd_seek (data->f, file->offset) != 0)
  115. {
  116. grub_error (GRUB_ERR_OUT_OF_RANGE, N_("cannot seek `%s': %s"),
  117. data->filename, grub_util_fd_strerror ());
  118. return -1;
  119. }
  120. unsigned int s = grub_util_fd_read (data->f, buf, len);
  121. if (s != len)
  122. grub_error (GRUB_ERR_FILE_READ_ERROR, N_("cannot read `%s': %s"),
  123. data->filename, grub_util_fd_strerror ());
  124. return (signed) s;
  125. }
  126. static grub_err_t
  127. grub_hostfs_close (grub_file_t file)
  128. {
  129. struct grub_hostfs_data *data;
  130. data = file->data;
  131. grub_util_fd_close (data->f);
  132. grub_free (data->filename);
  133. grub_free (data);
  134. return GRUB_ERR_NONE;
  135. }
  136. static grub_err_t
  137. grub_hostfs_label (grub_device_t device __attribute ((unused)),
  138. char **label __attribute ((unused)))
  139. {
  140. *label = 0;
  141. return GRUB_ERR_NONE;
  142. }
  143. #undef open
  144. #undef close
  145. static struct grub_fs grub_hostfs_fs =
  146. {
  147. .name = "hostfs",
  148. .fs_dir = grub_hostfs_dir,
  149. .fs_open = grub_hostfs_open,
  150. .fs_read = grub_hostfs_read,
  151. .fs_close = grub_hostfs_close,
  152. .fs_label = grub_hostfs_label,
  153. .next = 0
  154. };
  155. GRUB_MOD_INIT(hostfs)
  156. {
  157. grub_fs_register (&grub_hostfs_fs);
  158. }
  159. GRUB_MOD_FINI(hostfs)
  160. {
  161. grub_fs_unregister (&grub_hostfs_fs);
  162. }