2
0

apr_dbm_sdbm.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #define APR_WANT_MEMFUNC
  18. #define APR_WANT_STRFUNC
  19. #include "apr_want.h"
  20. #include "apu.h"
  21. #if APU_HAVE_SDBM
  22. #include "apr_dbm_private.h"
  23. #include "apr_sdbm.h"
  24. #if APR_HAVE_STDLIB_H
  25. #include <stdlib.h> /* For abort() */
  26. #endif
  27. /* this is used in a few places to define a noop "function". it is needed
  28. to stop "no effect" warnings from GCC. */
  29. #define NOOP_FUNCTION if (0) ; else
  30. /* ### define defaults for now; these will go away in a while */
  31. #define REGISTER_CLEANUP(dbm, pdatum) NOOP_FUNCTION
  32. #define SET_FILE(pdb, f) ((pdb)->file = (f))
  33. typedef apr_sdbm_t *real_file_t;
  34. typedef apr_sdbm_datum_t cvt_datum_t;
  35. #define CONVERT_DATUM(cvt, pinput) ((cvt).dptr = (pinput)->dptr, (cvt).dsize = (pinput)->dsize)
  36. typedef apr_sdbm_datum_t result_datum_t;
  37. #define RETURN_DATUM(poutput, rd) ((poutput)->dptr = (rd).dptr, (poutput)->dsize = (rd).dsize)
  38. #define APR_DBM_CLOSE(f) apr_sdbm_close(f)
  39. #define APR_DBM_FETCH(f, k, v) apr_sdbm_fetch(f, &(v), (k))
  40. #define APR_DBM_STORE(f, k, v) apr_sdbm_store(f, (k), (v), APR_SDBM_REPLACE)
  41. #define APR_DBM_DELETE(f, k) apr_sdbm_delete(f, (k))
  42. #define APR_DBM_FIRSTKEY(f, k) apr_sdbm_firstkey(f, &(k))
  43. #define APR_DBM_NEXTKEY(f, k, nk) apr_sdbm_nextkey(f, &(nk))
  44. #define APR_DBM_FREEDPTR(dptr) NOOP_FUNCTION
  45. #define APR_DBM_DBMODE_RO APR_READ
  46. #define APR_DBM_DBMODE_RW (APR_READ | APR_WRITE)
  47. #define APR_DBM_DBMODE_RWCREATE (APR_READ | APR_WRITE | APR_CREATE)
  48. #define APR_DBM_DBMODE_RWTRUNC (APR_READ | APR_WRITE | APR_CREATE | \
  49. APR_TRUNCATE)
  50. static apr_status_t set_error(apr_dbm_t *dbm, apr_status_t dbm_said)
  51. {
  52. apr_status_t rv = APR_SUCCESS;
  53. /* ### ignore whatever the DBM said (dbm_said); ask it explicitly */
  54. if ((dbm->errcode = dbm_said) == APR_SUCCESS) {
  55. dbm->errmsg = NULL;
  56. }
  57. else {
  58. dbm->errmsg = "I/O error occurred.";
  59. rv = APR_EGENERAL; /* ### need something better */
  60. }
  61. return rv;
  62. }
  63. /* --------------------------------------------------------------------------
  64. **
  65. ** DEFINE THE VTABLE FUNCTIONS FOR SDBM
  66. */
  67. static apr_status_t vt_sdbm_open(apr_dbm_t **pdb, const char *pathname,
  68. apr_int32_t mode, apr_fileperms_t perm,
  69. apr_pool_t *pool)
  70. {
  71. real_file_t file;
  72. int dbmode;
  73. *pdb = NULL;
  74. switch (mode) {
  75. case APR_DBM_READONLY:
  76. dbmode = APR_DBM_DBMODE_RO;
  77. break;
  78. case APR_DBM_READWRITE:
  79. dbmode = APR_DBM_DBMODE_RW;
  80. break;
  81. case APR_DBM_RWCREATE:
  82. dbmode = APR_DBM_DBMODE_RWCREATE;
  83. break;
  84. case APR_DBM_RWTRUNC:
  85. dbmode = APR_DBM_DBMODE_RWTRUNC;
  86. break;
  87. default:
  88. return APR_EINVAL;
  89. }
  90. {
  91. apr_status_t rv;
  92. rv = apr_sdbm_open(&file, pathname, dbmode, perm, pool);
  93. if (rv != APR_SUCCESS)
  94. return rv;
  95. }
  96. /* we have an open database... return it */
  97. *pdb = apr_pcalloc(pool, sizeof(**pdb));
  98. (*pdb)->pool = pool;
  99. (*pdb)->type = &apr_dbm_type_sdbm;
  100. SET_FILE(*pdb, file);
  101. /* ### register a cleanup to close the DBM? */
  102. return APR_SUCCESS;
  103. }
  104. static void vt_sdbm_close(apr_dbm_t *dbm)
  105. {
  106. APR_DBM_CLOSE(dbm->file);
  107. }
  108. static apr_status_t vt_sdbm_fetch(apr_dbm_t *dbm, apr_datum_t key,
  109. apr_datum_t * pvalue)
  110. {
  111. apr_status_t rv;
  112. cvt_datum_t ckey;
  113. result_datum_t rd;
  114. CONVERT_DATUM(ckey, &key);
  115. rv = APR_DBM_FETCH(dbm->file, ckey, rd);
  116. RETURN_DATUM(pvalue, rd);
  117. REGISTER_CLEANUP(dbm, pvalue);
  118. /* store the error info into DBM, and return a status code. Also, note
  119. that *pvalue should have been cleared on error. */
  120. return set_error(dbm, rv);
  121. }
  122. static apr_status_t vt_sdbm_store(apr_dbm_t *dbm, apr_datum_t key,
  123. apr_datum_t value)
  124. {
  125. apr_status_t rv;
  126. cvt_datum_t ckey;
  127. cvt_datum_t cvalue;
  128. CONVERT_DATUM(ckey, &key);
  129. CONVERT_DATUM(cvalue, &value);
  130. rv = APR_DBM_STORE(dbm->file, ckey, cvalue);
  131. /* store any error info into DBM, and return a status code. */
  132. return set_error(dbm, rv);
  133. }
  134. static apr_status_t vt_sdbm_del(apr_dbm_t *dbm, apr_datum_t key)
  135. {
  136. apr_status_t rv;
  137. cvt_datum_t ckey;
  138. CONVERT_DATUM(ckey, &key);
  139. rv = APR_DBM_DELETE(dbm->file, ckey);
  140. /* store any error info into DBM, and return a status code. */
  141. return set_error(dbm, rv);
  142. }
  143. static int vt_sdbm_exists(apr_dbm_t *dbm, apr_datum_t key)
  144. {
  145. int exists;
  146. apr_sdbm_datum_t ckey;
  147. CONVERT_DATUM(ckey, &key);
  148. {
  149. apr_sdbm_datum_t value;
  150. if (apr_sdbm_fetch(dbm->file, &value, ckey) != APR_SUCCESS) {
  151. exists = 0;
  152. }
  153. else
  154. exists = value.dptr != NULL;
  155. }
  156. return exists;
  157. }
  158. static apr_status_t vt_sdbm_firstkey(apr_dbm_t *dbm, apr_datum_t * pkey)
  159. {
  160. apr_status_t rv;
  161. result_datum_t rd;
  162. rv = APR_DBM_FIRSTKEY(dbm->file, rd);
  163. RETURN_DATUM(pkey, rd);
  164. REGISTER_CLEANUP(dbm, pkey);
  165. /* store any error info into DBM, and return a status code. */
  166. return set_error(dbm, rv);
  167. }
  168. static apr_status_t vt_sdbm_nextkey(apr_dbm_t *dbm, apr_datum_t * pkey)
  169. {
  170. apr_status_t rv;
  171. cvt_datum_t ckey;
  172. result_datum_t rd;
  173. CONVERT_DATUM(ckey, pkey);
  174. rv = APR_DBM_NEXTKEY(dbm->file, ckey, rd);
  175. RETURN_DATUM(pkey, rd);
  176. REGISTER_CLEANUP(dbm, pkey);
  177. /* store any error info into DBM, and return a status code. */
  178. return set_error(dbm, APR_SUCCESS);
  179. }
  180. static void vt_sdbm_freedatum(apr_dbm_t *dbm, apr_datum_t data)
  181. {
  182. APR_DBM_FREEDPTR(data.dptr);
  183. }
  184. static void vt_sdbm_usednames(apr_pool_t *pool, const char *pathname,
  185. const char **used1, const char **used2)
  186. {
  187. char *work;
  188. /* ### this could be optimized by computing strlen() once and using
  189. ### memcpy and pmemdup instead. but why bother? */
  190. *used1 = apr_pstrcat(pool, pathname, APR_SDBM_DIRFEXT, NULL);
  191. *used2 = work = apr_pstrdup(pool, *used1);
  192. /* we know the extension is 4 characters */
  193. memcpy(&work[strlen(work) - 4], APR_SDBM_PAGFEXT, 4);
  194. }
  195. APU_DECLARE_DATA const apr_dbm_type_t apr_dbm_type_sdbm = {
  196. "sdbm",
  197. vt_sdbm_open,
  198. vt_sdbm_close,
  199. vt_sdbm_fetch,
  200. vt_sdbm_store,
  201. vt_sdbm_del,
  202. vt_sdbm_exists,
  203. vt_sdbm_firstkey,
  204. vt_sdbm_nextkey,
  205. vt_sdbm_freedatum,
  206. vt_sdbm_usednames
  207. };
  208. #endif /* APU_HAVE_SDBM */