1999-07-21 23:12:32 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2014-09-20 00:33:14 +08:00
|
|
|
| PHP Version 7 |
|
1999-07-21 23:12:32 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2015-01-15 23:27:30 +08:00
|
|
|
| Copyright (c) 1997-2015 The PHP Group |
|
1999-07-21 23:12:32 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2006-01-01 20:51:34 +08:00
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
2001-06-06 21:06:12 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-11 04:04:29 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
2006-01-01 20:51:34 +08:00
|
|
|
| http://www.php.net/license/3_01.txt |
|
2001-06-06 21:06:12 +08:00
|
|
|
| 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. |
|
1999-07-21 23:12:32 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2002-11-03 23:09:49 +08:00
|
|
|
| Authors: Sascha Schumann <sascha@schumann.cx> |
|
|
|
|
| Marcus Boerger <helly@php.net> |
|
1999-07-21 23:12:32 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
2002-04-18 20:30:18 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
1999-07-21 23:12:32 +08:00
|
|
|
#include "php.h"
|
|
|
|
|
|
|
|
#if DBA_CDB
|
1999-12-05 03:19:57 +08:00
|
|
|
#include "php_cdb.h"
|
1999-07-21 23:12:32 +08:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
2002-11-13 20:12:44 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
1999-07-21 23:12:32 +08:00
|
|
|
#include <unistd.h>
|
2002-11-13 20:12:44 +08:00
|
|
|
#endif
|
1999-07-21 23:12:32 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
2002-11-05 01:53:04 +08:00
|
|
|
#if DBA_CDB_BUILTIN
|
2003-02-02 02:59:59 +08:00
|
|
|
# include "libcdb/cdb.h"
|
|
|
|
# include "libcdb/cdb_make.h"
|
|
|
|
# include "libcdb/uint32.h"
|
2002-11-05 01:53:04 +08:00
|
|
|
#else
|
2003-02-02 02:59:59 +08:00
|
|
|
# ifdef CDB_INCLUDE_FILE
|
|
|
|
# include CDB_INCLUDE_FILE
|
|
|
|
# endif
|
2002-11-05 01:53:04 +08:00
|
|
|
#endif
|
1999-07-21 23:12:32 +08:00
|
|
|
|
|
|
|
#define CDB_INFO \
|
|
|
|
dba_cdb *cdb = (dba_cdb *) info->dbf
|
|
|
|
|
|
|
|
typedef struct {
|
2002-09-19 12:18:20 +08:00
|
|
|
struct cdb c;
|
2002-11-05 01:53:04 +08:00
|
|
|
#if DBA_CDB_BUILTIN
|
|
|
|
struct cdb_make m;
|
|
|
|
php_stream *file;
|
|
|
|
int make;
|
|
|
|
#else
|
|
|
|
int file;
|
|
|
|
#endif
|
1999-07-21 23:12:32 +08:00
|
|
|
uint32 eod; /* size of constant database */
|
|
|
|
uint32 pos; /* current position for traversing */
|
|
|
|
} dba_cdb;
|
|
|
|
|
|
|
|
DBA_OPEN_FUNC(cdb)
|
|
|
|
{
|
2002-11-05 01:53:04 +08:00
|
|
|
#if DBA_CDB_BUILTIN
|
|
|
|
php_stream* file = 0;
|
|
|
|
int make;
|
|
|
|
#else
|
|
|
|
int file = 0;
|
|
|
|
#endif
|
1999-07-21 23:12:32 +08:00
|
|
|
dba_cdb *cdb;
|
|
|
|
dba_info *pinfo = (dba_info *) info;
|
|
|
|
|
2002-09-19 12:18:20 +08:00
|
|
|
switch (info->mode) {
|
2015-01-03 17:22:58 +08:00
|
|
|
case DBA_READER:
|
2002-11-05 01:53:04 +08:00
|
|
|
#if DBA_CDB_BUILTIN
|
|
|
|
make = 0;
|
2002-11-11 01:58:46 +08:00
|
|
|
file = info->fp;
|
2002-11-05 01:53:04 +08:00
|
|
|
#else
|
|
|
|
file = VCWD_OPEN(info->path, O_RDONLY);
|
|
|
|
if (file < 0) {
|
2002-11-05 22:46:36 +08:00
|
|
|
*error = "Unable to open file";
|
2002-11-05 01:53:04 +08:00
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
#if DBA_CDB_BUILTIN
|
|
|
|
case DBA_TRUNC:
|
|
|
|
make = 1;
|
2002-11-11 01:58:46 +08:00
|
|
|
file = info->fp;
|
2002-11-05 01:53:04 +08:00
|
|
|
break;
|
2003-04-15 05:50:48 +08:00
|
|
|
case DBA_CREAT:
|
2002-11-05 01:53:04 +08:00
|
|
|
case DBA_WRITER:
|
2002-11-05 22:46:36 +08:00
|
|
|
*error = "Update operations are not supported";
|
2002-11-05 01:53:04 +08:00
|
|
|
return FAILURE; /* not supported */
|
1999-07-21 23:12:32 +08:00
|
|
|
#endif
|
2015-01-03 17:22:58 +08:00
|
|
|
default:
|
2002-11-05 22:46:36 +08:00
|
|
|
*error = "Currently not supported";
|
1999-07-21 23:12:32 +08:00
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
2002-12-21 01:47:58 +08:00
|
|
|
cdb = pemalloc(sizeof(dba_cdb), info->flags&DBA_PERSISTENT);
|
2002-11-07 01:59:03 +08:00
|
|
|
memset(cdb, 0, sizeof(dba_cdb));
|
1999-07-21 23:12:32 +08:00
|
|
|
|
2002-11-05 01:53:04 +08:00
|
|
|
#if DBA_CDB_BUILTIN
|
|
|
|
if (make) {
|
2014-12-14 06:06:14 +08:00
|
|
|
cdb_make_start(&cdb->m, file);
|
2002-11-05 01:53:04 +08:00
|
|
|
} else {
|
2014-12-14 06:06:14 +08:00
|
|
|
cdb_init(&cdb->c, file);
|
2002-11-05 01:53:04 +08:00
|
|
|
}
|
|
|
|
cdb->make = make;
|
|
|
|
#else
|
|
|
|
cdb_init(&cdb->c, file);
|
|
|
|
#endif
|
|
|
|
cdb->file = file;
|
2015-01-03 17:22:58 +08:00
|
|
|
|
1999-07-21 23:12:32 +08:00
|
|
|
pinfo->dbf = cdb;
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBA_CLOSE_FUNC(cdb)
|
|
|
|
{
|
|
|
|
CDB_INFO;
|
|
|
|
|
2002-11-05 01:53:04 +08:00
|
|
|
/* cdb_free does not close associated file */
|
|
|
|
#if DBA_CDB_BUILTIN
|
|
|
|
if (cdb->make) {
|
2014-12-14 06:06:14 +08:00
|
|
|
cdb_make_finish(&cdb->m);
|
2002-11-05 01:53:04 +08:00
|
|
|
} else {
|
2014-12-14 06:06:14 +08:00
|
|
|
cdb_free(&cdb->c);
|
2002-11-05 01:53:04 +08:00
|
|
|
}
|
|
|
|
#else
|
2002-09-19 12:18:20 +08:00
|
|
|
cdb_free(&cdb->c);
|
2002-11-05 01:53:04 +08:00
|
|
|
close(cdb->file);
|
|
|
|
#endif
|
2002-12-21 01:47:58 +08:00
|
|
|
pefree(cdb, info->flags&DBA_PERSISTENT);
|
1999-07-21 23:12:32 +08:00
|
|
|
}
|
|
|
|
|
2002-11-05 01:53:04 +08:00
|
|
|
#if DBA_CDB_BUILTIN
|
2014-12-14 06:06:14 +08:00
|
|
|
# define php_cdb_read(cdb, buf, len, pos) cdb_read(cdb, buf, len, pos)
|
|
|
|
# define php_cdb_findnext(cdb, key, len) cdb_findnext(cdb, key, len)
|
|
|
|
# define php_cdb_find(cdb, key, len) cdb_find(cdb, key, len)
|
2002-11-05 01:53:04 +08:00
|
|
|
#else
|
|
|
|
# define php_cdb_read(cdb, buf, len, pos) cdb_read(cdb, buf, len, pos)
|
|
|
|
# define php_cdb_findnext(cdb, key, len) cdb_findnext(cdb, key, len)
|
|
|
|
# define php_cdb_find(cdb, key, len) cdb_find(cdb, key, len)
|
|
|
|
#endif
|
|
|
|
|
1999-07-21 23:12:32 +08:00
|
|
|
DBA_FETCH_FUNC(cdb)
|
|
|
|
{
|
|
|
|
CDB_INFO;
|
2002-09-19 12:18:20 +08:00
|
|
|
unsigned int len;
|
|
|
|
char *new_entry = NULL;
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2002-11-05 01:53:04 +08:00
|
|
|
#if DBA_CDB_BUILTIN
|
|
|
|
if (cdb->make)
|
|
|
|
return NULL; /* database was opened writeonly */
|
|
|
|
#endif
|
|
|
|
if (php_cdb_find(&cdb->c, key, keylen) == 1) {
|
2002-11-04 00:43:07 +08:00
|
|
|
while(skip--) {
|
2002-11-05 01:53:04 +08:00
|
|
|
if (php_cdb_findnext(&cdb->c, key, keylen) != 1) {
|
2002-11-04 00:43:07 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2002-09-19 12:18:20 +08:00
|
|
|
len = cdb_datalen(&cdb->c);
|
2003-04-25 04:54:43 +08:00
|
|
|
new_entry = safe_emalloc(len, 1, 1);
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2002-11-05 01:53:04 +08:00
|
|
|
if (php_cdb_read(&cdb->c, new_entry, len, cdb_datapos(&cdb->c)) == -1) {
|
2002-11-03 23:09:49 +08:00
|
|
|
efree(new_entry);
|
2002-09-19 12:18:20 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-11-03 23:09:49 +08:00
|
|
|
new_entry[len] = 0;
|
2015-01-03 17:22:58 +08:00
|
|
|
if (newlen)
|
2002-09-19 12:18:20 +08:00
|
|
|
*newlen = len;
|
1999-07-21 23:12:32 +08:00
|
|
|
}
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2002-09-19 12:18:20 +08:00
|
|
|
return new_entry;
|
1999-07-21 23:12:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DBA_UPDATE_FUNC(cdb)
|
|
|
|
{
|
2002-11-05 01:53:04 +08:00
|
|
|
#if DBA_CDB_BUILTIN
|
|
|
|
CDB_INFO;
|
|
|
|
|
|
|
|
if (!cdb->make)
|
|
|
|
return FAILURE; /* database was opened readonly */
|
|
|
|
if (!mode)
|
|
|
|
return FAILURE; /* cdb_make dosn't know replace */
|
2014-12-14 06:06:14 +08:00
|
|
|
if (cdb_make_add(&cdb->m, key, keylen, val, vallen) != -1)
|
2002-11-05 01:53:04 +08:00
|
|
|
return SUCCESS;
|
|
|
|
#endif
|
1999-07-21 23:12:32 +08:00
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBA_EXISTS_FUNC(cdb)
|
|
|
|
{
|
|
|
|
CDB_INFO;
|
|
|
|
|
2002-11-05 01:53:04 +08:00
|
|
|
#if DBA_CDB_BUILTIN
|
|
|
|
if (cdb->make)
|
|
|
|
return FAILURE; /* database was opened writeonly */
|
|
|
|
#endif
|
|
|
|
if (php_cdb_find(&cdb->c, key, keylen) == 1)
|
1999-07-21 23:12:32 +08:00
|
|
|
return SUCCESS;
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBA_DELETE_FUNC(cdb)
|
|
|
|
{
|
2002-11-05 01:53:04 +08:00
|
|
|
return FAILURE; /* cdb doesn't support delete */
|
1999-07-21 23:12:32 +08:00
|
|
|
}
|
|
|
|
|
2002-11-05 01:53:04 +08:00
|
|
|
/* {{{ cdb_file_read */
|
|
|
|
#if DBA_CDB_BUILTIN
|
|
|
|
# define cdb_file_read(fildes, buf, size) php_stream_read(fildes, buf, size)
|
|
|
|
#else
|
|
|
|
# define cdb_file_read(fildes, buf, size) read(fildes, buf, size)
|
|
|
|
#endif
|
|
|
|
/* }}} */
|
1999-07-21 23:12:32 +08:00
|
|
|
|
2002-09-19 12:18:20 +08:00
|
|
|
#define CREAD(n) do { \
|
2002-11-05 01:53:04 +08:00
|
|
|
if (cdb_file_read(cdb->file, buf, n) < n) return NULL; \
|
2002-09-19 12:18:20 +08:00
|
|
|
} while (0)
|
|
|
|
|
2015-01-03 17:22:58 +08:00
|
|
|
/* {{{ cdb_file_lseek
|
2002-11-05 01:53:04 +08:00
|
|
|
php_stream_seek does not return actual position */
|
|
|
|
#if DBA_CDB_BUILTIN
|
2014-12-14 06:06:14 +08:00
|
|
|
int cdb_file_lseek(php_stream *fp, off_t offset, int whence) {
|
2002-11-05 01:53:04 +08:00
|
|
|
php_stream_seek(fp, offset, whence);
|
|
|
|
return php_stream_tell(fp);
|
|
|
|
}
|
|
|
|
#else
|
2014-12-14 06:06:14 +08:00
|
|
|
int cdb_file_lseek(int fd, off_t offset, int whence) {
|
2002-11-05 01:53:04 +08:00
|
|
|
return lseek(fd, offset, whence);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* }}} */
|
|
|
|
|
2002-09-19 12:18:20 +08:00
|
|
|
#define CSEEK(n) do { \
|
|
|
|
if (n >= cdb->eod) return NULL; \
|
2014-12-14 06:06:14 +08:00
|
|
|
if (cdb_file_lseek(cdb->file, (off_t)n, SEEK_SET) != (off_t) n) return NULL; \
|
2002-09-19 12:18:20 +08:00
|
|
|
} while (0)
|
|
|
|
|
1999-07-21 23:12:32 +08:00
|
|
|
|
|
|
|
DBA_FIRSTKEY_FUNC(cdb)
|
|
|
|
{
|
|
|
|
CDB_INFO;
|
2002-09-19 12:18:20 +08:00
|
|
|
uint32 klen, dlen;
|
1999-07-21 23:12:32 +08:00
|
|
|
char buf[8];
|
|
|
|
char *key;
|
|
|
|
|
2002-11-05 01:53:04 +08:00
|
|
|
#if DBA_CDB_BUILTIN
|
|
|
|
if (cdb->make)
|
|
|
|
return NULL; /* database was opened writeonly */
|
|
|
|
#endif
|
|
|
|
|
1999-07-21 23:12:32 +08:00
|
|
|
cdb->eod = -1;
|
|
|
|
CSEEK(0);
|
|
|
|
CREAD(4);
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2002-09-19 12:18:20 +08:00
|
|
|
/* Total length of file in bytes */
|
|
|
|
uint32_unpack(buf, &cdb->eod);
|
2015-01-03 17:22:58 +08:00
|
|
|
|
1999-07-21 23:12:32 +08:00
|
|
|
CSEEK(2048);
|
|
|
|
CREAD(8);
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2002-09-19 12:18:20 +08:00
|
|
|
/* The first four bytes contain the length of the key */
|
|
|
|
uint32_unpack(buf, &klen);
|
|
|
|
uint32_unpack(buf + 4, &dlen);
|
1999-07-21 23:12:32 +08:00
|
|
|
|
2003-04-25 04:54:43 +08:00
|
|
|
key = safe_emalloc(klen, 1, 1);
|
2002-11-05 01:53:04 +08:00
|
|
|
if (cdb_file_read(cdb->file, key, klen) < klen) {
|
1999-07-21 23:12:32 +08:00
|
|
|
efree(key);
|
|
|
|
key = NULL;
|
2002-09-19 12:18:20 +08:00
|
|
|
} else {
|
|
|
|
key[klen] = '\0';
|
|
|
|
if (newlen) *newlen = klen;
|
|
|
|
}
|
|
|
|
|
1999-07-21 23:12:32 +08:00
|
|
|
/* header + klenlen + dlenlen + klen + dlen */
|
2002-09-19 12:18:20 +08:00
|
|
|
cdb->pos = 2048 + 4 + 4 + klen + dlen;
|
2015-01-03 17:22:58 +08:00
|
|
|
|
1999-07-21 23:12:32 +08:00
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBA_NEXTKEY_FUNC(cdb)
|
|
|
|
{
|
|
|
|
CDB_INFO;
|
2002-09-19 12:18:20 +08:00
|
|
|
uint32 klen, dlen;
|
1999-07-21 23:12:32 +08:00
|
|
|
char buf[8];
|
2002-09-19 12:18:20 +08:00
|
|
|
char *key;
|
1999-07-21 23:12:32 +08:00
|
|
|
|
2002-11-05 01:53:04 +08:00
|
|
|
#if DBA_CDB_BUILTIN
|
|
|
|
if (cdb->make)
|
|
|
|
return NULL; /* database was opened writeonly */
|
|
|
|
#endif
|
|
|
|
|
1999-07-21 23:12:32 +08:00
|
|
|
CSEEK(cdb->pos);
|
|
|
|
CREAD(8);
|
2002-09-19 12:18:20 +08:00
|
|
|
uint32_unpack(buf, &klen);
|
|
|
|
uint32_unpack(buf + 4, &dlen);
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2003-04-25 04:54:43 +08:00
|
|
|
key = safe_emalloc(klen, 1, 1);
|
2002-11-05 01:53:04 +08:00
|
|
|
if (cdb_file_read(cdb->file, key, klen) < klen) {
|
2002-09-19 12:18:20 +08:00
|
|
|
efree(key);
|
|
|
|
key = NULL;
|
|
|
|
} else {
|
|
|
|
key[klen] = '\0';
|
|
|
|
if (newlen) *newlen = klen;
|
1999-07-21 23:12:32 +08:00
|
|
|
}
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2002-09-19 12:18:20 +08:00
|
|
|
cdb->pos += 8 + klen + dlen;
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2002-09-19 12:18:20 +08:00
|
|
|
return key;
|
1999-07-21 23:12:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DBA_OPTIMIZE_FUNC(cdb)
|
|
|
|
{
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBA_SYNC_FUNC(cdb)
|
|
|
|
{
|
|
|
|
/* this is read-only */
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2002-12-30 21:07:30 +08:00
|
|
|
DBA_INFO_FUNC(cdb)
|
|
|
|
{
|
|
|
|
#if DBA_CDB_BUILTIN
|
|
|
|
if (!strcmp(hnd->name, "cdb")) {
|
|
|
|
return estrdup(cdb_version());
|
|
|
|
} else {
|
|
|
|
return estrdup(cdb_make_version());
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
return estrdup("External");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1999-07-21 23:12:32 +08:00
|
|
|
#endif
|
2001-06-05 21:12:10 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
2001-09-09 21:29:31 +08:00
|
|
|
* vim600: sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: sw=4 ts=4
|
2001-06-05 21:12:10 +08:00
|
|
|
*/
|