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