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