dba_db4.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. | Authors: Marcus Boerger <helly@php.net> |
  14. | Sascha Schumann <sascha@schumann.cx> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include "php.h"
  21. #if DBA_DB4
  22. #include "php_db4.h"
  23. #include <sys/stat.h>
  24. #include <string.h>
  25. #ifdef DB4_INCLUDE_FILE
  26. #include DB4_INCLUDE_FILE
  27. #else
  28. #include <db.h>
  29. #endif
  30. static void php_dba_db4_errcall_fcn(
  31. #if (DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3))
  32. const DB_ENV *dbenv,
  33. #endif
  34. const char *errpfx, const char *msg)
  35. {
  36. #if (DB_VERSION_MAJOR == 5 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 8))
  37. /* Bug 51086, Berkeley DB 4.8.26 */
  38. /* This code suppresses a BDB 4.8+ error message, thus keeping PHP test compatibility */
  39. {
  40. const char *function = get_active_function_name();
  41. if (function && (!strcmp(function,"dba_popen") || !strcmp(function,"dba_open"))
  42. && (!strncmp(msg, "fop_read_meta", sizeof("fop_read_meta")-1)
  43. || !strncmp(msg, "BDB0004 fop_read_meta", sizeof("BDB0004 fop_read_meta")-1))) {
  44. return;
  45. }
  46. }
  47. #endif
  48. php_error_docref(NULL, E_NOTICE, "%s%s", errpfx?errpfx:"", msg);
  49. }
  50. #define DB4_DATA dba_db4_data *dba = info->dbf
  51. #define DB4_GKEY \
  52. DBT gkey; \
  53. memset(&gkey, 0, sizeof(gkey)); \
  54. gkey.data = (char *) key; gkey.size = keylen
  55. typedef struct {
  56. DB *dbp;
  57. DBC *cursor;
  58. } dba_db4_data;
  59. DBA_OPEN_FUNC(db4)
  60. {
  61. DB *dbp = NULL;
  62. DBTYPE type;
  63. int gmode = 0, err;
  64. int filemode = 0644;
  65. struct stat check_stat;
  66. int s = VCWD_STAT(info->path, &check_stat);
  67. #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR <= 7) /* Bug 51086 */
  68. if (!s && !check_stat.st_size) {
  69. info->mode = DBA_TRUNC; /* force truncate */
  70. }
  71. type = info->mode == DBA_READER ? DB_UNKNOWN :
  72. info->mode == DBA_TRUNC ? DB_BTREE :
  73. s ? DB_BTREE : DB_UNKNOWN;
  74. gmode = info->mode == DBA_READER ? DB_RDONLY :
  75. (info->mode == DBA_CREAT && s) ? DB_CREATE :
  76. (info->mode == DBA_CREAT && !s) ? 0 :
  77. info->mode == DBA_WRITER ? 0 :
  78. info->mode == DBA_TRUNC ? DB_CREATE | DB_TRUNCATE : -1;
  79. #else
  80. if (!s && !check_stat.st_size) {
  81. info->mode = DBA_CREAT; /* force creation */
  82. }
  83. type = info->mode == DBA_READER ? DB_UNKNOWN :
  84. (info->mode == DBA_TRUNC || info->mode == DBA_CREAT) ? DB_BTREE :
  85. s ? DB_BTREE : DB_UNKNOWN;
  86. gmode = info->mode == DBA_READER ? DB_RDONLY :
  87. info->mode == DBA_CREAT ? DB_CREATE :
  88. info->mode == DBA_WRITER ? 0 :
  89. info->mode == DBA_TRUNC ? DB_CREATE | DB_TRUNCATE : -1;
  90. #endif
  91. if (gmode == -1) {
  92. return FAILURE; /* not possible */
  93. }
  94. if (info->flags & DBA_PERSISTENT) {
  95. gmode |= DB_THREAD;
  96. }
  97. if (info->argc > 0) {
  98. filemode = zval_get_long(&info->argv[0]);
  99. }
  100. if ((err=db_create(&dbp, NULL, 0)) == 0) {
  101. dbp->set_errcall(dbp, php_dba_db4_errcall_fcn);
  102. if (
  103. #if (DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1))
  104. (err=dbp->open(dbp, 0, info->path, NULL, type, gmode, filemode)) == 0) {
  105. #else
  106. (err=dbp->open(dbp, info->path, NULL, type, gmode, filemode)) == 0) {
  107. #endif
  108. dba_db4_data *data;
  109. data = pemalloc(sizeof(*data), info->flags&DBA_PERSISTENT);
  110. data->dbp = dbp;
  111. data->cursor = NULL;
  112. info->dbf = data;
  113. return SUCCESS;
  114. } else {
  115. dbp->close(dbp, 0);
  116. *error = db_strerror(err);
  117. }
  118. } else {
  119. *error = db_strerror(err);
  120. }
  121. return FAILURE;
  122. }
  123. DBA_CLOSE_FUNC(db4)
  124. {
  125. DB4_DATA;
  126. if (dba->cursor) dba->cursor->c_close(dba->cursor);
  127. dba->dbp->close(dba->dbp, 0);
  128. pefree(dba, info->flags&DBA_PERSISTENT);
  129. }
  130. DBA_FETCH_FUNC(db4)
  131. {
  132. DBT gval;
  133. char *new = NULL;
  134. DB4_DATA;
  135. DB4_GKEY;
  136. memset(&gval, 0, sizeof(gval));
  137. if (info->flags & DBA_PERSISTENT) {
  138. gval.flags |= DB_DBT_MALLOC;
  139. }
  140. if (!dba->dbp->get(dba->dbp, NULL, &gkey, &gval, 0)) {
  141. if (newlen) *newlen = gval.size;
  142. new = estrndup(gval.data, gval.size);
  143. if (info->flags & DBA_PERSISTENT) {
  144. free(gval.data);
  145. }
  146. }
  147. return new;
  148. }
  149. DBA_UPDATE_FUNC(db4)
  150. {
  151. DBT gval;
  152. DB4_DATA;
  153. DB4_GKEY;
  154. memset(&gval, 0, sizeof(gval));
  155. gval.data = (char *) val;
  156. gval.size = vallen;
  157. if (!dba->dbp->put(dba->dbp, NULL, &gkey, &gval,
  158. mode == 1 ? DB_NOOVERWRITE : 0)) {
  159. return SUCCESS;
  160. }
  161. return FAILURE;
  162. }
  163. DBA_EXISTS_FUNC(db4)
  164. {
  165. DBT gval;
  166. DB4_DATA;
  167. DB4_GKEY;
  168. memset(&gval, 0, sizeof(gval));
  169. if (info->flags & DBA_PERSISTENT) {
  170. gval.flags |= DB_DBT_MALLOC;
  171. }
  172. if (!dba->dbp->get(dba->dbp, NULL, &gkey, &gval, 0)) {
  173. if (info->flags & DBA_PERSISTENT) {
  174. free(gval.data);
  175. }
  176. return SUCCESS;
  177. }
  178. return FAILURE;
  179. }
  180. DBA_DELETE_FUNC(db4)
  181. {
  182. DB4_DATA;
  183. DB4_GKEY;
  184. return dba->dbp->del(dba->dbp, NULL, &gkey, 0) ? FAILURE : SUCCESS;
  185. }
  186. DBA_FIRSTKEY_FUNC(db4)
  187. {
  188. DB4_DATA;
  189. if (dba->cursor) {
  190. dba->cursor->c_close(dba->cursor);
  191. }
  192. dba->cursor = NULL;
  193. if (dba->dbp->cursor(dba->dbp, NULL, &dba->cursor, 0) != 0) {
  194. return NULL;
  195. }
  196. /* we should introduce something like PARAM_PASSTHRU... */
  197. return dba_nextkey_db4(info, newlen);
  198. }
  199. DBA_NEXTKEY_FUNC(db4)
  200. {
  201. DB4_DATA;
  202. DBT gkey, gval;
  203. char *nkey = NULL;
  204. memset(&gkey, 0, sizeof(gkey));
  205. memset(&gval, 0, sizeof(gval));
  206. if (info->flags & DBA_PERSISTENT) {
  207. gkey.flags |= DB_DBT_MALLOC;
  208. gval.flags |= DB_DBT_MALLOC;
  209. }
  210. if (dba->cursor && dba->cursor->c_get(dba->cursor, &gkey, &gval, DB_NEXT) == 0) {
  211. if (gkey.data) {
  212. nkey = estrndup(gkey.data, gkey.size);
  213. if (newlen) *newlen = gkey.size;
  214. }
  215. if (info->flags & DBA_PERSISTENT) {
  216. if (gkey.data) {
  217. free(gkey.data);
  218. }
  219. if (gval.data) {
  220. free(gval.data);
  221. }
  222. }
  223. }
  224. return nkey;
  225. }
  226. DBA_OPTIMIZE_FUNC(db4)
  227. {
  228. return SUCCESS;
  229. }
  230. DBA_SYNC_FUNC(db4)
  231. {
  232. DB4_DATA;
  233. return dba->dbp->sync(dba->dbp, 0) ? FAILURE : SUCCESS;
  234. }
  235. DBA_INFO_FUNC(db4)
  236. {
  237. return estrdup(DB_VERSION_STRING);
  238. }
  239. #endif