]> granicus.if.org Git - php/commitdiff
Reenable dba_popen()
authorMarcus Boerger <helly@php.net>
Sun, 13 Apr 2003 19:50:28 +0000 (19:50 +0000)
committerMarcus Boerger <helly@php.net>
Sun, 13 Apr 2003 19:50:28 +0000 (19:50 +0000)
ext/dba/dba.c
ext/dba/tests/dba009.phpt [new file with mode: 0755]

index 3f21fcf9201d64fe6a658ae89703cd6494af3968..cacb2086be11345338a85b8b4438e46e8e9d2bb3 100644 (file)
@@ -52,7 +52,7 @@
  */
 function_entry dba_functions[] = {
        PHP_FE(dba_open, NULL)
-       PHP_FALIAS(dba_popen, dba_open, NULL)
+       PHP_FE(dba_popen, NULL)
        /* Disabled until 4.3.1, when persistent STDIO streams are implemented.   */
        /* PHP_FE(dba_popen, NULL) */
        PHP_FE(dba_close, NULL)
@@ -339,11 +339,28 @@ static void dba_close(dba_info *info TSRMLS_DC)
 static void dba_close_rsrc(zend_rsrc_list_entry *rsrc TSRMLS_DC)
 {
        dba_info *info = (dba_info *)rsrc->ptr; 
-       
+
        dba_close(info TSRMLS_CC);
 }
 /* }}} */
 
+/* {{{ dba_close_pe_rsrc_deleter */
+int dba_close_pe_rsrc_deleter(list_entry *le, void *pDba TSRMLS_DC)
+{
+       return le->ptr == pDba;
+}
+/* }}} */
+
+/* {{{ dba_close_pe_rsrc */
+static void dba_close_pe_rsrc(zend_rsrc_list_entry *rsrc TSRMLS_DC)
+{
+       dba_info *info = (dba_info *)rsrc->ptr; 
+
+       /* closes the resource by calling dba_close_rsrc() */
+       zend_hash_apply_with_argument(&EG(persistent_list), (apply_func_arg_t) dba_close_pe_rsrc_deleter, info TSRMLS_CC);
+}
+/* }}} */
+
 /* {{{ PHP_INI
  */
 ZEND_INI_MH(OnUpdateDefaultHandler)
@@ -386,7 +403,7 @@ PHP_MINIT_FUNCTION(dba)
        ZEND_INIT_MODULE_GLOBALS(dba, php_dba_init_globals, NULL);
        REGISTER_INI_ENTRIES();
        le_db = zend_register_list_destructors_ex(dba_close_rsrc, NULL, "dba", module_number);
-       le_pdb = zend_register_list_destructors_ex(NULL, dba_close_rsrc, "dba persistent", module_number);
+       le_pdb = zend_register_list_destructors_ex(dba_close_pe_rsrc, dba_close_rsrc, "dba persistent", module_number);
        return SUCCESS;
 }
 /* }}} */
@@ -501,6 +518,7 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, int persistent)
        int lock_mode, lock_flag, lock_dbf = 0;
        char *file_mode;
        char mode[4], *pmode, *lock_file_mode = NULL;
+       int persistent_flag = persistent ? STREAM_OPEN_PERSISTENT : 0;
        
        if(ac < 2) {
                WRONG_PARAM_COUNT;
@@ -695,7 +713,7 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, int persistent)
                                /* when in read only mode try to use existing .lck file first */
                                /* do not log errors for .lck file while in read ony mode on .lck file */
                                lock_file_mode = "rb";
-                               info->lock.fp = php_stream_open_wrapper(info->lock.name, lock_file_mode, STREAM_MUST_SEEK|IGNORE_PATH|ENFORCE_SAFE_MODE, NULL);
+                               info->lock.fp = php_stream_open_wrapper(info->lock.name, lock_file_mode, STREAM_MUST_SEEK|IGNORE_PATH|ENFORCE_SAFE_MODE|persistent_flag, NULL);
                        }
                        if (!info->lock.fp) {
                                /* when not in read mode or failed to open .lck file read only. now try again in create(write) mode and log errors */
@@ -703,7 +721,7 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, int persistent)
                        }
                }
                if (!info->lock.fp) {
-                       info->lock.fp = php_stream_open_wrapper(info->lock.name, lock_file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|ENFORCE_SAFE_MODE, NULL);
+                       info->lock.fp = php_stream_open_wrapper(info->lock.name, lock_file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|ENFORCE_SAFE_MODE|persistent_flag, NULL);
                }
                if (!info->lock.fp) {
                        dba_close(info TSRMLS_CC);
@@ -724,7 +742,7 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, int persistent)
                if (info->lock.fp && lock_dbf) {
                        info->fp = info->lock.fp; /* use the same stream for locking and database access */
                } else {
-                       info->fp = php_stream_open_wrapper(info->path, file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|ENFORCE_SAFE_MODE, NULL);
+                       info->fp = php_stream_open_wrapper(info->path, file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|ENFORCE_SAFE_MODE|persistent_flag, NULL);
                }
                if (!info->fp) {
                        dba_close(info TSRMLS_CC);
@@ -751,6 +769,8 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, int persistent)
                Z_TYPE(new_le) = le_pdb;
                new_le.ptr = info;
                if (zend_hash_update(&EG(persistent_list), key, keylen+1, &new_le, sizeof(list_entry), NULL) == FAILURE) {
+                       dba_close(info TSRMLS_CC);
+                       php_error_docref2(NULL TSRMLS_CC, Z_STRVAL_PP(args[0]), Z_STRVAL_PP(args[1]), E_WARNING, "Could not register persistent resource");
                        FREENOW;
                        RETURN_FALSE;
                }
diff --git a/ext/dba/tests/dba009.phpt b/ext/dba/tests/dba009.phpt
new file mode 100755 (executable)
index 0000000..50a50c6
--- /dev/null
@@ -0,0 +1,33 @@
+--TEST--
+DBA dba_popen Test
+--SKIPIF--
+<?php 
+       require_once('skipif.inc');
+       print("info $HND handler used");
+?>
+--FILE--
+<?php
+       require_once('test.inc');
+       echo "database handler: $handler\n";
+       if (($db=dba_popen($db_file, "n", $handler))!==FALSE) {
+               echo "Opened\n";
+               dba_insert("a", "Inserted", $db);
+               echo dba_fetch("a", $db)."\n";
+               dba_close($db);
+               echo "Closed\n";
+       } else {
+               echo "Error creating database\n";
+       }
+       if (($db=dba_popen($db_file, "n", $handler))!==FALSE) {
+               echo "Opened\n";
+               dba_insert("a", "Inserted", $db);
+               echo dba_fetch("a", $db)."\n";
+       }
+?>
+--EXPECTF--
+database handler: %s
+Opened
+Inserted
+Closed
+Opened
+Inserted