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.

404 lines
9.6 KiB

  1. /*
  2. **++
  3. **
  4. ** Copyright (c) 2002 Microsoft Corporation
  5. **
  6. **
  7. ** Module Name:
  8. **
  9. ** utility.cpp
  10. **
  11. **
  12. ** Abstract:
  13. **
  14. ** defines functions and variable used by the Test writer
  15. **
  16. ** Author:
  17. **
  18. ** Reuven Lax [reuvenl] 04-June-2002
  19. **
  20. **
  21. **
  22. ** Revision History:
  23. **
  24. **--
  25. */
  26. ////////////////////////////////////////////////////////////////////////
  27. // Includes
  28. #include "stdafx.h"
  29. #include "utility.h"
  30. #include "writerconfig.h"
  31. #include "vs_xml.hxx"
  32. #include "msxml2.h"
  33. #include <string>
  34. #include <sstream>
  35. using std::wstring;
  36. void Utility::missingAttribute(const wchar_t* name)
  37. {
  38. wstring thrown(L"The attribute ");
  39. thrown += name;
  40. thrown += L" was omitted from the XML document";
  41. throw Utility::TestWriterException(thrown);
  42. }
  43. void Utility::missingElement(const wchar_t* name)
  44. {
  45. wstring thrown(L"The element ");
  46. thrown += name;
  47. thrown += L" was omitted from the XML document";
  48. throw Utility::TestWriterException(thrown);
  49. }
  50. void Utility::checkReturn(HRESULT returnCode, wstring function)
  51. {
  52. if (FAILED(returnCode))
  53. throw Utility::TestWriterException(returnCode, function);
  54. }
  55. void Utility::warnReturn(HRESULT returnCode, wstring function)
  56. {
  57. if (FAILED(returnCode)) {
  58. Utility::TestWriterException ex(returnCode, function);
  59. printf("%s\n", ex.what());
  60. }
  61. }
  62. void Utility::parseError(const CXMLDocument& doc)
  63. {
  64. CComBSTR reason, text;
  65. CComPtr<IXMLDOMParseError> parseError;
  66. HRESULT hr = doc.GetInterface()->get_parseError(&parseError);
  67. checkReturn(hr, L"IXMLDOMDocument::get_parseError");
  68. wstring thrown;
  69. if (parseError != NULL) {
  70. hr = parseError->get_reason(&reason);
  71. checkReturn(hr, L"IXMLDOMParseError::get_reason");
  72. hr = parseError->get_srcText(&text);
  73. checkReturn(hr, L"IXMLDOMParseError::get_srcText");
  74. thrown = L"Failed to load configuration file:\n";
  75. thrown += L" Reason: " ;
  76. thrown += reason;
  77. thrown += L"\n Source Text:\n " ;
  78. thrown += text;
  79. } else {
  80. thrown = L"Failed to load configuration file.";
  81. }
  82. throw Utility::TestWriterException(thrown);
  83. }
  84. // this function better not throw exceptions
  85. void Utility::printStatus(const wstring& status, Utility::Verbosity level)
  86. try
  87. {
  88. // if level == low, then we may be in exception-handling code. Don't dare use
  89. // the configuration object in that case
  90. WriterConfiguration* config = WriterConfiguration::instance();
  91. if (level == Utility::low || level <= config->verbosity())
  92. wprintf(L"%s\n", status.c_str());
  93. }
  94. catch(const std::exception&)
  95. {
  96. wprintf(L"Internal Error: an unexpected error happened in printStatus\n");
  97. wprintf(L"We were trying to print the following message: %s\n", status.c_str());
  98. }
  99. void Utility::printStatus(const std::string& status, Verbosity level)
  100. try
  101. {
  102. // if level == low, then we may be in exception-handling code. Don't dare use
  103. // the configuration object in that case
  104. WriterConfiguration* config = WriterConfiguration::instance();
  105. if (level == Utility::low || level <= config->verbosity())
  106. printf("%s\n", status.c_str());
  107. }
  108. catch(const std::exception& exception)
  109. {
  110. printf("Internal Error: an unexpected error happened in printStatus\n");
  111. printf("the error is the following\n\t%s\n", exception.what());
  112. printf("We were trying to print the following message: %s\n", status.c_str());
  113. }
  114. bool Utility::toBoolean(const wchar_t* name)
  115. {
  116. assert(!wcscmp(name, L"yes") || !wcscmp(name, L"no"));
  117. return (wcscmp(name, L"yes") == 0) ? true : false;
  118. }
  119. VSS_USAGE_TYPE Utility::toUsage(const wchar_t* name)
  120. {
  121. if (wcscmp(name, L"BOOTABLE_SYSTEM_STATE") == 0)
  122. return VSS_UT_BOOTABLESYSTEMSTATE;
  123. else if (wcscmp(name, L"SYSTEM_SERVICE") == 0)
  124. return VSS_UT_SYSTEMSERVICE;
  125. else if (wcscmp(name, L"USER_DATA") == 0)
  126. return VSS_UT_USERDATA;
  127. else if (wcscmp(name, L"OTHER") == 0)
  128. return VSS_UT_OTHER;
  129. else
  130. assert(false);
  131. return VSS_UT_UNDEFINED;
  132. }
  133. VSS_RESTOREMETHOD_ENUM Utility::toMethod(const wchar_t* name)
  134. {
  135. if (wcscmp(name, L"RESTORE_IF_NONE_THERE") == 0)
  136. return VSS_RME_RESTORE_IF_NOT_THERE;
  137. else if (wcscmp(name, L"RESTORE_IF_CAN_BE_REPLACED") == 0)
  138. return VSS_RME_RESTORE_IF_CAN_REPLACE;
  139. else if (wcscmp(name, L"STOP_RESTART_SERVICE") == 0)
  140. return VSS_RME_STOP_RESTORE_START;
  141. else if (wcscmp(name, L"REPLACE_AT_REBOOT") == 0)
  142. return VSS_RME_RESTORE_AT_REBOOT;
  143. else if (wcscmp(name, L"REPLACE_AT_REBOOT_IF_CANNOT_REPLACE") == 0)
  144. return VSS_RME_RESTORE_AT_REBOOT_IF_CANNOT_REPLACE;
  145. else if (wcscmp(name, L"RESTORE_TO_ALTERNATE_LOCATION") == 0)
  146. return VSS_RME_RESTORE_TO_ALTERNATE_LOCATION;
  147. else if (wcscmp(name, L"CUSTOM") == 0)
  148. return VSS_RME_CUSTOM;
  149. else
  150. assert(false);
  151. return VSS_RME_RESTORE_AT_REBOOT;
  152. }
  153. VSS_WRITERRESTORE_ENUM Utility::toWriterRestore(const wchar_t* name)
  154. {
  155. if (wcscmp(name, L"always") == 0)
  156. return VSS_WRE_ALWAYS;
  157. else if (wcscmp(name, L"never") == 0)
  158. return VSS_WRE_NEVER;
  159. else if (wcscmp(name, L"ifReplaceFails") == 0)
  160. return VSS_WRE_IF_REPLACE_FAILS;
  161. else
  162. assert(false);
  163. return VSS_WRE_UNDEFINED;
  164. }
  165. VSS_COMPONENT_TYPE Utility::toComponentType(const wchar_t* name)
  166. {
  167. if (wcscmp(name, L"database") == 0)
  168. return VSS_CT_DATABASE;
  169. else if (wcscmp(name, L"filegroup") == 0)
  170. return VSS_CT_FILEGROUP;
  171. else
  172. assert(false);
  173. return VSS_CT_UNDEFINED;
  174. }
  175. VSS_RESTORE_TARGET Utility::toRestoreTarget(const wchar_t* name)
  176. {
  177. if (wcscmp(name, L"VSS_RT_ORIGINAL") == 0)
  178. return VSS_RT_ORIGINAL;
  179. else if (wcscmp(name, L"VSS_RT_ALTERNATE") == 0)
  180. return VSS_RT_ALTERNATE;
  181. else
  182. assert(false);
  183. return VSS_RT_UNDEFINED;
  184. }
  185. Utility::Events Utility::toWriterEvent(const wchar_t* name)
  186. {
  187. if (wcscmp(name, L"Identify") == 0)
  188. return Identify;
  189. else if (wcscmp(name, L"PrepareForBackup") == 0)
  190. return PrepareForBackup;
  191. else if (wcscmp(name, L"PrepareForSnapshot") == 0)
  192. return PrepareForSnapshot;
  193. else if (wcscmp(name, L"Freeze") == 0)
  194. return Freeze;
  195. else if (wcscmp(name, L"Thaw") == 0)
  196. return Thaw;
  197. else if (wcscmp(name, L"PostSnapshot") == 0)
  198. return PostSnapshot;
  199. else if (wcscmp(name, L"Abort") == 0)
  200. return Abort;
  201. else if (wcscmp(name, L"BackupComplete") == 0)
  202. return BackupComplete;
  203. else if (wcscmp(name, L"BackupShutdown") == 0)
  204. return BackupShutdown;
  205. else if (wcscmp(name, L"PreRestore") == 0)
  206. return PreRestore;
  207. else if (wcscmp(name, L"PostRestore") == 0)
  208. return PostRestore;
  209. else
  210. assert(false);
  211. return Identify;
  212. }
  213. Utility::Verbosity Utility::toVerbosity(const wchar_t* name)
  214. {
  215. if (wcscmp(name, L"low") == 0)
  216. return low;
  217. else if (wcscmp(name, L"medium") == 0)
  218. return medium;
  219. else if (wcscmp(name, L"high") == 0)
  220. return high;
  221. else
  222. assert(false);
  223. return low;
  224. }
  225. long Utility::toLong(const wchar_t* name)
  226. {
  227. wchar_t* stopPointer = NULL;
  228. long number = wcstol(name, &stopPointer, 10);
  229. assert(stopPointer > name);
  230. return number;
  231. }
  232. wstring Utility::toString(VSS_USAGE_TYPE usage)
  233. {
  234. switch(usage) {
  235. case VSS_UT_BOOTABLESYSTEMSTATE:
  236. return L"BOOTABLE_SYSTEM_STATE";
  237. case VSS_UT_SYSTEMSERVICE:
  238. return L"SYSTEM_SERVICE";
  239. case VSS_UT_USERDATA:
  240. return L"USER_DATA";
  241. case VSS_UT_OTHER:
  242. return L"OTHER";
  243. default:
  244. assert(false);
  245. }
  246. return L"";
  247. }
  248. wstring Utility::toString(VSS_RESTOREMETHOD_ENUM method)
  249. {
  250. switch (method) {
  251. case VSS_RME_RESTORE_IF_NOT_THERE:
  252. return L"RESTORE_IF_NONE_THERE";
  253. case VSS_RME_RESTORE_IF_CAN_REPLACE:
  254. return L"RESTORE_IF_CAN_BE_REPLACED";
  255. case VSS_RME_STOP_RESTORE_START:
  256. return L"STOP_RESTART_SERVICE";
  257. case VSS_RME_RESTORE_AT_REBOOT:
  258. return L"REPLACE_AT_REBOOT";
  259. case VSS_RME_RESTORE_AT_REBOOT_IF_CANNOT_REPLACE:
  260. return L"REPLACE_AT_REBOOT_IF_CANNOT_REPLACE";
  261. case VSS_RME_RESTORE_TO_ALTERNATE_LOCATION:
  262. return L"RESTORE_TO_ALTERNATE_LOCATION";
  263. case VSS_RME_CUSTOM:
  264. return L"CUSTOM";
  265. default:
  266. assert(false);
  267. }
  268. return L"";
  269. }
  270. wstring Utility::toString(VSS_WRITERRESTORE_ENUM writerRestore)
  271. {
  272. switch (writerRestore) {
  273. case VSS_WRE_ALWAYS:
  274. return L"always";
  275. case VSS_WRE_NEVER:
  276. return L"never";
  277. case VSS_WRE_IF_REPLACE_FAILS:
  278. return L"ifReplaceFails";
  279. default:
  280. assert(false);
  281. }
  282. return L"";
  283. }
  284. wstring Utility::toString(VSS_COMPONENT_TYPE type)
  285. {
  286. switch (type) {
  287. case VSS_CT_DATABASE:
  288. return L"database";
  289. case VSS_CT_FILEGROUP:
  290. return L"filegroup";
  291. default:
  292. assert(false);
  293. }
  294. return L"";
  295. }
  296. wstring Utility::toString(VSS_RESTORE_TARGET target)
  297. {
  298. switch(target) {
  299. case VSS_RT_ORIGINAL:
  300. return L"VSS_RT_ORIGINAL";
  301. case VSS_RT_ALTERNATE:
  302. return L"VSS_RT_ALTERNATE";
  303. case VSS_RT_DIRECTED:
  304. return L"VSS_RT_DIRECTED";
  305. default:
  306. assert(false);
  307. }
  308. return L"";
  309. }
  310. wstring Utility::toString(Events event)
  311. {
  312. switch (event) {
  313. case Identify:
  314. return L"Identify";
  315. case PrepareForBackup:
  316. return L"PrepareForBackup";
  317. case PrepareForSnapshot:
  318. return L"PrepareForSnapshot";
  319. case Freeze:
  320. return L"Freeze";
  321. case Thaw:
  322. return L"Thaw";
  323. case PostSnapshot:
  324. return L"PostSnapshot";
  325. case Abort:
  326. return L"Abort";
  327. case BackupComplete:
  328. return L"BackupComplete";
  329. case BackupShutdown:
  330. return L"BackupShutdown";
  331. case PreRestore:
  332. return L"PreRestore";
  333. case PostRestore:
  334. return L"PostRestore";
  335. default:
  336. assert(false);
  337. }
  338. return L"";
  339. }
  340. wstring Utility::toString(Verbosity verbosity)
  341. {
  342. switch(verbosity) {
  343. case low:
  344. return L"low";
  345. case medium:
  346. return L"medium";
  347. case high:
  348. return L"high";
  349. default:
  350. assert(false);
  351. }
  352. return L"";
  353. }
  354. wstring Utility::toString(bool value)
  355. {
  356. return (value) ? L"yes" : L"no";
  357. }