forked from kaltura/nginx-vod-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx_file_reader.c
422 lines (344 loc) · 9.7 KB
/
ngx_file_reader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include "ngx_file_reader.h"
#include <ngx_event.h>
static ngx_int_t
ngx_file_reader_init_open_file_info(
ngx_open_file_info_t* of,
ngx_http_request_t *r,
ngx_http_core_loc_conf_t *clcf,
ngx_str_t* path)
{
ngx_int_t rc;
ngx_memzero(of, sizeof(ngx_open_file_info_t));
of->read_ahead = clcf->read_ahead;
of->directio = NGX_MAX_OFF_T_VALUE;
of->valid = clcf->open_file_cache_valid;
of->min_uses = clcf->open_file_cache_min_uses;
of->errors = clcf->open_file_cache_errors;
of->events = clcf->open_file_cache_events;
rc = ngx_http_set_disable_symlinks(r, clcf, path, of);
if (rc != NGX_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_file_reader_init_open_file_info: ngx_http_set_disable_symlinks failed %i", rc);
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
return rc;
}
static ngx_int_t
ngx_file_reader_update_state_file_info(ngx_file_reader_state_t* state, ngx_open_file_info_t* of, ngx_int_t rc)
{
ngx_uint_t level;
if (rc != NGX_OK)
{
switch (of->err)
{
case 0:
return NGX_HTTP_INTERNAL_SERVER_ERROR;
case NGX_ENOENT:
case NGX_ENOTDIR:
case NGX_ENAMETOOLONG:
level = NGX_LOG_ERR;
rc = NGX_HTTP_NOT_FOUND;
break;
case NGX_EACCES:
#if (NGX_HAVE_OPENAT)
case NGX_EMLINK:
case NGX_ELOOP:
#endif
level = NGX_LOG_ERR;
rc = NGX_HTTP_FORBIDDEN;
break;
default:
level = NGX_LOG_CRIT;
rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
break;
}
if (rc != NGX_HTTP_NOT_FOUND || state->log_not_found)
{
ngx_log_error(level, state->log, of->err, "ngx_file_reader_update_state_file_info: %s \"%s\" failed", of->failed, state->file.name.data);
}
return rc;
}
if (!of->is_file)
{
ngx_log_error(NGX_LOG_ERR, state->log, 0, "ngx_file_reader_update_state_file_info: \"%s\" is not a file", state->file.name.data);
if (of->fd != NGX_INVALID_FILE)
{
if (ngx_close_file(of->fd) == NGX_FILE_ERROR)
{
ngx_log_error(NGX_LOG_ALERT, state->log, ngx_errno, "ngx_file_reader_update_state_file_info: " ngx_close_file_n " \"%s\" failed", state->file.name.data);
}
}
return NGX_HTTP_FORBIDDEN;
}
state->file.fd = of->fd;
state->file_size = of->size;
return NGX_OK;
}
ngx_int_t
ngx_file_reader_init(
ngx_file_reader_state_t* state,
ngx_async_read_callback_t read_callback,
void* callback_context,
ngx_http_request_t *r,
ngx_http_core_loc_conf_t *clcf,
ngx_str_t* path)
{
ngx_open_file_info_t of;
ngx_int_t rc;
state->r = r;
state->file.name = *path;
state->file.log = r->connection->log;
state->directio = clcf->directio;
state->log_not_found = clcf->log_not_found;
state->log = r->connection->log;
#if (NGX_HAVE_FILE_AIO)
state->use_aio = clcf->aio;
state->read_callback = read_callback;
state->callback_context = callback_context;
#endif
rc = ngx_file_reader_init_open_file_info(&of, r, clcf, path);
if (rc != NGX_OK)
{
return rc;
}
rc = ngx_open_cached_file(clcf->open_file_cache, path, &of, r->pool);
return ngx_file_reader_update_state_file_info(state, &of, rc);
}
#if (NGX_THREADS)
typedef struct {
ngx_file_reader_state_t* state;
ngx_open_file_info_t of;
ngx_async_open_file_callback_t open_callback;
void* callback_context;
ngx_thread_task_t *task;
} ngx_file_reader_async_open_context_t;
static void
ngx_file_reader_async_open_callback(void* ctx, ngx_int_t rc)
{
ngx_file_reader_async_open_context_t* context = ctx;
ngx_file_reader_state_t* state = context->state;
ngx_http_request_t *r = state->r;
ngx_connection_t *c = r->connection;
r->main->blocked--;
r->aio = 0;
rc = ngx_file_reader_update_state_file_info(state, &context->of, rc);
context->open_callback(context->callback_context, rc);
ngx_http_run_posted_requests(c);
}
ngx_int_t
ngx_file_reader_init_async(
ngx_file_reader_state_t* state,
void** context,
ngx_thread_pool_t *thread_pool,
ngx_async_open_file_callback_t open_callback,
ngx_async_read_callback_t read_callback,
void* callback_context,
ngx_http_request_t *r,
ngx_http_core_loc_conf_t *clcf,
ngx_str_t* path)
{
ngx_file_reader_async_open_context_t* open_context;
ngx_int_t rc;
state->r = r;
state->file.name = *path;
state->file.log = r->connection->log;
state->directio = clcf->directio;
state->log_not_found = clcf->log_not_found;
state->log = r->connection->log;
#if (NGX_HAVE_FILE_AIO)
state->use_aio = clcf->aio;
state->read_callback = read_callback;
state->callback_context = callback_context;
#endif
open_context = *context;
if (open_context == NULL)
{
open_context = ngx_palloc(r->pool, sizeof(ngx_file_reader_async_open_context_t));
if (open_context == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, state->log, 0,
"ngx_file_reader_init_async: ngx_palloc failed");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
open_context->task = NULL; // all other fields explicitly set below
*context = open_context;
}
open_context->state = state;
open_context->open_callback = open_callback;
open_context->callback_context = callback_context;
rc = ngx_file_reader_init_open_file_info(&open_context->of, r, clcf, path);
if (rc != NGX_OK)
{
return rc;
}
rc = ngx_async_open_cached_file(
clcf->open_file_cache,
path,
&open_context->of,
r->pool,
thread_pool,
&open_context->task,
ngx_file_reader_async_open_callback,
open_context);
if (rc == NGX_AGAIN)
{
r->main->blocked++;
r->aio = 1;
return NGX_AGAIN;
}
return ngx_file_reader_update_state_file_info(state, &open_context->of, rc);
}
#endif // NGX_THREADS
ngx_int_t
ngx_file_reader_dump_file_part(ngx_file_reader_state_t* state, off_t start, off_t end)
{
ngx_http_request_t* r = state->r;
ngx_buf_t *b;
ngx_int_t rc;
ngx_chain_t out;
b = ngx_calloc_buf(r->pool);
if (b == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, state->log, 0,
"ngx_file_reader_dump_file_part: ngx_pcalloc failed (1)");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
if (b->file == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, state->log, 0,
"ngx_file_reader_dump_file_part: ngx_pcalloc failed (2)");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
b->file_pos = start;
if (end != 0)
{
if (end > state->file_size)
{
ngx_log_error(NGX_LOG_ERR, state->log, ngx_errno,
"ngx_file_reader_dump_file_part: end offset %O exceeds file size %O, probably a truncated file", end, state->file_size);
return NGX_HTTP_NOT_FOUND;
}
b->file_last = end;
}
else
{
b->file_last = state->file_size;
}
b->in_file = b->file_last ? 1 : 0;
b->last_buf = (r == r->main) ? 1 : 0;
b->last_in_chain = 1;
b->file->fd = state->file.fd;
b->file->name = state->file.name;
b->file->log = state->log;
b->file->directio = state->file.directio;
out.buf = b;
out.next = NULL;
rc = ngx_http_output_filter(r, &out);
if (rc != NGX_OK && rc != NGX_AGAIN)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, state->log, 0,
"ngx_file_reader_dump_file_part: ngx_http_output_filter failed %i", rc);
return rc;
}
return NGX_OK;
}
ngx_int_t
ngx_file_reader_enable_directio(ngx_file_reader_state_t* state)
{
if (state->directio <= state->file_size)
{
if (ngx_directio_on(state->file.fd) == NGX_FILE_ERROR)
{
ngx_log_error(NGX_LOG_ALERT, state->log, ngx_errno,
"ngx_file_reader_enable_directio: " ngx_directio_on_n " \"%s\" failed", state->file.name.data);
return NGX_FILE_ERROR;
}
state->file.directio = 1;
}
return NGX_OK;
}
#if (NGX_HAVE_FILE_AIO)
static void
ngx_async_read_completed_callback(ngx_event_t *ev)
{
ngx_file_reader_state_t* state;
ngx_http_request_t *r;
ngx_connection_t *c;
ngx_event_aio_t *aio;
ssize_t bytes_read;
ssize_t rc;
aio = ev->data;
state = aio->data;
r = state->r;
c = r->connection;
r->main->blocked--;
r->aio = 0;
// get the number of bytes read (offset, size, buffer are ignored in this case)
rc = ngx_file_aio_read(&state->file, NULL, 0, 0, r->pool);
if (rc < 0)
{
ngx_log_error(NGX_LOG_ERR, state->log, 0,
"ngx_async_read_completed_callback: ngx_file_aio_read failed rc=%z", rc);
bytes_read = 0;
}
else
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, state->log, 0, "ngx_async_read_completed_callback: ngx_file_aio_read returned %z", rc);
state->buf->last += rc;
bytes_read = rc;
rc = NGX_OK;
}
state->read_callback(state->callback_context, rc, NULL, bytes_read);
ngx_http_run_posted_requests(c);
}
ngx_int_t
ngx_async_file_read(ngx_file_reader_state_t* state, ngx_buf_t *buf, size_t size, off_t offset)
{
ssize_t rc;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, state->log, 0, "ngx_async_file_read: reading offset %O size %uz", offset, size);
if (state->use_aio)
{
rc = ngx_file_aio_read(&state->file, buf->last, size, offset, state->r->pool);
if (rc == NGX_AGAIN)
{
// wait for completion
state->file.aio->data = state;
state->file.aio->handler = ngx_async_read_completed_callback;
state->r->main->blocked++;
state->r->aio = 1;
state->buf = buf;
return rc;
}
}
else
{
rc = ngx_read_file(&state->file, buf->last, size, offset);
}
if (rc < 0)
{
ngx_log_error(NGX_LOG_ERR, state->log, 0, "ngx_async_file_read: ngx_file_aio_read failed rc=%z", rc);
return rc;
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, state->log, 0, "ngx_async_file_read: ngx_file_aio_read returned %z", rc);
buf->last += rc;
return NGX_OK;
}
#else
ngx_int_t
ngx_async_file_read(ngx_file_reader_state_t* state, ngx_buf_t *buf, size_t size, off_t offset)
{
ssize_t rc;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, state->log, 0, "ngx_async_file_read: reading offset %O size %uz", offset, size);
rc = ngx_read_file(&state->file, buf->last, size, offset);
if (rc < 0)
{
ngx_log_error(NGX_LOG_ERR, state->log, 0, "ngx_async_file_read: ngx_read_file failed rc=%z", rc);
return rc;
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, state->log, 0, "ngx_async_file_read: ngx_read_file returned %z", rc);
buf->last += rc;
return NGX_OK;
}
#endif