PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/dba - dba_db4.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 103 109 94.5 %
Date: 2014-10-24 Functions: 12 12 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Marcus Boerger <helly@php.net>                              |
      16             :    |          Sascha Schumann <sascha@schumann.cx>                        |
      17             :    +----------------------------------------------------------------------+
      18             :  */
      19             : 
      20             : /* $Id$ */
      21             : 
      22             : #ifdef HAVE_CONFIG_H
      23             : #include "config.h"
      24             : #endif
      25             : 
      26             : #include "php.h"
      27             : 
      28             : #if DBA_DB4
      29             : #include "php_db4.h"
      30             : #include <sys/stat.h>
      31             : 
      32             : #include <string.h>
      33             : #ifdef DB4_INCLUDE_FILE
      34             : #include DB4_INCLUDE_FILE
      35             : #else
      36             : #include <db.h>
      37             : #endif
      38             : 
      39           2 : static void php_dba_db4_errcall_fcn(
      40             : #if (DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3))
      41             :         const DB_ENV *dbenv, 
      42             : #endif
      43             :         const char *errpfx, const char *msg)
      44             : {
      45             :         TSRMLS_FETCH();
      46             : 
      47             : #if (DB_VERSION_MAJOR == 5 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 8))
      48             : /* Bug 51086, Berkeley DB 4.8.26 */
      49             : /* This code suppresses a BDB 4.8+ error message, thus keeping PHP test compatibility */
      50             :         {
      51             :                 const char *function = get_active_function_name(TSRMLS_C);
      52             :                 if (function && (!strcmp(function,"dba_popen") || !strcmp(function,"dba_open"))
      53             :                         && (!strncmp(msg, "fop_read_meta", sizeof("fop_read_meta")-1)
      54             :                                 || !strncmp(msg, "BDB0004 fop_read_meta", sizeof("BDB0004 fop_read_meta")-1))) {
      55             :                         return;
      56             :                 }
      57             :         }
      58             : #endif
      59             : 
      60           2 :         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "%s%s", errpfx?errpfx:"", msg);
      61           2 : }
      62             : 
      63             : #define DB4_DATA dba_db4_data *dba = info->dbf
      64             : #define DB4_GKEY \
      65             :         DBT gkey; \
      66             :         memset(&gkey, 0, sizeof(gkey)); \
      67             :         gkey.data = (char *) key; gkey.size = keylen
      68             : 
      69             : typedef struct {
      70             :         DB *dbp;
      71             :         DBC *cursor;
      72             : } dba_db4_data;
      73             : 
      74          35 : DBA_OPEN_FUNC(db4)
      75             : {
      76          35 :         DB *dbp = NULL;
      77             :         DBTYPE type;
      78          35 :         int gmode = 0, err;
      79          35 :         int filemode = 0644;
      80             :         struct stat check_stat;
      81          35 :         int s = VCWD_STAT(info->path, &check_stat);
      82             : 
      83             : #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR <= 7)  /* Bug 51086 */
      84          35 :         if (!s && !check_stat.st_size) {
      85          21 :                 info->mode = DBA_TRUNC; /* force truncate */
      86             :         }
      87             : 
      88          35 :         type = info->mode == DBA_READER ? DB_UNKNOWN :
      89             :                 info->mode == DBA_TRUNC ? DB_BTREE :
      90             :                 s ? DB_BTREE : DB_UNKNOWN;
      91             :           
      92          57 :         gmode = info->mode == DBA_READER ? DB_RDONLY :
      93             :                 (info->mode == DBA_CREAT && s) ? DB_CREATE : 
      94             :                 (info->mode == DBA_CREAT && !s) ? 0 :
      95             :                 info->mode == DBA_WRITER ? 0         : 
      96          22 :                 info->mode == DBA_TRUNC ? DB_CREATE | DB_TRUNCATE : -1;
      97             : #else
      98             :         if (!s && !check_stat.st_size) {
      99             :                 info->mode = DBA_CREAT; /* force creation */
     100             :         }
     101             : 
     102             :         type = info->mode == DBA_READER ? DB_UNKNOWN :
     103             :                 (info->mode == DBA_TRUNC || info->mode == DBA_CREAT) ? DB_BTREE :
     104             :                 s ? DB_BTREE : DB_UNKNOWN;
     105             :           
     106             :         gmode = info->mode == DBA_READER ? DB_RDONLY :
     107             :                 info->mode == DBA_CREAT ? DB_CREATE : 
     108             :                 info->mode == DBA_WRITER ? 0         : 
     109             :                 info->mode == DBA_TRUNC ? DB_CREATE | DB_TRUNCATE : -1;
     110             : #endif
     111             : 
     112          35 :         if (gmode == -1) {
     113           0 :                 return FAILURE; /* not possible */
     114             :         }
     115             : 
     116          35 :         if (info->flags & DBA_PERSISTENT) {
     117          14 :                 gmode |= DB_THREAD;
     118             :         }
     119             : 
     120          35 :         if (info->argc > 0) {
     121           0 :                 convert_to_long_ex(info->argv[0]);
     122           0 :                 filemode = Z_LVAL_PP(info->argv[0]);
     123             :         }
     124             : 
     125          35 :         if ((err=db_create(&dbp, NULL, 0)) == 0) {
     126          35 :             dbp->set_errcall(dbp, php_dba_db4_errcall_fcn);
     127          35 :             if (
     128             : #if (DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1))
     129          35 :                         (err=dbp->open(dbp, 0, info->path, NULL, type, gmode, filemode)) == 0) {
     130             : #else
     131             :                         (err=dbp->open(dbp, info->path, NULL, type, gmode, filemode)) == 0) {
     132             : #endif
     133             :                         dba_db4_data *data;
     134             : 
     135          31 :                         data = pemalloc(sizeof(*data), info->flags&DBA_PERSISTENT);
     136          31 :                         data->dbp = dbp;
     137          31 :                         data->cursor = NULL;
     138          31 :                         info->dbf = data;
     139             :                 
     140          31 :                         return SUCCESS;
     141             :                 } else {
     142           4 :                         dbp->close(dbp, 0);
     143           4 :                         *error = db_strerror(err);
     144             :                 }
     145             :         } else {
     146           0 :                 *error = db_strerror(err);
     147             :         }
     148             : 
     149           4 :         return FAILURE;
     150             : }
     151             : 
     152          31 : DBA_CLOSE_FUNC(db4)
     153             : {
     154          31 :         DB4_DATA;
     155             :         
     156          31 :         if (dba->cursor) dba->cursor->c_close(dba->cursor);
     157          31 :         dba->dbp->close(dba->dbp, 0);
     158          31 :         pefree(dba, info->flags&DBA_PERSISTENT);
     159          31 : }
     160             : 
     161          33 : DBA_FETCH_FUNC(db4)
     162             : {
     163             :         DBT gval;
     164          33 :         char *new = NULL;
     165          33 :         DB4_DATA;
     166          33 :         DB4_GKEY;
     167             :         
     168          33 :         memset(&gval, 0, sizeof(gval));
     169          33 :         if (info->flags & DBA_PERSISTENT) {
     170           9 :                 gval.flags |= DB_DBT_MALLOC;
     171             :         }
     172          33 :         if (!dba->dbp->get(dba->dbp, NULL, &gkey, &gval, 0)) {
     173          32 :                 if (newlen) *newlen = gval.size;
     174          32 :                 new = estrndup(gval.data, gval.size);
     175          32 :                 if (info->flags & DBA_PERSISTENT) {
     176           8 :                         free(gval.data);
     177             :                 }
     178             :         }
     179          33 :         return new;
     180             : }
     181             : 
     182          41 : DBA_UPDATE_FUNC(db4)
     183             : {
     184             :         DBT gval;
     185          41 :         DB4_DATA;
     186          41 :         DB4_GKEY;
     187             :         
     188          41 :         memset(&gval, 0, sizeof(gval));
     189          41 :         gval.data = (char *) val;
     190          41 :         gval.size = vallen;
     191             : 
     192          41 :         if (!dba->dbp->put(dba->dbp, NULL, &gkey, &gval, 
     193             :                                 mode == 1 ? DB_NOOVERWRITE : 0)) {
     194          37 :                 return SUCCESS;
     195             :         }
     196           4 :         return FAILURE;
     197             : }
     198             : 
     199          15 : DBA_EXISTS_FUNC(db4)
     200             : {
     201             :         DBT gval;
     202          15 :         DB4_DATA;
     203          15 :         DB4_GKEY;
     204             :         
     205          15 :         memset(&gval, 0, sizeof(gval));
     206             :         
     207          15 :         if (info->flags & DBA_PERSISTENT) {
     208           1 :                 gval.flags |= DB_DBT_MALLOC;
     209             :         }
     210             : 
     211          15 :         if (!dba->dbp->get(dba->dbp, NULL, &gkey, &gval, 0)) {
     212          11 :                 if (info->flags & DBA_PERSISTENT) {
     213           1 :                         free(gval.data);
     214             :                 }
     215          11 :                 return SUCCESS;
     216             :         }
     217           4 :         return FAILURE;
     218             : }
     219             : 
     220           6 : DBA_DELETE_FUNC(db4)
     221             : {
     222           6 :         DB4_DATA;
     223           6 :         DB4_GKEY;
     224             : 
     225           6 :         return dba->dbp->del(dba->dbp, NULL, &gkey, 0) ? FAILURE : SUCCESS;
     226             : }
     227             : 
     228           5 : DBA_FIRSTKEY_FUNC(db4)
     229             : {
     230           5 :         DB4_DATA;
     231             : 
     232           5 :         if (dba->cursor) {
     233           0 :                 dba->cursor->c_close(dba->cursor);
     234             :         }
     235             : 
     236           5 :         dba->cursor = NULL;
     237           5 :         if (dba->dbp->cursor(dba->dbp, NULL, &dba->cursor, 0) != 0) {
     238           0 :                 return NULL;
     239             :         }
     240             : 
     241             :         /* we should introduce something like PARAM_PASSTHRU... */
     242           5 :         return dba_nextkey_db4(info, newlen TSRMLS_CC);
     243             : }
     244             : 
     245          19 : DBA_NEXTKEY_FUNC(db4)
     246             : {
     247          19 :         DB4_DATA;
     248             :         DBT gkey, gval;
     249          19 :         char *nkey = NULL;
     250             :         
     251          19 :         memset(&gkey, 0, sizeof(gkey));
     252          19 :         memset(&gval, 0, sizeof(gval));
     253             : 
     254          19 :         if (info->flags & DBA_PERSISTENT) {
     255           2 :                 gkey.flags |= DB_DBT_MALLOC;
     256           2 :                 gval.flags |= DB_DBT_MALLOC;
     257             :         }
     258          19 :         if (dba->cursor && dba->cursor->c_get(dba->cursor, &gkey, &gval, DB_NEXT) == 0) {
     259          14 :                 if (gkey.data) {
     260          14 :                         nkey = estrndup(gkey.data, gkey.size);
     261          14 :                         if (newlen) *newlen = gkey.size;
     262             :                 }
     263          14 :                 if (info->flags & DBA_PERSISTENT) {
     264           2 :                         if (gkey.data) {
     265           2 :                                 free(gkey.data);
     266             :                         }
     267           2 :                         if (gval.data) {
     268           2 :                                 free(gval.data);
     269             :                         }
     270             :                 }
     271             :         }
     272             : 
     273          19 :         return nkey;
     274             : }
     275             : 
     276           1 : DBA_OPTIMIZE_FUNC(db4)
     277             : {
     278           1 :         return SUCCESS;
     279             : }
     280             : 
     281           1 : DBA_SYNC_FUNC(db4)
     282             : {
     283           1 :         DB4_DATA;
     284             : 
     285           1 :         return dba->dbp->sync(dba->dbp, 0) ? FAILURE : SUCCESS;
     286             : }
     287             : 
     288           2 : DBA_INFO_FUNC(db4)
     289             : {
     290           2 :         return estrdup(DB_VERSION_STRING);
     291             : }
     292             : 
     293             : #endif
     294             : 
     295             : /*
     296             :  * Local variables:
     297             :  * tab-width: 4
     298             :  * c-basic-offset: 4
     299             :  * End:
     300             :  * vim600: sw=4 ts=4 fdm=marker
     301             :  * vim<600: sw=4 ts=4
     302             :  */

Generated by: LCOV version 1.10

Generated at Fri, 24 Oct 2014 05:21:45 +0000 (8 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.