comparison src/core/ngx_open_file_cache.c @ 384:12defd37f578 NGINX_0_7_4

nginx 0.7.4 *) Feature: variables support in the "access_log" directive. *) Feature: the "open_log_file_cache" directive. *) Feature: the -g switch. *) Feature: the "Expect" request header line support. *) Bugfix: large SSI inclusions might be truncated.
author Igor Sysoev <http://sysoev.ru>
date Mon, 30 Jun 2008 00:00:00 +0400
parents edf1cb6c328e
children 0b6053502c55
comparison
equal deleted inserted replaced
383:6ee3ada01457 384:12defd37f578
133 ngx_pool_cleanup_t *cln; 133 ngx_pool_cleanup_t *cln;
134 ngx_cached_open_file_t *file; 134 ngx_cached_open_file_t *file;
135 ngx_pool_cleanup_file_t *clnf; 135 ngx_pool_cleanup_file_t *clnf;
136 ngx_open_file_cache_cleanup_t *ofcln; 136 ngx_open_file_cache_cleanup_t *ofcln;
137 137
138 of->fd = NGX_INVALID_FILE;
138 of->err = 0; 139 of->err = 0;
139 140
140 if (cache == NULL) { 141 if (cache == NULL) {
141 142
142 cln = ngx_pool_cleanup_add(pool, sizeof(ngx_pool_cleanup_file_t)); 143 cln = ngx_pool_cleanup_add(pool, sizeof(ngx_pool_cleanup_file_t));
186 } 187 }
187 188
188 goto add_event; 189 goto add_event;
189 } 190 }
190 191
191 if ((file->event && file->use_event) 192 if (file->use_event
192 || (file->event == NULL && now - file->created < of->valid)) 193 || (file->event == NULL
194 && (of->uniq == 0 || of->uniq == file->uniq)
195 && now - file->created < of->valid))
193 { 196 {
194 if (file->err == 0) { 197 if (file->err == 0) {
195 198
196 of->fd = file->fd; 199 of->fd = file->fd;
197 of->uniq = file->uniq; 200 of->uniq = file->uniq;
228 */ 231 */
229 232
230 of->test_dir = 1; 233 of->test_dir = 1;
231 } 234 }
232 235
236 of->fd = file->fd;
237 of->uniq = file->uniq;
238
233 rc = ngx_open_and_stat_file(name->data, of, pool->log); 239 rc = ngx_open_and_stat_file(name->data, of, pool->log);
234 240
235 if (rc != NGX_OK && (of->err == 0 || !of->errors)) { 241 if (rc != NGX_OK && (of->err == 0 || !of->errors)) {
236 goto failed; 242 goto failed;
237 } 243 }
248 254
249 if (file->is_dir || file->err) { 255 if (file->is_dir || file->err) {
250 goto add_event; 256 goto add_event;
251 } 257 }
252 258
253 if (of->uniq == file->uniq 259 if (of->uniq == file->uniq) {
254 && of->mtime == file->mtime 260
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);
261 }
262
263 of->fd = file->fd;
264 file->count++; 261 file->count++;
265 262
266 if (file->event) { 263 if (file->event) {
267 file->use_event = 1; 264 file->use_event = 1;
268 goto renew;
269 } 265 }
270
271 ngx_open_file_add_event(cache, file, of, pool->log);
272 266
273 goto renew; 267 goto renew;
274 } 268 }
275 269
276 /* file was changed */ 270 /* file was changed */
342 336
343 cache->current++; 337 cache->current++;
344 338
345 file->uses = 1; 339 file->uses = 1;
346 file->count = 0; 340 file->count = 0;
347 file->use_event = 0;
348 file->event = NULL; 341 file->event = NULL;
349 342
350 add_event: 343 add_event:
351 344
352 ngx_open_file_add_event(cache, file, of, pool->log); 345 ngx_open_file_add_event(cache, file, of, pool->log);
444 ngx_open_and_stat_file(u_char *name, ngx_open_file_info_t *of, ngx_log_t *log) 437 ngx_open_and_stat_file(u_char *name, ngx_open_file_info_t *of, ngx_log_t *log)
445 { 438 {
446 ngx_fd_t fd; 439 ngx_fd_t fd;
447 ngx_file_info_t fi; 440 ngx_file_info_t fi;
448 441
449 of->fd = NGX_INVALID_FILE; 442 if (of->fd != NGX_INVALID_FILE) {
450
451 if (of->test_dir) {
452 443
453 if (ngx_file_info(name, &fi) == -1) { 444 if (ngx_file_info(name, &fi) == -1) {
454 of->err = ngx_errno; 445 goto failed;
455 446 }
456 return NGX_ERROR; 447
457 } 448 if (of->uniq == ngx_file_uniq(&fi)) {
458 449 goto done;
459 of->uniq = ngx_file_uniq(&fi); 450 }
460 of->mtime = ngx_file_mtime(&fi); 451
461 of->size = ngx_file_size(&fi); 452 } else if (of->test_dir) {
462 of->is_dir = ngx_is_dir(&fi); 453
463 of->is_file = ngx_is_file(&fi); 454 if (ngx_file_info(name, &fi) == -1) {
464 of->is_link = ngx_is_link(&fi); 455 goto failed;
465 of->is_exec = ngx_is_exec(&fi); 456 }
466 457
467 if (of->is_dir) { 458 if (of->is_dir) {
468 return NGX_OK; 459 goto done;
469 } 460 }
470 } 461 }
471 462
472 fd = ngx_open_file(name, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0); 463 if (!of->log) {
464 fd = ngx_open_file(name, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
465
466 } else {
467 fd = ngx_open_file(name, NGX_FILE_RDWR,
468 NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND,
469 NGX_FILE_DEFAULT_ACCESS);
470 }
473 471
474 if (fd == NGX_INVALID_FILE) { 472 if (fd == NGX_INVALID_FILE) {
475 of->err = ngx_errno; 473 goto failed;
476 return NGX_ERROR;
477 } 474 }
478 475
479 if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) { 476 if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) {
480 ngx_log_error(NGX_LOG_CRIT, log, ngx_errno, 477 ngx_log_error(NGX_LOG_CRIT, log, ngx_errno,
481 ngx_fd_info_n " \"%s\" failed", name); 478 ngx_fd_info_n " \"%s\" failed", name);
482 479
483 if (ngx_close_file(fd) == NGX_FILE_ERROR) { 480 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
484 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, 481 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
485 ngx_close_file_n " \"%s\" failed", name); 482 ngx_close_file_n " \"%s\" failed", name);
486 } 483 }
484
485 of->fd = NGX_INVALID_FILE;
487 486
488 return NGX_ERROR; 487 return NGX_ERROR;
489 } 488 }
490 489
491 if (ngx_is_dir(&fi)) { 490 if (ngx_is_dir(&fi)) {
492 if (ngx_close_file(fd) == NGX_FILE_ERROR) { 491 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
493 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, 492 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
494 ngx_close_file_n " \"%s\" failed", name); 493 ngx_close_file_n " \"%s\" failed", name);
495 } 494 }
496 495
497 fd = NGX_INVALID_FILE; 496 of->fd = NGX_INVALID_FILE;
498 } 497
499 498 } else {
500 of->fd = fd; 499 of->fd = fd;
500 }
501
502 done:
503
501 of->uniq = ngx_file_uniq(&fi); 504 of->uniq = ngx_file_uniq(&fi);
502 of->mtime = ngx_file_mtime(&fi); 505 of->mtime = ngx_file_mtime(&fi);
503 of->size = ngx_file_size(&fi); 506 of->size = ngx_file_size(&fi);
504 of->is_dir = ngx_is_dir(&fi); 507 of->is_dir = ngx_is_dir(&fi);
505 of->is_file = ngx_is_file(&fi); 508 of->is_file = ngx_is_file(&fi);
506 of->is_link = ngx_is_link(&fi); 509 of->is_link = ngx_is_link(&fi);
507 of->is_exec = ngx_is_exec(&fi); 510 of->is_exec = ngx_is_exec(&fi);
508 511
509 return NGX_OK; 512 return NGX_OK;
513
514 failed:
515
516 of->fd = NGX_INVALID_FILE;
517 of->err = ngx_errno;
518
519 return NGX_ERROR;
510 } 520 }
511 521
512 522
513 /* 523 /*
514 * we ignore any possible event setting error and 524 * we ignore any possible event setting error and
528 || file->uses < of->min_uses) 538 || file->uses < of->min_uses)
529 { 539 {
530 return; 540 return;
531 } 541 }
532 542
543 file->use_event = 0;
544
533 file->event = ngx_calloc(sizeof(ngx_event_t), log); 545 file->event = ngx_calloc(sizeof(ngx_event_t), log);
534 if (file->event== NULL) { 546 if (file->event== NULL) {
535 return; 547 return;
536 } 548 }
537 549
565 file->event = NULL; 577 file->event = NULL;
566 return; 578 return;
567 } 579 }
568 580
569 /* 581 /*
570 * we do not file->use_event here because there may be a race 582 * we do not set file->use_event here because there may be a race
571 * condition between opening file and adding event, so we rely 583 * condition: a file may be deleted between opening the file and
572 * upon event notification only after first file revalidation 584 * adding event, so we rely upon event notification only after
585 * one file revalidation on next file access
573 */ 586 */
574 587
575 return; 588 return;
576 } 589 }
577 590
805 818
806 fev->cache->current--; 819 fev->cache->current--;
807 820
808 /* NGX_ONESHOT_EVENT was already deleted */ 821 /* NGX_ONESHOT_EVENT was already deleted */
809 file->event = NULL; 822 file->event = NULL;
823 file->use_event = 0;
810 824
811 file->close = 1; 825 file->close = 1;
812 826
813 ngx_close_cached_file(fev->cache, file, 0, ev->log); 827 ngx_close_cached_file(fev->cache, file, 0, ev->log);
814 828