Back to index

php5  5.3.10
dba_dbm.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Author: Sascha Schumann <sascha@schumann.cx>                         |
00016    +----------------------------------------------------------------------+
00017  */
00018 
00019 /* $Id: dba_dbm.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 #ifdef HAVE_CONFIG_H
00022 #include "config.h"
00023 #endif
00024 
00025 #include "php.h"
00026 
00027 #if DBA_DBM
00028 #include "php_dbm.h"
00029 
00030 #ifdef DBM_INCLUDE_FILE
00031 #include DBM_INCLUDE_FILE
00032 #endif
00033 #if DBA_GDBM
00034 #include "php_gdbm.h"
00035 #endif
00036 
00037 #include <unistd.h>
00038 #include <sys/types.h>
00039 #include <sys/stat.h>
00040 #include <fcntl.h>
00041 
00042 #define DBM_DATA dba_dbm_data *dba = info->dbf
00043 #define DBM_GKEY datum gkey; gkey.dptr = (char *) key; gkey.dsize = keylen
00044 
00045 #define TRUNC_IT(extension, mode) \
00046        snprintf(buf, MAXPATHLEN, "%s" extension, info->path); \
00047        buf[MAXPATHLEN-1] = '\0'; \
00048        if((fd = VCWD_OPEN_MODE(buf, O_CREAT | mode | O_WRONLY, filemode)) == -1) \
00049               return FAILURE; \
00050        close(fd);
00051 
00052 
00053 typedef struct {
00054        datum nextkey;
00055 } dba_dbm_data;
00056 
00057 DBA_OPEN_FUNC(dbm)
00058 {
00059        int fd;
00060        int filemode = 0644;
00061 
00062        if(info->argc > 0) {
00063               convert_to_long_ex(info->argv[0]);
00064               filemode = Z_LVAL_PP(info->argv[0]);
00065        }
00066        
00067        if(info->mode == DBA_TRUNC) {
00068               char buf[MAXPATHLEN];
00069 
00070               /* dbm/ndbm original */
00071               TRUNC_IT(".pag", O_TRUNC);
00072               TRUNC_IT(".dir", O_TRUNC);
00073        }
00074 
00075        if(info->mode == DBA_CREAT) {
00076               char buf[MAXPATHLEN];
00077 
00078               TRUNC_IT(".pag", 0);
00079               TRUNC_IT(".dir", 0);
00080        }
00081 
00082        if(dbminit((char *) info->path) == -1) {
00083               return FAILURE;
00084        }
00085 
00086        info->dbf = pemalloc(sizeof(dba_dbm_data), info->flags&DBA_PERSISTENT);
00087        memset(info->dbf, 0, sizeof(dba_dbm_data));
00088        return SUCCESS;
00089 }
00090 
00091 DBA_CLOSE_FUNC(dbm)
00092 {
00093        pefree(info->dbf, info->flags&DBA_PERSISTENT);
00094        dbmclose();
00095 }
00096 
00097 DBA_FETCH_FUNC(dbm)
00098 {
00099        datum gval;
00100        char *new = NULL;
00101 
00102        DBM_GKEY;
00103        gval = fetch(gkey);
00104        if(gval.dptr) {
00105               if(newlen) *newlen = gval.dsize;
00106               new = estrndup(gval.dptr, gval.dsize);
00107        }
00108        return new;
00109 }
00110 
00111 DBA_UPDATE_FUNC(dbm)
00112 {
00113        datum gval;
00114 
00115        DBM_GKEY;
00116 
00117        if (mode == 1) { /* insert */
00118               gval = fetch(gkey);
00119               if(gval.dptr) {
00120                      return FAILURE;
00121               }
00122        }
00123 
00124        gval.dptr = (char *) val;
00125        gval.dsize = vallen;
00126        
00127        return (store(gkey, gval) == -1 ? FAILURE : SUCCESS);
00128 }
00129 
00130 DBA_EXISTS_FUNC(dbm)
00131 {
00132        datum gval;
00133        DBM_GKEY;
00134        
00135        gval = fetch(gkey);
00136        if(gval.dptr) {
00137               return SUCCESS;
00138        }
00139        return FAILURE;
00140 }
00141 
00142 DBA_DELETE_FUNC(dbm)
00143 {
00144        DBM_GKEY;
00145        return(delete(gkey) == -1 ? FAILURE : SUCCESS);
00146 }
00147 
00148 DBA_FIRSTKEY_FUNC(dbm)
00149 {
00150        DBM_DATA;
00151        datum gkey;
00152        char *key = NULL;
00153 
00154        gkey = firstkey();
00155        if(gkey.dptr) {
00156               if(newlen) *newlen = gkey.dsize;
00157               key = estrndup(gkey.dptr, gkey.dsize);
00158               dba->nextkey = gkey;
00159        } else
00160               dba->nextkey.dptr = NULL;
00161        return key;
00162 }
00163 
00164 DBA_NEXTKEY_FUNC(dbm)
00165 {
00166        DBM_DATA;
00167        datum gkey;
00168        char *nkey = NULL;
00169        
00170        if(!dba->nextkey.dptr) return NULL;
00171        
00172        gkey = nextkey(dba->nextkey);
00173        if(gkey.dptr) {
00174               if(newlen) *newlen = gkey.dsize;
00175               nkey = estrndup(gkey.dptr, gkey.dsize);
00176               dba->nextkey = gkey;
00177        } else
00178               dba->nextkey.dptr = NULL;
00179        return nkey;
00180 }
00181 
00182 DBA_OPTIMIZE_FUNC(dbm)
00183 {
00184        /* dummy */
00185        return SUCCESS;
00186 }
00187 
00188 DBA_SYNC_FUNC(dbm)
00189 {
00190        return SUCCESS;
00191 }
00192 
00193 DBA_INFO_FUNC(dbm)
00194 {
00195 #if DBA_GDBM
00196        if (!strcmp(DBM_VERSION, "GDBM"))
00197        {
00198               return dba_info_gdbm(hnd, info TSRMLS_CC);
00199        }
00200 #endif
00201        return estrdup(DBM_VERSION);
00202 }
00203 
00204 #endif
00205 
00206 /*
00207  * Local variables:
00208  * tab-width: 4
00209  * c-basic-offset: 4
00210  * End:
00211  * vim600: sw=4 ts=4 fdm=marker
00212  * vim<600: sw=4 ts=4
00213  */