3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
11 static ngx_atomic_uint_t ngx_temp_number;
12 static ngx_atomic_uint_t ngx_random_number;
16 ngx_write_chain_to_temp_file(ngx_temp_file_t *tf, ngx_chain_t *chain)
20 if (tf->file.fd == NGX_INVALID_FILE) {
21 rc = ngx_create_temp_file(&tf->file, tf->path, tf->pool,
22 tf->persistent, tf->clean, tf->access);
24 if (rc == NGX_ERROR || rc == NGX_AGAIN) {
29 ngx_log_error(tf->log_level, tf->file.log, 0, "%s %V",
30 tf->warn, &tf->file.name);
34 return ngx_write_chain_to_file(&tf->file, chain, tf->offset, tf->pool);
39 ngx_create_temp_file(ngx_file_t *file, ngx_path_t *path, ngx_pool_t *pool,
40 ngx_uint_t persistent, ngx_uint_t clean, ngx_uint_t access)
44 ngx_pool_cleanup_t *cln;
45 ngx_pool_cleanup_file_t *clnf;
47 file->name.len = path->name.len + 1 + path->len + 10;
49 file->name.data = ngx_pnalloc(pool, file->name.len + 1);
50 if (file->name.data == NULL) {
55 for (i = 0; i < file->name.len; i++) {
56 file->name.data[i] = 'X';
60 ngx_memcpy(file->name.data, path->name.data, path->name.len);
62 n = (uint32_t) ngx_next_temp_number(0);
64 cln = ngx_pool_cleanup_add(pool, sizeof(ngx_pool_cleanup_file_t));
70 (void) ngx_sprintf(file->name.data + path->name.len + 1 + path->len,
73 ngx_create_hashed_filename(path, file->name.data, file->name.len);
75 ngx_log_debug1(NGX_LOG_DEBUG_CORE, file->log, 0,
76 "hashed path: %s", file->name.data);
78 file->fd = ngx_open_tempfile(file->name.data, persistent, access);
80 ngx_log_debug1(NGX_LOG_DEBUG_CORE, file->log, 0,
81 "temp fd:%d", file->fd);
83 if (file->fd != NGX_INVALID_FILE) {
85 cln->handler = clean ? ngx_pool_delete_file : ngx_pool_cleanup_file;
89 clnf->name = file->name.data;
90 clnf->log = pool->log;
97 if (err == NGX_EEXIST) {
98 n = (uint32_t) ngx_next_temp_number(1);
102 if ((path->level[0] == 0)
103 || (err != NGX_ENOENT
105 && err != NGX_ENOTDIR
109 ngx_log_error(NGX_LOG_CRIT, file->log, err,
110 ngx_open_tempfile_n " \"%s\" failed",
115 if (ngx_create_path(file, path) == NGX_ERROR) {
123 ngx_create_hashed_filename(ngx_path_t *path, u_char *file, size_t len)
128 i = path->name.len + 1;
130 file[path->name.len + path->len] = '/';
132 for (n = 0; n < 3; n++) {
133 level = path->level[n];
141 ngx_memcpy(&file[i], &file[len], level);
148 ngx_create_path(ngx_file_t *file, ngx_path_t *path)
154 pos = path->name.len;
156 for (i = 0; i < 3; i++) {
157 if (path->level[i] == 0) {
161 pos += path->level[i] + 1;
163 file->name.data[pos] = '\0';
165 ngx_log_debug1(NGX_LOG_DEBUG_CORE, file->log, 0,
166 "temp file: \"%s\"", file->name.data);
168 if (ngx_create_dir(file->name.data, 0700) == NGX_FILE_ERROR) {
170 if (err != NGX_EEXIST) {
171 ngx_log_error(NGX_LOG_CRIT, file->log, err,
172 ngx_create_dir_n " \"%s\" failed",
178 file->name.data[pos] = '/';
186 ngx_create_full_path(u_char *dir, ngx_uint_t access)
191 for (p = dir + 1; *p; p++) {
200 if (ngx_create_dir(dir, access) == NGX_FILE_ERROR) {
202 if (err != NGX_EEXIST) {
215 ngx_init_temp_number(void)
218 ngx_random_number = 123456;
223 ngx_next_temp_number(ngx_uint_t collision)
226 ngx_temp_number += ngx_random_number;
229 return ngx_temp_number++;
234 ngx_conf_set_path_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
241 ngx_path_t *path, **slot;
243 slot = (ngx_path_t **) (p + cmd->offset);
246 return "is duplicate";
249 path = ngx_pcalloc(cf->pool, sizeof(ngx_path_t));
251 return NGX_CONF_ERROR;
254 value = cf->args->elts;
256 path->name = value[1];
258 if (path->name.data[path->name.len - 1] == '/') {
262 if (ngx_conf_full_name(cf->cycle, &path->name, 0) == NGX_ERROR) {
267 path->cleaner = (ngx_gc_handler_pt) cmd->post;
268 path->conf_file = cf->conf_file->file.name.data;
269 path->line = cf->conf_file->line;
271 for (i = 0, n = 2; n < cf->args->nelts; i++, n++) {
272 level = ngx_atoi(value[n].data, value[n].len);
273 if (level == NGX_ERROR || level == 0) {
274 return "invalid value";
277 path->level[i] = level;
278 path->len += level + 1;
282 path->level[i++] = 0;
287 if (ngx_add_path(cf, slot) == NGX_ERROR) {
288 return NGX_CONF_ERROR;
296 ngx_conf_set_access_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
302 ngx_uint_t i, right, shift, *access;
304 access = (ngx_uint_t *) (confp + cmd->offset);
306 if (*access != NGX_CONF_UNSET_UINT) {
307 return "is duplicate";
310 value = cf->args->elts;
314 for (i = 1; i < cf->args->nelts; i++) {
318 if (ngx_strncmp(p, "user:", sizeof("user:") - 1) == 0) {
320 p += sizeof("user:") - 1;
322 } else if (ngx_strncmp(p, "group:", sizeof("group:") - 1) == 0) {
324 p += sizeof("group:") - 1;
326 } else if (ngx_strncmp(p, "all:", sizeof("all:") - 1) == 0) {
328 p += sizeof("all:") - 1;
334 if (ngx_strcmp(p, "rw") == 0) {
337 } else if (ngx_strcmp(p, "r") == 0) {
344 *access |= right << shift;
351 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid value \"%V\"", &value[i]);
353 return NGX_CONF_ERROR;
358 ngx_add_path(ngx_conf_t *cf, ngx_path_t **slot)
361 ngx_path_t *path, **p;
365 p = cf->cycle->pathes.elts;
366 for (i = 0; i < cf->cycle->pathes.nelts; i++) {
367 if (p[i]->name.len == path->name.len
368 && ngx_strcmp(p[i]->name.data, path->name.data) == 0)
370 for (n = 0; n < 3; n++) {
371 if (p[i]->level[n] != path->level[n]) {
372 if (path->conf_file == NULL) {
373 if (p[i]->conf_file == NULL) {
374 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
375 "the default path name \"%V\" has "
376 "the same name as another default path, "
377 "but the different levels, you need to "
378 "redefine one of them in http section",
383 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
384 "the path name \"%V\" in %s:%ui has "
385 "the same name as default path, but "
386 "the different levels, you need to "
387 "define default path in http section",
388 &p[i]->name, p[i]->conf_file, p[i]->line);
392 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
393 "the same path name \"%V\" in %s:%ui "
394 "has the different levels than",
395 &p[i]->name, p[i]->conf_file, p[i]->line);
399 if (p[i]->level[n] == 0) {
410 p = ngx_array_push(&cf->cycle->pathes);
422 ngx_create_pathes(ngx_cycle_t *cycle, ngx_uid_t user)
428 path = cycle->pathes.elts;
429 for (i = 0; i < cycle->pathes.nelts; i++) {
431 if (ngx_create_dir(path[i]->name.data, 0700) == NGX_FILE_ERROR) {
433 if (err != NGX_EEXIST) {
434 ngx_log_error(NGX_LOG_EMERG, cycle->log, err,
435 ngx_create_dir_n " \"%s\" failed",
441 if (user == (ngx_uid_t) NGX_CONF_UNSET_UINT) {
449 if (ngx_file_info((const char *) path[i]->name.data, &fi) == -1) {
450 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
451 ngx_file_info_n " \"%s\" failed", path[i]->name.data);
455 if (fi.st_uid != user) {
456 if (chown((const char *) path[i]->name.data, user, -1) == -1) {
457 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
458 "chown(\"%s\", %d) failed",
459 path[i]->name.data, user);
464 if ((fi.st_mode & (S_IRUSR|S_IWUSR|S_IXUSR))
465 != (S_IRUSR|S_IWUSR|S_IXUSR))
467 fi.st_mode |= (S_IRUSR|S_IWUSR|S_IXUSR);
469 if (chmod((const char *) path[i]->name.data, fi.st_mode) == -1) {
470 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
471 "chmod() \"%s\" failed", path[i]->name.data);
484 ngx_ext_rename_file(ngx_str_t *src, ngx_str_t *to, ngx_ext_rename_file_t *ext)
491 if (ngx_change_file_access(src->data, ext->access) == NGX_FILE_ERROR) {
492 ngx_log_error(NGX_LOG_CRIT, ext->log, ngx_errno,
493 ngx_change_file_access_n " \"%s\" failed", src->data);
501 if (ext->time != -1) {
502 if (ngx_set_file_time(src->data, ext->fd, ext->time) != NGX_OK) {
503 ngx_log_error(NGX_LOG_CRIT, ext->log, ngx_errno,
504 ngx_set_file_time_n " \"%s\" failed", src->data);
510 if (ngx_rename_file(src->data, to->data) != NGX_FILE_ERROR) {
516 if (err == NGX_ENOENT) {
518 if (!ext->create_path) {
522 err = ngx_create_full_path(to->data, ngx_dir_access(ext->path_access));
525 ngx_log_error(NGX_LOG_CRIT, ext->log, err,
526 ngx_create_dir_n " \"%s\" failed", to->data);
531 if (ngx_rename_file(src->data, to->data) != NGX_FILE_ERROR) {
541 if (err == NGX_EEXIST) {
542 if (ngx_win32_rename_file(src, to, ext->log) == NGX_OK) {
544 if (ngx_rename_file(src->data, to->data) != NGX_FILE_ERROR) {
559 if (ext->delete_file) {
560 if (ngx_delete_file(src->data) == NGX_FILE_ERROR) {
561 ngx_log_error(NGX_LOG_CRIT, ext->log, ngx_errno,
562 ngx_delete_file_n " \"%s\" failed", src->data);
566 if (err && ext->log_rename_error) {
567 ngx_log_error(NGX_LOG_CRIT, ext->log, err,
568 ngx_rename_file_n " \"%s\" to \"%s\" failed",
569 src->data, to->data);
572 ext->rename_error = err;
579 * ctx->init_handler() - see ctx->alloc
580 * ctx->file_handler() - file handler
581 * ctx->pre_tree_handler() - handler is called before entering directory
582 * ctx->post_tree_handler() - handler is called after leaving directory
583 * ctx->spec_handler() - special (socket, FIFO, etc.) file handler
585 * ctx->data - some data structure, it may be the same on all levels, or
586 * reallocated if ctx->alloc is nonzero
588 * ctx->alloc - a size of data structure that is allocated at every level
589 * and is initilialized by ctx->init_handler()
593 * on fatal (memory) error handler must return NGX_ABORT to stop walking tree
597 ngx_walk_tree(ngx_tree_ctx_t *ctx, ngx_str_t *tree)
610 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ctx->log, 0,
611 "walk tree \"%V\"", tree);
613 if (ngx_open_dir(tree, &dir) == NGX_ERROR) {
614 ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
615 ngx_open_dir_n " \"%s\" failed", tree->data);
622 data = ngx_alloc(ctx->alloc, ctx->log);
627 if (ctx->init_handler(data, prev) == NGX_ABORT) {
641 if (ngx_read_dir(&dir) == NGX_ERROR) {
644 if (err == NGX_ENOMOREFILES) {
648 ngx_log_error(NGX_LOG_CRIT, ctx->log, err,
649 ngx_read_dir_n " \"%s\" failed", tree->data);
656 len = ngx_de_namelen(&dir);
657 name = ngx_de_name(&dir);
659 ngx_log_debug2(NGX_LOG_DEBUG_CORE, ctx->log, 0,
660 "tree name %uz:\"%s\"", len, name);
662 if (len == 1 && name[0] == '.') {
666 if (len == 2 && name[0] == '.' && name[1] == '.') {
670 file.len = tree->len + 1 + len;
672 if (file.len + NGX_DIR_MASK_LEN > buf.len) {
678 buf.len = tree->len + 1 + len + NGX_DIR_MASK_LEN;
680 buf.data = ngx_alloc(buf.len + 1, ctx->log);
681 if (buf.data == NULL) {
686 p = ngx_cpymem(buf.data, tree->data, tree->len);
688 ngx_memcpy(p, name, len + 1);
690 file.data = buf.data;
692 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ctx->log, 0,
693 "tree path \"%s\"", file.data);
695 if (!dir.valid_info) {
696 if (ngx_de_info(file.data, &dir) == NGX_FILE_ERROR) {
697 ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
698 ngx_de_info_n " \"%s\" failed", file.data);
703 if (ngx_de_is_file(&dir)) {
705 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ctx->log, 0,
706 "tree file \"%s\"", file.data);
708 ctx->size = ngx_de_size(&dir);
709 ctx->access = ngx_de_access(&dir);
710 ctx->mtime = ngx_de_mtime(&dir);
712 if (ctx->file_handler(ctx, &file) == NGX_ABORT) {
716 } else if (ngx_de_is_dir(&dir)) {
718 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ctx->log, 0,
719 "tree enter dir \"%s\"", file.data);
721 ctx->access = ngx_de_access(&dir);
722 ctx->mtime = ngx_de_mtime(&dir);
724 if (ctx->pre_tree_handler(ctx, &file) == NGX_ABORT) {
728 if (ngx_walk_tree(ctx, &file) == NGX_ABORT) {
732 ctx->access = ngx_de_access(&dir);
733 ctx->mtime = ngx_de_mtime(&dir);
735 if (ctx->post_tree_handler(ctx, &file) == NGX_ABORT) {
741 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ctx->log, 0,
742 "tree special \"%s\"", file.data);
744 if (ctx->spec_handler(ctx, &file) == NGX_ABORT) {
765 if (ngx_close_dir(&dir) == NGX_ERROR) {
766 ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
767 ngx_close_dir_n " \"%s\" failed", tree->data);