glob support in include

This commit is contained in:
Igor Sysoev 2006-10-02 08:46:45 +00:00
parent 97c7e1a986
commit 97c2f469c3
11 changed files with 172 additions and 14 deletions

View File

@ -650,18 +650,52 @@ ngx_conf_read_token(ngx_conf_t *cf)
static char *
ngx_conf_include(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_str_t *value, file;
char *rv;
ngx_int_t n;
ngx_str_t *value, file;
ngx_glob_t gl;
value = cf->args->elts;
file = value[1];
ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data);
if (ngx_conf_full_name(cf->cycle, &file) == NGX_ERROR) {
return NGX_CONF_ERROR;
}
ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data);
ngx_memzero(&gl, sizeof(ngx_glob_t));
return ngx_conf_parse(cf, &file);
gl.pattern = file.data;
gl.log = cf->log;
if (ngx_open_glob(&gl) != NGX_OK) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
ngx_open_glob_n " \"%s\" failed", file.data);
return NGX_CONF_ERROR;
}
rv = NGX_CONF_OK;
for ( ;; ) {
n = ngx_read_glob(&gl, &file);
if (n != NGX_OK) {
break;
}
ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data);
rv = ngx_conf_parse(cf, &file);
if (rv != NGX_CONF_OK) {
break;
}
}
ngx_close_glob(&gl);
return rv;
}

View File

@ -205,7 +205,7 @@ ngx_init_cycle(ngx_cycle_t *old_cycle)
conf.module_type = NGX_CORE_MODULE;
conf.cmd_type = NGX_MAIN_CONF;
#if 0
#if 1
log->log_level = NGX_LOG_DEBUG_ALL;
#endif

View File

@ -253,6 +253,40 @@ ngx_open_dir(ngx_str_t *name, ngx_dir_t *dir)
}
ngx_int_t
ngx_open_glob(ngx_glob_t *gl)
{
if (glob((char *) gl->pattern, 0, NULL, &gl->pglob) == 0) {
return NGX_OK;
}
return NGX_ERROR;
}
ngx_int_t
ngx_read_glob(ngx_glob_t *gl, ngx_str_t *name)
{
if (gl->n < gl->pglob.gl_pathc) {
name->len = (size_t) ngx_strlen(gl->pglob.gl_pathv[gl->n]);
name->data = (u_char *) gl->pglob.gl_pathv[gl->n];
gl->n++;
return NGX_OK;
}
return NGX_DONE;
}
void
ngx_close_glob(ngx_glob_t *gl)
{
globfree(&gl->pglob);
}
ngx_err_t
ngx_trylock_fd(ngx_fd_t fd)
{

View File

@ -130,6 +130,20 @@ ngx_int_t ngx_open_dir(ngx_str_t *name, ngx_dir_t *dir);
#define ngx_de_mtime(dir) (dir)->info.st_mtime
typedef struct {
int n;
glob_t pglob;
u_char *pattern;
ngx_log_t *log;
} ngx_glob_t;
ngx_int_t ngx_open_glob(ngx_glob_t *gl);
#define ngx_open_glob_n "glob()"
ngx_int_t ngx_read_glob(ngx_glob_t *gl, ngx_str_t *name);
void ngx_close_glob(ngx_glob_t *gl);
ngx_err_t ngx_trylock_fd(ngx_fd_t fd);
ngx_err_t ngx_lock_fd(ngx_fd_t fd);
ngx_err_t ngx_unlock_fd(ngx_fd_t fd);

View File

@ -21,6 +21,7 @@
#include <pwd.h>
#include <grp.h>
#include <dirent.h>
#include <glob.h>
#include <sys/filio.h> /* FIONBIO */
#include <sys/uio.h>

View File

@ -27,6 +27,7 @@
#include <pwd.h>
#include <grp.h>
#include <dirent.h>
#include <glob.h>
#include <sys/uio.h>
#include <sys/stat.h>

View File

@ -37,6 +37,7 @@
#include <pwd.h>
#include <grp.h>
#include <dirent.h>
#include <glob.h>
#if (NGX_HAVE_SYS_FILIO_H)
#include <sys/filio.h> /* FIONBIO */

View File

@ -25,6 +25,7 @@
#include <pwd.h>
#include <grp.h>
#include <dirent.h>
#include <glob.h>
#include <sys/filio.h> /* FIONBIO */
#include <sys/uio.h>

View File

@ -301,7 +301,7 @@ ngx_open_dir(ngx_str_t *name, ngx_dir_t *dir)
{
ngx_cpystrn(name->data + name->len, NGX_DIR_MASK, NGX_DIR_MASK_LEN + 1);
dir->dir = FindFirstFile((const char *) name->data, &dir->fd);
dir->dir = FindFirstFile((const char *) name->data, &dir->finddata);
if (dir->dir == INVALID_HANDLE_VALUE) {
return NGX_ERROR;
@ -322,7 +322,7 @@ ngx_read_dir(ngx_dir_t *dir)
return NGX_OK;
}
if (FindNextFile(dir->dir, &dir->fd) != 0) {
if (FindNextFile(dir->dir, &dir->finddata) != 0) {
return NGX_OK;
}
@ -330,6 +330,64 @@ ngx_read_dir(ngx_dir_t *dir)
}
ngx_int_t
ngx_open_glob(ngx_glob_t *gl)
{
gl->dir = FindFirstFile((const char *) gl->pattern, &gl->finddata);
if (gl->dir == INVALID_HANDLE_VALUE) {
return NGX_ERROR;
}
gl->ready = 1;
return NGX_OK;
}
ngx_int_t
ngx_read_glob(ngx_glob_t *gl, ngx_str_t *name)
{
ngx_err_t err;
if (gl->ready) {
name->len = ngx_strlen(gl->finddata.cFileName);
name->data = (u_char *) gl->finddata.cFileName;
gl->ready = 0;
return NGX_OK;
}
if (FindNextFile(gl->dir, &gl->finddata) != 0) {
name->len = ngx_strlen(gl->finddata.cFileName);
name->data = (u_char *) gl->finddata.cFileName;
return NGX_OK;
}
err = ngx_errno;
if (err == NGX_ENOMOREFILES) {
return NGX_DONE;
}
ngx_log_error(NGX_LOG_ALERT, gl->log, err,
"FindNextFile(%s) failed", gl->pattern);
return NGX_ERROR;
}
void
ngx_close_glob(ngx_glob_t *gl)
{
if (FindClose(gl->dir) != 0) {
ngx_log_error(NGX_LOG_ALERT, gl->log, ngx_errno,
"FindClose(%s) failed", gl->pattern);
}
}
ngx_int_t
ngx_de_info(u_char *name, ngx_dir_t *dir)
{

View File

@ -141,8 +141,8 @@ ngx_int_t ngx_read_dir(ngx_dir_t *dir);
#define ngx_delete_dir_n "RemoveDirectory()"
#define ngx_de_name(dir) ((u_char *) (dir)->fd.cFileName)
#define ngx_de_namelen(dir) ngx_strlen((dir)->fd.cFileName)
#define ngx_de_name(dir) ((u_char *) (dir)->finddata.cFileName)
#define ngx_de_namelen(dir) ngx_strlen((dir)->finddata.cFileName)
ngx_int_t ngx_de_info(u_char *name, ngx_dir_t *dir);
#define ngx_de_info_n "dummy()"
@ -151,21 +151,35 @@ ngx_int_t ngx_de_link_info(u_char *name, ngx_dir_t *dir);
#define ngx_de_link_info_n "dummy()"
#define ngx_de_is_dir(dir) \
((dir)->fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
((dir)->finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
#define ngx_de_is_file(dir) \
!((dir)->fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
!((dir)->finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
#define ngx_de_is_link(dir) 0
#define ngx_de_size(dir) \
(((off_t) (dir)->fd.nFileSizeHigh << 32) | (dir)->fd.nFileSizeLow)
(((off_t) (dir)->finddata.nFileSizeHigh << 32) | (dir)->finddata.nFileSizeLow)
/* 116444736000000000 is commented in src/os/win32/ngx_time.c */
#define ngx_de_mtime(dir) \
(time_t) (((((unsigned __int64) \
(dir)->fd.ftLastWriteTime.dwHighDateTime << 32) \
| (dir)->fd.ftLastWriteTime.dwLowDateTime) \
(dir)->finddata.ftLastWriteTime.dwHighDateTime << 32) \
| (dir)->finddata.ftLastWriteTime.dwLowDateTime) \
- 116444736000000000) / 10000000)
typedef struct {
HANDLE dir;
WIN32_FIND_DATA finddata;
ngx_int_t ready;
u_char *pattern;
ngx_log_t *log;
} ngx_glob_t;
ngx_int_t ngx_open_glob(ngx_glob_t *gl);
#define ngx_open_glob_n "FindFirstFile()"
ngx_int_t ngx_read_glob(ngx_glob_t *gl, ngx_str_t *name);
void ngx_close_glob(ngx_glob_t *gl);
ssize_t ngx_read_file(ngx_file_t *file, u_char *buf, size_t size, off_t offset);

View File

@ -18,7 +18,7 @@ typedef uint64_t ngx_file_uniq_t;
typedef struct {
HANDLE dir;
WIN32_FIND_DATA fd;
WIN32_FIND_DATA finddata;
unsigned valid_info:1;
unsigned ready:1;