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 #ifdef 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
DBA_OPEN_FUNC(flatfile)35 DBA_OPEN_FUNC(flatfile)
36 {
37 info->dbf = pemalloc(sizeof(flatfile), info->flags&DBA_PERSISTENT);
38 memset(info->dbf, 0, sizeof(flatfile));
39
40 ((flatfile*)info->dbf)->fp = info->fp;
41
42 return SUCCESS;
43 }
44
DBA_CLOSE_FUNC(flatfile)45 DBA_CLOSE_FUNC(flatfile)
46 {
47 flatfile *dba = info->dbf;
48
49 if (dba->nextkey.dptr) {
50 efree(dba->nextkey.dptr);
51 }
52 pefree(dba, info->flags&DBA_PERSISTENT);
53 }
54
DBA_FETCH_FUNC(flatfile)55 DBA_FETCH_FUNC(flatfile)
56 {
57 flatfile *dba = info->dbf;
58 datum gval;
59 datum gkey;
60 zend_string *fetched_val = NULL;
61
62 gkey.dptr = ZSTR_VAL(key);
63 gkey.dsize = ZSTR_LEN(key);
64
65 gval = flatfile_fetch(dba, gkey);
66 if (gval.dptr) {
67 fetched_val = zend_string_init(gval.dptr, gval.dsize, /* persistent */ false);
68 efree(gval.dptr);
69 }
70 return fetched_val;
71 }
72
DBA_UPDATE_FUNC(flatfile)73 DBA_UPDATE_FUNC(flatfile)
74 {
75 flatfile *dba = info->dbf;
76 datum gval;
77 datum gkey;
78
79 gkey.dptr = ZSTR_VAL(key);
80 gkey.dsize = ZSTR_LEN(key);
81 gval.dptr = ZSTR_VAL(val);
82 gval.dsize = ZSTR_LEN(val);
83
84 switch(flatfile_store(dba, gkey, gval, mode==1 ? FLATFILE_INSERT : FLATFILE_REPLACE)) {
85 case 0:
86 return SUCCESS;
87 case 1:
88 return FAILURE;
89 case -1:
90 // TODO Check when this happens and confirm this can even happen
91 php_error_docref(NULL, E_WARNING, "Operation not possible");
92 return FAILURE;
93 default:
94 // TODO Convert this to an assertion failure
95 php_error_docref(NULL, E_WARNING, "Unknown return value");
96 return FAILURE;
97 }
98 }
99
DBA_EXISTS_FUNC(flatfile)100 DBA_EXISTS_FUNC(flatfile)
101 {
102 flatfile *dba = info->dbf;
103 datum gval;
104 datum gkey;
105
106 gkey.dptr = ZSTR_VAL(key);
107 gkey.dsize = ZSTR_LEN(key);
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 *dba = info->dbf;
119 datum gkey;
120
121 gkey.dptr = ZSTR_VAL(key);
122 gkey.dsize = ZSTR_LEN(key);
123 return(flatfile_delete(dba, gkey) == -1 ? FAILURE : SUCCESS);
124 }
125
DBA_FIRSTKEY_FUNC(flatfile)126 DBA_FIRSTKEY_FUNC(flatfile)
127 {
128 flatfile *dba = info->dbf;
129
130 if (dba->nextkey.dptr) {
131 efree(dba->nextkey.dptr);
132 }
133 dba->nextkey = flatfile_firstkey(dba);
134 if (dba->nextkey.dptr) {
135 return zend_string_init(dba->nextkey.dptr, dba->nextkey.dsize, /* persistent */ false);
136 }
137 return NULL;
138 }
139
DBA_NEXTKEY_FUNC(flatfile)140 DBA_NEXTKEY_FUNC(flatfile)
141 {
142 flatfile *dba = info->dbf;
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 return zend_string_init(dba->nextkey.dptr, dba->nextkey.dsize, /* persistent */ false);
154 }
155 return NULL;
156 }
157
DBA_OPTIMIZE_FUNC(flatfile)158 DBA_OPTIMIZE_FUNC(flatfile)
159 {
160 /* dummy */
161 return SUCCESS;
162 }
163
DBA_SYNC_FUNC(flatfile)164 DBA_SYNC_FUNC(flatfile)
165 {
166 /* dummy */
167 return SUCCESS;
168 }
169
DBA_INFO_FUNC(flatfile)170 DBA_INFO_FUNC(flatfile)
171 {
172 return estrdup(flatfile_version());
173 }
174
175 #endif
176