You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
4.2 KiB

  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP version 4.0 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2001 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.02 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available at through the world-wide-web at |
  10. | http://www.php.net/license/2_02.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. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #include "php.h"
  20. #if DBA_DB3
  21. #include "php_db3.h"
  22. #include <sys/stat.h>
  23. #include <string.h>
  24. #ifdef DB3_INCLUDE_FILE
  25. #include DB3_INCLUDE_FILE
  26. #else
  27. #include <db.h>
  28. #endif
  29. #define DB3_DATA dba_db3_data *dba = info->dbf
  30. #define DB3_GKEY \
  31. DBT gkey; \
  32. memset(&gkey, 0, sizeof(gkey)); \
  33. gkey.data = (char *) key; gkey.size = keylen
  34. typedef struct {
  35. DB *dbp;
  36. DBC *cursor;
  37. } dba_db3_data;
  38. DBA_OPEN_FUNC(db3)
  39. {
  40. DB *dbp = NULL;
  41. DBTYPE type;
  42. int gmode = 0;
  43. int filemode = 0644;
  44. struct stat check_stat;
  45. type = info->mode == DBA_READER ? DB_UNKNOWN :
  46. info->mode == DBA_TRUNC ? DB_BTREE :
  47. VCWD_STAT(info->path, &check_stat) ? DB_BTREE : DB_UNKNOWN;
  48. gmode = info->mode == DBA_READER ? DB_RDONLY :
  49. info->mode == DBA_CREAT ? DB_CREATE :
  50. info->mode == DBA_WRITER ? 0 :
  51. info->mode == DBA_TRUNC ? DB_CREATE | DB_TRUNCATE : -1;
  52. if (gmode == -1)
  53. return FAILURE;
  54. if (info->argc > 0) {
  55. convert_to_long_ex(info->argv[0]);
  56. filemode = Z_LVAL_PP(info->argv[0]);
  57. }
  58. if (db_create(&dbp, NULL, 0) == 0 &&
  59. dbp->open(dbp, info->path, NULL, type, gmode, filemode) == 0) {
  60. dba_db3_data *data;
  61. data = malloc(sizeof(*data));
  62. data->dbp = dbp;
  63. data->cursor = NULL;
  64. info->dbf = data;
  65. return SUCCESS;
  66. } else if (dbp != NULL) {
  67. dbp->close(dbp, 0);
  68. }
  69. return FAILURE;
  70. }
  71. DBA_CLOSE_FUNC(db3)
  72. {
  73. DB3_DATA;
  74. if (dba->cursor) dba->cursor->c_close(dba->cursor);
  75. dba->dbp->close(dba->dbp, 0);
  76. free(dba);
  77. }
  78. DBA_FETCH_FUNC(db3)
  79. {
  80. DBT gval;
  81. char *new = NULL;
  82. DB3_DATA;
  83. DB3_GKEY;
  84. memset(&gval, 0, sizeof(gval));
  85. if (!dba->dbp->get(dba->dbp, NULL, &gkey, &gval, 0)) {
  86. if (newlen) *newlen = gval.size;
  87. new = estrndup(gval.data, gval.size);
  88. }
  89. return new;
  90. }
  91. DBA_UPDATE_FUNC(db3)
  92. {
  93. DBT gval;
  94. DB3_DATA;
  95. DB3_GKEY;
  96. memset(&gval, 0, sizeof(gval));
  97. gval.data = (char *) val;
  98. gval.size = vallen;
  99. if (!dba->dbp->put(dba->dbp, NULL, &gkey, &gval,
  100. mode == 1 ? DB_NOOVERWRITE : 0)) {
  101. return SUCCESS;
  102. }
  103. return FAILURE;
  104. }
  105. DBA_EXISTS_FUNC(db3)
  106. {
  107. DBT gval;
  108. DB3_DATA;
  109. DB3_GKEY;
  110. memset(&gval, 0, sizeof(gval));
  111. if (!dba->dbp->get(dba->dbp, NULL, &gkey, &gval, 0)) {
  112. return SUCCESS;
  113. }
  114. return FAILURE;
  115. }
  116. DBA_DELETE_FUNC(db3)
  117. {
  118. DB3_DATA;
  119. DB3_GKEY;
  120. return dba->dbp->del(dba->dbp, NULL, &gkey, 0) ? FAILURE : SUCCESS;
  121. }
  122. DBA_FIRSTKEY_FUNC(db3)
  123. {
  124. DB3_DATA;
  125. if (dba->cursor) {
  126. dba->cursor->c_close(dba->cursor);
  127. }
  128. dba->cursor = NULL;
  129. if (dba->dbp->cursor(dba->dbp, NULL, &dba->cursor, 0) != 0) {
  130. return NULL;
  131. }
  132. /* we should introduce something like PARAM_PASSTHRU... */
  133. return dba_nextkey_db3(info, newlen);
  134. }
  135. DBA_NEXTKEY_FUNC(db3)
  136. {
  137. DB3_DATA;
  138. DBT gkey, gval;
  139. char *nkey = NULL;
  140. memset(&gkey, 0, sizeof(gkey));
  141. memset(&gval, 0, sizeof(gval));
  142. if (dba->cursor->c_get(dba->cursor, &gkey, &gval, DB_NEXT) == 0) {
  143. if (gkey.data) {
  144. nkey = estrndup(gkey.data, gkey.size);
  145. if (newlen) *newlen = gkey.size;
  146. }
  147. }
  148. return nkey;
  149. }
  150. DBA_OPTIMIZE_FUNC(db3)
  151. {
  152. return SUCCESS;
  153. }
  154. DBA_SYNC_FUNC(db3)
  155. {
  156. DB3_DATA;
  157. return dba->dbp->sync(dba->dbp, 0) ? FAILURE : SUCCESS;
  158. }
  159. #endif
  160. /*
  161. * Local variables:
  162. * tab-width: 4
  163. * c-basic-offset: 4
  164. * End:
  165. * vim600: sw=4 ts=4 fdm=marker
  166. * vim<600: sw=4 ts=4
  167. */