file.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. device_name = grub_file_get_device_name (name);
  60. if (grub_errno)
  61. goto fail;
  62. /* Get the file part of NAME. */
  63. file_name = (name[0] == '(') ? grub_strchr (name, ')') : NULL;
  64. if (file_name)
  65. file_name++;
  66. else
  67. file_name = name;
  68. device = grub_device_open (device_name);
  69. grub_free (device_name);
  70. if (! device)
  71. goto fail;
  72. file = (grub_file_t) grub_zalloc (sizeof (*file));
  73. if (! file)
  74. goto fail;
  75. file->device = device;
  76. /* In case of relative pathnames and non-Unix systems (like Windows)
  77. * name of host files may not start with `/'. Blocklists for host files
  78. * are meaningless as well (for a start, host disk does not allow any direct
  79. * access - it is just a marker). So skip host disk in this case.
  80. */
  81. if (device->disk && file_name[0] != '/'
  82. #if defined(GRUB_UTIL) || defined(GRUB_MACHINE_EMU)
  83. && grub_strcmp (device->disk->name, "host")
  84. #endif
  85. )
  86. /* This is a block list. */
  87. file->fs = &grub_fs_blocklist;
  88. else
  89. {
  90. file->fs = grub_fs_probe (device);
  91. if (! file->fs)
  92. goto fail;
  93. }
  94. if ((file->fs->fs_open) (file, file_name) != GRUB_ERR_NONE)
  95. goto fail;
  96. file->name = grub_strdup (name);
  97. grub_errno = GRUB_ERR_NONE;
  98. for (filter = 0; file && filter < ARRAY_SIZE (grub_file_filters);
  99. filter++)
  100. if (grub_file_filters[filter])
  101. {
  102. last_file = file;
  103. file = grub_file_filters[filter] (file, type);
  104. if (file && file != last_file)
  105. {
  106. file->name = grub_strdup (name);
  107. grub_errno = GRUB_ERR_NONE;
  108. }
  109. }
  110. if (!file)
  111. grub_file_close (last_file);
  112. return file;
  113. fail:
  114. if (device)
  115. grub_device_close (device);
  116. /* if (net) grub_net_close (net); */
  117. grub_free (file);
  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->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->fs_close)
  161. (file->fs->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. }