dba_cdb.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 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. | Authors: Sascha Schumann <sascha@schumann.cx> |
  16. | Marcus Boerger <helly@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "php.h"
  24. #if DBA_CDB
  25. #include "php_cdb.h"
  26. #include <sys/types.h>
  27. #ifdef HAVE_UNISTD_H
  28. #include <unistd.h>
  29. #endif
  30. #include <fcntl.h>
  31. #if DBA_CDB_BUILTIN
  32. # include "libcdb/cdb.h"
  33. # include "libcdb/cdb_make.h"
  34. # include "libcdb/uint32.h"
  35. #else
  36. # ifdef CDB_INCLUDE_FILE
  37. # include CDB_INCLUDE_FILE
  38. # endif
  39. #endif
  40. #define CDB_INFO \
  41. dba_cdb *cdb = (dba_cdb *) info->dbf
  42. typedef struct {
  43. struct cdb c;
  44. #if DBA_CDB_BUILTIN
  45. struct cdb_make m;
  46. php_stream *file;
  47. int make;
  48. #else
  49. int file;
  50. #endif
  51. uint32 eod; /* size of constant database */
  52. uint32 pos; /* current position for traversing */
  53. } dba_cdb;
  54. DBA_OPEN_FUNC(cdb)
  55. {
  56. #if DBA_CDB_BUILTIN
  57. php_stream* file = 0;
  58. int make;
  59. #else
  60. int file = 0;
  61. #endif
  62. dba_cdb *cdb;
  63. dba_info *pinfo = (dba_info *) info;
  64. switch (info->mode) {
  65. case DBA_READER:
  66. #if DBA_CDB_BUILTIN
  67. make = 0;
  68. file = info->fp;
  69. #else
  70. file = VCWD_OPEN(info->path, O_RDONLY);
  71. if (file < 0) {
  72. *error = "Unable to open file";
  73. return FAILURE;
  74. }
  75. #endif
  76. break;
  77. #if DBA_CDB_BUILTIN
  78. case DBA_TRUNC:
  79. make = 1;
  80. file = info->fp;
  81. break;
  82. case DBA_CREAT:
  83. case DBA_WRITER:
  84. *error = "Update operations are not supported";
  85. return FAILURE; /* not supported */
  86. #endif
  87. default:
  88. *error = "Currently not supported";
  89. return FAILURE;
  90. }
  91. cdb = pemalloc(sizeof(dba_cdb), info->flags&DBA_PERSISTENT);
  92. memset(cdb, 0, sizeof(dba_cdb));
  93. #if DBA_CDB_BUILTIN
  94. if (make) {
  95. cdb_make_start(&cdb->m, file TSRMLS_CC);
  96. } else {
  97. cdb_init(&cdb->c, file TSRMLS_CC);
  98. }
  99. cdb->make = make;
  100. #else
  101. cdb_init(&cdb->c, file);
  102. #endif
  103. cdb->file = file;
  104. pinfo->dbf = cdb;
  105. return SUCCESS;
  106. }
  107. DBA_CLOSE_FUNC(cdb)
  108. {
  109. CDB_INFO;
  110. /* cdb_free does not close associated file */
  111. #if DBA_CDB_BUILTIN
  112. if (cdb->make) {
  113. cdb_make_finish(&cdb->m TSRMLS_CC);
  114. } else {
  115. cdb_free(&cdb->c TSRMLS_CC);
  116. }
  117. #else
  118. cdb_free(&cdb->c);
  119. close(cdb->file);
  120. #endif
  121. pefree(cdb, info->flags&DBA_PERSISTENT);
  122. }
  123. #if DBA_CDB_BUILTIN
  124. # define php_cdb_read(cdb, buf, len, pos) cdb_read(cdb, buf, len, pos TSRMLS_CC)
  125. # define php_cdb_findnext(cdb, key, len) cdb_findnext(cdb, key, len TSRMLS_CC)
  126. # define php_cdb_find(cdb, key, len) cdb_find(cdb, key, len TSRMLS_CC)
  127. #else
  128. # define php_cdb_read(cdb, buf, len, pos) cdb_read(cdb, buf, len, pos)
  129. # define php_cdb_findnext(cdb, key, len) cdb_findnext(cdb, key, len)
  130. # define php_cdb_find(cdb, key, len) cdb_find(cdb, key, len)
  131. #endif
  132. DBA_FETCH_FUNC(cdb)
  133. {
  134. CDB_INFO;
  135. unsigned int len;
  136. char *new_entry = NULL;
  137. #if DBA_CDB_BUILTIN
  138. if (cdb->make)
  139. return NULL; /* database was opened writeonly */
  140. #endif
  141. if (php_cdb_find(&cdb->c, key, keylen) == 1) {
  142. while(skip--) {
  143. if (php_cdb_findnext(&cdb->c, key, keylen) != 1) {
  144. return NULL;
  145. }
  146. }
  147. len = cdb_datalen(&cdb->c);
  148. new_entry = safe_emalloc(len, 1, 1);
  149. if (php_cdb_read(&cdb->c, new_entry, len, cdb_datapos(&cdb->c)) == -1) {
  150. efree(new_entry);
  151. return NULL;
  152. }
  153. new_entry[len] = 0;
  154. if (newlen)
  155. *newlen = len;
  156. }
  157. return new_entry;
  158. }
  159. DBA_UPDATE_FUNC(cdb)
  160. {
  161. #if DBA_CDB_BUILTIN
  162. CDB_INFO;
  163. if (!cdb->make)
  164. return FAILURE; /* database was opened readonly */
  165. if (!mode)
  166. return FAILURE; /* cdb_make dosn't know replace */
  167. if (cdb_make_add(&cdb->m, key, keylen, val, vallen TSRMLS_CC) != -1)
  168. return SUCCESS;
  169. #endif
  170. return FAILURE;
  171. }
  172. DBA_EXISTS_FUNC(cdb)
  173. {
  174. CDB_INFO;
  175. #if DBA_CDB_BUILTIN
  176. if (cdb->make)
  177. return FAILURE; /* database was opened writeonly */
  178. #endif
  179. if (php_cdb_find(&cdb->c, key, keylen) == 1)
  180. return SUCCESS;
  181. return FAILURE;
  182. }
  183. DBA_DELETE_FUNC(cdb)
  184. {
  185. return FAILURE; /* cdb doesn't support delete */
  186. }
  187. /* {{{ cdb_file_read */
  188. #if DBA_CDB_BUILTIN
  189. # define cdb_file_read(fildes, buf, size) php_stream_read(fildes, buf, size)
  190. #else
  191. # define cdb_file_read(fildes, buf, size) read(fildes, buf, size)
  192. #endif
  193. /* }}} */
  194. #define CREAD(n) do { \
  195. if (cdb_file_read(cdb->file, buf, n) < n) return NULL; \
  196. } while (0)
  197. /* {{{ cdb_file_lseek
  198. php_stream_seek does not return actual position */
  199. #if DBA_CDB_BUILTIN
  200. int cdb_file_lseek(php_stream *fp, off_t offset, int whence TSRMLS_DC) {
  201. php_stream_seek(fp, offset, whence);
  202. return php_stream_tell(fp);
  203. }
  204. #else
  205. int cdb_file_lseek(int fd, off_t offset, int whence TSRMLS_DC) {
  206. return lseek(fd, offset, whence);
  207. }
  208. #endif
  209. /* }}} */
  210. #define CSEEK(n) do { \
  211. if (n >= cdb->eod) return NULL; \
  212. if (cdb_file_lseek(cdb->file, (off_t)n, SEEK_SET TSRMLS_CC) != (off_t) n) return NULL; \
  213. } while (0)
  214. DBA_FIRSTKEY_FUNC(cdb)
  215. {
  216. CDB_INFO;
  217. uint32 klen, dlen;
  218. char buf[8];
  219. char *key;
  220. #if DBA_CDB_BUILTIN
  221. if (cdb->make)
  222. return NULL; /* database was opened writeonly */
  223. #endif
  224. cdb->eod = -1;
  225. CSEEK(0);
  226. CREAD(4);
  227. /* Total length of file in bytes */
  228. uint32_unpack(buf, &cdb->eod);
  229. CSEEK(2048);
  230. CREAD(8);
  231. /* The first four bytes contain the length of the key */
  232. uint32_unpack(buf, &klen);
  233. uint32_unpack(buf + 4, &dlen);
  234. key = safe_emalloc(klen, 1, 1);
  235. if (cdb_file_read(cdb->file, key, klen) < klen) {
  236. efree(key);
  237. key = NULL;
  238. } else {
  239. key[klen] = '\0';
  240. if (newlen) *newlen = klen;
  241. }
  242. /* header + klenlen + dlenlen + klen + dlen */
  243. cdb->pos = 2048 + 4 + 4 + klen + dlen;
  244. return key;
  245. }
  246. DBA_NEXTKEY_FUNC(cdb)
  247. {
  248. CDB_INFO;
  249. uint32 klen, dlen;
  250. char buf[8];
  251. char *key;
  252. #if DBA_CDB_BUILTIN
  253. if (cdb->make)
  254. return NULL; /* database was opened writeonly */
  255. #endif
  256. CSEEK(cdb->pos);
  257. CREAD(8);
  258. uint32_unpack(buf, &klen);
  259. uint32_unpack(buf + 4, &dlen);
  260. key = safe_emalloc(klen, 1, 1);
  261. if (cdb_file_read(cdb->file, key, klen) < klen) {
  262. efree(key);
  263. key = NULL;
  264. } else {
  265. key[klen] = '\0';
  266. if (newlen) *newlen = klen;
  267. }
  268. cdb->pos += 8 + klen + dlen;
  269. return key;
  270. }
  271. DBA_OPTIMIZE_FUNC(cdb)
  272. {
  273. return SUCCESS;
  274. }
  275. DBA_SYNC_FUNC(cdb)
  276. {
  277. /* this is read-only */
  278. return SUCCESS;
  279. }
  280. DBA_INFO_FUNC(cdb)
  281. {
  282. #if DBA_CDB_BUILTIN
  283. if (!strcmp(hnd->name, "cdb")) {
  284. return estrdup(cdb_version());
  285. } else {
  286. return estrdup(cdb_make_version());
  287. }
  288. #else
  289. return estrdup("External");
  290. #endif
  291. }
  292. #endif
  293. /*
  294. * Local variables:
  295. * tab-width: 4
  296. * c-basic-offset: 4
  297. * End:
  298. * vim600: sw=4 ts=4 fdm=marker
  299. * vim<600: sw=4 ts=4
  300. */