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: 2015-08-29 Functions: 12 12 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sat, 29 Aug 2015 10:22:13 +0000 (42 hours ago)

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