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.

385 lines
12 KiB

  1. // History:
  2. // 5-30-94 KurtE Created.
  3. #include "shellprv.h"
  4. #pragma hdrstop
  5. //#define PARANOID_VALIDATE_UPDATE
  6. // Define our global states here. Note: we will do it per process
  7. typedef struct _RLPI // Registry List Process Info
  8. {
  9. HDPA hdpaRLList; // The dpa of items
  10. BOOL fCSInitialized; // Have we initialized the CS in this process
  11. BOOL fListValid; // Is the list up to date and valid
  12. CRITICAL_SECTION csRLList; // The critical section for the process
  13. } RLPI;
  14. RLPI g_rlpi = {NULL, FALSE, FALSE};
  15. // Simple DPA compare function make sure we don't have elsewhere...
  16. int CALLBACK _CompareStrings(LPVOID sz1, LPVOID sz2, LPARAM lparam)
  17. {
  18. return lstrcmpi((LPTSTR)sz1, (LPTSTR)sz2);
  19. }
  20. void RLEnterCritical()
  21. {
  22. if (!g_rlpi.fCSInitialized)
  23. {
  24. // Do this under the global critical section.
  25. ENTERCRITICAL;
  26. if (!g_rlpi.fCSInitialized)
  27. {
  28. g_rlpi.fCSInitialized = TRUE;
  29. InitializeCriticalSection(&g_rlpi.csRLList);
  30. }
  31. LEAVECRITICAL;
  32. }
  33. EnterCriticalSection(&g_rlpi.csRLList);
  34. }
  35. void RLLeaveCritical()
  36. {
  37. LeaveCriticalSection(&g_rlpi.csRLList);
  38. }
  39. // Enumerate through the registry looking for paths that
  40. // we may wish to track. The current ones that we look for
  41. STDAPI_(BOOL) RLEnumRegistry(HDPA hdpa, PRLCALLBACK pfnrlcb, LPCTSTR pszSource, LPCTSTR pszDest)
  42. {
  43. HKEY hkeyRoot;
  44. // First look in the App Paths section
  45. if (RegOpenKey(HKEY_LOCAL_MACHINE, REGSTR_PATH_APPPATHS, &hkeyRoot) == ERROR_SUCCESS)
  46. {
  47. int iRootName;
  48. TCHAR szRootName[80];
  49. for (iRootName = 0;
  50. RegEnumKey(hkeyRoot, iRootName, szRootName, ARRAYSIZE(szRootName)) == ERROR_SUCCESS;
  51. iRootName++)
  52. {
  53. // Now see if the app has a qualifid path here.
  54. HKEY hkeySecond;
  55. TCHAR szPath[MAX_PATH];
  56. long cbValue = sizeof(szPath);
  57. if (SHRegQueryValue(hkeyRoot, szRootName, szPath, &cbValue) == ERROR_SUCCESS)
  58. {
  59. PathUnquoteSpaces(szPath);
  60. pfnrlcb(hdpa, hkeyRoot, szRootName, NULL, szPath, pszSource, pszDest);
  61. }
  62. // Now try to enum this key for Path Value.
  63. if (RegOpenKey(hkeyRoot, szRootName, &hkeySecond) == ERROR_SUCCESS)
  64. {
  65. cbValue = sizeof(szPath);
  66. if (SHQueryValueEx(hkeySecond, TEXT("PATH"), NULL, NULL, szPath, &cbValue) == ERROR_SUCCESS)
  67. {
  68. // this is a ";" separated list
  69. LPTSTR psz = StrChr(szPath, TEXT(';'));
  70. if (psz)
  71. *psz = 0;
  72. PathUnquoteSpaces(szPath);
  73. pfnrlcb(hdpa, hkeySecond, NULL, TEXT("PATH"), szPath, pszSource, pszDest);
  74. }
  75. RegCloseKey(hkeySecond);
  76. }
  77. }
  78. RegCloseKey(hkeyRoot);
  79. }
  80. return TRUE;
  81. }
  82. // This is the call back called to build the list of paths.
  83. BOOL CALLBACK _RLBuildListCallBack(HDPA hdpa, HKEY hkey, LPCTSTR pszKey,
  84. LPCTSTR pszValueName, LPTSTR pszValue, LPCTSTR pszSource, LPCTSTR pszDest)
  85. {
  86. int iIndex;
  87. // Also don't add any relative paths.
  88. if (PathIsRelative(pszValue) || (lstrlen(pszValue) < 3))
  89. return TRUE;
  90. // Don't try UNC names as this can get expensive...
  91. if (PathIsUNC(pszValue))
  92. return TRUE;
  93. // If it is already in our list, we can simply return now..
  94. if (DPA_Search(hdpa, pszValue, 0, _CompareStrings, 0, DPAS_SORTED) != -1)
  95. return TRUE;
  96. // If it is in our old list then
  97. if (g_rlpi.hdpaRLList && ((iIndex = DPA_Search(g_rlpi.hdpaRLList, pszValue, 0,
  98. _CompareStrings, 0, DPAS_SORTED)) != -1))
  99. {
  100. // Found the item in the old list.
  101. TraceMsg(TF_REG, "_RLBuildListCallBack: Add from old list %s", pszValue);
  102. DPA_InsertPtr(hdpa,
  103. DPA_Search(hdpa, pszValue, 0,
  104. _CompareStrings, 0,
  105. DPAS_SORTED|DPAS_INSERTBEFORE),
  106. (LPTSTR)DPA_FastGetPtr(g_rlpi.hdpaRLList, iIndex));
  107. // now remove it from the old list
  108. DPA_DeletePtr(g_rlpi.hdpaRLList, iIndex);
  109. }
  110. else
  111. {
  112. // Not in either list.
  113. // Now see if we can convert the short name to a long name
  114. TCHAR szLongName[MAX_PATH];
  115. int cchName;
  116. LPTSTR psz;
  117. if (!GetLongPathName(pszValue, szLongName, ARRAYSIZE(szLongName)))
  118. szLongName[0] = 0;
  119. if (lstrcmpi(szLongName, pszValue) == 0)
  120. szLongName[0] = 0; // Don't need both strings.
  121. cchName = lstrlen(pszValue);
  122. psz = (LPTSTR)LocalAlloc(LPTR,
  123. (cchName + 1 + lstrlen(szLongName) + 1) * sizeof(TCHAR));
  124. if (psz)
  125. {
  126. TraceMsg(TF_REG, "_RLBuildListCallBack: Add %s", pszValue);
  127. lstrcpy(psz, pszValue);
  128. lstrcpy(psz + cchName + 1, szLongName);
  129. return DPA_InsertPtr(hdpa,
  130. DPA_Search(hdpa, pszValue, 0,
  131. _CompareStrings, 0,
  132. DPAS_SORTED|DPAS_INSERTBEFORE),
  133. psz);
  134. }
  135. }
  136. return TRUE;
  137. }
  138. // This function will build the list of items that we
  139. // will look through to see if the user may have changed the path of
  140. // of one of the programs that is registered in the registry.
  141. //
  142. BOOL WINAPI RLBuildListOfPaths()
  143. {
  144. BOOL fRet = FALSE;
  145. HDPA hdpa;
  146. DEBUG_CODE( DWORD dwStart = GetCurrentTime(); )
  147. RLEnterCritical();
  148. hdpa = DPA_Create(0);
  149. if (!hdpa)
  150. goto Error;
  151. // And initialize the list
  152. fRet = RLEnumRegistry(hdpa, _RLBuildListCallBack, NULL, NULL);
  153. // If we had on old list destroy it now.
  154. if (g_rlpi.hdpaRLList)
  155. {
  156. // Walk through all of the items in the list and
  157. // delete all of the strings.
  158. int i;
  159. for (i = DPA_GetPtrCount(g_rlpi.hdpaRLList)-1; i >= 0; i--)
  160. LocalFree((HLOCAL)DPA_FastGetPtr(g_rlpi.hdpaRLList, i));
  161. DPA_Destroy(g_rlpi.hdpaRLList);
  162. }
  163. g_rlpi.hdpaRLList = hdpa;
  164. g_rlpi.fListValid = TRUE; // Say that we are valid...
  165. DEBUG_CODE( TraceMsg(TF_REG, "RLBuildListOfPaths time: %ld", GetCurrentTime()-dwStart); )
  166. Error:
  167. RLLeaveCritical();
  168. return fRet;
  169. }
  170. // this function does any cleanup necessary for when a process
  171. // is no longer going to use the Registry list.
  172. void WINAPI RLTerminate()
  173. {
  174. int i;
  175. if (!g_rlpi.hdpaRLList)
  176. return;
  177. RLEnterCritical();
  178. // Re-check under critical section in case somebody else destroyed
  179. // it while we were waiting
  180. if (g_rlpi.hdpaRLList)
  181. {
  182. // Walk through all of the items in the list and
  183. // delete all of the strings.
  184. for (i = DPA_GetPtrCount(g_rlpi.hdpaRLList)-1; i >= 0; i--)
  185. LocalFree((HLOCAL)DPA_FastGetPtr(g_rlpi.hdpaRLList, i));
  186. DPA_Destroy(g_rlpi.hdpaRLList);
  187. g_rlpi.hdpaRLList = NULL;
  188. }
  189. RLLeaveCritical();
  190. }
  191. // This function returns TRUE if the path that is passed
  192. // in is contained in one or more of the paths that we extracted from
  193. // the registry.
  194. int WINAPI RLIsPathInList(LPCTSTR pszPath)
  195. {
  196. int i = -1;
  197. RLEnterCritical();
  198. if (!g_rlpi.hdpaRLList || !g_rlpi.fListValid)
  199. RLBuildListOfPaths();
  200. if (g_rlpi.hdpaRLList)
  201. {
  202. int cchPath = lstrlen(pszPath);
  203. for (i = DPA_GetPtrCount(g_rlpi.hdpaRLList) - 1; i >= 0; i--)
  204. {
  205. LPTSTR psz = DPA_FastGetPtr(g_rlpi.hdpaRLList, i);
  206. if (PathCommonPrefix(pszPath, psz, NULL) == cchPath)
  207. break;
  208. // See if a long file name to check.
  209. psz += lstrlen(psz) + 1;
  210. if (*psz && (PathCommonPrefix(pszPath, psz, NULL) == cchPath))
  211. break;
  212. }
  213. }
  214. RLLeaveCritical();
  215. return i; // -1 if none, >= 0 index
  216. }
  217. // This is the call back called to build the list of of paths.
  218. //
  219. BOOL CALLBACK _RLRenameCallBack(HDPA hdpa, HKEY hkey, LPCTSTR pszKey,
  220. LPCTSTR pszValueName, LPTSTR pszValue, LPCTSTR pszSource, LPCTSTR pszDest)
  221. {
  222. int cbMatch = PathCommonPrefix(pszValue, pszSource, NULL);
  223. if (cbMatch == lstrlen(pszSource))
  224. {
  225. TCHAR szPath[MAX_PATH+64]; // Add some slop just in case...
  226. // Found a match, lets try to rebuild the new line
  227. lstrcpy(szPath, pszDest);
  228. lstrcat(szPath, pszValue + cbMatch);
  229. if (pszValueName)
  230. RegSetValueEx(hkey, pszValueName, 0, REG_SZ, (BYTE *)szPath, (lstrlen(szPath) + 1) * sizeof(TCHAR));
  231. else
  232. RegSetValue(hkey, pszKey, REG_SZ, szPath, lstrlen(szPath));
  233. }
  234. // Make sure that we have not allready added
  235. // this path to our list.
  236. if (DPA_Search(hdpa, pszValue, 0, _CompareStrings, 0, DPAS_SORTED) == -1)
  237. {
  238. // One to Add!
  239. LPTSTR psz = StrDup(pszValue);
  240. if (psz)
  241. {
  242. return DPA_InsertPtr(hdpa,
  243. DPA_Search(hdpa, pszValue, 0,
  244. _CompareStrings, 0,
  245. DPAS_SORTED | DPAS_INSERTBEFORE), psz);
  246. }
  247. }
  248. return TRUE;
  249. }
  250. // This function handles the cases when we are notified of
  251. // a change to the file system and then we need to see if there are
  252. // any changes that we need to make to the regisry to handle the changes.
  253. int WINAPI RLFSChanged(LONG lEvent, LPITEMIDLIST pidl, LPITEMIDLIST pidlExtra)
  254. {
  255. TCHAR szSrc[MAX_PATH];
  256. TCHAR szDest[MAX_PATH+8]; // For slop like Quotes...
  257. int iIndex;
  258. LPTSTR psz;
  259. int iRet = -1;
  260. int i;
  261. // First see if the operation is something we are interested in.
  262. if ((lEvent & (SHCNE_RENAMEITEM | SHCNE_RENAMEFOLDER)) == 0)
  263. return -1; // Nope
  264. if (!SHGetPathFromIDList(pidl, szSrc))
  265. {
  266. // must be a rename of a non-filesys object (such as a printer!)
  267. return -1;
  268. }
  269. SHGetPathFromIDList(pidlExtra, szDest);
  270. // If either are roots we really can not rename them...
  271. if (PathIsRoot(szSrc) || PathIsRoot(szDest))
  272. return -1;
  273. // ignore if coming from bitbucket or going to ...
  274. // check bitbucket first. that's a cheap call
  275. if ((lEvent & SHCNE_RENAMEITEM) &&
  276. (IsFileInBitBucket(szSrc) || IsFileInBitBucket(szDest)))
  277. return -1;
  278. RLEnterCritical();
  279. // Now see if the source file is in our list of paths
  280. iIndex = RLIsPathInList(szSrc);
  281. if (iIndex != -1)
  282. {
  283. // Now make sure we are working with the short name
  284. // Note we may only be a subpiece of this item.
  285. // Count how many fields there are in the szSrc Now;
  286. for (i = 0, psz = szSrc; psz; i++)
  287. {
  288. psz = StrChr(psz + 1, TEXT('\\'));
  289. }
  290. lstrcpy(szSrc, (LPTSTR)DPA_FastGetPtr(g_rlpi.hdpaRLList, iIndex));
  291. // Now truncate off stuff that is not from us Go one more then we countd
  292. // above and if we have a non null value cut it off there.
  293. for (psz = szSrc; i > 0; i--)
  294. {
  295. psz = StrChr(psz+1, TEXT('\\'));
  296. }
  297. if (psz)
  298. *psz = 0;
  299. // verify that this is a fully qulified path and that it exists
  300. // before we go and muck with the registry.
  301. if (!PathIsRelative(szDest) && PathFileExistsAndAttributes(szDest, NULL) && (lstrlen(szDest) >= 3))
  302. {
  303. // Yes, so now lets reenum and try to update the paths...
  304. PathGetShortPath(szDest); // Convert to a short name...
  305. RLEnumRegistry(g_rlpi.hdpaRLList, _RLRenameCallBack, szSrc, szDest);
  306. // We changed something so mark it to be rebuilt
  307. g_rlpi.fListValid = FALSE; // Force it to rebuild.
  308. iRet = 1;
  309. }
  310. }
  311. RLLeaveCritical();
  312. return iRet;
  313. }