file.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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/mm.h>
  23. #include <grub/fs.h>
  24. #include <grub/device.h>
  25. #include <grub/term.h>
  26. GRUB_EXPORT(grub_file_get_device_name);
  27. GRUB_EXPORT(grub_file_open);
  28. GRUB_EXPORT(grub_file_read);
  29. GRUB_EXPORT(grub_file_seek);
  30. GRUB_EXPORT(grub_file_close);
  31. GRUB_EXPORT(grub_file_pb_init);
  32. GRUB_EXPORT(grub_file_pb_fini);
  33. GRUB_EXPORT(grub_file_pb_show);
  34. GRUB_EXPORT(grub_file_pb_read);
  35. /* Get the device part of the filename NAME. It is enclosed by parentheses. */
  36. char *
  37. grub_file_get_device_name (const char *name)
  38. {
  39. if (name[0] == '(')
  40. {
  41. char *p = grub_strchr (name, ')');
  42. char *ret;
  43. if (! p)
  44. {
  45. grub_error (GRUB_ERR_BAD_FILENAME, "missing `)'");
  46. return 0;
  47. }
  48. ret = (char *) grub_malloc (p - name);
  49. if (! ret)
  50. return 0;
  51. grub_memcpy (ret, name + 1, p - name - 1);
  52. ret[p - name - 1] = '\0';
  53. return ret;
  54. }
  55. return 0;
  56. }
  57. grub_file_t
  58. grub_file_open (const char *name)
  59. {
  60. grub_device_t device;
  61. grub_file_t file = 0;
  62. char *device_name;
  63. char *file_name;
  64. device_name = grub_file_get_device_name (name);
  65. if (grub_errno)
  66. return 0;
  67. /* Get the file part of NAME. */
  68. file_name = grub_strchr (name, ')');
  69. if (file_name)
  70. file_name++;
  71. else
  72. file_name = (char *) name;
  73. device = grub_device_open (device_name);
  74. grub_free (device_name);
  75. if (! device)
  76. goto fail;
  77. file = (grub_file_t) grub_zalloc (sizeof (*file));
  78. if (! file)
  79. goto fail;
  80. file->device = device;
  81. if (device->disk && file_name[0] != '/')
  82. /* This is a block list. */
  83. file->fs = &grub_fs_blocklist;
  84. else
  85. {
  86. file->fs = grub_fs_probe (device);
  87. if (! file->fs)
  88. goto fail;
  89. }
  90. if ((file->fs->open) (file, file_name) != GRUB_ERR_NONE)
  91. goto fail;
  92. return file;
  93. fail:
  94. if (device)
  95. grub_device_close (device);
  96. /* if (net) grub_net_close (net); */
  97. grub_free (file);
  98. return 0;
  99. }
  100. grub_ssize_t
  101. grub_file_read (grub_file_t file, void *buf, grub_size_t len)
  102. {
  103. grub_ssize_t res;
  104. if (file->offset > file->size)
  105. {
  106. grub_error (GRUB_ERR_OUT_OF_RANGE,
  107. "attempt to read past the end of file");
  108. return -1;
  109. }
  110. if (len == 0 || len > file->size - file->offset)
  111. len = file->size - file->offset;
  112. /* Prevent an overflow. */
  113. if ((grub_ssize_t) len < 0)
  114. len >>= 1;
  115. if (len == 0)
  116. return 0;
  117. res = (file->fs->read) (file, buf, len);
  118. if (res > 0)
  119. file->offset += res;
  120. return res;
  121. }
  122. grub_err_t
  123. grub_file_close (grub_file_t file)
  124. {
  125. if (file->fs->close)
  126. (file->fs->close) (file);
  127. if (file->device)
  128. grub_device_close (file->device);
  129. grub_free (file);
  130. return grub_errno;
  131. }
  132. grub_off_t
  133. grub_file_seek (grub_file_t file, grub_off_t offset)
  134. {
  135. grub_off_t old;
  136. if (offset > file->size)
  137. {
  138. grub_error (GRUB_ERR_OUT_OF_RANGE,
  139. "attempt to seek outside of the file");
  140. return -1;
  141. }
  142. old = file->offset;
  143. file->offset = offset;
  144. return old;
  145. }
  146. static void
  147. grub_file_pb_show_default (int num __attribute__((unused)),
  148. int total __attribute__((unused)))
  149. {
  150. grub_printf (".");
  151. grub_refresh ();
  152. }
  153. static void
  154. grub_file_pb_fini_default (void)
  155. {
  156. grub_printf ("\n");
  157. }
  158. void (*grub_file_pb_init) (void);
  159. void (*grub_file_pb_fini) (void) = grub_file_pb_fini_default;
  160. void (*grub_file_pb_show) (int num, int total) = grub_file_pb_show_default;
  161. grub_ssize_t
  162. grub_file_pb_read (grub_file_t file, void *b, grub_size_t len, int total)
  163. {
  164. grub_ssize_t ret;
  165. grub_size_t bsize;
  166. int num;
  167. char *buf = b;
  168. if ((len < GRUB_FILE_PB_MIN_SIZE) || (total == 0))
  169. return grub_file_read (file, buf, len);
  170. ret = 0;
  171. if (grub_file_pb_init)
  172. grub_file_pb_init ();
  173. bsize = ((len / total) + 511) & (~511);
  174. num = 0;
  175. while (len > 0)
  176. {
  177. grub_size_t n;
  178. grub_ssize_t r;
  179. grub_file_pb_show (num, total);
  180. n = (len > bsize) ? bsize : len;
  181. r = grub_file_read (file, buf, n);
  182. if (r <= 0)
  183. {
  184. if (ret == 0)
  185. ret = -1;
  186. break;
  187. }
  188. buf += r;
  189. len -= r;
  190. ret += r;
  191. num++;
  192. }
  193. if (grub_file_pb_fini)
  194. grub_file_pb_fini ();
  195. return ret;
  196. }