hostfs.c 4.4 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. #define _BSD_SOURCE
  20. #include <grub/fs.h>
  21. #include <grub/file.h>
  22. #include <grub/disk.h>
  23. #include <grub/misc.h>
  24. #include <grub/dl.h>
  25. #include <grub/util/misc.h>
  26. #include <dirent.h>
  27. #include <stdio.h>
  28. #include <errno.h>
  29. #ifdef __MACH__
  30. #include <sys/disk.h>
  31. #endif
  32. /* dirent.d_type is a BSD extension, not part of POSIX */
  33. #include <sys/stat.h>
  34. #include <string.h>
  35. static int
  36. is_dir (const char *path, const char *name)
  37. {
  38. int len1 = strlen(path);
  39. int len2 = strlen(name);
  40. char pathname[len1 + 1 + len2 + 1 + 13];
  41. strcpy (pathname, path);
  42. /* Avoid UNC-path "//name" on Cygwin. */
  43. if (len1 > 0 && pathname[len1 - 1] != '/')
  44. strcat (pathname, "/");
  45. strcat (pathname, name);
  46. struct stat st;
  47. if (stat (pathname, &st))
  48. return 0;
  49. return S_ISDIR (st.st_mode);
  50. }
  51. static grub_err_t
  52. grub_hostfs_dir (grub_device_t device, const char *path,
  53. int (*hook) (const char *filename,
  54. const struct grub_dirhook_info *info,
  55. void *closure), void *closure)
  56. {
  57. DIR *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 = opendir (path);
  62. if (! dir)
  63. return grub_error (GRUB_ERR_BAD_FILENAME,
  64. "can't open the hostfs directory `%s'", path);
  65. while (1)
  66. {
  67. struct dirent *de;
  68. struct grub_dirhook_info info;
  69. grub_memset (&info, 0, sizeof (info));
  70. de = readdir (dir);
  71. if (! de)
  72. break;
  73. info.dir = !! is_dir (path, de->d_name);
  74. hook (de->d_name, &info, closure);
  75. }
  76. closedir (dir);
  77. return GRUB_ERR_NONE;
  78. }
  79. /* Open a file named NAME and initialize FILE. */
  80. static grub_err_t
  81. grub_hostfs_open (struct grub_file *file, const char *name)
  82. {
  83. FILE *f;
  84. f = fopen (name, "rb");
  85. if (! f)
  86. return grub_error (GRUB_ERR_BAD_FILENAME,
  87. "can't open `%s'", name);
  88. file->data = f;
  89. #ifdef __MINGW32__
  90. file->size = grub_util_get_disk_size (name);
  91. #elif defined(__MACH__)
  92. if (! strncmp (name, "/dev/", 5))
  93. {
  94. unsigned long long int count;
  95. unsigned int size;
  96. if ((ioctl (fileno (f), DKIOCGETBLOCKSIZE, &size) < 0) ||
  97. (ioctl (fileno (f), DKIOCGETBLOCKCOUNT, &count) < 0))
  98. {
  99. file->data = 0;
  100. fclose (f);
  101. return grub_error (GRUB_ERR_BAD_FILENAME, "ioctl fails");
  102. }
  103. file->size = count * size;
  104. }
  105. else
  106. {
  107. fseeko (f, 0, SEEK_END);
  108. file->size = ftello (f);
  109. fseeko (f, 0, SEEK_SET);
  110. }
  111. #else
  112. fseeko (f, 0, SEEK_END);
  113. file->size = ftello (f);
  114. fseeko (f, 0, SEEK_SET);
  115. #endif
  116. return GRUB_ERR_NONE;
  117. }
  118. static grub_ssize_t
  119. grub_hostfs_read (grub_file_t file, char *buf, grub_size_t len)
  120. {
  121. FILE *f;
  122. f = (FILE *) file->data;
  123. if (fseeko (f, file->offset, SEEK_SET) != 0)
  124. {
  125. grub_error (GRUB_ERR_OUT_OF_RANGE, "fseeko: %s", strerror (errno));
  126. return -1;
  127. }
  128. unsigned int s = fread (buf, 1, len, f);
  129. if (s != len)
  130. grub_error (GRUB_ERR_FILE_READ_ERROR, "fread: %s", strerror (errno));
  131. return (signed) s;
  132. }
  133. static grub_err_t
  134. grub_hostfs_close (grub_file_t file)
  135. {
  136. FILE *f;
  137. f = (FILE *) file->data;
  138. fclose (f);
  139. return GRUB_ERR_NONE;
  140. }
  141. static grub_err_t
  142. grub_hostfs_label (grub_device_t device __attribute ((unused)),
  143. char **label __attribute ((unused)))
  144. {
  145. *label = 0;
  146. return GRUB_ERR_NONE;
  147. }
  148. static struct grub_fs grub_hostfs_fs =
  149. {
  150. .name = "hostfs",
  151. .dir = grub_hostfs_dir,
  152. .open = grub_hostfs_open,
  153. .read = grub_hostfs_read,
  154. .close = grub_hostfs_close,
  155. .label = grub_hostfs_label,
  156. .next = 0
  157. };
  158. GRUB_MOD_INIT(hostfs)
  159. {
  160. grub_fs_register (&grub_hostfs_fs);
  161. }
  162. GRUB_MOD_FINI(hostfs)
  163. {
  164. grub_fs_unregister (&grub_hostfs_fs);
  165. }