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