aboutsummaryrefslogtreecommitdiff
path: root/src/core/ngx_file.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/ngx_file.c')
-rw-r--r--src/core/ngx_file.c152
1 files changed, 122 insertions, 30 deletions
diff --git a/src/core/ngx_file.c b/src/core/ngx_file.c
index 8e1edbd1f..08d9abab5 100644
--- a/src/core/ngx_file.c
+++ b/src/core/ngx_file.c
@@ -12,9 +12,9 @@ static ngx_uint_t ngx_temp_number;
static ngx_uint_t ngx_random;
-int ngx_write_chain_to_temp_file(ngx_temp_file_t *tf, ngx_chain_t *chain)
+ssize_t ngx_write_chain_to_temp_file(ngx_temp_file_t *tf, ngx_chain_t *chain)
{
- int rc;
+ ngx_int_t rc;
if (tf->file.fd == NGX_INVALID_FILE) {
rc = ngx_create_temp_file(&tf->file, tf->path, tf->pool,
@@ -33,16 +33,17 @@ int ngx_write_chain_to_temp_file(ngx_temp_file_t *tf, ngx_chain_t *chain)
}
-int ngx_create_temp_file(ngx_file_t *file, ngx_path_t *path,
- ngx_pool_t *pool, int persistent)
+ngx_int_t ngx_create_temp_file(ngx_file_t *file, ngx_path_t *path,
+ ngx_pool_t *pool, int persistent)
{
- int num;
- ngx_err_t err;
+ ngx_err_t err;
+ uint32_t num;
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 (!(file->name.data = ngx_palloc(pool, file->name.len + 1))) {
+ return NGX_ERROR;
+ }
#if 0
for (i = 0; i < file->name.len; i++) {
@@ -52,11 +53,11 @@ int ngx_create_temp_file(ngx_file_t *file, ngx_path_t *path,
ngx_memcpy(file->name.data, path->name.data, path->name.len);
- num = ngx_next_temp_number(0);
+ num = (uint32_t) ngx_next_temp_number(0);
for ( ;; ) {
ngx_sprintf(file->name.data + path->name.len + 1 + path->len,
- "%010ud%Z", num);
+ "%010ui%Z", num);
ngx_create_hashed_filename(file, path);
@@ -85,7 +86,8 @@ int ngx_create_temp_file(ngx_file_t *file, ngx_path_t *path,
#if (NGX_WIN32)
&& err != NGX_ENOTDIR
#endif
- )) {
+ ))
+ {
ngx_log_error(NGX_LOG_CRIT, file->log, err,
ngx_open_tempfile_n " \"%s\" failed",
file->name.data);
@@ -101,8 +103,7 @@ int ngx_create_temp_file(ngx_file_t *file, ngx_path_t *path,
void ngx_create_hashed_filename(ngx_file_t *file, ngx_path_t *path)
{
- int i, name, pos;
- size_t level;
+ ngx_uint_t i, name, pos, level;
name = file->name.len;
pos = path->name.len + 1;
@@ -127,7 +128,7 @@ void ngx_create_hashed_filename(ngx_file_t *file, ngx_path_t *path)
}
-int ngx_create_path(ngx_file_t *file, ngx_path_t *path)
+ngx_int_t ngx_create_path(ngx_file_t *file, ngx_path_t *path)
{
int i, pos;
ngx_err_t err;
@@ -189,32 +190,28 @@ char *ngx_conf_set_path_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
char *p = conf;
- ngx_int_t level;
+ ssize_t level;
ngx_uint_t i, n;
ngx_str_t *value;
- ngx_path_t *path, **pp;
+ ngx_path_t *path, **pp, **slot;
- pp = (ngx_path_t **) (p + cmd->offset);
+ slot = (ngx_path_t **) (p + cmd->offset);
- if (*pp) {
+ if (*slot) {
return "is duplicate";
}
- /* TODO: check duplicate in cf->cycle->pathes */
-
- 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);
- *pp = path;
+ if (!(path = ngx_pcalloc(cf->pool, sizeof(ngx_path_t)))) {
+ return NGX_CONF_ERROR;
+ }
- value = (ngx_str_t *) cf->args->elts;
+ value = cf->args->elts;
path->name = value[1];
-
path->len = 0;
+ path->gc_handler = (ngx_gc_handler_pt) cmd->post;
+ path->conf_file = cf->conf_file->file.name.data;
+ path->line = cf->conf_file->line;
for (i = 0, n = 2; n < cf->args->nelts; i++, n++) {
level = ngx_atoi(value[n].data, value[n].len);
@@ -230,7 +227,102 @@ char *ngx_conf_set_path_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
path->level[i++] = 0;
}
- path->gc_handler = (ngx_gc_handler_pt) cmd->post;
+
+ pp = cf->cycle->pathes.elts;
+ for (i = 0; i < cf->cycle->pathes.nelts; i++) {
+ if (pp[i]->name.len == path->name.len
+ && ngx_strcmp(pp[i]->name.data, path->name.data) == 0)
+ {
+ for (n = 0; n < 3; n++) {
+ if (pp[i]->level[n] != path->level[n]) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "the same \"%V\" path name in %s:%ui "
+ "has the different levels than",
+ &pp[i]->name, pp[i]->conf_file, pp[i]->line);
+ return NGX_CONF_ERROR;
+ }
+
+ if (pp[i]->level[n] == 0) {
+ break;
+ }
+ }
+
+ *slot = pp[i];
+
+ return NGX_CONF_OK;
+ }
+ }
+
+ *slot = path;
+
+
+ if (!(pp = ngx_array_push(&cf->cycle->pathes))) {
+ return NGX_CONF_ERROR;
+ }
+
+ *pp = path;
return NGX_CONF_OK;
}
+
+
+ngx_int_t ngx_create_pathes(ngx_cycle_t *cycle, ngx_uid_t user)
+{
+ ngx_err_t err;
+ ngx_uint_t i;
+ ngx_path_t **path;
+#if !(NGX_WIN32)
+ ngx_file_info_t fi;
+#endif
+
+ path = cycle->pathes.elts;
+ for (i = 0; i < cycle->pathes.nelts; i++) {
+
+ if (ngx_create_dir(path[i]->name.data) == NGX_FILE_ERROR) {
+ err = ngx_errno;
+ if (err != NGX_EEXIST) {
+ ngx_log_error(NGX_LOG_EMERG, cycle->log, err,
+ ngx_create_dir_n " \"%s\" failed",
+ path[i]->name.data);
+ return NGX_ERROR;
+ }
+ }
+
+ if (user == (ngx_uid_t) -1) {
+ continue;
+ }
+
+#if !(NGX_WIN32)
+
+ if (ngx_file_info((const char *) path[i]->name.data, &fi) == -1) {
+ ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
+ ngx_file_info_n " \"%s\" failed", path[i]->name.data);
+ return NGX_ERROR;
+ }
+
+ if (fi.st_uid != user) {
+ if (chown((const char *) path[i]->name.data, user, -1) == -1) {
+ ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
+ "chown(\"%s\", %d) failed",
+ path[i]->name.data, user);
+ return NGX_ERROR;
+ }
+ }
+
+ if ((fi.st_mode & (S_IRUSR|S_IWUSR|S_IXUSR))
+ != (S_IRUSR|S_IWUSR|S_IXUSR))
+ {
+ fi.st_mode |= (S_IRUSR|S_IWUSR|S_IXUSR);
+
+ if (chmod((const char *) path[i]->name.data, fi.st_mode) == -1) {
+ ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
+ "chmod() \"%s\" failed", path[i]->name.data);
+ return NGX_ERROR;
+ }
+ }
+
+#endif
+ }
+
+ return NGX_OK;
+}