Mercurial > hg > nginx
comparison src/core/ngx_open_file_cache.c @ 4476:94ef9d25ec5b
Changed ngx_open_and_stat_file() to use ngx_str_t.
No functional changes.
author | Andrey Belov <defan@nginx.com> |
---|---|
date | Mon, 13 Feb 2012 16:16:45 +0000 |
parents | d620f497c50f |
children | 7033faf6dc3c |
comparison
equal
deleted
inserted
replaced
4475:0b05acc045eb | 4476:94ef9d25ec5b |
---|---|
20 | 20 |
21 #define NGX_MIN_READ_AHEAD (128 * 1024) | 21 #define NGX_MIN_READ_AHEAD (128 * 1024) |
22 | 22 |
23 | 23 |
24 static void ngx_open_file_cache_cleanup(void *data); | 24 static void ngx_open_file_cache_cleanup(void *data); |
25 static ngx_int_t ngx_open_and_stat_file(u_char *name, ngx_open_file_info_t *of, | 25 static ngx_int_t ngx_open_and_stat_file(ngx_str_t *name, |
26 ngx_log_t *log); | 26 ngx_open_file_info_t *of, ngx_log_t *log); |
27 static void ngx_open_file_add_event(ngx_open_file_cache_t *cache, | 27 static void ngx_open_file_add_event(ngx_open_file_cache_t *cache, |
28 ngx_cached_open_file_t *file, ngx_open_file_info_t *of, ngx_log_t *log); | 28 ngx_cached_open_file_t *file, ngx_open_file_info_t *of, ngx_log_t *log); |
29 static void ngx_open_file_cleanup(void *data); | 29 static void ngx_open_file_cleanup(void *data); |
30 static void ngx_close_cached_file(ngx_open_file_cache_t *cache, | 30 static void ngx_close_cached_file(ngx_open_file_cache_t *cache, |
31 ngx_cached_open_file_t *file, ngx_uint_t min_uses, ngx_log_t *log); | 31 ngx_cached_open_file_t *file, ngx_uint_t min_uses, ngx_log_t *log); |
168 cln = ngx_pool_cleanup_add(pool, sizeof(ngx_pool_cleanup_file_t)); | 168 cln = ngx_pool_cleanup_add(pool, sizeof(ngx_pool_cleanup_file_t)); |
169 if (cln == NULL) { | 169 if (cln == NULL) { |
170 return NGX_ERROR; | 170 return NGX_ERROR; |
171 } | 171 } |
172 | 172 |
173 rc = ngx_open_and_stat_file(name->data, of, pool->log); | 173 rc = ngx_open_and_stat_file(name, of, pool->log); |
174 | 174 |
175 if (rc == NGX_OK && !of->is_dir) { | 175 if (rc == NGX_OK && !of->is_dir) { |
176 cln->handler = ngx_pool_cleanup_file; | 176 cln->handler = ngx_pool_cleanup_file; |
177 clnf = cln->data; | 177 clnf = cln->data; |
178 | 178 |
203 | 203 |
204 if (file->fd == NGX_INVALID_FILE && file->err == 0 && !file->is_dir) { | 204 if (file->fd == NGX_INVALID_FILE && file->err == 0 && !file->is_dir) { |
205 | 205 |
206 /* file was not used often enough to keep open */ | 206 /* file was not used often enough to keep open */ |
207 | 207 |
208 rc = ngx_open_and_stat_file(name->data, of, pool->log); | 208 rc = ngx_open_and_stat_file(name, of, pool->log); |
209 | 209 |
210 if (rc != NGX_OK && (of->err == 0 || !of->errors)) { | 210 if (rc != NGX_OK && (of->err == 0 || !of->errors)) { |
211 goto failed; | 211 goto failed; |
212 } | 212 } |
213 | 213 |
261 } | 261 } |
262 | 262 |
263 of->fd = file->fd; | 263 of->fd = file->fd; |
264 of->uniq = file->uniq; | 264 of->uniq = file->uniq; |
265 | 265 |
266 rc = ngx_open_and_stat_file(name->data, of, pool->log); | 266 rc = ngx_open_and_stat_file(name, of, pool->log); |
267 | 267 |
268 if (rc != NGX_OK && (of->err == 0 || !of->errors)) { | 268 if (rc != NGX_OK && (of->err == 0 || !of->errors)) { |
269 goto failed; | 269 goto failed; |
270 } | 270 } |
271 | 271 |
309 | 309 |
310 ngx_open_file_del_event(file); | 310 ngx_open_file_del_event(file); |
311 | 311 |
312 if (ngx_close_file(file->fd) == NGX_FILE_ERROR) { | 312 if (ngx_close_file(file->fd) == NGX_FILE_ERROR) { |
313 ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno, | 313 ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno, |
314 ngx_close_file_n " \"%s\" failed", | 314 ngx_close_file_n " \"%V\" failed", name); |
315 name->data); | |
316 } | 315 } |
317 | 316 |
318 goto add_event; | 317 goto add_event; |
319 } | 318 } |
320 | 319 |
327 goto create; | 326 goto create; |
328 } | 327 } |
329 | 328 |
330 /* not found */ | 329 /* not found */ |
331 | 330 |
332 rc = ngx_open_and_stat_file(name->data, of, pool->log); | 331 rc = ngx_open_and_stat_file(name, of, pool->log); |
333 | 332 |
334 if (rc != NGX_OK && (of->err == 0 || !of->errors)) { | 333 if (rc != NGX_OK && (of->err == 0 || !of->errors)) { |
335 goto failed; | 334 goto failed; |
336 } | 335 } |
337 | 336 |
450 } | 449 } |
451 | 450 |
452 if (of->fd != NGX_INVALID_FILE) { | 451 if (of->fd != NGX_INVALID_FILE) { |
453 if (ngx_close_file(of->fd) == NGX_FILE_ERROR) { | 452 if (ngx_close_file(of->fd) == NGX_FILE_ERROR) { |
454 ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno, | 453 ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno, |
455 ngx_close_file_n " \"%s\" failed", name->data); | 454 ngx_close_file_n " \"%V\" failed", name); |
456 } | 455 } |
457 } | 456 } |
458 | 457 |
459 return NGX_ERROR; | 458 return NGX_ERROR; |
460 } | 459 } |
461 | 460 |
462 | 461 |
463 static ngx_int_t | 462 static ngx_int_t |
464 ngx_open_and_stat_file(u_char *name, ngx_open_file_info_t *of, ngx_log_t *log) | 463 ngx_open_and_stat_file(ngx_str_t *name, ngx_open_file_info_t *of, |
464 ngx_log_t *log) | |
465 { | 465 { |
466 ngx_fd_t fd; | 466 ngx_fd_t fd; |
467 ngx_file_info_t fi; | 467 ngx_file_info_t fi; |
468 | 468 |
469 if (of->fd != NGX_INVALID_FILE) { | 469 if (of->fd != NGX_INVALID_FILE) { |
470 | 470 |
471 if (ngx_file_info(name, &fi) == NGX_FILE_ERROR) { | 471 if (ngx_file_info(name->data, &fi) == NGX_FILE_ERROR) { |
472 of->failed = ngx_file_info_n; | 472 of->failed = ngx_file_info_n; |
473 goto failed; | 473 goto failed; |
474 } | 474 } |
475 | 475 |
476 if (of->uniq == ngx_file_uniq(&fi)) { | 476 if (of->uniq == ngx_file_uniq(&fi)) { |
477 goto done; | 477 goto done; |
478 } | 478 } |
479 | 479 |
480 } else if (of->test_dir) { | 480 } else if (of->test_dir) { |
481 | 481 |
482 if (ngx_file_info(name, &fi) == NGX_FILE_ERROR) { | 482 if (ngx_file_info(name->data, &fi) == NGX_FILE_ERROR) { |
483 of->failed = ngx_file_info_n; | 483 of->failed = ngx_file_info_n; |
484 goto failed; | 484 goto failed; |
485 } | 485 } |
486 | 486 |
487 if (ngx_is_dir(&fi)) { | 487 if (ngx_is_dir(&fi)) { |
494 /* | 494 /* |
495 * Use non-blocking open() not to hang on FIFO files, etc. | 495 * Use non-blocking open() not to hang on FIFO files, etc. |
496 * This flag has no effect on a regular files. | 496 * This flag has no effect on a regular files. |
497 */ | 497 */ |
498 | 498 |
499 fd = ngx_open_file(name, NGX_FILE_RDONLY|NGX_FILE_NONBLOCK, | 499 fd = ngx_open_file(name->data, NGX_FILE_RDONLY|NGX_FILE_NONBLOCK, |
500 NGX_FILE_OPEN, 0); | 500 NGX_FILE_OPEN, 0); |
501 | 501 |
502 } else { | 502 } else { |
503 fd = ngx_open_file(name, NGX_FILE_APPEND, NGX_FILE_CREATE_OR_OPEN, | 503 fd = ngx_open_file(name->data, NGX_FILE_APPEND, |
504 NGX_FILE_CREATE_OR_OPEN, | |
504 NGX_FILE_DEFAULT_ACCESS); | 505 NGX_FILE_DEFAULT_ACCESS); |
505 } | 506 } |
506 | 507 |
507 if (fd == NGX_INVALID_FILE) { | 508 if (fd == NGX_INVALID_FILE) { |
508 of->failed = ngx_open_file_n; | 509 of->failed = ngx_open_file_n; |
509 goto failed; | 510 goto failed; |
510 } | 511 } |
511 | 512 |
512 if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) { | 513 if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) { |
513 ngx_log_error(NGX_LOG_CRIT, log, ngx_errno, | 514 ngx_log_error(NGX_LOG_CRIT, log, ngx_errno, |
514 ngx_fd_info_n " \"%s\" failed", name); | 515 ngx_fd_info_n " \"%V\" failed", name); |
515 | 516 |
516 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | 517 if (ngx_close_file(fd) == NGX_FILE_ERROR) { |
517 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | 518 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, |
518 ngx_close_file_n " \"%s\" failed", name); | 519 ngx_close_file_n " \"%V\" failed", name); |
519 } | 520 } |
520 | 521 |
521 of->fd = NGX_INVALID_FILE; | 522 of->fd = NGX_INVALID_FILE; |
522 | 523 |
523 return NGX_ERROR; | 524 return NGX_ERROR; |
524 } | 525 } |
525 | 526 |
526 if (ngx_is_dir(&fi)) { | 527 if (ngx_is_dir(&fi)) { |
527 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | 528 if (ngx_close_file(fd) == NGX_FILE_ERROR) { |
528 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | 529 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, |
529 ngx_close_file_n " \"%s\" failed", name); | 530 ngx_close_file_n " \"%V\" failed", name); |
530 } | 531 } |
531 | 532 |
532 of->fd = NGX_INVALID_FILE; | 533 of->fd = NGX_INVALID_FILE; |
533 | 534 |
534 } else { | 535 } else { |
535 of->fd = fd; | 536 of->fd = fd; |
536 | 537 |
537 if (of->read_ahead && ngx_file_size(&fi) > NGX_MIN_READ_AHEAD) { | 538 if (of->read_ahead && ngx_file_size(&fi) > NGX_MIN_READ_AHEAD) { |
538 if (ngx_read_ahead(fd, of->read_ahead) == NGX_ERROR) { | 539 if (ngx_read_ahead(fd, of->read_ahead) == NGX_ERROR) { |
539 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | 540 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, |
540 ngx_read_ahead_n " \"%s\" failed", name); | 541 ngx_read_ahead_n " \"%V\" failed", name); |
541 } | 542 } |
542 } | 543 } |
543 | 544 |
544 if (of->directio <= ngx_file_size(&fi)) { | 545 if (of->directio <= ngx_file_size(&fi)) { |
545 if (ngx_directio_on(fd) == NGX_FILE_ERROR) { | 546 if (ngx_directio_on(fd) == NGX_FILE_ERROR) { |
546 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | 547 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, |
547 ngx_directio_on_n " \"%s\" failed", name); | 548 ngx_directio_on_n " \"%V\" failed", name); |
548 | 549 |
549 } else { | 550 } else { |
550 of->is_directio = 1; | 551 of->is_directio = 1; |
551 } | 552 } |
552 } | 553 } |