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.

438 lines
15 KiB

  1. // MDSPEnumStorage.cpp : Implementation of CMDSPEnumStorage
  2. #include "stdafx.h"
  3. #include "MsPMSP.h"
  4. #include "MDSPEnumStorage.h"
  5. #include "MDSPStorage.h"
  6. #include "MdspDefs.h"
  7. #include "loghelp.h"
  8. #include "wmsstd.h"
  9. #define STRSAFE_NO_DEPRECATE
  10. #include "strsafe.h"
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CMDSPEnumStorage
  13. CMDSPEnumStorage::CMDSPEnumStorage()
  14. {
  15. m_hFFile=INVALID_HANDLE_VALUE; // this is similar to a cursor
  16. m_nEndSearch=0; // this signals the cursor is at the end
  17. m_nFindFileIndex=0; // this indicates the position of FindFile, used for Clone()
  18. m_wcsPath[0] = 0;
  19. }
  20. CMDSPEnumStorage::~CMDSPEnumStorage()
  21. {
  22. if( m_hFFile !=INVALID_HANDLE_VALUE )
  23. FindClose(m_hFFile);
  24. }
  25. STDMETHODIMP CMDSPEnumStorage::Next(ULONG celt, IMDSPStorage * * ppStorage, ULONG * pceltFetched)
  26. {
  27. HRESULT hr=S_FALSE;
  28. CARg(ppStorage);
  29. CARg(pceltFetched);
  30. *pceltFetched = 0;
  31. if(m_nEndSearch) return S_FALSE;
  32. DWORD dwLen = wcslen(m_wcsPath);
  33. if (dwLen == 0 || dwLen >= ARRAYSIZE(m_wcsPath))
  34. {
  35. // a) Code below aassumes that dwLen > 0 (uses dwLen - 1 as an index
  36. // b) dwLen >= ARRAYSIZE(m_wcsPath) implies m_wcsPath has overflowed
  37. // Below, we use the fact that dwLen < ARRAYSIZE(m_wcsPath) to
  38. // bound the sizes of temp variables into which m_wcsPath is copied
  39. return E_FAIL;
  40. }
  41. if( dwLen < 3 )
  42. {
  43. CComObject<CMDSPStorage> *pStg;
  44. hr=CComObject<CMDSPStorage>::CreateInstance(&pStg);
  45. if( SUCCEEDED(hr) )
  46. {
  47. hr=pStg->QueryInterface(IID_IMDSPStorage, reinterpret_cast<void**>(ppStorage));
  48. if( FAILED(hr) ) { delete pStg; *pceltFetched=0; }
  49. else
  50. {
  51. wcscpy(pStg->m_wcsName, m_wcsPath);
  52. if( m_wcsPath[wcslen(m_wcsPath)-1] != 0x5c )
  53. {
  54. wcscat(pStg->m_wcsName, g_wcsBackslash);
  55. }
  56. pStg->m_bIsDirectory = TRUE;
  57. m_nEndSearch = 1; // Signal end of enumeration
  58. }
  59. }
  60. if( SUCCEEDED(hr) ) // if obj created successfully
  61. {
  62. *pceltFetched=1;
  63. if( celt != 1 ) hr=S_FALSE; // didn't get what he wanted
  64. }
  65. return hr;
  66. }
  67. // For non-root storage
  68. WCHAR wcsTmp[MAX_PATH+1+BACKSLASH_STRING_LENGTH];// for appending "\\*"
  69. // Note that ARRAYSIZE(m_wcsPath) == MAX_PATH
  70. char szTmp[MAX_PATH];
  71. ULONG i;
  72. if( g_bIsWinNT )
  73. {
  74. WIN32_FIND_DATAW wfd;
  75. for(i=0; (i<celt)&&(!m_nEndSearch); )
  76. {
  77. if( m_hFFile == INVALID_HANDLE_VALUE )
  78. {
  79. wcscpy(wcsTmp, m_wcsPath);
  80. if( m_wcsPath[wcslen(m_wcsPath)-1] != 0x5c ) wcscat(wcsTmp, g_wcsBackslash);
  81. wcscat(wcsTmp, L"*");
  82. m_hFFile = FindFirstFileW(wcsTmp, &wfd);
  83. if( m_hFFile == INVALID_HANDLE_VALUE )
  84. {
  85. m_nEndSearch = 1;
  86. }
  87. else m_nFindFileIndex=1;
  88. }
  89. else
  90. {
  91. if( !FindNextFileW(m_hFFile, &wfd) )
  92. {
  93. m_nEndSearch = 1;
  94. }
  95. else m_nFindFileIndex++;
  96. }
  97. if ( !m_nEndSearch )
  98. {
  99. if( !wcscmp(wfd.cFileName, L".") || !wcscmp(wfd.cFileName, L"..") )
  100. {
  101. continue;
  102. }
  103. CComObject<CMDSPStorage> *pStg;
  104. hr=CComObject<CMDSPStorage>::CreateInstance(&pStg);
  105. if( SUCCEEDED(hr) )
  106. {
  107. hr=pStg->QueryInterface(IID_IMDSPStorage, reinterpret_cast<void**>(&(ppStorage[*pceltFetched])));
  108. if( FAILED(hr) )
  109. {
  110. delete pStg;
  111. break;
  112. /* *pceltFetched=0; */
  113. }
  114. else
  115. {
  116. // Compute the number of chars we'll use
  117. // up in pStg->m_wcsName
  118. int nHave = ARRAYSIZE(pStg->m_wcsName) - 1;
  119. // -1 for the NULL terminator
  120. nHave -= dwLen;
  121. if( m_wcsPath[dwLen-1] != 0x5c )
  122. {
  123. nHave -= BACKSLASH_STRING_LENGTH;
  124. }
  125. nHave -= wcslen(wfd.cFileName);
  126. if (nHave >= 0)
  127. {
  128. wcscpy(pStg->m_wcsName, m_wcsPath);
  129. if( m_wcsPath[wcslen(m_wcsPath)-1] != 0x5c )
  130. {
  131. wcscat(pStg->m_wcsName, g_wcsBackslash);
  132. }
  133. wcscat(pStg->m_wcsName, wfd.cFileName);
  134. pStg->m_bIsDirectory = ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
  135. *pceltFetched = (*pceltFetched)+1;
  136. i++;
  137. }
  138. else
  139. {
  140. ppStorage[*pceltFetched]->Release();
  141. ppStorage[*pceltFetched] = NULL;
  142. hr = STRSAFE_E_INSUFFICIENT_BUFFER;
  143. //defined in strsafe.h
  144. break;
  145. }
  146. }
  147. }
  148. else
  149. {
  150. break;
  151. }
  152. }
  153. } // end of For loop
  154. if (FAILED(hr))
  155. {
  156. // Note: m_nFindFileIndex and m_hFind are not reset to their
  157. // state at the start of this function
  158. for (; *pceltFetched; )
  159. {
  160. (*pceltFetched)--;
  161. ppStorage[*pceltFetched]->Release();
  162. ppStorage[*pceltFetched] = NULL;
  163. }
  164. }
  165. }
  166. else
  167. { // On Win9x, use A-version of Win32 APIs
  168. WIN32_FIND_DATAA fd;
  169. for(i=0; (i<celt)&&(!m_nEndSearch); )
  170. {
  171. if( m_hFFile == INVALID_HANDLE_VALUE )
  172. {
  173. wcscpy(wcsTmp, m_wcsPath);
  174. if( m_wcsPath[wcslen(m_wcsPath)-1] != 0x5c )
  175. {
  176. wcscat(wcsTmp, g_wcsBackslash);
  177. }
  178. wcscat(wcsTmp, L"*");
  179. WideCharToMultiByte(CP_ACP, NULL, wcsTmp, -1, szTmp, MAX_PATH, NULL, NULL);
  180. m_hFFile = FindFirstFileA(szTmp, &fd);
  181. if( m_hFFile == INVALID_HANDLE_VALUE )
  182. {
  183. m_nEndSearch = 1;
  184. }
  185. else m_nFindFileIndex=1;
  186. }
  187. else
  188. {
  189. if( !FindNextFileA(m_hFFile, &fd) )
  190. {
  191. m_nEndSearch = 1;
  192. }
  193. else m_nFindFileIndex++;
  194. }
  195. if ( !m_nEndSearch )
  196. {
  197. if( !strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, "..") )
  198. {
  199. continue;
  200. }
  201. CComObject<CMDSPStorage> *pStg;
  202. hr=CComObject<CMDSPStorage>::CreateInstance(&pStg);
  203. if( SUCCEEDED(hr) )
  204. {
  205. hr=pStg->QueryInterface(IID_IMDSPStorage, reinterpret_cast<void**>(&(ppStorage[*pceltFetched])));
  206. if( FAILED(hr) )
  207. {
  208. delete pStg;
  209. /* *pceltFetched=0; */
  210. }
  211. else
  212. {
  213. wcscpy(pStg->m_wcsName, m_wcsPath);
  214. if( m_wcsPath[wcslen(m_wcsPath)-1] != 0x5c )
  215. {
  216. wcscat(pStg->m_wcsName, g_wcsBackslash);
  217. }
  218. MultiByteToWideChar(CP_ACP, NULL, fd.cFileName, -1, wcsTmp, MAX_PATH);
  219. wcscat(pStg->m_wcsName, wcsTmp);
  220. pStg->m_bIsDirectory = ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
  221. *pceltFetched = (*pceltFetched)+1;
  222. i++;
  223. }
  224. }
  225. }
  226. } // end of For loop
  227. }
  228. if( SUCCEEDED(hr) && (*pceltFetched<celt) )
  229. hr = S_FALSE;
  230. Error:
  231. hrLogDWORD("IMDSPEnumStorage::Next returned 0x%08lx", hr, hr);
  232. return hr;
  233. }
  234. STDMETHODIMP CMDSPEnumStorage::Skip(ULONG celt, ULONG *pceltFetched)
  235. {
  236. HRESULT hr=S_OK;
  237. CARg(celt);
  238. CARg(pceltFetched);
  239. CFRg(!m_nEndSearch); // make sure it is not the end of list
  240. *pceltFetched = 0;
  241. if( wcslen(m_wcsPath) < 3 ) // do nothing if it is the root storage
  242. return S_OK;
  243. char szTmp[MAX_PATH];
  244. WCHAR wcsTmp[MAX_PATH+1+BACKSLASH_STRING_LENGTH]; // for appending "\\*"
  245. ULONG i;
  246. if( wcslen(m_wcsPath) >= ARRAYSIZE(wcsTmp) - BACKSLASH_STRING_LENGTH - 1 )
  247. {
  248. // We check the length against wcsTmp's size because wcsTmp is the
  249. // recipient of a string copy below. However, note that m_wcsPath
  250. // also has MAX_PATH characters, so if this happens, it means that
  251. // it has overflowed. Bail out.
  252. return E_FAIL;
  253. }
  254. if( g_bIsWinNT )
  255. {
  256. WIN32_FIND_DATAW wfd;
  257. for(i=0; (i<celt)&&(!m_nEndSearch); )
  258. {
  259. if( m_hFFile==INVALID_HANDLE_VALUE ) // at the start
  260. {
  261. wcscpy(wcsTmp, m_wcsPath);
  262. if( m_wcsPath[wcslen(m_wcsPath)-1] != 0x5c ) wcscat(wcsTmp, g_wcsBackslash);
  263. wcscat(wcsTmp, L"*");
  264. m_hFFile = FindFirstFileW(wcsTmp, &wfd);
  265. if( m_hFFile == INVALID_HANDLE_VALUE ) m_nEndSearch = 1;
  266. else m_nFindFileIndex=1;
  267. } else {
  268. if( !FindNextFileW(m_hFFile, &wfd) ) m_nEndSearch = 1;
  269. else m_nFindFileIndex++;
  270. }
  271. if( !m_nEndSearch ) {
  272. if( !wcscmp(wfd.cFileName, L".") || !wcscmp(wfd.cFileName, L"..") )
  273. continue;
  274. *pceltFetched = (*pceltFetched)+1;
  275. i++;
  276. }
  277. }
  278. } else { // On Win9x, use A-version of Win32 APIs
  279. WIN32_FIND_DATAA fd;
  280. for(i=0; (i<celt)&&(!m_nEndSearch); )
  281. {
  282. if( m_hFFile==INVALID_HANDLE_VALUE ) // at the start
  283. {
  284. wcscpy(wcsTmp, m_wcsPath);
  285. if( m_wcsPath[wcslen(m_wcsPath)-1] != 0x5c ) wcscat(wcsTmp, g_wcsBackslash);
  286. wcscat(wcsTmp, L"*");
  287. WideCharToMultiByte(CP_ACP, NULL, wcsTmp, -1, szTmp, MAX_PATH, NULL, NULL);
  288. m_hFFile = FindFirstFileA(szTmp, &fd);
  289. if( m_hFFile == INVALID_HANDLE_VALUE ) m_nEndSearch = 1;
  290. else m_nFindFileIndex=1;
  291. } else {
  292. if( !FindNextFileA(m_hFFile, &fd) ) m_nEndSearch = 1;
  293. else m_nFindFileIndex++;
  294. }
  295. if( !m_nEndSearch ) {
  296. if( !strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, "..") )
  297. continue;
  298. *pceltFetched = (*pceltFetched)+1;
  299. i++;
  300. }
  301. }
  302. }
  303. if( *pceltFetched < celt ) hr = S_FALSE;
  304. Error:
  305. hrLogDWORD("IMDSPEnumStorage::Skip returned 0x%08lx", hr, hr);
  306. return hr;
  307. }
  308. STDMETHODIMP CMDSPEnumStorage::Reset()
  309. {
  310. HRESULT hr = S_OK;
  311. m_nEndSearch=0;
  312. if(m_hFFile && m_hFFile != INVALID_HANDLE_VALUE )
  313. FindClose(m_hFFile);
  314. m_hFFile = INVALID_HANDLE_VALUE;
  315. m_nFindFileIndex=0;
  316. // Error:
  317. hrLogDWORD("IMDSPEnumStorage::Reset returned 0x%08lx", hr, hr);
  318. return hr;
  319. }
  320. STDMETHODIMP CMDSPEnumStorage::Clone(IMDSPEnumStorage * * ppEnumStorage)
  321. {
  322. HRESULT hr=E_FAIL;
  323. CARg(ppEnumStorage);
  324. if (wcslen(m_wcsPath) >= ARRAYSIZE(m_wcsPath))
  325. {
  326. // The variable has overflowed
  327. goto Error;
  328. }
  329. CComObject<CMDSPEnumStorage> *pEnumObj;
  330. CORg(CComObject<CMDSPEnumStorage>::CreateInstance(&pEnumObj));
  331. hr=pEnumObj->QueryInterface(IID_IMDSPEnumStorage, reinterpret_cast<void**>(ppEnumStorage));
  332. if( FAILED(hr) )
  333. delete pEnumObj;
  334. else {
  335. WCHAR wcsTmp[MAX_PATH+1+BACKSLASH_STRING_LENGTH];
  336. char szTmp[MAX_PATH];
  337. int i, nErrorEnd=0;
  338. wcscpy(pEnumObj->m_wcsPath, m_wcsPath);
  339. pEnumObj->m_nEndSearch = m_nEndSearch;
  340. pEnumObj->m_nFindFileIndex = m_nFindFileIndex;
  341. if( !(pEnumObj->m_nEndSearch) && (pEnumObj->m_nFindFileIndex) )
  342. // now Clone the FindFile state
  343. {
  344. if( g_bIsWinNT )
  345. {
  346. WIN32_FIND_DATAW wfd;
  347. for(i=0; (i<m_nFindFileIndex)&&(!nErrorEnd); )
  348. {
  349. if( pEnumObj->m_hFFile == INVALID_HANDLE_VALUE ) {
  350. wcscpy(wcsTmp, m_wcsPath);
  351. if( m_wcsPath[wcslen(m_wcsPath)-1] != 0x5c ) wcscat(wcsTmp, g_wcsBackslash);
  352. wcscat(wcsTmp, L"*");
  353. pEnumObj->m_hFFile = FindFirstFileW(wcsTmp, &wfd);
  354. if( pEnumObj->m_hFFile == INVALID_HANDLE_VALUE ) nErrorEnd = 1;
  355. else i=1;
  356. } else {
  357. if( !FindNextFileW(pEnumObj->m_hFFile, &wfd) ) nErrorEnd = 1;
  358. else i++;
  359. }
  360. if ( !nErrorEnd )
  361. {
  362. if( !wcscmp(wfd.cFileName, L".") || !wcscmp(wfd.cFileName, L"..") )
  363. {
  364. continue;
  365. }
  366. }
  367. } // end of FOR loop
  368. } else {
  369. WIN32_FIND_DATAA fd;
  370. for(i=0; (i<m_nFindFileIndex)&&(!nErrorEnd); )
  371. {
  372. if( pEnumObj->m_hFFile == INVALID_HANDLE_VALUE ) {
  373. wcscpy(wcsTmp, m_wcsPath);
  374. if( m_wcsPath[wcslen(m_wcsPath)-1] != 0x5c ) wcscat(wcsTmp, g_wcsBackslash);
  375. wcscat(wcsTmp, L"*");
  376. WideCharToMultiByte(CP_ACP, NULL, wcsTmp, -1, szTmp, MAX_PATH, NULL, NULL);
  377. pEnumObj->m_hFFile = FindFirstFileA(szTmp, &fd);
  378. if( pEnumObj->m_hFFile == INVALID_HANDLE_VALUE ) nErrorEnd = 1;
  379. else i=1;
  380. } else {
  381. if( !FindNextFileA(pEnumObj->m_hFFile, &fd) ) nErrorEnd = 1;
  382. else i++;
  383. }
  384. if ( !nErrorEnd )
  385. {
  386. if( !strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, "..") )
  387. {
  388. continue;
  389. }
  390. }
  391. } // end of FOR loop
  392. }
  393. }
  394. if ( nErrorEnd ) hr = E_UNEXPECTED;
  395. else hr=S_OK;
  396. }
  397. Error:
  398. hrLogDWORD("IMDSPEnumStorage::Clone returned 0x%08lx", hr, hr);
  399. return hr;
  400. }