From e9f9ee23fa20bfe662251bc74ec3a4ce06bc5320 Mon Sep 17 00:00:00 2001 From: xiyuan Date: Wed, 10 May 2017 22:11:01 +0800 Subject: [PATCH 01/10] =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- php_yaconf.h | 3 + yaconf.c | 233 +++++++++++++++++++++++++++++---------------------- 2 files changed, 138 insertions(+), 98 deletions(-) diff --git a/php_yaconf.h b/php_yaconf.h index 305a2a4..a118cb8 100644 --- a/php_yaconf.h +++ b/php_yaconf.h @@ -45,6 +45,9 @@ extern zend_module_entry yaconf_module_entry; #define YACONF_DEBUG(m) #endif + +#define MAX_SEARCH_DIR_SIZE 10240 + ZEND_BEGIN_MODULE_GLOBALS(yaconf) char *directory; int parse_err; diff --git a/yaconf.c b/yaconf.c index 8faa0b6..ef71b08 100644 --- a/yaconf.c +++ b/yaconf.c @@ -473,7 +473,12 @@ PHP_GINIT_FUNCTION(yaconf) */ PHP_MINIT_FUNCTION(yaconf) { + char *(*search_dir_names)[MAX_SEARCH_DIR_SIZE]; + int search_dir_size=0; + char *cur_dir_name = NULL; + const char *dirname; + int dirname_len = 0; size_t dirlen; zend_class_entry ce; struct zend_stat dir_sb = {0}; @@ -492,69 +497,83 @@ PHP_MINIT_FUNCTION(yaconf) zval result; int ndir; struct dirent **namelist; - char *p, ini_file[MAXPATHLEN]; + char *p, ini_file[MAXPATHLEN],ini_file_len=0; #ifndef ZTS YACONF_G(directory_mtime) = dir_sb.st_mtime; #endif - if ((ndir = php_scandir(dirname, &namelist, 0, php_alphasort)) > 0) { - uint32_t i; - unsigned char c; - struct zend_stat sb; - zend_file_handle fh = {0}; + dirname_len = strlen(dirname); + search_dir_names[search_dir_size++] = estrdup(dirname); + while( search_dir_size > 0 ){ + cur_dir_name = search_dir_names[--search_dir_size]; - PALLOC_HASHTABLE(ini_containers); - zend_hash_init(ini_containers, ndir, NULL, NULL, 1); + if ((ndir = php_scandir(cur_dir_name, &namelist, 0, php_alphasort)) > 0) { + uint32_t i; + unsigned char c; + struct zend_stat sb; + zend_file_handle fh = {0}; - PALLOC_HASHTABLE(parsed_ini_files); - zend_hash_init(parsed_ini_files, ndir, NULL, NULL, 1); + PALLOC_HASHTABLE(ini_containers); + zend_hash_init(ini_containers, ndir, NULL, NULL, 1); - for (i = 0; i < ndir; i++) { - if (!(p = strrchr(namelist[i]->d_name, '.')) || strcmp(p, ".ini")) { - free(namelist[i]); - continue; - } + PALLOC_HASHTABLE(parsed_ini_files); + zend_hash_init(parsed_ini_files, ndir, NULL, NULL, 1); + + for (i = 0; i < ndir; i++) { + if (!(p = strrchr(namelist[i]->d_name, '.')) || strcmp(p, ".ini")) { + free(namelist[i]); + continue; + } - snprintf(ini_file, MAXPATHLEN, "%s%c%s", dirname, DEFAULT_SLASH, namelist[i]->d_name); - - if (VCWD_STAT(ini_file, &sb) == 0) { - if (S_ISREG(sb.st_mode)) { - yaconf_filenode node; - if ((fh.handle.fp = VCWD_FOPEN(ini_file, "r"))) { - fh.filename = ini_file; - fh.type = ZEND_HANDLE_FP; - ZVAL_UNDEF(&active_ini_file_section); - YACONF_G(parse_err) = 0; - php_yaconf_hash_init(&result, 128); - if (zend_parse_ini_file(&fh, 1, 0 /* ZEND_INI_SCANNER_NORMAL */, - php_yaconf_ini_parser_cb, (void *)&result) == FAILURE || YACONF_G(parse_err)) { + ini_file_len = snprintf(ini_file, MAXPATHLEN, "%s%c%s", cur_dir_name, DEFAULT_SLASH, namelist[i]->d_name); + + if (ini_file_len < 0 || VCWD_STAT(ini_file, &sb) == 0) { + if (S_ISREG(sb.st_mode)) { + yaconf_filenode node; + if ((fh.handle.fp = VCWD_FOPEN(ini_file, "r"))) { + fh.filename = ini_file; + fh.type = ZEND_HANDLE_FP; + ZVAL_UNDEF(&active_ini_file_section); YACONF_G(parse_err) = 0; - php_yaconf_hash_destroy(Z_ARRVAL(result)); - free(namelist[i]); - continue; + php_yaconf_hash_init(&result, 128); + if (zend_parse_ini_file(&fh, 1, 0 /* ZEND_INI_SCANNER_NORMAL */, + php_yaconf_ini_parser_cb, (void *)&result) == FAILURE || YACONF_G(parse_err)) { + YACONF_G(parse_err) = 0; + php_yaconf_hash_destroy(Z_ARRVAL(result)); + free(namelist[i]); + continue; + } + } + + php_yaconf_symtable_update(ini_containers, + php_yaconf_str_persistent(ini_file+dirname_len+1, ini_file_len - dirname_len - 1 - strlen(p) ), &result); + + node.filename = zend_string_init(ini_file+dirname_len+1, ini_file_len - dirname_len - 1 , 1); + node.mtime = sb.st_mtime; + zend_hash_update_mem(parsed_ini_files, node.filename, &node, sizeof(yaconf_filenode)); + }else if(S_IFDIR(sb.st_mode) ){ + if(search_dir_size >= MAX_SEARCH_DIR_SIZE){ + php_error(E_ERROR, "max dir size '%d'", search_dir_size); + }else{ + search_dir_names[search_dir_size++] = estrdup(ini_file); } } - - php_yaconf_symtable_update(ini_containers, - php_yaconf_str_persistent(namelist[i]->d_name, p - namelist[i]->d_name), &result); - - node.filename = zend_string_init(namelist[i]->d_name, strlen(namelist[i]->d_name), 1); - node.mtime = sb.st_mtime; - zend_hash_update_mem(parsed_ini_files, node.filename, &node, sizeof(yaconf_filenode)); + } else { + php_error(E_ERROR, "Could not stat '%s'", ini_file); } - } else { - php_error(E_ERROR, "Could not stat '%s'", ini_file); + free(namelist[i]); } - free(namelist[i]); + free(namelist); + } else { + php_error(E_ERROR, "Couldn't opendir '%s'", dirname); } -#ifndef ZTS - YACONF_G(last_check) = time(NULL); -#endif - free(namelist); - } else { - php_error(E_ERROR, "Couldn't opendir '%s'", dirname); + + efree(cur_dir_name); } + #ifndef ZTS + YACONF_G(last_check) = time(NULL); + #endif } return SUCCESS; @@ -583,72 +602,90 @@ PHP_RINIT_FUNCTION(yaconf) zval result; int i, ndir; struct dirent **namelist; - char *p, ini_file[MAXPATHLEN]; + char *p, ini_file[MAXPATHLEN],ini_file_len=0; + + char *(*search_dir_names)[MAX_SEARCH_DIR_SIZE]; + int search_dir_size=0; + char *cur_dir_name = NULL; YACONF_G(directory_mtime) = dir_sb.st_mtime; - if ((ndir = php_scandir(dirname, &namelist, 0, php_alphasort)) > 0) { - zend_string *file_key; - struct zend_stat sb; - zend_file_handle fh = {0}; - yaconf_filenode *node = NULL; + dirname_len = strlen(dirname); + search_dir_names[search_dir_size++] = estrdup(dirname); + while( search_dir_size > 0 ){ + cur_dir_name = search_dir_names[--search_dir_size]; - for (i = 0; i < ndir; i++) { - zval *orig_ht = NULL; - if (!(p = strrchr(namelist[i]->d_name, '.')) || strcmp(p, ".ini")) { - free(namelist[i]); - continue; - } + if ((ndir = php_scandir(cur_dir_name, &namelist, 0, php_alphasort)) > 0) { + zend_string *file_key; + struct zend_stat sb; + zend_file_handle fh = {0}; + yaconf_filenode *node = NULL; - snprintf(ini_file, MAXPATHLEN, "%s%c%s", dirname, DEFAULT_SLASH, namelist[i]->d_name); - if (VCWD_STAT(ini_file, &sb) || !S_ISREG(sb.st_mode)) { - free(namelist[i]); - continue; - } + for (i = 0; i < ndir; i++) { + zval *orig_ht = NULL; + if (!(p = strrchr(namelist[i]->d_name, '.')) || strcmp(p, ".ini")) { + free(namelist[i]); + continue; + } - if ((node = (yaconf_filenode*)zend_hash_str_find_ptr(parsed_ini_files, namelist[i]->d_name, strlen(namelist[i]->d_name))) == NULL) { - YACONF_DEBUG("new configure file found"); - } else if (node->mtime == sb.st_mtime) { - free(namelist[i]); - continue; - } + ini_file_len = snprintf(ini_file, MAXPATHLEN, "%s%c%s", cur_dir_name, DEFAULT_SLASH, namelist[i]->d_name); + if (ini_file_len < 0 || VCWD_STAT(ini_file, &sb) || !S_ISREG(sb.st_mode)) { + if(S_IFDIR(sb.st_mode) ){ + if(search_dir_size >= MAX_SEARCH_DIR_SIZE){ + php_error(E_ERROR, "max dir size '%d'", search_dir_size); + }else{ + search_dir_names[search_dir_size++] = estrdup(ini_file); + } + } + free(namelist[i]); + continue; + } - if ((fh.handle.fp = VCWD_FOPEN(ini_file, "r"))) { - fh.filename = ini_file; - fh.type = ZEND_HANDLE_FP; - ZVAL_UNDEF(&active_ini_file_section); - YACONF_G(parse_err) = 0; - php_yaconf_hash_init(&result, 128); - if (zend_parse_ini_file(&fh, 1, 0 /* ZEND_INI_SCANNER_NORMAL */, - php_yaconf_ini_parser_cb, (void *)&result) == FAILURE || YACONF_G(parse_err)) { - YACONF_G(parse_err) = 0; - php_yaconf_hash_destroy(Z_ARRVAL(result)); + if ((node = (yaconf_filenode*)zend_hash_str_find_ptr(parsed_ini_files, ini_file+dirname_len+1, ini_file_len - dirname_len - 1)) == NULL) { + YACONF_DEBUG("new configure file found"); + } else if (node->mtime == sb.st_mtime) { free(namelist[i]); continue; } - } + if ((fh.handle.fp = VCWD_FOPEN(ini_file, "r"))) { + fh.filename = ini_file; + fh.type = ZEND_HANDLE_FP; + ZVAL_UNDEF(&active_ini_file_section); + YACONF_G(parse_err) = 0; + php_yaconf_hash_init(&result, 128); + if (zend_parse_ini_file(&fh, 1, 0 /* ZEND_INI_SCANNER_NORMAL */, + php_yaconf_ini_parser_cb, (void *)&result) == FAILURE || YACONF_G(parse_err)) { + YACONF_G(parse_err) = 0; + php_yaconf_hash_destroy(Z_ARRVAL(result)); + free(namelist[i]); + continue; + } + } - file_key = php_yaconf_str_persistent(namelist[i]->d_name, p - namelist[i]->d_name); - if ((orig_ht = zend_symtable_find(ini_containers, file_key)) != NULL) { - php_yaconf_hash_destroy(Z_ARRVAL_P(orig_ht)); - ZVAL_COPY_VALUE(orig_ht, &result); - free(file_key); - } else { - php_yaconf_symtable_update(ini_containers, file_key, &result); - } - if (node) { - node->mtime = sb.st_mtime; - } else { - yaconf_filenode n = {0}; - n.filename = zend_string_init(namelist[i]->d_name, strlen(namelist[i]->d_name), 1); - n.mtime = sb.st_mtime; - zend_hash_update_mem(parsed_ini_files, n.filename, &n, sizeof(yaconf_filenode)); + file_key = php_yaconf_str_persistent(ini_file+dirname_len+1, ini_file_len - dirname_len - 1 - strlen(p)); + if ((orig_ht = zend_symtable_find(ini_containers, file_key)) != NULL) { + php_yaconf_hash_destroy(Z_ARRVAL_P(orig_ht)); + ZVAL_COPY_VALUE(orig_ht, &result); + free(file_key); + } else { + php_yaconf_symtable_update(ini_containers, file_key, &result); + } + + if (node) { + node->mtime = sb.st_mtime; + } else { + yaconf_filenode n = {0}; + n.filename = zend_string_init(ini_file+dirname_len+1, ini_file_len - dirname_len - 1, 1); + n.mtime = sb.st_mtime; + zend_hash_update_mem(parsed_ini_files, n.filename, &n, sizeof(yaconf_filenode)); + } + free(namelist[i]); } - free(namelist[i]); + free(namelist); } - free(namelist); + efree(cur_dir_name); } return SUCCESS; } From 3c9ce35705f0bc57f1c1d63b982accf18b094f0b Mon Sep 17 00:00:00 2001 From: xiyuan Date: Thu, 11 May 2017 00:22:25 +0800 Subject: [PATCH 02/10] fix --- yaconf.c | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/yaconf.c b/yaconf.c index ef71b08..50c5a5e 100644 --- a/yaconf.c +++ b/yaconf.c @@ -473,7 +473,7 @@ PHP_GINIT_FUNCTION(yaconf) */ PHP_MINIT_FUNCTION(yaconf) { - char *(*search_dir_names)[MAX_SEARCH_DIR_SIZE]; + char * search_dir_names[MAX_SEARCH_DIR_SIZE]; int search_dir_size=0; char *cur_dir_name = NULL; @@ -505,6 +505,13 @@ PHP_MINIT_FUNCTION(yaconf) dirname_len = strlen(dirname); search_dir_names[search_dir_size++] = estrdup(dirname); + + PALLOC_HASHTABLE(ini_containers); + zend_hash_init(ini_containers, 32, NULL, NULL, 1); + + PALLOC_HASHTABLE(parsed_ini_files); + zend_hash_init(parsed_ini_files, 32, NULL, NULL, 1); + while( search_dir_size > 0 ){ cur_dir_name = search_dir_names[--search_dir_size]; @@ -513,15 +520,8 @@ PHP_MINIT_FUNCTION(yaconf) unsigned char c; struct zend_stat sb; zend_file_handle fh = {0}; - - PALLOC_HASHTABLE(ini_containers); - zend_hash_init(ini_containers, ndir, NULL, NULL, 1); - - PALLOC_HASHTABLE(parsed_ini_files); - zend_hash_init(parsed_ini_files, ndir, NULL, NULL, 1); - for (i = 0; i < ndir; i++) { - if (!(p = strrchr(namelist[i]->d_name, '.')) || strcmp(p, ".ini")) { + if (strcmp(namelist[i]->d_name, ".") == 0 || strcmp(namelist[i]->d_name, "..") == 0) { free(namelist[i]); continue; } @@ -530,6 +530,11 @@ PHP_MINIT_FUNCTION(yaconf) if (ini_file_len < 0 || VCWD_STAT(ini_file, &sb) == 0) { if (S_ISREG(sb.st_mode)) { + if (!(p = strrchr(namelist[i]->d_name, '.')) || strcmp(p, ".ini")) { + free(namelist[i]); + continue; + } + yaconf_filenode node; if ((fh.handle.fp = VCWD_FOPEN(ini_file, "r"))) { fh.filename = ini_file; @@ -552,7 +557,7 @@ PHP_MINIT_FUNCTION(yaconf) node.filename = zend_string_init(ini_file+dirname_len+1, ini_file_len - dirname_len - 1 , 1); node.mtime = sb.st_mtime; zend_hash_update_mem(parsed_ini_files, node.filename, &node, sizeof(yaconf_filenode)); - }else if(S_IFDIR(sb.st_mode) ){ + }else if(S_ISDIR(sb.st_mode) ){ if(search_dir_size >= MAX_SEARCH_DIR_SIZE){ php_error(E_ERROR, "max dir size '%d'", search_dir_size); }else{ @@ -590,6 +595,7 @@ PHP_RINIT_FUNCTION(yaconf) return SUCCESS; } else { char *dirname; + int dirname_len =0; struct zend_stat dir_sb = {0}; YACONF_G(last_check) = time(NULL); @@ -604,7 +610,7 @@ PHP_RINIT_FUNCTION(yaconf) struct dirent **namelist; char *p, ini_file[MAXPATHLEN],ini_file_len=0; - char *(*search_dir_names)[MAX_SEARCH_DIR_SIZE]; + char *search_dir_names[MAX_SEARCH_DIR_SIZE]; int search_dir_size=0; char *cur_dir_name = NULL; @@ -622,15 +628,16 @@ PHP_RINIT_FUNCTION(yaconf) yaconf_filenode *node = NULL; for (i = 0; i < ndir; i++) { - zval *orig_ht = NULL; - if (!(p = strrchr(namelist[i]->d_name, '.')) || strcmp(p, ".ini")) { + if (strcmp(namelist[i]->d_name, ".") == 0 || strcmp(namelist[i]->d_name, "..") == 0) { free(namelist[i]); continue; } + + zval *orig_ht = NULL; ini_file_len = snprintf(ini_file, MAXPATHLEN, "%s%c%s", cur_dir_name, DEFAULT_SLASH, namelist[i]->d_name); if (ini_file_len < 0 || VCWD_STAT(ini_file, &sb) || !S_ISREG(sb.st_mode)) { - if(S_IFDIR(sb.st_mode) ){ + if(S_ISDIR(sb.st_mode) ){ if(search_dir_size >= MAX_SEARCH_DIR_SIZE){ php_error(E_ERROR, "max dir size '%d'", search_dir_size); }else{ @@ -641,6 +648,11 @@ PHP_RINIT_FUNCTION(yaconf) continue; } + if (!(p = strrchr(namelist[i]->d_name, '.')) || strcmp(p, ".ini")) { + free(namelist[i]); + continue; + } + if ((node = (yaconf_filenode*)zend_hash_str_find_ptr(parsed_ini_files, ini_file+dirname_len+1, ini_file_len - dirname_len - 1)) == NULL) { YACONF_DEBUG("new configure file found"); } else if (node->mtime == sb.st_mtime) { From 02f1f222340a5b57424f69349d61c4618fefc77e Mon Sep 17 00:00:00 2001 From: xiyuan Date: Thu, 11 May 2017 09:52:17 +0800 Subject: [PATCH 03/10] modify test --- tests/002.phpt | 3 ++- tests/003.phpt | 2 +- tests/004.phpt | 2 +- tests/inis/{ => data}/a.ini | 0 tests/inis/{ => data}/a.ini.OLD | 0 tests/inis/{ => data}/b.ini | 0 tests/inis/data/common/a.ini | 4 ++++ tests/inis/{ => data}/d.ini | 0 tests/inis/{ => data}/e.ini | 0 tests/issue19.phpt | 2 +- 10 files changed, 9 insertions(+), 4 deletions(-) rename tests/inis/{ => data}/a.ini (100%) rename tests/inis/{ => data}/a.ini.OLD (100%) rename tests/inis/{ => data}/b.ini (100%) create mode 100644 tests/inis/data/common/a.ini rename tests/inis/{ => data}/d.ini (100%) rename tests/inis/{ => data}/e.ini (100%) diff --git a/tests/002.phpt b/tests/002.phpt index de4bf88..f6410fb 100644 --- a/tests/002.phpt +++ b/tests/002.phpt @@ -3,7 +3,7 @@ Check for Yaconf --SKIPIF-- --INI-- -yaconf.directory={PWD}/inis/ +yaconf.directory={PWD}/inis/data --FILE-- --EXPECTF-- Array diff --git a/tests/003.phpt b/tests/003.phpt index ac15122..e886f45 100644 --- a/tests/003.phpt +++ b/tests/003.phpt @@ -3,7 +3,7 @@ Check for Yaconf --SKIPIF-- --INI-- -yaconf.directory={PWD}/inis/ +yaconf.directory={PWD}/inis/data --FILE-- --INI-- -yaconf.directory={PWD}/inis/ +yaconf.directory={PWD}/inis/data --FILE-- --INI-- -yaconf.directory={PWD}/inis/ +yaconf.directory={PWD}/inis/data --FILE-- Date: Thu, 11 May 2017 11:20:43 +0800 Subject: [PATCH 04/10] update tests --- tests/001.phpt | 2 ++ tests/002.phpt | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/tests/001.phpt b/tests/001.phpt index d6ed78a..734dca8 100644 --- a/tests/001.phpt +++ b/tests/001.phpt @@ -2,6 +2,8 @@ Check for Yaconf presence --SKIPIF-- +--INI-- +yaconf.directory={PWD}/inis/data --FILE-- Array + ( + [domain] => Array + ( + [allow] => Array + ( + [0] => 127.0.0.1 + ) + + [deny] => Array + ( + [0] => 192.168.1.0 + ) + + ) + + ) + +) From d9a1826be4fb91e668e3934837c367c16dd41caa Mon Sep 17 00:00:00 2001 From: destinyac Date: Thu, 11 May 2017 11:24:09 +0800 Subject: [PATCH 05/10] Update README.md --- README.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/README.md b/README.md index 5fdf092..1ddba49 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,18 @@ children="NULL" [children:base] children="set" ```` + +and test/foo.ini( fullpath is : /tmp/yaconf/test/foo.ini) +````ini +name="test/yaconf" +year=2015 +features[]="fast" +features.1="light" +features.plus="zero-copy" +features.constant=PHP_VERSION +features.env=${HOME} +```` + #### Run lets access the configurations @@ -146,4 +158,28 @@ array(2) { */ ```` +##### test/foo.ini +Now let's see the ini in the directories : +````php +php7 -r 'var_dump(Yaconf::get("test/foo"));' +/* +array(2) { + ["base"]=> + array(2) { + ["parent"]=> + string(6) "test/yaconf" + ["children"]=> + string(4) "NULL" + } + ["children"]=> + array(2) { + ["parent"]=> + string(6) "yaconf" + ["children"]=> + string(3) "set" + } +} +*/ +```` + Children section has inherited values in base sections, and children were able to override the values they want. From 6f05957e2cab273901d67907f1888c12e4372e5a Mon Sep 17 00:00:00 2001 From: destinyac Date: Thu, 11 May 2017 11:30:20 +0800 Subject: [PATCH 06/10] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1ddba49..aebedf7 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,8 @@ array(2) { } */ ```` +Children section has inherited values in base sections, and children were able to override the values they want. + ##### test/foo.ini Now let's see the ini in the directories : @@ -181,5 +183,3 @@ array(2) { } */ ```` - -Children section has inherited values in base sections, and children were able to override the values they want. From a63540b901056b272016f4131eab0c5abcbf018b Mon Sep 17 00:00:00 2001 From: destinyac Date: Thu, 11 May 2017 11:32:32 +0800 Subject: [PATCH 07/10] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index aebedf7..49e11cc 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ Yaconf is a configurations container, it parses ini files, and store the result - Zero-copy while accesses configurations - Support sections, sections inheritance - Configurations reload automatically after changed +- Support multi-level directory ### Install From 66a670879cc7b3ddef5ccbf39b38f9f79ed872c1 Mon Sep 17 00:00:00 2001 From: xiyuan Date: Thu, 11 May 2017 15:09:14 +0800 Subject: [PATCH 08/10] commit --- php_yaconf.h | 1 + yaconf.c | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/php_yaconf.h b/php_yaconf.h index a118cb8..700b503 100644 --- a/php_yaconf.h +++ b/php_yaconf.h @@ -52,6 +52,7 @@ ZEND_BEGIN_MODULE_GLOBALS(yaconf) char *directory; int parse_err; #ifndef ZTS + long check_type; long check_delay; time_t last_check; time_t directory_mtime; diff --git a/yaconf.c b/yaconf.c index 50c5a5e..96cc653 100644 --- a/yaconf.c +++ b/yaconf.c @@ -456,6 +456,7 @@ zend_function_entry yaconf_methods[] = { PHP_INI_BEGIN() STD_PHP_INI_ENTRY("yaconf.directory", "", PHP_INI_SYSTEM, OnUpdateString, directory, zend_yaconf_globals, yaconf_globals) #ifndef ZTS + STD_PHP_INI_ENTRY("yaconf.check_type", "0", PHP_INI_SYSTEM, OnUpdateLong, check_type, zend_yaconf_globals, yaconf_globals) STD_PHP_INI_ENTRY("yaconf.check_delay", "300", PHP_INI_SYSTEM, OnUpdateLong, check_delay, zend_yaconf_globals, yaconf_globals) #endif PHP_INI_END() @@ -559,7 +560,7 @@ PHP_MINIT_FUNCTION(yaconf) zend_hash_update_mem(parsed_ini_files, node.filename, &node, sizeof(yaconf_filenode)); }else if(S_ISDIR(sb.st_mode) ){ if(search_dir_size >= MAX_SEARCH_DIR_SIZE){ - php_error(E_ERROR, "max dir size '%d'", search_dir_size); + php_error(E_WARNING, "max dir size '%d'", search_dir_size); }else{ search_dir_names[search_dir_size++] = estrdup(ini_file); } @@ -601,7 +602,7 @@ PHP_RINIT_FUNCTION(yaconf) YACONF_G(last_check) = time(NULL); if ((dirname = YACONF_G(directory)) && !VCWD_STAT(dirname, &dir_sb) && S_ISDIR(dir_sb.st_mode)) { - if (dir_sb.st_mtime == YACONF_G(directory_mtime)) { + if (YACONF_G(check_type) == 0 && dir_sb.st_mtime == YACONF_G(directory_mtime) ) { YACONF_DEBUG("config directory is not modefied"); return SUCCESS; } else { @@ -639,7 +640,7 @@ PHP_RINIT_FUNCTION(yaconf) if (ini_file_len < 0 || VCWD_STAT(ini_file, &sb) || !S_ISREG(sb.st_mode)) { if(S_ISDIR(sb.st_mode) ){ if(search_dir_size >= MAX_SEARCH_DIR_SIZE){ - php_error(E_ERROR, "max dir size '%d'", search_dir_size); + php_error(E_WARNING, "max dir size '%d'", search_dir_size); }else{ search_dir_names[search_dir_size++] = estrdup(ini_file); } From 3da3f602dded6a7d70c507f0378b2b655cc1467e Mon Sep 17 00:00:00 2001 From: destinyac Date: Thu, 11 May 2017 15:23:37 +0800 Subject: [PATCH 09/10] Update README.md --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 49e11cc..b84fcb7 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,11 @@ $ make && make install In which interval Yaconf will detect ini file's change(by directory's mtime), if it is set to zero, you have to restart php to reloading configurations. ``` +- yaconf.check_type +``` + if it is set to zero (default) will detect ini file's change by directory's mtime , + otherwise will detect each file's mtime. +``` ### APIs From e07d239f166fb55031cb95d4df31d813f1b87c27 Mon Sep 17 00:00:00 2001 From: xiyuan Date: Thu, 11 May 2017 16:32:03 +0800 Subject: [PATCH 10/10] fix coredump --- tests/006.phpt | 10 ++++++++++ tests/inis/err/section/b.ini | 3 +++ yaconf.c | 8 ++++++-- 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 tests/inis/err/section/b.ini diff --git a/tests/006.phpt b/tests/006.phpt index cfb90a1..bb47368 100644 --- a/tests/006.phpt +++ b/tests/006.phpt @@ -7,9 +7,19 @@ yaconf.directory={PWD}/inis/err/section --FILE-- --EXPECTF-- PHP Warning: Nesting too deep? Only less than 16 level inheritance is allowed in Unknown on line 0 Warning: Nesting too deep? Only less than 16 level inheritance is allowed in Unknown on line 0 bool(false) +array(2) { + ["name"]=> + string(1) "1" + ["student"]=> + array(1) { + ["test"]=> + string(1) "2" + } +} \ No newline at end of file diff --git a/tests/inis/err/section/b.ini b/tests/inis/err/section/b.ini new file mode 100644 index 0000000..84293cf --- /dev/null +++ b/tests/inis/err/section/b.ini @@ -0,0 +1,3 @@ +name=1 +[student:name] +test=2 \ No newline at end of file diff --git a/yaconf.c b/yaconf.c index 96cc653..25f37d1 100644 --- a/yaconf.c +++ b/yaconf.c @@ -333,7 +333,9 @@ static void php_yaconf_ini_parser_cb(zval *key, zval *value, zval *index, int ca while (*(section) == ' ' || *(section) == ':') { *(section++) = '\0'; } - if ((parent = zend_symtable_str_find(Z_ARRVAL_P(arr), section, strlen(section)))) { + if ((parent = zend_symtable_str_find(Z_ARRVAL_P(arr), section, strlen(section))) && + EXPECTED(Z_TYPE_P(parent) == IS_ARRAY) + ) { php_yaconf_hash_copy(Z_ARRVAL(active_ini_file_section), Z_ARRVAL_P(parent)); } } while ((section = strrchr(seg, ':'))); @@ -345,7 +347,9 @@ static void php_yaconf_ini_parser_cb(zval *key, zval *value, zval *index, int ca *(section--) = '\0'; } - if ((parent = zend_symtable_str_find(Z_ARRVAL_P(arr), seg, strlen(seg)))) { + if ((parent = zend_symtable_str_find(Z_ARRVAL_P(arr), seg, strlen(seg))) && + EXPECTED(Z_TYPE_P(parent) == IS_ARRAY) + ) { php_yaconf_hash_copy(Z_ARRVAL(active_ini_file_section), Z_ARRVAL_P(parent)); } }