dba_dbm.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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_DBM
  23. #include "php_dbm.h"
  24. #ifdef DBM_INCLUDE_FILE
  25. #include DBM_INCLUDE_FILE
  26. #endif
  27. #if DBA_GDBM
  28. #include "php_gdbm.h"
  29. #endif
  30. #include <unistd.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <fcntl.h>
  34. #define DBM_DATA dba_dbm_data *dba = info->dbf
  35. #define DBM_GKEY datum gkey; gkey.dptr = (char *) key; gkey.dsize = keylen
  36. #define TRUNC_IT(extension, mode) \
  37. snprintf(buf, MAXPATHLEN, "%s" extension, info->path); \
  38. buf[MAXPATHLEN-1] = '\0'; \
  39. if((fd = VCWD_OPEN_MODE(buf, O_CREAT | mode | O_WRONLY, filemode)) == -1) \
  40. return FAILURE; \
  41. close(fd);
  42. typedef struct {
  43. datum nextkey;
  44. } dba_dbm_data;
  45. DBA_OPEN_FUNC(dbm)
  46. {
  47. int fd;
  48. int filemode = 0644;
  49. if(info->argc > 0) {
  50. filemode = zval_get_long(&info->argv[0]);
  51. }
  52. if(info->mode == DBA_TRUNC) {
  53. char buf[MAXPATHLEN];
  54. /* dbm/ndbm original */
  55. TRUNC_IT(".pag", O_TRUNC);
  56. TRUNC_IT(".dir", O_TRUNC);
  57. }
  58. if(info->mode == DBA_CREAT) {
  59. char buf[MAXPATHLEN];
  60. TRUNC_IT(".pag", 0);
  61. TRUNC_IT(".dir", 0);
  62. }
  63. if(dbminit((char *) info->path) == -1) {
  64. return FAILURE;
  65. }
  66. info->dbf = pemalloc(sizeof(dba_dbm_data), info->flags&DBA_PERSISTENT);
  67. memset(info->dbf, 0, sizeof(dba_dbm_data));
  68. return SUCCESS;
  69. }
  70. DBA_CLOSE_FUNC(dbm)
  71. {
  72. pefree(info->dbf, info->flags&DBA_PERSISTENT);
  73. dbmclose();
  74. }
  75. DBA_FETCH_FUNC(dbm)
  76. {
  77. datum gval;
  78. char *new = NULL;
  79. DBM_GKEY;
  80. gval = fetch(gkey);
  81. if(gval.dptr) {
  82. if(newlen) *newlen = gval.dsize;
  83. new = estrndup(gval.dptr, gval.dsize);
  84. }
  85. return new;
  86. }
  87. DBA_UPDATE_FUNC(dbm)
  88. {
  89. datum gval;
  90. DBM_GKEY;
  91. if (mode == 1) { /* insert */
  92. gval = fetch(gkey);
  93. if(gval.dptr) {
  94. return FAILURE;
  95. }
  96. }
  97. gval.dptr = (char *) val;
  98. gval.dsize = vallen;
  99. return (store(gkey, gval) == -1 ? FAILURE : SUCCESS);
  100. }
  101. DBA_EXISTS_FUNC(dbm)
  102. {
  103. datum gval;
  104. DBM_GKEY;
  105. gval = fetch(gkey);
  106. if(gval.dptr) {
  107. return SUCCESS;
  108. }
  109. return FAILURE;
  110. }
  111. DBA_DELETE_FUNC(dbm)
  112. {
  113. DBM_GKEY;
  114. return(delete(gkey) == -1 ? FAILURE : SUCCESS);
  115. }
  116. DBA_FIRSTKEY_FUNC(dbm)
  117. {
  118. DBM_DATA;
  119. datum gkey;
  120. char *key = NULL;
  121. gkey = firstkey();
  122. if(gkey.dptr) {
  123. if(newlen) *newlen = gkey.dsize;
  124. key = estrndup(gkey.dptr, gkey.dsize);
  125. dba->nextkey = gkey;
  126. } else
  127. dba->nextkey.dptr = NULL;
  128. return key;
  129. }
  130. DBA_NEXTKEY_FUNC(dbm)
  131. {
  132. DBM_DATA;
  133. datum gkey;
  134. char *nkey = NULL;
  135. if(!dba->nextkey.dptr) return NULL;
  136. gkey = nextkey(dba->nextkey);
  137. if(gkey.dptr) {
  138. if(newlen) *newlen = gkey.dsize;
  139. nkey = estrndup(gkey.dptr, gkey.dsize);
  140. dba->nextkey = gkey;
  141. } else
  142. dba->nextkey.dptr = NULL;
  143. return nkey;
  144. }
  145. DBA_OPTIMIZE_FUNC(dbm)
  146. {
  147. /* dummy */
  148. return SUCCESS;
  149. }
  150. DBA_SYNC_FUNC(dbm)
  151. {
  152. return SUCCESS;
  153. }
  154. DBA_INFO_FUNC(dbm)
  155. {
  156. #if DBA_GDBM
  157. if (!strcmp(DBM_VERSION, "GDBM"))
  158. {
  159. return dba_info_gdbm(hnd, info);
  160. }
  161. #endif
  162. return estrdup(DBM_VERSION);
  163. }
  164. #endif
  165. /*
  166. * Local variables:
  167. * tab-width: 4
  168. * c-basic-offset: 4
  169. * End:
  170. * vim600: sw=4 ts=4 fdm=marker
  171. * vim<600: sw=4 ts=4
  172. */