2
0

apr_dbm_gdbm.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
  2. * applicable.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "apr_strings.h"
  17. #if APR_HAVE_STDLIB_H
  18. #include <stdlib.h> /* for free() */
  19. #endif
  20. #include "apu.h"
  21. #if APU_HAVE_GDBM
  22. #include "apr_dbm_private.h"
  23. #include <gdbm.h>
  24. /* this is used in a few places to define a noop "function". it is needed
  25. to stop "no effect" warnings from GCC. */
  26. #define NOOP_FUNCTION if (0) ; else
  27. /* ### define defaults for now; these will go away in a while */
  28. #define REGISTER_CLEANUP(dbm, pdatum) NOOP_FUNCTION
  29. #define SET_FILE(pdb, f) ((pdb)->file = (f))
  30. typedef GDBM_FILE real_file_t;
  31. typedef datum *cvt_datum_t;
  32. #define CONVERT_DATUM(cvt, pinput) ((cvt) = (datum *)(pinput))
  33. typedef datum result_datum_t;
  34. #define RETURN_DATUM(poutput, rd) (*(poutput) = *(apr_datum_t *)&(rd))
  35. #define APR_DBM_CLOSE(f) gdbm_close(f)
  36. #define APR_DBM_FETCH(f, k, v) ((v) = gdbm_fetch(f, *(k)), APR_SUCCESS)
  37. #define APR_DBM_STORE(f, k, v) g2s(gdbm_store(f, *(k), *(v), GDBM_REPLACE))
  38. #define APR_DBM_DELETE(f, k) g2s(gdbm_delete(f, *(k)))
  39. #define APR_DBM_FIRSTKEY(f, k) ((k) = gdbm_firstkey(f), APR_SUCCESS)
  40. #define APR_DBM_NEXTKEY(f, k, nk) ((nk) = gdbm_nextkey(f, *(k)), APR_SUCCESS)
  41. #define APR_DBM_FREEDPTR(dptr) ((dptr) ? free(dptr) : 0)
  42. #undef REGISTER_CLEANUP
  43. #define REGISTER_CLEANUP(dbm, pdatum) \
  44. if ((pdatum)->dptr) \
  45. apr_pool_cleanup_register((dbm)->pool, (pdatum)->dptr, \
  46. datum_cleanup, apr_pool_cleanup_null); \
  47. else
  48. #define APR_DBM_DBMODE_RO GDBM_READER
  49. #define APR_DBM_DBMODE_RW GDBM_WRITER
  50. #define APR_DBM_DBMODE_RWCREATE GDBM_WRCREAT
  51. #define APR_DBM_DBMODE_RWTRUNC GDBM_NEWDB
  52. /* map a GDBM error to an apr_status_t */
  53. static apr_status_t g2s(int gerr)
  54. {
  55. if (gerr == -1) {
  56. /* ### need to fix this */
  57. return APR_EGENERAL;
  58. }
  59. return APR_SUCCESS;
  60. }
  61. static apr_status_t datum_cleanup(void *dptr)
  62. {
  63. if (dptr)
  64. free(dptr);
  65. return APR_SUCCESS;
  66. }
  67. static apr_status_t set_error(apr_dbm_t *dbm, apr_status_t dbm_said)
  68. {
  69. apr_status_t rv = APR_SUCCESS;
  70. /* ### ignore whatever the DBM said (dbm_said); ask it explicitly */
  71. if ((dbm->errcode = gdbm_errno) == GDBM_NO_ERROR) {
  72. dbm->errmsg = NULL;
  73. }
  74. else {
  75. dbm->errmsg = gdbm_strerror(gdbm_errno);
  76. rv = APR_EGENERAL; /* ### need something better */
  77. }
  78. /* captured it. clear it now. */
  79. gdbm_errno = GDBM_NO_ERROR;
  80. return rv;
  81. }
  82. /* --------------------------------------------------------------------------
  83. **
  84. ** DEFINE THE VTABLE FUNCTIONS FOR GDBM
  85. */
  86. static apr_status_t vt_gdbm_open(apr_dbm_t **pdb, const char *pathname,
  87. apr_int32_t mode, apr_fileperms_t perm,
  88. apr_pool_t *pool)
  89. {
  90. real_file_t file;
  91. int dbmode;
  92. *pdb = NULL;
  93. switch (mode) {
  94. case APR_DBM_READONLY:
  95. dbmode = APR_DBM_DBMODE_RO;
  96. break;
  97. case APR_DBM_READWRITE:
  98. dbmode = APR_DBM_DBMODE_RW;
  99. break;
  100. case APR_DBM_RWCREATE:
  101. dbmode = APR_DBM_DBMODE_RWCREATE;
  102. break;
  103. case APR_DBM_RWTRUNC:
  104. dbmode = APR_DBM_DBMODE_RWTRUNC;
  105. break;
  106. default:
  107. return APR_EINVAL;
  108. }
  109. {
  110. /* Note: stupid cast to get rid of "const" on the pathname */
  111. file = gdbm_open((char *) pathname, 0, dbmode,
  112. apr_posix_perms2mode(perm), NULL);
  113. if (file == NULL)
  114. return APR_EGENERAL; /* ### need a better error */
  115. }
  116. /* we have an open database... return it */
  117. *pdb = apr_pcalloc(pool, sizeof(**pdb));
  118. (*pdb)->pool = pool;
  119. (*pdb)->type = &apr_dbm_type_gdbm;
  120. SET_FILE(*pdb, file);
  121. /* ### register a cleanup to close the DBM? */
  122. return APR_SUCCESS;
  123. }
  124. static void vt_gdbm_close(apr_dbm_t *dbm)
  125. {
  126. APR_DBM_CLOSE(dbm->file);
  127. }
  128. static apr_status_t vt_gdbm_fetch(apr_dbm_t *dbm, apr_datum_t key,
  129. apr_datum_t * pvalue)
  130. {
  131. apr_status_t rv;
  132. cvt_datum_t ckey;
  133. result_datum_t rd;
  134. CONVERT_DATUM(ckey, &key);
  135. rv = APR_DBM_FETCH(dbm->file, ckey, rd);
  136. RETURN_DATUM(pvalue, rd);
  137. REGISTER_CLEANUP(dbm, pvalue);
  138. /* store the error info into DBM, and return a status code. Also, note
  139. that *pvalue should have been cleared on error. */
  140. return set_error(dbm, rv);
  141. }
  142. static apr_status_t vt_gdbm_store(apr_dbm_t *dbm, apr_datum_t key,
  143. apr_datum_t value)
  144. {
  145. apr_status_t rv;
  146. cvt_datum_t ckey;
  147. cvt_datum_t cvalue;
  148. CONVERT_DATUM(ckey, &key);
  149. CONVERT_DATUM(cvalue, &value);
  150. rv = APR_DBM_STORE(dbm->file, ckey, cvalue);
  151. /* store any error info into DBM, and return a status code. */
  152. return set_error(dbm, rv);
  153. }
  154. static apr_status_t vt_gdbm_del(apr_dbm_t *dbm, apr_datum_t key)
  155. {
  156. apr_status_t rv;
  157. cvt_datum_t ckey;
  158. CONVERT_DATUM(ckey, &key);
  159. rv = APR_DBM_DELETE(dbm->file, ckey);
  160. /* store any error info into DBM, and return a status code. */
  161. return set_error(dbm, rv);
  162. }
  163. static int vt_gdbm_exists(apr_dbm_t *dbm, apr_datum_t key)
  164. {
  165. datum *ckey = (datum *)&key;
  166. return gdbm_exists(dbm->file, *ckey) != 0;
  167. }
  168. static apr_status_t vt_gdbm_firstkey(apr_dbm_t *dbm, apr_datum_t * pkey)
  169. {
  170. apr_status_t rv;
  171. result_datum_t rd;
  172. rv = APR_DBM_FIRSTKEY(dbm->file, rd);
  173. RETURN_DATUM(pkey, rd);
  174. REGISTER_CLEANUP(dbm, pkey);
  175. /* store any error info into DBM, and return a status code. */
  176. return set_error(dbm, rv);
  177. }
  178. static apr_status_t vt_gdbm_nextkey(apr_dbm_t *dbm, apr_datum_t * pkey)
  179. {
  180. apr_status_t rv;
  181. cvt_datum_t ckey;
  182. result_datum_t rd;
  183. CONVERT_DATUM(ckey, pkey);
  184. rv = APR_DBM_NEXTKEY(dbm->file, ckey, rd);
  185. RETURN_DATUM(pkey, rd);
  186. REGISTER_CLEANUP(dbm, pkey);
  187. /* store any error info into DBM, and return a status code. */
  188. return set_error(dbm, APR_SUCCESS);
  189. }
  190. static void vt_gdbm_freedatum(apr_dbm_t *dbm, apr_datum_t data)
  191. {
  192. (void) apr_pool_cleanup_run(dbm->pool, data.dptr, datum_cleanup);
  193. }
  194. static void vt_gdbm_usednames(apr_pool_t *pool, const char *pathname,
  195. const char **used1, const char **used2)
  196. {
  197. *used1 = apr_pstrdup(pool, pathname);
  198. *used2 = NULL;
  199. }
  200. APU_DECLARE_DATA const apr_dbm_type_t apr_dbm_type_gdbm = {
  201. "gdbm",
  202. vt_gdbm_open,
  203. vt_gdbm_close,
  204. vt_gdbm_fetch,
  205. vt_gdbm_store,
  206. vt_gdbm_del,
  207. vt_gdbm_exists,
  208. vt_gdbm_firstkey,
  209. vt_gdbm_nextkey,
  210. vt_gdbm_freedatum,
  211. vt_gdbm_usednames
  212. };
  213. #endif /* APU_HAVE_GDBM */