dba_gdbm.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. | Author: Sascha Schumann <sascha@schumann.cx> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include "php.h"
  22. #if DBA_GDBM
  23. #include "php_gdbm.h"
  24. #ifdef GDBM_INCLUDE_FILE
  25. #include GDBM_INCLUDE_FILE
  26. #endif
  27. #define GDBM_DATA dba_gdbm_data *dba = info->dbf
  28. #define GDBM_GKEY datum gkey; gkey.dptr = (char *) key; gkey.dsize = keylen
  29. typedef struct {
  30. GDBM_FILE dbf;
  31. datum nextkey;
  32. } dba_gdbm_data;
  33. DBA_OPEN_FUNC(gdbm)
  34. {
  35. GDBM_FILE dbf;
  36. int gmode = 0;
  37. int filemode = 0644;
  38. gmode = info->mode == DBA_READER ? GDBM_READER :
  39. info->mode == DBA_WRITER ? GDBM_WRITER :
  40. info->mode == DBA_CREAT ? GDBM_WRCREAT :
  41. info->mode == DBA_TRUNC ? GDBM_NEWDB : -1;
  42. if(gmode == -1)
  43. return FAILURE; /* not possible */
  44. if(info->argc > 0) {
  45. filemode = zval_get_long(&info->argv[0]);
  46. }
  47. dbf = gdbm_open(info->path, 0, gmode, filemode, NULL);
  48. if(dbf) {
  49. info->dbf = pemalloc(sizeof(dba_gdbm_data), info->flags&DBA_PERSISTENT);
  50. memset(info->dbf, 0, sizeof(dba_gdbm_data));
  51. ((dba_gdbm_data *) info->dbf)->dbf = dbf;
  52. return SUCCESS;
  53. }
  54. *error = (char *)gdbm_strerror(gdbm_errno);
  55. return FAILURE;
  56. }
  57. DBA_CLOSE_FUNC(gdbm)
  58. {
  59. GDBM_DATA;
  60. if(dba->nextkey.dptr) free(dba->nextkey.dptr);
  61. gdbm_close(dba->dbf);
  62. pefree(dba, info->flags&DBA_PERSISTENT);
  63. }
  64. DBA_FETCH_FUNC(gdbm)
  65. {
  66. GDBM_DATA;
  67. datum gval;
  68. char *new = NULL;
  69. GDBM_GKEY;
  70. gval = gdbm_fetch(dba->dbf, gkey);
  71. if(gval.dptr) {
  72. if(newlen) *newlen = gval.dsize;
  73. new = estrndup(gval.dptr, gval.dsize);
  74. free(gval.dptr);
  75. }
  76. return new;
  77. }
  78. DBA_UPDATE_FUNC(gdbm)
  79. {
  80. datum gval;
  81. GDBM_DATA;
  82. GDBM_GKEY;
  83. gval.dptr = (char *) val;
  84. gval.dsize = vallen;
  85. switch (gdbm_store(dba->dbf, gkey, gval, mode == 1 ? GDBM_INSERT : GDBM_REPLACE)) {
  86. case 0:
  87. return SUCCESS;
  88. case 1:
  89. return FAILURE;
  90. case -1:
  91. php_error_docref2(NULL, key, val, E_WARNING, "%s", gdbm_strerror(gdbm_errno));
  92. return FAILURE;
  93. default:
  94. php_error_docref2(NULL, key, val, E_WARNING, "Unknown return value");
  95. return FAILURE;
  96. }
  97. }
  98. DBA_EXISTS_FUNC(gdbm)
  99. {
  100. GDBM_DATA;
  101. GDBM_GKEY;
  102. return gdbm_exists(dba->dbf, gkey) ? SUCCESS : FAILURE;
  103. }
  104. DBA_DELETE_FUNC(gdbm)
  105. {
  106. GDBM_DATA;
  107. GDBM_GKEY;
  108. return gdbm_delete(dba->dbf, gkey) == -1 ? FAILURE : SUCCESS;
  109. }
  110. DBA_FIRSTKEY_FUNC(gdbm)
  111. {
  112. GDBM_DATA;
  113. datum gkey;
  114. char *key = NULL;
  115. if(dba->nextkey.dptr) {
  116. free(dba->nextkey.dptr);
  117. }
  118. gkey = gdbm_firstkey(dba->dbf);
  119. if(gkey.dptr) {
  120. key = estrndup(gkey.dptr, gkey.dsize);
  121. if(newlen) *newlen = gkey.dsize;
  122. dba->nextkey = gkey;
  123. } else {
  124. dba->nextkey.dptr = NULL;
  125. }
  126. return key;
  127. }
  128. DBA_NEXTKEY_FUNC(gdbm)
  129. {
  130. GDBM_DATA;
  131. char *nkey = NULL;
  132. datum gkey;
  133. if(!dba->nextkey.dptr) return NULL;
  134. gkey = gdbm_nextkey(dba->dbf, dba->nextkey);
  135. free(dba->nextkey.dptr);
  136. if(gkey.dptr) {
  137. nkey = estrndup(gkey.dptr, gkey.dsize);
  138. if(newlen) *newlen = gkey.dsize;
  139. dba->nextkey = gkey;
  140. } else {
  141. dba->nextkey.dptr = NULL;
  142. }
  143. return nkey;
  144. }
  145. DBA_OPTIMIZE_FUNC(gdbm)
  146. {
  147. GDBM_DATA;
  148. gdbm_reorganize(dba->dbf);
  149. return SUCCESS;
  150. }
  151. DBA_SYNC_FUNC(gdbm)
  152. {
  153. GDBM_DATA;
  154. gdbm_sync(dba->dbf);
  155. return SUCCESS;
  156. }
  157. DBA_INFO_FUNC(gdbm)
  158. {
  159. return estrdup(gdbm_version);
  160. }
  161. #endif
  162. /*
  163. * Local variables:
  164. * tab-width: 4
  165. * c-basic-offset: 4
  166. * End:
  167. * vim600: sw=4 ts=4 fdm=marker
  168. * vim<600: sw=4 ts=4
  169. */