如何正确使用ngx_write_chain_to_temp_file?

时间:2014-09-22 10:51:34

标签: file-io nginx

我正在编写构造nginx链的nginx模块,然后将此链缓冲区写入nginx临时文件以便稍后使用(刚刚写入后)。我一直在搜索每一页,唯一的解决方案是下面的一个:

// Create temp file to test
            ngx_temp_file_t *tf;
            tf = ngx_pcalloc(r->pool, sizeof (ngx_temp_file_t));
            if (tf == NULL) {
                return NGX_HTTP_INTERNAL_SERVER_ERROR;
            }
            tf->file.fd = NGX_INVALID_FILE;
            tf->file.log = nlog;
            tf->path = clcf->client_body_temp_path;
            tf->pool = r->pool;
            tf->log_level = r->request_body_file_log_level;
            tf->persistent = r->request_body_in_persistent_file;
            tf->clean = r->request_body_in_clean_file;
            //        if (r->request_body_file_group_access) {
            //            tf->access = 0660;
            //        }
            if (ngx_create_temp_file(&tf->file, tf->path, tf->pool, tf->persistent, tf->clean, tf->access) != NGX_OK) {
                return NGX_HTTP_INTERNAL_SERVER_ERROR;
            }

            if (ngx_write_chain_to_temp_file(tf, bucket->first) == NGX_ERROR) {
                return NGX_HTTP_INTERNAL_SERVER_ERROR;
            }

此代码不返回NGX_ERROR,这是否意味着nginx成功将临时文件写入client_body_temporay_path?答案是肯定的,之后,我使用fopen打开文件,文件不存在?

有谁能请给我正确的解决方案来处理ngx_write_chain_to_temp_file?

1 个答案:

答案 0 :(得分:1)

我发现自己是解决方案

        ngx_temp_file_t *tf;
        tf = ngx_pcalloc(r->pool, sizeof (ngx_temp_file_t));
        tf->file.fd = NGX_INVALID_FILE;
        tf->file.log = nlog;
        tf->path = clcf->client_body_temp_path;
        tf->pool = r->pool;
        tf->persistent = 1;
        rc = ngx_create_temp_file(&tf->file, tf->path, tf->pool, tf->persistent, tf->clean, tf->access);
        //ngx_write_chain_to_file(&tf->file, bucket->first, bucket->content_length, r->pool);
        ngx_write_chain_to_temp_file(tf, bucket->first);

我唯一无法理解的是,如果我将tf->persistent设置为false(0),在创建文件之后,即使我还没有通过对output_filter的响应,我也无法读取它。