file.c 5.1 KB

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