1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
#include "apr_general.h"
19
#include "apr_file_io.h"
20
#include "apr_buckets.h"
25
/* mmap support for static files based on ideas from John Heidemann's
26
* patch against 1.0.5. See
27
* <http://www.isi.edu/~johnh/SOFTWARE/APACHE/index.html>.
30
#endif /* APR_HAS_MMAP */
32
static void file_bucket_destroy(void *data)
34
apr_bucket_file *f = data;
36
if (apr_bucket_shared_destroy(f)) {
37
/* no need to close the file here; it will get
38
* done automatically when the pool gets cleaned up */
44
static int file_make_mmap(apr_bucket *e, apr_size_t filelength,
45
apr_off_t fileoffset, apr_pool_t *p)
47
apr_bucket_file *a = e->data;
54
if (filelength > APR_MMAP_LIMIT) {
55
if (apr_mmap_create(&mm, a->fd, fileoffset, APR_MMAP_LIMIT,
56
APR_MMAP_READ, p) != APR_SUCCESS)
60
apr_bucket_split(e, APR_MMAP_LIMIT);
61
filelength = APR_MMAP_LIMIT;
63
else if ((filelength < APR_MMAP_THRESHOLD) ||
64
(apr_mmap_create(&mm, a->fd, fileoffset, filelength,
65
APR_MMAP_READ, p) != APR_SUCCESS))
69
apr_bucket_mmap_make(e, mm, 0, filelength);
70
file_bucket_destroy(a);
75
static apr_status_t file_bucket_read(apr_bucket *e, const char **str,
76
apr_size_t *len, apr_read_type_e block)
78
apr_bucket_file *a = e->data;
79
apr_file_t *f = a->fd;
83
apr_size_t filelength = e->length; /* bytes remaining in file past offset */
84
apr_off_t fileoffset = e->start;
85
#if APR_HAS_THREADS && !APR_HAS_XTHREAD_FILES
90
if (file_make_mmap(e, filelength, fileoffset, a->readpool)) {
91
return apr_bucket_read(e, str, len, block);
95
#if APR_HAS_THREADS && !APR_HAS_XTHREAD_FILES
96
if ((flags = apr_file_flags_get(f)) & APR_XTHREAD) {
97
/* this file descriptor is shared across multiple threads and
98
* this OS doesn't support that natively, so as a workaround
99
* we must reopen the file into a->readpool */
101
apr_file_name_get(&fname, f);
103
rv = apr_file_open(&f, fname, (flags & ~APR_XTHREAD), 0, a->readpool);
104
if (rv != APR_SUCCESS)
111
*len = (filelength > APR_BUCKET_BUFF_SIZE)
112
? APR_BUCKET_BUFF_SIZE
114
*str = NULL; /* in case we die prematurely */
115
buf = apr_bucket_alloc(*len, e->list);
117
/* Handle offset ... */
118
rv = apr_file_seek(f, APR_SET, &fileoffset);
119
if (rv != APR_SUCCESS) {
120
apr_bucket_free(buf);
123
rv = apr_file_read(f, buf, len);
124
if (rv != APR_SUCCESS && rv != APR_EOF) {
125
apr_bucket_free(buf);
130
* Change the current bucket to refer to what we read,
131
* even if we read nothing because we hit EOF.
133
apr_bucket_heap_make(e, buf, *len, apr_bucket_free);
135
/* If we have more to read from the file, then create another bucket */
136
if (filelength > 0 && rv != APR_EOF) {
137
/* for efficiency, we can just build a new apr_bucket struct
138
* to wrap around the existing file bucket */
139
b = apr_bucket_alloc(sizeof(*b), e->list);
140
b->start = fileoffset + (*len);
141
b->length = filelength;
143
b->type = &apr_bucket_type_file;
144
b->free = apr_bucket_free;
146
APR_BUCKET_INSERT_AFTER(e, b);
149
file_bucket_destroy(a);
156
APU_DECLARE(apr_bucket *) apr_bucket_file_make(apr_bucket *b, apr_file_t *fd,
158
apr_size_t len, apr_pool_t *p)
162
f = apr_bucket_alloc(sizeof(*f), b->list);
169
b = apr_bucket_shared_make(b, f, offset, len);
170
b->type = &apr_bucket_type_file;
175
APU_DECLARE(apr_bucket *) apr_bucket_file_create(apr_file_t *fd,
177
apr_size_t len, apr_pool_t *p,
178
apr_bucket_alloc_t *list)
180
apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
183
b->free = apr_bucket_free;
185
return apr_bucket_file_make(b, fd, offset, len, p);
188
APU_DECLARE(apr_status_t) apr_bucket_file_enable_mmap(apr_bucket *e,
192
apr_bucket_file *a = e->data;
193
a->can_mmap = enabled;
197
#endif /* APR_HAS_MMAP */
201
static apr_status_t file_bucket_setaside(apr_bucket *data, apr_pool_t *reqpool)
203
apr_bucket_file *a = data->data;
204
apr_file_t *fd = NULL;
205
apr_file_t *f = a->fd;
206
apr_pool_t *curpool = apr_file_pool_get(f);
208
if (apr_pool_is_ancestor(curpool, reqpool)) {
212
if (!apr_pool_is_ancestor(a->readpool, reqpool)) {
213
a->readpool = reqpool;
216
apr_file_setaside(&fd, f, reqpool);
221
APU_DECLARE_DATA const apr_bucket_type_t apr_bucket_type_file = {
222
"FILE", 5, APR_BUCKET_DATA,
225
file_bucket_setaside,
226
apr_bucket_shared_split,
227
apr_bucket_shared_copy