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.

605 lines
14 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Abstract:
  4. @doc
  5. @module vssadmin.hxx | header of VSS demo
  6. @end
  7. Author:
  8. Adi Oltean [aoltean] 09/17/1999
  9. TBD:
  10. Add comments.
  11. Revision History:
  12. Name Date Comments
  13. aoltean 09/17/1999 Created
  14. --*/
  15. #ifndef __VSS_DEMO_H_
  16. #define __VSS_DEMO_H_
  17. /////////////////////////////////////////////////////////////////////////////
  18. // Defines and pragmas
  19. // C4290: C++ Exception Specification ignored
  20. #pragma warning(disable:4290)
  21. // warning C4511: copy constructor could not be generated
  22. #pragma warning(disable:4511)
  23. // warning C4127: conditional expression is constant
  24. #pragma warning(disable:4127)
  25. /////////////////////////////////////////////////////////////////////////////
  26. // Includes
  27. #include <wtypes.h>
  28. #include <stddef.h>
  29. #include <oleauto.h>
  30. #include <comadmin.h>
  31. // Enabling asserts in ATL and VSS
  32. #include "vs_assert.hxx"
  33. // ATL
  34. #include <atlconv.h>
  35. #include <atlbase.h>
  36. // Application specific
  37. #include "vs_inc.hxx"
  38. // Generated MIDL headers
  39. #include "vs_idl.hxx"
  40. #include "copy.hxx"
  41. #include "pointer.hxx"
  42. #include "resource.h"
  43. #include "vssmsg.h"
  44. #include "msg.h"
  45. #include "vswriter.h"
  46. #include "vsbackup.h"
  47. /////////////////////////////////////////////////////////////////////////////
  48. // Constants
  49. const x_nStringBufferSize = 1024; // Includes the zero character
  50. const x_nPollingInterval = 2500; // Three seconds
  51. const x_nMaxRetriesCount = 4; // Retries for polling
  52. const WCHAR x_wszVssOptBoolTrue[] = L"TRUE";
  53. #define VSSADM_E_NO_ITEMS_IN_QUERY S_FALSE
  54. #define VSSADM_E_FIRST_PARSING_ERROR 0x1001
  55. #define VSSADM_E_INVALID_NUMBER 0x1001
  56. #define VSSADM_E_INVALID_COMMAND 0x1002
  57. #define VSSADM_E_INVALID_OPTION 0x1003
  58. #define VSSADM_E_INVALID_OPTION_VALUE 0x1004
  59. #define VSSADM_E_DUPLICATE_OPTION 0x1005
  60. #define VSSADM_E_OPTION_NOT_ALLOWED_FOR_COMMAND 0x1006
  61. #define VSSADM_E_REQUIRED_OPTION_MISSING 0x1007
  62. #define VSSADM_E_INVALID_SET_OF_OPTIONS 0x1008
  63. #define VSSADM_E_SNAPSHOT_NOT_FOUND 0x1009
  64. #define VSSADM_E_DELETION_DENIED 0x100a
  65. #define VSSADM_E_LAST_PARSING_ERROR 0x100a
  66. // Note: if any skus are added in the CVssSKU class, they need to
  67. // be updated here. Make sure to update the SKU_INT and SKU_A
  68. // definitions as well.
  69. #define SKU_C CVssSKU::VSS_SKU_CLIENT
  70. #define SKU_S CVssSKU::VSS_SKU_SERVER
  71. #define SKU_N CVssSKU::VSS_SKU_NAS
  72. #define SKU_I CVssSKU::VSS_SKU_INVALID
  73. #define SKU_INT ((DWORD)(~SKU_C & ~ SKU_S & ~SKU_N))
  74. #define SKU_A ( SKU_C | SKU_S | SKU_N | SKU_INT) // 0xffff
  75. #define SKU_SN ( SKU_S | SKU_N )
  76. #define SKU_SNI (SKU_S | SKU_N | SKU_INT)
  77. enum EVssAdmSnapshotType
  78. {
  79. VSSADM_ST_FIRST = 0,
  80. VSSADM_ST_NAS_ROLLBACK = 0,
  81. VSSADM_ST_PERSISTENT_TIMEWARP,
  82. VSSADM_ST_TIMEWARP,
  83. VSSADM_ST_NUM_TYPES,
  84. VSSADM_ST_INVALID,
  85. VSSADM_ST_ALL
  86. };
  87. struct SVssAdmSnapshotTypeName
  88. {
  89. LPCWSTR pwszName;
  90. DWORD dwSKUs; // Specifies which SKUs this type is supported for snapshot creation using vssadmin, formed from ORing CVssSKU::EVssSKUType
  91. LONG lSnapshotContext; // The snapshot context from vss.idl
  92. LONG pwszDescription;
  93. };
  94. //
  95. // List of all options. This list must remain in sync with the g_asAdmOptions list.
  96. //
  97. enum EVssAdmOption
  98. {
  99. VSSADM_O_FIRST = 0,
  100. VSSADM_O_ALL = 0,
  101. VSSADM_O_AUTORETRY,
  102. VSSADM_O_EXPOSE_USING,
  103. VSSADM_O_FOR,
  104. VSSADM_O_MAXSIZE,
  105. VSSADM_O_OLDEST,
  106. VSSADM_O_ON,
  107. VSSADM_O_PROVIDER,
  108. VSSADM_O_QUIET,
  109. VSSADM_O_SET,
  110. VSSADM_O_SHAREPATH,
  111. VSSADM_O_SNAPSHOT,
  112. VSSADM_O_SNAPTYPE,
  113. VSSADM_O_NUM_OPTIONS,
  114. VSSADM_O_INVALID
  115. };
  116. //
  117. // LIst of all commands. This list must remain in sync with the g_asAdmCommands list.
  118. //
  119. enum EVssAdmCommand
  120. {
  121. VSSADM_C_FIRST = 0,
  122. VSSADM_C_ADD_DIFFAREA_INT = 0,
  123. VSSADM_C_ADD_DIFFAREA_PUB,
  124. VSSADM_C_CREATE_SNAPSHOT_INT,
  125. VSSADM_C_CREATE_SNAPSHOT_PUB,
  126. VSSADM_C_DELETE_SNAPSHOTS_INT,
  127. VSSADM_C_DELETE_SNAPSHOTS_PUB,
  128. VSSADM_C_DELETE_DIFFAREAS_INT,
  129. VSSADM_C_DELETE_DIFFAREAS_PUB,
  130. VSSADM_C_EXPOSE_SNAPSHOT,
  131. VSSADM_C_LIST_PROVIDERS,
  132. VSSADM_C_LIST_SNAPSHOTS_INT,
  133. VSSADM_C_LIST_SNAPSHOTS_PUB,
  134. VSSADM_C_LIST_DIFFAREAS_INT,
  135. VSSADM_C_LIST_DIFFAREAS_PUB,
  136. VSSADM_C_LIST_VOLUMES_INT,
  137. VSSADM_C_LIST_VOLUMES_PUB,
  138. VSSADM_C_LIST_WRITERS,
  139. VSSADM_C_RESIZE_DIFFAREA_INT,
  140. VSSADM_C_RESIZE_DIFFAREA_PUB,
  141. VSSADM_C_NUM_COMMANDS,
  142. VSSADM_C_INVALID
  143. };
  144. enum EVssAdmOptionType
  145. {
  146. VSSADM_OT_BOOL = 0, // no qualifier on the option, i.e. /quiet, TRUE if present
  147. VSSADM_OT_STR,
  148. VSSADM_OT_NUM
  149. };
  150. struct SVssAdmOption
  151. {
  152. EVssAdmOption eOpt;
  153. LPCWSTR pwszOptName; // The option name as typed on the command-line, i.e. the "for" in /for=XXXX
  154. EVssAdmOptionType eOptType;
  155. };
  156. //
  157. // Specifies the validity of the option for a particular command.
  158. //
  159. enum EVssAdmOptionFlag
  160. {
  161. V_NO = 0, // Option not allowed
  162. V_YES, // Option manditory
  163. V_OPT // Option optional
  164. };
  165. //
  166. // The main command structure. The commands are structured like:
  167. // vssadmin <pwszMajorOption> <pwszMinorOption> <OPTIONS>
  168. //
  169. struct SVssAdmCommandsEntry
  170. {
  171. LPCWSTR pwszMajorOption;
  172. LPCWSTR pwszMinorOption;
  173. EVssAdmCommand eAdmCmd;
  174. DWORD dwSKUs; // Specifies which SKUs this command is supported, formed from ORing CVssSKU::EVssSKUType
  175. LONG lMsgGen;
  176. LONG lMsgDetail;
  177. BOOL bShowSSTypes; // If true, in detailed usage show a list of valid snapshot types at end of message
  178. EVssAdmOptionFlag aeOptionFlags[VSSADM_O_NUM_OPTIONS]; // Array of option flags indexed by EVssAdmOption
  179. };
  180. //
  181. // The structure of the parsed command. One of these is created by the
  182. // ParseCmdLine method.
  183. //
  184. struct SVssAdmParsedCommand
  185. {
  186. EVssAdmCommand eAdmCmd;
  187. LPWSTR apwszOptionValues[VSSADM_O_NUM_OPTIONS];
  188. // Simple initializer constructor
  189. SVssAdmParsedCommand()
  190. {
  191. eAdmCmd = VSSADM_C_INVALID;
  192. // psUnnamedOptions = NULL;
  193. // Clear out the option values arrays
  194. for ( INT i = 0; i < VSSADM_O_NUM_OPTIONS; ++i )
  195. apwszOptionValues[ i ] = NULL;
  196. };
  197. ~SVssAdmParsedCommand()
  198. {
  199. // Free any allocated memory
  200. for ( INT i = 0; i < VSSADM_O_NUM_OPTIONS; ++i )
  201. ::VssFreeString( apwszOptionValues[ i ] );
  202. }
  203. };
  204. extern const SVssAdmOption g_asAdmOptions[];
  205. extern const SVssAdmCommandsEntry g_asAdmCommands[];
  206. extern const SVssAdmSnapshotTypeName g_asAdmTypeNames[];
  207. LPWSTR GuidToString(
  208. IN GUID guid
  209. );
  210. LPWSTR LonglongToString(
  211. IN LONGLONG llValue
  212. );
  213. LPWSTR DateTimeToString(
  214. IN VSS_TIMESTAMP *pTimeStamp
  215. );
  216. WCHAR MyGetChar(
  217. );
  218. BOOL MapVssErrorToMsg(
  219. IN HRESULT hr,
  220. OUT LONG *plMsgNum
  221. ) throw( HRESULT );
  222. /////////////////////////////////////////////////////////////////////////////
  223. // class CVssAdminCLI
  224. class CCommandVerifier;
  225. class CVssAdminCLI
  226. {
  227. // Enums and typedefs
  228. private:
  229. enum _RETURN_VALUE
  230. {
  231. VSS_CMDRET_SUCCESS = 0,
  232. VSS_CMDRET_EMPTY_RESULT = 1,
  233. VSS_CMDRET_ERROR = 2,
  234. };
  235. // Constructors& destructors
  236. private:
  237. CVssAdminCLI(const CVssAdminCLI&);
  238. CVssAdminCLI();
  239. public:
  240. CVssAdminCLI(
  241. IN INT argc,
  242. IN PWSTR argv[]
  243. );
  244. ~CVssAdminCLI();
  245. // Attributes
  246. private:
  247. BOOL IsQuiet() { return GetOptionValueBool( VSSADM_O_QUIET ); }
  248. INT GetReturnValue() { return m_nReturnValue; };
  249. LPWSTR GetOptionValueStr(
  250. IN EVssAdmOption eOption
  251. )
  252. {
  253. CVssFunctionTracer ft( VSSDBG_VSSADMIN, L"CVssAdminCLI::GetOptionValueStr" );
  254. BS_ASSERT( g_asAdmOptions[ eOption ].eOptType == VSSADM_OT_STR );
  255. // BS_ASSERT( g_asAdmCommands[ m_sParsedCommand.eAdmCmd].aeOptionFlags[ eOption ] != V_NO );
  256. return m_sParsedCommand.apwszOptionValues[ eOption ];
  257. };
  258. BOOL GetOptionValueBool(
  259. IN EVssAdmOption eOption
  260. )
  261. {
  262. CVssFunctionTracer ft( VSSDBG_VSSADMIN, L"CVssAdminCLI::GetOptionValueBool" );
  263. BS_ASSERT( g_asAdmOptions[ eOption ].eOptType == VSSADM_OT_BOOL );
  264. BS_ASSERT( g_asAdmCommands[ m_sParsedCommand.eAdmCmd].aeOptionFlags[ eOption ] != V_NO );
  265. return m_sParsedCommand.apwszOptionValues[ eOption ] != NULL;
  266. };
  267. BOOL GetOptionValueNum(
  268. IN EVssAdmOption eOption,
  269. OUT LONGLONG *pllValue,
  270. IN BOOL bSuffixAllowed = TRUE
  271. ) throw( HRESULT )
  272. {
  273. CVssFunctionTracer ft( VSSDBG_VSSADMIN, L"CVssAdminCLI::GetOptionValueNum" );
  274. BS_ASSERT( g_asAdmOptions[ eOption ].eOptType == VSSADM_OT_NUM );
  275. BS_ASSERT( g_asAdmCommands[ m_sParsedCommand.eAdmCmd].aeOptionFlags[ eOption ] != V_NO );
  276. if ( m_sParsedCommand.apwszOptionValues[ eOption ] == NULL )
  277. {
  278. BS_ASSERT( g_asAdmCommands[ m_sParsedCommand.eAdmCmd].aeOptionFlags[ eOption ] == V_OPT );
  279. // Option wasn't specified on command line - an optional one
  280. *pllValue = 0;
  281. return FALSE;
  282. }
  283. *pllValue = ScanNumber( m_sParsedCommand.apwszOptionValues[ eOption ], bSuffixAllowed );
  284. return TRUE;
  285. };
  286. // Operations
  287. public:
  288. static HRESULT Main(
  289. IN INT argc,
  290. IN PWSTR argv[]
  291. );
  292. private:
  293. void Initialize(
  294. ) throw(HRESULT);
  295. BOOL ParseCmdLine(
  296. ) throw(HRESULT);
  297. void DoProcessing(
  298. ) throw(HRESULT);
  299. void Finalize();
  300. // Processing
  301. private:
  302. void PrintUsage(
  303. ) throw(HRESULT);
  304. // The following are the methods that get called for each command.
  305. void AddDiffArea(
  306. ) throw(HRESULT);
  307. void CreateSnapshot(
  308. ) throw(HRESULT);
  309. void DeleteDiffAreas(
  310. ) throw(HRESULT);
  311. void DeleteSnapshots(
  312. ) throw(HRESULT);
  313. void ExposeSnapshot(
  314. ) throw(HRESULT);
  315. void ListDiffAreas(
  316. ) throw(HRESULT);
  317. void ListProviders(
  318. ) throw(HRESULT);
  319. void ListSnapshots(
  320. ) throw(HRESULT);
  321. void ListVolumes(
  322. ) throw(HRESULT);
  323. void ListWriters(
  324. ) throw(HRESULT);
  325. void ResizeDiffArea(
  326. ) throw(HRESULT);
  327. // Implementation
  328. private:
  329. static BOOL UnloggableError(IN HRESULT hError);
  330. void GetDifferentialSoftwareSnapshotMgmtInterface(
  331. IN VSS_ID ProviderId,
  332. IN IVssSnapshotMgmt *pIMgmt,
  333. OUT IUnknown** ppItf
  334. );
  335. LPCWSTR GetVolumeDisplayName(
  336. IN LPCWSTR pwszVolumeName
  337. );
  338. LONG DetermineSnapshotType(
  339. IN LPCWSTR pwszType
  340. ) throw(HRESULT);
  341. LPWSTR DetermineSnapshotType(
  342. IN LONG lSnapshotAttributes
  343. ) throw(HRESULT);
  344. void DisplayDiffAreasPrivate(
  345. IVssEnumMgmtObject *pIEnumMgmt
  346. ) throw(HRESULT);
  347. LPWSTR BuildSnapshotAttributeDisplayString(
  348. IN DWORD Attr
  349. ) throw(HRESULT);
  350. void DumpSnapshotTypes(
  351. ) throw(HRESULT);
  352. LPCWSTR LoadString(
  353. IN UINT nStringId
  354. ) throw(HRESULT);
  355. LPCWSTR GetNextCmdlineToken(
  356. IN bool bFirstToken = false
  357. ) throw(HRESULT);
  358. bool Match(
  359. IN LPCWSTR wszString,
  360. IN LPCWSTR wszPatternString
  361. ) throw(HRESULT);
  362. bool ScanGuid(
  363. IN LPCWSTR wszString,
  364. OUT VSS_ID& Guid
  365. ) throw(HRESULT);
  366. void Output(
  367. IN LPCWSTR wszFormat,
  368. ...
  369. ) throw(HRESULT);
  370. void OutputMsg(
  371. IN LONG msgId,
  372. ...
  373. ) throw(HRESULT);
  374. void OutputOnConsole(
  375. IN LPCWSTR wszStr
  376. );
  377. LPWSTR GetMsg(
  378. IN BOOL bLineBreaks,
  379. IN LONG msgId,
  380. ...
  381. );
  382. void AppendMessageToStr(
  383. IN LPWSTR pwszString,
  384. IN SIZE_T cMaxStrLen,
  385. IN LONG lMsgId,
  386. IN DWORD AttrBit,
  387. IN LPCWSTR pwszDelimitStr
  388. ) throw( HRESULT );
  389. LONGLONG ScanNumber(
  390. IN LPCWSTR pwszNumToConvert,
  391. IN BOOL bSuffixAllowed
  392. ) throw( HRESULT );
  393. LPWSTR FormatNumber(
  394. IN LONGLONG llNum
  395. ) throw(HRESULT);
  396. void OutputErrorMsg(
  397. IN LONG msgId,
  398. ...
  399. ) throw(HRESULT);
  400. BOOL PromptUserForConfirmation(
  401. IN LONG lPromptMsgId,
  402. IN ULONG ulNum
  403. );
  404. void GetProviderId(
  405. OUT VSS_ID *pProviderId
  406. );
  407. LPCWSTR GetProviderName(
  408. IN VSS_ID& ProviderId
  409. ) throw(HRESULT);
  410. BOOL GetProviderIdByName(
  411. IN LPCWSTR pwszProviderName,
  412. OUT VSS_ID *pProviderId
  413. ) throw(HRESULT);
  414. // Data members
  415. private:
  416. CCommandVerifier* m_pVerifier;
  417. HANDLE m_hConsoleOutput;
  418. CVssSimpleMap<UINT, LPCWSTR> m_mapCachedResourceStrings;
  419. CVssSimpleMap<VSS_ID, LPCWSTR> m_mapCachedProviderNames;
  420. CVssSimpleMap<LPCWSTR, LPCWSTR> *m_pMapVolumeNames;
  421. INT m_nReturnValue;
  422. INT m_argc;
  423. PWSTR *m_argv;
  424. EVssAdmCommand m_eCommandType;
  425. SVssAdmParsedCommand m_sParsedCommand;
  426. VSS_OBJECT_TYPE m_eFilterObjectType;
  427. VSS_OBJECT_TYPE m_eListedObjectType;
  428. VSS_ID m_FilterSnapshotId;
  429. };
  430. class CVssAutoSnapshotProperties
  431. {
  432. // Constructors/destructors
  433. private:
  434. CVssAutoSnapshotProperties(const CVssAutoSnapshotProperties&);
  435. public:
  436. CVssAutoSnapshotProperties(VSS_SNAPSHOT_PROP &Snap): m_pSnap(&Snap) {};
  437. CVssAutoSnapshotProperties(VSS_OBJECT_PROP &Prop): m_pSnap(&Prop.Obj.Snap) {};
  438. // Automatically closes the handle
  439. ~CVssAutoSnapshotProperties() {
  440. Clear();
  441. };
  442. // Operations
  443. public:
  444. // Returns the value
  445. VSS_SNAPSHOT_PROP *GetPtr() {
  446. return m_pSnap;
  447. }
  448. // NULLs out the pointer. Used after a pointer has been transferred to another
  449. // funtion.
  450. void Transferred() {
  451. m_pSnap = NULL;
  452. }
  453. // Clears the contents of the auto string
  454. void Clear() {
  455. if ( m_pSnap != NULL )
  456. {
  457. ::VssFreeSnapshotProperties(m_pSnap);
  458. m_pSnap = NULL;
  459. }
  460. }
  461. // Returns the value to the actual pointer
  462. VSS_SNAPSHOT_PROP* operator->() const {
  463. return m_pSnap;
  464. }
  465. // Returns the value of the actual pointer
  466. operator VSS_SNAPSHOT_PROP* () const {
  467. return m_pSnap;
  468. }
  469. private:
  470. VSS_SNAPSHOT_PROP *m_pSnap;
  471. };
  472. #endif //__VSS_DEMO_H_