changeset 727:532d15ddbe68

glob support in include
author Igor Sysoev <igor@sysoev.ru>
date Mon, 02 Oct 2006 08:46:45 +0000
parents 7b71936d5299
children e82eed614d4a
files src/core/ngx_conf_file.c src/core/ngx_cycle.c src/os/unix/ngx_files.c src/os/unix/ngx_files.h src/os/unix/ngx_freebsd_config.h src/os/unix/ngx_linux_config.h src/os/unix/ngx_posix_config.h src/os/unix/ngx_solaris_config.h src/os/win32/ngx_files.c src/os/win32/ngx_files.h src/os/win32/ngx_types.h
diffstat 11 files changed, 172 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- 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;
 }
 
 
--- 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
 
--- 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)
 {
--- 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);
--- a/src/os/unix/ngx_freebsd_config.h
+++ b/src/os/unix/ngx_freebsd_config.h
@@ -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>
--- a/src/os/unix/ngx_linux_config.h
+++ b/src/os/unix/ngx_linux_config.h
@@ -27,6 +27,7 @@
 #include <pwd.h>
 #include <grp.h>
 #include <dirent.h>
+#include <glob.h>
 
 #include <sys/uio.h>
 #include <sys/stat.h>
--- a/src/os/unix/ngx_posix_config.h
+++ b/src/os/unix/ngx_posix_config.h
@@ -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 */
--- a/src/os/unix/ngx_solaris_config.h
+++ b/src/os/unix/ngx_solaris_config.h
@@ -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>
--- 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;
--- 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);
--- 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;