# HG changeset patch # User Igor Sysoev # Date 1159778805 0 # Node ID 532d15ddbe689c40e0558381b9f10cba176314c5 # Parent 7b71936d52996ac1dc7cc97c069876365e9b31b6 glob support in include diff --git a/src/core/ngx_conf_file.c b/src/core/ngx_conf_file.c --- a/src/core/ngx_conf_file.c +++ b/src/core/ngx_conf_file.c @@ -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)); + + 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); - return ngx_conf_parse(cf, &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; } diff --git a/src/core/ngx_cycle.c b/src/core/ngx_cycle.c --- a/src/core/ngx_cycle.c +++ b/src/core/ngx_cycle.c @@ -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 diff --git a/src/os/unix/ngx_files.c b/src/os/unix/ngx_files.c --- a/src/os/unix/ngx_files.c +++ b/src/os/unix/ngx_files.c @@ -253,6 +253,40 @@ ngx_open_dir(ngx_str_t *name, ngx_dir_t } +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) { diff --git a/src/os/unix/ngx_files.h b/src/os/unix/ngx_files.h --- a/src/os/unix/ngx_files.h +++ b/src/os/unix/ngx_files.h @@ -130,6 +130,20 @@ ngx_int_t ngx_open_dir(ngx_str_t *name, #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); diff --git a/src/os/unix/ngx_freebsd_config.h b/src/os/unix/ngx_freebsd_config.h --- a/src/os/unix/ngx_freebsd_config.h +++ b/src/os/unix/ngx_freebsd_config.h @@ -21,6 +21,7 @@ #include #include #include +#include #include /* FIONBIO */ #include diff --git a/src/os/unix/ngx_linux_config.h b/src/os/unix/ngx_linux_config.h --- a/src/os/unix/ngx_linux_config.h +++ b/src/os/unix/ngx_linux_config.h @@ -27,6 +27,7 @@ #include #include #include +#include #include #include diff --git a/src/os/unix/ngx_posix_config.h b/src/os/unix/ngx_posix_config.h --- a/src/os/unix/ngx_posix_config.h +++ b/src/os/unix/ngx_posix_config.h @@ -37,6 +37,7 @@ #include #include #include +#include #if (NGX_HAVE_SYS_FILIO_H) #include /* FIONBIO */ diff --git a/src/os/unix/ngx_solaris_config.h b/src/os/unix/ngx_solaris_config.h --- a/src/os/unix/ngx_solaris_config.h +++ b/src/os/unix/ngx_solaris_config.h @@ -25,6 +25,7 @@ #include #include #include +#include #include /* FIONBIO */ #include diff --git a/src/os/win32/ngx_files.c b/src/os/win32/ngx_files.c --- a/src/os/win32/ngx_files.c +++ b/src/os/win32/ngx_files.c @@ -301,7 +301,7 @@ ngx_open_dir(ngx_str_t *name, ngx_dir_t { 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; } @@ -331,6 +331,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) { return NGX_OK; diff --git a/src/os/win32/ngx_files.h b/src/os/win32/ngx_files.h --- a/src/os/win32/ngx_files.h +++ b/src/os/win32/ngx_files.h @@ -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, #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); diff --git a/src/os/win32/ngx_types.h b/src/os/win32/ngx_types.h --- a/src/os/win32/ngx_types.h +++ b/src/os/win32/ngx_types.h @@ -18,7 +18,7 @@ typedef uint64_t ngx_ typedef struct { HANDLE dir; - WIN32_FIND_DATA fd; + WIN32_FIND_DATA finddata; unsigned valid_info:1; unsigned ready:1;