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