1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2008 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Marcus Boerger <helly@php.net> |
| based on ext/db/db.c by: |
| Rasmus Lerdorf <rasmus@php.net> |
| Jim Winstead <jimw@php.net> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_globals.h"
#include "safe_mode.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "flatfile.h"
#define FLATFILE_BLOCK_SIZE 1024
/*
* ret = -1 means that database was opened for read-only
* ret = 0 success
* ret = 1 key already exists - nothing done
*/
/* {{{ flatfile_store
*/
int flatfile_store(flatfile *dba, datum key_datum, datum value_datum, int mode TSRMLS_DC) {
if (mode == FLATFILE_INSERT) {
if (flatfile_findkey(dba, key_datum TSRMLS_CC)) {
return 1;
}
php_stream_seek(dba->fp, 0L, SEEK_END);
php_stream_printf(dba->fp TSRMLS_CC, "%d\n", key_datum.dsize);
php_stream_flush(dba->fp);
if (php_stream_write(dba->fp, key_datum.dptr, key_datum.dsize) < key_datum.dsize) {
return -1;
}
php_stream_printf(dba->fp TSRMLS_CC, "%d\n", value_datum.dsize);
php_stream_flush(dba->fp);
if (php_stream_write(dba->fp, value_datum.dptr, value_datum.dsize) < value_datum.dsize) {
return -1;
}
} else { /* FLATFILE_REPLACE */
flatfile_delete(dba, key_datum TSRMLS_CC);
php_stream_printf(dba->fp TSRMLS_CC, "%d\n", key_datum.dsize);
php_stream_flush(dba->fp);
if (php_stream_write(dba->fp, key_datum.dptr, key_datum.dsize) < key_datum.dsize) {
return -1;
}
php_stream_printf(dba->fp TSRMLS_CC, "%d\n", value_datum.dsize);
if (php_stream_write(dba->fp, value_datum.dptr, value_datum.dsize) < value_datum.dsize) {
return -1;
}
}
php_stream_flush(dba->fp);
return 0;
}
/* }}} */
/* {{{ flatfile_fetch
*/
datum flatfile_fetch(flatfile *dba, datum key_datum TSRMLS_DC) {
datum value_datum = {NULL, 0};
char buf[16];
if (flatfile_findkey(dba, key_datum TSRMLS_CC)) {
if (php_stream_gets(dba->fp, buf, sizeof(buf))) {
value_datum.dsize = atoi(buf);
value_datum.dptr = safe_emalloc(value_datum.dsize, 1, 1);
value_datum.dsize = php_stream_read(dba->fp, value_datum.dptr, value_datum.dsize);
} else {
value_datum.dptr = NULL;
value_datum.dsize = 0;
}
}
return value_datum;
}
/* }}} */
/* {{{ flatfile_delete
*/
int flatfile_delete(flatfile *dba, datum key_datum TSRMLS_DC) {
char *key = key_datum.dptr;
size_t size = key_datum.dsize;
size_t buf_size = FLATFILE_BLOCK_SIZE;
char *buf = emalloc(buf_size);
size_t num;
size_t pos;
php_stream_rewind(dba->fp);
while(!php_stream_eof(dba->fp)) {
/* read in the length of the key name */
if (!php_stream_gets(dba->fp, buf, 15)) {
break;
}
num = atoi(buf);
if (num >= buf_size) {
buf_size = num + FLATFILE_BLOCK_SIZE;
buf = erealloc(buf, buf_size);
}
pos = php_stream_tell(dba->fp);
/* read in the key name */
num = php_stream_read(dba->fp, buf, num);
if (num < 0) {
break;
}
if (size == num && !memcmp(buf, key, size)) {
php_stream_seek(dba->fp, pos, SEEK_SET);
php_stream_putc(dba->fp, 0);
php_stream_flush(dba->fp);
php_stream_seek(dba->fp, 0L, SEEK_END);
efree(buf);
return SUCCESS;
}
/* read in the length of the value */
if (!php_stream_gets(dba->fp, buf, 15)) {
break;
}
num = atoi(buf);
if (num >= buf_size) {
buf_size = num + FLATFILE_BLOCK_SIZE;
buf = erealloc(buf, buf_size);
}
/* read in the value */
num = php_stream_read(dba->fp, buf, num);
if (num < 0) {
break;
}
}
efree(buf);
return FAILURE;
}
/* }}} */
/* {{{ flatfile_findkey
*/
int flatfile_findkey(flatfile *dba, datum key_datum TSRMLS_DC) {
size_t buf_size = FLATFILE_BLOCK_SIZE;
char *buf = emalloc(buf_size);
size_t num;
int ret=0;
void *key = key_datum.dptr;
size_t size = key_datum.dsize;
php_stream_rewind(dba->fp);
while (!php_stream_eof(dba->fp)) {
if (!php_stream_gets(dba->fp, buf, 15)) {
break;
}
num = atoi(buf);
if (num >= buf_size) {
buf_size = num + FLATFILE_BLOCK_SIZE;
buf = erealloc(buf, buf_size);
}
num = php_stream_read(dba->fp, buf, num);
if (num < 0) {
break;
}
if (size == num) {
if (!memcmp(buf, key, size)) {
ret = 1;
break;
}
}
if (!php_stream_gets(dba->fp, buf, 15)) {
break;
}
num = atoi(buf);
if (num >= buf_size) {
buf_size = num + FLATFILE_BLOCK_SIZE;
buf = erealloc(buf, buf_size);
}
num = php_stream_read(dba->fp, buf, num);
if (num < 0) {
break;
}
}
efree(buf);
return ret;
}
/* }}} */
/* {{{ flatfile_firstkey
*/
datum flatfile_firstkey(flatfile *dba TSRMLS_DC) {
datum res;
size_t num;
size_t buf_size = FLATFILE_BLOCK_SIZE;
char *buf = emalloc(buf_size);
php_stream_rewind(dba->fp);
while(!php_stream_eof(dba->fp)) {
if (!php_stream_gets(dba->fp, buf, 15)) {
break;
}
num = atoi(buf);
if (num >= buf_size) {
buf_size = num + FLATFILE_BLOCK_SIZE;
buf = erealloc(buf, buf_size);
}
num = php_stream_read(dba->fp, buf, num);
if (num < 0) {
break;
}
if (*(buf) != 0) {
dba->CurrentFlatFilePos = php_stream_tell(dba->fp);
res.dptr = buf;
res.dsize = num;
return res;
}
if (!php_stream_gets(dba->fp, buf, 15)) {
break;
}
num = atoi(buf);
if (num >= buf_size) {
buf_size = num + FLATFILE_BLOCK_SIZE;
buf = erealloc(buf, buf_size);
}
num = php_stream_read(dba->fp, buf, num);
if (num < 0) {
break;
}
}
efree(buf);
res.dptr = NULL;
res.dsize = 0;
return res;
}
/* }}} */
/* {{{ flatfile_nextkey
*/
datum flatfile_nextkey(flatfile *dba TSRMLS_DC) {
datum res;
size_t num;
size_t buf_size = FLATFILE_BLOCK_SIZE;
char *buf = emalloc(buf_size);
php_stream_seek(dba->fp, dba->CurrentFlatFilePos, SEEK_SET);
while(!php_stream_eof(dba->fp)) {
if (!php_stream_gets(dba->fp, buf, 15)) {
break;
}
num = atoi(buf);
if (num >= buf_size) {
buf_size = num + FLATFILE_BLOCK_SIZE;
buf = erealloc(buf, buf_size);
}
num = php_stream_read(dba->fp, buf, num);
if (num < 0) {
break;
}
if (!php_stream_gets(dba->fp, buf, 15)) {
break;
}
num = atoi(buf);
if (num >= buf_size) {
buf_size = num + FLATFILE_BLOCK_SIZE;
buf = erealloc(buf, buf_size);
}
num = php_stream_read(dba->fp, buf, num);
if (num < 0) {
break;
}
if (*(buf)!=0) {
dba->CurrentFlatFilePos = php_stream_tell(dba->fp);
res.dptr = buf;
res.dsize = num;
return res;
}
}
efree(buf);
res.dptr = NULL;
res.dsize = 0;
return res;
}
/* }}} */
/* {{{ flatfile_version */
char *flatfile_version()
{
return "1.0, $Revision$";
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/
|