nginx/src/core/ngx_file.c

238 lines
5.1 KiB
C
Raw Normal View History

2003-04-12 00:01:14 +08:00
/*
* Copyright (C) Igor Sysoev
*/
2003-04-12 00:01:14 +08:00
#include <ngx_config.h>
#include <ngx_core.h>
2004-03-16 15:10:12 +08:00
static ngx_uint_t ngx_temp_number;
static ngx_uint_t ngx_random;
2003-04-28 23:06:39 +08:00
2003-10-27 16:53:49 +08:00
int ngx_write_chain_to_temp_file(ngx_temp_file_t *tf, ngx_chain_t *chain)
{
int rc;
if (tf->file.fd == NGX_INVALID_FILE) {
2003-11-03 06:56:18 +08:00
rc = ngx_create_temp_file(&tf->file, tf->path, tf->pool,
2003-10-27 16:53:49 +08:00
tf->persistent);
2003-12-09 04:48:12 +08:00
2003-10-27 16:53:49 +08:00
if (rc == NGX_ERROR || rc == NGX_AGAIN) {
return rc;
}
if (!tf->persistent && tf->warn) {
ngx_log_error(NGX_LOG_WARN, tf->file.log, 0, tf->warn);
}
}
2003-11-03 06:56:18 +08:00
return ngx_write_chain_to_file(&tf->file, chain, tf->offset, tf->pool);
2003-10-27 16:53:49 +08:00
}
2003-04-12 00:01:14 +08:00
int ngx_create_temp_file(ngx_file_t *file, ngx_path_t *path,
2003-04-28 23:06:39 +08:00
ngx_pool_t *pool, int persistent)
2003-04-12 00:01:14 +08:00
{
2003-05-20 23:37:55 +08:00
int num;
2003-04-12 00:01:14 +08:00
ngx_err_t err;
file->name.len = path->name.len + 1 + path->len + 10;
ngx_test_null(file->name.data, ngx_palloc(pool, file->name.len + 1),
NGX_ERROR);
#if 0
for (i = 0; i < file->name.len; i++) {
file->name.data[i] = 'X';
}
#endif
ngx_memcpy(file->name.data, path->name.data, path->name.len);
2003-04-28 23:06:39 +08:00
num = ngx_next_temp_number(0);
2003-04-12 00:01:14 +08:00
for ( ;; ) {
2004-03-16 15:10:12 +08:00
ngx_snprintf((char *)
(file->name.data + path->name.len + 1 + path->len),
11, "%010u", num);
2003-04-12 00:01:14 +08:00
ngx_create_hashed_filename(file, path);
2004-03-31 23:26:46 +08:00
#if 1
2003-04-12 00:01:14 +08:00
file->fd = ngx_open_tempfile(file->name.data, persistent);
2004-03-17 05:26:01 +08:00
#else
2003-11-26 04:44:56 +08:00
file->fd = ngx_open_tempfile(file->name.data, 1);
2004-03-17 05:26:01 +08:00
#endif
2003-04-12 00:01:14 +08:00
2004-02-12 01:08:49 +08:00
ngx_log_debug1(NGX_LOG_DEBUG_CORE, file->log, 0,
"temp fd:%d", file->fd);
2003-04-15 01:04:58 +08:00
2003-04-12 00:01:14 +08:00
if (file->fd != NGX_INVALID_FILE) {
return NGX_OK;
}
err = ngx_errno;
if (err == NGX_EEXIST) {
2003-04-28 23:06:39 +08:00
num = ngx_next_temp_number(1);
2003-04-12 00:01:14 +08:00
continue;
}
2004-04-26 04:13:21 +08:00
if ((path->level[0] == 0)
|| (err != NGX_ENOENT
2003-11-17 05:49:42 +08:00
#if (WIN32)
2004-04-26 04:13:21 +08:00
&& err != NGX_ENOTDIR
2003-04-12 00:01:14 +08:00
#endif
2004-04-26 04:13:21 +08:00
)) {
2003-04-12 00:01:14 +08:00
ngx_log_error(NGX_LOG_CRIT, file->log, err,
ngx_open_tempfile_n " \"%s\" failed",
file->name.data);
return NGX_ERROR;
}
if (ngx_create_path(file, path) == NGX_ERROR) {
return NGX_ERROR;
2003-05-21 21:28:21 +08:00
}
2003-04-12 00:01:14 +08:00
}
}
void ngx_create_hashed_filename(ngx_file_t *file, ngx_path_t *path)
{
2003-11-21 14:30:49 +08:00
int i, name, pos;
size_t level;
2003-04-12 00:01:14 +08:00
name = file->name.len;
pos = path->name.len + 1;
file->name.data[path->name.len + path->len] = '/';
for (i = 0; i < 3; i++) {
level = path->level[i];
if (level == 0) {
break;
}
name -= level;
file->name.data[pos - 1] = '/';
ngx_memcpy(&file->name.data[pos], &file->name.data[name], level);
pos += level + 1;
}
2004-02-12 01:08:49 +08:00
ngx_log_debug1(NGX_LOG_DEBUG_CORE, file->log, 0,
"hashed path: %s", file->name.data);
2003-04-12 00:01:14 +08:00
}
int ngx_create_path(ngx_file_t *file, ngx_path_t *path)
{
int i, pos;
ngx_err_t err;
pos = path->name.len;
for (i = 0; i < 3; i++) {
if (path->level[i] == 0) {
break;
}
pos += path->level[i] + 1;
file->name.data[pos] = '\0';
2004-02-12 01:08:49 +08:00
ngx_log_debug1(NGX_LOG_DEBUG_CORE, file->log, 0,
"temp file: \"%s\"", file->name.data);
2003-04-12 00:01:14 +08:00
2003-11-17 05:49:42 +08:00
if (ngx_create_dir(file->name.data) == NGX_FILE_ERROR) {
2003-04-12 00:01:14 +08:00
err = ngx_errno;
if (err != NGX_EEXIST) {
ngx_log_error(NGX_LOG_CRIT, file->log, err,
2003-11-17 05:49:42 +08:00
ngx_create_dir_n " \"%s\" failed",
file->name.data);
2003-04-12 00:01:14 +08:00
return NGX_ERROR;
}
}
file->name.data[pos] = '/';
}
return NGX_OK;
}
2003-04-28 23:06:39 +08:00
void ngx_init_temp_number()
{
ngx_random = 0;
ngx_temp_number = ngx_random;
while (ngx_random < 10000) {
ngx_random = 123456;
}
}
2004-03-16 15:10:12 +08:00
ngx_uint_t ngx_next_temp_number(ngx_uint_t collision)
2003-04-28 23:06:39 +08:00
{
if (collision) {
ngx_temp_number += ngx_random;
}
return ngx_temp_number++;
}
2003-10-23 00:38:26 +08:00
char *ngx_conf_set_path_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
char *p = conf;
2004-03-16 15:10:12 +08:00
ngx_int_t level;
ngx_uint_t i, n;
2003-10-23 00:38:26 +08:00
ngx_str_t *value;
ngx_path_t *path, **pp;
pp = (ngx_path_t **) (p + cmd->offset);
if (*pp) {
return "is duplicate";
}
2003-11-19 05:34:08 +08:00
/* TODO: check duplicate in cf->cycle->pathes */
2003-10-23 00:38:26 +08:00
2003-11-19 05:34:08 +08:00
ngx_test_null(path, ngx_pcalloc(cf->pool, sizeof(ngx_path_t)),
NGX_CONF_ERROR);
*pp = path;
ngx_test_null(pp, ngx_push_array(&cf->cycle->pathes), NGX_CONF_ERROR);
2003-10-23 00:38:26 +08:00
*pp = path;
value = (ngx_str_t *) cf->args->elts;
path->name = value[1];
path->len = 0;
for (i = 0, n = 2; n < cf->args->nelts; i++, n++) {
2003-11-21 14:30:49 +08:00
level = ngx_atoi(value[n].data, value[n].len);
if (level == NGX_ERROR || level == 0) {
2003-10-23 00:38:26 +08:00
return "invalid value";
}
2003-11-26 04:44:56 +08:00
path->level[i] = level;
path->len += level + 1;
2003-10-23 00:38:26 +08:00
}
while (i < 3) {
path->level[i++] = 0;
}
2003-11-26 04:44:56 +08:00
path->gc_handler = (ngx_gc_handler_pt) cmd->post;
2003-11-19 05:34:08 +08:00
2003-10-23 14:13:16 +08:00
return NGX_CONF_OK;
2003-10-23 00:38:26 +08:00
}