php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
dba_flatfile.c
Go to the documentation of this file.
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
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
35DBA_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
45DBA_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
55DBA_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
73DBA_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
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
100DBA_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
116DBA_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
126DBA_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
140DBA_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
158DBA_OPTIMIZE_FUNC(flatfile)
159{
160 /* dummy */
161 return SUCCESS;
162}
163
164DBA_SYNC_FUNC(flatfile)
165{
166 /* dummy */
167 return SUCCESS;
168}
169
170DBA_INFO_FUNC(flatfile)
171{
172 return estrdup(flatfile_version());
173}
174
175#endif
memset(ptr, 0, type->size)
zval * val
Definition ffi.c:4262
int flatfile_delete(flatfile *dba, datum key_datum)
Definition flatfile.c:101
datum flatfile_fetch(flatfile *dba, datum key_datum)
Definition flatfile.c:82
datum flatfile_firstkey(flatfile *dba)
Definition flatfile.c:194
datum flatfile_nextkey(flatfile *dba)
Definition flatfile.c:236
int flatfile_store(flatfile *dba, datum key_datum, datum value_datum, int mode)
Definition flatfile.c:47
const char * flatfile_version(void)
Definition flatfile.c:279
#define FLATFILE_REPLACE
Definition flatfile.h:34
#define FLATFILE_INSERT
Definition flatfile.h:33
char * mode
#define NULL
Definition gdcache.h:45
#define SUCCESS
Definition hash_sha3.c:261
PHPAPI ZEND_COLD void php_error_docref(const char *docref, int type, const char *format,...)
Definition main.c:1173
#define gval(n)
Definition minilua.c:766
#define gkey(n)
Definition minilua.c:765
unsigned char key[REFLECTION_KEY_LEN]
char * dptr
Definition flatfile.h:21
size_t dsize
Definition flatfile.h:22
datum nextkey
Definition flatfile.h:30
#define efree(ptr)
Definition zend_alloc.h:155
#define estrdup(s)
Definition zend_alloc.h:164
#define pefree(ptr, persistent)
Definition zend_alloc.h:191
#define pemalloc(size, persistent)
Definition zend_alloc.h:189
#define E_WARNING
Definition zend_errors.h:24
struct _zend_string zend_string
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
#define ZSTR_LEN(zstr)
Definition zend_string.h:69
@ FAILURE
Definition zend_types.h:61