testdbinterop.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* --- BEGIN COPYRIGHT BLOCK ---
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * --- END COPYRIGHT BLOCK --- */
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #include <sys/types.h>
  42. #include <stdio.h>
  43. #include "testdbinterop.h"
  44. #include "slapi-plugin.h"
  45. #define DB_PLUGIN_NAME "nullsuffix-preop"
  46. static PRLock *db_lock=NULL;
  47. #ifdef USE_BDB
  48. #include "db.h"
  49. #define DATABASE "access.db"
  50. static int number_of_keys=100;
  51. static int key_buffer_size = 8000;
  52. #if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR >= 4100
  53. #define DB_OPEN(db, txnid, file, database, type, flags, mode) \
  54. (db)->open((db), (txnid), (file), (database), (type), (flags), (mode))
  55. #else
  56. (db)->open((db), (file), (database), (type), (flags), (mode))
  57. #endif
  58. DB *dbp=NULL;
  59. void
  60. create_db()
  61. {
  62. int ret;
  63. DBT key, data;
  64. if ((ret = db_create(&dbp, NULL, 0)) != 0) {
  65. fprintf(stderr, "db_create: %s\n", db_strerror(ret));
  66. exit (1);
  67. }
  68. }
  69. void make_key(DBT *key)
  70. {
  71. char *key_string = (char*)(key->data);
  72. unsigned int seed = (unsigned int)time( (time_t*) 0);
  73. long int key_long = slapi_rand_r(&seed) % number_of_keys;
  74. sprintf(key_string,"key%ld",key_long);
  75. slapi_log_error(SLAPI_LOG_PLUGIN, DB_PLUGIN_NAME,"generated key: %s\n", key_string);
  76. key->size = strlen(key_string);
  77. }
  78. void
  79. db_put_dn(char *data_dn)
  80. {
  81. int ret;
  82. DBT key = {0};
  83. DBT data = {0};
  84. if(db_lock == NULL){
  85. db_lock = PR_NewLock();
  86. }
  87. PR_Lock(db_lock);
  88. create_db();
  89. if ((ret = DB_OPEN(dbp, NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
  90. dbp->err(dbp, ret, "%s", DATABASE);
  91. }
  92. memset(&key, 0, sizeof(key));
  93. memset(&data, 0, sizeof(data));
  94. key.data = (char *)malloc(key_buffer_size);
  95. /* make_key will set up the key and the data */
  96. make_key(&key);
  97. data.data = slapi_ch_strdup(data_dn);
  98. data.size = strlen(data_dn);
  99. switch (ret =
  100. dbp->put(dbp, NULL, &key, &data, DB_NOOVERWRITE)) {
  101. case 0:
  102. slapi_log_error(SLAPI_LOG_PLUGIN, DB_PLUGIN_NAME, "db: %s: key stored.\n", (char *)key.data);
  103. break;
  104. case DB_KEYEXIST:
  105. slapi_log_error(SLAPI_LOG_PLUGIN, DB_PLUGIN_NAME, "db: %s: key previously stored.\n",
  106. (char *)key.data);
  107. break;
  108. default:
  109. dbp->err(dbp, ret, "DB->put");
  110. goto err;
  111. }
  112. err:
  113. if(ret){
  114. slapi_log_error(SLAPI_LOG_PLUGIN, DB_PLUGIN_NAME, "db: Error detected in db_put \n");
  115. }
  116. free(key.data);
  117. if (dbp){
  118. dbp->close(dbp,0);
  119. dbp=NULL;
  120. }
  121. PR_Unlock(db_lock);
  122. }
  123. #else /* USE_BDB */
  124. #include <string.h>
  125. #define DATABASE "/tmp/testdbinterop.db"
  126. #define DATABASE_BACK "/tmp/testdbinterop.db.bak"
  127. void
  128. db_put_dn(char *data_dn)
  129. {
  130. int ret;
  131. char *db_path = DATABASE;
  132. char *db_path_bak = DATABASE_BACK;
  133. PRFileInfo64 info;
  134. PRFileDesc *prfd;
  135. PRInt32 data_sz;
  136. char *data_dnp = NULL;
  137. if(db_lock == NULL){
  138. db_lock = PR_NewLock();
  139. }
  140. PR_Lock(db_lock);
  141. /* if db_path is a directory, rename it */
  142. ret = PR_GetFileInfo64(db_path, &info);
  143. if (PR_SUCCESS == ret) {
  144. if (PR_FILE_DIRECTORY == info.type) { /* directory */
  145. ret = PR_GetFileInfo64(db_path_bak, &info);
  146. if (PR_SUCCESS == ret) {
  147. if (PR_FILE_DIRECTORY != info.type) { /* not a directory */
  148. PR_Delete(db_path_bak);
  149. }
  150. }
  151. PR_Rename(db_path, db_path_bak);
  152. }
  153. }
  154. /* open a file */
  155. if ((prfd = PR_Open(db_path, PR_RDWR | PR_CREATE_FILE | PR_APPEND, 0600)) == NULL ) {
  156. slapi_log_error(SLAPI_LOG_FATAL, DB_PLUGIN_NAME,
  157. "db: Could not open file \"%s\" for read/write; %d (%s)\n",
  158. db_path, PR_GetError(), slapd_pr_strerror(PR_GetError()));
  159. return;
  160. }
  161. data_dnp = slapi_ch_smprintf("%s\n", data_dn);
  162. data_sz = (PRInt32)strlen(data_dnp);
  163. ret = PR_Write(prfd, data_dnp, data_sz);
  164. if (ret == data_sz) {
  165. slapi_log_error(SLAPI_LOG_PLUGIN, DB_PLUGIN_NAME,
  166. "db: %s: key stored.\n", data_dn);
  167. ret = 0;
  168. } else {
  169. slapi_log_error(SLAPI_LOG_FATAL, DB_PLUGIN_NAME,
  170. "db: Failed to store key \"%s\"; %d (%s)\n",
  171. data_dn, PR_GetError(), slapd_pr_strerror(PR_GetError()));
  172. ret = 1;
  173. }
  174. if(ret) {
  175. slapi_log_error(SLAPI_LOG_FATAL, DB_PLUGIN_NAME,
  176. "db: Error detected in db_put_dn \n");
  177. }
  178. slapi_ch_free_string(&data_dnp);
  179. PR_Close(prfd);
  180. PR_Unlock(db_lock);
  181. return;
  182. }
  183. #endif