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.

356 lines
7.8 KiB

  1. #include "stdafx.h"
  2. #include "storageglobals.h"
  3. #include "device.h"
  4. #include "storage.h"
  5. //#include "findleak.h"
  6. //DECLARE_THIS_FILE;
  7. //
  8. // Initializtion
  9. //
  10. CStorageGlobals::CStorageGlobals()
  11. {
  12. memset(m_szStartPath, 0, sizeof(m_szStartPath) );
  13. }
  14. HRESULT CStorageGlobals::Init(LPCWSTR szStartPath, IMDSPDevice *pDevice)
  15. {
  16. DWORD dwAttrib;
  17. if( NULL == szStartPath || NULL == pDevice)
  18. {
  19. return( E_INVALIDARG );
  20. }
  21. HRESULT hr = StringCbCopyW(m_szStartPath, sizeof(m_szStartPath), szStartPath);
  22. if (FAILED(hr))
  23. {
  24. hr = HRESULT_FROM_WIN32(HRESULT_CODE(hr));
  25. }
  26. else
  27. {
  28. if( wcslen(m_szStartPath) > 1 )
  29. {
  30. LPWSTR szChop = wcschr( &m_szStartPath[1], L'\\' );
  31. if( szChop )
  32. {
  33. WCHAR chSave = *szChop;
  34. *szChop = _T('\0');
  35. dwAttrib = CeGetFileAttributes( m_szStartPath );
  36. if( ! ( ( dwAttrib & FILE_ATTRIBUTE_DIRECTORY ) &&
  37. ( dwAttrib & FILE_ATTRIBUTE_TEMPORARY ) ) )
  38. {
  39. m_szStartPath[1] = L'\0';
  40. }
  41. }
  42. else
  43. {
  44. dwAttrib = CeGetFileAttributes( m_szStartPath );
  45. if( ! ( ( dwAttrib & FILE_ATTRIBUTE_DIRECTORY ) &&
  46. ( dwAttrib & FILE_ATTRIBUTE_TEMPORARY ) ) )
  47. {
  48. m_szStartPath[1] = L'\0';
  49. }
  50. }
  51. }
  52. else
  53. {
  54. m_szStartPath[1] = L'\0';
  55. }
  56. m_spDevice = pDevice;
  57. }
  58. return hr;
  59. }
  60. //
  61. // IMDSPStorageGloabls
  62. //
  63. STDMETHODIMP CStorageGlobals::GetCapabilities ( DWORD *pdwCapabilities )
  64. {
  65. if( NULL == pdwCapabilities )
  66. {
  67. return( E_INVALIDARG );
  68. }
  69. *pdwCapabilities = WMDM_STORAGECAP_FOLDERSINROOT | WMDM_STORAGECAP_FILESINROOT | WMDM_STORAGECAP_FOLDERSINFOLDERS | WMDM_STORAGECAP_FILESINFOLDERS;
  70. return( S_OK );
  71. }
  72. STDMETHODIMP CStorageGlobals::GetSerialNumber ( PWMDMID pSerialNum, BYTE abMac[ 20 ] )
  73. {
  74. HRESULT hr = S_OK;
  75. hr = CeUtilGetSerialNumber( m_szStartPath, pSerialNum, NULL, 0 );
  76. if( hr == S_OK )
  77. {
  78. // MAC the parameters
  79. HMAC hMAC;
  80. hr = g_pAppSCServer->MACInit(&hMAC);
  81. if( SUCCEEDED( hr ) )
  82. {
  83. hr = g_pAppSCServer->MACUpdate(hMAC, (BYTE*)(pSerialNum), sizeof(WMDMID));
  84. }
  85. if( SUCCEEDED( hr ) )
  86. {
  87. hr = g_pAppSCServer->MACFinal(hMAC, abMac);
  88. }
  89. }
  90. else
  91. {
  92. hr = WMDM_E_NOTSUPPORTED;
  93. }
  94. return( hr );
  95. }
  96. STDMETHODIMP CStorageGlobals::GetTotalSize ( DWORD *pdwFreeLow, DWORD *pdwFreeHigh )
  97. {
  98. HRESULT hr = S_OK;
  99. WCHAR wszTestPath[MAX_PATH];
  100. LPCWSTR pszTestPath = wszTestPath;
  101. memset( wszTestPath, 0, sizeof(wszTestPath) );
  102. if( wcslen( m_szStartPath ) > 1 )
  103. {
  104. _snwprintf( wszTestPath, sizeof(wszTestPath)/sizeof(wszTestPath[0]) - 1, L"%s\\", m_szStartPath );
  105. }
  106. else
  107. {
  108. pszTestPath = m_szStartPath;
  109. }
  110. ULARGE_INTEGER liAvailFree;
  111. ULARGE_INTEGER liTotalBytes;
  112. ULARGE_INTEGER liTotalFree;
  113. liAvailFree.QuadPart = 0;
  114. liTotalBytes.QuadPart = 0;
  115. liTotalFree.QuadPart = 0;
  116. if( NULL != pdwFreeLow )
  117. {
  118. *pdwFreeLow = 0;
  119. }
  120. if( NULL != pdwFreeHigh )
  121. {
  122. *pdwFreeHigh = 0;
  123. }
  124. hr = CeGetDiskFreeSpaceEx( pszTestPath, &liAvailFree, &liTotalBytes, &liTotalFree );
  125. if( SUCCEEDED( hr ) )
  126. {
  127. if( NULL != pdwFreeLow )
  128. {
  129. *pdwFreeLow = liTotalBytes.LowPart;
  130. }
  131. if( NULL != pdwFreeHigh )
  132. {
  133. *pdwFreeHigh = liTotalBytes.HighPart;
  134. }
  135. }
  136. return( hr );
  137. }
  138. STDMETHODIMP CStorageGlobals::GetTotalFree ( DWORD *pdwFreeLow, DWORD *pdwFreeHigh )
  139. {
  140. HRESULT hr = S_OK;
  141. WCHAR wszTestPath[MAX_PATH];
  142. LPCWSTR pszTestPath = wszTestPath;
  143. memset( wszTestPath, 0, sizeof(wszTestPath) );
  144. if( wcslen( m_szStartPath ) > 1 )
  145. {
  146. _snwprintf( wszTestPath, sizeof(wszTestPath)/sizeof(wszTestPath[0]) - 1, L"%s\\", m_szStartPath );
  147. }
  148. else
  149. {
  150. pszTestPath = m_szStartPath;
  151. }
  152. ULARGE_INTEGER liAvailFree;
  153. ULARGE_INTEGER liTotalBytes;
  154. ULARGE_INTEGER liTotalFree;
  155. liAvailFree.QuadPart = 0;
  156. liTotalBytes.QuadPart = 0;
  157. liTotalFree.QuadPart = 0;
  158. if( NULL != pdwFreeLow )
  159. {
  160. *pdwFreeLow = 0;
  161. }
  162. if( NULL != pdwFreeHigh )
  163. {
  164. *pdwFreeHigh = 0;
  165. }
  166. hr = CeGetDiskFreeSpaceEx( pszTestPath, &liAvailFree, &liTotalBytes, &liTotalFree );
  167. if( SUCCEEDED( hr ) )
  168. {
  169. if( NULL != pdwFreeLow )
  170. {
  171. *pdwFreeLow = liAvailFree.LowPart;
  172. }
  173. if( NULL != pdwFreeHigh )
  174. {
  175. *pdwFreeHigh = liAvailFree.HighPart;
  176. }
  177. }
  178. return( hr );
  179. }
  180. STDMETHODIMP CStorageGlobals::GetTotalBad ( DWORD *pdwBadLow, DWORD *pdwBadHigh )
  181. {
  182. if( NULL != pdwBadLow )
  183. {
  184. *pdwBadLow = 0;
  185. }
  186. if( NULL != pdwBadHigh )
  187. {
  188. *pdwBadHigh = 0;
  189. }
  190. return( S_OK );
  191. }
  192. STDMETHODIMP CStorageGlobals::GetStatus ( DWORD *pdwStatus )
  193. {
  194. if( NULL == pdwStatus )
  195. {
  196. return( E_INVALIDARG );
  197. }
  198. if( _Module.g_fDeviceConnected )
  199. {
  200. *pdwStatus = WMDM_STATUS_READY;
  201. }
  202. else
  203. {
  204. *pdwStatus = WMDM_STATUS_STORAGE_NOTPRESENT;
  205. }
  206. return( S_OK );
  207. }
  208. STDMETHODIMP CStorageGlobals::Initialize ( UINT fuMode, IWMDMProgress *pProgress )
  209. {
  210. return( S_OK );
  211. }
  212. STDMETHODIMP CStorageGlobals::GetDevice ( IMDSPDevice * *ppDevice )
  213. {
  214. if( NULL == ppDevice )
  215. {
  216. return( E_INVALIDARG );
  217. }
  218. *ppDevice = m_spDevice;
  219. if( *ppDevice )
  220. {
  221. (*ppDevice)->AddRef();
  222. return( S_OK );
  223. }
  224. return( E_FAIL );
  225. }
  226. STDMETHODIMP CStorageGlobals::GetRootStorage ( IMDSPStorage * *ppRoot )
  227. {
  228. WCHAR wszPath[MAX_PATH];
  229. CE_FIND_DATA findData;
  230. CComStorage *pNewStorage = NULL;
  231. HRESULT hr = S_OK;
  232. if( NULL == ppRoot )
  233. {
  234. return( E_INVALIDARG );
  235. }
  236. *ppRoot = NULL;
  237. hr = CComStorage::CreateInstance( &pNewStorage );
  238. CComPtr<IMDSPStorage> spStorage = pNewStorage;
  239. memset( wszPath, 0, sizeof(wszPath) );
  240. if( SUCCEEDED( hr ) )
  241. {
  242. LPWSTR pszSlash = wcschr(&m_szStartPath[1], L'\\');
  243. if( pszSlash )
  244. {
  245. wcsncpy(wszPath, m_szStartPath, (pszSlash - m_szStartPath) - 1 );
  246. }
  247. else
  248. {
  249. wcscpy(wszPath, m_szStartPath);
  250. }
  251. if( wcslen(wszPath) > 1 )
  252. {
  253. HANDLE hFind = CeFindFirstFile( wszPath, &findData );
  254. if( INVALID_HANDLE_VALUE == hFind )
  255. {
  256. hr = HRESULT_FROM_WIN32( CeGetLastError() );
  257. if( SUCCEEDED( hr ) )
  258. {
  259. hr = CeRapiGetError();
  260. }
  261. }
  262. else
  263. {
  264. CeFindClose( hFind );
  265. }
  266. }
  267. else
  268. {
  269. memset( &findData, 0, sizeof(findData) );
  270. findData.dwFileAttributes = FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_DIRECTORY;
  271. wcscpy(findData.cFileName, L"\\");
  272. }
  273. }
  274. if( SUCCEEDED( hr ) )
  275. {
  276. if( ! ( findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) )
  277. {
  278. hr = E_FAIL;
  279. }
  280. }
  281. if( SUCCEEDED( hr ) )
  282. {
  283. hr = pNewStorage->Init(&findData, m_szStartPath, TRUE, m_spDevice);
  284. }
  285. if( SUCCEEDED( hr ) )
  286. {
  287. *ppRoot = spStorage;
  288. spStorage.Detach();
  289. }
  290. return( hr );
  291. }