dba_db1.c 3.6 KB

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