apr_buckets_file.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
  2. * applicable.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "apr.h"
  17. #include "apr_general.h"
  18. #include "apr_file_io.h"
  19. #include "apr_buckets.h"
  20. #if APR_HAS_MMAP
  21. #include "apr_mmap.h"
  22. /* mmap support for static files based on ideas from John Heidemann's
  23. * patch against 1.0.5. See
  24. * <http://www.isi.edu/~johnh/SOFTWARE/APACHE/index.html>.
  25. */
  26. #endif /* APR_HAS_MMAP */
  27. static void file_bucket_destroy(void *data)
  28. {
  29. apr_bucket_file *f = data;
  30. if (apr_bucket_shared_destroy(f)) {
  31. /* no need to close the file here; it will get
  32. * done automatically when the pool gets cleaned up */
  33. apr_bucket_free(f);
  34. }
  35. }
  36. #if APR_HAS_MMAP
  37. static int file_make_mmap(apr_bucket *e, apr_size_t filelength,
  38. apr_off_t fileoffset, apr_pool_t *p)
  39. {
  40. apr_bucket_file *a = e->data;
  41. apr_mmap_t *mm;
  42. if (!a->can_mmap) {
  43. return 0;
  44. }
  45. if (filelength > APR_MMAP_LIMIT) {
  46. if (apr_mmap_create(&mm, a->fd, fileoffset, APR_MMAP_LIMIT,
  47. APR_MMAP_READ, p) != APR_SUCCESS)
  48. {
  49. return 0;
  50. }
  51. apr_bucket_split(e, APR_MMAP_LIMIT);
  52. filelength = APR_MMAP_LIMIT;
  53. }
  54. else if ((filelength < APR_MMAP_THRESHOLD) ||
  55. (apr_mmap_create(&mm, a->fd, fileoffset, filelength,
  56. APR_MMAP_READ, p) != APR_SUCCESS))
  57. {
  58. return 0;
  59. }
  60. apr_bucket_mmap_make(e, mm, 0, filelength);
  61. file_bucket_destroy(a);
  62. return 1;
  63. }
  64. #endif
  65. static apr_status_t file_bucket_read(apr_bucket *e, const char **str,
  66. apr_size_t *len, apr_read_type_e block)
  67. {
  68. apr_bucket_file *a = e->data;
  69. apr_file_t *f = a->fd;
  70. apr_bucket *b = NULL;
  71. char *buf;
  72. apr_status_t rv;
  73. apr_size_t filelength = e->length; /* bytes remaining in file past offset */
  74. apr_off_t fileoffset = e->start;
  75. #if APR_HAS_THREADS && !APR_HAS_XTHREAD_FILES
  76. apr_int32_t flags;
  77. #endif
  78. #if APR_HAS_MMAP
  79. if (file_make_mmap(e, filelength, fileoffset, a->readpool)) {
  80. return apr_bucket_read(e, str, len, block);
  81. }
  82. #endif
  83. #if APR_HAS_THREADS && !APR_HAS_XTHREAD_FILES
  84. if ((flags = apr_file_flags_get(f)) & APR_XTHREAD) {
  85. /* this file descriptor is shared across multiple threads and
  86. * this OS doesn't support that natively, so as a workaround
  87. * we must reopen the file into a->readpool */
  88. const char *fname;
  89. apr_file_name_get(&fname, f);
  90. rv = apr_file_open(&f, fname, (flags & ~APR_XTHREAD), 0, a->readpool);
  91. if (rv != APR_SUCCESS)
  92. return rv;
  93. a->fd = f;
  94. }
  95. #endif
  96. *len = (filelength > APR_BUCKET_BUFF_SIZE)
  97. ? APR_BUCKET_BUFF_SIZE
  98. : filelength;
  99. *str = NULL; /* in case we die prematurely */
  100. buf = apr_bucket_alloc(*len, e->list);
  101. /* Handle offset ... */
  102. rv = apr_file_seek(f, APR_SET, &fileoffset);
  103. if (rv != APR_SUCCESS) {
  104. apr_bucket_free(buf);
  105. return rv;
  106. }
  107. rv = apr_file_read(f, buf, len);
  108. if (rv != APR_SUCCESS && rv != APR_EOF) {
  109. apr_bucket_free(buf);
  110. return rv;
  111. }
  112. filelength -= *len;
  113. /*
  114. * Change the current bucket to refer to what we read,
  115. * even if we read nothing because we hit EOF.
  116. */
  117. apr_bucket_heap_make(e, buf, *len, apr_bucket_free);
  118. /* If we have more to read from the file, then create another bucket */
  119. if (filelength > 0 && rv != APR_EOF) {
  120. /* for efficiency, we can just build a new apr_bucket struct
  121. * to wrap around the existing file bucket */
  122. b = apr_bucket_alloc(sizeof(*b), e->list);
  123. b->start = fileoffset + (*len);
  124. b->length = filelength;
  125. b->data = a;
  126. b->type = &apr_bucket_type_file;
  127. b->free = apr_bucket_free;
  128. b->list = e->list;
  129. APR_BUCKET_INSERT_AFTER(e, b);
  130. }
  131. else {
  132. file_bucket_destroy(a);
  133. }
  134. *str = buf;
  135. return rv;
  136. }
  137. APU_DECLARE(apr_bucket *) apr_bucket_file_make(apr_bucket *b, apr_file_t *fd,
  138. apr_off_t offset,
  139. apr_size_t len, apr_pool_t *p)
  140. {
  141. apr_bucket_file *f;
  142. f = apr_bucket_alloc(sizeof(*f), b->list);
  143. f->fd = fd;
  144. f->readpool = p;
  145. #if APR_HAS_MMAP
  146. f->can_mmap = 1;
  147. #endif
  148. b = apr_bucket_shared_make(b, f, offset, len);
  149. b->type = &apr_bucket_type_file;
  150. return b;
  151. }
  152. APU_DECLARE(apr_bucket *) apr_bucket_file_create(apr_file_t *fd,
  153. apr_off_t offset,
  154. apr_size_t len, apr_pool_t *p,
  155. apr_bucket_alloc_t *list)
  156. {
  157. apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
  158. APR_BUCKET_INIT(b);
  159. b->free = apr_bucket_free;
  160. b->list = list;
  161. return apr_bucket_file_make(b, fd, offset, len, p);
  162. }
  163. APU_DECLARE(apr_status_t) apr_bucket_file_enable_mmap(apr_bucket *e,
  164. int enabled)
  165. {
  166. #if APR_HAS_MMAP
  167. apr_bucket_file *a = e->data;
  168. a->can_mmap = enabled;
  169. return APR_SUCCESS;
  170. #else
  171. return APR_ENOTIMPL;
  172. #endif /* APR_HAS_MMAP */
  173. }
  174. static apr_status_t file_bucket_setaside(apr_bucket *data, apr_pool_t *reqpool)
  175. {
  176. apr_bucket_file *a = data->data;
  177. apr_file_t *fd = NULL;
  178. apr_file_t *f = a->fd;
  179. apr_pool_t *curpool = apr_file_pool_get(f);
  180. if (apr_pool_is_ancestor(curpool, reqpool)) {
  181. return APR_SUCCESS;
  182. }
  183. if (!apr_pool_is_ancestor(a->readpool, reqpool)) {
  184. a->readpool = reqpool;
  185. }
  186. apr_file_setaside(&fd, f, reqpool);
  187. a->fd = fd;
  188. return APR_SUCCESS;
  189. }
  190. APU_DECLARE_DATA const apr_bucket_type_t apr_bucket_type_file = {
  191. "FILE", 5, APR_BUCKET_DATA,
  192. file_bucket_destroy,
  193. file_bucket_read,
  194. file_bucket_setaside,
  195. apr_bucket_shared_split,
  196. apr_bucket_shared_copy
  197. };