cvs: pecl /automap Automap.c Automap.h package.xml php_automap.c php_automap.h utils.c utils.h
| From: | Francois Laupretre | Date: | Sat, 19 Jan 2008 02:53:52 +0000 |
| Subject: | cvs: pecl /automap Automap.c Automap.h package.xml php_automap.c php_automap.h utils.c utils.h | ||
| Groups: | php.pecl.cvs | ||
| Request: | Send a blank email to pecl-cvs+get-9558@lists.php.net to get a copy of this message | ||
francois Sat Jan 19 02:53:52 2008 UTC
Modified files:
/pecl/automap Automap.c Automap.h package.xml php_automap.c
php_automap.h utils.c utils.h
Log:
Bug fixes
http://cvs.php.net/viewvc.cgi/pecl/automap/Automap.c?r1=1.2&r2=1.3&diff_format=u Index: pecl/automap/Automap.c diff -u pecl/automap/Automap.c:1.2 pecl/automap/Automap.c:1.3 --- pecl/automap/Automap.c:1.2 Fri Jan 11 23:25:32 2008 +++ pecl/automap/Automap.c Sat Jan 19 02:53:52 2008 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: Automap.c,v 1.2 2008/01/11 23:25:32 francois Exp $ */ +/* $Id: Automap.c,v 1.3 2008/01/19 02:53:52 francois Exp $ */ #define AUTOMAP_VERSION "" @@ -53,7 +53,7 @@ static HashTable persistent_mtab; -MutexDeclare(persistent_mtab); +static MutexDeclare(persistent_mtab); /*---------------------------------------------------------------*/ @@ -63,7 +63,9 @@ \ DBG_MSG("Entering Automap::" #_func); \ CHECK_MEM(); \ - FIND_HKEY(Z_OBJPROP_P(getThis()),m,&_tmp); \ + if (FIND_HKEY(Z_OBJPROP_P(getThis()),mp_property_name,&_tmp)!=SUCCESS) { ; \ + EXCEPTION_ABORT("Accessing invalid or unmounted object"); \ + } \ mp=*((Automap_Mnt_Info **)(Z_STRVAL_PP(_tmp))); /*---------------------------------------------------------------*/ @@ -80,7 +82,6 @@ static zval *Automap_instance(zval * mnt, ulong hash TSRMLS_DC); static void Automap_mnt_list(zval * ret TSRMLS_DC); static void Automap_path_to_mnt(zval * path, zval * mnt TSRMLS_DC); -static void Automap_compute_mnt(zval * path, zval ** mnt TSRMLS_DC); static Automap_Mnt_Info *Automap_mount(zval * path, zval * base_dir, zval * mnt, int flags TSRMLS_DC); static Automap_Mnt_Persistent_Data *Automap_get_persistent_data(zval * mnt, ulong hash TSRMLS_DC); static Automap_Mnt_Persistent_Data *Automap_get_or_create_persistent_data(Automap_Mnt_Info * mp TSRMLS_DC); @@ -128,10 +129,18 @@ static void Automap_mnt_info_dtor(Automap_Mnt_Info * mp) { + TSRMLS_FETCH(); + if (mp->refcountp) (*(mp->refcountp))--; if (mp->mnt) zval_ptr_dtor(&(mp->mnt)); - if (mp->instance) zval_ptr_dtor(&(mp->instance)); + + if (mp->instance) { + (void)zend_hash_del(Z_OBJPROP_P(mp->instance),MP_PROPERTY_NAME + ,sizeof(MP_PROPERTY_NAME)); /* Invalidate object */ + zval_ptr_dtor(&(mp->instance)); + } + if (mp->path) zval_ptr_dtor(&(mp->path)); if (mp->base_dir) zval_ptr_dtor(&(mp->base_dir)); if (mp->flags) zval_ptr_dtor(&(mp->flags)); @@ -360,13 +369,13 @@ /* }}} */ /*---------------------------------------------------------------*/ -/* In case of error, free mem allocated by compute_mnt() */ +/* In case of error, free mem allocated by ut_path_unique_id() */ static void Automap_path_to_mnt(zval * path, zval * mnt TSRMLS_DC) { zval *tmp_mnt; - Automap_compute_mnt(path, &tmp_mnt TSRMLS_CC); + ut_path_unique_id('m', path, &tmp_mnt, NULL TSRMLS_CC); if (EG(exception)) return; Automap_get_mnt_info(tmp_mnt, 0, 1 TSRMLS_CC); @@ -397,39 +406,6 @@ /* }}} */ /*---------------------------------------------------------------*/ -static void Automap_compute_mnt(zval * path, zval ** mnt TSRMLS_DC) -{ - char *p; - php_stream_statbuf ssb; - time_t mti; - - DBG_MSG1("Starting Automap_compute_mnt(%s)",Z_STRVAL_P(path)); - - if (php_stream_stat_path(Z_STRVAL_P(path), &ssb) != 0) { - THROW_EXCEPTION_1("%s: File not found", Z_STRVAL_P(path)); - return; - } - - DBG_MSG("after stat"); - -#ifdef NETWARE - mti = ssb.sb.st_mtime.tv_sec; -#else - mti = ssb.sb.st_mtime; -#endif - - if (mnt) { - spprintf(&p, 256, "%lX_%lX_%lX", (unsigned long) (ssb.sb.st_dev) - , (unsigned long) (ssb.sb.st_ino), (unsigned long) mti); - MAKE_STD_ZVAL(*mnt); - ZVAL_STRING((*mnt), p, 0); - } - - DBG_MSG("Ending Automap_compute_mnt"); -} - -/*---------------------------------------------------------------*/ - #define INIT_AUTOMAP_MOUNT() \ { \ mp=NULL; \ @@ -459,7 +435,7 @@ SEPARATE_ARG_IF_REF(mnt); if (!ZVAL_IS_STRING(mnt)) convert_to_string(mnt); } else { - Automap_compute_mnt(path, &mnt TSRMLS_CC); + ut_path_unique_id('m',path,&mnt,NULL TSRMLS_CC); if (EG(exception)) ABORT_AUTOMAP_MOUNT(); } @@ -1311,7 +1287,8 @@ type=*Z_STRVAL_PP(value); if (type == AUTOMAP_F_EXTENSION) { - ut_load_extension(symbol,slen TSRMLS_CC); + ZVAL_STRINGL(&zv,symbol,slen,0); + ut_load_extension_file(&zv TSRMLS_CC); if (EG(exception)) return 0; Automap_call_success_handlers(key,*value,mp TSRMLS_CC); return 1; @@ -1591,7 +1568,8 @@ static void Automap_set_mp_property(zval * obj, Automap_Mnt_Info * mp TSRMLS_DC) { - add_property_stringl_ex(obj, "m", 2, (char *)(&mp), sizeof(mp),1 TSRMLS_CC); + zend_update_property_stringl(Z_OBJCE_P(obj),obj,"m",1,(char *)(&mp) + , sizeof(mp) TSRMLS_CC); } /*---------------------------------------------------------------*/ @@ -1611,6 +1589,33 @@ } /*---------------------------------------------------------------*/ +/* {{{ proto bool PHK::accelerator_is_present() */ + +static PHP_METHOD(Automap, accelerator_is_present) +{ + RETVAL_TRUE; +} + +/* }}} */ +/*---------------------------------------------------------------*/ +/* {{{ proto void Automap::accel_techinfo() */ + +static PHP_METHOD(Automap, accel_techinfo) +{ + if (sapi_module.phpinfo_as_text) { + php_printf("Using Automap Accelerator: Yes\n"); + php_printf("Accelerator Version: %s\n", AUTOMAP_EXT_VERSION); + } else { + php_printf("<table border=0>"); + php_printf("<tr><td>Using Automap Accelerator: </td><td>Yes</td></tr>"); + php_printf("<tr><td>Accelerator Version: </td><td>%s</td></tr>", + AUTOMAP_EXT_VERSION); + php_printf("</table>"); + } +} + +/* }}} */ +/*---------------------------------------------------------------*/ static ZEND_BEGIN_ARG_INFO_EX(Automap_mount_arginfo, 0, 1, 1) ZEND_ARG_INFO(0, path) @@ -1683,6 +1688,10 @@ PHP_ME(Automap, option, UT_1arg_ref_arginfo, ZEND_ACC_PUBLIC) PHP_ME(Automap, show, UT_noarg_arginfo, ZEND_ACC_PUBLIC) PHP_ME(Automap, export, UT_noarg_arginfo, ZEND_ACC_PUBLIC) + PHP_ME(Automap, accelerator_is_present, UT_noarg_arginfo, + ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + PHP_ME(Automap, accel_techinfo, UT_noarg_arginfo, + ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) {NULL, NULL, NULL, 0, 0} }; @@ -1736,6 +1745,9 @@ INIT_CLASS_ENTRY(ce, "Automap", Automap_functions); entry = zend_register_internal_class(&ce TSRMLS_CC); + + zend_declare_property_null(entry,"m",1,ZEND_ACC_PRIVATE TSRMLS_CC); + set_constants(entry); return SUCCESS; http://cvs.php.net/viewvc.cgi/pecl/automap/Automap.h?r1=1.1&r2=1.2&diff_format=u Index: pecl/automap/Automap.h diff -u pecl/automap/Automap.h:1.1 pecl/automap/Automap.h:1.2 --- pecl/automap/Automap.h:1.1 Thu Jan 10 22:03:25 2008 +++ pecl/automap/Automap.h Sat Jan 19 02:53:52 2008 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: Automap.h,v 1.1 2008/01/10 22:03:25 francois Exp $ */ +/* $Id: Automap.h,v 1.2 2008/01/19 02:53:52 francois Exp $ */ #ifndef AUTOMAP_H #define AUTOMAP_H 1 @@ -46,7 +46,7 @@ { AUTOMAP_T_CLASS, "class" }, { AUTOMAP_T_EXTENSION, "extension" }, { AUTOMAP_F_SCRIPT, "script" }, - { AUTOMAP_F_EXTENSION, "extension" }, + { AUTOMAP_F_EXTENSION, "extension file" }, { AUTOMAP_F_PACKAGE, "package" }, { '\0', NULL } }; http://cvs.php.net/viewvc.cgi/pecl/automap/package.xml?r1=1.1&r2=1.2&diff_format=u Index: pecl/automap/package.xml diff -u pecl/automap/package.xml:1.1 pecl/automap/package.xml:1.2 --- pecl/automap/package.xml:1.1 Thu Jan 10 22:03:25 2008 +++ pecl/automap/package.xml Sat Jan 19 02:53:52 2008 @@ -10,7 +10,7 @@ <email>francois@php.net</email> <active>yes</active> </lead> - <date>2008-01-09</date> + <date>2008-01-19</date> <time>12:00:00</time> <version> <release>1.1.0</release> @@ -63,7 +63,7 @@ <release>alpha</release> <api>alpha</api> </stability> - <date>2008-01-09</date> + <date>2008-01-19</date> <license uri="http://www.php.net/license">PHP License</license> <notes>Initial release in PECL</notes> </release> http://cvs.php.net/viewvc.cgi/pecl/automap/php_automap.c?r1=1.1&r2=1.2&diff_format=u Index: pecl/automap/php_automap.c diff -u pecl/automap/php_automap.c:1.1 pecl/automap/php_automap.c:1.2 --- pecl/automap/php_automap.c:1.1 Thu Jan 10 22:03:25 2008 +++ pecl/automap/php_automap.c Sat Jan 19 02:53:52 2008 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_automap.c,v 1.1 2008/01/10 22:03:25 francois Exp $ */ +/* $Id: php_automap.c,v 1.2 2008/01/19 02:53:52 francois Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -40,6 +40,8 @@ ZEND_GET_MODULE(automap) #endif +static int init_done=0; + /*------------------------*/ #include "utils.c" @@ -48,6 +50,7 @@ /*---------------------------------------------------------------*/ /* phpinfo() output */ + static PHP_MINFO_FUNCTION(automap) { php_info_print_table_start(); @@ -97,13 +100,17 @@ INIT_HKEY(map); INIT_HKEY(options); - INIT_HKEY(m); + INIT_HKEY(automap); + + INIT_HKEY_VALUE(mp_property_name,MP_PROPERTY_NAME); } /*---------------------------------------------------------------*/ static PHP_RINIT_FUNCTION(automap) { + if (!init_done) return SUCCESS; + DBG_INIT(); if (RINIT_utils(TSRMLS_C) == FAILURE) return FAILURE; @@ -117,6 +124,8 @@ static PHP_RSHUTDOWN_FUNCTION(automap) { + if (!init_done) return SUCCESS; + if (RSHUTDOWN_Automap(TSRMLS_C) == FAILURE) return FAILURE; if (RSHUTDOWN_utils(TSRMLS_C) == FAILURE) return FAILURE; @@ -128,13 +137,20 @@ static PHP_MINIT_FUNCTION(automap) { + build_constant_values(); + + /* Handle case where the Automap extension is dynamically loaded after + the Automap PHP runtime has been initialized. In this case, + we must not define anything/ */ + + if (EG(class_table) && HKEY_EXISTS(EG(class_table),automap)) return SUCCESS; + else init_done=1; + ZEND_INIT_MODULE_GLOBALS(automap, automap_globals_ctor, NULL); REGISTER_STRING_CONSTANT("AUTOMAP_EXT_VERSION", AUTOMAP_EXT_VERSION, CONST_CS | CONST_PERSISTENT); - build_constant_values(); - if (MINIT_utils(TSRMLS_C) == FAILURE) return FAILURE; if (MINIT_Automap(TSRMLS_C) == FAILURE) return FAILURE; @@ -146,6 +162,8 @@ static PHP_MSHUTDOWN_FUNCTION(automap) { + if (!init_done) return SUCCESS; + #ifndef ZTS automap_globals_dtor(&automap_globals TSRMLS_CC); #endif http://cvs.php.net/viewvc.cgi/pecl/automap/php_automap.h?r1=1.2&r2=1.3&diff_format=u Index: pecl/automap/php_automap.h diff -u pecl/automap/php_automap.h:1.2 pecl/automap/php_automap.h:1.3 --- pecl/automap/php_automap.h:1.2 Fri Jan 11 23:25:32 2008 +++ pecl/automap/php_automap.h Sat Jan 19 02:53:52 2008 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_automap.h,v 1.2 2008/01/11 23:25:32 francois Exp $ */ +/* $Id: php_automap.h,v 1.3 2008/01/19 02:53:52 francois Exp $ */ /*#define AUTOMAP_DEBUG*/ @@ -54,7 +54,9 @@ static DECLARE_HKEY(map); static DECLARE_HKEY(options); -static DECLARE_HKEY(m); +static DECLARE_HKEY(automap); + +static DECLARE_HKEY(mp_property_name); /*============================================================================*/ @@ -100,4 +102,11 @@ #endif /*---------------------------------------------------------------*/ + +/* We need a private property here, so that it cannot be accessed nor + modified by a malicious PHP script */ + +#define MP_PROPERTY_NAME "\0Automap\0m" + +/*---------------------------------------------------------------*/ #endif /* PHP_AUTOMAP_H */ http://cvs.php.net/viewvc.cgi/pecl/automap/utils.c?r1=1.1&r2=1.2&diff_format=u Index: pecl/automap/utils.c diff -u pecl/automap/utils.c:1.1 pecl/automap/utils.c:1.2 --- pecl/automap/utils.c:1.1 Thu Jan 10 22:03:25 2008 +++ pecl/automap/utils.c Sat Jan 19 02:53:52 2008 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: utils.c,v 1.1 2008/01/10 22:03:25 francois Exp $ */ +/* $Id: utils.c,v 1.2 2008/01/19 02:53:52 francois Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -39,6 +39,9 @@ #ifdef HAVE_UNISTD_H #include <unistd.h> #endif +#ifdef HAVE_SYS_STAT_H +#include <sys/stat.h> +#endif #include "php.h" #include "zend_execute.h" @@ -52,6 +55,14 @@ #include "utils.h" /*============================================================================*/ + +static HashTable simul_inodes; + +static unsigned long simul_inode_index; + +static MutexDeclare(simul_inodes); + +/*============================================================================*/ /* Generic arginfo structures */ static ZEND_BEGIN_ARG_INFO_EX(UT_noarg_arginfo, 0, 0, 0) @@ -91,6 +102,27 @@ ZEND_END_ARG_INFO() /*============================================================================*/ +/*--- Init persistent data */ + +static void ut_init_persistent_data(TSRMLS_D) +{ + MutexSetup(simul_inodes); + + zend_hash_init(&simul_inodes, 16, NULL,NULL, 1); + + simul_inode_index = 1; +} + +/*---------------------------------------------------------------*/ + +static void ut_shutdown_persistent_data(TSRMLS_D) +{ + zend_hash_destroy(&simul_inodes); + + MutexShutdown(simul_inodes); +} + +/*---------------------------------------------------------------*/ static inline void *_allocate(void *ptr, size_t size, int persistent) { @@ -396,6 +428,15 @@ /*---------------------------------------------------------------*/ +static void ut_load_extension_file(zval *file TSRMLS_DC) +{ + if (!ut_call_user_function_bool(NULL,&CZVAL(dl),1,&file TSRMLS_CC)) { + THROW_EXCEPTION_1("%s: Cannot load extension",Z_STRVAL_P(file)); + } +} + +/*---------------------------------------------------------------*/ + #ifdef PHP_WIN32 #define _UT_LE_PREFIX "php_" #else @@ -404,22 +445,18 @@ static void ut_load_extension(char *name, int len TSRMLS_DC) { - zval zv,*zp; + zval zv; char *p; - int status; if (ut_extension_loaded(name, len TSRMLS_CC)) return; spprintf(&p,1024,_UT_LE_PREFIX "%s." PHP_SHLIB_SUFFIX,name); INIT_ZVAL(zv); - zp=&zv; - ZVAL_STRING(zp,p,0); + ZVAL_STRING(&zv,p,0); - status=ut_call_user_function_bool(NULL,&CZVAL(dl),1,&zp TSRMLS_CC); + ut_load_extension_file(&zv TSRMLS_CC); - zval_dtor(zp); - - if (!status) THROW_EXCEPTION_1("%s: Cannot load extension",name); + zval_dtor(&zv); } /*---------------------------------------------------------------*/ @@ -903,6 +940,58 @@ return i; } +/*---------------------------------------------------------------*/ + +static void ut_path_unique_id(char prefix, zval * path, zval ** mnt + , time_t *mtp TSRMLS_DC) +{ + char *p; + php_stream_statbuf ssb; + time_t mtime; + unsigned long inode,dev,*lp,hash; + int rlen; + char resolved_path_buff[MAXPATHLEN]; + + if (php_stream_stat_path(Z_STRVAL_P(path), &ssb) != 0) { + EXCEPTION_ABORT_1("%s: Cannot stat",Z_STRVAL_P(path)); + } + + dev=(unsigned long) ssb.sb.st_dev; + inode=(unsigned long) ssb.sb.st_ino; + +#ifdef NETWARE + mtime= ssb.sb.st_mtime.tv_sec; +#else + mtime = ssb.sb.st_mtime; +#endif + + if (mnt) { + if (!inode) { + if (!VCWD_REALPATH(Z_STRVAL_P(path), resolved_path_buff)) + EXCEPTION_ABORT_1("%s: Cannot compute realpath",Z_STRVAL_P(path)); + + rlen=strlen(resolved_path_buff)+1; + hash=zend_get_hash_value(resolved_path_buff,rlen); + + MutexLock(simul_inodes); + if (zend_hash_quick_find(&simul_inodes,resolved_path_buff + ,rlen,hash,(void **)(&lp))==SUCCESS) inode=(*lp); + else { + inode=simul_inode_index++; + zend_hash_quick_add(&simul_inodes,resolved_path_buff + ,rlen,hash,(void *)(&inode),sizeof(inode),NULL); + } + MutexUnlock(simul_inodes); + } + + spprintf(&p, 256, "%c_%lX_%lX_%lX", prefix, dev, inode, mtime); + MAKE_STD_ZVAL(*mnt); + ZVAL_STRING((*mnt), p, 0); + } + + if (mtp) (*mtp)=mtime; +} + /*========================================================================*/ static void ut_build_constants(TSRMLS_D) @@ -923,6 +1012,8 @@ { ut_build_constants(TSRMLS_C); + ut_init_persistent_data(TSRMLS_C); + return SUCCESS; } @@ -930,6 +1021,8 @@ static int MSHUTDOWN_utils(TSRMLS_D) { + ut_shutdown_persistent_data(TSRMLS_C); + return SUCCESS; } http://cvs.php.net/viewvc.cgi/pecl/automap/utils.h?r1=1.2&r2=1.3&diff_format=u Index: pecl/automap/utils.h diff -u pecl/automap/utils.h:1.2 pecl/automap/utils.h:1.3 --- pecl/automap/utils.h:1.2 Fri Jan 11 23:25:32 2008 +++ pecl/automap/utils.h Sat Jan 19 02:53:52 2008 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: utils.h,v 1.2 2008/01/11 23:25:32 francois Exp $ */ +/* $Id: utils.h,v 1.3 2008/01/19 02:53:52 francois Exp $ */ #ifndef FLP_UTILS_H #define FLP_UTILS_H 1 @@ -48,20 +48,24 @@ #define DECLARE_CZVAL(name) zval CZVAL(name) -#define INIT_CZVAL(name) \ +#define INIT_CZVAL(name) INIT_CZVAL_VALUE(name, #name) + +#define INIT_CZVAL_VALUE(name,value) \ { \ INIT_ZVAL(CZVAL(name)); \ - ZVAL_STRING(&(CZVAL(name)), #name,0); \ + ZVAL_STRING(&(CZVAL(name)), value,0); \ } #define HKEY(name) ( hkey_ ## name ) #define DECLARE_HKEY(name) HKEY_STRUCT HKEY(name) -#define INIT_HKEY(name) \ +#define INIT_HKEY(name) INIT_HKEY_VALUE(name, #name) + +#define INIT_HKEY_VALUE(name,value) \ { \ - HKEY(name).string= #name ; \ - HKEY(name).len=sizeof( #name ); \ + HKEY(name).string= value ; \ + HKEY(name).len=sizeof( value ); \ HKEY(name).hash=zend_get_hash_value(HKEY(name).string,HKEY(name).len); \ } @@ -193,9 +197,9 @@ #define ALLOC_PERSISTENT_ZVAL(zp) (zp)=pallocate(NULL,sizeof(zval)) -#define MAKE_STD_PERSISTENT_ZVAL(zv) \ - ALLOC_PERSISTENT_ZVAL(zv); \ - INIT_PZVAL(zv); +#define MAKE_STD_PERSISTENT_ZVAL(zp) \ + ALLOC_PERSISTENT_ZVAL(zp); \ + INIT_PZVAL(zp); #ifndef ZVAL_ARRAY /*--------------*/ @@ -363,6 +367,7 @@ zval ** args TSRMLS_DC); static int ut_extension_loaded(char *name, int len TSRMLS_DC); +static void ut_load_extension_file(zval *file TSRMLS_DC); static void ut_load_extension(char *name, int len TSRMLS_DC); static void ut_require(char *string, zval * ret TSRMLS_DC); static inline int ut_strings_are_equal(zval * zp1, zval * zp2 TSRMLS_DC); @@ -390,6 +395,7 @@ static inline int ut_is_uri(char *path, int len TSRMLS_DC); static char *ut_mk_absolute_path(char *path, int len, int *reslen, int separ TSRMLS_DC); static int ut_rtrim(char *p TSRMLS_DC); +static void ut_path_unique_id(char prefix, zval * path, zval ** mnt, time_t *mtp TSRMLS_DC); /*============================================================================*/ #endif /* FLP_UTILS_H */
http://cvs.php.net/viewvc.cgi/pecl/automap/Automap.c?r1=1.2&r2=1.3&diff_format=u Index: pecl/automap/Automap.c diff -u pecl/automap/Automap.c:1.2 pecl/automap/Automap.c:1.3 --- pecl/automap/Automap.c:1.2 Fri Jan 11 23:25:32 2008 +++ pecl/automap/Automap.c Sat Jan 19 02:53:52 2008 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: Automap.c,v 1.2 2008/01/11 23:25:32 francois Exp $ */ +/* $Id: Automap.c,v 1.3 2008/01/19 02:53:52 francois Exp $ */ #define AUTOMAP_VERSION "" @@ -53,7 +53,7 @@ static HashTable persistent_mtab; -MutexDeclare(persistent_mtab); +static MutexDeclare(persistent_mtab); /*---------------------------------------------------------------*/ @@ -63,7 +63,9 @@ \ DBG_MSG("Entering Automap::" #_func); \ CHECK_MEM(); \ - FIND_HKEY(Z_OBJPROP_P(getThis()),m,&_tmp); \ + if (FIND_HKEY(Z_OBJPROP_P(getThis()),mp_property_name,&_tmp)!=SUCCESS) { ; \ + EXCEPTION_ABORT("Accessing invalid or unmounted object"); \ + } \ mp=*((Automap_Mnt_Info **)(Z_STRVAL_PP(_tmp))); /*---------------------------------------------------------------*/ @@ -80,7 +82,6 @@ static zval *Automap_instance(zval * mnt, ulong hash TSRMLS_DC); static void Automap_mnt_list(zval * ret TSRMLS_DC); static void Automap_path_to_mnt(zval * path, zval * mnt TSRMLS_DC); -static void Automap_compute_mnt(zval * path, zval ** mnt TSRMLS_DC); static Automap_Mnt_Info *Automap_mount(zval * path, zval * base_dir, zval * mnt, int flags TSRMLS_DC); static Automap_Mnt_Persistent_Data *Automap_get_persistent_data(zval * mnt, ulong hash TSRMLS_DC); static Automap_Mnt_Persistent_Data *Automap_get_or_create_persistent_data(Automap_Mnt_Info * mp TSRMLS_DC); @@ -128,10 +129,18 @@ static void Automap_mnt_info_dtor(Automap_Mnt_Info * mp) { + TSRMLS_FETCH(); + if (mp->refcountp) (*(mp->refcountp))--; if (mp->mnt) zval_ptr_dtor(&(mp->mnt)); - if (mp->instance) zval_ptr_dtor(&(mp->instance)); + + if (mp->instance) { + (void)zend_hash_del(Z_OBJPROP_P(mp->instance),MP_PROPERTY_NAME + ,sizeof(MP_PROPERTY_NAME)); /* Invalidate object */ + zval_ptr_dtor(&(mp->instance)); + } + if (mp->path) zval_ptr_dtor(&(mp->path)); if (mp->base_dir) zval_ptr_dtor(&(mp->base_dir)); if (mp->flags) zval_ptr_dtor(&(mp->flags)); @@ -360,13 +369,13 @@ /* }}} */ /*---------------------------------------------------------------*/ -/* In case of error, free mem allocated by compute_mnt() */ +/* In case of error, free mem allocated by ut_path_unique_id() */ static void Automap_path_to_mnt(zval * path, zval * mnt TSRMLS_DC) { zval *tmp_mnt; - Automap_compute_mnt(path, &tmp_mnt TSRMLS_CC); + ut_path_unique_id('m', path, &tmp_mnt, NULL TSRMLS_CC); if (EG(exception)) return; Automap_get_mnt_info(tmp_mnt, 0, 1 TSRMLS_CC); @@ -397,39 +406,6 @@ /* }}} */ /*---------------------------------------------------------------*/ -static void Automap_compute_mnt(zval * path, zval ** mnt TSRMLS_DC) -{ - char *p; - php_stream_statbuf ssb; - time_t mti; - - DBG_MSG1("Starting Automap_compute_mnt(%s)",Z_STRVAL_P(path)); - - if (php_stream_stat_path(Z_STRVAL_P(path), &ssb) != 0) { - THROW_EXCEPTION_1("%s: File not found", Z_STRVAL_P(path)); - return; - } - - DBG_MSG("after stat"); - -#ifdef NETWARE - mti = ssb.sb.st_mtime.tv_sec; -#else - mti = ssb.sb.st_mtime; -#endif - - if (mnt) { - spprintf(&p, 256, "%lX_%lX_%lX", (unsigned long) (ssb.sb.st_dev) - , (unsigned long) (ssb.sb.st_ino), (unsigned long) mti); - MAKE_STD_ZVAL(*mnt); - ZVAL_STRING((*mnt), p, 0); - } - - DBG_MSG("Ending Automap_compute_mnt"); -} - -/*---------------------------------------------------------------*/ - #define INIT_AUTOMAP_MOUNT() \ { \ mp=NULL; \ @@ -459,7 +435,7 @@ SEPARATE_ARG_IF_REF(mnt); if (!ZVAL_IS_STRING(mnt)) convert_to_string(mnt); } else { - Automap_compute_mnt(path, &mnt TSRMLS_CC); + ut_path_unique_id('m',path,&mnt,NULL TSRMLS_CC); if (EG(exception)) ABORT_AUTOMAP_MOUNT(); } @@ -1311,7 +1287,8 @@ type=*Z_STRVAL_PP(value); if (type == AUTOMAP_F_EXTENSION) { - ut_load_extension(symbol,slen TSRMLS_CC); + ZVAL_STRINGL(&zv,symbol,slen,0); + ut_load_extension_file(&zv TSRMLS_CC); if (EG(exception)) return 0; Automap_call_success_handlers(key,*value,mp TSRMLS_CC); return 1; @@ -1591,7 +1568,8 @@ static void Automap_set_mp_property(zval * obj, Automap_Mnt_Info * mp TSRMLS_DC) { - add_property_stringl_ex(obj, "m", 2, (char *)(&mp), sizeof(mp),1 TSRMLS_CC); + zend_update_property_stringl(Z_OBJCE_P(obj),obj,"m",1,(char *)(&mp) + , sizeof(mp) TSRMLS_CC); } /*---------------------------------------------------------------*/ @@ -1611,6 +1589,33 @@ } /*---------------------------------------------------------------*/ +/* {{{ proto bool PHK::accelerator_is_present() */ + +static PHP_METHOD(Automap, accelerator_is_present) +{ + RETVAL_TRUE; +} + +/* }}} */ +/*---------------------------------------------------------------*/ +/* {{{ proto void Automap::accel_techinfo() */ + +static PHP_METHOD(Automap, accel_techinfo) +{ + if (sapi_module.phpinfo_as_text) { + php_printf("Using Automap Accelerator: Yes\n"); + php_printf("Accelerator Version: %s\n", AUTOMAP_EXT_VERSION); + } else { + php_printf("<table border=0>"); + php_printf("<tr><td>Using Automap Accelerator: </td><td>Yes</td></tr>"); + php_printf("<tr><td>Accelerator Version: </td><td>%s</td></tr>", + AUTOMAP_EXT_VERSION); + php_printf("</table>"); + } +} + +/* }}} */ +/*---------------------------------------------------------------*/ static ZEND_BEGIN_ARG_INFO_EX(Automap_mount_arginfo, 0, 1, 1) ZEND_ARG_INFO(0, path) @@ -1683,6 +1688,10 @@ PHP_ME(Automap, option, UT_1arg_ref_arginfo, ZEND_ACC_PUBLIC) PHP_ME(Automap, show, UT_noarg_arginfo, ZEND_ACC_PUBLIC) PHP_ME(Automap, export, UT_noarg_arginfo, ZEND_ACC_PUBLIC) + PHP_ME(Automap, accelerator_is_present, UT_noarg_arginfo, + ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + PHP_ME(Automap, accel_techinfo, UT_noarg_arginfo, + ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) {NULL, NULL, NULL, 0, 0} }; @@ -1736,6 +1745,9 @@ INIT_CLASS_ENTRY(ce, "Automap", Automap_functions); entry = zend_register_internal_class(&ce TSRMLS_CC); + + zend_declare_property_null(entry,"m",1,ZEND_ACC_PRIVATE TSRMLS_CC); + set_constants(entry); return SUCCESS; http://cvs.php.net/viewvc.cgi/pecl/automap/Automap.h?r1=1.1&r2=1.2&diff_format=u Index: pecl/automap/Automap.h diff -u pecl/automap/Automap.h:1.1 pecl/automap/Automap.h:1.2 --- pecl/automap/Automap.h:1.1 Thu Jan 10 22:03:25 2008 +++ pecl/automap/Automap.h Sat Jan 19 02:53:52 2008 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: Automap.h,v 1.1 2008/01/10 22:03:25 francois Exp $ */ +/* $Id: Automap.h,v 1.2 2008/01/19 02:53:52 francois Exp $ */ #ifndef AUTOMAP_H #define AUTOMAP_H 1 @@ -46,7 +46,7 @@ { AUTOMAP_T_CLASS, "class" }, { AUTOMAP_T_EXTENSION, "extension" }, { AUTOMAP_F_SCRIPT, "script" }, - { AUTOMAP_F_EXTENSION, "extension" }, + { AUTOMAP_F_EXTENSION, "extension file" }, { AUTOMAP_F_PACKAGE, "package" }, { '\0', NULL } }; http://cvs.php.net/viewvc.cgi/pecl/automap/package.xml?r1=1.1&r2=1.2&diff_format=u Index: pecl/automap/package.xml diff -u pecl/automap/package.xml:1.1 pecl/automap/package.xml:1.2 --- pecl/automap/package.xml:1.1 Thu Jan 10 22:03:25 2008 +++ pecl/automap/package.xml Sat Jan 19 02:53:52 2008 @@ -10,7 +10,7 @@ <email>francois@php.net</email> <active>yes</active> </lead> - <date>2008-01-09</date> + <date>2008-01-19</date> <time>12:00:00</time> <version> <release>1.1.0</release> @@ -63,7 +63,7 @@ <release>alpha</release> <api>alpha</api> </stability> - <date>2008-01-09</date> + <date>2008-01-19</date> <license uri="http://www.php.net/license">PHP License</license> <notes>Initial release in PECL</notes> </release> http://cvs.php.net/viewvc.cgi/pecl/automap/php_automap.c?r1=1.1&r2=1.2&diff_format=u Index: pecl/automap/php_automap.c diff -u pecl/automap/php_automap.c:1.1 pecl/automap/php_automap.c:1.2 --- pecl/automap/php_automap.c:1.1 Thu Jan 10 22:03:25 2008 +++ pecl/automap/php_automap.c Sat Jan 19 02:53:52 2008 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_automap.c,v 1.1 2008/01/10 22:03:25 francois Exp $ */ +/* $Id: php_automap.c,v 1.2 2008/01/19 02:53:52 francois Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -40,6 +40,8 @@ ZEND_GET_MODULE(automap) #endif +static int init_done=0; + /*------------------------*/ #include "utils.c" @@ -48,6 +50,7 @@ /*---------------------------------------------------------------*/ /* phpinfo() output */ + static PHP_MINFO_FUNCTION(automap) { php_info_print_table_start(); @@ -97,13 +100,17 @@ INIT_HKEY(map); INIT_HKEY(options); - INIT_HKEY(m); + INIT_HKEY(automap); + + INIT_HKEY_VALUE(mp_property_name,MP_PROPERTY_NAME); } /*---------------------------------------------------------------*/ static PHP_RINIT_FUNCTION(automap) { + if (!init_done) return SUCCESS; + DBG_INIT(); if (RINIT_utils(TSRMLS_C) == FAILURE) return FAILURE; @@ -117,6 +124,8 @@ static PHP_RSHUTDOWN_FUNCTION(automap) { + if (!init_done) return SUCCESS; + if (RSHUTDOWN_Automap(TSRMLS_C) == FAILURE) return FAILURE; if (RSHUTDOWN_utils(TSRMLS_C) == FAILURE) return FAILURE; @@ -128,13 +137,20 @@ static PHP_MINIT_FUNCTION(automap) { + build_constant_values(); + + /* Handle case where the Automap extension is dynamically loaded after + the Automap PHP runtime has been initialized. In this case, + we must not define anything/ */ + + if (EG(class_table) && HKEY_EXISTS(EG(class_table),automap)) return SUCCESS; + else init_done=1; + ZEND_INIT_MODULE_GLOBALS(automap, automap_globals_ctor, NULL); REGISTER_STRING_CONSTANT("AUTOMAP_EXT_VERSION", AUTOMAP_EXT_VERSION, CONST_CS | CONST_PERSISTENT); - build_constant_values(); - if (MINIT_utils(TSRMLS_C) == FAILURE) return FAILURE; if (MINIT_Automap(TSRMLS_C) == FAILURE) return FAILURE; @@ -146,6 +162,8 @@ static PHP_MSHUTDOWN_FUNCTION(automap) { + if (!init_done) return SUCCESS; + #ifndef ZTS automap_globals_dtor(&automap_globals TSRMLS_CC); #endif http://cvs.php.net/viewvc.cgi/pecl/automap/php_automap.h?r1=1.2&r2=1.3&diff_format=u Index: pecl/automap/php_automap.h diff -u pecl/automap/php_automap.h:1.2 pecl/automap/php_automap.h:1.3 --- pecl/automap/php_automap.h:1.2 Fri Jan 11 23:25:32 2008 +++ pecl/automap/php_automap.h Sat Jan 19 02:53:52 2008 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_automap.h,v 1.2 2008/01/11 23:25:32 francois Exp $ */ +/* $Id: php_automap.h,v 1.3 2008/01/19 02:53:52 francois Exp $ */ /*#define AUTOMAP_DEBUG*/ @@ -54,7 +54,9 @@ static DECLARE_HKEY(map); static DECLARE_HKEY(options); -static DECLARE_HKEY(m); +static DECLARE_HKEY(automap); + +static DECLARE_HKEY(mp_property_name); /*============================================================================*/ @@ -100,4 +102,11 @@ #endif /*---------------------------------------------------------------*/ + +/* We need a private property here, so that it cannot be accessed nor + modified by a malicious PHP script */ + +#define MP_PROPERTY_NAME "\0Automap\0m" + +/*---------------------------------------------------------------*/ #endif /* PHP_AUTOMAP_H */ http://cvs.php.net/viewvc.cgi/pecl/automap/utils.c?r1=1.1&r2=1.2&diff_format=u Index: pecl/automap/utils.c diff -u pecl/automap/utils.c:1.1 pecl/automap/utils.c:1.2 --- pecl/automap/utils.c:1.1 Thu Jan 10 22:03:25 2008 +++ pecl/automap/utils.c Sat Jan 19 02:53:52 2008 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: utils.c,v 1.1 2008/01/10 22:03:25 francois Exp $ */ +/* $Id: utils.c,v 1.2 2008/01/19 02:53:52 francois Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -39,6 +39,9 @@ #ifdef HAVE_UNISTD_H #include <unistd.h> #endif +#ifdef HAVE_SYS_STAT_H +#include <sys/stat.h> +#endif #include "php.h" #include "zend_execute.h" @@ -52,6 +55,14 @@ #include "utils.h" /*============================================================================*/ + +static HashTable simul_inodes; + +static unsigned long simul_inode_index; + +static MutexDeclare(simul_inodes); + +/*============================================================================*/ /* Generic arginfo structures */ static ZEND_BEGIN_ARG_INFO_EX(UT_noarg_arginfo, 0, 0, 0) @@ -91,6 +102,27 @@ ZEND_END_ARG_INFO() /*============================================================================*/ +/*--- Init persistent data */ + +static void ut_init_persistent_data(TSRMLS_D) +{ + MutexSetup(simul_inodes); + + zend_hash_init(&simul_inodes, 16, NULL,NULL, 1); + + simul_inode_index = 1; +} + +/*---------------------------------------------------------------*/ + +static void ut_shutdown_persistent_data(TSRMLS_D) +{ + zend_hash_destroy(&simul_inodes); + + MutexShutdown(simul_inodes); +} + +/*---------------------------------------------------------------*/ static inline void *_allocate(void *ptr, size_t size, int persistent) { @@ -396,6 +428,15 @@ /*---------------------------------------------------------------*/ +static void ut_load_extension_file(zval *file TSRMLS_DC) +{ + if (!ut_call_user_function_bool(NULL,&CZVAL(dl),1,&file TSRMLS_CC)) { + THROW_EXCEPTION_1("%s: Cannot load extension",Z_STRVAL_P(file)); + } +} + +/*---------------------------------------------------------------*/ + #ifdef PHP_WIN32 #define _UT_LE_PREFIX "php_" #else @@ -404,22 +445,18 @@ static void ut_load_extension(char *name, int len TSRMLS_DC) { - zval zv,*zp; + zval zv; char *p; - int status; if (ut_extension_loaded(name, len TSRMLS_CC)) return; spprintf(&p,1024,_UT_LE_PREFIX "%s." PHP_SHLIB_SUFFIX,name); INIT_ZVAL(zv); - zp=&zv; - ZVAL_STRING(zp,p,0); + ZVAL_STRING(&zv,p,0); - status=ut_call_user_function_bool(NULL,&CZVAL(dl),1,&zp TSRMLS_CC); + ut_load_extension_file(&zv TSRMLS_CC); - zval_dtor(zp); - - if (!status) THROW_EXCEPTION_1("%s: Cannot load extension",name); + zval_dtor(&zv); } /*---------------------------------------------------------------*/ @@ -903,6 +940,58 @@ return i; } +/*---------------------------------------------------------------*/ + +static void ut_path_unique_id(char prefix, zval * path, zval ** mnt + , time_t *mtp TSRMLS_DC) +{ + char *p; + php_stream_statbuf ssb; + time_t mtime; + unsigned long inode,dev,*lp,hash; + int rlen; + char resolved_path_buff[MAXPATHLEN]; + + if (php_stream_stat_path(Z_STRVAL_P(path), &ssb) != 0) { + EXCEPTION_ABORT_1("%s: Cannot stat",Z_STRVAL_P(path)); + } + + dev=(unsigned long) ssb.sb.st_dev; + inode=(unsigned long) ssb.sb.st_ino; + +#ifdef NETWARE + mtime= ssb.sb.st_mtime.tv_sec; +#else + mtime = ssb.sb.st_mtime; +#endif + + if (mnt) { + if (!inode) { + if (!VCWD_REALPATH(Z_STRVAL_P(path), resolved_path_buff)) + EXCEPTION_ABORT_1("%s: Cannot compute realpath",Z_STRVAL_P(path)); + + rlen=strlen(resolved_path_buff)+1; + hash=zend_get_hash_value(resolved_path_buff,rlen); + + MutexLock(simul_inodes); + if (zend_hash_quick_find(&simul_inodes,resolved_path_buff + ,rlen,hash,(void **)(&lp))==SUCCESS) inode=(*lp); + else { + inode=simul_inode_index++; + zend_hash_quick_add(&simul_inodes,resolved_path_buff + ,rlen,hash,(void *)(&inode),sizeof(inode),NULL); + } + MutexUnlock(simul_inodes); + } + + spprintf(&p, 256, "%c_%lX_%lX_%lX", prefix, dev, inode, mtime); + MAKE_STD_ZVAL(*mnt); + ZVAL_STRING((*mnt), p, 0); + } + + if (mtp) (*mtp)=mtime; +} + /*========================================================================*/ static void ut_build_constants(TSRMLS_D) @@ -923,6 +1012,8 @@ { ut_build_constants(TSRMLS_C); + ut_init_persistent_data(TSRMLS_C); + return SUCCESS; } @@ -930,6 +1021,8 @@ static int MSHUTDOWN_utils(TSRMLS_D) { + ut_shutdown_persistent_data(TSRMLS_C); + return SUCCESS; } http://cvs.php.net/viewvc.cgi/pecl/automap/utils.h?r1=1.2&r2=1.3&diff_format=u Index: pecl/automap/utils.h diff -u pecl/automap/utils.h:1.2 pecl/automap/utils.h:1.3 --- pecl/automap/utils.h:1.2 Fri Jan 11 23:25:32 2008 +++ pecl/automap/utils.h Sat Jan 19 02:53:52 2008 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: utils.h,v 1.2 2008/01/11 23:25:32 francois Exp $ */ +/* $Id: utils.h,v 1.3 2008/01/19 02:53:52 francois Exp $ */ #ifndef FLP_UTILS_H #define FLP_UTILS_H 1 @@ -48,20 +48,24 @@ #define DECLARE_CZVAL(name) zval CZVAL(name) -#define INIT_CZVAL(name) \ +#define INIT_CZVAL(name) INIT_CZVAL_VALUE(name, #name) + +#define INIT_CZVAL_VALUE(name,value) \ { \ INIT_ZVAL(CZVAL(name)); \ - ZVAL_STRING(&(CZVAL(name)), #name,0); \ + ZVAL_STRING(&(CZVAL(name)), value,0); \ } #define HKEY(name) ( hkey_ ## name ) #define DECLARE_HKEY(name) HKEY_STRUCT HKEY(name) -#define INIT_HKEY(name) \ +#define INIT_HKEY(name) INIT_HKEY_VALUE(name, #name) + +#define INIT_HKEY_VALUE(name,value) \ { \ - HKEY(name).string= #name ; \ - HKEY(name).len=sizeof( #name ); \ + HKEY(name).string= value ; \ + HKEY(name).len=sizeof( value ); \ HKEY(name).hash=zend_get_hash_value(HKEY(name).string,HKEY(name).len); \ } @@ -193,9 +197,9 @@ #define ALLOC_PERSISTENT_ZVAL(zp) (zp)=pallocate(NULL,sizeof(zval)) -#define MAKE_STD_PERSISTENT_ZVAL(zv) \ - ALLOC_PERSISTENT_ZVAL(zv); \ - INIT_PZVAL(zv); +#define MAKE_STD_PERSISTENT_ZVAL(zp) \ + ALLOC_PERSISTENT_ZVAL(zp); \ + INIT_PZVAL(zp); #ifndef ZVAL_ARRAY /*--------------*/ @@ -363,6 +367,7 @@ zval ** args TSRMLS_DC); static int ut_extension_loaded(char *name, int len TSRMLS_DC); +static void ut_load_extension_file(zval *file TSRMLS_DC); static void ut_load_extension(char *name, int len TSRMLS_DC); static void ut_require(char *string, zval * ret TSRMLS_DC); static inline int ut_strings_are_equal(zval * zp1, zval * zp2 TSRMLS_DC); @@ -390,6 +395,7 @@ static inline int ut_is_uri(char *path, int len TSRMLS_DC); static char *ut_mk_absolute_path(char *path, int len, int *reslen, int separ TSRMLS_DC); static int ut_rtrim(char *p TSRMLS_DC); +static void ut_path_unique_id(char prefix, zval * path, zval ** mnt, time_t *mtp TSRMLS_DC); /*============================================================================*/ #endif /* FLP_UTILS_H */