Coverage Report

Created: 2026-12-01 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/httpd/srclib/apr/buckets/apr_buckets_file.c
Line
Count
Source
1
/* Licensed to the Apache Software Foundation (ASF) under one or more
2
 * contributor license agreements.  See the NOTICE file distributed with
3
 * this work for additional information regarding copyright ownership.
4
 * The ASF licenses this file to You under the Apache License, Version 2.0
5
 * (the "License"); you may not use this file except in compliance with
6
 * the License.  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
17
#include "apr.h"
18
#include "apr_general.h"
19
#include "apr_file_io.h"
20
#include "apr_buckets.h"
21
22
#if APR_HAVE_UNISTD_H
23
#include <unistd.h> /* for sysconf() */
24
#endif
25
26
#if APR_HAS_MMAP
27
#include "apr_mmap.h"
28
29
/* mmap support for static files based on ideas from John Heidemann's
30
 * patch against 1.0.5.  See
31
 * <http://www.isi.edu/~johnh/SOFTWARE/APACHE/index.html>.
32
 */
33
34
#endif /* APR_HAS_MMAP */
35
36
static void file_bucket_destroy(void *data)
37
0
{
38
0
    apr_bucket_file *f = data;
39
40
0
    if (apr_bucket_shared_destroy(f)) {
41
        /* no need to close the file here; it will get
42
         * done automatically when the pool gets cleaned up */
43
0
        apr_bucket_free(f);
44
0
    }
45
0
}
46
47
#if APR_HAS_MMAP
48
static int file_make_mmap(apr_bucket *e, apr_size_t filelength,
49
                           apr_off_t fileoffset, apr_pool_t *p)
50
0
{
51
0
    apr_bucket_file *a = e->data;
52
0
    apr_mmap_t *mm;
53
54
0
    if (!a->can_mmap) {
55
0
        return 0;
56
0
    }
57
58
0
    if (filelength > APR_MMAP_LIMIT) {
59
0
        if (apr_mmap_create(&mm, a->fd, fileoffset, APR_MMAP_LIMIT,
60
0
                            APR_MMAP_READ, p) != APR_SUCCESS)
61
0
        {
62
0
            return 0;
63
0
        }
64
0
        apr_bucket_split(e, APR_MMAP_LIMIT);
65
0
        filelength = APR_MMAP_LIMIT;
66
0
    }
67
0
    else if ((filelength < APR_MMAP_THRESHOLD) ||
68
0
             (apr_mmap_create(&mm, a->fd, fileoffset, filelength,
69
0
                              APR_MMAP_READ, p) != APR_SUCCESS))
70
0
    {
71
0
        return 0;
72
0
    }
73
0
    apr_bucket_mmap_make(e, mm, 0, filelength);
74
0
    file_bucket_destroy(a);
75
0
    return 1;
76
0
}
77
78
/* Returns the number of bytes to read from the bucket to reach the
79
 * next page boundary in the file, or zero if the bucket should not be
80
 * split at a page boundary.  A bucket which does not start on a page
81
 * boundary, and which cannot be read in a single read regardless, is
82
 * read up to the next page boundary so that the remainder of the
83
 * bucket can be mapped from a page-aligned offset, provided that
84
 * remainder is large enough to be mapped. */
85
static apr_size_t file_align_length(apr_bucket_file *a, apr_size_t filelength,
86
                                    apr_off_t fileoffset)
87
0
{
88
0
#ifdef _SC_PAGESIZE
89
0
    long psize;
90
91
0
    if (!a->can_mmap || filelength <= a->read_size) {
92
0
        return 0;
93
0
    }
94
95
0
    psize = sysconf(_SC_PAGESIZE);
96
0
    if (psize > 0 && (fileoffset & (apr_off_t)(psize - 1)) != 0) {
97
0
        apr_size_t align = (apr_size_t)(psize
98
0
                                        - (fileoffset & (apr_off_t)(psize - 1)));
99
100
        /* Only worthwhile if enough of the bucket remains beyond the
101
         * page boundary to be mapped. */
102
0
        if (align < filelength
103
0
            && filelength - align >= APR_MMAP_THRESHOLD) {
104
0
            return align;
105
0
        }
106
0
    }
107
0
#endif
108
0
    return 0;
109
0
}
110
#endif
111
112
static apr_status_t file_bucket_read(apr_bucket *e, const char **str,
113
                                     apr_size_t *len, apr_read_type_e block)
114
0
{
115
0
    apr_bucket_file *a = e->data;
116
0
    apr_file_t *f = a->fd;
117
0
    apr_bucket *b = NULL;
118
0
    char *buf;
119
0
    apr_status_t rv;
120
0
    apr_size_t filelength = e->length;  /* bytes remaining in file past offset */
121
0
    apr_off_t fileoffset = e->start;
122
0
#if APR_HAS_THREADS && !APR_HAS_XTHREAD_FILES
123
0
    apr_int32_t flags;
124
0
#endif
125
126
0
#if APR_HAS_MMAP
127
0
    apr_size_t alignlength = file_align_length(a, filelength, fileoffset);
128
129
0
    if (!alignlength
130
0
        && file_make_mmap(e, filelength, fileoffset, a->readpool)) {
131
0
        return apr_bucket_read(e, str, len, block);
132
0
    }
133
0
#endif
134
135
0
#if APR_HAS_THREADS && !APR_HAS_XTHREAD_FILES
136
0
    if ((flags = apr_file_flags_get(f)) & APR_FOPEN_XTHREAD) {
137
        /* this file descriptor is shared across multiple threads and
138
         * this OS doesn't support that natively, so as a workaround
139
         * we must reopen the file into a->readpool */
140
0
        const char *fname;
141
0
        apr_file_name_get(&fname, f);
142
143
0
        rv = apr_file_open(&f, fname, (flags & ~APR_FOPEN_XTHREAD), 0, a->readpool);
144
0
        if (rv != APR_SUCCESS)
145
0
            return rv;
146
147
0
        a->fd = f;
148
0
    }
149
0
#endif
150
151
0
    *str = NULL;  /* in case we die prematurely */
152
0
    *len = (filelength > a->read_size) ? a->read_size : filelength;
153
0
#if APR_HAS_MMAP
154
0
    if (alignlength && alignlength < *len) {
155
0
        *len = alignlength;
156
0
    }
157
0
#endif
158
0
    buf = apr_bucket_alloc(*len, e->list);
159
160
    /* Handle offset ... */
161
0
    rv = apr_file_seek(f, APR_SET, &fileoffset);
162
0
    if (rv != APR_SUCCESS) {
163
0
        apr_bucket_free(buf);
164
0
        return rv;
165
0
    }
166
0
    rv = apr_file_read(f, buf, len);
167
0
    if (rv != APR_SUCCESS && rv != APR_EOF) {
168
0
        apr_bucket_free(buf);
169
0
        return rv;
170
0
    }
171
0
    filelength -= *len;
172
    /*
173
     * Change the current bucket to refer to what we read,
174
     * even if we read nothing because we hit EOF.
175
     */
176
0
    apr_bucket_heap_make(e, buf, *len, apr_bucket_free);
177
178
    /* If we have more to read from the file, then create another bucket */
179
0
    if (filelength > 0 && rv != APR_EOF) {
180
        /* for efficiency, we can just build a new apr_bucket struct
181
         * to wrap around the existing file bucket */
182
0
        b = apr_bucket_alloc(sizeof(*b), e->list);
183
0
        b->start  = fileoffset + (*len);
184
0
        b->length = filelength;
185
0
        b->data   = a;
186
0
        b->type   = &apr_bucket_type_file;
187
0
        b->free   = apr_bucket_free;
188
0
        b->list   = e->list;
189
0
        APR_BUCKET_INSERT_AFTER(e, b);
190
0
    }
191
0
    else {
192
0
        file_bucket_destroy(a);
193
0
    }
194
195
0
    *str = buf;
196
0
    return rv;
197
0
}
198
199
APR_DECLARE(apr_bucket *) apr_bucket_file_make(apr_bucket *b, apr_file_t *fd,
200
                                               apr_off_t offset,
201
                                               apr_size_t len, apr_pool_t *p)
202
0
{
203
0
    apr_bucket_file *f;
204
205
0
    f = apr_bucket_alloc(sizeof(*f), b->list);
206
0
    f->fd = fd;
207
0
    f->readpool = p;
208
0
#if APR_HAS_MMAP
209
0
    f->can_mmap = 1;
210
0
#endif
211
0
    f->read_size = APR_BUCKET_BUFF_SIZE;
212
213
0
    b = apr_bucket_shared_make(b, f, offset, len);
214
0
    b->type = &apr_bucket_type_file;
215
216
0
    return b;
217
0
}
218
219
APR_DECLARE(apr_bucket *) apr_bucket_file_create(apr_file_t *fd,
220
                                                 apr_off_t offset,
221
                                                 apr_size_t len, apr_pool_t *p,
222
                                                 apr_bucket_alloc_t *list)
223
0
{
224
0
    apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
225
226
0
    APR_BUCKET_INIT(b);
227
0
    b->free = apr_bucket_free;
228
0
    b->list = list;
229
0
    return apr_bucket_file_make(b, fd, offset, len, p);
230
0
}
231
232
APR_DECLARE(apr_status_t) apr_bucket_file_enable_mmap(apr_bucket *e,
233
                                                      int enabled)
234
0
{
235
0
#if APR_HAS_MMAP
236
0
    apr_bucket_file *a = e->data;
237
0
    a->can_mmap = enabled;
238
0
    return APR_SUCCESS;
239
#else
240
    return APR_ENOTIMPL;
241
#endif /* APR_HAS_MMAP */
242
0
}
243
244
APR_DECLARE(apr_status_t) apr_bucket_file_set_buf_size(apr_bucket *e,
245
                                                       apr_size_t size)
246
0
{
247
0
    apr_bucket_file *a = e->data;
248
249
0
    if (size <= APR_BUCKET_BUFF_SIZE) {
250
0
        a->read_size = APR_BUCKET_BUFF_SIZE;
251
0
    }
252
0
    else {
253
0
        apr_size_t floor = apr_bucket_alloc_aligned_floor(e->list, size);
254
0
        a->read_size = (size < floor) ? size : floor;
255
0
    }
256
257
0
    return APR_SUCCESS;
258
0
}
259
260
static apr_status_t file_bucket_setaside(apr_bucket *b, apr_pool_t *reqpool)
261
0
{
262
0
    apr_bucket_file *a = b->data;
263
0
    apr_file_t *fd = NULL;
264
0
    apr_file_t *f = a->fd;
265
0
    apr_pool_t *curpool = apr_file_pool_get(f);
266
267
0
    if (apr_pool_is_ancestor(curpool, reqpool)) {
268
0
        return APR_SUCCESS;
269
0
    }
270
271
    /* If the file is shared/split accross multiple buckets, this bucket can't
272
     * take exclusive ownership with apr_file_setaside() (thus invalidating the
273
     * f->filedes), let's apr_file_dup() in this case instead.
274
     */
275
0
    if (a->refcount.refcount > 1) {
276
0
        apr_bucket_file *new;
277
0
        apr_status_t rv;
278
279
0
        rv = apr_file_dup(&fd, f, reqpool);
280
0
        if (rv != APR_SUCCESS) {
281
0
            return rv;
282
0
        }
283
284
0
        new = apr_bucket_alloc(sizeof(*new), b->list);
285
0
        memcpy(new, a, sizeof(*new));
286
0
        new->refcount.refcount = 1;
287
288
0
        a->refcount.refcount--;
289
0
        a = b->data = new;
290
0
    }
291
0
    else {
292
0
        apr_file_setaside(&fd, f, reqpool);
293
0
    }
294
0
    a->fd = fd;
295
0
    a->readpool = reqpool;
296
0
    return APR_SUCCESS;
297
0
}
298
299
APR_DECLARE_DATA const apr_bucket_type_t apr_bucket_type_file = {
300
    "FILE", 5, APR_BUCKET_DATA,
301
    file_bucket_destroy,
302
    file_bucket_read,
303
    file_bucket_setaside,
304
    apr_bucket_shared_split,
305
    apr_bucket_shared_copy
306
};