vislee / leevis.com

Blog
87 stars 13 forks source link

nginx中的路径和文件 #134

Open vislee opened 6 years ago

vislee commented 6 years ago

概述

nginx中使用文件的地方有配置文件和日志、临时文件。实现在ngx_files.h|c ngx_file.c文件中。 临时文件在nginx中用来临时保存body,在ngx_http_write_request_body函数中使用。

实现

// 路径,一般通过ngx_conf_set_path_slot函数设置
typedef struct {
    ngx_str_t                  name;  // 路径的名字
    size_t                     len;    // 路径下子目录的长度,包括分隔符。例如/ab/cde 长度是7
    size_t                     level[NGX_MAX_PATH_LEVEL];    // 路径下的子目录,最多3级:2 3。数组内容是每一级子目录路径长度 例如 2 3 可能的子目录是 ./ab/cde/ 

    ngx_path_manager_pt        manager;  // 定时处理,有单独的进程调用。
    ngx_path_purger_pt         purger;
    ngx_path_loader_pt         loader;    // 启动时加载,有单独的进程调用。
    void                      *data;

    u_char                    *conf_file;    // 对应的配置文件
    ngx_uint_t                 line;           // 对应的配置文件的行数
} ngx_path_t;

struct ngx_file_s {
    ngx_fd_t                   fd;
    ngx_str_t                  name;
    ngx_file_info_t            info;

    off_t                      offset;
    off_t                      sys_offset;

    ngx_log_t                 *log;

#if (NGX_THREADS || NGX_COMPAT)
    ngx_int_t                (*thread_handler)(ngx_thread_task_t *task,
                                               ngx_file_t *file);
    void                      *thread_ctx;
    ngx_thread_task_t         *thread_task;
#endif

#if (NGX_HAVE_FILE_AIO || NGX_COMPAT)
    ngx_event_aio_t           *aio;
#endif

    unsigned                   valid_info:1;
    unsigned                   directio:1;
};

// 临时文件结构体定义
typedef struct {
    ngx_file_t                 file;
    off_t                      offset;    // 临时文件大小偏移
    ngx_path_t                *path;  // 临时文件路径
    ngx_pool_t                *pool;
    char                      *warn;    // 日志输出使用

    ngx_uint_t                 access;    // 文件权限

    unsigned                   log_level:8;    日志级别
    unsigned                   persistent:1;  // 是否保留文件
    unsigned                   clean:1;    // 是否清理
    unsigned                   thread_write:1;
} ngx_temp_file_t;

// 创建临时文件 实现在ngx_file.c文件中
ngx_int_t
ngx_create_temp_file(ngx_file_t *file, ngx_path_t *path, ngx_pool_t *pool,
    ngx_uint_t persistent, ngx_uint_t clean, ngx_uint_t access)
{
    size_t                    levels;
    u_char                   *p;
    uint32_t                  n;
    ngx_err_t                 err;
    ngx_str_t                 name;
    ngx_uint_t                prefix;
    ngx_pool_cleanup_t       *cln;
    ngx_pool_cleanup_file_t  *clnf;

    if (file->name.len) {
        // 指定了文件名字
        name = file->name;
        levels = 0;  // 如果指定了文件名,就不需要子目录了
        prefix = 1;

    } else {
        // 没有指定文件名
        name = path->name;
        levels = path->len;  // 子目录
        prefix = 0;
    }

    // 临时文件的长度 = 路径长度 + 1位的分隔符(/)+ 子目录长度 + 临时文件名长度
    file->name.len = name.len + 1 + levels + 10;

    file->name.data = ngx_pnalloc(pool, file->name.len + 1);  // +1位结束符号
    if (file->name.data == NULL) {
        return NGX_ERROR;
    }

#if 0
    for (i = 0; i < file->name.len; i++) {
        file->name.data[i] = 'X';
    }
#endif

    p = ngx_cpymem(file->name.data, name.data, name.len);

    if (prefix) {
        *p = '.';
    }

    p += 1 + levels;    // 跳过子目录,子目录还没赋值。

    n = (uint32_t) ngx_next_temp_number(0);

    cln = ngx_pool_cleanup_add(pool, sizeof(ngx_pool_cleanup_file_t));
    if (cln == NULL) {
        return NGX_ERROR;
    }

    for ( ;; ) {
        //  临时文件名 10位
        (void) ngx_sprintf(p, "%010uD%Z", n);

        if (!prefix) {
            // 根据子目录配置情况填充子目录,从文件名后面复制对应level数组下标的长度的字符串进行填充
            ngx_create_hashed_filename(path, file->name.data, file->name.len);
        }

        ngx_log_debug1(NGX_LOG_DEBUG_CORE, file->log, 0,
                       "hashed path: %s", file->name.data);

        file->fd = ngx_open_tempfile(file->name.data, persistent, access);

        ngx_log_debug1(NGX_LOG_DEBUG_CORE, file->log, 0,
                       "temp fd:%d", file->fd);

        if (file->fd != NGX_INVALID_FILE) {
            // clean  关闭并删除文件  否则仅关闭文件
            cln->handler = clean ? ngx_pool_delete_file : ngx_pool_cleanup_file;
            clnf = cln->data;

            clnf->fd = file->fd;
            clnf->name = file->name.data;
            clnf->log = pool->log;

            return NGX_OK;
        }

        err = ngx_errno;

        if (err == NGX_EEXIST_FILE) {
            n = (uint32_t) ngx_next_temp_number(1);
            continue;
        }

        if ((path->level[0] == 0) || (err != NGX_ENOPATH)) {
            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;
        }
    }
}

// 设置文件访问时间和修改时间
ngx_int_t
ngx_set_file_time(u_char *name, ngx_fd_t fd, time_t s)
{
    struct timeval  tv[2];

    // 访问时间
    tv[0].tv_sec = ngx_time();
    tv[0].tv_usec = 0;
    // 修改时间
    tv[1].tv_sec = s;
    tv[1].tv_usec = 0;

    if (utimes((char *) name, tv) != -1) {
        return NGX_OK;
    }

    return NGX_ERROR;
}

// 写入临时文件
ssize_t
ngx_write_chain_to_file(ngx_file_t *file, ngx_chain_t *cl, off_t offset,
    ngx_pool_t *pool)
{
    ssize_t        total, n;
    ngx_iovec_t    vec;
    struct iovec   iovs[NGX_IOVS_PREALLOCATE];

    /* use pwrite() if there is the only buf in a chain */

    if (cl->next == NULL) {
        return ngx_write_file(file, cl->buf->pos,
                              (size_t) (cl->buf->last - cl->buf->pos),
                              offset);
    }

    total = 0;

    vec.iovs = iovs;
    vec.nalloc = NGX_IOVS_PREALLOCATE;

    do {
        /* create the iovec and coalesce the neighbouring bufs */
        cl = ngx_chain_to_iovec(&vec, cl);

        /* use pwrite() if there is the only iovec buffer */

        if (vec.count == 1) {
            n = ngx_write_file(file, (u_char *) iovs[0].iov_base,
                               iovs[0].iov_len, offset);

            if (n == NGX_ERROR) {
                return n;
            }

            return total + n;
        }
        // 使用writev函数把多块内存写入文件
        n = ngx_writev_file(file, &vec, offset);

        if (n == NGX_ERROR) {
            return n;
        }

        offset += n;
        total += n;

    } while (cl);

    return total;
}

// 路径结构体添加到cycle->paths 数组中
// nginx 所有打开的文件,所有需要管理的路径,需要建立的tcp连接都会添加到cycle这个结构体做统一管理。这样可以有效防止资源泄漏。
ngx_int_t
ngx_add_path(ngx_conf_t *cf, ngx_path_t **slot)
{
    ngx_uint_t   i, n;
    ngx_path_t  *path, **p;

    path = *slot;

    p = cf->cycle->paths.elts;
    for (i = 0; i < cf->cycle->paths.nelts; i++) {
        if (p[i]->name.len == path->name.len
            && ngx_strcmp(p[i]->name.data, path->name.data) == 0)
        {
            if (p[i]->data != path->data) {
                ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                                   "the same path name \"%V\" "
                                   "used in %s:%ui and",
                                   &p[i]->name, p[i]->conf_file, p[i]->line);
                return NGX_ERROR;
            }

            for (n = 0; n < NGX_MAX_PATH_LEVEL; n++) {
                if (p[i]->level[n] != path->level[n]) {
                    // conf_file 为NULL,说明该路径不是配置文件配置产生的
                    if (path->conf_file == NULL) {
                        if (p[i]->conf_file == NULL) {
                            ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
                                      "the default path name \"%V\" has "
                                      "the same name as another default path, "
                                      "but the different levels, you need to "
                                      "redefine one of them in http section",
                                      &p[i]->name);
                            return NGX_ERROR;
                        }

                        ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
                                      "the path name \"%V\" in %s:%ui has "
                                      "the same name as default path, but "
                                      "the different levels, you need to "
                                      "define default path in http section",
                                      &p[i]->name, p[i]->conf_file, p[i]->line);
                        return NGX_ERROR;
                    }

                    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                                      "the same path name \"%V\" in %s:%ui "
                                      "has the different levels than",
                                      &p[i]->name, p[i]->conf_file, p[i]->line);
                    return NGX_ERROR;
                }

                // level为0是默认的。自路径结束退出循环。
                if (p[i]->level[n] == 0) {
                    break;
                }
            }

            *slot = p[i];

            return NGX_OK;
        }
    }

    // 添加路径到cycle->paths 数组中
    p = ngx_array_push(&cf->cycle->paths);
    if (p == NULL) {
        return NGX_ERROR;
    }

    *p = path;

    return NGX_OK;
}