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