dba_db4.c 6.7 KB

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