xref: /PHP-8.1/ext/dba/dba_qdbm.c (revision 01b3fc03)
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 #if DBA_QDBM
24 #include "php_qdbm.h"
25 
26 #ifdef QDBM_INCLUDE_FILE
27 #include QDBM_INCLUDE_FILE
28 #endif
29 
30 #define QDBM_DATA dba_qdbm_data *dba = info->dbf
31 
32 typedef struct {
33 	DEPOT *dbf;
34 } dba_qdbm_data;
35 
DBA_OPEN_FUNC(qdbm)36 DBA_OPEN_FUNC(qdbm)
37 {
38 	DEPOT *dbf;
39 
40 	switch(info->mode) {
41 		case DBA_READER:
42 			dbf = dpopen(info->path, DP_OREADER, 0);
43 			break;
44 		case DBA_WRITER:
45 			dbf = dpopen(info->path, DP_OWRITER, 0);
46 			break;
47 		case DBA_CREAT:
48 			dbf = dpopen(info->path, DP_OWRITER | DP_OCREAT, 0);
49 			break;
50 		case DBA_TRUNC:
51 			dbf = dpopen(info->path, DP_OWRITER | DP_OCREAT | DP_OTRUNC, 0);
52 			break;
53 		default:
54 			return FAILURE;
55 	}
56 
57 	if (dbf) {
58 		info->dbf = pemalloc(sizeof(dba_qdbm_data), info->flags & DBA_PERSISTENT);
59 		memset(info->dbf, 0, sizeof(dba_qdbm_data));
60 		((dba_qdbm_data *) info->dbf)->dbf = dbf;
61 		return SUCCESS;
62 	}
63 
64 	*error = (char *) dperrmsg(dpecode);
65 	return FAILURE;
66 }
67 
DBA_CLOSE_FUNC(qdbm)68 DBA_CLOSE_FUNC(qdbm)
69 {
70 	QDBM_DATA;
71 
72 	dpclose(dba->dbf);
73 	pefree(dba, info->flags & DBA_PERSISTENT);
74 }
75 
DBA_FETCH_FUNC(qdbm)76 DBA_FETCH_FUNC(qdbm)
77 {
78 	QDBM_DATA;
79 	char *value, *new = NULL;
80 	int value_size;
81 
82 	value = dpget(dba->dbf, key, keylen, 0, -1, &value_size);
83 	if (value) {
84 		if (newlen) *newlen = value_size;
85 		new = estrndup(value, value_size);
86 		free(value);
87 	}
88 
89 	return new;
90 }
91 
DBA_UPDATE_FUNC(qdbm)92 DBA_UPDATE_FUNC(qdbm)
93 {
94 	QDBM_DATA;
95 
96 	if (dpput(dba->dbf, key, keylen, val, vallen, mode == 1 ? DP_DKEEP : DP_DOVER)) {
97 		return SUCCESS;
98 	}
99 
100 	if (dpecode != DP_EKEEP) {
101 		php_error_docref2(NULL, key, val, E_WARNING, "%s", dperrmsg(dpecode));
102 	}
103 
104 	return FAILURE;
105 }
106 
DBA_EXISTS_FUNC(qdbm)107 DBA_EXISTS_FUNC(qdbm)
108 {
109 	QDBM_DATA;
110 	char *value;
111 
112 	value = dpget(dba->dbf, key, keylen, 0, -1, NULL);
113 	if (value) {
114 		free(value);
115 		return SUCCESS;
116 	}
117 
118 	return FAILURE;
119 }
120 
DBA_DELETE_FUNC(qdbm)121 DBA_DELETE_FUNC(qdbm)
122 {
123 	QDBM_DATA;
124 
125 	return dpout(dba->dbf, key, keylen) ? SUCCESS : FAILURE;
126 }
127 
DBA_FIRSTKEY_FUNC(qdbm)128 DBA_FIRSTKEY_FUNC(qdbm)
129 {
130 	QDBM_DATA;
131 	int value_size;
132 	char *value, *new = NULL;
133 
134 	dpiterinit(dba->dbf);
135 
136 	value = dpiternext(dba->dbf, &value_size);
137 	if (value) {
138 		if (newlen) *newlen = value_size;
139 		new = estrndup(value, value_size);
140 		free(value);
141 	}
142 
143 	return new;
144 }
145 
DBA_NEXTKEY_FUNC(qdbm)146 DBA_NEXTKEY_FUNC(qdbm)
147 {
148 	QDBM_DATA;
149 	int value_size;
150 	char *value, *new = NULL;
151 
152 	value = dpiternext(dba->dbf, &value_size);
153 	if (value) {
154 		if (newlen) *newlen = value_size;
155 		new = estrndup(value, value_size);
156 		free(value);
157 	}
158 
159 	return new;
160 }
161 
DBA_OPTIMIZE_FUNC(qdbm)162 DBA_OPTIMIZE_FUNC(qdbm)
163 {
164 	QDBM_DATA;
165 
166 	dpoptimize(dba->dbf, 0);
167 	return SUCCESS;
168 }
169 
DBA_SYNC_FUNC(qdbm)170 DBA_SYNC_FUNC(qdbm)
171 {
172 	QDBM_DATA;
173 
174 	dpsync(dba->dbf);
175 	return SUCCESS;
176 }
177 
DBA_INFO_FUNC(qdbm)178 DBA_INFO_FUNC(qdbm)
179 {
180 	return estrdup(dpversion);
181 }
182 
183 #endif
184