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.

214 lines
5.1 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. // Copyright (c) Microsoft Corporation 1993-1994
  4. //
  5. // File: cpath.c
  6. //
  7. // This files contains code for the cached briefcase paths.
  8. //
  9. // History:
  10. // 01-31-94 ScottH Created
  11. //
  12. //---------------------------------------------------------------------------
  13. ///////////////////////////////////////////////////// INCLUDES
  14. #include "brfprv.h" // common headers
  15. CACHE g_cacheCPATH = {0, 0, 0}; // Briefcase path cache
  16. #define CPATH_EnterCS() EnterCriticalSection(&g_cacheCPATH.cs)
  17. #define CPATH_LeaveCS() LeaveCriticalSection(&g_cacheCPATH.cs)
  18. #ifdef DEBUG
  19. /*----------------------------------------------------------
  20. Purpose: Dumps a CPATH entry
  21. Returns:
  22. Cond: --
  23. */
  24. void PRIVATE CPATH_DumpEntry(
  25. CPATH * pcpath)
  26. {
  27. ASSERT(pcpath);
  28. TRACE_MSG(TF_ALWAYS, TEXT("CPATH: Atom %d: %s"), pcpath->atomPath, Atom_GetName(pcpath->atomPath));
  29. TRACE_MSG(TF_ALWAYS, TEXT(" Ref [%u] "),
  30. Cache_GetRefCount(&g_cacheCPATH, pcpath->atomPath));
  31. }
  32. /*----------------------------------------------------------
  33. Purpose: Dumps all CPATH cache
  34. Returns:
  35. Cond: --
  36. */
  37. void PUBLIC CPATH_DumpAll()
  38. {
  39. CPATH * pcpath;
  40. int atom;
  41. BOOL bDump;
  42. ENTEREXCLUSIVE()
  43. {
  44. bDump = IsFlagSet(g_uDumpFlags, DF_CPATH);
  45. }
  46. LEAVEEXCLUSIVE()
  47. if (!bDump)
  48. return ;
  49. atom = Cache_FindFirstKey(&g_cacheCPATH);
  50. while (atom != ATOM_ERR)
  51. {
  52. pcpath = Cache_GetPtr(&g_cacheCPATH, atom);
  53. ASSERT(pcpath);
  54. if (pcpath)
  55. {
  56. CPATH_DumpEntry(pcpath);
  57. Cache_DeleteItem(&g_cacheCPATH, atom, FALSE, NULL, CPATH_Free); // Decrement count
  58. }
  59. atom = Cache_FindNextKey(&g_cacheCPATH, atom);
  60. }
  61. }
  62. #endif
  63. /*----------------------------------------------------------
  64. Purpose: Release the volume ID handle
  65. Returns: --
  66. Cond: hwndOwner is not used.
  67. This function is serialized by the caller (Cache_Term or
  68. Cache_DeleteItem).
  69. */
  70. void CALLBACK CPATH_Free(
  71. LPVOID lpv,
  72. HWND hwndOwner)
  73. {
  74. CPATH * pcpath = (CPATH *)lpv;
  75. DEBUG_CODE( TRACE_MSG(TF_CACHE, TEXT("CPATH Freeing Briefcase path %s"), Atom_GetName(pcpath->atomPath)); )
  76. // Delete the atom one extra time, because we explicitly added
  77. // it for this cache.
  78. Atom_Delete(pcpath->atomPath);
  79. SharedFree(&pcpath);
  80. }
  81. /*----------------------------------------------------------
  82. Purpose: Add the atomPath to the cache.
  83. If atomPath is already in the cache, we replace it
  84. with a newly obtained path.
  85. Returns: Pointer to CPATH
  86. NULL on OOM
  87. Cond: --
  88. */
  89. CPATH * PUBLIC CPATH_Replace(
  90. int atomPath)
  91. {
  92. CPATH * pcpath;
  93. BOOL bJustAllocd;
  94. CPATH_EnterCS();
  95. {
  96. pcpath = Cache_GetPtr(&g_cacheCPATH, atomPath);
  97. if (pcpath)
  98. bJustAllocd = FALSE;
  99. else
  100. {
  101. // Allocate using commctrl's Alloc, so the structure will be in
  102. // shared heap space across processes.
  103. pcpath = SharedAllocType(CPATH);
  104. bJustAllocd = TRUE;
  105. }
  106. if (pcpath)
  107. {
  108. LPCTSTR pszPath = Atom_GetName(atomPath);
  109. ASSERT(pszPath);
  110. DEBUG_CODE( TRACE_MSG(TF_CACHE, TEXT("CPATH Adding known Briefcase %s"), pszPath); )
  111. pcpath->atomPath = atomPath;
  112. if (bJustAllocd)
  113. {
  114. if (!Cache_AddItem(&g_cacheCPATH, atomPath, (LPVOID)pcpath))
  115. {
  116. // Cache_AddItem failed here
  117. //
  118. SharedFree(&pcpath);
  119. }
  120. }
  121. else
  122. Cache_DeleteItem(&g_cacheCPATH, atomPath, FALSE, NULL, CPATH_Free); // Decrement count
  123. }
  124. }
  125. CPATH_LeaveCS();
  126. return pcpath;
  127. }
  128. /*----------------------------------------------------------
  129. Purpose: Search for the given path in the cache. If the path
  130. exists, its locality will be returned.
  131. If it is not found, its locality is not known (but
  132. PL_FALSE is returned).
  133. Returns: path locality (PL_) value
  134. Cond: --
  135. */
  136. UINT PUBLIC CPATH_GetLocality(
  137. LPCTSTR pszPath,
  138. LPTSTR pszBuf) // Can be NULL, or must be MAXPATHLEN
  139. {
  140. UINT uRet = PL_FALSE;
  141. LPCTSTR pszBrf;
  142. int atom;
  143. ASSERT(pszPath);
  144. CPATH_EnterCS();
  145. {
  146. atom = Cache_FindFirstKey(&g_cacheCPATH);
  147. while (atom != ATOM_ERR)
  148. {
  149. pszBrf = Atom_GetName(atom);
  150. ASSERT(pszBrf);
  151. if (IsSzEqual(pszBrf, pszPath))
  152. {
  153. uRet = PL_ROOT;
  154. break;
  155. }
  156. else if (PathIsPrefix(pszBrf, pszPath))
  157. {
  158. uRet = PL_INSIDE;
  159. break;
  160. }
  161. atom = Cache_FindNextKey(&g_cacheCPATH, atom);
  162. }
  163. if (uRet != PL_FALSE && pszBuf)
  164. lstrcpy(pszBuf, pszBrf);
  165. }
  166. CPATH_LeaveCS();
  167. return uRet;
  168. }