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.

344 lines
6.9 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1996 - 2000
  3. Module Name:
  4. mountmed.cpp
  5. Abstract:
  6. This component is an object representing a mounting media, i.e. a media in the process of mounting.
  7. Author:
  8. Ran Kalach [rankala] 28-Sep-2000
  9. Revision History:
  10. --*/
  11. #include "stdafx.h"
  12. #include "mountmed.h"
  13. static USHORT iCountMount = 0; // Count of existing objects
  14. HRESULT
  15. CMountingMedia::FinalConstruct(
  16. void
  17. )
  18. /*++
  19. Implements:
  20. CComObjectRoot::FinalConstruct().
  21. --*/
  22. {
  23. HRESULT hr = S_OK;
  24. WsbTraceIn(OLESTR("CMountingMedia::FinalConstruct"), OLESTR(""));
  25. m_mediaId = GUID_NULL;
  26. m_mountEvent = NULL;
  27. m_bReadOnly = FALSE;
  28. iCountMount++;
  29. WsbTraceOut(OLESTR("CMountingMedia::FinalConstruct"), OLESTR("hr = <%ls>, Count is <%d>"), WsbHrAsString(hr), (int)iCountMount);
  30. return(hr);
  31. }
  32. void
  33. CMountingMedia::FinalRelease(
  34. void
  35. )
  36. /*++
  37. Implements:
  38. CComObjectRoot::FinalRelease().
  39. --*/
  40. {
  41. WsbTraceIn(OLESTR("CMountingMedia::FinalRelease"), OLESTR(""));
  42. // Free event handle
  43. if (m_mountEvent != NULL) {
  44. // Set the event (just to be on the safe side - we expect the event to be signaled at this point)
  45. SetEvent(m_mountEvent);
  46. CloseHandle(m_mountEvent);
  47. m_mountEvent = NULL;
  48. }
  49. iCountMount--;
  50. WsbTraceOut(OLESTR("CMountingMedia::FinalRelease"), OLESTR("Count is <%d>"), (int)iCountMount);
  51. }
  52. HRESULT
  53. CMountingMedia::Init(
  54. REFGUID mediaId,
  55. BOOL bReadOnly
  56. )
  57. /*++
  58. Implements:
  59. IMountingMedia::Init().
  60. --*/
  61. {
  62. HRESULT hr = S_OK;
  63. WsbTraceIn(OLESTR("CMountingMedia::Init"), OLESTR(""));
  64. try {
  65. m_mediaId = mediaId;
  66. m_bReadOnly = bReadOnly;
  67. WsbAffirmHandle(m_mountEvent= CreateEvent(NULL, TRUE, FALSE, NULL));
  68. } WsbCatch(hr);
  69. WsbTraceOut(OLESTR("CMountingMedia::Init"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  70. return(hr);
  71. }
  72. HRESULT
  73. CMountingMedia::GetMediaId(
  74. GUID *pMediaId
  75. )
  76. /*++
  77. Implements:
  78. IMountingMedia::GetMediaId().
  79. --*/
  80. {
  81. HRESULT hr = S_OK;
  82. WsbTraceIn(OLESTR("CMountingMedia::GetMediaId"), OLESTR(""));
  83. try {
  84. WsbAffirm(0 != pMediaId, E_POINTER);
  85. *pMediaId = m_mediaId;
  86. } WsbCatch(hr);
  87. WsbTraceOut(OLESTR("CMountingMedia::GetMediaId"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  88. return(hr);
  89. }
  90. HRESULT
  91. CMountingMedia::SetMediaId(
  92. REFGUID mediaId
  93. )
  94. /*++
  95. Implements:
  96. IMountingMedia::SetMediaId().
  97. --*/
  98. {
  99. HRESULT hr = S_OK;
  100. WsbTraceIn(OLESTR("CMountingMedia::SetMediaId"), OLESTR(""));
  101. m_mediaId = mediaId;
  102. WsbTraceOut(OLESTR("CMountingMedia::SetMediaId"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  103. return(hr);
  104. }
  105. HRESULT
  106. CMountingMedia::SetIsReadOnly(
  107. BOOL bReadOnly
  108. )
  109. /*++
  110. Implements:
  111. IMountingMedia::SetIsReadOnly().
  112. --*/
  113. {
  114. WsbTraceIn(OLESTR("CMountingMedia::SetIsReadOnly"), OLESTR("bReadOnly = %d"), bReadOnly);
  115. m_bReadOnly = bReadOnly;
  116. WsbTraceOut(OLESTR("CMountingMedia::SetIsReadOnly"), OLESTR(""));
  117. return(S_OK);
  118. }
  119. HRESULT
  120. CMountingMedia::IsReadOnly(
  121. void
  122. )
  123. /*++
  124. Implements:
  125. IMountingMedia::IsReadOnly().
  126. --*/
  127. {
  128. HRESULT hr = S_OK;
  129. WsbTraceIn(OLESTR("CMountingMedia::IsReadOnly"), OLESTR(""));
  130. hr = (m_bReadOnly ? S_OK : S_FALSE);
  131. WsbTraceOut(OLESTR("CMountingMedia::IsReadOnly"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  132. return(hr);
  133. }
  134. HRESULT
  135. CMountingMedia::WaitForMount(
  136. DWORD dwTimeout
  137. )
  138. /*++
  139. Implements:
  140. IMountingMedia::WaitForMount().
  141. --*/
  142. {
  143. HRESULT hr = S_OK;
  144. WsbTraceIn(OLESTR("CMountingMedia::WaitForMount"), OLESTR(""));
  145. // Wait for the mount event for the given timeout
  146. switch (WaitForSingleObject(m_mountEvent, dwTimeout)) {
  147. case WAIT_OBJECT_0:
  148. WsbTrace(OLESTR("CMountingMedia::WaitForMount: signaled that media is mounted\n"));
  149. break;
  150. case WAIT_TIMEOUT:
  151. WsbTrace(OLESTR("CMountingMedia::WaitForMount: WaitForSingleObject timed out after waiting for %lu ms\n"), dwTimeout);
  152. hr = E_FAIL;
  153. break;
  154. case WAIT_FAILED:
  155. default:
  156. DWORD dwErr = GetLastError();
  157. hr = HRESULT_FROM_WIN32(dwErr);
  158. WsbTrace(OLESTR("CMountingMedia::WaitForMount: WaitForSingleObject returned error %lu\n"), dwErr);
  159. break;
  160. }
  161. WsbTraceOut(OLESTR("CMountingMedia::WaitForMount"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  162. return(hr);
  163. }
  164. HRESULT
  165. CMountingMedia::MountDone(
  166. void
  167. )
  168. /*++
  169. Implements:
  170. IMountingMedia::MountDone().
  171. --*/
  172. {
  173. HRESULT hr = S_OK;
  174. WsbTraceIn(OLESTR("CMountingMedia::MountDone"), OLESTR(""));
  175. // Mount is done: set the mount event
  176. if (! SetEvent(m_mountEvent)) {
  177. DWORD dwErr = GetLastError();
  178. WsbTrace(OLESTR("CMountingMedia::MountDone: SetEvent returned error %lu\n"), dwErr);
  179. hr = HRESULT_FROM_WIN32(dwErr);
  180. }
  181. WsbTraceOut(OLESTR("CMountingMedia::MountDone"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  182. return(hr);
  183. }
  184. HRESULT
  185. CMountingMedia::IsEqual(
  186. IUnknown* pCollectable
  187. )
  188. /*++
  189. Implements:
  190. IWsbCollectable::IsEqual().
  191. --*/
  192. {
  193. HRESULT hr = S_OK;
  194. WsbTraceIn(OLESTR("CMountingMedia::IsEqual"), OLESTR(""));
  195. hr = CompareTo(pCollectable, NULL);
  196. WsbTraceOut(OLESTR("CMountingMedia::IsEqual"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  197. return(hr);
  198. }
  199. HRESULT
  200. CMountingMedia::CompareTo(
  201. IUnknown* pCollectable,
  202. SHORT* pResult
  203. )
  204. /*++
  205. Implements:
  206. IWsbCollectable::CompareTo().
  207. --*/
  208. {
  209. HRESULT hr = S_OK;
  210. SHORT result = 0;
  211. CComPtr<IMountingMedia> pMountingMedia;
  212. GUID mediaId;
  213. WsbTraceIn(OLESTR("CMountingMedia::CompareTo"), OLESTR(""));
  214. try {
  215. // Did they give us a valid item to compare to?
  216. WsbAssert(0 != pCollectable, E_POINTER);
  217. WsbAffirmHr(pCollectable->QueryInterface(IID_IMountingMedia, (void**) &pMountingMedia));
  218. WsbAffirmHr(pMountingMedia->GetMediaId(&mediaId));
  219. // Compare
  220. if (IsEqualGUID(m_mediaId, mediaId)) {
  221. hr = S_OK;
  222. result = 0;
  223. } else {
  224. // Need to provide signed result...
  225. hr = S_FALSE;
  226. result = WsbSign(memcmp(&m_mediaId, &mediaId, sizeof(GUID)));
  227. }
  228. // If they asked for the relative value back, then return it to them.
  229. if (pResult != NULL) {
  230. *pResult = result;
  231. }
  232. } WsbCatch(hr);
  233. WsbTraceOut(OLESTR("CMountingMedia::CompareTo"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  234. return(hr);
  235. }