xref: /PHP-7.2/ext/dba/dba_db1.c (revision 7a7ec01a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 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: Shen Cheng-Da <cdsheen@gmail.com>                            |
16    +----------------------------------------------------------------------+
17  */
18 
19 /* $Id$ */
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include "php.h"
26 
27 #if DBA_DB1
28 #include "php_db1.h"
29 
30 #ifdef DB1_INCLUDE_FILE
31 #include DB1_INCLUDE_FILE
32 #endif
33 
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 
38 #define DB1_DATA dba_db1_data *dba = info->dbf
39 #define DB1_GKEY DBT gkey; gkey.data = (char *) key; gkey.size = keylen
40 
41 typedef struct {
42 	DB  *dbp;
43 } dba_db1_data;
44 
DBA_OPEN_FUNC(db1)45 DBA_OPEN_FUNC(db1)
46 {
47 	dba_db1_data	*dba;
48 	DB		*db;
49 
50 	int gmode;
51 	int filemode = 0644;
52 
53 	if (info->argc > 0) {
54 		convert_to_long_ex(&info->argv[0]);
55 		filemode = Z_LVAL(info->argv[0]);
56 	}
57 
58 	gmode = 0;
59 	switch (info->mode) {
60 		case DBA_READER:
61 			gmode = O_RDONLY;
62 			break;
63 		case DBA_WRITER:
64 			gmode = O_RDWR;
65 			break;
66 		case DBA_CREAT:
67 			gmode = O_RDWR | O_CREAT;
68 			break;
69 		case DBA_TRUNC:
70 			gmode = O_RDWR | O_CREAT | O_TRUNC;
71 			break;
72 		default:
73 			return FAILURE; /* not possible */
74 	}
75 
76 	db = dbopen((char *)info->path, gmode, filemode, DB_HASH, NULL);
77 
78 	if (db == NULL) {
79 		return FAILURE;
80 	}
81 
82 	dba = pemalloc(sizeof(*dba), info->flags&DBA_PERSISTENT);
83 	dba->dbp = db;
84 
85 	info->dbf = dba;
86 
87 	return SUCCESS;
88 }
89 
DBA_CLOSE_FUNC(db1)90 DBA_CLOSE_FUNC(db1)
91 {
92 	DB1_DATA;
93 	dba->dbp->close(dba->dbp);
94 	pefree(info->dbf, info->flags&DBA_PERSISTENT);
95 }
96 
DBA_FETCH_FUNC(db1)97 DBA_FETCH_FUNC(db1)
98 {
99 	DBT gval;
100 	DB1_DATA;
101 	DB1_GKEY;
102 
103 	memset(&gval, 0, sizeof(gval));
104 	if (dba->dbp->get(dba->dbp, &gkey, &gval, 0) == RET_SUCCESS) {
105 		if (newlen) *newlen = gval.size;
106 		return estrndup(gval.data, gval.size);
107 	}
108 	return NULL;
109 }
110 
DBA_UPDATE_FUNC(db1)111 DBA_UPDATE_FUNC(db1)
112 {
113 	DBT gval;
114 	DB1_DATA;
115 	DB1_GKEY;
116 
117 	gval.data = (char *) val;
118 	gval.size = vallen;
119 
120 	return dba->dbp->put(dba->dbp, &gkey, &gval, mode == 1 ? R_NOOVERWRITE : 0) != RET_SUCCESS ? FAILURE : SUCCESS;
121 }
122 
DBA_EXISTS_FUNC(db1)123 DBA_EXISTS_FUNC(db1)
124 {
125 	DBT gval;
126 	DB1_DATA;
127 	DB1_GKEY;
128 
129 	return dba->dbp->get(dba->dbp, &gkey, &gval, 0) != RET_SUCCESS ? FAILURE : SUCCESS;
130 }
131 
DBA_DELETE_FUNC(db1)132 DBA_DELETE_FUNC(db1)
133 {
134 	DB1_DATA;
135 	DB1_GKEY;
136 
137 	return dba->dbp->del(dba->dbp, &gkey, 0) != RET_SUCCESS ? FAILURE : SUCCESS;
138 }
139 
DBA_FIRSTKEY_FUNC(db1)140 DBA_FIRSTKEY_FUNC(db1)
141 {
142 	DBT gkey;
143 	DBT gval;
144 	DB1_DATA;
145 
146 	memset(&gkey, 0, sizeof(gkey));
147 	memset(&gval, 0, sizeof(gval));
148 
149 	if (dba->dbp->seq(dba->dbp, &gkey, &gval, R_FIRST) == RET_SUCCESS) {
150 		if (newlen) *newlen = gkey.size;
151 		return estrndup(gkey.data, gkey.size);
152 	}
153 	return NULL;
154 }
155 
DBA_NEXTKEY_FUNC(db1)156 DBA_NEXTKEY_FUNC(db1)
157 {
158 	DBT gkey;
159 	DBT gval;
160 	DB1_DATA;
161 
162 	memset(&gkey, 0, sizeof(gkey));
163 	memset(&gval, 0, sizeof(gval));
164 
165 	if (dba->dbp->seq(dba->dbp, &gkey, &gval, R_NEXT) == RET_SUCCESS) {
166 		if (newlen) *newlen = gkey.size;
167 		return estrndup(gkey.data, gkey.size);
168 	}
169 	return NULL;
170 }
171 
DBA_OPTIMIZE_FUNC(db1)172 DBA_OPTIMIZE_FUNC(db1)
173 {
174 	/* dummy */
175 	return SUCCESS;
176 }
177 
DBA_SYNC_FUNC(db1)178 DBA_SYNC_FUNC(db1)
179 {
180 	return SUCCESS;
181 }
182 
DBA_INFO_FUNC(db1)183 DBA_INFO_FUNC(db1)
184 {
185 	return estrdup(DB1_VERSION);
186 }
187 
188 #endif
189 
190 /*
191  * Local variables:
192  * tab-width: 4
193  * c-basic-offset: 4
194  * End:
195  * vim600: sw=4 ts=4 fdm=marker
196  * vim<600: sw=4 ts=4
197  */
198