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.

651 lines
12 KiB

  1. /*++
  2. 1998 Seagate Software, Inc. All rights reserved.
  3. Module Name:
  4. hsmpolcy.cpp
  5. Abstract:
  6. This component represents a job's policy.
  7. Author:
  8. Chuck Bardeen [cbardeen] 29-Oct-1996
  9. Revision History:
  10. --*/
  11. #include "stdafx.h"
  12. #include "wsb.h"
  13. #include "job.h"
  14. #include "hsmpolcy.h"
  15. #define WSB_TRACE_IS WSB_TRACE_BIT_JOB
  16. HRESULT
  17. CHsmPolicy::CompareTo(
  18. IN IUnknown* pUnknown,
  19. OUT SHORT* pResult
  20. )
  21. /*++
  22. Implements:
  23. IWsbCollectable::CompareTo().
  24. --*/
  25. {
  26. HRESULT hr = S_OK;
  27. CComPtr<IHsmPolicy> pPolicy;
  28. WsbTraceIn(OLESTR("CHsmPolicy::CompareTo"), OLESTR(""));
  29. try {
  30. // Did they give us a valid item to compare to?
  31. WsbAssert(0 != pUnknown, E_POINTER);
  32. // We need the IWsbBool interface to get the value of the object.
  33. WsbAffirmHr(pUnknown->QueryInterface(IID_IHsmPolicy, (void**) &pPolicy));
  34. // Compare the rules.
  35. hr = CompareToIPolicy(pPolicy, pResult);
  36. } WsbCatch(hr);
  37. WsbTraceOut(OLESTR("CHsmPolicy::CompareTo"), OLESTR("hr = <%ls>, result = <%ls>"), WsbHrAsString(hr), WsbPtrToShortAsString(pResult));
  38. return(hr);
  39. }
  40. HRESULT
  41. CHsmPolicy::CompareToIPolicy(
  42. IN IHsmPolicy* pPolicy,
  43. OUT SHORT* pResult
  44. )
  45. /*++
  46. Implements:
  47. IHsmPolicy::CompareToIPolicy().
  48. --*/
  49. {
  50. HRESULT hr = S_OK;
  51. GUID id;
  52. WsbTraceIn(OLESTR("CHsmPolicy::CompareToIPolicy"), OLESTR(""));
  53. try {
  54. // Did they give us a valid item to compare to?
  55. WsbAssert(0 != pPolicy, E_POINTER);
  56. // Get the path and name.
  57. WsbAffirmHr(pPolicy->GetIdentifier(&id));
  58. // Compare to the path and name.
  59. hr = CompareToIdentifier(id, pResult);
  60. } WsbCatch(hr);
  61. WsbTraceOut(OLESTR("CHsmPolicy::CompareToIPolicy"), OLESTR("hr = <%ls>, result = <%ls>"), WsbHrAsString(hr), WsbPtrToShortAsString(pResult));
  62. return(hr);
  63. }
  64. HRESULT
  65. CHsmPolicy::CompareToIdentifier(
  66. IN GUID id,
  67. OUT SHORT* pResult
  68. )
  69. /*++
  70. Implements:
  71. IHsmPolicy::CompareToIdentifier().
  72. --*/
  73. {
  74. HRESULT hr = S_OK;
  75. SHORT aResult = 0;
  76. WsbTraceIn(OLESTR("CHsmPolicy::CompareToIdentifier"), OLESTR("id = <%ls>"), WsbGuidAsString(id));
  77. try {
  78. // Compare the guids.
  79. aResult = WsbSign( memcmp(&m_id, &id, sizeof(GUID)) );
  80. if (0 != aResult) {
  81. hr = S_FALSE;
  82. }
  83. if (0 != pResult) {
  84. *pResult = aResult;
  85. }
  86. } WsbCatch(hr);
  87. WsbTraceOut(OLESTR("CHsmPolicy::CompareToIdentifier"), OLESTR("hr = <%ls>, result = <%d>"), WsbHrAsString(hr), aResult);
  88. return(hr);
  89. }
  90. HRESULT
  91. CHsmPolicy::EnumRules(
  92. OUT IWsbEnum** ppEnum
  93. )
  94. /*++
  95. Implements:
  96. IHsmPolicy::EnumRules().
  97. --*/
  98. {
  99. HRESULT hr = S_OK;
  100. try {
  101. WsbAssert(0 != ppEnum, E_POINTER);
  102. WsbAffirmHr(m_pRules->Enum(ppEnum));
  103. } WsbCatch(hr);
  104. return(hr);
  105. }
  106. HRESULT
  107. CHsmPolicy::FinalConstruct(
  108. void
  109. )
  110. /*++
  111. Implements:
  112. CComObjectRoot::FinalConstruct().
  113. --*/
  114. {
  115. HRESULT hr = S_OK;
  116. try {
  117. WsbAffirmHr(CWsbObject::FinalConstruct());
  118. m_scale = 1000;
  119. m_usesDefaultRules = FALSE;
  120. //Create the criteria collection.
  121. WsbAffirmHr(CoCreateInstance(CLSID_CWsbOrderedCollection, NULL, CLSCTX_ALL, IID_IWsbCollection, (void**) &m_pRules));
  122. // Each instance should have its own unique identifier.
  123. WsbAffirmHr(CoCreateGuid(&m_id));
  124. } WsbCatch(hr);
  125. return(hr);
  126. }
  127. HRESULT
  128. CHsmPolicy::GetAction(
  129. OUT IHsmAction** ppAction
  130. )
  131. /*++
  132. Implements:
  133. IHsmPolicy::GetAction().
  134. --*/
  135. {
  136. HRESULT hr = S_OK;
  137. try {
  138. WsbAssert(0 != ppAction, E_POINTER);
  139. *ppAction = m_pAction;
  140. m_pAction.p->AddRef();
  141. } WsbCatch(hr);
  142. return(hr);
  143. }
  144. HRESULT
  145. CHsmPolicy::GetClassID(
  146. OUT CLSID* pClsid
  147. )
  148. /*++
  149. Implements:
  150. IPersist::GetClassID().
  151. --*/
  152. {
  153. HRESULT hr = S_OK;
  154. WsbTraceIn(OLESTR("CHsmPolicy::GetClassID"), OLESTR(""));
  155. try {
  156. WsbAssert(0 != pClsid, E_POINTER);
  157. *pClsid = CLSID_CHsmPolicy;
  158. } WsbCatch(hr);
  159. WsbTraceOut(OLESTR("CHsmPolicy::GetClassID"), OLESTR("hr = <%ls>, CLSID = <%ls>"), WsbHrAsString(hr), WsbGuidAsString(*pClsid));
  160. return(hr);
  161. }
  162. HRESULT
  163. CHsmPolicy::GetIdentifier(
  164. OUT GUID* pId
  165. )
  166. /*++
  167. Implements:
  168. IHsmPolicy::GetIdentifier().
  169. --*/
  170. {
  171. HRESULT hr = S_OK;
  172. try {
  173. WsbAssert(0 != pId, E_POINTER);
  174. *pId = m_id;
  175. } WsbCatch(hr);
  176. return(hr);
  177. }
  178. HRESULT
  179. CHsmPolicy::GetName(
  180. OUT OLECHAR** pName,
  181. IN ULONG bufferSize
  182. )
  183. /*++
  184. Implements:
  185. IHsmPolicy::GetName().
  186. --*/
  187. {
  188. HRESULT hr = S_OK;
  189. try {
  190. WsbAssert(0 != pName, E_POINTER);
  191. WsbAffirmHr(m_name.CopyTo(pName, bufferSize));
  192. } WsbCatch(hr);
  193. return(hr);
  194. }
  195. HRESULT
  196. CHsmPolicy::GetScale(
  197. OUT USHORT* pScale
  198. )
  199. /*++
  200. Implements:
  201. IHsmPolicy::GetScale().
  202. --*/
  203. {
  204. HRESULT hr = S_OK;
  205. try {
  206. WsbAssert(0 != pScale, E_POINTER);
  207. *pScale = m_scale;
  208. } WsbCatch(hr);
  209. return(hr);
  210. }
  211. HRESULT
  212. CHsmPolicy::GetSizeMax(
  213. OUT ULARGE_INTEGER* pSize
  214. )
  215. /*++
  216. Implements:
  217. IPersistStream::GetSizeMax().
  218. --*/
  219. {
  220. HRESULT hr = S_OK;
  221. CComPtr<IPersistStream> pPersistStream;
  222. ULARGE_INTEGER entrySize;
  223. WsbTraceIn(OLESTR("CHsmPolicy::GetSizeMax"), OLESTR(""));
  224. try {
  225. WsbAssert(0 != pSize, E_POINTER);
  226. // Determine the size for a rule with no criteria.
  227. pSize->QuadPart = WsbPersistSize((wcslen(m_name) + 1) * sizeof(OLECHAR)) + WsbPersistSizeOf(GUID) + WsbPersistSizeOf(USHORT) + 2 * WsbPersistSizeOf(BOOL);
  228. // If there is an action, how big is it?
  229. if (m_pAction != 0) {
  230. WsbAffirmHr(m_pAction->QueryInterface(IID_IPersistStream, (void**) &pPersistStream));
  231. WsbAffirmHr(pPersistStream->GetSizeMax(&entrySize));
  232. pSize->QuadPart += entrySize.QuadPart;
  233. pPersistStream = 0;
  234. }
  235. // Now allocate space for the rules (assume they are all the
  236. // same size).
  237. WsbAffirmHr(m_pRules->QueryInterface(IID_IPersistStream, (void**) &pPersistStream));
  238. WsbAffirmHr(pPersistStream->GetSizeMax(&entrySize));
  239. pSize->QuadPart += entrySize.QuadPart;
  240. } WsbCatch(hr);
  241. WsbTraceOut(OLESTR("CHsmPolicy::GetSizeMax"), OLESTR("hr = <%ls>, Size = <%ls>"), WsbHrAsString(hr), WsbPtrToUliAsString(pSize));
  242. return(hr);
  243. }
  244. HRESULT
  245. CHsmPolicy::Load(
  246. IN IStream* pStream
  247. )
  248. /*++
  249. Implements:
  250. IPersistStream::Load().
  251. --*/
  252. {
  253. HRESULT hr = S_OK;
  254. CComPtr<IPersistStream> pPersistStream;
  255. BOOL hasAction;
  256. WsbTraceIn(OLESTR("CHsmPolicy::Load"), OLESTR(""));
  257. try {
  258. WsbAssert(0 != pStream, E_POINTER);
  259. // Do the easy stuff, but make sure that this order matches the order
  260. // in the save method.
  261. WsbAffirmHr(WsbLoadFromStream(pStream, &m_id));
  262. WsbAffirmHr(WsbLoadFromStream(pStream, &m_name, 0));
  263. WsbAffirmHr(WsbLoadFromStream(pStream, &m_scale));
  264. WsbAffirmHr(WsbLoadFromStream(pStream, &m_usesDefaultRules));
  265. // Is there an action?
  266. WsbAffirmHr(WsbLoadFromStream(pStream, &hasAction));
  267. if (hasAction) {
  268. WsbAffirmHr(OleLoadFromStream(pStream, IID_IHsmAction, (void**) &m_pAction));
  269. }
  270. WsbAffirmHr(m_pRules->QueryInterface(IID_IPersistStream, (void**) &pPersistStream));
  271. WsbAffirmHr(pPersistStream->Load(pStream));
  272. } WsbCatch(hr);
  273. WsbTraceOut(OLESTR("CHsmPolicy::Load"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  274. return(hr);
  275. }
  276. HRESULT
  277. CHsmPolicy::Rules(
  278. OUT IWsbCollection** ppRules
  279. )
  280. /*++
  281. Implements:
  282. IHsmPolicy::Rules().
  283. --*/
  284. {
  285. HRESULT hr = S_OK;
  286. try {
  287. WsbAssert(0 != ppRules, E_POINTER);
  288. *ppRules = m_pRules;
  289. m_pRules.p->AddRef();
  290. } WsbCatch(hr);
  291. return(hr);
  292. }
  293. HRESULT
  294. CHsmPolicy::Save(
  295. IN IStream* pStream,
  296. IN BOOL clearDirty
  297. )
  298. /*++
  299. Implements:
  300. IPersistStream::Save().
  301. --*/
  302. {
  303. HRESULT hr = S_OK;
  304. CComPtr<IWsbEnum> pEnum;
  305. CComPtr<IPersistStream> pPersistStream;
  306. BOOL hasAction = FALSE;
  307. WsbTraceIn(OLESTR("CHsmPolicy::Save"), OLESTR("clearDirty = <%ls>"), WsbBoolAsString(clearDirty));
  308. try {
  309. WsbAssert(0 != pStream, E_POINTER);
  310. // Do the easy stuff, but make sure that this order matches the order
  311. // in the save method.
  312. WsbAffirmHr(WsbSaveToStream(pStream, m_id));
  313. WsbAffirmHr(WsbSaveToStream(pStream, m_name));
  314. WsbAffirmHr(WsbSaveToStream(pStream, m_scale));
  315. WsbAffirmHr(WsbSaveToStream(pStream, m_usesDefaultRules));
  316. // Is there an action?
  317. if (m_pAction != 0) {
  318. hasAction = TRUE;
  319. WsbAffirmHr(WsbSaveToStream(pStream, hasAction));
  320. WsbAffirmHr(m_pAction->QueryInterface(IID_IPersistStream, (void**) &pPersistStream));
  321. WsbAffirmHr(OleSaveToStream(pPersistStream, pStream));
  322. pPersistStream = 0;
  323. } else {
  324. hasAction = FALSE;
  325. WsbAffirmHr(WsbSaveToStream(pStream, hasAction));
  326. }
  327. WsbAffirmHr(m_pRules->QueryInterface(IID_IPersistStream, (void**) &pPersistStream));
  328. WsbAffirmHr(pPersistStream->Save(pStream, clearDirty));
  329. // The loop should terminate with a not found error.
  330. WsbAffirm(hr == WSB_E_NOTFOUND, hr);
  331. hr = S_OK;
  332. // If we got it saved and we were asked to clear the dirty bit, then
  333. // do so now.
  334. if (clearDirty) {
  335. m_isDirty = FALSE;
  336. }
  337. } WsbCatch(hr);
  338. WsbTraceOut(OLESTR("CHsmPolicy::Save"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  339. return(hr);
  340. }
  341. HRESULT
  342. CHsmPolicy::SetAction(
  343. IN IHsmAction* pAction
  344. )
  345. /*++
  346. Implements:
  347. IHsmPolicy::SetAction().
  348. --*/
  349. {
  350. HRESULT hr = S_OK;
  351. try {
  352. m_pAction = pAction;
  353. m_isDirty = TRUE;
  354. } WsbCatch(hr);
  355. return(hr);
  356. }
  357. HRESULT
  358. CHsmPolicy::SetName(
  359. IN OLECHAR* name
  360. )
  361. /*++
  362. Implements:
  363. IHsmPolicy::SetName().
  364. --*/
  365. {
  366. HRESULT hr = S_OK;
  367. try {
  368. m_name = name;
  369. m_isDirty = TRUE;
  370. } WsbCatch(hr);
  371. return(hr);
  372. }
  373. HRESULT
  374. CHsmPolicy::SetScale(
  375. IN USHORT scale
  376. )
  377. /*++
  378. Implements:
  379. IHsmPolicy::SetScale().
  380. --*/
  381. {
  382. HRESULT hr = S_OK;
  383. try {
  384. m_scale = scale;
  385. m_isDirty = TRUE;
  386. } WsbCatch(hr);
  387. return(hr);
  388. }
  389. HRESULT
  390. CHsmPolicy::SetUsesDefaultRules(
  391. IN BOOL usesDefaultRules
  392. )
  393. /*++
  394. Implements:
  395. IHsmPolicy::SetUsesDefaultRules().
  396. --*/
  397. {
  398. m_usesDefaultRules = usesDefaultRules;
  399. m_isDirty = TRUE;
  400. return(S_OK);
  401. }
  402. HRESULT
  403. CHsmPolicy::Test(
  404. USHORT* passed,
  405. USHORT* failed
  406. )
  407. /*++
  408. Implements:
  409. IWsbTestable::Test().
  410. --*/
  411. {
  412. HRESULT hr = S_OK;
  413. try {
  414. WsbAssert(0 != passed, E_POINTER);
  415. WsbAssert(0 != failed, E_POINTER);
  416. *passed = 0;
  417. *failed = 0;
  418. } WsbCatch(hr);
  419. return(hr);
  420. }
  421. HRESULT
  422. CHsmPolicy::UsesDefaultRules(
  423. void
  424. )
  425. /*++
  426. Implements:
  427. IHsmPolicy::UsesDefaultRules().
  428. --*/
  429. {
  430. HRESULT hr = S_OK;
  431. if (!m_usesDefaultRules) {
  432. hr = S_FALSE;
  433. }
  434. return(hr);
  435. }