bufio.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* bufio.c - buffered io access */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2008 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/err.h>
  20. #include <grub/types.h>
  21. #include <grub/mm.h>
  22. #include <grub/misc.h>
  23. #include <grub/fs.h>
  24. #include <grub/bufio.h>
  25. #include <grub/dl.h>
  26. GRUB_MOD_LICENSE ("GPLv3+");
  27. #define GRUB_BUFIO_DEF_SIZE 8192
  28. #define GRUB_BUFIO_MAX_SIZE 1048576
  29. struct grub_bufio
  30. {
  31. grub_file_t file;
  32. grub_size_t block_size;
  33. grub_size_t buffer_len;
  34. grub_off_t buffer_at;
  35. char buffer[0];
  36. };
  37. typedef struct grub_bufio *grub_bufio_t;
  38. static struct grub_fs grub_bufio_fs;
  39. grub_file_t
  40. grub_bufio_open (grub_file_t io, int size)
  41. {
  42. grub_file_t file;
  43. grub_bufio_t bufio = 0;
  44. file = (grub_file_t) grub_zalloc (sizeof (*file));
  45. if (! file)
  46. return 0;
  47. if (size == 0)
  48. size = GRUB_BUFIO_DEF_SIZE;
  49. else if (size > GRUB_BUFIO_MAX_SIZE)
  50. size = GRUB_BUFIO_MAX_SIZE;
  51. if ((size < 0) || ((unsigned) size > io->size))
  52. size = ((io->size > GRUB_BUFIO_MAX_SIZE) ? GRUB_BUFIO_MAX_SIZE :
  53. io->size);
  54. bufio = grub_zalloc (sizeof (struct grub_bufio) + size);
  55. if (! bufio)
  56. {
  57. grub_free (file);
  58. return 0;
  59. }
  60. bufio->file = io;
  61. bufio->block_size = size;
  62. file->device = io->device;
  63. file->size = io->size;
  64. file->data = bufio;
  65. file->fs = &grub_bufio_fs;
  66. file->not_easily_seekable = io->not_easily_seekable;
  67. return file;
  68. }
  69. grub_file_t
  70. grub_buffile_open (const char *name, int size)
  71. {
  72. grub_file_t io, file;
  73. io = grub_file_open (name);
  74. if (! io)
  75. return 0;
  76. file = grub_bufio_open (io, size);
  77. if (! file)
  78. {
  79. grub_file_close (io);
  80. return 0;
  81. }
  82. return file;
  83. }
  84. static grub_ssize_t
  85. grub_bufio_read (grub_file_t file, char *buf, grub_size_t len)
  86. {
  87. grub_size_t res = 0;
  88. grub_off_t next_buf;
  89. grub_bufio_t bufio = file->data;
  90. grub_ssize_t really_read;
  91. if (file->size == GRUB_FILE_SIZE_UNKNOWN)
  92. file->size = bufio->file->size;
  93. /* First part: use whatever we already have in the buffer. */
  94. if ((file->offset >= bufio->buffer_at) &&
  95. (file->offset < bufio->buffer_at + bufio->buffer_len))
  96. {
  97. grub_size_t n;
  98. grub_uint64_t pos;
  99. pos = file->offset - bufio->buffer_at;
  100. n = bufio->buffer_len - pos;
  101. if (n > len)
  102. n = len;
  103. grub_memcpy (buf, &bufio->buffer[pos], n);
  104. len -= n;
  105. res += n;
  106. buf += n;
  107. }
  108. if (len == 0)
  109. return res;
  110. /* Need to read some more. */
  111. next_buf = (file->offset + res + len - 1) & ~((grub_off_t) bufio->block_size - 1);
  112. /* Now read between file->offset + res and bufio->buffer_at. */
  113. if (file->offset + res < next_buf)
  114. {
  115. grub_size_t read_now;
  116. read_now = next_buf - (file->offset + res);
  117. grub_file_seek (bufio->file, file->offset + res);
  118. really_read = grub_file_read (bufio->file, buf, read_now);
  119. if (really_read < 0)
  120. return -1;
  121. if (file->size == GRUB_FILE_SIZE_UNKNOWN)
  122. file->size = bufio->file->size;
  123. len -= really_read;
  124. buf += really_read;
  125. res += really_read;
  126. /* Partial read. File ended unexpectedly. Save the last chunk in buffer.
  127. */
  128. if (really_read != (grub_ssize_t) read_now)
  129. {
  130. bufio->buffer_len = really_read;
  131. if (bufio->buffer_len > bufio->block_size)
  132. bufio->buffer_len = bufio->block_size;
  133. bufio->buffer_at = file->offset + res - bufio->buffer_len;
  134. grub_memcpy (&bufio->buffer[0], buf - bufio->buffer_len,
  135. bufio->buffer_len);
  136. return res;
  137. }
  138. }
  139. /* Read into buffer. */
  140. grub_file_seek (bufio->file, next_buf);
  141. really_read = grub_file_read (bufio->file, bufio->buffer,
  142. bufio->block_size);
  143. if (really_read < 0)
  144. return -1;
  145. bufio->buffer_at = next_buf;
  146. bufio->buffer_len = really_read;
  147. if (file->size == GRUB_FILE_SIZE_UNKNOWN)
  148. file->size = bufio->file->size;
  149. if (len > bufio->buffer_len)
  150. len = bufio->buffer_len;
  151. grub_memcpy (buf, &bufio->buffer[file->offset + res - next_buf], len);
  152. res += len;
  153. return res;
  154. }
  155. static grub_err_t
  156. grub_bufio_close (grub_file_t file)
  157. {
  158. grub_bufio_t bufio = file->data;
  159. grub_file_close (bufio->file);
  160. grub_free (bufio);
  161. file->device = 0;
  162. return grub_errno;
  163. }
  164. static struct grub_fs grub_bufio_fs =
  165. {
  166. .name = "bufio",
  167. .dir = 0,
  168. .open = 0,
  169. .read = grub_bufio_read,
  170. .close = grub_bufio_close,
  171. .label = 0,
  172. .next = 0
  173. };