repl5_replica_dnhash.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright 2001 Sun Microsystems, Inc.
  3. * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
  4. * All rights reserved.
  5. * END COPYRIGHT BLOCK **/
  6. /* repl5_replica_dnhash.c */
  7. #include "repl5.h"
  8. #include "plhash.h"
  9. /* global data */
  10. static PLHashTable *s_hash;
  11. static PRRWLock *s_lock;
  12. /* Forward declarations */
  13. static PRIntn replica_destroy_hash_entry (PLHashEntry *he, PRIntn index, void *arg);
  14. int replica_init_dn_hash ()
  15. {
  16. /* allocate table */
  17. s_hash = PL_NewHashTable(0, PL_HashString, PL_CompareStrings,
  18. PL_CompareValues, NULL, NULL);
  19. if (s_hash == NULL)
  20. {
  21. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_init_dn_hash: "
  22. "failed to allocate hash table; NSPR error - %d\n",
  23. PR_GetError ());
  24. return -1;
  25. }
  26. /* create lock */
  27. s_lock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "replica_dnhash_lock");
  28. if (s_lock == NULL)
  29. {
  30. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_init_dn_hash: "
  31. "failed to create lock; NSPR error - %d\n",
  32. PR_GetError ());
  33. replica_destroy_dn_hash ();
  34. return -1;
  35. }
  36. return 0;
  37. }
  38. void replica_destroy_dn_hash ()
  39. {
  40. /* destroy the content */
  41. PL_HashTableEnumerateEntries(s_hash, replica_destroy_hash_entry, NULL);
  42. if (s_hash)
  43. PL_HashTableDestroy(s_hash);
  44. if (s_lock)
  45. PR_DestroyRWLock (s_lock);
  46. }
  47. int replica_add_by_dn (const char *dn)
  48. {
  49. char *dn_copy = NULL;
  50. if (dn == NULL)
  51. {
  52. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_add_by_dn: NULL argument\n");
  53. return -1;
  54. }
  55. if (s_hash == NULL || s_lock == NULL)
  56. {
  57. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_add_by_dn: "
  58. "replica hash is not initialized\n");
  59. return -1;
  60. }
  61. PR_RWLock_Wlock (s_lock);
  62. /* make sure that the dn is unique */
  63. if (PL_HashTableLookup(s_hash, dn) != NULL)
  64. {
  65. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_add_by_dn: "
  66. "replica with dn (%s) already in the hash\n", dn);
  67. PR_RWLock_Unlock (s_lock);
  68. return -1 ;
  69. }
  70. /* add dn */
  71. dn_copy = slapi_ch_strdup(dn);
  72. if (PL_HashTableAdd(s_hash, dn_copy, dn_copy) == NULL)
  73. {
  74. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_add_by_dn: "
  75. "failed to add dn (%s); NSPR error - %d\n",
  76. dn_copy, PR_GetError ());
  77. slapi_ch_free((void **)&dn_copy);
  78. PR_RWLock_Unlock (s_lock);
  79. return -1;
  80. }
  81. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_add_by_dn: "
  82. "added dn (%s)\n",
  83. dn_copy);
  84. PR_RWLock_Unlock (s_lock);
  85. return 0;
  86. }
  87. int replica_delete_by_dn (const char *dn)
  88. {
  89. char *dn_copy = NULL;
  90. if (dn == NULL)
  91. {
  92. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_delete_by_dn: "
  93. "NULL argument\n");
  94. return -1;
  95. }
  96. if (s_hash == NULL || s_lock == NULL)
  97. {
  98. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_delete_by_dn: "
  99. "replica hash is not initialized\n");
  100. return -1;
  101. }
  102. PR_RWLock_Wlock (s_lock);
  103. /* locate object */
  104. if (NULL == (dn_copy = (char *)PL_HashTableLookup(s_hash, dn)))
  105. {
  106. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_delete_by_dn: "
  107. "dn (%s) is not in the hash.\n", dn);
  108. PR_RWLock_Unlock (s_lock);
  109. return -1;
  110. }
  111. /* remove from hash */
  112. PL_HashTableRemove(s_hash, dn);
  113. slapi_ch_free((void **)&dn_copy);
  114. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_delete_by_dn: "
  115. "removed dn (%s)\n",
  116. dn);
  117. PR_RWLock_Unlock (s_lock);
  118. return 0;
  119. }
  120. int replica_is_being_configured (const char *dn)
  121. {
  122. if (dn == NULL)
  123. {
  124. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_is_dn_in_hash: "
  125. "NULL argument\n");
  126. return 0;
  127. }
  128. if (s_hash == NULL || s_lock == NULL)
  129. {
  130. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_is_dn_in_hash: "
  131. "dn hash is not initialized\n");
  132. return 0;
  133. }
  134. PR_RWLock_Wlock (s_lock);
  135. /* locate object */
  136. if (NULL == PL_HashTableLookup(s_hash, dn))
  137. {
  138. PR_RWLock_Unlock (s_lock);
  139. return 0;
  140. }
  141. PR_RWLock_Unlock (s_lock);
  142. return 1;
  143. }
  144. /* Helper functions */
  145. /* this function called for each hash node during hash destruction */
  146. static PRIntn replica_destroy_hash_entry (PLHashEntry *he, PRIntn index, void *arg)
  147. {
  148. char *dn_copy;
  149. if (he == NULL)
  150. return HT_ENUMERATE_NEXT;
  151. dn_copy = (char*)he->value;
  152. slapi_ch_free((void **)&dn_copy);
  153. return HT_ENUMERATE_REMOVE;
  154. }