dba_flatfile.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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: Marcus Boerger <helly@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include "php.h"
  22. #if DBA_FLATFILE
  23. #include "php_flatfile.h"
  24. #include "libflatfile/flatfile.h"
  25. #ifdef HAVE_UNISTD_H
  26. #include <unistd.h>
  27. #endif
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <fcntl.h>
  31. #define FLATFILE_DATA flatfile *dba = info->dbf
  32. #define FLATFILE_GKEY datum gkey; gkey.dptr = (char *) key; gkey.dsize = keylen
  33. DBA_OPEN_FUNC(flatfile)
  34. {
  35. info->dbf = pemalloc(sizeof(flatfile), info->flags&DBA_PERSISTENT);
  36. memset(info->dbf, 0, sizeof(flatfile));
  37. ((flatfile*)info->dbf)->fp = info->fp;
  38. return SUCCESS;
  39. }
  40. DBA_CLOSE_FUNC(flatfile)
  41. {
  42. FLATFILE_DATA;
  43. if (dba->nextkey.dptr) {
  44. efree(dba->nextkey.dptr);
  45. }
  46. pefree(dba, info->flags&DBA_PERSISTENT);
  47. }
  48. DBA_FETCH_FUNC(flatfile)
  49. {
  50. datum gval;
  51. char *new = NULL;
  52. FLATFILE_DATA;
  53. FLATFILE_GKEY;
  54. gval = flatfile_fetch(dba, gkey);
  55. if (gval.dptr) {
  56. if (newlen) {
  57. *newlen = gval.dsize;
  58. }
  59. new = estrndup(gval.dptr, gval.dsize);
  60. efree(gval.dptr);
  61. }
  62. return new;
  63. }
  64. DBA_UPDATE_FUNC(flatfile)
  65. {
  66. datum gval;
  67. FLATFILE_DATA;
  68. FLATFILE_GKEY;
  69. gval.dptr = (char *) val;
  70. gval.dsize = vallen;
  71. switch(flatfile_store(dba, gkey, gval, mode==1 ? FLATFILE_INSERT : FLATFILE_REPLACE)) {
  72. case 0:
  73. return SUCCESS;
  74. case 1:
  75. return FAILURE;
  76. case -1:
  77. php_error_docref1(NULL, key, E_WARNING, "Operation not possible");
  78. return FAILURE;
  79. default:
  80. php_error_docref2(NULL, key, val, E_WARNING, "Unknown return value");
  81. return FAILURE;
  82. }
  83. }
  84. DBA_EXISTS_FUNC(flatfile)
  85. {
  86. datum gval;
  87. FLATFILE_DATA;
  88. FLATFILE_GKEY;
  89. gval = flatfile_fetch(dba, gkey);
  90. if (gval.dptr) {
  91. efree(gval.dptr);
  92. return SUCCESS;
  93. }
  94. return FAILURE;
  95. }
  96. DBA_DELETE_FUNC(flatfile)
  97. {
  98. FLATFILE_DATA;
  99. FLATFILE_GKEY;
  100. return(flatfile_delete(dba, gkey) == -1 ? FAILURE : SUCCESS);
  101. }
  102. DBA_FIRSTKEY_FUNC(flatfile)
  103. {
  104. FLATFILE_DATA;
  105. if (dba->nextkey.dptr) {
  106. efree(dba->nextkey.dptr);
  107. }
  108. dba->nextkey = flatfile_firstkey(dba);
  109. if (dba->nextkey.dptr) {
  110. if (newlen) {
  111. *newlen = dba->nextkey.dsize;
  112. }
  113. return estrndup(dba->nextkey.dptr, dba->nextkey.dsize);
  114. }
  115. return NULL;
  116. }
  117. DBA_NEXTKEY_FUNC(flatfile)
  118. {
  119. FLATFILE_DATA;
  120. if (!dba->nextkey.dptr) {
  121. return NULL;
  122. }
  123. if (dba->nextkey.dptr) {
  124. efree(dba->nextkey.dptr);
  125. }
  126. dba->nextkey = flatfile_nextkey(dba);
  127. if (dba->nextkey.dptr) {
  128. if (newlen) {
  129. *newlen = dba->nextkey.dsize;
  130. }
  131. return estrndup(dba->nextkey.dptr, dba->nextkey.dsize);
  132. }
  133. return NULL;
  134. }
  135. DBA_OPTIMIZE_FUNC(flatfile)
  136. {
  137. /* dummy */
  138. return SUCCESS;
  139. }
  140. DBA_SYNC_FUNC(flatfile)
  141. {
  142. /* dummy */
  143. return SUCCESS;
  144. }
  145. DBA_INFO_FUNC(flatfile)
  146. {
  147. return estrdup(flatfile_version());
  148. }
  149. #endif
  150. /*
  151. * Local variables:
  152. * tab-width: 4
  153. * c-basic-offset: 4
  154. * End:
  155. * vim600: sw=4 ts=4 fdm=marker
  156. * vim<600: sw=4 ts=4
  157. */