xref: /PHP-7.2/ext/dba/dba_flatfile.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: Marcus Boerger <helly@php.net>                               |
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_FLATFILE
28 #include "php_flatfile.h"
29 
30 #include "libflatfile/flatfile.h"
31 
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 
39 #define FLATFILE_DATA flatfile *dba = info->dbf
40 #define FLATFILE_GKEY datum gkey; gkey.dptr = (char *) key; gkey.dsize = keylen
41 
DBA_OPEN_FUNC(flatfile)42 DBA_OPEN_FUNC(flatfile)
43 {
44 	info->dbf = pemalloc(sizeof(flatfile), info->flags&DBA_PERSISTENT);
45 	memset(info->dbf, 0, sizeof(flatfile));
46 
47 	((flatfile*)info->dbf)->fp = info->fp;
48 
49 	return SUCCESS;
50 }
51 
DBA_CLOSE_FUNC(flatfile)52 DBA_CLOSE_FUNC(flatfile)
53 {
54 	FLATFILE_DATA;
55 
56 	if (dba->nextkey.dptr) {
57 		efree(dba->nextkey.dptr);
58 	}
59 	pefree(dba, info->flags&DBA_PERSISTENT);
60 }
61 
DBA_FETCH_FUNC(flatfile)62 DBA_FETCH_FUNC(flatfile)
63 {
64 	datum gval;
65 	char *new = NULL;
66 
67 	FLATFILE_DATA;
68 	FLATFILE_GKEY;
69 
70 	gval = flatfile_fetch(dba, gkey);
71 	if (gval.dptr) {
72 		if (newlen) {
73 			*newlen = gval.dsize;
74 		}
75 		new = estrndup(gval.dptr, gval.dsize);
76 		efree(gval.dptr);
77 	}
78 	return new;
79 }
80 
DBA_UPDATE_FUNC(flatfile)81 DBA_UPDATE_FUNC(flatfile)
82 {
83 	datum gval;
84 
85 	FLATFILE_DATA;
86 	FLATFILE_GKEY;
87 	gval.dptr = (char *) val;
88 	gval.dsize = vallen;
89 
90 	switch(flatfile_store(dba, gkey, gval, mode==1 ? FLATFILE_INSERT : FLATFILE_REPLACE)) {
91 		case 0:
92 			return SUCCESS;
93 		case 1:
94 			return FAILURE;
95 		case -1:
96 			php_error_docref1(NULL, key, E_WARNING, "Operation not possible");
97 			return FAILURE;
98 		default:
99 			php_error_docref2(NULL, key, val, E_WARNING, "Unknown return value");
100 			return FAILURE;
101 	}
102 }
103 
DBA_EXISTS_FUNC(flatfile)104 DBA_EXISTS_FUNC(flatfile)
105 {
106 	datum gval;
107 	FLATFILE_DATA;
108 	FLATFILE_GKEY;
109 
110 	gval = flatfile_fetch(dba, gkey);
111 	if (gval.dptr) {
112 		efree(gval.dptr);
113 		return SUCCESS;
114 	}
115 	return FAILURE;
116 }
117 
DBA_DELETE_FUNC(flatfile)118 DBA_DELETE_FUNC(flatfile)
119 {
120 	FLATFILE_DATA;
121 	FLATFILE_GKEY;
122 	return(flatfile_delete(dba, gkey) == -1 ? FAILURE : SUCCESS);
123 }
124 
DBA_FIRSTKEY_FUNC(flatfile)125 DBA_FIRSTKEY_FUNC(flatfile)
126 {
127 	FLATFILE_DATA;
128 
129 	if (dba->nextkey.dptr) {
130 		efree(dba->nextkey.dptr);
131 	}
132 	dba->nextkey = flatfile_firstkey(dba);
133 	if (dba->nextkey.dptr) {
134 		if (newlen)  {
135 			*newlen = dba->nextkey.dsize;
136 		}
137 		return estrndup(dba->nextkey.dptr, dba->nextkey.dsize);
138 	}
139 	return NULL;
140 }
141 
DBA_NEXTKEY_FUNC(flatfile)142 DBA_NEXTKEY_FUNC(flatfile)
143 {
144 	FLATFILE_DATA;
145 
146 	if (!dba->nextkey.dptr) {
147 		return NULL;
148 	}
149 
150 	if (dba->nextkey.dptr) {
151 		efree(dba->nextkey.dptr);
152 	}
153 	dba->nextkey = flatfile_nextkey(dba);
154 	if (dba->nextkey.dptr) {
155 		if (newlen) {
156 			*newlen = dba->nextkey.dsize;
157 		}
158 		return estrndup(dba->nextkey.dptr, dba->nextkey.dsize);
159 	}
160 	return NULL;
161 }
162 
DBA_OPTIMIZE_FUNC(flatfile)163 DBA_OPTIMIZE_FUNC(flatfile)
164 {
165 	/* dummy */
166 	return SUCCESS;
167 }
168 
DBA_SYNC_FUNC(flatfile)169 DBA_SYNC_FUNC(flatfile)
170 {
171 	/* dummy */
172 	return SUCCESS;
173 }
174 
DBA_INFO_FUNC(flatfile)175 DBA_INFO_FUNC(flatfile)
176 {
177 	return estrdup(flatfile_version());
178 }
179 
180 #endif
181 
182 /*
183  * Local variables:
184  * tab-width: 4
185  * c-basic-offset: 4
186  * End:
187  * vim600: sw=4 ts=4 fdm=marker
188  * vim<600: sw=4 ts=4
189  */
190