xref: /PHP-8.3/ext/dba/dba_gdbm.c (revision 421c56dd)
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: Sascha Schumann <sascha@schumann.cx>                         |
14    +----------------------------------------------------------------------+
15  */
16 
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include "php.h"
22 
23 #ifdef DBA_GDBM
24 #include "php_gdbm.h"
25 
26 #ifdef GDBM_INCLUDE_FILE
27 #include GDBM_INCLUDE_FILE
28 #endif
29 
30 typedef struct {
31 	GDBM_FILE dbf;
32 	datum nextkey;
33 } dba_gdbm_data;
34 
DBA_OPEN_FUNC(gdbm)35 DBA_OPEN_FUNC(gdbm)
36 {
37 	GDBM_FILE dbf;
38 	int gmode = 0;
39 	int filemode = info->file_permission;
40 
41 	gmode = info->mode == DBA_READER ? GDBM_READER :
42 		info->mode == DBA_WRITER ? GDBM_WRITER :
43 		info->mode == DBA_CREAT  ? GDBM_WRCREAT :
44 		info->mode == DBA_TRUNC ? GDBM_NEWDB : -1;
45 
46 	if(gmode == -1)
47 		return FAILURE; /* not possible */
48 
49 	dbf = gdbm_open(ZSTR_VAL(info->path), /* int block_size */ 0, gmode, filemode, NULL);
50 
51 	if(dbf) {
52 		info->dbf = pemalloc(sizeof(dba_gdbm_data), info->flags&DBA_PERSISTENT);
53 		memset(info->dbf, 0, sizeof(dba_gdbm_data));
54 		((dba_gdbm_data *) info->dbf)->dbf = dbf;
55 		return SUCCESS;
56 	}
57 	*error = (char *)gdbm_strerror(gdbm_errno);
58 	return FAILURE;
59 }
60 
DBA_CLOSE_FUNC(gdbm)61 DBA_CLOSE_FUNC(gdbm)
62 {
63 	dba_gdbm_data *dba = info->dbf;
64 
65 	if(dba->nextkey.dptr) free(dba->nextkey.dptr);
66 	gdbm_close(dba->dbf);
67 	pefree(dba, info->flags&DBA_PERSISTENT);
68 }
69 
DBA_FETCH_FUNC(gdbm)70 DBA_FETCH_FUNC(gdbm)
71 {
72 	dba_gdbm_data *dba = info->dbf;
73 	datum gval;
74 	datum gkey;
75 	zend_string *fetched_val = NULL;
76 
77 	gkey.dptr = ZSTR_VAL(key);
78 	gkey.dsize = ZSTR_LEN(key);
79 
80 	gval = gdbm_fetch(dba->dbf, gkey);
81 	if (gval.dptr) {
82 		fetched_val = zend_string_init(gval.dptr, gval.dsize, /* persistent */ false);
83 		free(gval.dptr);
84 	}
85 	return fetched_val;
86 }
87 
DBA_UPDATE_FUNC(gdbm)88 DBA_UPDATE_FUNC(gdbm)
89 {
90 	dba_gdbm_data *dba = info->dbf;
91 	datum gval;
92 	datum gkey;
93 
94 	gkey.dptr = ZSTR_VAL(key);
95 	gkey.dsize = ZSTR_LEN(key);
96 	gval.dptr = ZSTR_VAL(val);
97 	gval.dsize = ZSTR_LEN(val);
98 
99 	switch (gdbm_store(dba->dbf, gkey, gval, mode == 1 ? GDBM_INSERT : GDBM_REPLACE)) {
100 		case 0:
101 			return SUCCESS;
102 		case 1:
103 			return FAILURE;
104 		case -1:
105 			// TODO Check when this happens and confirm this can even happen
106 			php_error_docref(NULL, E_WARNING, "%s", gdbm_strerror(gdbm_errno));
107 			return FAILURE;
108 		default:
109 			// TODO Convert this to an assertion failure
110 			php_error_docref(NULL, E_WARNING, "Unknown return value");
111 			return FAILURE;
112 	}
113 }
114 
DBA_EXISTS_FUNC(gdbm)115 DBA_EXISTS_FUNC(gdbm)
116 {
117 	dba_gdbm_data *dba = info->dbf;
118 	datum gkey;
119 
120 	gkey.dptr = ZSTR_VAL(key);
121 	gkey.dsize = ZSTR_LEN(key);
122 
123 	return gdbm_exists(dba->dbf, gkey) ? SUCCESS : FAILURE;
124 }
125 
DBA_DELETE_FUNC(gdbm)126 DBA_DELETE_FUNC(gdbm)
127 {
128 	dba_gdbm_data *dba = info->dbf;
129 	datum gkey;
130 
131 	gkey.dptr = ZSTR_VAL(key);
132 	gkey.dsize = ZSTR_LEN(key);
133 
134 	return gdbm_delete(dba->dbf, gkey) == -1 ? FAILURE : SUCCESS;
135 }
136 
DBA_FIRSTKEY_FUNC(gdbm)137 DBA_FIRSTKEY_FUNC(gdbm)
138 {
139 	dba_gdbm_data *dba = info->dbf;
140 	datum gkey;
141 	zend_string *key = NULL;
142 
143 	if (dba->nextkey.dptr) {
144 		free(dba->nextkey.dptr);
145 	}
146 
147 	gkey = gdbm_firstkey(dba->dbf);
148 	if (gkey.dptr) {
149 		key = zend_string_init(gkey.dptr, gkey.dsize, /* persistent */ false);
150 		dba->nextkey = gkey;
151 	} else {
152 		dba->nextkey.dptr = NULL;
153 	}
154 	return key;
155 }
156 
DBA_NEXTKEY_FUNC(gdbm)157 DBA_NEXTKEY_FUNC(gdbm)
158 {
159 	dba_gdbm_data *dba = info->dbf;
160 	zend_string *key = NULL;
161 	datum gkey;
162 
163 	if(!dba->nextkey.dptr) { return NULL; }
164 
165 	gkey = gdbm_nextkey(dba->dbf, dba->nextkey);
166 	free(dba->nextkey.dptr);
167 	if (gkey.dptr) {
168 		key = zend_string_init(gkey.dptr, gkey.dsize, /* persistent */ false);
169 		dba->nextkey = gkey;
170 	} else {
171 		dba->nextkey.dptr = NULL;
172 	}
173 	return key;
174 }
175 
DBA_OPTIMIZE_FUNC(gdbm)176 DBA_OPTIMIZE_FUNC(gdbm)
177 {
178 	dba_gdbm_data *dba = info->dbf;
179 
180 	gdbm_reorganize(dba->dbf);
181 	return SUCCESS;
182 }
183 
DBA_SYNC_FUNC(gdbm)184 DBA_SYNC_FUNC(gdbm)
185 {
186 	dba_gdbm_data *dba = info->dbf;
187 
188 	gdbm_sync(dba->dbf);
189 	return SUCCESS;
190 }
191 
DBA_INFO_FUNC(gdbm)192 DBA_INFO_FUNC(gdbm)
193 {
194 	return estrdup(gdbm_version);
195 }
196 
197 #endif
198