glob support in include
Igor Sysoev
15 years ago
649 | 649 | static char * |
650 | 650 | ngx_conf_include(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
651 | 651 | { |
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; | |
653 | 656 | |
654 | 657 | value = cf->args->elts; |
655 | 658 | file = value[1]; |
656 | 659 | |
660 | ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data); | |
661 | ||
657 | 662 | if (ngx_conf_full_name(cf->cycle, &file) == NGX_ERROR) { |
658 | 663 | return NGX_CONF_ERROR; |
659 | 664 | } |
660 | 665 | |
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; | |
664 | 698 | } |
665 | 699 | |
666 | 700 |
204 | 204 | conf.module_type = NGX_CORE_MODULE; |
205 | 205 | conf.cmd_type = NGX_MAIN_CONF; |
206 | 206 | |
207 | #if 0 | |
207 | #if 1 | |
208 | 208 | log->log_level = NGX_LOG_DEBUG_ALL; |
209 | 209 | #endif |
210 | 210 |
252 | 252 | } |
253 | 253 | |
254 | 254 | |
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 | ||
255 | 289 | ngx_err_t |
256 | 290 | ngx_trylock_fd(ngx_fd_t fd) |
257 | 291 | { |
129 | 129 | #define ngx_de_mtime(dir) (dir)->info.st_mtime |
130 | 130 | |
131 | 131 | |
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 | ||
132 | 146 | ngx_err_t ngx_trylock_fd(ngx_fd_t fd); |
133 | 147 | ngx_err_t ngx_lock_fd(ngx_fd_t fd); |
134 | 148 | ngx_err_t ngx_unlock_fd(ngx_fd_t fd); |
20 | 20 | #include <pwd.h> |
21 | 21 | #include <grp.h> |
22 | 22 | #include <dirent.h> |
23 | #include <glob.h> | |
23 | 24 | |
24 | 25 | #include <sys/filio.h> /* FIONBIO */ |
25 | 26 | #include <sys/uio.h> |
26 | 26 | #include <pwd.h> |
27 | 27 | #include <grp.h> |
28 | 28 | #include <dirent.h> |
29 | #include <glob.h> | |
29 | 30 | |
30 | 31 | #include <sys/uio.h> |
31 | 32 | #include <sys/stat.h> |
36 | 36 | #include <pwd.h> |
37 | 37 | #include <grp.h> |
38 | 38 | #include <dirent.h> |
39 | #include <glob.h> | |
39 | 40 | |
40 | 41 | #if (NGX_HAVE_SYS_FILIO_H) |
41 | 42 | #include <sys/filio.h> /* FIONBIO */ |
24 | 24 | #include <pwd.h> |
25 | 25 | #include <grp.h> |
26 | 26 | #include <dirent.h> |
27 | #include <glob.h> | |
27 | 28 | |
28 | 29 | #include <sys/filio.h> /* FIONBIO */ |
29 | 30 | #include <sys/uio.h> |
300 | 300 | { |
301 | 301 | ngx_cpystrn(name->data + name->len, NGX_DIR_MASK, NGX_DIR_MASK_LEN + 1); |
302 | 302 | |
303 | dir->dir = FindFirstFile((const char *) name->data, &dir->fd); | |
303 | dir->dir = FindFirstFile((const char *) name->data, &dir->finddata); | |
304 | 304 | |
305 | 305 | if (dir->dir == INVALID_HANDLE_VALUE) { |
306 | 306 | return NGX_ERROR; |
321 | 321 | return NGX_OK; |
322 | 322 | } |
323 | 323 | |
324 | if (FindNextFile(dir->dir, &dir->fd) != 0) { | |
324 | if (FindNextFile(dir->dir, &dir->finddata) != 0) { | |
325 | 325 | return NGX_OK; |
326 | 326 | } |
327 | 327 | |
328 | 328 | 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 | } | |
329 | 387 | } |
330 | 388 | |
331 | 389 |
140 | 140 | #define ngx_delete_dir_n "RemoveDirectory()" |
141 | 141 | |
142 | 142 | |
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) | |
145 | 145 | |
146 | 146 | ngx_int_t ngx_de_info(u_char *name, ngx_dir_t *dir); |
147 | 147 | #define ngx_de_info_n "dummy()" |
150 | 150 | #define ngx_de_link_info_n "dummy()" |
151 | 151 | |
152 | 152 | #define ngx_de_is_dir(dir) \ |
153 | ((dir)->fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) | |
153 | ((dir)->finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) | |
154 | 154 | #define ngx_de_is_file(dir) \ |
155 | !((dir)->fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) | |
155 | !((dir)->finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) | |
156 | 156 | #define ngx_de_is_link(dir) 0 |
157 | 157 | #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) | |
159 | 159 | |
160 | 160 | /* 116444736000000000 is commented in src/os/win32/ngx_time.c */ |
161 | 161 | |
162 | 162 | #define ngx_de_mtime(dir) \ |
163 | 163 | (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) \ | |
166 | 166 | - 116444736000000000) / 10000000) |
167 | 167 | |
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); | |
168 | 182 | |
169 | 183 | |
170 | 184 | ssize_t ngx_read_file(ngx_file_t *file, u_char *buf, size_t size, off_t offset); |