Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
5.4 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. // Copyright (c) Microsoft Corporation 1993-1994
  4. //
  5. // File: cvol.c
  6. //
  7. // This files contains code for the cached volume ID structs.
  8. //
  9. // History:
  10. // 09-02-93 ScottH Created
  11. // 01-31-94 ScottH Moved from cache.c
  12. //
  13. //---------------------------------------------------------------------------
  14. ///////////////////////////////////////////////////// INCLUDES
  15. #include "brfprv.h" // common headers
  16. ///////////////////////////////////////////////////// TYPEDEFS
  17. ///////////////////////////////////////////////////// CONTROLLING DEFINES
  18. ///////////////////////////////////////////////////// DEFINES
  19. ///////////////////////////////////////////////////// MACROS
  20. ///////////////////////////////////////////////////// MODULE DATA
  21. CACHE g_cacheCVOL = {0, 0, 0}; // Volume ID cache
  22. ///////////////////////////////////////////////////// Generic Cache Routines
  23. #ifdef DEBUG
  24. void PRIVATE CVOL_DumpEntry(
  25. CVOL * pcvol)
  26. {
  27. ASSERT(pcvol);
  28. TRACE_MSG(TF_ALWAYS, TEXT("CVOL: Atom %d: %s"), pcvol->atomPath, Atom_GetName(pcvol->atomPath));
  29. TRACE_MSG(TF_ALWAYS, TEXT(" Ref [%u] Hvid = %lx"),
  30. Cache_GetRefCount(&g_cacheCVOL, pcvol->atomPath),
  31. pcvol->hvid);
  32. }
  33. void PUBLIC CVOL_DumpAll()
  34. {
  35. CVOL * pcvol;
  36. int atom;
  37. BOOL bDump;
  38. ENTEREXCLUSIVE()
  39. {
  40. bDump = IsFlagSet(g_uDumpFlags, DF_CVOL);
  41. }
  42. LEAVEEXCLUSIVE()
  43. if (!bDump)
  44. return ;
  45. atom = Cache_FindFirstKey(&g_cacheCVOL);
  46. while (atom != ATOM_ERR)
  47. {
  48. pcvol = Cache_GetPtr(&g_cacheCVOL, atom);
  49. ASSERT(pcvol);
  50. if (pcvol)
  51. {
  52. CVOL_DumpEntry(pcvol);
  53. Cache_DeleteItem(&g_cacheCVOL, atom, FALSE); // Decrement count
  54. }
  55. atom = Cache_FindNextKey(&g_cacheCVOL, atom);
  56. }
  57. }
  58. #endif
  59. /*----------------------------------------------------------
  60. Purpose: Release the volume ID handle
  61. Returns: --
  62. Cond: --
  63. */
  64. void CALLBACK CVOL_Free(
  65. LPVOID lpv)
  66. {
  67. CVOL * pcvol = (CVOL *)lpv;
  68. DEBUG_CODE( TRACE_MSG(TF_CACHE, TEXT("CVOL Releasing volume ID %s"), Atom_GetName(pcvol->atomPath)); )
  69. ASSERT(Sync_IsEngineLoaded());
  70. Sync_ReleaseVolumeIDHandle(pcvol->hvid);
  71. SharedFree(&pcvol);
  72. }
  73. /*----------------------------------------------------------
  74. Purpose: Add the atomPath to the cache. We add the volume ID.
  75. If atomPath is already in the cache, we replace it
  76. with a newly obtained volume ID.
  77. Returns: Pointer to CVOL
  78. NULL on OOM
  79. Cond: --
  80. */
  81. CVOL * PUBLIC CVOL_Replace(
  82. int atomPath)
  83. {
  84. CVOL * pcvol;
  85. BOOL bJustAllocd;
  86. pcvol = Cache_GetPtr(&g_cacheCVOL, atomPath);
  87. if (pcvol)
  88. bJustAllocd = FALSE;
  89. else
  90. {
  91. // Allocate using commctrl's Alloc, so the structure will be in
  92. // shared heap space across processes.
  93. pcvol = SharedAllocType(CVOL);
  94. bJustAllocd = TRUE;
  95. }
  96. if (pcvol)
  97. {
  98. HVOLUMEID hvid;
  99. LPCTSTR pszPath = Atom_GetName(atomPath);
  100. ASSERT(pszPath);
  101. DEBUG_CODE( TRACE_MSG(TF_CACHE, TEXT("CVOL Adding volume ID %s"), pszPath); )
  102. if (Sync_GetVolumeIDHandle(pszPath, &hvid) != TR_SUCCESS)
  103. {
  104. if (bJustAllocd)
  105. SharedFree(&pcvol);
  106. else
  107. Cache_DeleteItem(&g_cacheCVOL, atomPath, FALSE); // Decrement count
  108. pcvol = NULL; // Fail
  109. }
  110. else
  111. {
  112. ENTEREXCLUSIVE()
  113. {
  114. pcvol->atomPath = atomPath;
  115. pcvol->hvid = hvid;
  116. }
  117. LEAVEEXCLUSIVE()
  118. if (bJustAllocd)
  119. {
  120. if (!Cache_AddItem(&g_cacheCVOL, atomPath, (LPVOID)pcvol))
  121. {
  122. // Cache_AddItem failed here
  123. //
  124. Sync_ReleaseVolumeIDHandle(hvid);
  125. SharedFree(&pcvol);
  126. }
  127. }
  128. else
  129. Cache_DeleteItem(&g_cacheCVOL, atomPath, FALSE); // Decrement count
  130. }
  131. }
  132. return pcvol;
  133. }
  134. /*----------------------------------------------------------
  135. Purpose: Search for the given volume ID in the cache. Return
  136. the atomKey if it exists, otherwise ATOM_ERR.
  137. Returns: atom
  138. ATOM_ERR if not found
  139. Cond: --
  140. */
  141. int PUBLIC CVOL_FindID(
  142. HVOLUMEID hvid)
  143. {
  144. int atom;
  145. CVOL * pcvol;
  146. atom = Cache_FindFirstKey(&g_cacheCVOL);
  147. while (atom != ATOM_ERR)
  148. {
  149. LPCTSTR pszPath = Atom_GetName(atom);
  150. ASSERT(pszPath);
  151. ENTEREXCLUSIVE()
  152. {
  153. pcvol = CVOL_Get(atom);
  154. ASSERT(pcvol);
  155. if (pcvol)
  156. {
  157. int nCmp;
  158. Sync_CompareVolumeIDs(pcvol->hvid, hvid, &nCmp);
  159. if (Sync_GetLastError() == TR_SUCCESS && nCmp == 0)
  160. {
  161. // We found it
  162. CVOL_Delete(atom);
  163. LEAVEEXCLUSIVE()
  164. return atom;
  165. }
  166. CVOL_Delete(atom); // decrement count
  167. }
  168. }
  169. LEAVEEXCLUSIVE()
  170. atom = Cache_FindNextKey(&g_cacheCVOL, atom);
  171. }
  172. return ATOM_ERR;
  173. }