Mercurial > hg > nginx
annotate src/core/ngx_open_file_cache.c @ 1936:e829527ca810
axe useless code
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 13 Mar 2008 12:34:59 +0000 |
parents | 108576aef610 |
children | c46432b9dbb7 |
rev | line source |
---|---|
1453 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_event.h> | |
10 | |
11 | |
12 /* | |
13 * open file cache caches | |
14 * open file handles with stat() info; | |
15 * directories stat() info; | |
16 * files and directories errors: not found, access denied, etc. | |
17 */ | |
18 | |
19 | |
20 static void ngx_open_file_cache_cleanup(void *data); | |
1775 | 21 static ngx_int_t ngx_open_and_stat_file(u_char *name, ngx_open_file_info_t *of, |
22 ngx_log_t *log); | |
23 static void ngx_open_file_add_event(ngx_open_file_cache_t *cache, | |
24 ngx_cached_open_file_t *file, ngx_open_file_info_t *of, ngx_log_t *log); | |
1453 | 25 static void ngx_open_file_cleanup(void *data); |
26 static void ngx_close_cached_file(ngx_open_file_cache_t *cache, | |
1772 | 27 ngx_cached_open_file_t *file, ngx_uint_t min_uses, ngx_log_t *log); |
1775 | 28 static void ngx_open_file_del_event(ngx_cached_open_file_t *file); |
1453 | 29 static void ngx_expire_old_cached_files(ngx_open_file_cache_t *cache, |
30 ngx_uint_t n, ngx_log_t *log); | |
31 static void ngx_open_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp, | |
32 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel); | |
1775 | 33 static ngx_cached_open_file_t * |
34 ngx_open_file_lookup(ngx_open_file_cache_t *cache, ngx_str_t *name, | |
35 uint32_t hash); | |
1453 | 36 static void ngx_open_file_cache_remove(ngx_event_t *ev); |
37 | |
38 | |
39 ngx_open_file_cache_t * | |
40 ngx_open_file_cache_init(ngx_pool_t *pool, ngx_uint_t max, time_t inactive) | |
41 { | |
42 ngx_pool_cleanup_t *cln; | |
43 ngx_open_file_cache_t *cache; | |
44 | |
45 cache = ngx_palloc(pool, sizeof(ngx_open_file_cache_t)); | |
46 if (cache == NULL) { | |
47 return NULL; | |
48 } | |
49 | |
1761 | 50 ngx_rbtree_init(&cache->rbtree, &cache->sentinel, |
1743
4fc402c3ec73
optimize rbtree initialization and insert
Igor Sysoev <igor@sysoev.ru>
parents:
1458
diff
changeset
|
51 ngx_open_file_cache_rbtree_insert_value); |
1453 | 52 |
1765 | 53 ngx_queue_init(&cache->expire_queue); |
54 | |
1453 | 55 cache->current = 0; |
56 cache->max = max; | |
57 cache->inactive = inactive; | |
58 | |
59 cln = ngx_pool_cleanup_add(pool, 0); | |
60 if (cln == NULL) { | |
61 return NULL; | |
62 } | |
63 | |
64 cln->handler = ngx_open_file_cache_cleanup; | |
65 cln->data = cache; | |
66 | |
67 return cache; | |
68 } | |
69 | |
70 | |
71 static void | |
72 ngx_open_file_cache_cleanup(void *data) | |
73 { | |
74 ngx_open_file_cache_t *cache = data; | |
75 | |
1765 | 76 ngx_queue_t *q; |
1453 | 77 ngx_cached_open_file_t *file; |
78 | |
79 ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, | |
80 "open file cache cleanup"); | |
81 | |
82 for ( ;; ) { | |
83 | |
1765 | 84 if (ngx_queue_empty(&cache->expire_queue)) { |
1766 | 85 break; |
1453 | 86 } |
87 | |
1765 | 88 q = ngx_queue_last(&cache->expire_queue); |
89 | |
90 file = ngx_queue_data(q, ngx_cached_open_file_t, queue); | |
91 | |
92 ngx_queue_remove(q); | |
1453 | 93 |
94 ngx_rbtree_delete(&cache->rbtree, &file->node); | |
95 | |
96 cache->current--; | |
97 | |
98 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, | |
99 "delete cached open file: %s", file->name); | |
100 | |
101 if (!file->err && !file->is_dir) { | |
102 file->close = 1; | |
103 file->count = 0; | |
1772 | 104 ngx_close_cached_file(cache, file, 0, ngx_cycle->log); |
1453 | 105 |
106 } else { | |
107 ngx_free(file->name); | |
108 ngx_free(file); | |
109 } | |
110 } | |
111 | |
112 if (cache->current) { | |
113 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0, | |
114 "%d items still leave in open file cache", | |
115 cache->current); | |
116 } | |
117 | |
118 if (cache->rbtree.root != cache->rbtree.sentinel) { | |
119 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0, | |
120 "rbtree still is not empty in open file cache"); | |
121 | |
122 } | |
123 } | |
124 | |
125 | |
126 ngx_int_t | |
127 ngx_open_cached_file(ngx_open_file_cache_t *cache, ngx_str_t *name, | |
128 ngx_open_file_info_t *of, ngx_pool_t *pool) | |
129 { | |
130 time_t now; | |
131 uint32_t hash; | |
132 ngx_int_t rc; | |
133 ngx_pool_cleanup_t *cln; | |
134 ngx_cached_open_file_t *file; | |
135 ngx_pool_cleanup_file_t *clnf; | |
136 ngx_open_file_cache_cleanup_t *ofcln; | |
137 | |
138 of->err = 0; | |
139 | |
140 if (cache == NULL) { | |
141 | |
142 cln = ngx_pool_cleanup_add(pool, sizeof(ngx_pool_cleanup_file_t)); | |
143 if (cln == NULL) { | |
144 return NGX_ERROR; | |
145 } | |
146 | |
147 rc = ngx_open_and_stat_file(name->data, of, pool->log); | |
148 | |
149 if (rc == NGX_OK && !of->is_dir) { | |
150 cln->handler = ngx_pool_cleanup_file; | |
151 clnf = cln->data; | |
152 | |
153 clnf->fd = of->fd; | |
154 clnf->name = name->data; | |
155 clnf->log = pool->log; | |
156 } | |
157 | |
158 return rc; | |
159 } | |
160 | |
161 cln = ngx_pool_cleanup_add(pool, sizeof(ngx_open_file_cache_cleanup_t)); | |
162 if (cln == NULL) { | |
163 return NGX_ERROR; | |
164 } | |
165 | |
166 now = ngx_time(); | |
167 | |
1775 | 168 hash = ngx_crc32_long(name->data, name->len); |
169 | |
170 file = ngx_open_file_lookup(cache, name, hash); | |
171 | |
172 if (file) { | |
173 | |
174 file->uses++; | |
175 | |
176 ngx_queue_remove(&file->queue); | |
1453 | 177 |
1775 | 178 if (file->fd == NGX_INVALID_FILE && file->err == 0 && !file->is_dir) { |
179 | |
180 /* file was not used often enough to keep open */ | |
181 | |
182 rc = ngx_open_and_stat_file(name->data, of, pool->log); | |
1453 | 183 |
1775 | 184 if (rc != NGX_OK && (of->err == 0 || !of->errors)) { |
185 goto failed; | |
186 } | |
187 | |
188 goto add_event; | |
1453 | 189 } |
190 | |
1775 | 191 if ((file->event && file->use_event) |
192 || (file->event == NULL && now - file->created < of->valid)) | |
193 { | |
194 if (file->err == 0) { | |
1772 | 195 |
1775 | 196 of->fd = file->fd; |
197 of->uniq = file->uniq; | |
198 of->mtime = file->mtime; | |
199 of->size = file->size; | |
1453 | 200 |
1775 | 201 of->is_dir = file->is_dir; |
202 of->is_file = file->is_file; | |
203 of->is_link = file->is_link; | |
204 of->is_exec = file->is_exec; | |
1453 | 205 |
1775 | 206 if (!file->is_dir) { |
207 file->count++; | |
208 ngx_open_file_add_event(cache, file, of, pool->log); | |
1453 | 209 } |
210 | |
1775 | 211 } else { |
212 of->err = file->err; | |
213 } | |
214 | |
215 goto found; | |
216 } | |
217 | |
218 ngx_log_debug4(NGX_LOG_DEBUG_CORE, pool->log, 0, | |
219 "retest open file: %s, fd:%d, c:%d, e:%d", | |
220 file->name, file->fd, file->count, file->err); | |
1453 | 221 |
1775 | 222 if (file->is_dir) { |
223 | |
224 /* | |
225 * chances that directory became file are very small | |
226 * so test_dir flag allows to use a single syscall | |
227 * in ngx_file_info() instead of three syscalls | |
228 */ | |
229 | |
230 of->test_dir = 1; | |
231 } | |
232 | |
233 rc = ngx_open_and_stat_file(name->data, of, pool->log); | |
1453 | 234 |
1775 | 235 if (rc != NGX_OK && (of->err == 0 || !of->errors)) { |
236 goto failed; | |
237 } | |
238 | |
239 if (of->is_dir) { | |
240 | |
241 if (file->is_dir || file->err) { | |
242 goto update; | |
243 } | |
244 | |
245 /* file became directory */ | |
1453 | 246 |
1775 | 247 } else if (of->err == 0) { /* file */ |
248 | |
249 if (file->is_dir || file->err) { | |
250 goto add_event; | |
251 } | |
1453 | 252 |
1775 | 253 if (of->uniq == file->uniq |
254 && of->mtime == file->mtime | |
255 && of->size == file->size) | |
256 { | |
257 if (ngx_close_file(of->fd) == NGX_FILE_ERROR) { | |
258 ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno, | |
259 ngx_close_file_n " \"%s\" failed", | |
260 name->data); | |
1453 | 261 } |
262 | |
1775 | 263 of->fd = file->fd; |
264 file->count++; | |
1453 | 265 |
1775 | 266 if (file->event) { |
267 file->use_event = 1; | |
268 goto renew; | |
1453 | 269 } |
270 | |
1775 | 271 ngx_open_file_add_event(cache, file, of, pool->log); |
272 | |
273 goto renew; | |
274 } | |
1453 | 275 |
1775 | 276 /* file was changed */ |
1453 | 277 |
1775 | 278 } else { /* error to cache */ |
1453 | 279 |
1775 | 280 if (file->err || file->is_dir) { |
281 goto update; | |
1453 | 282 } |
283 | |
1775 | 284 /* file was removed, etc. */ |
285 } | |
286 | |
287 if (file->count == 0) { | |
288 | |
289 ngx_open_file_del_event(file); | |
1453 | 290 |
1775 | 291 if (ngx_close_file(file->fd) == NGX_FILE_ERROR) { |
292 ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno, | |
293 ngx_close_file_n " \"%s\" failed", | |
294 name->data); | |
295 } | |
1453 | 296 |
1775 | 297 goto add_event; |
298 } | |
299 | |
300 ngx_rbtree_delete(&cache->rbtree, &file->node); | |
301 | |
302 cache->current--; | |
303 | |
304 file->close = 1; | |
305 | |
306 goto create; | |
1453 | 307 } |
308 | |
309 /* not found */ | |
310 | |
311 rc = ngx_open_and_stat_file(name->data, of, pool->log); | |
312 | |
313 if (rc != NGX_OK && (of->err == 0 || !of->errors)) { | |
314 goto failed; | |
315 } | |
316 | |
317 create: | |
318 | |
319 if (cache->current >= cache->max) { | |
320 ngx_expire_old_cached_files(cache, 0, pool->log); | |
321 } | |
322 | |
323 file = ngx_alloc(sizeof(ngx_cached_open_file_t), pool->log); | |
324 | |
325 if (file == NULL) { | |
326 goto failed; | |
327 } | |
328 | |
329 file->name = ngx_alloc(name->len + 1, pool->log); | |
330 | |
331 if (file->name == NULL) { | |
332 ngx_free(file); | |
333 file = NULL; | |
334 goto failed; | |
335 } | |
336 | |
337 ngx_cpystrn(file->name, name->data, name->len + 1); | |
338 | |
339 file->node.key = hash; | |
340 | |
341 ngx_rbtree_insert(&cache->rbtree, &file->node); | |
342 | |
343 cache->current++; | |
344 | |
1775 | 345 file->uses = 1; |
1453 | 346 file->count = 0; |
1775 | 347 file->use_event = 0; |
348 file->event = NULL; | |
349 | |
350 add_event: | |
351 | |
352 ngx_open_file_add_event(cache, file, of, pool->log); | |
1453 | 353 |
354 update: | |
355 | |
356 file->fd = of->fd; | |
357 file->err = of->err; | |
358 | |
359 if (of->err == 0) { | |
360 file->uniq = of->uniq; | |
361 file->mtime = of->mtime; | |
362 file->size = of->size; | |
363 | |
364 file->close = 0; | |
365 | |
366 file->is_dir = of->is_dir; | |
367 file->is_file = of->is_file; | |
368 file->is_link = of->is_link; | |
369 file->is_exec = of->is_exec; | |
370 | |
371 if (!of->is_dir) { | |
372 file->count++; | |
373 } | |
374 } | |
375 | |
376 renew: | |
377 | |
378 file->created = now; | |
379 | |
380 found: | |
381 | |
382 file->accessed = now; | |
383 | |
1765 | 384 ngx_queue_insert_head(&cache->expire_queue, &file->queue); |
1453 | 385 |
1772 | 386 ngx_log_debug5(NGX_LOG_DEBUG_CORE, pool->log, 0, |
387 "cached open file: %s, fd:%d, c:%d, e:%d, u:%d", | |
388 file->name, file->fd, file->count, file->err, file->uses); | |
1453 | 389 |
390 if (of->err == 0) { | |
391 | |
392 if (!of->is_dir) { | |
393 cln->handler = ngx_open_file_cleanup; | |
394 ofcln = cln->data; | |
395 | |
396 ofcln->cache = cache; | |
397 ofcln->file = file; | |
1772 | 398 ofcln->min_uses = of->min_uses; |
1453 | 399 ofcln->log = pool->log; |
400 } | |
401 | |
402 return NGX_OK; | |
403 } | |
404 | |
405 return NGX_ERROR; | |
406 | |
407 failed: | |
408 | |
409 if (file && file->count == 0) { | |
410 ngx_rbtree_delete(&cache->rbtree, &file->node); | |
411 | |
412 cache->current--; | |
413 | |
414 if (ngx_close_file(file->fd) == NGX_FILE_ERROR) { | |
415 ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno, | |
416 ngx_close_file_n " \"%s\" failed", file->name); | |
417 } | |
418 | |
419 ngx_free(file->name); | |
420 ngx_free(file); | |
421 } | |
422 | |
423 if (of->fd != NGX_INVALID_FILE) { | |
424 if (ngx_close_file(of->fd) == NGX_FILE_ERROR) { | |
425 ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno, | |
426 ngx_close_file_n " \"%s\" failed", name->data); | |
427 } | |
428 } | |
429 | |
430 return NGX_ERROR; | |
431 } | |
432 | |
433 | |
434 static ngx_int_t | |
435 ngx_open_and_stat_file(u_char *name, ngx_open_file_info_t *of, ngx_log_t *log) | |
436 { | |
437 ngx_fd_t fd; | |
438 ngx_file_info_t fi; | |
439 | |
440 of->fd = NGX_INVALID_FILE; | |
441 | |
442 if (of->test_dir) { | |
443 | |
444 if (ngx_file_info(name, &fi) == -1) { | |
445 of->err = ngx_errno; | |
446 | |
447 return NGX_ERROR; | |
448 } | |
449 | |
450 of->uniq = ngx_file_uniq(&fi); | |
451 of->mtime = ngx_file_mtime(&fi); | |
452 of->size = ngx_file_size(&fi); | |
453 of->is_dir = ngx_is_dir(&fi); | |
454 of->is_file = ngx_is_file(&fi); | |
455 of->is_link = ngx_is_link(&fi); | |
456 of->is_exec = ngx_is_exec(&fi); | |
457 | |
458 if (of->is_dir) { | |
459 return NGX_OK; | |
460 } | |
461 } | |
462 | |
463 fd = ngx_open_file(name, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0); | |
464 | |
465 if (fd == NGX_INVALID_FILE) { | |
466 of->err = ngx_errno; | |
467 return NGX_ERROR; | |
468 } | |
469 | |
470 if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) { | |
471 ngx_log_error(NGX_LOG_CRIT, log, ngx_errno, | |
472 ngx_fd_info_n " \"%s\" failed", name); | |
473 | |
474 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
475 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | |
476 ngx_close_file_n " \"%s\" failed", name); | |
477 } | |
478 | |
479 return NGX_ERROR; | |
480 } | |
481 | |
482 if (ngx_is_dir(&fi)) { | |
483 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
484 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | |
485 ngx_close_file_n " \"%s\" failed", name); | |
486 } | |
487 | |
488 fd = NGX_INVALID_FILE; | |
489 } | |
490 | |
491 of->fd = fd; | |
492 of->uniq = ngx_file_uniq(&fi); | |
493 of->mtime = ngx_file_mtime(&fi); | |
494 of->size = ngx_file_size(&fi); | |
495 of->is_dir = ngx_is_dir(&fi); | |
496 of->is_file = ngx_is_file(&fi); | |
497 of->is_link = ngx_is_link(&fi); | |
498 of->is_exec = ngx_is_exec(&fi); | |
499 | |
500 return NGX_OK; | |
501 } | |
502 | |
503 | |
1775 | 504 /* |
505 * we ignore any possible event setting error and | |
506 * fallback to usual periodic file retests | |
507 */ | |
508 | |
509 static void | |
510 ngx_open_file_add_event(ngx_open_file_cache_t *cache, | |
511 ngx_cached_open_file_t *file, ngx_open_file_info_t *of, ngx_log_t *log) | |
512 { | |
513 ngx_open_file_cache_event_t *fev; | |
514 | |
515 if (!(ngx_event_flags & NGX_USE_VNODE_EVENT) | |
516 || !of->events | |
517 || file->event | |
518 || of->fd == NGX_INVALID_FILE | |
519 || file->uses < of->min_uses) | |
520 { | |
521 return; | |
522 } | |
523 | |
524 file->event = ngx_calloc(sizeof(ngx_event_t), log); | |
525 if (file->event== NULL) { | |
526 return; | |
527 } | |
528 | |
529 fev = ngx_alloc(sizeof(ngx_open_file_cache_event_t), log); | |
530 if (fev == NULL) { | |
531 ngx_free(file->event); | |
532 file->event = NULL; | |
533 return; | |
534 } | |
535 | |
536 fev->fd = of->fd; | |
537 fev->file = file; | |
538 fev->cache = cache; | |
539 | |
540 file->event->handler = ngx_open_file_cache_remove; | |
541 file->event->data = fev; | |
542 | |
543 /* | |
544 * although vnode event may be called while ngx_cycle->poll | |
545 * destruction, however, cleanup procedures are run before any | |
546 * memory freeing and events will be canceled. | |
547 */ | |
548 | |
549 file->event->log = ngx_cycle->log; | |
550 | |
551 if (ngx_add_event(file->event, NGX_VNODE_EVENT, NGX_ONESHOT_EVENT) | |
552 != NGX_OK) | |
553 { | |
554 ngx_free(file->event->data); | |
555 ngx_free(file->event); | |
556 file->event = NULL; | |
557 return; | |
558 } | |
559 | |
560 /* | |
561 * we do not file->use_event here because there may be a race | |
562 * condition between opening file and adding event, so we rely | |
563 * upon event notification only after first file revalidation | |
564 */ | |
565 | |
566 return; | |
567 } | |
568 | |
569 | |
1453 | 570 static void |
571 ngx_open_file_cleanup(void *data) | |
572 { | |
573 ngx_open_file_cache_cleanup_t *c = data; | |
574 | |
575 c->file->count--; | |
576 | |
1772 | 577 ngx_close_cached_file(c->cache, c->file, c->min_uses, c->log); |
1453 | 578 |
579 /* drop one or two expired open files */ | |
580 ngx_expire_old_cached_files(c->cache, 1, c->log); | |
581 } | |
582 | |
583 | |
584 static void | |
585 ngx_close_cached_file(ngx_open_file_cache_t *cache, | |
1772 | 586 ngx_cached_open_file_t *file, ngx_uint_t min_uses, ngx_log_t *log) |
1453 | 587 { |
1772 | 588 ngx_log_debug5(NGX_LOG_DEBUG_CORE, log, 0, |
589 "close cached open file: %s, fd:%d, c:%d, u:%d, %d", | |
590 file->name, file->fd, file->count, file->uses, file->close); | |
1453 | 591 |
592 if (!file->close) { | |
593 | |
594 file->accessed = ngx_time(); | |
595 | |
1765 | 596 ngx_queue_remove(&file->queue); |
1453 | 597 |
1765 | 598 ngx_queue_insert_head(&cache->expire_queue, &file->queue); |
1453 | 599 |
1772 | 600 if (file->uses >= min_uses || file->count) { |
601 return; | |
602 } | |
1453 | 603 } |
604 | |
1775 | 605 ngx_open_file_del_event(file); |
1453 | 606 |
607 if (file->count) { | |
608 return; | |
609 } | |
610 | |
1772 | 611 if (file->fd != NGX_INVALID_FILE) { |
612 | |
613 if (ngx_close_file(file->fd) == NGX_FILE_ERROR) { | |
614 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | |
615 ngx_close_file_n " \"%s\" failed", file->name); | |
616 } | |
617 | |
618 file->fd = NGX_INVALID_FILE; | |
619 } | |
620 | |
621 if (!file->close) { | |
622 return; | |
1453 | 623 } |
624 | |
625 ngx_free(file->name); | |
626 ngx_free(file); | |
627 } | |
628 | |
629 | |
630 static void | |
1775 | 631 ngx_open_file_del_event(ngx_cached_open_file_t *file) |
632 { | |
633 if (file->event == NULL) { | |
634 return; | |
635 } | |
636 | |
637 (void) ngx_del_event(file->event, NGX_VNODE_EVENT, | |
638 file->count ? NGX_FLUSH_EVENT : NGX_CLOSE_EVENT); | |
639 | |
640 ngx_free(file->event->data); | |
641 ngx_free(file->event); | |
642 file->event = NULL; | |
643 file->use_event = 0; | |
644 } | |
645 | |
646 | |
647 static void | |
1453 | 648 ngx_expire_old_cached_files(ngx_open_file_cache_t *cache, ngx_uint_t n, |
649 ngx_log_t *log) | |
650 { | |
651 time_t now; | |
1765 | 652 ngx_queue_t *q; |
1453 | 653 ngx_cached_open_file_t *file; |
654 | |
655 now = ngx_time(); | |
656 | |
657 /* | |
658 * n == 1 deletes one or two inactive files | |
659 * n == 0 deletes least recently used file by force | |
660 * and one or two inactive files | |
661 */ | |
662 | |
663 while (n < 3) { | |
664 | |
1765 | 665 if (ngx_queue_empty(&cache->expire_queue)) { |
1453 | 666 return; |
667 } | |
668 | |
1765 | 669 q = ngx_queue_last(&cache->expire_queue); |
670 | |
671 file = ngx_queue_data(q, ngx_cached_open_file_t, queue); | |
672 | |
1453 | 673 if (n++ != 0 && now - file->accessed <= cache->inactive) { |
674 return; | |
675 } | |
676 | |
1765 | 677 ngx_queue_remove(q); |
1453 | 678 |
679 ngx_rbtree_delete(&cache->rbtree, &file->node); | |
680 | |
681 cache->current--; | |
682 | |
683 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, | |
684 "expire cached open file: %s", file->name); | |
685 | |
686 if (!file->err && !file->is_dir) { | |
687 file->close = 1; | |
1772 | 688 ngx_close_cached_file(cache, file, 0, log); |
1453 | 689 |
690 } else { | |
691 ngx_free(file->name); | |
692 ngx_free(file); | |
693 } | |
694 } | |
695 } | |
696 | |
697 | |
698 static void | |
699 ngx_open_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp, | |
700 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel) | |
701 { | |
702 ngx_rbtree_node_t **p; | |
703 ngx_cached_open_file_t *file, *file_temp; | |
704 | |
705 for ( ;; ) { | |
706 | |
707 if (node->key < temp->key) { | |
708 | |
709 p = &temp->left; | |
710 | |
711 } else if (node->key > temp->key) { | |
712 | |
713 p = &temp->right; | |
714 | |
715 } else { /* node->key == temp->key */ | |
716 | |
717 file = (ngx_cached_open_file_t *) node; | |
718 file_temp = (ngx_cached_open_file_t *) temp; | |
719 | |
720 p = (ngx_strcmp(file->name, file_temp->name) < 0) | |
721 ? &temp->left : &temp->right; | |
722 } | |
723 | |
724 if (*p == sentinel) { | |
725 break; | |
726 } | |
727 | |
728 temp = *p; | |
729 } | |
730 | |
731 *p = node; | |
732 node->parent = temp; | |
733 node->left = sentinel; | |
734 node->right = sentinel; | |
735 ngx_rbt_red(node); | |
736 } | |
737 | |
738 | |
1775 | 739 static ngx_cached_open_file_t * |
740 ngx_open_file_lookup(ngx_open_file_cache_t *cache, ngx_str_t *name, | |
741 uint32_t hash) | |
742 { | |
743 ngx_int_t rc; | |
744 ngx_rbtree_node_t *node, *sentinel; | |
745 ngx_cached_open_file_t *file; | |
746 | |
747 node = cache->rbtree.root; | |
748 sentinel = cache->rbtree.sentinel; | |
749 | |
750 while (node != sentinel) { | |
751 | |
752 if (hash < node->key) { | |
753 node = node->left; | |
754 continue; | |
755 } | |
756 | |
757 if (hash > node->key) { | |
758 node = node->right; | |
759 continue; | |
760 } | |
761 | |
762 /* hash == node->key */ | |
763 | |
764 do { | |
765 file = (ngx_cached_open_file_t *) node; | |
766 | |
767 rc = ngx_strcmp(name->data, file->name); | |
768 | |
769 if (rc == 0) { | |
770 return file; | |
771 } | |
772 | |
773 node = (rc < 0) ? node->left : node->right; | |
774 | |
775 } while (node != sentinel && hash == node->key); | |
776 | |
777 break; | |
778 } | |
779 | |
780 return NULL; | |
781 } | |
782 | |
783 | |
1453 | 784 static void |
785 ngx_open_file_cache_remove(ngx_event_t *ev) | |
786 { | |
787 ngx_cached_open_file_t *file; | |
788 ngx_open_file_cache_event_t *fev; | |
789 | |
790 fev = ev->data; | |
791 file = fev->file; | |
792 | |
1765 | 793 ngx_queue_remove(&file->queue); |
1453 | 794 |
795 ngx_rbtree_delete(&fev->cache->rbtree, &file->node); | |
796 | |
797 fev->cache->current--; | |
798 | |
799 /* NGX_ONESHOT_EVENT was already deleted */ | |
800 file->event = NULL; | |
801 | |
802 file->close = 1; | |
803 | |
1772 | 804 ngx_close_cached_file(fev->cache, file, 0, ev->log); |
1453 | 805 |
806 /* free memory only when fev->cache and fev->file are already not needed */ | |
807 | |
808 ngx_free(ev->data); | |
809 ngx_free(ev); | |
810 } |