comparison src/http/ngx_http_file_cache.c @ 3970:2d05952a324d

elimination of reading cache files by cache loader
author Igor Sysoev <igor@sysoev.ru>
date Sun, 24 Jul 2011 19:46:53 +0000
parents 3ea15c6b16da
children 44f4fc874b2f
comparison
equal deleted inserted replaced
3969:3ea15c6b16da 3970:2d05952a324d
1318 1318
1319 1319
1320 static ngx_int_t 1320 static ngx_int_t
1321 ngx_http_file_cache_add_file(ngx_tree_ctx_t *ctx, ngx_str_t *name) 1321 ngx_http_file_cache_add_file(ngx_tree_ctx_t *ctx, ngx_str_t *name)
1322 { 1322 {
1323 u_char *p; 1323 u_char *p;
1324 ngx_fd_t fd; 1324 ngx_int_t n;
1325 ngx_int_t n; 1325 ngx_uint_t i;
1326 ngx_uint_t i; 1326 ngx_http_cache_t c;
1327 ngx_file_info_t fi; 1327 ngx_http_file_cache_t *cache;
1328 ngx_http_cache_t c;
1329 ngx_http_file_cache_t *cache;
1330 ngx_http_file_cache_header_t h;
1331 1328
1332 if (name->len < 2 * NGX_HTTP_CACHE_KEY_LEN) { 1329 if (name->len < 2 * NGX_HTTP_CACHE_KEY_LEN) {
1333 return NGX_ERROR; 1330 return NGX_ERROR;
1334 } 1331 }
1335 1332
1336 ngx_memzero(&c, sizeof(ngx_http_cache_t)); 1333 if (ctx->size < (off_t) sizeof(ngx_http_file_cache_header_t)) {
1337
1338 fd = ngx_open_file(name->data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
1339
1340 if (fd == NGX_INVALID_FILE) {
1341 ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
1342 ngx_open_file_n " \"%s\" failed", name->data);
1343 return NGX_ERROR;
1344 }
1345
1346 c.file.fd = fd;
1347 c.file.name = *name;
1348 c.file.log = ctx->log;
1349
1350 n = ngx_read_file(&c.file, (u_char *) &h,
1351 sizeof(ngx_http_file_cache_header_t), 0);
1352 if (n == NGX_ERROR) {
1353 return NGX_ERROR;
1354 }
1355
1356 if ((size_t) n < sizeof(ngx_http_file_cache_header_t)) {
1357 ngx_log_error(NGX_LOG_CRIT, ctx->log, 0, 1334 ngx_log_error(NGX_LOG_CRIT, ctx->log, 0,
1358 "cache file \"%s\" is too small", name->data); 1335 "cache file \"%s\" is too small", name->data);
1359 return NGX_ERROR; 1336 return NGX_ERROR;
1360 } 1337 }
1361 1338
1339 ngx_memzero(&c, sizeof(ngx_http_cache_t));
1362 cache = ctx->data; 1340 cache = ctx->data;
1363 1341
1364 if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) { 1342 c.length = ctx->size;
1365 ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno, 1343 c.fs_size = (ctx->fs_size + cache->bsize - 1) / cache->bsize;
1366 ngx_fd_info_n " \"%s\" failed", name->data);
1367
1368 } else {
1369 c.length = ngx_file_size(&fi);
1370 c.fs_size = (ngx_file_fs_size(&fi) + cache->bsize - 1) / cache->bsize;
1371 }
1372
1373 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
1374 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
1375 ngx_close_file_n " \"%s\" failed", name->data);
1376 }
1377 1344
1378 p = &name->data[name->len - 2 * NGX_HTTP_CACHE_KEY_LEN]; 1345 p = &name->data[name->len - 2 * NGX_HTTP_CACHE_KEY_LEN];
1379 1346
1380 for (i = 0; i < NGX_HTTP_CACHE_KEY_LEN; i++) { 1347 for (i = 0; i < NGX_HTTP_CACHE_KEY_LEN; i++) {
1381 n = ngx_hextoi(p, 2); 1348 n = ngx_hextoi(p, 2);