Klaus Demo nginx / 97c2f46
glob support in include Igor Sysoev 15 years ago
11 changed file(s) with 173 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
649649 static char *
650650 ngx_conf_include(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
651651 {
652 ngx_str_t *value, file;
652 char *rv;
653 ngx_int_t n;
654 ngx_str_t *value, file;
655 ngx_glob_t gl;
653656
654657 value = cf->args->elts;
655658 file = value[1];
656659
660 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data);
661
657662 if (ngx_conf_full_name(cf->cycle, &file) == NGX_ERROR) {
658663 return NGX_CONF_ERROR;
659664 }
660665
661 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data);
662
663 return ngx_conf_parse(cf, &file);
666 ngx_memzero(&gl, sizeof(ngx_glob_t));
667
668 gl.pattern = file.data;
669 gl.log = cf->log;
670
671 if (ngx_open_glob(&gl) != NGX_OK) {
672 ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
673 ngx_open_glob_n " \"%s\" failed", file.data);
674 return NGX_CONF_ERROR;
675 }
676
677 rv = NGX_CONF_OK;
678
679 for ( ;; ) {
680 n = ngx_read_glob(&gl, &file);
681
682 if (n != NGX_OK) {
683 break;
684 }
685
686 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data);
687
688 rv = ngx_conf_parse(cf, &file);
689
690 if (rv != NGX_CONF_OK) {
691 break;
692 }
693 }
694
695 ngx_close_glob(&gl);
696
697 return rv;
664698 }
665699
666700
204204 conf.module_type = NGX_CORE_MODULE;
205205 conf.cmd_type = NGX_MAIN_CONF;
206206
207 #if 0
207 #if 1
208208 log->log_level = NGX_LOG_DEBUG_ALL;
209209 #endif
210210
252252 }
253253
254254
255 ngx_int_t
256 ngx_open_glob(ngx_glob_t *gl)
257 {
258 if (glob((char *) gl->pattern, 0, NULL, &gl->pglob) == 0) {
259 return NGX_OK;
260 }
261
262 return NGX_ERROR;
263 }
264
265
266 ngx_int_t
267 ngx_read_glob(ngx_glob_t *gl, ngx_str_t *name)
268 {
269 if (gl->n < gl->pglob.gl_pathc) {
270
271 name->len = (size_t) ngx_strlen(gl->pglob.gl_pathv[gl->n]);
272 name->data = (u_char *) gl->pglob.gl_pathv[gl->n];
273 gl->n++;
274
275 return NGX_OK;
276 }
277
278 return NGX_DONE;
279 }
280
281
282 void
283 ngx_close_glob(ngx_glob_t *gl)
284 {
285 globfree(&gl->pglob);
286 }
287
288
255289 ngx_err_t
256290 ngx_trylock_fd(ngx_fd_t fd)
257291 {
129129 #define ngx_de_mtime(dir) (dir)->info.st_mtime
130130
131131
132 typedef struct {
133 int n;
134 glob_t pglob;
135 u_char *pattern;
136 ngx_log_t *log;
137 } ngx_glob_t;
138
139
140 ngx_int_t ngx_open_glob(ngx_glob_t *gl);
141 #define ngx_open_glob_n "glob()"
142 ngx_int_t ngx_read_glob(ngx_glob_t *gl, ngx_str_t *name);
143 void ngx_close_glob(ngx_glob_t *gl);
144
145
132146 ngx_err_t ngx_trylock_fd(ngx_fd_t fd);
133147 ngx_err_t ngx_lock_fd(ngx_fd_t fd);
134148 ngx_err_t ngx_unlock_fd(ngx_fd_t fd);
2020 #include <pwd.h>
2121 #include <grp.h>
2222 #include <dirent.h>
23 #include <glob.h>
2324
2425 #include <sys/filio.h> /* FIONBIO */
2526 #include <sys/uio.h>
2626 #include <pwd.h>
2727 #include <grp.h>
2828 #include <dirent.h>
29 #include <glob.h>
2930
3031 #include <sys/uio.h>
3132 #include <sys/stat.h>
3636 #include <pwd.h>
3737 #include <grp.h>
3838 #include <dirent.h>
39 #include <glob.h>
3940
4041 #if (NGX_HAVE_SYS_FILIO_H)
4142 #include <sys/filio.h> /* FIONBIO */
2424 #include <pwd.h>
2525 #include <grp.h>
2626 #include <dirent.h>
27 #include <glob.h>
2728
2829 #include <sys/filio.h> /* FIONBIO */
2930 #include <sys/uio.h>
300300 {
301301 ngx_cpystrn(name->data + name->len, NGX_DIR_MASK, NGX_DIR_MASK_LEN + 1);
302302
303 dir->dir = FindFirstFile((const char *) name->data, &dir->fd);
303 dir->dir = FindFirstFile((const char *) name->data, &dir->finddata);
304304
305305 if (dir->dir == INVALID_HANDLE_VALUE) {
306306 return NGX_ERROR;
321321 return NGX_OK;
322322 }
323323
324 if (FindNextFile(dir->dir, &dir->fd) != 0) {
324 if (FindNextFile(dir->dir, &dir->finddata) != 0) {
325325 return NGX_OK;
326326 }
327327
328328 return NGX_ERROR;
329 }
330
331
332 ngx_int_t
333 ngx_open_glob(ngx_glob_t *gl)
334 {
335 gl->dir = FindFirstFile((const char *) gl->pattern, &gl->finddata);
336
337 if (gl->dir == INVALID_HANDLE_VALUE) {
338 return NGX_ERROR;
339 }
340
341 gl->ready = 1;
342
343 return NGX_OK;
344 }
345
346
347 ngx_int_t
348 ngx_read_glob(ngx_glob_t *gl, ngx_str_t *name)
349 {
350 ngx_err_t err;
351
352 if (gl->ready) {
353 name->len = ngx_strlen(gl->finddata.cFileName);
354 name->data = (u_char *) gl->finddata.cFileName;
355
356 gl->ready = 0;
357 return NGX_OK;
358 }
359
360 if (FindNextFile(gl->dir, &gl->finddata) != 0) {
361 name->len = ngx_strlen(gl->finddata.cFileName);
362 name->data = (u_char *) gl->finddata.cFileName;
363
364 return NGX_OK;
365 }
366
367 err = ngx_errno;
368
369 if (err == NGX_ENOMOREFILES) {
370 return NGX_DONE;
371 }
372
373 ngx_log_error(NGX_LOG_ALERT, gl->log, err,
374 "FindNextFile(%s) failed", gl->pattern);
375
376 return NGX_ERROR;
377 }
378
379
380 void
381 ngx_close_glob(ngx_glob_t *gl)
382 {
383 if (FindClose(gl->dir) != 0) {
384 ngx_log_error(NGX_LOG_ALERT, gl->log, ngx_errno,
385 "FindClose(%s) failed", gl->pattern);
386 }
329387 }
330388
331389
140140 #define ngx_delete_dir_n "RemoveDirectory()"
141141
142142
143 #define ngx_de_name(dir) ((u_char *) (dir)->fd.cFileName)
144 #define ngx_de_namelen(dir) ngx_strlen((dir)->fd.cFileName)
143 #define ngx_de_name(dir) ((u_char *) (dir)->finddata.cFileName)
144 #define ngx_de_namelen(dir) ngx_strlen((dir)->finddata.cFileName)
145145
146146 ngx_int_t ngx_de_info(u_char *name, ngx_dir_t *dir);
147147 #define ngx_de_info_n "dummy()"
150150 #define ngx_de_link_info_n "dummy()"
151151
152152 #define ngx_de_is_dir(dir) \
153 ((dir)->fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
153 ((dir)->finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
154154 #define ngx_de_is_file(dir) \
155 !((dir)->fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
155 !((dir)->finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
156156 #define ngx_de_is_link(dir) 0
157157 #define ngx_de_size(dir) \
158 (((off_t) (dir)->fd.nFileSizeHigh << 32) | (dir)->fd.nFileSizeLow)
158 (((off_t) (dir)->finddata.nFileSizeHigh << 32) | (dir)->finddata.nFileSizeLow)
159159
160160 /* 116444736000000000 is commented in src/os/win32/ngx_time.c */
161161
162162 #define ngx_de_mtime(dir) \
163163 (time_t) (((((unsigned __int64) \
164 (dir)->fd.ftLastWriteTime.dwHighDateTime << 32) \
165 | (dir)->fd.ftLastWriteTime.dwLowDateTime) \
164 (dir)->finddata.ftLastWriteTime.dwHighDateTime << 32) \
165 | (dir)->finddata.ftLastWriteTime.dwLowDateTime) \
166166 - 116444736000000000) / 10000000)
167167
168 typedef struct {
169 HANDLE dir;
170 WIN32_FIND_DATA finddata;
171 ngx_int_t ready;
172 u_char *pattern;
173 ngx_log_t *log;
174 } ngx_glob_t;
175
176
177 ngx_int_t ngx_open_glob(ngx_glob_t *gl);
178 #define ngx_open_glob_n "FindFirstFile()"
179
180 ngx_int_t ngx_read_glob(ngx_glob_t *gl, ngx_str_t *name);
181 void ngx_close_glob(ngx_glob_t *gl);
168182
169183
170184 ssize_t ngx_read_file(ngx_file_t *file, u_char *buf, size_t size, off_t offset);
1717
1818 typedef struct {
1919 HANDLE dir;
20 WIN32_FIND_DATA fd;
20 WIN32_FIND_DATA finddata;
2121
2222 unsigned valid_info:1;
2323 unsigned ready:1;