1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Author: Marcin Gibula <mg@iceni.pl> |
14 +----------------------------------------------------------------------+
15 */
16
17 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif
20
21 #include "php.h"
22
23 #ifdef DBA_QDBM
24 #include "php_qdbm.h"
25
26 #ifdef QDBM_INCLUDE_FILE
27 #include QDBM_INCLUDE_FILE
28 #endif
29
30 typedef struct {
31 DEPOT *dbf;
32 } dba_qdbm_data;
33
DBA_OPEN_FUNC(qdbm)34 DBA_OPEN_FUNC(qdbm)
35 {
36 DEPOT *dbf;
37
38 switch(info->mode) {
39 case DBA_READER:
40 dbf = dpopen(ZSTR_VAL(info->path), DP_OREADER, 0);
41 break;
42 case DBA_WRITER:
43 dbf = dpopen(ZSTR_VAL(info->path), DP_OWRITER, 0);
44 break;
45 case DBA_CREAT:
46 dbf = dpopen(ZSTR_VAL(info->path), DP_OWRITER | DP_OCREAT, 0);
47 break;
48 case DBA_TRUNC:
49 dbf = dpopen(ZSTR_VAL(info->path), DP_OWRITER | DP_OCREAT | DP_OTRUNC, 0);
50 break;
51 default:
52 return FAILURE;
53 }
54
55 if (dbf) {
56 info->dbf = pemalloc(sizeof(dba_qdbm_data), info->flags & DBA_PERSISTENT);
57 memset(info->dbf, 0, sizeof(dba_qdbm_data));
58 ((dba_qdbm_data *) info->dbf)->dbf = dbf;
59 return SUCCESS;
60 }
61
62 *error = (char *) dperrmsg(dpecode);
63 return FAILURE;
64 }
65
DBA_CLOSE_FUNC(qdbm)66 DBA_CLOSE_FUNC(qdbm)
67 {
68 dba_qdbm_data *dba = info->dbf;
69
70 dpclose(dba->dbf);
71 pefree(dba, info->flags & DBA_PERSISTENT);
72 }
73
DBA_FETCH_FUNC(qdbm)74 DBA_FETCH_FUNC(qdbm)
75 {
76 dba_qdbm_data *dba = info->dbf;
77 char *value;
78 int value_size;
79 zend_string *fetched_val = NULL;
80
81 value = dpget(dba->dbf, ZSTR_VAL(key), ZSTR_LEN(key), 0, -1, &value_size);
82 if (value) {
83 fetched_val = zend_string_init(value, value_size, /* persistent */ false);
84 free(value);
85 }
86
87 return fetched_val;
88 }
89
DBA_UPDATE_FUNC(qdbm)90 DBA_UPDATE_FUNC(qdbm)
91 {
92 dba_qdbm_data *dba = info->dbf;
93
94 if (dpput(dba->dbf, ZSTR_VAL(key), ZSTR_LEN(key), ZSTR_VAL(val), ZSTR_LEN(val), mode == 1 ? DP_DKEEP : DP_DOVER)) {
95 return SUCCESS;
96 }
97
98 if (dpecode != DP_EKEEP) {
99 php_error_docref(NULL, E_WARNING, "%s", dperrmsg(dpecode));
100 }
101
102 return FAILURE;
103 }
104
DBA_EXISTS_FUNC(qdbm)105 DBA_EXISTS_FUNC(qdbm)
106 {
107 dba_qdbm_data *dba = info->dbf;
108 char *value;
109
110 value = dpget(dba->dbf, ZSTR_VAL(key), ZSTR_LEN(key), 0, -1, NULL);
111 if (value) {
112 free(value);
113 return SUCCESS;
114 }
115
116 return FAILURE;
117 }
118
DBA_DELETE_FUNC(qdbm)119 DBA_DELETE_FUNC(qdbm)
120 {
121 dba_qdbm_data *dba = info->dbf;
122
123 return dpout(dba->dbf, ZSTR_VAL(key), ZSTR_LEN(key)) ? SUCCESS : FAILURE;
124 }
125
DBA_FIRSTKEY_FUNC(qdbm)126 DBA_FIRSTKEY_FUNC(qdbm)
127 {
128 dba_qdbm_data *dba = info->dbf;
129 int value_size;
130 char *value;
131 zend_string *key = NULL;
132
133 dpiterinit(dba->dbf);
134
135 value = dpiternext(dba->dbf, &value_size);
136 if (value) {
137 key = zend_string_init(value, value_size, /* persistent */ false);
138 free(value);
139 }
140
141 return key;
142 }
143
DBA_NEXTKEY_FUNC(qdbm)144 DBA_NEXTKEY_FUNC(qdbm)
145 {
146 dba_qdbm_data *dba = info->dbf;
147 int value_size;
148 char *value;
149 zend_string *key = NULL;
150
151 value = dpiternext(dba->dbf, &value_size);
152 if (value) {
153 key = zend_string_init(value, value_size, /* persistent */ false);
154 free(value);
155 }
156
157 return key;
158 }
159
DBA_OPTIMIZE_FUNC(qdbm)160 DBA_OPTIMIZE_FUNC(qdbm)
161 {
162 dba_qdbm_data *dba = info->dbf;
163
164 dpoptimize(dba->dbf, 0);
165 return SUCCESS;
166 }
167
DBA_SYNC_FUNC(qdbm)168 DBA_SYNC_FUNC(qdbm)
169 {
170 dba_qdbm_data *dba = info->dbf;
171
172 dpsync(dba->dbf);
173 return SUCCESS;
174 }
175
DBA_INFO_FUNC(qdbm)176 DBA_INFO_FUNC(qdbm)
177 {
178 return estrdup(dpversion);
179 }
180
181 #endif
182