Leaked source code of windows server 2003
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.

271 lines
8.2 KiB

  1. #include "pch.hxx"
  2. #include <demand.h>
  3. #include <strconst.h>
  4. #include <storfldr.h>
  5. #include <regstr.h>
  6. #include <error.h>
  7. #include <shlwapi.h>
  8. #include "shlwapip.h"
  9. #include <resource.h>
  10. #include "goptions.h"
  11. #include "optres.h"
  12. #include "storutil.h"
  13. #include "multiusr.h"
  14. INT_PTR CALLBACK StoreLocationDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  15. {
  16. LONG lRet;
  17. WORD code;
  18. char *psz, szDir[MAX_PATH];
  19. BOOL fRet = TRUE;
  20. switch (msg)
  21. {
  22. case WM_INITDIALOG:
  23. psz = (char *)lParam;
  24. Assert(psz != NULL);
  25. SetWindowLongPtr(hwnd, DWLP_USER, (LPARAM)psz);
  26. SetDlgItemText(hwnd, IDC_STORE_EDIT, psz);
  27. SetFocus(GetDlgItem(hwnd, IDOK));
  28. fRet = FALSE;
  29. break;
  30. case WM_COMMAND:
  31. psz = (char *)GetWindowLongPtr(hwnd, DWLP_USER);
  32. Assert(psz != NULL);
  33. code = HIWORD(wParam);
  34. switch(LOWORD(wParam))
  35. {
  36. case IDC_CHANGE_BTN:
  37. if (code == BN_CLICKED)
  38. {
  39. StrCpyN(szDir, psz, ARRAYSIZE(szDir));
  40. if (DoStoreFolderDlg(hwnd, szDir, ARRAYSIZE(szDir)))
  41. SetDlgItemText(hwnd, IDC_STORE_EDIT, szDir);
  42. }
  43. break;
  44. case IDOK:
  45. GetDlgItemText(hwnd, IDC_STORE_EDIT, szDir, ARRAYSIZE(szDir));
  46. // BUGBUG: Not foolproof...
  47. if (0 != lstrcmpi(szDir, psz))
  48. {
  49. int cch, cchOrig, iRet;
  50. BOOL fFound = FALSE;
  51. HANDLE hFile;
  52. WIN32_FIND_DATA fd;
  53. DWORD dwMove = 1;
  54. // Are there already ods files in the directory?
  55. cchOrig = cch = lstrlen(szDir);
  56. if (*CharPrev(szDir, szDir+cch) != _T('\\'))
  57. szDir[cch++] = _T('\\');
  58. StrCatBuff(szDir, TEXT("*.dbx"), ARRAYSIZE(szDir));
  59. hFile = FindFirstFile(szDir, &fd);
  60. if (hFile != INVALID_HANDLE_VALUE)
  61. {
  62. do
  63. {
  64. // Look for a non directory match
  65. if (!(FILE_ATTRIBUTE_DIRECTORY & fd.dwFileAttributes))
  66. fFound = TRUE;
  67. }
  68. while (!fFound && FindNextFile(hFile, &fd));
  69. FindClose(hFile);
  70. }
  71. // If we found some store files...
  72. if (fFound)
  73. {
  74. // Ask them what they want
  75. iRet = AthMessageBoxW(hwnd, MAKEINTRESOURCEW(idsAthena), MAKEINTRESOURCEW(idsMoveStoreFoundODS), NULL, MB_YESNOCANCEL | MB_ICONEXCLAMATION);
  76. if (IDCANCEL == iRet)
  77. // Bail early
  78. break;
  79. else if (IDYES == iRet)
  80. // They want us to just change the store root
  81. dwMove = 0;
  82. }
  83. // Restore dest directory name
  84. szDir[cchOrig] = 0;
  85. AthMessageBoxW(hwnd, MAKEINTRESOURCEW(idsAthena), MAKEINTRESOURCEW(idsConfirmChangeStoreLocation), NULL, MB_OK | MB_ICONINFORMATION);
  86. lRet = AthUserSetValue(NULL, c_szNewStoreDir, REG_SZ, (LPBYTE)szDir, lstrlen(szDir) + 1);
  87. if (ERROR_SUCCESS == lRet)
  88. {
  89. lRet = AthUserSetValue(NULL, c_szMoveStore, REG_DWORD, (LPBYTE)&dwMove, sizeof(dwMove));
  90. if (ERROR_SUCCESS != lRet)
  91. {
  92. AthUserDeleteValue(NULL, c_szNewStoreDir);
  93. }
  94. }
  95. if (ERROR_SUCCESS != lRet)
  96. AthMessageBoxW(hwnd, MAKEINTRESOURCEW(idsAthena), MAKEINTRESOURCEW(idsStoreMoveRegWriteFail), NULL, MB_OK | MB_ICONSTOP);
  97. }
  98. // Purposefully fall through
  99. //break;
  100. case IDCANCEL:
  101. EndDialog(hwnd, code);
  102. break;
  103. default:
  104. // We couldn't handle this
  105. fRet = TRUE;
  106. break;
  107. }
  108. break;
  109. default:
  110. fRet = FALSE;
  111. break;
  112. }
  113. return(fRet);
  114. }
  115. void DoStoreLocationDlg(HWND hwnd)
  116. {
  117. char szTemp[MAX_PATH];
  118. char szDir[MAX_PATH];
  119. if (SUCCEEDED(GetStoreRootDirectory(szTemp, ARRAYSIZE(szTemp))))
  120. {
  121. // Strip out any relative path crap
  122. PathCanonicalize(szDir, szTemp);
  123. DialogBoxParam(g_hLocRes, MAKEINTRESOURCE(iddStoreLocation), hwnd, StoreLocationDlgProc, (LPARAM)szDir);
  124. }
  125. }
  126. BOOL DoStoreFolderDlg(HWND hwnd, TCHAR *szDir, DWORD cch)
  127. {
  128. return BrowseForFolder(g_hLocRes, hwnd, szDir, cch, IDS_BROWSE_FOLDER, TRUE);
  129. }
  130. HRESULT GetDefaultStoreRoot(HWND hwnd, TCHAR *pszDir, int cch)
  131. {
  132. HRESULT hr;
  133. LPSTR psz;
  134. BOOL fIllegalCharExists = FALSE;
  135. Assert(pszDir != NULL);
  136. Assert(cch >= MAX_PATH);
  137. IF_FAILEXIT(hr = MU_GetCurrentUserDirectoryRoot(pszDir, cch));
  138. psz = pszDir;
  139. // Look for any ???. That would mean there was a bad conversion to ANSI.
  140. while (*psz)
  141. {
  142. // If we are a lead byte, then can't possibly be a ?
  143. if (IsDBCSLeadByte(*psz))
  144. {
  145. psz++;
  146. }
  147. else
  148. {
  149. if ('?' == *psz)
  150. {
  151. fIllegalCharExists = TRUE;
  152. break;
  153. }
  154. }
  155. psz++;
  156. }
  157. // If we had a bad conversion, then we need to prompt the user for a new path
  158. if (fIllegalCharExists)
  159. {
  160. if (!BrowseForFolder(g_hLocRes, hwnd, pszDir, cch, IDS_BROWSE_FOLDER, TRUE))
  161. hr = E_FAIL;
  162. }
  163. exit:
  164. return(hr);
  165. }
  166. // checks for existence of directory, if it doesn't exist
  167. // it is created
  168. HRESULT OpenDirectory(TCHAR *szDir)
  169. {
  170. TCHAR *sz, ch;
  171. HRESULT hr;
  172. Assert(szDir != NULL);
  173. hr = S_OK;
  174. if (!PathIsRoot(szDir) && !CreateDirectory(szDir, NULL) && ERROR_ALREADY_EXISTS != GetLastError())
  175. {
  176. Assert(szDir[1] == _T(':'));
  177. Assert(szDir[2] == _T('\\'));
  178. sz = &szDir[3];
  179. while (TRUE)
  180. {
  181. while (*sz != 0)
  182. {
  183. if (!IsDBCSLeadByte(*sz))
  184. {
  185. if (*sz == _T('\\'))
  186. break;
  187. }
  188. sz = CharNext(sz);
  189. }
  190. ch = *sz;
  191. *sz = 0;
  192. if (!CreateDirectory(szDir, NULL))
  193. {
  194. if (GetLastError() != ERROR_ALREADY_EXISTS)
  195. {
  196. hr = HrFromLastError();
  197. *sz = ch;
  198. break;
  199. }
  200. }
  201. *sz = ch;
  202. if (*sz == 0)
  203. break;
  204. sz++;
  205. }
  206. }
  207. return(hr);
  208. }
  209. HRESULT HrFromLastError()
  210. {
  211. HRESULT hr;
  212. switch (GetLastError())
  213. {
  214. case ERROR_NOT_ENOUGH_MEMORY:
  215. case ERROR_OUTOFMEMORY:
  216. hr = hrMemory;
  217. break;
  218. case ERROR_DISK_FULL:
  219. hr = hrDiskFull;
  220. break;
  221. default:
  222. hr = E_FAIL;
  223. break;
  224. }
  225. return(hr);
  226. }