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.

501 lines
13 KiB

  1. // MDSPStorageGlobals.cpp : Implementation of CMDSPStorageGlobals
  2. #include "stdafx.h"
  3. #include "MsPMSP.h"
  4. #include "MDSPStorageGlobals.h"
  5. #include "MDSPStorage.h"
  6. #include "MDSPDevice.h"
  7. #include "MdspDefs.h"
  8. #include "SerialNumber.h"
  9. #include "winnt.h"
  10. #include "loghelp.h"
  11. #include "SHFormatDrive.h"
  12. // #include "process.h" /* _beginthread, _endthread */
  13. #include "strsafe.h"
  14. typedef struct __FORMATTHREADARGS
  15. {
  16. CMDSPStorageGlobals *pThis;
  17. DWORD dwDriveNumber;
  18. BOOL bNewThread;
  19. IWMDMProgress *pProgress;
  20. LPSTREAM pStream;
  21. } FORMATTHREADARGS;
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CMDSPStorageGlobals
  24. CMDSPStorageGlobals::~CMDSPStorageGlobals()
  25. {
  26. if( m_pMDSPDevice != NULL )
  27. m_pMDSPDevice->Release();
  28. for(int i=0; i<MDSP_MAX_DEVICE_OBJ;i++)
  29. {
  30. if( !wcscmp(g_GlobalDeviceInfo[i].wcsDevName, m_wcsName) )
  31. {
  32. g_GlobalDeviceInfo[i].pIMDSPStorageGlobals = NULL;
  33. }
  34. }
  35. }
  36. STDMETHODIMP CMDSPStorageGlobals::GetCapabilities(DWORD * pdwCapabilities)
  37. {
  38. HRESULT hr = S_OK;
  39. CFRg(g_pAppSCServer);
  40. if ( !(g_pAppSCServer->fIsAuthenticated()) )
  41. {
  42. CORg(WMDM_E_NOTCERTIFIED);
  43. }
  44. CARg(pdwCapabilities);
  45. *pdwCapabilities = 0;
  46. *pdwCapabilities = WMDM_STORAGECAP_FOLDERSINROOT |
  47. WMDM_STORAGECAP_FILESINROOT |
  48. WMDM_STORAGECAP_FOLDERSINFOLDERS |
  49. WMDM_STORAGECAP_FILESINFOLDERS ;
  50. Error:
  51. hrLogDWORD("IMDSPStorageGlobals::GetCapabilities returned 0x%08lx", hr, hr);
  52. return hr;
  53. }
  54. STDMETHODIMP CMDSPStorageGlobals::GetSerialNumber(PWMDMID pSerialNum,
  55. BYTE abMac[WMDM_MAC_LENGTH])
  56. {
  57. HRESULT hr;
  58. CFRg(g_pAppSCServer);
  59. if ( !(g_pAppSCServer->fIsAuthenticated()) )
  60. {
  61. CORg(WMDM_E_NOTCERTIFIED);
  62. }
  63. CARg(pSerialNum);
  64. // CARg((pSerialNum->cbSize)==sizeof(WMDMID));
  65. IMDSPDevice *pDev; // For PM SP, device is the same as StorageGlobals
  66. CHRg(GetDevice(&pDev));
  67. hr = UtilGetSerialNumber(m_wcsName, pSerialNum, FALSE);
  68. pDev->Release();
  69. if( hr == HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED) )
  70. {
  71. hr = WMDM_E_NOTSUPPORTED;
  72. }
  73. if( hr == S_OK )
  74. {
  75. // MAC the parameters
  76. HMAC hMAC;
  77. CORg(g_pAppSCServer->MACInit(&hMAC));
  78. CORg(g_pAppSCServer->MACUpdate(hMAC, (BYTE*)(pSerialNum), sizeof(WMDMID)));
  79. CORg(g_pAppSCServer->MACFinal(hMAC, abMac));
  80. }
  81. Error:
  82. hrLogDWORD("IMDSPStorageGlobals::GetSerialNumber returned 0x%08lx", hr, hr);
  83. return hr;
  84. }
  85. STDMETHODIMP CMDSPStorageGlobals::GetTotalSize(DWORD * pdwTotalSizeLow, DWORD * pdwTotalSizeHigh)
  86. {
  87. HRESULT hr=S_OK;
  88. CFRg(g_pAppSCServer);
  89. if ( !(g_pAppSCServer->fIsAuthenticated()) )
  90. {
  91. CORg(WMDM_E_NOTCERTIFIED);
  92. }
  93. CARg(pdwTotalSizeLow);
  94. CARg(pdwTotalSizeHigh);
  95. ULARGE_INTEGER i64FreeBytesToCaller, i64TotalBytes, i64FreeBytes;
  96. DWORD dwSectPerClust, dwBytesPerSect, dwFreeClusters, dwTotalClusters;
  97. if( g_bIsWinNT )
  98. {
  99. typedef BOOL (WINAPI *P_GDFSE)(LPCWSTR,PULARGE_INTEGER,PULARGE_INTEGER,PULARGE_INTEGER);
  100. P_GDFSE pGetDiskFreeSpaceExW;
  101. pGetDiskFreeSpaceExW = (P_GDFSE)GetProcAddress (GetModuleHandleW(L"kernel32.dll"),
  102. "GetDiskFreeSpaceExW");
  103. if (pGetDiskFreeSpaceExW)
  104. {
  105. CFRg(pGetDiskFreeSpaceExW (m_wcsName,
  106. (PULARGE_INTEGER)&i64FreeBytesToCaller,
  107. (PULARGE_INTEGER)&i64TotalBytes,
  108. (PULARGE_INTEGER)&i64FreeBytes));
  109. } else {
  110. CFRg(GetDiskFreeSpaceW(m_wcsName, &dwSectPerClust, &dwBytesPerSect,
  111. &dwFreeClusters, &dwTotalClusters));
  112. i64TotalBytes.QuadPart = UInt32x32To64(dwBytesPerSect, dwSectPerClust*dwTotalClusters);
  113. // i64FreeBytesToCaller.QuadPart = UInt32x32To64(dwBytesPerSect, dwSectPerClust*dwFreeClusters);
  114. }
  115. } else { // On Win9x, use A-version of Win32 APIs
  116. char pszDrive[32];
  117. WideCharToMultiByte(CP_ACP, NULL, m_wcsName, -1, pszDrive, 32, NULL, NULL);
  118. typedef BOOL (WINAPI *P_GDFSE)(LPCSTR,PULARGE_INTEGER,PULARGE_INTEGER,PULARGE_INTEGER);
  119. P_GDFSE pGetDiskFreeSpaceEx;
  120. pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress (GetModuleHandleA("kernel32.dll"),
  121. "GetDiskFreeSpaceExA");
  122. if (pGetDiskFreeSpaceEx)
  123. {
  124. CFRg(pGetDiskFreeSpaceEx (pszDrive,
  125. (PULARGE_INTEGER)&i64FreeBytesToCaller,
  126. (PULARGE_INTEGER)&i64TotalBytes,
  127. (PULARGE_INTEGER)&i64FreeBytes));
  128. } else {
  129. CFRg(GetDiskFreeSpaceA(pszDrive, &dwSectPerClust, &dwBytesPerSect,
  130. &dwFreeClusters, &dwTotalClusters));
  131. i64TotalBytes.QuadPart = UInt32x32To64(dwBytesPerSect, dwSectPerClust*dwTotalClusters);
  132. // i64FreeBytesToCaller.QuadPart = UInt32x32To64(dwBytesPerSect, dwSectPerClust*dwFreeClusters);
  133. }
  134. }
  135. *pdwTotalSizeLow = i64TotalBytes.LowPart;
  136. *pdwTotalSizeHigh = i64TotalBytes.HighPart;
  137. Error:
  138. hrLogDWORD("IMDSPStorageGlobals::GetTotalSize returned 0x%08lx", hr, hr);
  139. return hr;
  140. }
  141. STDMETHODIMP CMDSPStorageGlobals::GetTotalFree(DWORD * pdwFreeLow, DWORD * pdwFreeHigh)
  142. {
  143. HRESULT hr=S_OK;
  144. CFRg(g_pAppSCServer);
  145. if ( !(g_pAppSCServer->fIsAuthenticated()) )
  146. {
  147. CORg(WMDM_E_NOTCERTIFIED);
  148. }
  149. CARg(pdwFreeLow);
  150. CARg(pdwFreeHigh);
  151. ULARGE_INTEGER i64FreeBytesToCaller, i64TotalBytes, i64FreeBytes;
  152. DWORD dwSectPerClust, dwBytesPerSect, dwFreeClusters, dwTotalClusters;
  153. if( g_bIsWinNT )
  154. {
  155. typedef BOOL (WINAPI *P_GDFSE)(LPCWSTR,PULARGE_INTEGER,PULARGE_INTEGER,PULARGE_INTEGER);
  156. P_GDFSE pGetDiskFreeSpaceExW;
  157. pGetDiskFreeSpaceExW = (P_GDFSE)GetProcAddress (GetModuleHandleW(L"kernel32.dll"),
  158. "GetDiskFreeSpaceExW");
  159. if (pGetDiskFreeSpaceExW)
  160. {
  161. CFRg(pGetDiskFreeSpaceExW (m_wcsName,
  162. (PULARGE_INTEGER)&i64FreeBytesToCaller,
  163. (PULARGE_INTEGER)&i64TotalBytes,
  164. (PULARGE_INTEGER)&i64FreeBytes));
  165. } else {
  166. CFRg(GetDiskFreeSpaceW(m_wcsName, &dwSectPerClust, &dwBytesPerSect,
  167. &dwFreeClusters, &dwTotalClusters));
  168. // i64TotalBytes.QuadPart = UInt32x32To64(dwBytesPerSect, dwSectPerClust*dwTotalClusters);
  169. i64FreeBytesToCaller.QuadPart = UInt32x32To64(dwBytesPerSect, dwSectPerClust*dwFreeClusters);
  170. }
  171. } else { // On Win9x, use A-version of Win32 APIs
  172. char pszDrive[32];
  173. WideCharToMultiByte(CP_ACP, NULL, m_wcsName, -1, pszDrive, 32, NULL, NULL);
  174. typedef BOOL (WINAPI *P_GDFSE)(LPCSTR,PULARGE_INTEGER,PULARGE_INTEGER,PULARGE_INTEGER);
  175. P_GDFSE pGetDiskFreeSpaceEx;
  176. pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress (GetModuleHandleA("kernel32.dll"),
  177. "GetDiskFreeSpaceExA");
  178. if (pGetDiskFreeSpaceEx)
  179. {
  180. CFRg(pGetDiskFreeSpaceEx (pszDrive,
  181. (PULARGE_INTEGER)&i64FreeBytesToCaller,
  182. (PULARGE_INTEGER)&i64TotalBytes,
  183. (PULARGE_INTEGER)&i64FreeBytes));
  184. } else {
  185. CFRg(GetDiskFreeSpace(pszDrive, &dwSectPerClust, &dwBytesPerSect,
  186. &dwFreeClusters, &dwTotalClusters));
  187. // i64TotalBytes.QuadPart = UInt32x32To64(dwBytesPerSect, dwSectPerClust*dwTotalClusters);
  188. i64FreeBytesToCaller.QuadPart = UInt32x32To64(dwBytesPerSect, dwSectPerClust*dwFreeClusters);
  189. }
  190. }
  191. *pdwFreeLow = i64FreeBytesToCaller.LowPart;
  192. *pdwFreeHigh = i64FreeBytesToCaller.HighPart;
  193. Error:
  194. hrLogDWORD("IMDSPStorageGlobals::GetTotalFree returned 0x%08lx", hr, hr);
  195. return hr;
  196. }
  197. STDMETHODIMP CMDSPStorageGlobals::GetTotalBad(DWORD * pdwBadLow, DWORD * pdwBadHigh)
  198. {
  199. HRESULT hr = WMDM_E_NOTSUPPORTED;
  200. CFRg(g_pAppSCServer);
  201. if ( !(g_pAppSCServer->fIsAuthenticated()) )
  202. {
  203. CORg(WMDM_E_NOTCERTIFIED);
  204. }
  205. Error:
  206. hrLogDWORD("IMDSPStorageGlobals::GetTotalBad returned 0x%08lx", hr, hr);
  207. return hr;
  208. }
  209. STDMETHODIMP CMDSPStorageGlobals::GetStatus(DWORD * pdwStatus)
  210. {
  211. HRESULT hr;
  212. IMDSPDevice *pDev; // For PM SP, device is the same as StorageGlobals
  213. CFRg(g_pAppSCServer);
  214. if ( !(g_pAppSCServer->fIsAuthenticated()) )
  215. {
  216. CORg(WMDM_E_NOTCERTIFIED);
  217. }
  218. CHRg(GetDevice(&pDev));
  219. hr = pDev->GetStatus(pdwStatus);
  220. pDev->Release();
  221. Error:
  222. hrLogDWORD("IMDSPStorageGlobals::GetStatus returned 0x%08lx", hr, hr);
  223. return hr;
  224. }
  225. DWORD DriveFormatFunc( void *dn )
  226. {
  227. HRESULT hr=S_OK;
  228. FORMATTHREADARGS *pChildArgs=NULL;
  229. BOOL bProgStarted=FALSE;
  230. pChildArgs = (FORMATTHREADARGS *)dn;
  231. if( pChildArgs->bNewThread )
  232. {
  233. CORg(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED));
  234. if( pChildArgs->pProgress )
  235. {
  236. CORg(CoGetInterfaceAndReleaseStream(pChildArgs->pStream,
  237. IID_IWMDMProgress, (LPVOID *)&(pChildArgs->pProgress)));
  238. }
  239. }
  240. if( pChildArgs->pProgress )
  241. {
  242. CORg(pChildArgs->pProgress->Begin(100));
  243. bProgStarted=TRUE;
  244. }
  245. CHRg(SetGlobalDeviceStatus(pChildArgs->pThis->m_wcsName, WMDM_STATUS_BUSY | WMDM_STATUS_STORAGE_INITIALIZING, TRUE));
  246. hr = SHFormatDrive(NULL, pChildArgs->dwDriveNumber, SHFMT_ID_DEFAULT, SHFMT_OPT_FULL);
  247. SetGlobalDeviceStatus(pChildArgs->pThis->m_wcsName, WMDM_STATUS_READY, TRUE);
  248. Error:
  249. if( bProgStarted )
  250. {
  251. pChildArgs->pProgress->Progress(100);
  252. pChildArgs->pProgress->End();
  253. pChildArgs->pProgress->Release();
  254. }
  255. if( pChildArgs->bNewThread )
  256. {
  257. CoUninitialize();
  258. }
  259. if( pChildArgs )
  260. {
  261. delete pChildArgs;
  262. }
  263. return hr;
  264. }
  265. STDMETHODIMP CMDSPStorageGlobals::Initialize(UINT fuMode, IWMDMProgress * pProgress)
  266. {
  267. HRESULT hr=S_OK;
  268. CFRg(g_pAppSCServer);
  269. if ( !(g_pAppSCServer->fIsAuthenticated()) )
  270. {
  271. CORg(WMDM_E_NOTCERTIFIED);
  272. }
  273. CORg(WMDM_E_NOTSUPPORTED);
  274. /* // This implementation is for PM-SP only
  275. DWORD dwStat;
  276. DWORD driveNum, dwThreadID;
  277. CORg(GetStatus(&dwStat));
  278. if( dwStat & WMDM_STATUS_BUSY )
  279. {
  280. return WMDM_E_BUSY;
  281. }
  282. FORMATTHREADARGS *pParentArgs;
  283. pParentArgs = new FORMATTHREADARGS;
  284. CPRg(pParentArgs);
  285. driveNum = (m_wcsName[0]>0x60) ? (m_wcsName[0]-L'a') : (m_wcsName[0]-L'A');
  286. pParentArgs->dwDriveNumber = driveNum;
  287. pParentArgs->bNewThread = (fuMode & WMDM_MODE_THREAD)?TRUE:FALSE;
  288. pParentArgs->pThis = this;
  289. if( pParentArgs->bNewThread )
  290. {
  291. if( pProgress )
  292. {
  293. pProgress->AddRef();
  294. CORg(CoMarshalInterThreadInterfaceInStream(
  295. IID_IWMDMProgress, (LPUNKNOWN)pProgress,
  296. (LPSTREAM *)&(pParentArgs->pStream)));
  297. pParentArgs->pProgress=pProgress; // mark it but don't use it
  298. } else {
  299. pParentArgs->pProgress=NULL;
  300. }
  301. } else {
  302. pParentArgs->pProgress = pProgress;
  303. if( pProgress ) pProgress->AddRef();
  304. }
  305. if( fuMode & WMDM_MODE_BLOCK )
  306. {
  307. dwStat=DriveFormatFunc((void *)pParentArgs);
  308. if( (dwStat==E_FAIL) || (dwStat==SHFMT_ERROR) ||
  309. (dwStat==SHFMT_CANCEL) || (dwStat==SHFMT_NOFORMAT) )
  310. hr=E_FAIL;
  311. } else if ( fuMode & WMDM_MODE_THREAD ) {
  312. CWRg(CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)DriveFormatFunc,
  313. (void *)pParentArgs, 0, &dwThreadID));
  314. } else
  315. hr = E_INVALIDARG;
  316. */
  317. Error:
  318. hrLogDWORD("IMDSPStorageGlobals::Initialize returned 0x%08lx", hr, hr);
  319. return hr;
  320. }
  321. STDMETHODIMP CMDSPStorageGlobals::GetDevice(IMDSPDevice * * ppDevice)
  322. {
  323. HRESULT hr;
  324. CFRg(g_pAppSCServer);
  325. if ( !(g_pAppSCServer->fIsAuthenticated()) )
  326. {
  327. CORg(WMDM_E_NOTCERTIFIED);
  328. }
  329. CARg(ppDevice);
  330. if( m_pMDSPDevice )
  331. {
  332. *ppDevice = m_pMDSPDevice;
  333. (*ppDevice)->AddRef();
  334. return S_OK;
  335. }
  336. CComObject<CMDSPDevice> *pObj;
  337. CORg(CComObject<CMDSPDevice>::CreateInstance(&pObj));
  338. hr=pObj->QueryInterface(IID_IMDSPDevice, reinterpret_cast<void**>(ppDevice));
  339. if( FAILED(hr) )
  340. delete pObj;
  341. else {
  342. // wcscpy(pObj->m_wcsName, m_wcsName);
  343. hr = StringCbCopyW(pObj->m_wcsName, sizeof(pObj->m_wcsName), m_wcsName);
  344. if( FAILED(hr) )
  345. {
  346. (*ppDevice)->Release();
  347. *ppDevice = NULL;
  348. goto Error;
  349. }
  350. pObj->InitGlobalDeviceInfo();
  351. m_pMDSPDevice = *ppDevice;
  352. m_pMDSPDevice->AddRef();
  353. hr = S_OK;
  354. }
  355. Error:
  356. hrLogDWORD("IMDSPStorageGlobals::GetDevice returned 0x%08lx", hr, hr);
  357. return hr;
  358. }
  359. STDMETHODIMP CMDSPStorageGlobals::GetRootStorage(IMDSPStorage * * ppRoot)
  360. {
  361. HRESULT hr;
  362. CFRg(g_pAppSCServer);
  363. if ( !(g_pAppSCServer->fIsAuthenticated()) )
  364. {
  365. CORg(WMDM_E_NOTCERTIFIED);
  366. }
  367. CARg(ppRoot);
  368. CComObject<CMDSPStorage> *pObj;
  369. CORg(CComObject<CMDSPStorage>::CreateInstance(&pObj));
  370. hr=pObj->QueryInterface(IID_IMDSPStorage, reinterpret_cast<void**>(ppRoot));
  371. if( FAILED(hr) )
  372. {
  373. delete pObj;
  374. }
  375. else
  376. {
  377. // wcscpy(pObj->m_wcsName, m_wcsName);
  378. hr = StringCbCopyW(pObj->m_wcsName, sizeof(pObj->m_wcsName), m_wcsName);
  379. if( FAILED(hr) )
  380. {
  381. (*ppRoot)->Release();
  382. *ppRoot = NULL;
  383. goto Error;
  384. }
  385. DWORD dwLen = wcslen(m_wcsName);
  386. if (dwLen == 0)
  387. {
  388. hr = E_FAIL;
  389. (*ppRoot)->Release();
  390. *ppRoot = NULL;
  391. goto Error;
  392. }
  393. if( m_wcsName[dwLen-1] != 0x5c )
  394. {
  395. // wcscat(pObj->m_wcsName, g_wcsBackslash);
  396. hr = StringCbCatW(pObj->m_wcsName, sizeof(pObj->m_wcsName),g_wcsBackslash);
  397. if( FAILED(hr) )
  398. {
  399. (*ppRoot)->Release();
  400. *ppRoot = NULL;
  401. goto Error;
  402. }
  403. }
  404. pObj->m_bIsDirectory = TRUE;
  405. hr = S_OK;
  406. }
  407. Error:
  408. hrLogDWORD("IMDSPStorageGlobals::GetRootStorage returned 0x%08lx", hr, hr);
  409. return hr;
  410. }