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