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.

198 lines
4.7 KiB

  1. //=======================================================================
  2. //
  3. // Copyright (c) 1998-1999 Microsoft Corporation. All Rights Reserved.
  4. //
  5. // File: util.cpp
  6. //
  7. // Purpose:
  8. //
  9. //=======================================================================
  10. #include <windows.h>
  11. #include <malloc.h>
  12. #include <v3stdlib.h>
  13. #include <tchar.h>
  14. #include <mistsafe.h>
  15. #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
  16. //---------------------------------------------------------------------
  17. // Memory management wrappers
  18. //
  19. // main difference is that they will throw an exception if there is
  20. // not enough memory available. V3_free handles NULL value
  21. //---------------------------------------------------------------------
  22. void *V3_calloc(size_t num, size_t size)
  23. {
  24. void *pRet;
  25. if (!(pRet = calloc(num, size)))
  26. {
  27. throw HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  28. }
  29. return pRet;
  30. }
  31. void V3_free(void *p)
  32. {
  33. if (p)
  34. free(p);
  35. }
  36. void *V3_malloc(size_t size)
  37. {
  38. void *pRet;
  39. if (!(pRet = malloc(size)))
  40. {
  41. throw HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  42. }
  43. return pRet;
  44. }
  45. void *V3_realloc(void *memblock, size_t size)
  46. {
  47. void *pRet;
  48. if (!(pRet = realloc(memblock, size)))
  49. {
  50. throw HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  51. }
  52. return pRet;
  53. }
  54. //-----------------------------------------------------------------------------------
  55. // GetWindowsUpdateDirectory
  56. // This function returns the location of the WindowsUpdate directory. All local
  57. // files are store in this directory. The pszPath parameter needs to be at least
  58. // MAX_PATH. The directory is created if not found
  59. //-----------------------------------------------------------------------------------
  60. BOOL GetWindowsUpdateDirectory(LPTSTR pszPath, DWORD dwBuffLen)
  61. {
  62. static TCHAR szCachePath[MAX_PATH + 1] = {_T('\0')};
  63. if (NULL == pszPath)
  64. return FALSE;
  65. if (szCachePath[0] == _T('\0'))
  66. {
  67. HKEY hkey;
  68. pszPath[0] = _T('\0');
  69. if (RegOpenKey(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion"), &hkey) == ERROR_SUCCESS)
  70. {
  71. DWORD cbPath = dwBuffLen;
  72. RegQueryValueEx(hkey, _T("ProgramFilesDir"), NULL, NULL, (LPBYTE)pszPath, &cbPath);
  73. RegCloseKey(hkey);
  74. }
  75. if (pszPath[0] == _T('\0'))
  76. {
  77. TCHAR szWinDir[MAX_PATH + 1];
  78. if (! GetWindowsDirectory(szWinDir, ARRAYSIZE(szWinDir)))
  79. {
  80. if (FAILED(StringCchCopyEx(szWinDir, ARRAYSIZE(szWinDir), _T("C"), NULL, NULL, MISTSAFE_STRING_FLAGS)))
  81. return FALSE;
  82. }
  83. pszPath[0] = szWinDir[0];
  84. pszPath[1] = _T('\0');
  85. if (FAILED(StringCchCatEx(pszPath, dwBuffLen, _T(":\\Program Files"), NULL, NULL, MISTSAFE_STRING_FLAGS)))
  86. return FALSE;
  87. }
  88. if (FAILED(StringCchCatEx(pszPath, dwBuffLen, _T("\\WindowsUpdate\\"), NULL, NULL, MISTSAFE_STRING_FLAGS)))
  89. return FALSE;
  90. V3_CreateDirectory(pszPath);
  91. //
  92. // save it in the cache
  93. //
  94. if (FAILED(StringCchCopyEx(szCachePath, ARRAYSIZE(szCachePath), pszPath, NULL, NULL, MISTSAFE_STRING_FLAGS)))
  95. {
  96. // ignore
  97. }
  98. }
  99. else
  100. {
  101. if (FAILED(StringCchCopyEx(pszPath, dwBuffLen, szCachePath, NULL, NULL, MISTSAFE_STRING_FLAGS)))
  102. return FALSE;
  103. }
  104. return TRUE;
  105. }
  106. //---------------------------------------------------------------------
  107. // V3_CreateDirectory
  108. // Creates the full path of the directory (nested directories)
  109. //---------------------------------------------------------------------
  110. BOOL V3_CreateDirectory(LPCTSTR pszDir)
  111. {
  112. BOOL bRc;
  113. TCHAR szPath[MAX_PATH + 1];
  114. //
  115. // make a local copy and remove final slash
  116. //
  117. if (FAILED(StringCchCopyEx(szPath, ARRAYSIZE(szPath), pszDir, NULL, NULL, MISTSAFE_STRING_FLAGS)))
  118. return FALSE;
  119. int iLast = lstrlen(szPath) - 1;
  120. if (szPath[iLast] == _T('\\'))
  121. szPath[iLast] = 0;
  122. //
  123. // check to see if directory already exists
  124. //
  125. DWORD dwAttr = GetFileAttributes(szPath);
  126. if (dwAttr != 0xFFFFFFFF)
  127. {
  128. if ((dwAttr & FILE_ATTRIBUTE_DIRECTORY) != 0)
  129. return TRUE;
  130. }
  131. //
  132. // create it
  133. //
  134. TCHAR* p = szPath;
  135. if (p[1] == _T(':'))
  136. p += 2;
  137. else
  138. {
  139. if (p[0] == _T('\\') && p[1] == _T('\\'))
  140. p += 2;
  141. }
  142. if (*p == _T('\\'))
  143. p++;
  144. while (p = _tcschr(p, _T('\\')))
  145. {
  146. *p = 0;
  147. bRc = CreateDirectory(szPath, NULL);
  148. *p = _T('\\');
  149. p++;
  150. if (!bRc)
  151. {
  152. if (GetLastError() != ERROR_ALREADY_EXISTS)
  153. {
  154. return FALSE;
  155. }
  156. }
  157. }
  158. bRc = CreateDirectory(szPath, NULL);
  159. if ( !bRc )
  160. {
  161. if (GetLastError() != ERROR_ALREADY_EXISTS)
  162. {
  163. return FALSE;
  164. }
  165. }
  166. return TRUE;
  167. }