dba_db1.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. | Author: Shen Cheng-Da <cdsheen@gmail.com> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "php.h"
  23. #if DBA_DB1
  24. #include "php_db1.h"
  25. #ifdef DB1_INCLUDE_FILE
  26. #include DB1_INCLUDE_FILE
  27. #endif
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <fcntl.h>
  31. #define DB1_DATA dba_db1_data *dba = info->dbf
  32. #define DB1_GKEY DBT gkey; gkey.data = (char *) key; gkey.size = keylen
  33. typedef struct {
  34. DB *dbp;
  35. } dba_db1_data;
  36. DBA_OPEN_FUNC(db1)
  37. {
  38. dba_db1_data *dba;
  39. DB *db;
  40. int gmode;
  41. int filemode = 0644;
  42. if (info->argc > 0) {
  43. convert_to_long_ex(info->argv[0]);
  44. filemode = Z_LVAL_PP(info->argv[0]);
  45. }
  46. gmode = 0;
  47. switch (info->mode) {
  48. case DBA_READER:
  49. gmode = O_RDONLY;
  50. break;
  51. case DBA_WRITER:
  52. gmode = O_RDWR;
  53. break;
  54. case DBA_CREAT:
  55. gmode = O_RDWR | O_CREAT;
  56. break;
  57. case DBA_TRUNC:
  58. gmode = O_RDWR | O_CREAT | O_TRUNC;
  59. break;
  60. default:
  61. return FAILURE; /* not possible */
  62. }
  63. db = dbopen((char *)info->path, gmode, filemode, DB_HASH, NULL);
  64. if (db == NULL) {
  65. return FAILURE;
  66. }
  67. dba = pemalloc(sizeof(*dba), info->flags&DBA_PERSISTENT);
  68. dba->dbp = db;
  69. info->dbf = dba;
  70. return SUCCESS;
  71. }
  72. DBA_CLOSE_FUNC(db1)
  73. {
  74. DB1_DATA;
  75. dba->dbp->close(dba->dbp);
  76. pefree(info->dbf, info->flags&DBA_PERSISTENT);
  77. }
  78. DBA_FETCH_FUNC(db1)
  79. {
  80. DBT gval;
  81. DB1_DATA;
  82. DB1_GKEY;
  83. memset(&gval, 0, sizeof(gval));
  84. if (dba->dbp->get(dba->dbp, &gkey, &gval, 0) == RET_SUCCESS) {
  85. if (newlen) *newlen = gval.size;
  86. return estrndup(gval.data, gval.size);
  87. }
  88. return NULL;
  89. }
  90. DBA_UPDATE_FUNC(db1)
  91. {
  92. DBT gval;
  93. DB1_DATA;
  94. DB1_GKEY;
  95. gval.data = (char *) val;
  96. gval.size = vallen;
  97. return dba->dbp->put(dba->dbp, &gkey, &gval, mode == 1 ? R_NOOVERWRITE : 0) != RET_SUCCESS ? FAILURE : SUCCESS;
  98. }
  99. DBA_EXISTS_FUNC(db1)
  100. {
  101. DBT gval;
  102. DB1_DATA;
  103. DB1_GKEY;
  104. return dba->dbp->get(dba->dbp, &gkey, &gval, 0) != RET_SUCCESS ? FAILURE : SUCCESS;
  105. }
  106. DBA_DELETE_FUNC(db1)
  107. {
  108. DB1_DATA;
  109. DB1_GKEY;
  110. return dba->dbp->del(dba->dbp, &gkey, 0) != RET_SUCCESS ? FAILURE : SUCCESS;
  111. }
  112. DBA_FIRSTKEY_FUNC(db1)
  113. {
  114. DBT gkey;
  115. DBT gval;
  116. DB1_DATA;
  117. memset(&gkey, 0, sizeof(gkey));
  118. memset(&gval, 0, sizeof(gval));
  119. if (dba->dbp->seq(dba->dbp, &gkey, &gval, R_FIRST) == RET_SUCCESS) {
  120. if (newlen) *newlen = gkey.size;
  121. return estrndup(gkey.data, gkey.size);
  122. }
  123. return NULL;
  124. }
  125. DBA_NEXTKEY_FUNC(db1)
  126. {
  127. DBT gkey;
  128. DBT gval;
  129. DB1_DATA;
  130. memset(&gkey, 0, sizeof(gkey));
  131. memset(&gval, 0, sizeof(gval));
  132. if (dba->dbp->seq(dba->dbp, &gkey, &gval, R_NEXT) == RET_SUCCESS) {
  133. if (newlen) *newlen = gkey.size;
  134. return estrndup(gkey.data, gkey.size);
  135. }
  136. return NULL;
  137. }
  138. DBA_OPTIMIZE_FUNC(db1)
  139. {
  140. /* dummy */
  141. return SUCCESS;
  142. }
  143. DBA_SYNC_FUNC(db1)
  144. {
  145. return SUCCESS;
  146. }
  147. DBA_INFO_FUNC(db1)
  148. {
  149. return estrdup(DB1_VERSION);
  150. }
  151. #endif
  152. /*
  153. * Local variables:
  154. * tab-width: 4
  155. * c-basic-offset: 4
  156. * End:
  157. * vim600: sw=4 ts=4 fdm=marker
  158. * vim<600: sw=4 ts=4
  159. */