file.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* file.c - file I/O functions */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2002,2006,2007,2009 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 <grub/misc.h>
  20. #include <grub/err.h>
  21. #include <grub/file.h>
  22. #include <grub/net.h>
  23. #include <grub/mm.h>
  24. #include <grub/fs.h>
  25. #include <grub/device.h>
  26. #include <grub/i18n.h>
  27. void (*EXPORT_VAR (grub_grubnet_fini)) (void);
  28. grub_file_filter_t grub_file_filters[GRUB_FILE_FILTER_MAX];
  29. /* Get the device part of the filename NAME. It is enclosed by parentheses. */
  30. char *
  31. grub_file_get_device_name (const char *name)
  32. {
  33. if (name[0] == '(')
  34. {
  35. char *p = grub_strchr (name, ')');
  36. char *ret;
  37. if (! p)
  38. {
  39. grub_error (GRUB_ERR_BAD_FILENAME, N_("missing `%c' symbol"), ')');
  40. return 0;
  41. }
  42. ret = (char *) grub_malloc (p - name);
  43. if (! ret)
  44. return 0;
  45. grub_memcpy (ret, name + 1, p - name - 1);
  46. ret[p - name - 1] = '\0';
  47. return ret;
  48. }
  49. return 0;
  50. }
  51. grub_file_t
  52. grub_file_open (const char *name, enum grub_file_type type)
  53. {
  54. grub_device_t device = 0;
  55. grub_file_t file = 0, last_file = 0;
  56. char *device_name;
  57. const char *file_name;
  58. grub_file_filter_id_t filter;
  59. /* Reset grub_errno before we start. */
  60. grub_errno = GRUB_ERR_NONE;
  61. device_name = grub_file_get_device_name (name);
  62. if (grub_errno)
  63. goto fail;
  64. /* Get the file part of NAME. */
  65. file_name = (name[0] == '(') ? grub_strchr (name, ')') : NULL;
  66. if (file_name)
  67. file_name++;
  68. else
  69. file_name = name;
  70. device = grub_device_open (device_name);
  71. grub_free (device_name);
  72. device_name = NULL;
  73. if (! device)
  74. goto fail;
  75. file = (grub_file_t) grub_zalloc (sizeof (*file));
  76. if (! file)
  77. goto fail;
  78. file->device = device;
  79. /* In case of relative pathnames and non-Unix systems (like Windows)
  80. * name of host files may not start with `/'. Blocklists for host files
  81. * are meaningless as well (for a start, host disk does not allow any direct
  82. * access - it is just a marker). So skip host disk in this case.
  83. */
  84. if (device->disk && file_name[0] != '/'
  85. #if defined(GRUB_UTIL) || defined(GRUB_MACHINE_EMU)
  86. && grub_strcmp (device->disk->name, "host")
  87. #endif
  88. )
  89. /* This is a block list. */
  90. file->fs = &grub_fs_blocklist;
  91. else
  92. {
  93. file->fs = grub_fs_probe (device);
  94. if (! file->fs)
  95. goto fail;
  96. }
  97. if ((file->fs->fs_open) (file, file_name) != GRUB_ERR_NONE)
  98. goto fail;
  99. file->name = grub_strdup (name);
  100. grub_errno = GRUB_ERR_NONE;
  101. for (filter = 0; file && filter < ARRAY_SIZE (grub_file_filters);
  102. filter++)
  103. if (grub_file_filters[filter])
  104. {
  105. last_file = file;
  106. file = grub_file_filters[filter] (file, type);
  107. if (file && file != last_file)
  108. {
  109. file->name = grub_strdup (name);
  110. grub_errno = GRUB_ERR_NONE;
  111. }
  112. }
  113. if (!file)
  114. grub_file_close (last_file);
  115. return file;
  116. fail:
  117. grub_free (device_name);
  118. if (device)
  119. grub_device_close (device);
  120. /* if (net) grub_net_close (net); */
  121. grub_free (file);
  122. return 0;
  123. }
  124. grub_disk_read_hook_t grub_file_progress_hook;
  125. grub_ssize_t
  126. grub_file_read (grub_file_t file, void *buf, grub_size_t len)
  127. {
  128. grub_ssize_t res;
  129. grub_disk_read_hook_t read_hook;
  130. void *read_hook_data;
  131. if (file->offset > file->size)
  132. {
  133. grub_error (GRUB_ERR_OUT_OF_RANGE,
  134. N_("attempt to read past the end of file"));
  135. return -1;
  136. }
  137. if (len == 0)
  138. return 0;
  139. if (len > file->size - file->offset)
  140. len = file->size - file->offset;
  141. /* Prevent an overflow. */
  142. if ((grub_ssize_t) len < 0)
  143. len >>= 1;
  144. if (len == 0)
  145. return 0;
  146. read_hook = file->read_hook;
  147. read_hook_data = file->read_hook_data;
  148. if (!file->read_hook)
  149. {
  150. file->read_hook = grub_file_progress_hook;
  151. file->read_hook_data = file;
  152. file->progress_offset = file->offset;
  153. }
  154. res = (file->fs->fs_read) (file, buf, len);
  155. file->read_hook = read_hook;
  156. file->read_hook_data = read_hook_data;
  157. if (res > 0)
  158. file->offset += res;
  159. return res;
  160. }
  161. grub_err_t
  162. grub_file_close (grub_file_t file)
  163. {
  164. if (file->fs->fs_close)
  165. (file->fs->fs_close) (file);
  166. if (file->device)
  167. grub_device_close (file->device);
  168. grub_free (file->name);
  169. grub_free (file);
  170. return grub_errno;
  171. }
  172. grub_off_t
  173. grub_file_seek (grub_file_t file, grub_off_t offset)
  174. {
  175. grub_off_t old;
  176. if (offset > file->size)
  177. {
  178. grub_error (GRUB_ERR_OUT_OF_RANGE,
  179. N_("attempt to seek outside of the file"));
  180. return -1;
  181. }
  182. old = file->offset;
  183. file->offset = offset;
  184. return old;
  185. }