Source code of Windows XP (NT5)
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.

6282 lines
158 KiB

  1. /*++
  2. Copyright (C) 2000-2001 Microsoft Corporation
  3. --*/
  4. #include <windows.h>
  5. #include <wbemidl.h>
  6. #include <wbemint.h>
  7. #include <stdio.h>
  8. #include <wbemcomn.h>
  9. #include <ql.h>
  10. #include <time.h>
  11. #include "a51rep.h"
  12. #include <md5.h>
  13. #include <objpath.h>
  14. #include "lock.h"
  15. #include <persistcfg.h>
  16. #include "a51fib.h"
  17. #include "RepositoryPackager.h"
  18. #include "a51conv.h"
  19. #include "A51Exp.h"
  20. #include "A51Imp.h"
  21. //**************************************************************************************************
  22. HRESULT STDMETHODCALLTYPE CSession::QueryInterface(REFIID riid, void** ppv)
  23. {
  24. if(riid == IID_IUnknown || riid == IID_IWmiDbSession ||
  25. riid == IID_IWmiDbSessionEx)
  26. {
  27. AddRef();
  28. *ppv = this;
  29. return S_OK;
  30. }
  31. else return E_NOINTERFACE;
  32. }
  33. ULONG STDMETHODCALLTYPE CSession::Release()
  34. {
  35. return CUnkBase<IWmiDbSessionEx, &IID_IWmiDbSessionEx>::Release();
  36. }
  37. CSession::~CSession()
  38. {
  39. }
  40. HRESULT STDMETHODCALLTYPE CSession::GetObject(
  41. IWmiDbHandle *pScope,
  42. IWbemPath *pPath,
  43. DWORD dwFlags,
  44. DWORD dwRequestedHandleType,
  45. IWmiDbHandle **ppResult
  46. )
  47. {
  48. try
  49. {
  50. HRESULT hres;
  51. CAutoReadLock lock(&g_readWriteLock, FALSE);
  52. if (!m_bInWriteTransaction)
  53. {
  54. lock.Lock();
  55. }
  56. if (g_bShuttingDown)
  57. {
  58. return WBEM_E_SHUTTING_DOWN;
  59. }
  60. CNamespaceHandle* pNs = (CNamespaceHandle*)pScope;
  61. if(FAILED(pNs->GetErrorStatus()))
  62. {
  63. return pNs->GetErrorStatus();
  64. }
  65. hres = pNs->GetObject(pPath, dwFlags, dwRequestedHandleType,
  66. ppResult);
  67. return hres;
  68. }
  69. catch (...)
  70. {
  71. return WBEM_E_CRITICAL_ERROR;
  72. }
  73. }
  74. HRESULT STDMETHODCALLTYPE CSession::GetObjectDirect(
  75. IWmiDbHandle *pScope,
  76. IWbemPath *pPath,
  77. DWORD dwFlags,
  78. REFIID riid,
  79. LPVOID *pObj
  80. )
  81. {
  82. try
  83. {
  84. HRESULT hres;
  85. CAutoReadLock lock(&g_readWriteLock, FALSE);
  86. if (!m_bInWriteTransaction)
  87. {
  88. lock.Lock();
  89. }
  90. if (g_bShuttingDown)
  91. return WBEM_E_SHUTTING_DOWN;
  92. CNamespaceHandle* pNs = (CNamespaceHandle*)pScope;
  93. if(FAILED(pNs->GetErrorStatus()))
  94. {
  95. return pNs->GetErrorStatus();
  96. }
  97. hres = pNs->GetObjectDirect(pPath, dwFlags, riid, pObj);
  98. return hres;
  99. }
  100. catch (...)
  101. {
  102. return WBEM_E_CRITICAL_ERROR;
  103. }
  104. }
  105. HRESULT STDMETHODCALLTYPE CSession::GetObjectByPath(
  106. IWmiDbHandle *pScope,
  107. LPCWSTR wszObjectPath,
  108. DWORD dwFlags,
  109. REFIID riid,
  110. LPVOID *pObj
  111. )
  112. {
  113. try
  114. {
  115. HRESULT hres;
  116. CAutoReadLock lock(&g_readWriteLock, FALSE);
  117. if (!m_bInWriteTransaction)
  118. {
  119. lock.Lock();
  120. }
  121. if (g_bShuttingDown)
  122. return WBEM_E_SHUTTING_DOWN;
  123. CNamespaceHandle* pNs = (CNamespaceHandle*)pScope;
  124. if(FAILED(pNs->GetErrorStatus()))
  125. {
  126. return pNs->GetErrorStatus();
  127. }
  128. DWORD dwLen = wcslen(wszObjectPath)+1;
  129. LPWSTR wszPath = (WCHAR*)TempAlloc(dwLen*sizeof(WCHAR));
  130. if (wszPath == NULL)
  131. {
  132. return WBEM_E_OUT_OF_MEMORY;
  133. }
  134. wcscpy(wszPath, wszObjectPath);
  135. CTempFreeMe vdm(wszPath, dwLen * sizeof(WCHAR));
  136. hres = pNs->GetObjectByPath(wszPath, dwFlags, riid, pObj);
  137. return hres;
  138. }
  139. catch (...)
  140. {
  141. return WBEM_E_CRITICAL_ERROR;
  142. }
  143. }
  144. HRESULT STDMETHODCALLTYPE CSession::PutObject(
  145. IWmiDbHandle *pScope,
  146. REFIID riid,
  147. LPVOID pObj,
  148. DWORD dwFlags,
  149. DWORD dwRequestedHandleType,
  150. IWmiDbHandle **ppResult
  151. )
  152. {
  153. try
  154. {
  155. HRESULT hres;
  156. long lRes;
  157. CAutoWriteLock lock(&g_readWriteLock, FALSE);
  158. CEventCollector aNonTransactedEvents;
  159. CEventCollector *aEvents = &m_aTransactedEvents;
  160. if (!m_bInWriteTransaction)
  161. {
  162. lock.Lock();
  163. if (g_bShuttingDown)
  164. return WBEM_E_SHUTTING_DOWN;
  165. aEvents = &aNonTransactedEvents;
  166. lRes = g_Glob.GetFileCache()->BeginTransaction();
  167. if(lRes != ERROR_SUCCESS)
  168. return A51TranslateErrorCode(lRes);
  169. g_Glob.GetForestCache()->BeginTransaction();
  170. }
  171. else if (g_bShuttingDown)
  172. return WBEM_E_SHUTTING_DOWN;
  173. CNamespaceHandle* pNs = (CNamespaceHandle*)pScope;
  174. if(FAILED(pNs->GetErrorStatus()))
  175. {
  176. if(!m_bInWriteTransaction)
  177. {
  178. g_Glob.GetFileCache()->AbortTransaction();
  179. g_Glob.GetForestCache()->AbortTransaction();
  180. }
  181. return pNs->GetErrorStatus();
  182. }
  183. hres = pNs->PutObject(riid, pObj, dwFlags, dwRequestedHandleType, ppResult, *aEvents);
  184. if(!m_bInWriteTransaction)
  185. {
  186. if (FAILED(hres))
  187. {
  188. g_Glob.GetFileCache()->AbortTransaction();
  189. g_Glob.GetForestCache()->AbortTransaction();
  190. }
  191. else
  192. {
  193. lRes = g_Glob.GetFileCache()->CommitTransaction();
  194. if(lRes != ERROR_SUCCESS)
  195. {
  196. hres = A51TranslateErrorCode(lRes);
  197. g_Glob.GetFileCache()->AbortTransaction();
  198. g_Glob.GetForestCache()->AbortTransaction();
  199. }
  200. else
  201. {
  202. g_Glob.GetForestCache()->CommitTransaction();
  203. lock.Unlock();
  204. _IWmiCoreServices * pSvcs = g_Glob.GetCoreSvcs();
  205. CReleaseMe rm(pSvcs);
  206. aNonTransactedEvents.SendEvents(pSvcs);
  207. }
  208. }
  209. aNonTransactedEvents.DeleteAllEvents();
  210. }
  211. return hres;
  212. }
  213. catch (...)
  214. {
  215. return WBEM_E_CRITICAL_ERROR;
  216. }
  217. }
  218. HRESULT STDMETHODCALLTYPE CSession::DeleteObject(
  219. IWmiDbHandle *pScope,
  220. DWORD dwFlags,
  221. REFIID riid,
  222. LPVOID pObj
  223. )
  224. {
  225. try
  226. {
  227. HRESULT hres;
  228. long lRes;
  229. CAutoWriteLock lock(&g_readWriteLock, FALSE);
  230. CEventCollector aNonTransactedEvents;
  231. CEventCollector *aEvents = &m_aTransactedEvents;
  232. if (!m_bInWriteTransaction)
  233. {
  234. lock.Lock();
  235. if (g_bShuttingDown)
  236. return WBEM_E_SHUTTING_DOWN;
  237. aEvents = &aNonTransactedEvents;
  238. lRes = g_Glob.GetFileCache()->BeginTransaction();
  239. if(lRes != ERROR_SUCCESS)
  240. return A51TranslateErrorCode(lRes);
  241. }
  242. else if (g_bShuttingDown)
  243. return WBEM_E_SHUTTING_DOWN;
  244. CNamespaceHandle* pNs = (CNamespaceHandle*)pScope;
  245. if(FAILED(pNs->GetErrorStatus()))
  246. {
  247. if(!m_bInWriteTransaction)
  248. {
  249. g_Glob.GetFileCache()->AbortTransaction();
  250. }
  251. return pNs->GetErrorStatus();
  252. }
  253. hres = pNs->DeleteObject(dwFlags, riid, pObj, *aEvents);
  254. if(!m_bInWriteTransaction)
  255. {
  256. if (FAILED(hres))
  257. {
  258. g_Glob.GetFileCache()->AbortTransaction();
  259. }
  260. else
  261. {
  262. lRes = g_Glob.GetFileCache()->CommitTransaction();
  263. if(lRes != ERROR_SUCCESS)
  264. {
  265. hres = A51TranslateErrorCode(lRes);
  266. g_Glob.GetFileCache()->AbortTransaction();
  267. }
  268. else
  269. {
  270. lock.Unlock();
  271. _IWmiCoreServices * pSvcs = g_Glob.GetCoreSvcs();
  272. CReleaseMe rm(pSvcs);
  273. aNonTransactedEvents.SendEvents(pSvcs);
  274. }
  275. }
  276. aNonTransactedEvents.DeleteAllEvents();
  277. }
  278. return hres;
  279. }
  280. catch (...)
  281. {
  282. return WBEM_E_CRITICAL_ERROR;
  283. }
  284. }
  285. HRESULT STDMETHODCALLTYPE CSession::DeleteObjectByPath(
  286. IWmiDbHandle *pScope,
  287. LPCWSTR wszObjectPath,
  288. DWORD dwFlags
  289. )
  290. {
  291. try
  292. {
  293. HRESULT hres;
  294. long lRes;
  295. CAutoWriteLock lock(&g_readWriteLock, FALSE);
  296. CEventCollector aNonTransactedEvents;
  297. CEventCollector *aEvents = &m_aTransactedEvents;
  298. if (!m_bInWriteTransaction)
  299. {
  300. lock.Lock();
  301. if (g_bShuttingDown)
  302. return WBEM_E_SHUTTING_DOWN;
  303. aEvents = &aNonTransactedEvents;
  304. lRes = g_Glob.GetFileCache()->BeginTransaction();
  305. if(lRes != ERROR_SUCCESS)
  306. return A51TranslateErrorCode(lRes);
  307. }
  308. else if (g_bShuttingDown)
  309. return WBEM_E_SHUTTING_DOWN;
  310. CNamespaceHandle* pNs = (CNamespaceHandle*)pScope;
  311. if(FAILED(pNs->GetErrorStatus()))
  312. {
  313. if(!m_bInWriteTransaction)
  314. {
  315. g_Glob.GetFileCache()->AbortTransaction();
  316. }
  317. return pNs->GetErrorStatus();
  318. }
  319. DWORD dwLen = wcslen(wszObjectPath)+1;
  320. LPWSTR wszPath = (WCHAR*)TempAlloc(dwLen*sizeof(WCHAR));
  321. if (wszPath == NULL)
  322. {
  323. if(!m_bInWriteTransaction)
  324. {
  325. g_Glob.GetFileCache()->AbortTransaction();
  326. }
  327. return WBEM_E_OUT_OF_MEMORY;
  328. }
  329. wcscpy(wszPath, wszObjectPath);
  330. CTempFreeMe vdm(wszPath, dwLen * sizeof(WCHAR));
  331. hres = pNs->DeleteObjectByPath(dwFlags, wszPath, *aEvents);
  332. if(!m_bInWriteTransaction)
  333. {
  334. if (FAILED(hres))
  335. {
  336. g_Glob.GetFileCache()->AbortTransaction();
  337. }
  338. else
  339. {
  340. lRes = g_Glob.GetFileCache()->CommitTransaction();
  341. if(lRes != ERROR_SUCCESS)
  342. {
  343. hres = A51TranslateErrorCode(lRes);
  344. g_Glob.GetFileCache()->AbortTransaction();
  345. }
  346. else
  347. {
  348. lock.Unlock();
  349. _IWmiCoreServices * pSvcs = g_Glob.GetCoreSvcs();
  350. CReleaseMe rm(pSvcs);
  351. aNonTransactedEvents.SendEvents(pSvcs);
  352. }
  353. }
  354. aNonTransactedEvents.DeleteAllEvents();
  355. }
  356. return hres;
  357. }
  358. catch (...)
  359. {
  360. return WBEM_E_CRITICAL_ERROR;
  361. }
  362. }
  363. HRESULT STDMETHODCALLTYPE CSession::ExecQuery(
  364. IWmiDbHandle *pScope,
  365. IWbemQuery *pQuery,
  366. DWORD dwFlags,
  367. DWORD dwRequestedHandleType,
  368. DWORD *dwMessageFlags,
  369. IWmiDbIterator **ppQueryResult
  370. )
  371. {
  372. try
  373. {
  374. HRESULT hres;
  375. CAutoReadLock lock(&g_readWriteLock, FALSE);
  376. if (!m_bInWriteTransaction)
  377. {
  378. lock.Lock();
  379. }
  380. if (g_bShuttingDown)
  381. return WBEM_E_SHUTTING_DOWN;
  382. CNamespaceHandle* pNs = (CNamespaceHandle*)pScope;
  383. if(FAILED(pNs->GetErrorStatus()))
  384. {
  385. return pNs->GetErrorStatus();
  386. }
  387. //If we are in a transaction, we have to get a message to the iteratir
  388. //on create so it does not mess around with the locks!
  389. if (m_bInWriteTransaction)
  390. pNs->TellIteratorNotToLock();
  391. hres = pNs->ExecQuery(pQuery, dwFlags,
  392. dwRequestedHandleType, dwMessageFlags, ppQueryResult);
  393. return hres;
  394. }
  395. catch (...)
  396. {
  397. return WBEM_E_CRITICAL_ERROR;
  398. }
  399. }
  400. HRESULT STDMETHODCALLTYPE CSession::ExecQuerySink(
  401. IWmiDbHandle *pScope,
  402. IWbemQuery *pQuery,
  403. DWORD dwFlags,
  404. DWORD dwRequestedHandleType,
  405. IWbemObjectSink* pSink,
  406. DWORD *dwMessageFlags
  407. )
  408. {
  409. try
  410. {
  411. HRESULT hres;
  412. CAutoReadLock lock(&g_readWriteLock, FALSE);
  413. if (!m_bInWriteTransaction)
  414. {
  415. lock.Lock();
  416. }
  417. if (g_bShuttingDown)
  418. return WBEM_E_SHUTTING_DOWN;
  419. CNamespaceHandle* pNs = (CNamespaceHandle*)pScope;
  420. if(FAILED(pNs->GetErrorStatus()))
  421. {
  422. return pNs->GetErrorStatus();
  423. }
  424. hres = pNs->ExecQuerySink(pQuery, dwFlags,
  425. dwRequestedHandleType, pSink, dwMessageFlags);
  426. return hres;
  427. }
  428. catch (...)
  429. {
  430. return WBEM_E_CRITICAL_ERROR;
  431. }
  432. }
  433. HRESULT STDMETHODCALLTYPE CSession::RenameObject(
  434. IWbemPath *pOldPath,
  435. IWbemPath *pNewPath,
  436. DWORD dwFlags,
  437. DWORD dwRequestedHandleType,
  438. IWmiDbHandle **ppResult
  439. )
  440. {
  441. DebugBreak();
  442. return E_NOTIMPL;
  443. }
  444. HRESULT STDMETHODCALLTYPE CSession::Enumerate(
  445. IWmiDbHandle *pScope,
  446. DWORD dwFlags,
  447. DWORD dwRequestedHandleType,
  448. IWmiDbIterator **ppQueryResult
  449. )
  450. {
  451. DebugBreak();
  452. return E_NOTIMPL;
  453. }
  454. HRESULT STDMETHODCALLTYPE CSession::AddObject(
  455. IWmiDbHandle *pScope,
  456. IWbemPath *pPath,
  457. DWORD dwFlags,
  458. DWORD dwRequestedHandleType,
  459. IWmiDbHandle **ppResult
  460. )
  461. {
  462. DebugBreak();
  463. return E_NOTIMPL;
  464. }
  465. HRESULT STDMETHODCALLTYPE CSession::RemoveObject (
  466. IWmiDbHandle *pScope,
  467. IWbemPath *pPath,
  468. DWORD dwFlags
  469. )
  470. {
  471. DebugBreak();
  472. return E_NOTIMPL;
  473. }
  474. HRESULT STDMETHODCALLTYPE CSession::SetDecoration(
  475. LPWSTR lpMachineName,
  476. LPWSTR lpNamespacePath
  477. )
  478. {
  479. //
  480. // As the default driver, we really don't care.
  481. //
  482. return WBEM_S_NO_ERROR;
  483. }
  484. HRESULT STDMETHODCALLTYPE CSession::BeginWriteTransaction(DWORD dwFlags)
  485. {
  486. g_readWriteLock.WriteLock();
  487. if (g_bShuttingDown)
  488. {
  489. g_readWriteLock.WriteUnlock();
  490. return WBEM_E_SHUTTING_DOWN;
  491. }
  492. long lRes = g_Glob.GetFileCache()->BeginTransaction();
  493. if(lRes != ERROR_SUCCESS)
  494. {
  495. g_readWriteLock.WriteUnlock();
  496. return A51TranslateErrorCode(lRes);
  497. }
  498. m_bInWriteTransaction = true;
  499. return ERROR_SUCCESS;
  500. }
  501. HRESULT STDMETHODCALLTYPE CSession::BeginReadTransaction(DWORD dwFlags)
  502. {
  503. g_readWriteLock.ReadLock();
  504. if (g_bShuttingDown)
  505. {
  506. g_readWriteLock.ReadUnlock();
  507. return WBEM_E_SHUTTING_DOWN;
  508. }
  509. return ERROR_SUCCESS;
  510. }
  511. HRESULT STDMETHODCALLTYPE CSession::CommitTransaction(DWORD dwFlags)
  512. {
  513. if (m_bInWriteTransaction)
  514. {
  515. long lRes = g_Glob.GetFileCache()->CommitTransaction();
  516. if(lRes != ERROR_SUCCESS)
  517. {
  518. HRESULT hres = A51TranslateErrorCode(lRes);
  519. AbortTransaction(0);
  520. return hres;
  521. }
  522. m_bInWriteTransaction = false;
  523. //Copy the event list and delete the original. We need to deliver
  524. //outside the write lock.
  525. CEventCollector aTransactedEvents;
  526. aTransactedEvents.TransferEvents(m_aTransactedEvents);
  527. g_readWriteLock.WriteUnlock();
  528. _IWmiCoreServices * pSvcs = g_Glob.GetCoreSvcs();
  529. CReleaseMe rm(pSvcs);
  530. aTransactedEvents.SendEvents(pSvcs);
  531. aTransactedEvents.DeleteAllEvents();
  532. }
  533. else
  534. {
  535. if (m_aTransactedEvents.GetSize())
  536. {
  537. _ASSERT(false, L"Read transaction has events to send");
  538. }
  539. g_readWriteLock.ReadUnlock();
  540. }
  541. return ERROR_SUCCESS;
  542. }
  543. HRESULT STDMETHODCALLTYPE CSession::AbortTransaction(DWORD dwFlags)
  544. {
  545. if (m_bInWriteTransaction)
  546. {
  547. m_bInWriteTransaction = false;
  548. g_Glob.GetFileCache()->AbortTransaction();
  549. m_aTransactedEvents.DeleteAllEvents();
  550. g_readWriteLock.WriteUnlock();
  551. }
  552. else
  553. {
  554. if (m_aTransactedEvents.GetSize())
  555. {
  556. _ASSERT(false, L"Read transaction has events to send");
  557. }
  558. g_readWriteLock.ReadUnlock();
  559. }
  560. return ERROR_SUCCESS;
  561. }
  562. //
  563. //
  564. //
  565. //
  566. ///////////////////////////////////////////////////////////////////////
  567. long CNamespaceHandle::s_lActiveRepNs = 0;
  568. CNamespaceHandle::CNamespaceHandle(CLifeControl* pControl,CRepository * pRepository)
  569. : TUnkBase(pControl), m_pClassCache(NULL),
  570. m_pNullClass(NULL), m_bCached(false), m_pRepository(pRepository),
  571. m_bUseIteratorLock(true)
  572. {
  573. m_pRepository->AddRef();
  574. // unrefed pointer to a global
  575. m_ForestCache = g_Glob.GetForestCache();
  576. InterlockedIncrement(&s_lActiveRepNs);
  577. }
  578. CNamespaceHandle::~CNamespaceHandle()
  579. {
  580. if(m_pClassCache)
  581. {
  582. // give-up our own reference
  583. // m_pClassCache->Release();
  584. // remove from the Forest cache this namespace
  585. m_ForestCache->ReleaseNamespaceCache(m_wsNamespace, m_pClassCache);
  586. }
  587. m_pRepository->Release();
  588. if(m_pNullClass)
  589. m_pNullClass->Release();
  590. InterlockedDecrement(&s_lActiveRepNs);
  591. }
  592. CHR CNamespaceHandle::GetErrorStatus()
  593. {
  594. //
  595. // TEMP CODE: Someone is calling us on an impersonated thread. Let's catch
  596. // the, ahem, culprit
  597. //
  598. HANDLE hToken;
  599. BOOL bRes = OpenThreadToken(GetCurrentThread(), TOKEN_READ, TRUE, &hToken);
  600. if(bRes)
  601. {
  602. //_ASSERT(false, L"Called with a thread token");
  603. ERRORTRACE((LOG_WBEMCORE, "Repository called with a thread token! "
  604. "It shall be removed\n"));
  605. CloseHandle(hToken);
  606. SetThreadToken(NULL, NULL);
  607. }
  608. return m_pClassCache->GetError();
  609. }
  610. void CNamespaceHandle::SetErrorStatus(HRESULT hres)
  611. {
  612. m_pClassCache->SetError(hres);
  613. }
  614. CHR CNamespaceHandle::Initialize(LPCWSTR wszNamespace, LPCWSTR wszScope)
  615. {
  616. HRESULT hres;
  617. m_wsNamespace = wszNamespace;
  618. m_wsFullNamespace = L"\\\\.\\";
  619. m_wsFullNamespace += wszNamespace;
  620. DWORD dwSize = MAX_COMPUTERNAME_LENGTH+1;
  621. GetComputerNameW(m_wszMachineName, &dwSize);
  622. if(wszScope)
  623. m_wsScope = wszScope;
  624. //
  625. // Ask the forest for the cache for this namespace
  626. //
  627. m_pClassCache = g_Glob.GetForestCache()->
  628. GetNamespaceCache(wszNamespace);
  629. if(m_pClassCache == NULL)
  630. return WBEM_E_OUT_OF_MEMORY;
  631. wcscpy(m_wszClassRootDir, g_Glob.GetRootDir());
  632. //
  633. // Append namespace-specific prefix
  634. //
  635. wcscat(m_wszClassRootDir, L"\\NS_");
  636. //
  637. // Append hashed namespace name
  638. //
  639. if (!Hash(wszNamespace, m_wszClassRootDir + wcslen(m_wszClassRootDir)))
  640. return WBEM_E_OUT_OF_MEMORY;
  641. m_lClassRootDirLen = wcslen(m_wszClassRootDir);
  642. //
  643. // Constuct the instance root dir
  644. //
  645. if(wszScope == NULL)
  646. {
  647. //
  648. // Basic namespace --- instances go into the root of the namespace
  649. //
  650. wcscpy(m_wszInstanceRootDir, m_wszClassRootDir);
  651. m_lInstanceRootDirLen = m_lClassRootDirLen;
  652. }
  653. else
  654. {
  655. wcscpy(m_wszInstanceRootDir, m_wszClassRootDir);
  656. wcscat(m_wszInstanceRootDir, L"\\" A51_SCOPE_DIR_PREFIX);
  657. if(!Hash(m_wsScope,
  658. m_wszInstanceRootDir + wcslen(m_wszInstanceRootDir)))
  659. {
  660. return WBEM_E_OUT_OF_MEMORY;
  661. }
  662. m_lInstanceRootDirLen = wcslen(m_wszInstanceRootDir);
  663. }
  664. return WBEM_S_NO_ERROR;
  665. }
  666. CHR CNamespaceHandle::GetObject(
  667. IWbemPath *pPath,
  668. DWORD dwFlags,
  669. DWORD dwRequestedHandleType,
  670. IWmiDbHandle **ppResult
  671. )
  672. {
  673. HRESULT hres;
  674. if((dwRequestedHandleType & WMIDB_HANDLE_TYPE_COOKIE) == 0)
  675. {
  676. DebugBreak();
  677. return E_NOTIMPL;
  678. }
  679. DWORD dwLen = 0;
  680. hres = pPath->GetText(WBEMPATH_GET_ORIGINAL, &dwLen, NULL);
  681. if(FAILED(hres) && hres != WBEM_E_BUFFER_TOO_SMALL)
  682. return hres;
  683. WCHAR* wszBuffer = (WCHAR*)TempAlloc(dwLen * sizeof(WCHAR));
  684. if(wszBuffer == NULL)
  685. return WBEM_E_OUT_OF_MEMORY;
  686. CTempFreeMe tfm(wszBuffer, dwLen * sizeof(WCHAR));
  687. if(FAILED(pPath->GetText(WBEMPATH_GET_ORIGINAL, &dwLen, wszBuffer)))
  688. return WBEM_E_FAILED;
  689. return GetObjectHandleByPath(wszBuffer, dwFlags, dwRequestedHandleType,
  690. ppResult);
  691. }
  692. CHR CNamespaceHandle::GetObjectHandleByPath(
  693. LPWSTR wszBuffer,
  694. DWORD dwFlags,
  695. DWORD dwRequestedHandleType,
  696. IWmiDbHandle **ppResult
  697. )
  698. {
  699. //
  700. // Get the key from path
  701. //
  702. DWORD dwLen = wcslen(wszBuffer)*sizeof(WCHAR)+2;
  703. LPWSTR wszKey = (WCHAR*)TempAlloc(dwLen);
  704. if(wszKey == NULL)
  705. return WBEM_E_OUT_OF_MEMORY;
  706. CTempFreeMe tfm(wszKey, dwLen);
  707. bool bIsClass;
  708. LPWSTR wszClassName = NULL;
  709. HRESULT hres = ComputeKeyFromPath(wszBuffer, wszKey, &wszClassName,
  710. &bIsClass);
  711. if(FAILED(hres))
  712. return hres;
  713. CTempFreeMe tfm1(wszClassName, (wcslen(wszClassName)+1) * sizeof(WCHAR*));
  714. //
  715. // Check if it exists (except for ROOT --- it's fake)
  716. //
  717. _IWmiObject* pObj = NULL;
  718. if(m_wsNamespace.Length() > 0)
  719. {
  720. hres = GetInstanceByKey(wszClassName, wszKey, IID__IWmiObject,
  721. (void**)&pObj);
  722. if(FAILED(hres))
  723. return hres;
  724. }
  725. CReleaseMe rm1(pObj);
  726. CNamespaceHandle* pNewHandle = new CNamespaceHandle(m_pControl,m_pRepository);
  727. if (pNewHandle == NULL)
  728. return WBEM_E_OUT_OF_MEMORY;
  729. pNewHandle->AddRef();
  730. CReleaseMe rm2(pNewHandle);
  731. //
  732. // Check if this is a namespace or not
  733. //
  734. if(pObj == NULL || pObj->InheritsFrom(L"__Namespace") == S_OK)
  735. {
  736. //
  737. // It's a namespace. Open a basic handle pointing to it
  738. //
  739. WString wsName = m_wsNamespace;
  740. if(wsName.Length() > 0)
  741. wsName += L"\\";
  742. wsName += wszKey;
  743. hres = pNewHandle->Initialize(wsName);
  744. //
  745. // Since our namespace is for real, tell the cache that it is now valid.
  746. // The cache might have been invalidated if this namespace was deleted
  747. // in the past
  748. //
  749. pNewHandle->SetErrorStatus(S_OK);
  750. }
  751. else
  752. {
  753. //
  754. // It's a scope. Construct the new scope name by appending this
  755. // object's path to our own scope
  756. //
  757. VARIANT v;
  758. VariantInit(&v);
  759. CClearMe cm(&v);
  760. hres = pObj->Get(L"__RELPATH", 0, &v, NULL, NULL);
  761. if(FAILED(hres))
  762. return hres;
  763. if(V_VT(&v) != VT_BSTR)
  764. return WBEM_E_INVALID_OBJECT;
  765. WString wsScope = m_wsScope;
  766. if(wsScope.Length() > 0)
  767. wsScope += L":";
  768. wsScope += V_BSTR(&v);
  769. hres = pNewHandle->Initialize(m_wsNamespace, wsScope);
  770. }
  771. if(FAILED(hres))
  772. return hres;
  773. return pNewHandle->QueryInterface(IID_IWmiDbHandle, (void**)ppResult);
  774. }
  775. CHR CNamespaceHandle::ComputeKeyFromPath(LPWSTR wszPath, LPWSTR wszKey,
  776. TEMPFREE_ME LPWSTR* pwszClass,
  777. bool* pbIsClass,
  778. TEMPFREE_ME LPWSTR* pwszNamespace)
  779. {
  780. HRESULT hres;
  781. *pbIsClass = false;
  782. //
  783. // Get and skip the namespace portion.
  784. //
  785. if(wszPath[0] == '\\' || wszPath[0] == '/')
  786. {
  787. //
  788. // Find where the server portion ends
  789. //
  790. WCHAR* pwcNextSlash = wcschr(wszPath+2, wszPath[0]);
  791. if(pwcNextSlash == NULL)
  792. return WBEM_E_INVALID_OBJECT_PATH;
  793. //
  794. // Find where the namespace portion ends
  795. //
  796. WCHAR* pwcColon = wcschr(pwcNextSlash, L':');
  797. if(pwcColon == NULL)
  798. return WBEM_E_INVALID_OBJECT_PATH;
  799. if(pwszNamespace)
  800. {
  801. DWORD dwLen = pwcColon - pwcNextSlash;
  802. *pwszNamespace = (WCHAR*)TempAlloc(dwLen * sizeof(WCHAR));
  803. if(*pwszNamespace == NULL)
  804. return WBEM_E_OUT_OF_MEMORY;
  805. *pwcColon = 0;
  806. wcscpy(*pwszNamespace, pwcNextSlash+1);
  807. }
  808. //
  809. // Advance wszPath to beyond the namespace portion
  810. //
  811. wszPath = pwcColon+1;
  812. }
  813. else if(pwszNamespace)
  814. {
  815. *pwszNamespace = NULL;
  816. }
  817. // Get the first key
  818. WCHAR* pwcFirstEq = wcschr(wszPath, L'=');
  819. if(pwcFirstEq == NULL)
  820. {
  821. //
  822. // It's a class!
  823. //
  824. *pbIsClass = true;
  825. // path to the "class" to distinguish from its instances
  826. wszKey[0] = 1;
  827. wszKey[1] = 0;
  828. *pwszClass = (WCHAR*)TempAlloc((wcslen(wszPath)+1) * sizeof(WCHAR));
  829. if(*pwszClass == NULL)
  830. {
  831. if(pwszNamespace)
  832. TempFree(*pwszNamespace);
  833. return WBEM_E_OUT_OF_MEMORY;
  834. }
  835. wcscpy(*pwszClass, wszPath);
  836. return S_OK;
  837. }
  838. WCHAR* pwcFirstDot = wcschr(wszPath, L'.');
  839. if(pwcFirstDot == NULL || pwcFirstDot > pwcFirstEq)
  840. {
  841. // No name on the first key
  842. *pwcFirstEq = 0;
  843. *pwszClass = (WCHAR*)TempAlloc((wcslen(wszPath)+1) * sizeof(WCHAR));
  844. if(*pwszClass == NULL)
  845. {
  846. if(pwszNamespace)
  847. TempFree(*pwszNamespace);
  848. return WBEM_E_OUT_OF_MEMORY;
  849. }
  850. wcscpy(*pwszClass, wszPath);
  851. WCHAR* pwcThisKey = NULL;
  852. WCHAR* pwcEnd = NULL;
  853. hres = ParseKey(pwcFirstEq+1, &pwcThisKey, &pwcEnd);
  854. if(FAILED(hres))
  855. {
  856. TempFree(*pwszClass);
  857. if(pwszNamespace)
  858. TempFree(*pwszNamespace);
  859. return hres;
  860. }
  861. if(*pwcEnd != NULL)
  862. {
  863. TempFree(*pwszClass);
  864. if(pwszNamespace)
  865. TempFree(*pwszNamespace);
  866. return WBEM_E_INVALID_OBJECT_PATH;
  867. }
  868. wcscpy(wszKey, pwcThisKey);
  869. return S_OK;
  870. }
  871. //
  872. // Normal case
  873. //
  874. //
  875. // Get all the key values
  876. //
  877. struct CKeyStruct
  878. {
  879. WCHAR* m_pwcValue;
  880. WCHAR* m_pwcName;
  881. } * aKeys = (CKeyStruct*)TempAlloc(sizeof(CKeyStruct[256]));
  882. if (0==aKeys)
  883. {
  884. if(pwszNamespace)
  885. TempFree(*pwszNamespace);
  886. return WBEM_E_OUT_OF_MEMORY;
  887. }
  888. CTempFreeMe release_aKeys(aKeys);
  889. DWORD dwNumKeys = 0;
  890. *pwcFirstDot = NULL;
  891. *pwszClass = (WCHAR*)TempAlloc((wcslen(wszPath)+1) * sizeof(WCHAR));
  892. if(*pwszClass == NULL)
  893. {
  894. if(pwszNamespace)
  895. TempFree(*pwszNamespace);
  896. return WBEM_E_OUT_OF_MEMORY;
  897. }
  898. wcscpy(*pwszClass, wszPath);
  899. WCHAR* pwcNextKey = pwcFirstDot+1;
  900. do
  901. {
  902. pwcFirstEq = wcschr(pwcNextKey, L'=');
  903. if(pwcFirstEq == NULL)
  904. {
  905. TempFree(*pwszClass);
  906. if(pwszNamespace)
  907. TempFree(*pwszNamespace);
  908. return WBEM_E_INVALID_OBJECT_PATH;
  909. }
  910. *pwcFirstEq = 0;
  911. aKeys[dwNumKeys].m_pwcName = pwcNextKey;
  912. hres = ParseKey(pwcFirstEq+1, &(aKeys[dwNumKeys].m_pwcValue),
  913. &pwcNextKey);
  914. if(FAILED(hres))
  915. {
  916. TempFree(*pwszClass);
  917. if(pwszNamespace)
  918. TempFree(*pwszNamespace);
  919. return hres;
  920. }
  921. dwNumKeys++;
  922. }
  923. while(*pwcNextKey);
  924. if(*pwcNextKey != 0)
  925. {
  926. TempFree(*pwszClass);
  927. if(pwszNamespace)
  928. TempFree(*pwszNamespace);
  929. return WBEM_E_INVALID_OBJECT_PATH;
  930. }
  931. //
  932. // We have the array of keys --- sort it
  933. //
  934. DWORD dwCurrentIndex = 0;
  935. while(dwCurrentIndex < dwNumKeys-1)
  936. {
  937. if(wbem_wcsicmp(aKeys[dwCurrentIndex].m_pwcName,
  938. aKeys[dwCurrentIndex+1].m_pwcName) > 0)
  939. {
  940. CKeyStruct Temp = aKeys[dwCurrentIndex];
  941. aKeys[dwCurrentIndex] = aKeys[dwCurrentIndex+1];
  942. aKeys[dwCurrentIndex+1] = Temp;
  943. if(dwCurrentIndex)
  944. dwCurrentIndex--;
  945. else
  946. dwCurrentIndex++;
  947. }
  948. else
  949. dwCurrentIndex++;
  950. }
  951. //
  952. // Now generate the result
  953. //
  954. WCHAR* pwcKeyEnd = wszKey;
  955. for(DWORD i = 0; i < dwNumKeys; i++)
  956. {
  957. wcscpy(pwcKeyEnd, aKeys[i].m_pwcValue);
  958. pwcKeyEnd += wcslen(aKeys[i].m_pwcValue);
  959. if(i < dwNumKeys-1)
  960. *(pwcKeyEnd++) = -1;
  961. }
  962. *pwcKeyEnd = 0;
  963. return S_OK;
  964. }
  965. CHR CNamespaceHandle::ParseKey(LPWSTR wszKeyStart, LPWSTR* pwcRealStart,
  966. LPWSTR* pwcNextKey)
  967. {
  968. if(wszKeyStart[0] == L'"' || wszKeyStart[0] == L'\'')
  969. {
  970. WCHAR wcStart = wszKeyStart[0];
  971. WCHAR* pwcRead = wszKeyStart+1;
  972. WCHAR* pwcWrite = wszKeyStart+1;
  973. while(*pwcRead && *pwcRead != wcStart)
  974. {
  975. if(*pwcRead == '\\')
  976. pwcRead++;
  977. *(pwcWrite++) = *(pwcRead++);
  978. }
  979. if(*pwcRead == 0)
  980. return WBEM_E_INVALID_OBJECT_PATH;
  981. *pwcWrite = 0;
  982. if(pwcRealStart)
  983. *pwcRealStart = wszKeyStart+1;
  984. //
  985. // Check separator
  986. //
  987. if(pwcRead[1] && pwcRead[1] != L',')
  988. return WBEM_E_INVALID_OBJECT_PATH;
  989. if(pwcNextKey)
  990. {
  991. //
  992. // If there is a separator, skip it. Don't skip end of string!
  993. //
  994. if(pwcRead[1])
  995. *pwcNextKey = pwcRead+2;
  996. else
  997. *pwcNextKey = pwcRead+1;
  998. }
  999. }
  1000. else
  1001. {
  1002. if(pwcRealStart)
  1003. *pwcRealStart = wszKeyStart;
  1004. WCHAR* pwcComma = wcschr(wszKeyStart, L',');
  1005. if(pwcComma == NULL)
  1006. {
  1007. if(pwcNextKey)
  1008. *pwcNextKey = wszKeyStart + wcslen(wszKeyStart);
  1009. }
  1010. else
  1011. {
  1012. *pwcComma = 0;
  1013. if(pwcNextKey)
  1014. *pwcNextKey = pwcComma+1;
  1015. }
  1016. }
  1017. return S_OK;
  1018. }
  1019. CHR CNamespaceHandle::GetObjectDirect(
  1020. IWbemPath *pPath,
  1021. DWORD dwFlags,
  1022. REFIID riid,
  1023. LPVOID *pObj
  1024. )
  1025. {
  1026. HRESULT hres;
  1027. DWORD dwLen = 0;
  1028. hres = pPath->GetText(WBEMPATH_GET_ORIGINAL, &dwLen, NULL);
  1029. LPWSTR wszPath = (WCHAR*)TempAlloc(dwLen*sizeof(WCHAR));
  1030. if (wszPath == NULL)
  1031. return WBEM_E_OUT_OF_MEMORY;
  1032. CTempFreeMe vdm(wszPath, dwLen * sizeof(WCHAR));
  1033. hres = pPath->GetText(WBEMPATH_GET_ORIGINAL, &dwLen, wszPath);
  1034. if(FAILED(hres))
  1035. return hres;
  1036. return GetObjectByPath(wszPath, dwFlags, riid, pObj);
  1037. }
  1038. CHR CNamespaceHandle::GetObjectByPath(
  1039. LPWSTR wszPath,
  1040. DWORD dwFlags,
  1041. REFIID riid,
  1042. LPVOID *pObj
  1043. )
  1044. {
  1045. HRESULT hres;
  1046. //
  1047. // Get the key from path
  1048. //
  1049. DWORD dwLen = wcslen(wszPath)*sizeof(WCHAR)+2;
  1050. LPWSTR wszKey = (WCHAR*)TempAlloc(dwLen);
  1051. if(wszKey == NULL)
  1052. return WBEM_E_OUT_OF_MEMORY;
  1053. CTempFreeMe tfm(wszKey, dwLen);
  1054. bool bIsClass;
  1055. LPWSTR wszClassName = NULL;
  1056. hres = ComputeKeyFromPath(wszPath, wszKey, &wszClassName, &bIsClass);
  1057. if(FAILED(hres))
  1058. return hres;
  1059. CTempFreeMe tfm1(wszClassName, (wcslen(wszClassName)+1) * sizeof(WCHAR*));
  1060. if(bIsClass)
  1061. {
  1062. return GetClassDirect(wszClassName, riid, pObj, true, NULL, NULL, NULL);
  1063. }
  1064. else
  1065. {
  1066. return GetInstanceByKey(wszClassName, wszKey, riid, pObj);
  1067. }
  1068. }
  1069. CHR CNamespaceHandle::GetInstanceByKey(LPCWSTR wszClassName,
  1070. LPCWSTR wszKey,
  1071. REFIID riid, void** ppObj)
  1072. {
  1073. HRESULT hres;
  1074. //
  1075. // Get the class definition
  1076. //
  1077. _IWmiObject* pClass = NULL;
  1078. hres = GetClassDirect(wszClassName, IID__IWmiObject, (void**)&pClass,
  1079. false, NULL, NULL, NULL);
  1080. if(FAILED(hres))
  1081. return hres;
  1082. CReleaseMe rm1(pClass);
  1083. //
  1084. // Construct directory path
  1085. //
  1086. CFileName wszFilePath;
  1087. if (wszFilePath == NULL)
  1088. return WBEM_E_OUT_OF_MEMORY;
  1089. hres = ConstructKeyRootDirFromClass(wszFilePath, wszClassName);
  1090. if(FAILED(hres))
  1091. return hres;
  1092. //
  1093. // Construct the file path
  1094. //
  1095. int nLen = wcslen(wszFilePath);
  1096. wszFilePath[nLen] = L'\\';
  1097. hres = ConstructInstanceDefName(wszFilePath+nLen+1, wszKey);
  1098. if(FAILED(hres))
  1099. return hres;
  1100. //
  1101. // Get the object from that file
  1102. //
  1103. _IWmiObject* pInst;
  1104. hres = FileToInstance(wszFilePath, &pInst);
  1105. if(FAILED(hres))
  1106. return hres;
  1107. CReleaseMe rm2(pInst);
  1108. //
  1109. // Return
  1110. //
  1111. return pInst->QueryInterface(riid, (void**)ppObj);
  1112. }
  1113. CHR CNamespaceHandle::GetClassByHash(LPCWSTR wszHash, bool bClone,
  1114. _IWmiObject** ppClass,
  1115. __int64* pnTime,
  1116. bool* pbRead,
  1117. bool *pbSystemClass)
  1118. {
  1119. HRESULT hres;
  1120. //
  1121. // Check the cache first
  1122. //
  1123. *ppClass = m_pClassCache->GetClassDefByHash(wszHash, bClone, pnTime, pbRead, pbSystemClass);
  1124. if(*ppClass)
  1125. return S_OK;
  1126. //
  1127. // Not found --- construct the file name and read it
  1128. //
  1129. if(pbRead)
  1130. *pbRead = true;
  1131. CFileName wszFileName;
  1132. if (wszFileName == NULL)
  1133. return WBEM_E_OUT_OF_MEMORY;
  1134. hres = ConstructClassDefFileNameFromHash(wszHash, wszFileName);
  1135. if(FAILED(hres))
  1136. return hres;
  1137. CFileName wszFilePath;
  1138. if (wszFilePath == NULL)
  1139. return WBEM_E_OUT_OF_MEMORY;
  1140. swprintf(wszFilePath, L"%s\\%s", m_wszClassRootDir, wszFileName);
  1141. hres = FileToClass(wszFilePath, ppClass, bClone, pnTime, pbSystemClass);
  1142. if(FAILED(hres))
  1143. return hres;
  1144. return S_OK;
  1145. }
  1146. CHR CNamespaceHandle::GetClassDirect(LPCWSTR wszClassName,
  1147. REFIID riid, void** ppObj, bool bClone,
  1148. __int64* pnTime, bool* pbRead,
  1149. bool *pbSystemClass)
  1150. {
  1151. HRESULT hres;
  1152. if(wszClassName == NULL || wcslen(wszClassName) == 0)
  1153. {
  1154. if(m_pNullClass == NULL)
  1155. {
  1156. hres = CoCreateInstance(CLSID_WbemClassObject, NULL,
  1157. CLSCTX_INPROC_SERVER,
  1158. IID__IWmiObject, (void **)&m_pNullClass);
  1159. if (FAILED(hres))
  1160. return hres;
  1161. }
  1162. IWbemClassObject* pRawObj;
  1163. hres = m_pNullClass->Clone(&pRawObj);
  1164. if (FAILED(hres))
  1165. return hres;
  1166. CReleaseMe rm(pRawObj);
  1167. if(pnTime)
  1168. *pnTime = 0;
  1169. if(pbRead)
  1170. *pbRead = false;
  1171. return pRawObj->QueryInterface(riid, ppObj);
  1172. }
  1173. _IWmiObject* pClass;
  1174. //
  1175. // Check the cache first
  1176. //
  1177. pClass = m_pClassCache->GetClassDef(wszClassName, bClone, pnTime, pbRead);
  1178. if(pClass)
  1179. {
  1180. CReleaseMe rm1(pClass);
  1181. return pClass->QueryInterface(riid, ppObj);
  1182. }
  1183. if(pbRead)
  1184. *pbRead = true;
  1185. //
  1186. // Construct the path for the file
  1187. //
  1188. CFileName wszFileName;
  1189. if (wszFileName == NULL)
  1190. return WBEM_E_OUT_OF_MEMORY;
  1191. hres = ConstructClassDefFileName(wszClassName, wszFileName);
  1192. if(FAILED(hres))
  1193. return hres;
  1194. CFileName wszFilePath;
  1195. if (wszFilePath == NULL)
  1196. return WBEM_E_OUT_OF_MEMORY;
  1197. swprintf(wszFilePath, L"%s\\%s", m_wszClassRootDir, wszFileName);
  1198. //
  1199. // Read it from the file
  1200. //
  1201. hres = FileToClass(wszFilePath, &pClass, bClone, pnTime, pbSystemClass);
  1202. if(FAILED(hres))
  1203. return hres;
  1204. CReleaseMe rm1(pClass);
  1205. return pClass->QueryInterface(riid, ppObj);
  1206. }
  1207. CHR CNamespaceHandle::FileToInstance(LPCWSTR wszFileName,
  1208. _IWmiObject** ppInstance, bool bMustBeThere)
  1209. {
  1210. HRESULT hres;
  1211. //
  1212. // Read the data from the file
  1213. //
  1214. DWORD dwSize;
  1215. BYTE* pBlob;
  1216. long lRes = g_Glob.GetFileCache()->ReadFile(wszFileName, &dwSize, &pBlob,
  1217. bMustBeThere);
  1218. if(lRes != ERROR_SUCCESS)
  1219. {
  1220. if(lRes == ERROR_FILE_NOT_FOUND || lRes == ERROR_PATH_NOT_FOUND)
  1221. return WBEM_E_NOT_FOUND;
  1222. else
  1223. return WBEM_E_FAILED;
  1224. }
  1225. CTempFreeMe tfm1(pBlob, dwSize);
  1226. _ASSERT(dwSize > sizeof(__int64), L"Instance blob too short");
  1227. if(dwSize <= sizeof(__int64))
  1228. return WBEM_E_OUT_OF_MEMORY;
  1229. //
  1230. // Extract the class hash
  1231. //
  1232. WCHAR wszClassHash[MAX_HASH_LEN+1];
  1233. DWORD dwClassHashLen = MAX_HASH_LEN*sizeof(WCHAR);
  1234. memcpy(wszClassHash, pBlob, MAX_HASH_LEN*sizeof(WCHAR));
  1235. wszClassHash[MAX_HASH_LEN] = 0;
  1236. __int64 nInstanceTime;
  1237. memcpy(&nInstanceTime, pBlob + dwClassHashLen, sizeof(__int64));
  1238. __int64 nOldClassTime;
  1239. memcpy(&nOldClassTime, pBlob + dwClassHashLen + sizeof(__int64),
  1240. sizeof(__int64));
  1241. BYTE* pInstancePart = pBlob + dwClassHashLen + sizeof(__int64)*2;
  1242. DWORD dwInstancePartSize = dwSize - dwClassHashLen - sizeof(__int64)*2;
  1243. //
  1244. // Get the class def
  1245. //
  1246. _IWmiObject* pClass = NULL;
  1247. __int64 nClassTime;
  1248. bool bRead;
  1249. bool bSystemClass = false;
  1250. hres = GetClassByHash(wszClassHash, false, &pClass, &nClassTime, &bRead, &bSystemClass);
  1251. if(FAILED(hres))
  1252. return hres;
  1253. CReleaseMe rm1(pClass);
  1254. #ifdef A51_CHECK_TIMESTAMPS
  1255. _ASSERT(nClassTime <= nInstanceTime, L"Instance is older than its class");
  1256. _ASSERT(nClassTime == nOldClassTime, L"Instance verified with the wrong "
  1257. L"class definition");
  1258. #endif
  1259. //
  1260. // Construct the instance
  1261. //
  1262. _IWmiObject* pInst = NULL;
  1263. hres = pClass->Merge(WMIOBJECT_MERGE_FLAG_INSTANCE,
  1264. dwInstancePartSize, pInstancePart, &pInst);
  1265. if(FAILED(hres))
  1266. return hres;
  1267. //
  1268. // Decorate it
  1269. //
  1270. pInst->SetDecoration(m_wszMachineName, m_wsNamespace);
  1271. A51TRACE(("Read instance from %S in namespace %S\n",
  1272. wszFileName, (LPCWSTR)m_wsNamespace));
  1273. *ppInstance = pInst;
  1274. return S_OK;
  1275. }
  1276. CHR CNamespaceHandle::FileToSystemClass(LPCWSTR wszFileName,
  1277. _IWmiObject** ppClass, bool bClone,
  1278. __int64* pnTime)
  1279. {
  1280. //
  1281. // Note: we must always clone the result of the system class retrieval,
  1282. // since it will be decorated by the caller
  1283. //
  1284. return GetClassByHash(wszFileName + (wcslen(wszFileName) - MAX_HASH_LEN),
  1285. true,
  1286. ppClass, pnTime, NULL, NULL);
  1287. }
  1288. CHR CNamespaceHandle::FileToClass(LPCWSTR wszFileName,
  1289. _IWmiObject** ppClass, bool bClone,
  1290. __int64* pnTime, bool *pbSystemClass)
  1291. {
  1292. HRESULT hres;
  1293. //
  1294. // Read the data from the file
  1295. //
  1296. __int64 nTime;
  1297. DWORD dwSize;
  1298. BYTE* pBlob;
  1299. VARIANT vClass;
  1300. long lRes = g_Glob.GetFileCache()->ReadFile(wszFileName, &dwSize, &pBlob);
  1301. if(lRes != ERROR_SUCCESS)
  1302. {
  1303. //We didn't find it here, so lets try and find it in the default namespace!
  1304. //If we are not in the __SYSTEMCLASS namespace then we need to call into that...
  1305. if((lRes == ERROR_FILE_NOT_FOUND || lRes == ERROR_PATH_NOT_FOUND) && g_pSystemClassNamespace && wcscmp(m_wsNamespace, A51_SYSTEMCLASS_NS) != 0)
  1306. {
  1307. hres = g_pSystemClassNamespace->FileToSystemClass(wszFileName, ppClass, bClone, &nTime);
  1308. if (FAILED(hres))
  1309. return hres;
  1310. if (pnTime)
  1311. *pnTime = nTime;
  1312. //need to cache this item in the local cache
  1313. hres = (*ppClass)->Get(L"__CLASS", 0, &vClass, NULL, NULL);
  1314. if(FAILED(hres) || V_VT(&vClass) != VT_BSTR)
  1315. return WBEM_E_INVALID_OBJECT;
  1316. CClearMe cm1(&vClass);
  1317. A51TRACE(("Read class %S from disk in namespace %S\n", V_BSTR(&vClass), m_wsNamespace));
  1318. (*ppClass)->SetDecoration(m_wszMachineName, m_wsNamespace);
  1319. m_pClassCache->AssertClass((*ppClass), V_BSTR(&vClass), bClone, nTime, true);
  1320. if (pbSystemClass)
  1321. *pbSystemClass = true;
  1322. return hres;
  1323. }
  1324. else if (lRes == ERROR_FILE_NOT_FOUND || lRes == ERROR_PATH_NOT_FOUND)
  1325. return WBEM_E_NOT_FOUND;
  1326. else
  1327. return WBEM_E_FAILED;
  1328. }
  1329. CTempFreeMe tfm1(pBlob, dwSize);
  1330. _ASSERT(dwSize > sizeof(__int64), L"Class blob too short");
  1331. if(dwSize <= sizeof(__int64))
  1332. return WBEM_E_OUT_OF_MEMORY;
  1333. //
  1334. // Read off the superclass name
  1335. //
  1336. DWORD dwSuperLen;
  1337. memcpy(&dwSuperLen, pBlob, sizeof(DWORD));
  1338. LPWSTR wszSuperClass = (WCHAR*)TempAlloc(dwSuperLen*sizeof(WCHAR)+2);
  1339. if (wszSuperClass == NULL)
  1340. return WBEM_E_OUT_OF_MEMORY;
  1341. CTempFreeMe vdm1(wszSuperClass, dwSuperLen*sizeof(WCHAR)+2);
  1342. wszSuperClass[dwSuperLen] = 0;
  1343. memcpy(wszSuperClass, pBlob+sizeof(DWORD), dwSuperLen*sizeof(WCHAR));
  1344. DWORD dwPrefixLen = sizeof(DWORD) + dwSuperLen*sizeof(WCHAR);
  1345. memcpy(&nTime, pBlob + dwPrefixLen, sizeof(__int64));
  1346. //
  1347. // Get the superclass
  1348. //
  1349. _IWmiObject* pSuperClass;
  1350. __int64 nSuperTime;
  1351. bool bRead;
  1352. hres = GetClassDirect(wszSuperClass, IID__IWmiObject, (void**)&pSuperClass,
  1353. false, &nSuperTime, &bRead, NULL);
  1354. if(FAILED(hres))
  1355. return WBEM_E_CRITICAL_ERROR;
  1356. CReleaseMe rm1(pSuperClass);
  1357. #ifdef A51_CHECK_TIMESTAMPS
  1358. _ASSERT(nSuperTime <= nTime, L"Parent class is older than child");
  1359. #endif
  1360. DWORD dwClassLen = dwSize - dwPrefixLen - sizeof(__int64);
  1361. _IWmiObject* pNewObj;
  1362. hres = pSuperClass->Merge(0, dwClassLen,
  1363. pBlob + dwPrefixLen + sizeof(__int64), &pNewObj);
  1364. if(FAILED(hres))
  1365. return hres;
  1366. //
  1367. // Decorate it
  1368. //
  1369. pNewObj->SetDecoration(m_wszMachineName, m_wsNamespace);
  1370. //
  1371. // Cache it!
  1372. //
  1373. hres = pNewObj->Get(L"__CLASS", 0, &vClass, NULL, NULL);
  1374. if(FAILED(hres) || V_VT(&vClass) != VT_BSTR)
  1375. return WBEM_E_INVALID_OBJECT;
  1376. CClearMe cm1(&vClass);
  1377. A51TRACE(("Read class %S from disk in namespace %S\n",
  1378. V_BSTR(&vClass), m_wsNamespace));
  1379. m_pClassCache->AssertClass(pNewObj, V_BSTR(&vClass), bClone, nTime, false);
  1380. *ppClass = pNewObj;
  1381. if(pnTime)
  1382. *pnTime = nTime;
  1383. if (pbSystemClass)
  1384. *pbSystemClass = false;
  1385. return S_OK;
  1386. }
  1387. CHR CNamespaceHandle::PutObject(
  1388. REFIID riid,
  1389. LPVOID pObj,
  1390. DWORD dwFlags,
  1391. DWORD dwRequestedHandleType,
  1392. IWmiDbHandle **ppResult,
  1393. CEventCollector &aEvents
  1394. )
  1395. {
  1396. HRESULT hres;
  1397. _IWmiObject* pObjEx = NULL;
  1398. ((IUnknown*)pObj)->QueryInterface(IID__IWmiObject, (void**)&pObjEx);
  1399. CReleaseMe rm1(pObjEx);
  1400. if(pObjEx->IsObjectInstance() == S_OK)
  1401. {
  1402. hres = PutInstance(pObjEx, dwFlags, aEvents);
  1403. }
  1404. else
  1405. {
  1406. hres = PutClass(pObjEx, dwFlags, aEvents);
  1407. }
  1408. if(FAILED(hres))
  1409. return hres;
  1410. if(ppResult)
  1411. {
  1412. //
  1413. // Got to get a handle
  1414. //
  1415. VARIANT v;
  1416. hres = pObjEx->Get(L"__RELPATH", 0, &v, NULL, NULL);
  1417. if(FAILED(hres) || V_VT(&v) != VT_BSTR)
  1418. return WBEM_E_INVALID_OBJECT;
  1419. hres = GetObjectHandleByPath(V_BSTR(&v), 0, WMIDB_HANDLE_TYPE_COOKIE,
  1420. ppResult);
  1421. if(FAILED(hres))
  1422. return hres;
  1423. }
  1424. return S_OK;
  1425. }
  1426. CHR CNamespaceHandle::PutInstance(_IWmiObject* pInst, DWORD dwFlags,
  1427. CEventCollector &aEvents)
  1428. {
  1429. HRESULT hres;
  1430. bool bDisableEvents = ((dwFlags & WMIDB_DISABLE_EVENTS)?true:false);
  1431. //
  1432. // Get the class name
  1433. //
  1434. VARIANT vClass;
  1435. hres = pInst->Get(L"__CLASS", 0, &vClass, NULL, NULL);
  1436. if(FAILED(hres) || V_VT(&vClass) != VT_BSTR)
  1437. return WBEM_E_INVALID_OBJECT;
  1438. CClearMe cm1(&vClass);
  1439. LPCWSTR wszClassName = V_BSTR(&vClass);
  1440. //
  1441. // Get the class so we can compare to make sure it is the same class used to
  1442. // create the instance
  1443. //
  1444. _IWmiObject* pClass = NULL;
  1445. __int64 nClassTime;
  1446. hres = GetClassDirect(wszClassName, IID__IWmiObject, (void**)&pClass,
  1447. false, &nClassTime, NULL, NULL);
  1448. if(FAILED(hres))
  1449. return hres;
  1450. CReleaseMe rm2(pClass);
  1451. if(wszClassName[0] != L'_')
  1452. {
  1453. hres = pInst->IsParentClass(0, pClass);
  1454. if(FAILED(hres))
  1455. return hres;
  1456. if(hres == WBEM_S_FALSE)
  1457. return WBEM_E_INVALID_CLASS;
  1458. }
  1459. //
  1460. // Get the path
  1461. //
  1462. BSTR strKey = NULL;
  1463. hres = pInst->GetKeyString(0, &strKey);
  1464. if(FAILED(hres))
  1465. return hres;
  1466. CSysFreeMe sfm(strKey);
  1467. A51TRACE(("Putting instance %S of class %S\n", strKey, wszClassName));
  1468. //
  1469. // Get the old copy
  1470. //
  1471. _IWmiObject* pOldInst = NULL;
  1472. hres = GetInstanceByKey(wszClassName, strKey, IID__IWmiObject,
  1473. (void**)&pOldInst);
  1474. if(FAILED(hres) && hres != WBEM_E_NOT_FOUND)
  1475. return hres;
  1476. CReleaseMe rm1(pOldInst);
  1477. if ((dwFlags & WBEM_FLAG_CREATE_ONLY) && (hres != WBEM_E_NOT_FOUND))
  1478. return WBEM_E_ALREADY_EXISTS;
  1479. else if ((dwFlags & WBEM_FLAG_UPDATE_ONLY) && (hres != WBEM_S_NO_ERROR))
  1480. return WBEM_E_NOT_FOUND;
  1481. if(pOldInst)
  1482. {
  1483. //
  1484. // Check that this guy is of the same class as the new one
  1485. //
  1486. //
  1487. // Get the class name
  1488. //
  1489. VARIANT vClass;
  1490. hres = pOldInst->Get(L"__CLASS", 0, &vClass, NULL, NULL);
  1491. if(FAILED(hres))
  1492. return hres;
  1493. if(V_VT(&vClass) != VT_BSTR)
  1494. return WBEM_E_INVALID_OBJECT;
  1495. CClearMe cm1(&vClass);
  1496. if(wbem_wcsicmp(V_BSTR(&vClass), wszClassName))
  1497. return WBEM_E_INVALID_CLASS;
  1498. }
  1499. //
  1500. // Construct the hash for the file
  1501. //
  1502. CFileName wszInstanceHash;
  1503. if (wszInstanceHash == NULL)
  1504. return WBEM_E_OUT_OF_MEMORY;
  1505. if(!Hash(strKey, wszInstanceHash))
  1506. return WBEM_E_OUT_OF_MEMORY;
  1507. //
  1508. // Construct the path to the instance file in key root
  1509. //
  1510. CFileName wszInstanceFilePath;
  1511. if (wszInstanceFilePath == NULL)
  1512. return WBEM_E_OUT_OF_MEMORY;
  1513. hres = ConstructKeyRootDirFromClass(wszInstanceFilePath, wszClassName);
  1514. if(FAILED(hres))
  1515. return hres;
  1516. wcscat(wszInstanceFilePath, L"\\" A51_INSTDEF_FILE_PREFIX);
  1517. wcscat(wszInstanceFilePath, wszInstanceHash);
  1518. //
  1519. // Clean up what was there, if anything
  1520. //
  1521. if(pOldInst)
  1522. {
  1523. //
  1524. // Just delete it, but be careful not to delete the scope!
  1525. //
  1526. hres = DeleteInstanceSelf(wszInstanceFilePath, pOldInst, false);
  1527. if(FAILED(hres) && hres != WBEM_E_NOT_FOUND)
  1528. return hres;
  1529. }
  1530. //
  1531. // Create the actual instance def under key root
  1532. //
  1533. hres = InstanceToFile(pInst, wszClassName, wszInstanceFilePath, nClassTime);
  1534. if(FAILED(hres))
  1535. return hres;
  1536. //
  1537. // Create the link under the class
  1538. //
  1539. hres = WriteInstanceLinkByHash(wszClassName, wszInstanceHash);
  1540. if(FAILED(hres))
  1541. return hres;
  1542. //
  1543. // Write the references
  1544. //
  1545. hres = WriteInstanceReferences(pInst, wszClassName, wszInstanceFilePath);
  1546. if(FAILED(hres))
  1547. return hres;
  1548. if(!bDisableEvents)
  1549. {
  1550. //
  1551. // Fire Event
  1552. //
  1553. if(pInst->InheritsFrom(L"__Namespace") == S_OK)
  1554. {
  1555. //
  1556. // Get the namespace name
  1557. //
  1558. VARIANT vClass;
  1559. VariantInit(&vClass);
  1560. CClearMe cm1(&vClass);
  1561. hres = pInst->Get(L"Name", 0, &vClass, NULL, NULL);
  1562. if(FAILED(hres) || V_VT(&vClass) != VT_BSTR)
  1563. return WBEM_E_INVALID_OBJECT;
  1564. if(pOldInst)
  1565. {
  1566. hres = FireEvent(aEvents, WBEM_EVENTTYPE_NamespaceModification,
  1567. V_BSTR(&vClass), pInst, pOldInst);
  1568. }
  1569. else
  1570. {
  1571. hres = FireEvent(aEvents, WBEM_EVENTTYPE_NamespaceCreation,
  1572. V_BSTR(&vClass), pInst);
  1573. }
  1574. }
  1575. else
  1576. {
  1577. if(pOldInst)
  1578. {
  1579. hres = FireEvent(aEvents, WBEM_EVENTTYPE_InstanceModification,
  1580. wszClassName, pInst, pOldInst);
  1581. }
  1582. else
  1583. {
  1584. hres = FireEvent(aEvents, WBEM_EVENTTYPE_InstanceCreation,
  1585. wszClassName, pInst);
  1586. }
  1587. }
  1588. }
  1589. A51TRACE(("PutInstance for %S of class %S succeeded\n",
  1590. strKey, wszClassName));
  1591. return S_OK;
  1592. }
  1593. CHR CNamespaceHandle::GetKeyRoot(LPCWSTR wszClass,
  1594. TEMPFREE_ME LPWSTR* pwszKeyRootClass)
  1595. {
  1596. HRESULT hres;
  1597. //
  1598. // Look in the cache first
  1599. //
  1600. hres = m_pClassCache->GetKeyRoot(wszClass, pwszKeyRootClass);
  1601. if(hres == S_OK)
  1602. return S_OK;
  1603. else if(hres == WBEM_E_CANNOT_BE_ABSTRACT)
  1604. return WBEM_E_CANNOT_BE_ABSTRACT;
  1605. //
  1606. // Walk up the tree getting classes until you hit an unkeyed one
  1607. //
  1608. WString wsThisName = wszClass;
  1609. WString wsPreviousName;
  1610. while(1)
  1611. {
  1612. _IWmiObject* pClass = NULL;
  1613. hres = GetClassDirect(wsThisName, IID__IWmiObject, (void**)&pClass,
  1614. false, NULL, NULL, NULL);
  1615. if(FAILED(hres))
  1616. return hres;
  1617. CReleaseMe rm1(pClass);
  1618. //
  1619. // Check if this class is keyed
  1620. //
  1621. unsigned __int64 i64Flags = 0;
  1622. hres = pClass->QueryObjectFlags(0, WMIOBJECT_GETOBJECT_LOFLAG_KEYED,
  1623. &i64Flags);
  1624. if(FAILED(hres))
  1625. return hres;
  1626. if(i64Flags == 0)
  1627. {
  1628. //
  1629. // It is not keyed --- the previous class wins!
  1630. //
  1631. if(wsPreviousName.Length() == 0)
  1632. return WBEM_E_CANNOT_BE_ABSTRACT;
  1633. DWORD dwLen = (wsPreviousName.Length()+1)*sizeof(WCHAR);
  1634. *pwszKeyRootClass = (WCHAR*)TempAlloc(dwLen);
  1635. if (*pwszKeyRootClass == NULL)
  1636. return WBEM_E_OUT_OF_MEMORY;
  1637. wcscpy(*pwszKeyRootClass, (LPCWSTR)wsPreviousName);
  1638. return S_OK;
  1639. }
  1640. //
  1641. // It is keyed --- get the parent and continue;
  1642. //
  1643. VARIANT vParent;
  1644. VariantInit(&vParent);
  1645. CClearMe cm(&vParent);
  1646. hres = pClass->Get(L"__SUPERCLASS", 0, &vParent, NULL, NULL);
  1647. if(FAILED(hres))
  1648. return hres;
  1649. if(V_VT(&vParent) != VT_BSTR)
  1650. {
  1651. //
  1652. // We've reached the top --- return this class
  1653. //
  1654. DWORD dwLen = (wsThisName.Length()+1)*sizeof(WCHAR);
  1655. *pwszKeyRootClass = (WCHAR*)TempAlloc(dwLen);
  1656. if (*pwszKeyRootClass == NULL)
  1657. return WBEM_E_OUT_OF_MEMORY;
  1658. wcscpy(*pwszKeyRootClass, (LPCWSTR)wsThisName);
  1659. return S_OK;
  1660. }
  1661. wsPreviousName = wsThisName;
  1662. wsThisName = V_BSTR(&vParent);
  1663. }
  1664. // Never here
  1665. DebugBreak();
  1666. return WBEM_E_CRITICAL_ERROR;
  1667. }
  1668. CHR CNamespaceHandle::GetKeyRootByHash(LPCWSTR wszClassHash,
  1669. TEMPFREE_ME LPWSTR* pwszKeyRootClass)
  1670. {
  1671. //
  1672. // Look in the cache first
  1673. //
  1674. HRESULT hres = m_pClassCache->GetKeyRootByKey(wszClassHash,
  1675. pwszKeyRootClass);
  1676. if(hres == S_OK)
  1677. return S_OK;
  1678. else if(hres == WBEM_E_CANNOT_BE_ABSTRACT)
  1679. return WBEM_E_CANNOT_BE_ABSTRACT;
  1680. //
  1681. // NOTE: this could be done more efficiently, but it happens once in a
  1682. // lifetime, so it's not worth the complexity.
  1683. //
  1684. //
  1685. // Get Class definition
  1686. //
  1687. _IWmiObject* pClass = NULL;
  1688. hres = GetClassByHash(wszClassHash, false, &pClass, NULL, NULL, NULL);
  1689. if(FAILED(hres))
  1690. return hres;
  1691. CReleaseMe rm1(pClass);
  1692. //
  1693. // Get the class name
  1694. //
  1695. VARIANT vClass;
  1696. hres = pClass->Get(L"__CLASS", 0, &vClass, NULL, NULL);
  1697. if(FAILED(hres) || (V_VT(&vClass) != VT_BSTR) ||
  1698. !V_BSTR(&vClass) || !wcslen(V_BSTR(&vClass)))
  1699. {
  1700. return WBEM_E_INVALID_OBJECT;
  1701. }
  1702. CClearMe cm1(&vClass);
  1703. LPCWSTR wszClassName = V_BSTR(&vClass);
  1704. //
  1705. // Now get it by name
  1706. //
  1707. return GetKeyRoot(wszClassName, pwszKeyRootClass);
  1708. }
  1709. CHR CNamespaceHandle::ConstructKeyRootDirFromClass(LPWSTR wszDir,
  1710. LPCWSTR wszClassName)
  1711. {
  1712. HRESULT hres;
  1713. //
  1714. // NULL class stands for "meta-class"
  1715. //
  1716. if(wszClassName == NULL)
  1717. return ConstructKeyRootDirFromKeyRoot(wszDir, L"");
  1718. //
  1719. // Figure out the key root for the class
  1720. //
  1721. LPWSTR wszKeyRootClass = NULL;
  1722. hres = GetKeyRoot(wszClassName, &wszKeyRootClass);
  1723. if(FAILED(hres))
  1724. return hres;
  1725. if(wszKeyRootClass == NULL)
  1726. {
  1727. // Abstract class --- bad error
  1728. return WBEM_E_INVALID_CLASS;
  1729. }
  1730. CTempFreeMe tfm(wszKeyRootClass, (wcslen(wszKeyRootClass)+1)*sizeof(WCHAR));
  1731. return ConstructKeyRootDirFromKeyRoot(wszDir, wszKeyRootClass);
  1732. }
  1733. CHR CNamespaceHandle::ConstructKeyRootDirFromClassHash(LPWSTR wszDir,
  1734. LPCWSTR wszClassHash)
  1735. {
  1736. HRESULT hres;
  1737. //
  1738. // Figure out the key root for the class
  1739. //
  1740. LPWSTR wszKeyRootClass = NULL;
  1741. hres = GetKeyRootByHash(wszClassHash, &wszKeyRootClass);
  1742. if(FAILED(hres))
  1743. return hres;
  1744. if(wszKeyRootClass == NULL)
  1745. {
  1746. // Abstract class --- bad error
  1747. return WBEM_E_INVALID_CLASS;
  1748. }
  1749. CTempFreeMe tfm(wszKeyRootClass, (wcslen(wszKeyRootClass)+1)*sizeof(WCHAR));
  1750. return ConstructKeyRootDirFromKeyRoot(wszDir, wszKeyRootClass);
  1751. }
  1752. CHR CNamespaceHandle::ConstructKeyRootDirFromKeyRoot(LPWSTR wszDir,
  1753. LPCWSTR wszKeyRootClass)
  1754. {
  1755. wcscpy(wszDir, m_wszInstanceRootDir);
  1756. wszDir[m_lInstanceRootDirLen] = L'\\';
  1757. wcscpy(wszDir+m_lInstanceRootDirLen+1, A51_KEYROOTINST_DIR_PREFIX);
  1758. if(!Hash(wszKeyRootClass,
  1759. wszDir+m_lInstanceRootDirLen+wcslen(A51_KEYROOTINST_DIR_PREFIX)+1))
  1760. {
  1761. return WBEM_E_OUT_OF_MEMORY;
  1762. }
  1763. return S_OK;
  1764. }
  1765. CHR CNamespaceHandle::ConstructLinkDirFromClass(LPWSTR wszDir,
  1766. LPCWSTR wszClassName)
  1767. {
  1768. wcscpy(wszDir, m_wszInstanceRootDir);
  1769. wszDir[m_lInstanceRootDirLen] = L'\\';
  1770. wcscpy(wszDir+m_lInstanceRootDirLen+1, A51_CLASSINST_DIR_PREFIX);
  1771. if(!Hash(wszClassName,
  1772. wszDir+m_lInstanceRootDirLen+wcslen(A51_CLASSINST_DIR_PREFIX)+1))
  1773. {
  1774. return WBEM_E_OUT_OF_MEMORY;
  1775. }
  1776. return S_OK;
  1777. }
  1778. CHR CNamespaceHandle::ConstructLinkDirFromClassHash(LPWSTR wszDir,
  1779. LPCWSTR wszClassHash)
  1780. {
  1781. wcscpy(wszDir, m_wszInstanceRootDir);
  1782. wszDir[m_lInstanceRootDirLen] = L'\\';
  1783. wcscpy(wszDir+m_lInstanceRootDirLen+1, A51_CLASSINST_DIR_PREFIX);
  1784. wcscat(wszDir, wszClassHash);
  1785. return S_OK;
  1786. }
  1787. CHR CNamespaceHandle::WriteInstanceLinkByHash(LPCWSTR wszClassName,
  1788. LPCWSTR wszInstanceHash)
  1789. {
  1790. HRESULT hres;
  1791. //
  1792. // Construct the path to the link file under the class
  1793. //
  1794. CFileName wszInstanceLinkPath;
  1795. if (wszInstanceLinkPath == NULL)
  1796. return WBEM_E_OUT_OF_MEMORY;
  1797. hres = ConstructLinkDirFromClass(wszInstanceLinkPath, wszClassName);
  1798. if(FAILED(hres))
  1799. return hres;
  1800. wcscat(wszInstanceLinkPath, L"\\" A51_INSTLINK_FILE_PREFIX);
  1801. wcscat(wszInstanceLinkPath, wszInstanceHash);
  1802. //
  1803. // Create an empty file there
  1804. //
  1805. long lRes = g_Glob.GetFileCache()->WriteFile(wszInstanceLinkPath, 0, NULL);
  1806. if(lRes != ERROR_SUCCESS)
  1807. return WBEM_E_FAILED;
  1808. return S_OK;
  1809. }
  1810. CHR CNamespaceHandle::WriteInstanceReferences(_IWmiObject* pInst,
  1811. LPCWSTR wszClassName,
  1812. LPCWSTR wszFilePath)
  1813. {
  1814. HRESULT hres;
  1815. hres = pInst->BeginEnumeration(WBEM_FLAG_REFS_ONLY);
  1816. if(FAILED(hres))
  1817. return hres;
  1818. VARIANT v;
  1819. BSTR strName;
  1820. while((hres = pInst->Next(0, &strName, &v, NULL, NULL)) == S_OK)
  1821. {
  1822. CSysFreeMe sfm(strName);
  1823. CClearMe cm(&v);
  1824. if(V_VT(&v) == VT_BSTR)
  1825. {
  1826. hres = WriteInstanceReference(wszFilePath, wszClassName, strName,
  1827. V_BSTR(&v));
  1828. if(FAILED(hres))
  1829. return hres;
  1830. }
  1831. }
  1832. if(FAILED(hres))
  1833. return hres;
  1834. pInst->EndEnumeration();
  1835. return S_OK;
  1836. }
  1837. // NOTE: will clobber wszTargetPath
  1838. CHR CNamespaceHandle::ConstructReferenceDir(LPWSTR wszTargetPath,
  1839. LPWSTR wszReferenceDir)
  1840. {
  1841. //
  1842. // Deconstruct the target path name so that we could get a directory
  1843. // for it
  1844. //
  1845. DWORD dwKeySpace = (wcslen(wszTargetPath)+1) * sizeof(WCHAR);
  1846. LPWSTR wszKey = (LPWSTR)TempAlloc(dwKeySpace);
  1847. if(wszKey == NULL)
  1848. return WBEM_E_OUT_OF_MEMORY;
  1849. CTempFreeMe tfm2(wszKey, dwKeySpace);
  1850. LPWSTR wszClassName = NULL;
  1851. LPWSTR wszTargetNamespace = NULL;
  1852. bool bIsClass;
  1853. HRESULT hres = ComputeKeyFromPath(wszTargetPath, wszKey, &wszClassName,
  1854. &bIsClass, &wszTargetNamespace);
  1855. if(FAILED(hres))
  1856. return hres;
  1857. CTempFreeMe tfm1(wszClassName);
  1858. wszTargetPath = NULL; // invalidated by parsing
  1859. CTempFreeMe tfm3(wszTargetNamespace);
  1860. //
  1861. // Check if the target namespace is the same as ours
  1862. //
  1863. CNamespaceHandle* pTargetHandle = NULL;
  1864. if(wszTargetNamespace && wbem_wcsicmp(wszTargetNamespace, m_wsNamespace))
  1865. {
  1866. //
  1867. // It's different --- open it!
  1868. //
  1869. hres = m_pRepository->GetNamespaceHandle(wszTargetNamespace,
  1870. &pTargetHandle);
  1871. if(FAILED(hres))
  1872. {
  1873. ERRORTRACE((LOG_WBEMCORE, "Unable to open target namespace "
  1874. "'%S' in namespace '%S'\n", wszTargetNamespace,
  1875. (LPCWSTR)m_wsNamespace));
  1876. return hres;
  1877. }
  1878. }
  1879. else
  1880. {
  1881. pTargetHandle = this;
  1882. pTargetHandle->AddRef();
  1883. }
  1884. CReleaseMe rm1(pTargetHandle);
  1885. if(bIsClass)
  1886. {
  1887. return pTargetHandle->ConstructReferenceDirFromKey(NULL, wszClassName,
  1888. wszReferenceDir);
  1889. }
  1890. else
  1891. {
  1892. return pTargetHandle->ConstructReferenceDirFromKey(wszClassName, wszKey,
  1893. wszReferenceDir);
  1894. }
  1895. }
  1896. CHR CNamespaceHandle::ConstructReferenceDirFromKey(LPCWSTR wszClassName,
  1897. LPCWSTR wszKey, LPWSTR wszReferenceDir)
  1898. {
  1899. HRESULT hres;
  1900. //
  1901. // Construct the class directory for this instance
  1902. //
  1903. hres = ConstructKeyRootDirFromClass(wszReferenceDir, wszClassName);
  1904. if(FAILED(hres))
  1905. return hres;
  1906. int nLen = wcslen(wszReferenceDir);
  1907. wcscpy(wszReferenceDir+nLen, L"\\" A51_INSTREF_DIR_PREFIX);
  1908. nLen += 1 + wcslen(A51_INSTREF_DIR_PREFIX);
  1909. //
  1910. // Write instance hash
  1911. //
  1912. if(!Hash(wszKey, wszReferenceDir+nLen))
  1913. return WBEM_E_OUT_OF_MEMORY;
  1914. return S_OK;
  1915. }
  1916. // NOTE: will clobber wszReference
  1917. CHR CNamespaceHandle::ConstructReferenceFileName(LPWSTR wszReference,
  1918. LPCWSTR wszReferringFile, LPWSTR wszReferenceFile)
  1919. {
  1920. HRESULT hres = ConstructReferenceDir(wszReference, wszReferenceFile);
  1921. if(FAILED(hres))
  1922. return hres;
  1923. wszReference = NULL; // invalid
  1924. //
  1925. // It is basically
  1926. // irrelevant, we should use a randomly constructed name. Right now, we
  1927. // use a hash of the class name of the referrer --- THIS IS A BUG, THE SAME
  1928. // INSTANCE CAN POINT TO THE SAME ENDPOINT TWICE!!
  1929. //
  1930. wcscat(wszReferenceFile, L"\\"A51_REF_FILE_PREFIX);
  1931. DWORD dwLen = wcslen(wszReferenceFile);
  1932. if (!Hash(wszReferringFile, wszReferenceFile+dwLen))
  1933. return WBEM_E_OUT_OF_MEMORY;
  1934. return S_OK;
  1935. }
  1936. // NOTE: will clobber wszReference
  1937. CHR CNamespaceHandle::WriteInstanceReference(LPCWSTR wszReferringFile,
  1938. LPCWSTR wszReferringClass,
  1939. LPCWSTR wszReferringProp, LPWSTR wszReference)
  1940. {
  1941. HRESULT hres;
  1942. //
  1943. // Figure out the name of the file for the reference.
  1944. //
  1945. CFileName wszReferenceFile;
  1946. if (wszReferenceFile == NULL)
  1947. return WBEM_E_OUT_OF_MEMORY;
  1948. hres = ConstructReferenceFileName(wszReference, wszReferringFile,
  1949. wszReferenceFile);
  1950. if(FAILED(hres))
  1951. {
  1952. if(hres == WBEM_E_NOT_FOUND)
  1953. {
  1954. //
  1955. // Oh joy. A reference to an instance of a *class* that does not
  1956. // exist (not a non-existence instance, those are normal).
  1957. // Forget it (BUGBUG)
  1958. //
  1959. return S_OK;
  1960. }
  1961. else
  1962. return hres;
  1963. }
  1964. //
  1965. // Construct the buffer
  1966. //
  1967. DWORD dwTotalLen = 4 * sizeof(DWORD) +
  1968. (wcslen(wszReferringClass) + wcslen(wszReferringProp) +
  1969. wcslen(wszReferringFile) - g_Glob.GetRootDirLen() +
  1970. wcslen(m_wsNamespace) + 4)
  1971. * sizeof(WCHAR);
  1972. BYTE* pBuffer = (BYTE*)TempAlloc(dwTotalLen);
  1973. if (pBuffer == NULL)
  1974. return WBEM_E_OUT_OF_MEMORY;
  1975. CTempFreeMe vdm(pBuffer, dwTotalLen);
  1976. BYTE* pCurrent = pBuffer;
  1977. DWORD dwStringLen;
  1978. //
  1979. // Write namespace name
  1980. //
  1981. dwStringLen = wcslen(m_wsNamespace);
  1982. memcpy(pCurrent, &dwStringLen, sizeof(DWORD));
  1983. pCurrent += sizeof(DWORD);
  1984. memcpy(pCurrent, m_wsNamespace, sizeof(WCHAR)*dwStringLen);
  1985. pCurrent += sizeof(WCHAR)*dwStringLen;
  1986. //
  1987. // Write the referring class name
  1988. //
  1989. dwStringLen = wcslen(wszReferringClass);
  1990. memcpy(pCurrent, &dwStringLen, sizeof(DWORD));
  1991. pCurrent += sizeof(DWORD);
  1992. memcpy(pCurrent, wszReferringClass, sizeof(WCHAR)*dwStringLen);
  1993. pCurrent += sizeof(WCHAR)*dwStringLen;
  1994. //
  1995. // Write referring property name
  1996. //
  1997. dwStringLen = wcslen(wszReferringProp);
  1998. memcpy(pCurrent, &dwStringLen, sizeof(DWORD));
  1999. pCurrent += sizeof(DWORD);
  2000. memcpy(pCurrent, wszReferringProp, sizeof(WCHAR)*dwStringLen);
  2001. pCurrent += sizeof(WCHAR)*dwStringLen;
  2002. //
  2003. // Write referring file name minus the database root path. Notice that we
  2004. // cannot skip the namespace-specific prefix lest we break cross-namespace
  2005. // associations
  2006. //
  2007. dwStringLen = wcslen(wszReferringFile) - g_Glob.GetRootDirLen();
  2008. memcpy(pCurrent, &dwStringLen, sizeof(DWORD));
  2009. pCurrent += sizeof(DWORD);
  2010. memcpy(pCurrent, wszReferringFile + g_Glob.GetRootDirLen(),
  2011. sizeof(WCHAR)*dwStringLen);
  2012. pCurrent += sizeof(WCHAR)*dwStringLen;
  2013. //
  2014. // All done --- create the file
  2015. //
  2016. long lRes = g_Glob.GetFileCache()->WriteFile(wszReferenceFile, dwTotalLen,
  2017. pBuffer);
  2018. if(lRes != ERROR_SUCCESS)
  2019. return WBEM_E_FAILED;
  2020. return S_OK;
  2021. }
  2022. CHR CNamespaceHandle::PutClass(_IWmiObject* pClass, DWORD dwFlags,
  2023. CEventCollector &aEvents)
  2024. {
  2025. HRESULT hres;
  2026. bool bDisableEvents = ((dwFlags & WMIDB_DISABLE_EVENTS)?true:false);
  2027. //
  2028. // Get the class name
  2029. //
  2030. VARIANT vClass;
  2031. hres = pClass->Get(L"__CLASS", 0, &vClass, NULL, NULL);
  2032. if(FAILED(hres) || (V_VT(&vClass) != VT_BSTR) ||
  2033. !V_BSTR(&vClass) || !wcslen(V_BSTR(&vClass)))
  2034. {
  2035. return WBEM_E_INVALID_OBJECT;
  2036. }
  2037. CClearMe cm1(&vClass);
  2038. LPCWSTR wszClassName = V_BSTR(&vClass);
  2039. //
  2040. // Check to make sure this class was created from a valid parent class
  2041. //
  2042. VARIANT vSuperClass;
  2043. hres = pClass->Get(L"__SUPERCLASS", 0, &vSuperClass, NULL, NULL);
  2044. if (FAILED(hres))
  2045. return WBEM_E_INVALID_OBJECT;
  2046. CClearMe cm2(&vSuperClass);
  2047. _IWmiObject* pSuperClass = NULL;
  2048. if ((V_VT(&vSuperClass) == VT_BSTR) && V_BSTR(&vSuperClass) &&
  2049. wcslen(V_BSTR(&vSuperClass)))
  2050. {
  2051. LPCWSTR wszSuperClassName = V_BSTR(&vSuperClass);
  2052. // do not clone
  2053. hres = GetClassDirect(wszSuperClassName, IID__IWmiObject,
  2054. (void**)&pSuperClass, false, NULL, NULL, NULL);
  2055. if (hres == WBEM_E_NOT_FOUND)
  2056. return WBEM_E_INVALID_SUPERCLASS;
  2057. if (FAILED(hres))
  2058. return hres;
  2059. if(wszClassName[0] != L'_')
  2060. {
  2061. hres = pClass->IsParentClass(0, pSuperClass);
  2062. if(FAILED(hres))
  2063. return hres;
  2064. if(hres == WBEM_S_FALSE)
  2065. return WBEM_E_INVALID_SUPERCLASS;
  2066. }
  2067. }
  2068. CReleaseMe rm(pSuperClass);
  2069. //
  2070. // Retrieve the previous definition, if any
  2071. //
  2072. _IWmiObject* pOldClass = NULL;
  2073. __int64 nOldTime = 0;
  2074. hres = GetClassDirect(wszClassName, IID__IWmiObject, (void**)&pOldClass,
  2075. false, &nOldTime, NULL, NULL); // do not clone
  2076. if(FAILED(hres) && hres != WBEM_E_NOT_FOUND)
  2077. return hres;
  2078. CReleaseMe rm1(pOldClass);
  2079. if ((dwFlags & WBEM_FLAG_CREATE_ONLY) && (hres != WBEM_E_NOT_FOUND))
  2080. return WBEM_E_ALREADY_EXISTS;
  2081. if ((dwFlags & WBEM_FLAG_UPDATE_ONLY) && (FAILED(hres)))
  2082. return WBEM_E_NOT_FOUND;
  2083. //
  2084. // If the class exists, we need to check the update scenarios to make sure
  2085. // we do not break any
  2086. //
  2087. bool bNoClassChangeDetected = false;
  2088. if (pOldClass)
  2089. {
  2090. hres = pClass->CompareDerivedMostClass(0, pOldClass);
  2091. if ((hres != WBEM_S_FALSE) && (hres != WBEM_S_NO_ERROR))
  2092. return hres;
  2093. else if (hres == WBEM_S_NO_ERROR)
  2094. bNoClassChangeDetected = true;
  2095. }
  2096. A51TRACE(("Putting class %S, dwFlags=0x%X. Old was %p, changed=%d\n",
  2097. wszClassName, dwFlags, pOldClass, !bNoClassChangeDetected));
  2098. if (!bNoClassChangeDetected)
  2099. {
  2100. if (pOldClass != NULL)
  2101. {
  2102. hres = CanClassBeUpdatedCompatible(dwFlags, wszClassName, pOldClass,
  2103. pClass);
  2104. if(FAILED(hres))
  2105. {
  2106. if((dwFlags & WBEM_FLAG_UPDATE_SAFE_MODE) == 0 &&
  2107. (dwFlags & WBEM_FLAG_UPDATE_FORCE_MODE) == 0)
  2108. {
  2109. // Can't compatibly, not allowed any other way
  2110. return hres;
  2111. }
  2112. if(hres != WBEM_E_CLASS_HAS_CHILDREN &&
  2113. hres != WBEM_E_CLASS_HAS_INSTANCES)
  2114. {
  2115. // some serious failure!
  2116. return hres;
  2117. }
  2118. //
  2119. // This is a safe mode or force mode update which takes more
  2120. // than a compatible update to carry out the operation
  2121. //
  2122. return UpdateClassSafeForce(pSuperClass, dwFlags, wszClassName,
  2123. pOldClass, pClass, aEvents);
  2124. }
  2125. }
  2126. //
  2127. // Either there was no previous copy, or it is compatible with the new
  2128. // one, so we can perform a compatible update
  2129. //
  2130. hres = UpdateClassCompatible(pSuperClass, wszClassName, pClass,
  2131. pOldClass, nOldTime);
  2132. if (FAILED(hres))
  2133. return hres;
  2134. }
  2135. if(!bDisableEvents)
  2136. {
  2137. if(pOldClass)
  2138. {
  2139. hres = FireEvent(aEvents, WBEM_EVENTTYPE_ClassModification,
  2140. wszClassName, pClass, pOldClass);
  2141. }
  2142. else
  2143. {
  2144. hres = FireEvent(aEvents, WBEM_EVENTTYPE_ClassCreation,
  2145. wszClassName, pClass);
  2146. }
  2147. }
  2148. return S_OK;
  2149. }
  2150. CHR CNamespaceHandle::UpdateClassCompatible(_IWmiObject* pSuperClass,
  2151. LPCWSTR wszClassName, _IWmiObject *pClass, _IWmiObject *pOldClass,
  2152. __int64 nFakeUpdateTime)
  2153. {
  2154. HRESULT hres;
  2155. //
  2156. // Construct the path for the file
  2157. //
  2158. CFileName wszHash;
  2159. if (wszHash == NULL)
  2160. return WBEM_E_OUT_OF_MEMORY;
  2161. if(!A51Hash(wszClassName, wszHash))
  2162. return WBEM_E_OUT_OF_MEMORY;
  2163. A51TRACE(("Class %S has has %S\n", wszClassName, wszHash));
  2164. return UpdateClassCompatibleHash(pSuperClass, wszHash, pClass, pOldClass,
  2165. nFakeUpdateTime);
  2166. }
  2167. CHR CNamespaceHandle::UpdateClassCompatibleHash(_IWmiObject* pSuperClass,
  2168. LPCWSTR wszClassHash, _IWmiObject *pClass, _IWmiObject *pOldClass,
  2169. __int64 nFakeUpdateTime)
  2170. {
  2171. HRESULT hres;
  2172. CFileName wszFileName;
  2173. CFileName wszFilePath;
  2174. if ((wszFileName == NULL) || (wszFilePath == NULL))
  2175. return WBEM_E_OUT_OF_MEMORY;
  2176. wcscpy(wszFileName, A51_CLASSDEF_FILE_PREFIX);
  2177. wcscat(wszFileName, wszClassHash);
  2178. wcscpy(wszFilePath, m_wszClassRootDir);
  2179. wcscat(wszFilePath, L"\\");
  2180. wcscat(wszFilePath, wszFileName);
  2181. //
  2182. // Write it into the file
  2183. //
  2184. hres = ClassToFile(pSuperClass, pClass, wszFilePath,
  2185. nFakeUpdateTime);
  2186. if(FAILED(hres))
  2187. return hres;
  2188. //
  2189. // Add all needed references --- parent, pointers, etc
  2190. //
  2191. if (pOldClass)
  2192. {
  2193. VARIANT v;
  2194. VariantInit(&v);
  2195. hres = pClass->Get(L"__CLASS", 0, &v, NULL, NULL);
  2196. CClearMe cm(&v);
  2197. if(SUCCEEDED(hres))
  2198. {
  2199. hres = EraseClassRelationships(V_BSTR(&v), pOldClass, wszFileName);
  2200. }
  2201. if (FAILED(hres))
  2202. return hres;
  2203. }
  2204. hres = WriteClassRelationships(pClass, wszFileName);
  2205. return hres;
  2206. }
  2207. CHR CNamespaceHandle::UpdateClassSafeForce(_IWmiObject* pSuperClass,
  2208. DWORD dwFlags, LPCWSTR wszClassName, _IWmiObject *pOldClass,
  2209. _IWmiObject *pNewClass, CEventCollector &aEvents)
  2210. {
  2211. HRESULT hres = UpdateClassAggressively(pSuperClass, dwFlags, wszClassName,
  2212. pNewClass, pOldClass, aEvents);
  2213. //
  2214. // If this is a force mode update and we failed for anything other than
  2215. // out of memory then we should delete the class and try again.
  2216. //
  2217. if (FAILED(hres) && (hres != WBEM_E_OUT_OF_MEMORY) &&
  2218. (dwFlags & WBEM_FLAG_UPDATE_FORCE_MODE))
  2219. {
  2220. //
  2221. // We need to delete the class and try again.
  2222. //
  2223. hres = DeleteClass(wszClassName, aEvents);
  2224. if(FAILED(hres))
  2225. return hres;
  2226. hres = UpdateClassAggressively(pSuperClass, dwFlags, wszClassName,
  2227. pNewClass, pOldClass, aEvents);
  2228. }
  2229. return hres;
  2230. }
  2231. CHR CNamespaceHandle::UpdateClassAggressively(_IWmiObject* pSuperClass,
  2232. DWORD dwFlags, LPCWSTR wszClassName, _IWmiObject *pNewClass,
  2233. _IWmiObject *pOldClass, CEventCollector &aEvents)
  2234. {
  2235. HRESULT hres = WBEM_S_NO_ERROR;
  2236. if ((dwFlags & WBEM_FLAG_UPDATE_FORCE_MODE) == 0)
  2237. {
  2238. //
  2239. // If we have instances we need to quit as we cannot update them.
  2240. //
  2241. hres = ClassHasInstances(wszClassName);
  2242. if(FAILED(hres))
  2243. return hres;
  2244. if (hres == WBEM_S_NO_ERROR)
  2245. return WBEM_E_CLASS_HAS_INSTANCES;
  2246. _ASSERT(hres == WBEM_S_FALSE, L"Unknown success code!");
  2247. }
  2248. else if (dwFlags & WBEM_FLAG_UPDATE_FORCE_MODE)
  2249. {
  2250. //
  2251. // We need to delete the instances
  2252. //
  2253. hres = DeleteClassInstances(wszClassName, pOldClass, aEvents);
  2254. if(FAILED(hres))
  2255. return hres;
  2256. }
  2257. //
  2258. // Retrieve all child classes and update them
  2259. //
  2260. CWStringArray wsChildHashes;
  2261. hres = GetChildHashes(wszClassName, wsChildHashes);
  2262. if(FAILED(hres))
  2263. return hres;
  2264. for (int i = 0; i != wsChildHashes.Size(); i++)
  2265. {
  2266. hres = UpdateChildClassAggressively(dwFlags, wsChildHashes[i],
  2267. pNewClass, aEvents);
  2268. if (FAILED(hres))
  2269. return hres;
  2270. }
  2271. //
  2272. // Now we need to write the class back, update class refs etc.
  2273. //
  2274. hres = UpdateClassCompatible(pSuperClass, wszClassName, pNewClass,
  2275. pOldClass);
  2276. if(FAILED(hres))
  2277. return hres;
  2278. //
  2279. // Generate the class modification event...
  2280. //
  2281. if(!(dwFlags & WMIDB_DISABLE_EVENTS))
  2282. {
  2283. hres = FireEvent(aEvents, WBEM_EVENTTYPE_ClassModification, wszClassName, pNewClass, pOldClass);
  2284. }
  2285. return S_OK;
  2286. }
  2287. CHR CNamespaceHandle::UpdateChildClassAggressively(DWORD dwFlags,
  2288. LPCWSTR wszClassHash, _IWmiObject *pNewParentClass,
  2289. CEventCollector &aEvents)
  2290. {
  2291. HRESULT hres = WBEM_S_NO_ERROR;
  2292. dwFlags &= (WBEM_FLAG_UPDATE_FORCE_MODE | WBEM_FLAG_UPDATE_SAFE_MODE);
  2293. if ((dwFlags & WBEM_FLAG_UPDATE_FORCE_MODE) == 0)
  2294. {
  2295. hres = ClassHasInstancesFromClassHash(wszClassHash);
  2296. if(FAILED(hres))
  2297. return hres;
  2298. if (hres == WBEM_S_NO_ERROR)
  2299. return WBEM_E_CLASS_HAS_INSTANCES;
  2300. _ASSERT(hres == WBEM_S_FALSE, L"Unknown success code!");
  2301. }
  2302. //
  2303. // Get the old class definition
  2304. //
  2305. _IWmiObject *pOldClass = NULL;
  2306. hres = GetClassByHash(wszClassHash, true, &pOldClass, NULL, NULL, NULL);
  2307. if(FAILED(hres))
  2308. return hres;
  2309. CReleaseMe rm1(pOldClass);
  2310. if (dwFlags & WBEM_FLAG_UPDATE_FORCE_MODE)
  2311. {
  2312. //
  2313. // Need to delete all its instances, if any
  2314. //
  2315. VARIANT v;
  2316. VariantInit(&v);
  2317. hres = pOldClass->Get(L"__CLASS", 0, &v, NULL, NULL);
  2318. if(FAILED(hres))
  2319. return hres;
  2320. CClearMe cm(&v);
  2321. hres = DeleteClassInstances(V_BSTR(&v), pOldClass, aEvents);
  2322. if(FAILED(hres))
  2323. return hres;
  2324. }
  2325. //
  2326. // Update the existing class definition to work with the new parent class
  2327. //
  2328. _IWmiObject *pNewClass = NULL;
  2329. hres = pNewParentClass->Update(pOldClass, dwFlags, &pNewClass);
  2330. if(FAILED(hres))
  2331. return hres;
  2332. CReleaseMe rm2(pNewClass);
  2333. //
  2334. // Now we have to recurse through all child classes and do the same
  2335. //
  2336. CWStringArray wsChildHashes;
  2337. hres = GetChildHashesByHash(wszClassHash, wsChildHashes);
  2338. if(FAILED(hres))
  2339. return hres;
  2340. for (int i = 0; i != wsChildHashes.Size(); i++)
  2341. {
  2342. hres = UpdateChildClassAggressively(dwFlags, wsChildHashes[i],
  2343. pNewClass, aEvents);
  2344. if (FAILED(hres))
  2345. return hres;
  2346. }
  2347. //
  2348. // Now we need to write the class back, update class refs etc
  2349. //
  2350. hres = UpdateClassCompatibleHash(pNewParentClass, wszClassHash,
  2351. pNewClass, pOldClass);
  2352. if(FAILED(hres))
  2353. return hres;
  2354. return S_OK;
  2355. }
  2356. CHR CNamespaceHandle::CanClassBeUpdatedCompatible(DWORD dwFlags,
  2357. LPCWSTR wszClassName, _IWmiObject *pOldClass, _IWmiObject *pNewClass)
  2358. {
  2359. HRESULT hres;
  2360. HRESULT hresError = WBEM_S_NO_ERROR;
  2361. //
  2362. // Do we have subclasses?
  2363. //
  2364. hres = ClassHasChildren(wszClassName);
  2365. if(FAILED(hres))
  2366. return hres;
  2367. if(hres == WBEM_S_NO_ERROR)
  2368. {
  2369. hresError = WBEM_E_CLASS_HAS_CHILDREN;
  2370. }
  2371. else
  2372. {
  2373. _ASSERT(hres == WBEM_S_FALSE, L"Unknown success code");
  2374. //
  2375. // Do we have instances belonging to this class? Don't even need to
  2376. // worry about sub-classes because we know we have none at this point!
  2377. //
  2378. hres = ClassHasInstances(wszClassName);
  2379. if(FAILED(hres))
  2380. return hres;
  2381. if(hres == WBEM_S_NO_ERROR)
  2382. {
  2383. hresError = WBEM_E_CLASS_HAS_INSTANCES;
  2384. }
  2385. else
  2386. {
  2387. _ASSERT(hres == WBEM_S_FALSE, L"Unknown success code");
  2388. //
  2389. // No nothing!
  2390. //
  2391. return WBEM_S_NO_ERROR;
  2392. }
  2393. }
  2394. _ASSERT(hresError != WBEM_S_NO_ERROR, L"");
  2395. //
  2396. // We have either subclasses or instances.
  2397. // Can we reconcile this class safely?
  2398. //
  2399. hres = pOldClass->ReconcileWith(
  2400. WMIOBJECT_RECONCILE_FLAG_TESTRECONCILE, pNewClass);
  2401. if(hres == WBEM_S_NO_ERROR)
  2402. {
  2403. // reconcilable, so OK
  2404. return WBEM_S_NO_ERROR;
  2405. }
  2406. else if(hres == WBEM_E_FAILED) // awful, isn't it
  2407. {
  2408. // irreconcilable
  2409. return hresError;
  2410. }
  2411. else
  2412. {
  2413. return hres;
  2414. }
  2415. }
  2416. CHR CNamespaceHandle::FireEvent(CEventCollector &aEvents,
  2417. DWORD dwType, LPCWSTR wszArg1,
  2418. _IWmiObject* pObj1, _IWmiObject* pObj2)
  2419. {
  2420. try
  2421. {
  2422. CRepEvent *pEvent = new CRepEvent(dwType, m_wsFullNamespace, wszArg1,
  2423. pObj1, pObj2);
  2424. if (pEvent == NULL)
  2425. return WBEM_E_OUT_OF_MEMORY;
  2426. if (!aEvents.AddEvent(pEvent))
  2427. {
  2428. delete pEvent;
  2429. return WBEM_E_OUT_OF_MEMORY;
  2430. }
  2431. return WBEM_S_NO_ERROR;
  2432. }
  2433. catch (CX_MemoryException)
  2434. {
  2435. return WBEM_E_OUT_OF_MEMORY;
  2436. }
  2437. }
  2438. HRESULT CNamespaceHandle::SendEvents(CEventCollector &aEvents)
  2439. {
  2440. _IWmiCoreServices * pSvcs = g_Glob.GetCoreSvcs();
  2441. CReleaseMe rm(pSvcs);
  2442. aEvents.SendEvents(pSvcs);
  2443. //
  2444. // Ignore ESS return codes --- they do not invalidate the operation
  2445. //
  2446. return WBEM_S_NO_ERROR;
  2447. }
  2448. CHR CNamespaceHandle::WriteClassRelationships(_IWmiObject* pClass,
  2449. LPCWSTR wszFileName)
  2450. {
  2451. HRESULT hres;
  2452. //
  2453. // Get the parent
  2454. //
  2455. VARIANT v;
  2456. VariantInit(&v);
  2457. hres = pClass->Get(L"__SUPERCLASS", 0, &v, NULL, NULL);
  2458. CClearMe cm(&v);
  2459. if(FAILED(hres))
  2460. return hres;
  2461. if(V_VT(&v) == VT_BSTR)
  2462. hres = WriteParentChildRelationship(wszFileName, V_BSTR(&v));
  2463. else
  2464. hres = WriteParentChildRelationship(wszFileName, L"");
  2465. if(FAILED(hres))
  2466. return hres;
  2467. //
  2468. // Write references
  2469. //
  2470. hres = pClass->BeginEnumeration(WBEM_FLAG_REFS_ONLY);
  2471. if(FAILED(hres))
  2472. return hres;
  2473. BSTR strName = NULL;
  2474. while((hres = pClass->Next(0, &strName, NULL, NULL, NULL)) == S_OK)
  2475. {
  2476. CSysFreeMe sfm(strName);
  2477. hres = WriteClassReference(pClass, wszFileName, strName);
  2478. if(FAILED(hres))
  2479. return hres;
  2480. }
  2481. pClass->EndEnumeration();
  2482. if(FAILED(hres))
  2483. return hres;
  2484. return S_OK;
  2485. }
  2486. CHR CNamespaceHandle::WriteClassReference(_IWmiObject* pReferringClass,
  2487. LPCWSTR wszReferringFile,
  2488. LPCWSTR wszReferringProp)
  2489. {
  2490. HRESULT hres;
  2491. //
  2492. // Figure out the class we are pointing to
  2493. //
  2494. DWORD dwSize = 0;
  2495. DWORD dwFlavor = 0;
  2496. CIMTYPE ct;
  2497. hres = pReferringClass->GetPropQual(wszReferringProp, L"CIMTYPE", 0, 0,
  2498. &ct, &dwFlavor, &dwSize, NULL);
  2499. if(dwSize == 0)
  2500. return WBEM_E_OUT_OF_MEMORY;
  2501. LPWSTR wszQual = (WCHAR*)TempAlloc(dwSize);
  2502. if(wszQual == NULL)
  2503. return WBEM_E_OUT_OF_MEMORY;
  2504. CTempFreeMe tfm(wszQual, dwSize);
  2505. hres = pReferringClass->GetPropQual(wszReferringProp, L"CIMTYPE", 0, dwSize,
  2506. &ct, &dwFlavor, &dwSize, wszQual);
  2507. if(FAILED(hres))
  2508. return hres;
  2509. //
  2510. // Parse out the class name
  2511. //
  2512. WCHAR* pwcColon = wcschr(wszQual, L':');
  2513. if(pwcColon == NULL)
  2514. return S_OK; // untyped reference requires no bookkeeping
  2515. LPCWSTR wszReferredToClass = pwcColon+1;
  2516. //
  2517. // Figure out the name of the file for the reference.
  2518. //
  2519. CFileName wszReferenceFile;
  2520. if (wszReferenceFile == NULL)
  2521. return WBEM_E_OUT_OF_MEMORY;
  2522. hres = ConstructClassReferenceFileName(wszReferredToClass,
  2523. wszReferringFile, wszReferringProp,
  2524. wszReferenceFile);
  2525. if(FAILED(hres))
  2526. return hres;
  2527. //
  2528. // Create the empty file
  2529. //
  2530. long lRes = g_Glob.GetFileCache()->WriteFile(wszReferenceFile, 0, NULL);
  2531. if(lRes != ERROR_SUCCESS)
  2532. return WBEM_E_FAILED;
  2533. return S_OK;
  2534. }
  2535. CHR CNamespaceHandle::WriteParentChildRelationship(
  2536. LPCWSTR wszChildFileName, LPCWSTR wszParentName)
  2537. {
  2538. CFileName wszParentChildFileName;
  2539. if (wszParentChildFileName == NULL)
  2540. return WBEM_E_OUT_OF_MEMORY;
  2541. HRESULT hres = ConstructParentChildFileName(wszChildFileName,
  2542. wszParentName,
  2543. wszParentChildFileName);
  2544. //
  2545. // Create the file
  2546. //
  2547. long lRes = g_Glob.GetFileCache()->WriteFile(wszParentChildFileName, 0, NULL);
  2548. if(lRes != ERROR_SUCCESS)
  2549. return WBEM_E_FAILED;
  2550. return S_OK;
  2551. }
  2552. CHR CNamespaceHandle::ConstructParentChildFileName(
  2553. LPCWSTR wszChildFileName, LPCWSTR wszParentName,
  2554. LPWSTR wszParentChildFileName)
  2555. {
  2556. //
  2557. // Construct the name of the directory where the parent class keeps its
  2558. // children
  2559. //
  2560. HRESULT hres = ConstructClassRelationshipsDir(wszParentName,
  2561. wszParentChildFileName);
  2562. if(FAILED(hres))
  2563. return hres;
  2564. //
  2565. // Append the filename of the child, but substituting the child-class prefix
  2566. // for the class-def prefix
  2567. //
  2568. wcscat(wszParentChildFileName, L"\\" A51_CHILDCLASS_FILE_PREFIX);
  2569. wcscat(wszParentChildFileName,
  2570. wszChildFileName + wcslen(A51_CLASSDEF_FILE_PREFIX));
  2571. return S_OK;
  2572. }
  2573. CHR CNamespaceHandle::ConstructClassRelationshipsDir(LPCWSTR wszClassName,
  2574. LPWSTR wszDirPath)
  2575. {
  2576. wcscpy(wszDirPath, m_wszClassRootDir);
  2577. wcscpy(wszDirPath + m_lClassRootDirLen, L"\\" A51_CLASSRELATION_DIR_PREFIX);
  2578. if(!Hash(wszClassName,
  2579. wszDirPath + m_lClassRootDirLen + 1 + wcslen(A51_CLASSRELATION_DIR_PREFIX)))
  2580. {
  2581. return WBEM_E_OUT_OF_MEMORY;
  2582. }
  2583. return S_OK;
  2584. }
  2585. CHR CNamespaceHandle::ConstructClassRelationshipsDirFromHash(
  2586. LPCWSTR wszHash, LPWSTR wszDirPath)
  2587. {
  2588. wcscpy(wszDirPath, m_wszClassRootDir);
  2589. wcscpy(wszDirPath + m_lClassRootDirLen, L"\\" A51_CLASSRELATION_DIR_PREFIX);
  2590. wcscpy(wszDirPath + m_lClassRootDirLen + 1 +wcslen(A51_CLASSRELATION_DIR_PREFIX),
  2591. wszHash);
  2592. return S_OK;
  2593. }
  2594. CHR CNamespaceHandle::ConstructClassReferenceFileName(
  2595. LPCWSTR wszReferredToClass,
  2596. LPCWSTR wszReferringFile,
  2597. LPCWSTR wszReferringProp,
  2598. LPWSTR wszFileName)
  2599. {
  2600. HRESULT hres;
  2601. hres = ConstructClassRelationshipsDir(wszReferredToClass, wszFileName);
  2602. if(FAILED(hres))
  2603. return hres;
  2604. //
  2605. // Extract the portion of the referring file containing the class hash
  2606. //
  2607. WCHAR* pwcLastUnderscore = wcsrchr(wszReferringFile, L'_');
  2608. if(pwcLastUnderscore == NULL)
  2609. return WBEM_E_CRITICAL_ERROR;
  2610. LPCWSTR wszReferringClassHash = pwcLastUnderscore+1;
  2611. wcscat(wszFileName, L"\\" A51_REF_FILE_PREFIX);
  2612. wcscat(wszFileName, wszReferringClassHash);
  2613. return S_OK;
  2614. }
  2615. CHR CNamespaceHandle::DeleteObject(
  2616. DWORD dwFlags,
  2617. REFIID riid,
  2618. LPVOID pObj,
  2619. CEventCollector &aEvents
  2620. )
  2621. {
  2622. DebugBreak();
  2623. return E_NOTIMPL;
  2624. }
  2625. CHR CNamespaceHandle::DeleteObjectByPath(DWORD dwFlags, LPWSTR wszPath,
  2626. CEventCollector &aEvents)
  2627. {
  2628. HRESULT hres;
  2629. //
  2630. // Get the key from path
  2631. //
  2632. DWORD dwLen = wcslen(wszPath)*sizeof(WCHAR)+2;
  2633. LPWSTR wszKey = (WCHAR*)TempAlloc(dwLen);
  2634. if(wszKey == NULL)
  2635. return WBEM_E_OUT_OF_MEMORY;
  2636. CTempFreeMe tfm(wszKey, dwLen);
  2637. bool bIsClass;
  2638. LPWSTR wszClassName = NULL;
  2639. hres = ComputeKeyFromPath(wszPath, wszKey, &wszClassName, &bIsClass);
  2640. if(FAILED(hres))
  2641. return hres;
  2642. CTempFreeMe tfm1(wszClassName, (wcslen(wszClassName)+1) * sizeof(WCHAR*));
  2643. if(bIsClass)
  2644. {
  2645. return DeleteClass(wszClassName, aEvents);
  2646. }
  2647. else
  2648. {
  2649. return DeleteInstance(wszClassName, wszKey, aEvents);
  2650. }
  2651. }
  2652. CHR CNamespaceHandle::DeleteInstance(LPCWSTR wszClassName, LPCWSTR wszKey,
  2653. CEventCollector &aEvents)
  2654. {
  2655. HRESULT hres;
  2656. //
  2657. // Get Class definition
  2658. //
  2659. _IWmiObject* pClass = NULL;
  2660. hres = GetClassDirect(wszClassName, IID__IWmiObject, (void**)&pClass,
  2661. false, NULL, NULL, NULL);
  2662. if(FAILED(hres))
  2663. return hres;
  2664. CReleaseMe rm1(pClass);
  2665. //
  2666. // Create its directory
  2667. //
  2668. CFileName wszFilePath;
  2669. if (wszFilePath == NULL)
  2670. return WBEM_E_OUT_OF_MEMORY;
  2671. hres = ConstructKeyRootDirFromClass(wszFilePath, wszClassName);
  2672. if(FAILED(hres))
  2673. return hres;
  2674. //
  2675. // Construct the path for the file
  2676. //
  2677. CFileName wszFileName;
  2678. if (wszFileName == NULL)
  2679. return WBEM_E_OUT_OF_MEMORY;
  2680. hres = ConstructInstanceDefName(wszFileName, wszKey);
  2681. if(FAILED(hres))
  2682. return hres;
  2683. wcscat(wszFilePath, L"\\");
  2684. wcscat(wszFilePath, wszFileName);
  2685. _IWmiObject* pInst;
  2686. hres = FileToInstance(wszFilePath, &pInst);
  2687. if(FAILED(hres))
  2688. return hres;
  2689. CReleaseMe rm2(pInst);
  2690. if(pInst->InheritsFrom(L"__Namespace") == S_OK)
  2691. {
  2692. //Make sure this is not a deletion of the root\default namespace
  2693. VARIANT vName;
  2694. VariantInit(&vName);
  2695. CClearMe cm1(&vName);
  2696. hres = pInst->Get(L"Name", 0, &vName, NULL, NULL);
  2697. if(FAILED(hres))
  2698. return WBEM_E_INVALID_OBJECT;
  2699. LPCWSTR wszName = V_BSTR(&vName);
  2700. if ((_wcsicmp(m_wsFullNamespace, L"\\\\.\\root") == 0) && (_wcsicmp(wszName, L"default") == 0))
  2701. return WBEM_E_ACCESS_DENIED;
  2702. }
  2703. hres = DeleteInstanceByFile(wszFilePath, pInst, false, aEvents);
  2704. if(FAILED(hres))
  2705. return hres;
  2706. //
  2707. // Fire an event
  2708. //
  2709. if(pInst->InheritsFrom(L"__Namespace") == S_OK)
  2710. {
  2711. //
  2712. // There is no need to do anything --- deletion of namespaces
  2713. // automatically fires events in DeleteInstanceByFile (because we need
  2714. // to accomplish it in the case of deleting a class derived from
  2715. // __NAMESPACE.
  2716. //
  2717. }
  2718. else
  2719. {
  2720. hres = FireEvent(aEvents, WBEM_EVENTTYPE_InstanceDeletion, wszClassName,
  2721. pInst);
  2722. }
  2723. A51TRACE(("DeleteInstance for class %S succeeded\n", wszClassName));
  2724. return S_OK;
  2725. }
  2726. CHR CNamespaceHandle::DeleteInstanceByFile(LPCWSTR wszFilePath,
  2727. _IWmiObject* pInst, bool bClassDeletion,
  2728. CEventCollector &aEvents)
  2729. {
  2730. HRESULT hres;
  2731. hres = DeleteInstanceSelf(wszFilePath, pInst, bClassDeletion);
  2732. if(FAILED(hres))
  2733. return hres;
  2734. hres = DeleteInstanceAsScope(pInst, aEvents);
  2735. if(FAILED(hres) && hres != WBEM_E_NOT_FOUND)
  2736. {
  2737. return hres;
  2738. }
  2739. return S_OK;
  2740. }
  2741. CHR CNamespaceHandle::DeleteInstanceSelf(LPCWSTR wszFilePath,
  2742. _IWmiObject* pInst,
  2743. bool bClassDeletion)
  2744. {
  2745. HRESULT hres;
  2746. //
  2747. // Delete the file
  2748. //
  2749. long lRes = g_Glob.GetFileCache()->DeleteFile(wszFilePath);
  2750. if(lRes == ERROR_FILE_NOT_FOUND || lRes == ERROR_PATH_NOT_FOUND)
  2751. {
  2752. return WBEM_E_NOT_FOUND;
  2753. }
  2754. else if(lRes != ERROR_SUCCESS)
  2755. return WBEM_E_FAILED;
  2756. hres = DeleteInstanceLink(pInst, wszFilePath);
  2757. if(FAILED(hres) && hres != WBEM_E_NOT_FOUND)
  2758. return hres;
  2759. hres = DeleteInstanceReferences(pInst, wszFilePath);
  2760. if(FAILED(hres) && hres != WBEM_E_NOT_FOUND)
  2761. return hres;
  2762. if(bClassDeletion)
  2763. {
  2764. //
  2765. // We need to remove all dangling references to this instance,
  2766. // because they make no sense once the class is deleted --- we don't
  2767. // know what key structure the new class will even have. In the future,
  2768. // we'll want to move these references to some class-wide location
  2769. //
  2770. hres = DeleteInstanceBackReferences(wszFilePath);
  2771. if(FAILED(hres) && hres != WBEM_E_NOT_FOUND)
  2772. return hres;
  2773. }
  2774. return S_OK;
  2775. }
  2776. CHR CNamespaceHandle::ConstructReferenceDirFromFilePath(
  2777. LPCWSTR wszFilePath, LPWSTR wszReferenceDir)
  2778. {
  2779. //
  2780. // It's the same, only with INSTDEF_FILE_PREFIX replaced with
  2781. // INSTREF_DIR_PREFIX
  2782. //
  2783. CFileName wszEnding;
  2784. if (wszEnding == NULL)
  2785. return WBEM_E_OUT_OF_MEMORY;
  2786. WCHAR* pwcLastSlash = wcsrchr(wszFilePath, L'\\');
  2787. if(pwcLastSlash == NULL)
  2788. return WBEM_E_FAILED;
  2789. wcscpy(wszEnding, pwcLastSlash + 1 + wcslen(A51_INSTDEF_FILE_PREFIX));
  2790. wcscpy(wszReferenceDir, wszFilePath);
  2791. wszReferenceDir[(pwcLastSlash+1)-wszFilePath] = 0;
  2792. wcscat(wszReferenceDir, A51_INSTREF_DIR_PREFIX);
  2793. wcscat(wszReferenceDir, wszEnding);
  2794. return S_OK;
  2795. }
  2796. CHR CNamespaceHandle::DeleteInstanceBackReferences(LPCWSTR wszFilePath)
  2797. {
  2798. HRESULT hres;
  2799. CFileName wszReferenceDir;
  2800. if (wszReferenceDir == NULL)
  2801. return WBEM_E_OUT_OF_MEMORY;
  2802. hres = ConstructReferenceDirFromFilePath(wszFilePath, wszReferenceDir);
  2803. if(FAILED(hres))
  2804. return hres;
  2805. wcscat(wszReferenceDir, L"\\");
  2806. CFileName wszReferencePrefix;
  2807. if (wszReferencePrefix == NULL)
  2808. return WBEM_E_OUT_OF_MEMORY;
  2809. wcscpy(wszReferencePrefix, wszReferenceDir);
  2810. wcscat(wszReferencePrefix, A51_REF_FILE_PREFIX);
  2811. //
  2812. // Enumerate all files in it
  2813. //
  2814. WIN32_FIND_DATAW fd;
  2815. void* hSearch;
  2816. long lRes = g_Glob.GetFileCache()->FindFirst(wszReferencePrefix, &fd, &hSearch);
  2817. if(lRes == ERROR_FILE_NOT_FOUND || lRes == ERROR_PATH_NOT_FOUND)
  2818. {
  2819. //
  2820. // No files in dir --- no problem
  2821. //
  2822. return WBEM_S_NO_ERROR;
  2823. }
  2824. else if(lRes != ERROR_SUCCESS)
  2825. {
  2826. return WBEM_E_FAILED;
  2827. }
  2828. CFileCache::CFindCloseMe fcm(g_Glob.GetFileCache(), hSearch);
  2829. //
  2830. // Prepare a buffer for file path
  2831. //
  2832. CFileName wszFullFileName;
  2833. if (wszFullFileName == NULL)
  2834. return WBEM_E_OUT_OF_MEMORY;
  2835. wcscpy(wszFullFileName, wszReferenceDir);
  2836. long lDirLen = wcslen(wszFullFileName);
  2837. do
  2838. {
  2839. if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  2840. continue;
  2841. wcscpy(wszFullFileName+lDirLen, fd.cFileName);
  2842. long lRes = g_Glob.GetFileCache()->DeleteFile(wszFullFileName);
  2843. if(lRes != ERROR_SUCCESS)
  2844. {
  2845. ERRORTRACE((LOG_WBEMCORE, "Cannot delete reference file '%S' with "
  2846. "error code %d\n", wszFullFileName, lRes));
  2847. return WBEM_E_FAILED;
  2848. }
  2849. }
  2850. while(g_Glob.GetFileCache()->FindNext(hSearch, &fd) == ERROR_SUCCESS);
  2851. return S_OK;
  2852. }
  2853. CHR CNamespaceHandle::DeleteInstanceLink(_IWmiObject* pInst,
  2854. LPCWSTR wszInstanceDefFilePath)
  2855. {
  2856. HRESULT hres;
  2857. //
  2858. // Get the class name
  2859. //
  2860. VARIANT vClass;
  2861. VariantInit(&vClass);
  2862. CClearMe cm1(&vClass);
  2863. hres = pInst->Get(L"__CLASS", 0, &vClass, NULL, NULL);
  2864. if(FAILED(hres))
  2865. return WBEM_E_INVALID_OBJECT;
  2866. LPCWSTR wszClassName = V_BSTR(&vClass);
  2867. //
  2868. // Construct the link directory for the class
  2869. //
  2870. CFileName wszInstanceLinkPath;
  2871. if (wszInstanceLinkPath == NULL)
  2872. return WBEM_E_OUT_OF_MEMORY;
  2873. hres = ConstructLinkDirFromClass(wszInstanceLinkPath, wszClassName);
  2874. if(FAILED(hres))
  2875. return hres;
  2876. wcscat(wszInstanceLinkPath, L"\\" A51_INSTLINK_FILE_PREFIX);
  2877. //
  2878. // It remains to append the instance-specific part of the file name.
  2879. // Convineintly, it is the same material as was used for the def file path,
  2880. // so we can steal it. ALERT: RELIES ON ALL PREFIXES ENDING IN '_'!!
  2881. //
  2882. WCHAR* pwcLastUnderscore = wcsrchr(wszInstanceDefFilePath, L'_');
  2883. if(pwcLastUnderscore == NULL)
  2884. return WBEM_E_CRITICAL_ERROR;
  2885. wcscat(wszInstanceLinkPath, pwcLastUnderscore+1);
  2886. //
  2887. // Delete the file
  2888. //
  2889. long lRes = g_Glob.GetFileCache()->DeleteFile(wszInstanceLinkPath);
  2890. if(lRes == ERROR_FILE_NOT_FOUND || lRes == ERROR_PATH_NOT_FOUND)
  2891. return WBEM_E_NOT_FOUND;
  2892. else if(lRes != ERROR_SUCCESS)
  2893. return WBEM_E_FAILED;
  2894. return S_OK;
  2895. }
  2896. CHR CNamespaceHandle::DeleteInstanceAsScope(_IWmiObject* pInst, CEventCollector &aEvents)
  2897. {
  2898. HRESULT hres;
  2899. //
  2900. // For now, just check if it is a namespace
  2901. //
  2902. hres = pInst->InheritsFrom(L"__Namespace");
  2903. if(FAILED(hres))
  2904. return hres;
  2905. if(hres != S_OK) // not a namespace
  2906. return S_FALSE;
  2907. //
  2908. // It is a namespace --- construct full path
  2909. //
  2910. WString wsFullName = m_wsNamespace;
  2911. wsFullName += L"\\";
  2912. VARIANT vName;
  2913. VariantInit(&vName);
  2914. CClearMe cm(&vName);
  2915. hres = pInst->Get(L"Name", 0, &vName, NULL, NULL);
  2916. if(FAILED(hres))
  2917. return hres;
  2918. if(V_VT(&vName) != VT_BSTR)
  2919. return WBEM_E_INVALID_OBJECT;
  2920. wsFullName += V_BSTR(&vName);
  2921. //
  2922. // Delete it
  2923. //
  2924. CNamespaceHandle* pNewHandle = new CNamespaceHandle(m_pControl,
  2925. m_pRepository);
  2926. if(pNewHandle == NULL)
  2927. return WBEM_E_OUT_OF_MEMORY;
  2928. pNewHandle->AddRef();
  2929. CReleaseMe rm1(pNewHandle);
  2930. hres = pNewHandle->Initialize(wsFullName);
  2931. if(FAILED(hres))
  2932. return hres;
  2933. //
  2934. // Mind to only fire child namespace deletion events from the inside
  2935. //
  2936. bool bNamespaceOnly = aEvents.IsNamespaceOnly();
  2937. aEvents.SetNamespaceOnly(true);
  2938. hres = pNewHandle->DeleteSelf(aEvents);
  2939. if(FAILED(hres))
  2940. return hres;
  2941. aEvents.SetNamespaceOnly(bNamespaceOnly);
  2942. //
  2943. // Fire the event
  2944. //
  2945. hres = FireEvent(aEvents, WBEM_EVENTTYPE_NamespaceDeletion,
  2946. V_BSTR(&vName), pInst);
  2947. return S_OK;
  2948. }
  2949. CHR CNamespaceHandle::DeleteSelf(CEventCollector &aEvents)
  2950. {
  2951. //
  2952. // Delete all top-level classes. This will delete all namespaces
  2953. // (as instances of __Namespace), all classes (as children of top-levels)
  2954. // and all instances
  2955. //
  2956. HRESULT hres = DeleteDerivedClasses(L"", aEvents);
  2957. if(FAILED(hres))
  2958. return hres;
  2959. //
  2960. // One extra thing --- clean up relationships for the empty class
  2961. //
  2962. CFileName wszRelationshipDir;
  2963. if (wszRelationshipDir== NULL)
  2964. return WBEM_E_OUT_OF_MEMORY;
  2965. hres = ConstructClassRelationshipsDir(L"", wszRelationshipDir);
  2966. if(FAILED(hres))
  2967. return hres;
  2968. long lRes = g_Glob.GetFileCache()->RemoveDirectory(wszRelationshipDir);
  2969. if(lRes != ERROR_SUCCESS && lRes != ERROR_FILE_NOT_FOUND &&
  2970. lRes != ERROR_PATH_NOT_FOUND)
  2971. {
  2972. return WBEM_E_FAILED;
  2973. }
  2974. //
  2975. // Delete our own root directory -- it should be empty by now
  2976. //
  2977. if(g_Glob.GetFileCache()->RemoveDirectory(m_wszInstanceRootDir) != ERROR_SUCCESS)
  2978. return WBEM_E_FAILED;
  2979. //
  2980. // We do not delete our class root directory --- if we are a namespace, it
  2981. // is the same as our instance root directory so we are already done; if not
  2982. // we should not be cleaning up all the classes!
  2983. //
  2984. m_pClassCache->SetError(WBEM_E_INVALID_NAMESPACE);
  2985. return S_OK;
  2986. }
  2987. CHR CNamespaceHandle::DeleteInstanceReferences(_IWmiObject* pInst,
  2988. LPCWSTR wszFilePath)
  2989. {
  2990. HRESULT hres;
  2991. hres = pInst->BeginEnumeration(WBEM_FLAG_REFS_ONLY);
  2992. if(FAILED(hres))
  2993. return hres;
  2994. VARIANT v;
  2995. while((hres = pInst->Next(0, NULL, &v, NULL, NULL)) == S_OK)
  2996. {
  2997. CClearMe cm(&v);
  2998. if(V_VT(&v) == VT_BSTR)
  2999. {
  3000. hres = DeleteInstanceReference(wszFilePath, V_BSTR(&v));
  3001. if(FAILED(hres))
  3002. return hres;
  3003. }
  3004. }
  3005. if(FAILED(hres))
  3006. return hres;
  3007. pInst->EndEnumeration();
  3008. return S_OK;
  3009. }
  3010. // NOTE: will clobber wszReference
  3011. CHR CNamespaceHandle::DeleteInstanceReference(LPCWSTR wszOurFilePath,
  3012. LPWSTR wszReference)
  3013. {
  3014. HRESULT hres;
  3015. CFileName wszReferenceFile;
  3016. if (wszReferenceFile == NULL)
  3017. return WBEM_E_OUT_OF_MEMORY;
  3018. hres = ConstructReferenceFileName(wszReference, wszOurFilePath, wszReferenceFile);
  3019. if(FAILED(hres))
  3020. {
  3021. if(hres == WBEM_E_NOT_FOUND)
  3022. {
  3023. //
  3024. // Oh joy. A reference to an instance of a *class* that does not
  3025. // exist (not a non-existence instance, those are normal).
  3026. // Forget it (BUGBUG)
  3027. //
  3028. return S_OK;
  3029. }
  3030. else
  3031. return hres;
  3032. }
  3033. long lRes = g_Glob.GetFileCache()->DeleteFile(wszReferenceFile);
  3034. if(lRes != ERROR_SUCCESS)
  3035. {
  3036. if(lRes == ERROR_FILE_NOT_FOUND)
  3037. return WBEM_E_NOT_FOUND;
  3038. else
  3039. return WBEM_E_FAILED;
  3040. }
  3041. else
  3042. return WBEM_S_NO_ERROR;
  3043. }
  3044. CHR CNamespaceHandle::DeleteClassByHash(LPCWSTR wszHash, CEventCollector &aEvents)
  3045. {
  3046. HRESULT hres;
  3047. //
  3048. // Get Class definition
  3049. //
  3050. _IWmiObject* pClass = NULL;
  3051. bool bSystemClass = false;
  3052. hres = GetClassByHash(wszHash, false, &pClass, NULL, NULL, &bSystemClass);
  3053. CReleaseMe rm1(pClass);
  3054. if(FAILED(hres))
  3055. return hres;
  3056. //
  3057. // Get the actual class name
  3058. //
  3059. VARIANT v;
  3060. hres = pClass->Get(L"__CLASS", 0, &v, NULL, NULL);
  3061. if(FAILED(hres))
  3062. return hres;
  3063. CClearMe cm1(&v);
  3064. if(V_VT(&v) != VT_BSTR)
  3065. return WBEM_E_INVALID_CLASS;
  3066. //
  3067. // Construct definition file name
  3068. //
  3069. CFileName wszFileName;
  3070. if (wszFileName == NULL)
  3071. return WBEM_E_OUT_OF_MEMORY;
  3072. hres = ConstructClassDefFileNameFromHash(wszHash, wszFileName);
  3073. if(FAILED(hres))
  3074. return hres;
  3075. return DeleteClassInternal(V_BSTR(&v), pClass, wszFileName, aEvents, bSystemClass);
  3076. }
  3077. CHR CNamespaceHandle::DeleteClass(LPCWSTR wszClassName, CEventCollector &aEvents)
  3078. {
  3079. HRESULT hres;
  3080. A51TRACE(("Deleting class %S\n", wszClassName));
  3081. //
  3082. // Construct the path for the file
  3083. //
  3084. CFileName wszFileName;
  3085. if (wszFileName == NULL)
  3086. return WBEM_E_OUT_OF_MEMORY;
  3087. hres = ConstructClassDefFileName(wszClassName, wszFileName);
  3088. if(FAILED(hres))
  3089. return hres;
  3090. //
  3091. // Get Class definition
  3092. //
  3093. _IWmiObject* pClass = NULL;
  3094. bool bSystemClass = false;
  3095. hres = GetClassDirect(wszClassName, IID__IWmiObject, (void**)&pClass,
  3096. false, NULL, NULL, &bSystemClass);
  3097. if(FAILED(hres))
  3098. return hres;
  3099. CReleaseMe rm1(pClass);
  3100. return DeleteClassInternal(wszClassName, pClass, wszFileName, aEvents, bSystemClass);
  3101. }
  3102. CHR CNamespaceHandle::DeleteClassInternal(LPCWSTR wszClassName,
  3103. _IWmiObject* pClass,
  3104. LPCWSTR wszFileName,
  3105. CEventCollector &aEvents,
  3106. bool bSystemClass)
  3107. {
  3108. HRESULT hres;
  3109. CFileName wszFilePath;
  3110. if (wszFilePath == NULL)
  3111. return WBEM_E_OUT_OF_MEMORY;
  3112. swprintf(wszFilePath, L"%s\\%s", m_wszClassRootDir, wszFileName);
  3113. //
  3114. // Delete all derived classes
  3115. //
  3116. hres = DeleteDerivedClasses(wszClassName, aEvents);
  3117. if(FAILED(hres))
  3118. return hres;
  3119. //
  3120. // Delete all instances. Only fire events if namespaces are deleted
  3121. //
  3122. bool bNamespaceOnly = aEvents.IsNamespaceOnly();
  3123. aEvents.SetNamespaceOnly(true);
  3124. hres = DeleteClassInstances(wszClassName, pClass, aEvents);
  3125. if(FAILED(hres))
  3126. return hres;
  3127. aEvents.SetNamespaceOnly(bNamespaceOnly);
  3128. if (!bSystemClass)
  3129. {
  3130. //
  3131. // Clean up references
  3132. //
  3133. hres = EraseClassRelationships(wszClassName, pClass, wszFileName);
  3134. if(FAILED(hres))
  3135. return hres;
  3136. //
  3137. // Delete the file
  3138. //
  3139. long lRes = g_Glob.GetFileCache()->DeleteFile(wszFilePath);
  3140. if(lRes == ERROR_FILE_NOT_FOUND || lRes == ERROR_PATH_NOT_FOUND)
  3141. return WBEM_E_NOT_FOUND;
  3142. else if(lRes != ERROR_SUCCESS)
  3143. return WBEM_E_FAILED;
  3144. }
  3145. m_pClassCache->InvalidateClass(wszClassName);
  3146. //
  3147. // Fire an event
  3148. //
  3149. hres = FireEvent(aEvents, WBEM_EVENTTYPE_ClassDeletion, wszClassName, pClass);
  3150. return S_OK;
  3151. }
  3152. CHR CNamespaceHandle::DeleteDerivedClasses(LPCWSTR wszClassName, CEventCollector &aEvents)
  3153. {
  3154. HRESULT hres;
  3155. CWStringArray wsChildHashes;
  3156. hres = GetChildHashes(wszClassName, wsChildHashes);
  3157. if(FAILED(hres))
  3158. return hres;
  3159. for(int i = 0; i < wsChildHashes.Size(); i++)
  3160. {
  3161. hres = DeleteClassByHash(wsChildHashes[i], aEvents);
  3162. if(FAILED(hres) && (hres != WBEM_E_NOT_FOUND))
  3163. {
  3164. return hres;
  3165. }
  3166. }
  3167. return S_OK;
  3168. }
  3169. CHR CNamespaceHandle::GetChildDefs(LPCWSTR wszClassName, bool bRecursive,
  3170. IWbemObjectSink* pSink, bool bClone)
  3171. {
  3172. CFileName wszHash;
  3173. if (wszHash == NULL)
  3174. return WBEM_E_OUT_OF_MEMORY;
  3175. if(!A51Hash(wszClassName, wszHash))
  3176. return WBEM_E_OUT_OF_MEMORY;
  3177. return GetChildDefsByHash(wszHash, bRecursive, pSink, bClone);
  3178. }
  3179. CHR CNamespaceHandle::GetChildDefsByHash(LPCWSTR wszHash, bool bRecursive,
  3180. IWbemObjectSink* pSink, bool bClone)
  3181. {
  3182. HRESULT hres;
  3183. long lStartIndex = m_pClassCache->GetLastInvalidationIndex();
  3184. //
  3185. // Get the hashes of the child filenames
  3186. //
  3187. CWStringArray wsChildHashes;
  3188. hres = GetChildHashesByHash(wszHash, wsChildHashes);
  3189. if(FAILED(hres))
  3190. return hres;
  3191. //
  3192. // Get their class definitions
  3193. //
  3194. for(int i = 0; i < wsChildHashes.Size(); i++)
  3195. {
  3196. LPCWSTR wszChildHash = wsChildHashes[i];
  3197. _IWmiObject* pClass = NULL;
  3198. hres = GetClassByHash(wszChildHash, bClone, &pClass, NULL, NULL, NULL);
  3199. if(FAILED(hres))
  3200. return hres;
  3201. CReleaseMe rm1(pClass);
  3202. hres = pSink->Indicate(1, (IWbemClassObject**)&pClass);
  3203. if(FAILED(hres))
  3204. return hres;
  3205. //
  3206. // Continue recursively if indicated
  3207. //
  3208. if(bRecursive)
  3209. {
  3210. hres = GetChildDefsByHash(wszChildHash, bRecursive, pSink, bClone);
  3211. if(FAILED(hres))
  3212. return hres;
  3213. }
  3214. }
  3215. //
  3216. // Mark cache completeness
  3217. //
  3218. m_pClassCache->DoneWithChildrenByHash(wszHash, bRecursive, lStartIndex);
  3219. return S_OK;
  3220. }
  3221. CHR CNamespaceHandle::GetChildHashes(LPCWSTR wszClassName,
  3222. CWStringArray& wsChildHashes)
  3223. {
  3224. CFileName wszHash;
  3225. if (wszHash == NULL)
  3226. return WBEM_E_OUT_OF_MEMORY;
  3227. if(!A51Hash(wszClassName, wszHash))
  3228. return WBEM_E_OUT_OF_MEMORY;
  3229. return GetChildHashesByHash(wszHash, wsChildHashes);
  3230. }
  3231. CHR CNamespaceHandle::GetChildHashesByHash(LPCWSTR wszHash,
  3232. CWStringArray& wsChildHashes)
  3233. {
  3234. HRESULT hres;
  3235. long lRes;
  3236. //Try retrieving the system classes namespace first...
  3237. if (g_pSystemClassNamespace && (wcscmp(m_wsNamespace, A51_SYSTEMCLASS_NS) != 0))
  3238. {
  3239. hres = g_pSystemClassNamespace->GetChildHashesByHash(wszHash, wsChildHashes);
  3240. if (FAILED(hres))
  3241. return hres;
  3242. }
  3243. //
  3244. // Construct the prefix for the children classes
  3245. //
  3246. CFileName wszChildPrefix;
  3247. if (wszChildPrefix == NULL)
  3248. return WBEM_E_OUT_OF_MEMORY;
  3249. hres = ConstructClassRelationshipsDirFromHash(wszHash, wszChildPrefix);
  3250. if(FAILED(hres))
  3251. return hres;
  3252. wcscat(wszChildPrefix, L"\\" A51_CHILDCLASS_FILE_PREFIX);
  3253. //
  3254. // Enumerate all such files in the cache
  3255. //
  3256. void* pHandle = NULL;
  3257. WIN32_FIND_DATAW wfd;
  3258. lRes = g_Glob.GetFileCache()->FindFirst(wszChildPrefix, &wfd, &pHandle);
  3259. while(lRes == ERROR_SUCCESS)
  3260. {
  3261. wsChildHashes.Add(wfd.cFileName + wcslen(A51_CHILDCLASS_FILE_PREFIX));
  3262. lRes = g_Glob.GetFileCache()->FindNext(pHandle, &wfd);
  3263. }
  3264. if(pHandle)
  3265. g_Glob.GetFileCache()->FindClose(pHandle);
  3266. if(lRes != ERROR_FILE_NOT_FOUND && lRes != ERROR_NO_MORE_FILES)
  3267. return WBEM_E_FAILED;
  3268. else
  3269. return S_OK;
  3270. }
  3271. CHR CNamespaceHandle::ClassHasChildren(LPCWSTR wszClassName)
  3272. {
  3273. CFileName wszHash;
  3274. if (wszHash == NULL)
  3275. return WBEM_E_OUT_OF_MEMORY;
  3276. if(!A51Hash(wszClassName, wszHash))
  3277. return WBEM_E_OUT_OF_MEMORY;
  3278. HRESULT hres;
  3279. long lRes;
  3280. //Try retrieving the system classes namespace first...
  3281. if (g_pSystemClassNamespace && (wcscmp(m_wsNamespace, A51_SYSTEMCLASS_NS) != 0))
  3282. {
  3283. hres = g_pSystemClassNamespace->ClassHasChildren(wszClassName);
  3284. if (FAILED(hres) || (hres == WBEM_S_NO_ERROR))
  3285. return hres;
  3286. }
  3287. //
  3288. // Construct the prefix for the children classes
  3289. //
  3290. CFileName wszChildPrefix;
  3291. if (wszChildPrefix == NULL)
  3292. return WBEM_E_OUT_OF_MEMORY;
  3293. hres = ConstructClassRelationshipsDirFromHash(wszHash, wszChildPrefix);
  3294. if(FAILED(hres))
  3295. return hres;
  3296. wcscat(wszChildPrefix, L"\\" A51_CHILDCLASS_FILE_PREFIX);
  3297. void* pHandle = NULL;
  3298. WIN32_FIND_DATAW wfd;
  3299. lRes = g_Glob.GetFileCache()->FindFirst(wszChildPrefix, &wfd, &pHandle);
  3300. A51TRACE(("FindFirst %S returned %d\n", wszChildPrefix, lRes));
  3301. if(pHandle)
  3302. g_Glob.GetFileCache()->FindClose(pHandle);
  3303. if(lRes != ERROR_FILE_NOT_FOUND && lRes != ERROR_NO_MORE_FILES && lRes != S_OK)
  3304. {
  3305. ERRORTRACE((LOG_WBEMCORE, "Unexpected error code %d from FindFirst on "
  3306. "'%S'", lRes, wszChildPrefix));
  3307. return WBEM_E_FAILED;
  3308. }
  3309. else if (lRes == ERROR_FILE_NOT_FOUND)
  3310. return WBEM_S_FALSE;
  3311. else
  3312. return WBEM_S_NO_ERROR;
  3313. }
  3314. CHR CNamespaceHandle::ClassHasInstances(LPCWSTR wszClassName)
  3315. {
  3316. CFileName wszHash;
  3317. if (wszHash == NULL)
  3318. return WBEM_E_OUT_OF_MEMORY;
  3319. if(!A51Hash(wszClassName, wszHash))
  3320. return WBEM_E_OUT_OF_MEMORY;
  3321. return ClassHasInstancesFromClassHash(wszHash);
  3322. }
  3323. CHR CNamespaceHandle::ClassHasInstancesFromClassHash(LPCWSTR wszClassHash)
  3324. {
  3325. HRESULT hres;
  3326. long lRes;
  3327. //
  3328. // Check the instances in this namespace first. The instance directory in
  3329. // default scope is the class directory of the namespace
  3330. //
  3331. hres = ClassHasInstancesInScopeFromClassHash(m_wszClassRootDir,
  3332. wszClassHash);
  3333. if(hres != WBEM_S_FALSE)
  3334. return hres;
  3335. //
  3336. // No instances in the namespace --- have to enumerate all the scopes
  3337. //
  3338. /*
  3339. CFileName wszScopeDir;
  3340. if (wszScopeDir == NULL)
  3341. return WBEM_E_OUT_OF_MEMORY;
  3342. wcscpy(wszScopeDir, m_wszClassRootDir);
  3343. wcscat(wszScopeDir, L"\\" A51_SCOPE_DIR_PREFIX);
  3344. void* pScopeHandle = NULL;
  3345. WIN32_FIND_DATAW fdScope;
  3346. lRes = g_Glob.GetFileCache()->FindFirst(wszScopeDir, &fdScope, &pScopeHandle);
  3347. if(lRes != ERROR_SUCCESS)
  3348. {
  3349. */
  3350. return WBEM_S_FALSE;
  3351. }
  3352. CHR CNamespaceHandle::ClassHasInstancesInScopeFromClassHash(
  3353. LPCWSTR wszInstanceRootDir, LPCWSTR wszClassHash)
  3354. {
  3355. CFileName wszFullDirName;
  3356. if (wszFullDirName == NULL)
  3357. return WBEM_E_OUT_OF_MEMORY;
  3358. wcscpy(wszFullDirName, wszInstanceRootDir);
  3359. wcscat(wszFullDirName, L"\\" A51_CLASSINST_DIR_PREFIX);
  3360. wcscat(wszFullDirName, wszClassHash);
  3361. wcscat(wszFullDirName, L"\\" A51_INSTLINK_FILE_PREFIX);
  3362. void* pHandle = NULL;
  3363. WIN32_FIND_DATAW fd;
  3364. LONG lRes;
  3365. lRes = g_Glob.GetFileCache()->FindFirst(wszFullDirName, &fd, &pHandle);
  3366. if(pHandle)
  3367. g_Glob.GetFileCache()->FindClose(pHandle);
  3368. if(lRes != ERROR_FILE_NOT_FOUND && lRes != ERROR_NO_MORE_FILES && lRes != S_OK)
  3369. {
  3370. A51TRACE(("ClassHasInstances returning WBEM_E_FAILED for %S\\CD_%S\n", m_wsFullNamespace, wszClassHash));
  3371. return WBEM_E_FAILED;
  3372. }
  3373. else if (lRes == ERROR_FILE_NOT_FOUND)
  3374. {
  3375. A51TRACE(("ClassHasInstances returning WBEM_S_FALSE for %S\\CD_%S\n", m_wsFullNamespace, wszClassHash));
  3376. return WBEM_S_FALSE;
  3377. }
  3378. else
  3379. {
  3380. A51TRACE(("ClassHasInstances returning WBEM_S_NO_ERROR for %S\\CD_%S\n", m_wsFullNamespace, wszClassHash));
  3381. return WBEM_S_NO_ERROR;
  3382. }
  3383. }
  3384. CHR CNamespaceHandle::EraseParentChildRelationship(
  3385. LPCWSTR wszChildFileName, LPCWSTR wszParentName)
  3386. {
  3387. CFileName wszParentChildFileName;
  3388. if (wszParentChildFileName == NULL)
  3389. return WBEM_E_OUT_OF_MEMORY;
  3390. HRESULT hres = ConstructParentChildFileName(wszChildFileName,
  3391. wszParentName,
  3392. wszParentChildFileName);
  3393. //
  3394. // Delete the file
  3395. //
  3396. long lRes = g_Glob.GetFileCache()->DeleteFile(wszParentChildFileName);
  3397. if(lRes != ERROR_SUCCESS)
  3398. return WBEM_E_FAILED;
  3399. return S_OK;
  3400. }
  3401. CHR CNamespaceHandle::EraseClassRelationships(LPCWSTR wszClassName,
  3402. _IWmiObject* pClass, LPCWSTR wszFileName)
  3403. {
  3404. HRESULT hres;
  3405. //
  3406. // Get the parent
  3407. //
  3408. VARIANT v;
  3409. VariantInit(&v);
  3410. hres = pClass->Get(L"__SUPERCLASS", 0, &v, NULL, NULL);
  3411. CClearMe cm(&v);
  3412. if(FAILED(hres))
  3413. return hres;
  3414. if(V_VT(&v) == VT_BSTR)
  3415. hres = EraseParentChildRelationship(wszFileName, V_BSTR(&v));
  3416. else
  3417. hres = EraseParentChildRelationship(wszFileName, L"");
  3418. if(FAILED(hres))
  3419. return hres;
  3420. //
  3421. // Erase references
  3422. //
  3423. hres = pClass->BeginEnumeration(WBEM_FLAG_REFS_ONLY);
  3424. if(FAILED(hres))
  3425. return hres;
  3426. BSTR strName = NULL;
  3427. while((hres = pClass->Next(0, &strName, NULL, NULL, NULL)) == S_OK)
  3428. {
  3429. CSysFreeMe sfm(strName);
  3430. hres = EraseClassReference(pClass, wszFileName, strName);
  3431. if(FAILED(hres) && (hres != WBEM_E_NOT_FOUND))
  3432. return hres;
  3433. }
  3434. pClass->EndEnumeration();
  3435. //
  3436. // Erase our relationship directories. For now, they must be
  3437. // empty at this point, BUT THIS WILL BREAK WHEN WE ADD CLASS REFERENCES.
  3438. //
  3439. CFileName wszRelationshipDir;
  3440. if (wszRelationshipDir == NULL)
  3441. {
  3442. return WBEM_E_OUT_OF_MEMORY;
  3443. }
  3444. hres = ConstructClassRelationshipsDir(wszClassName, wszRelationshipDir);
  3445. if(FAILED(hres))
  3446. return hres;
  3447. long lRes = g_Glob.GetFileCache()->RemoveDirectory(wszRelationshipDir, false);
  3448. if(lRes != ERROR_SUCCESS && lRes != ERROR_FILE_NOT_FOUND &&
  3449. lRes != ERROR_PATH_NOT_FOUND && lRes != ERROR_DIR_NOT_EMPTY)
  3450. {
  3451. return WBEM_E_FAILED;
  3452. }
  3453. return S_OK;
  3454. }
  3455. CHR CNamespaceHandle::EraseClassReference(_IWmiObject* pReferringClass,
  3456. LPCWSTR wszReferringFile,
  3457. LPCWSTR wszReferringProp)
  3458. {
  3459. HRESULT hres;
  3460. //
  3461. // Figure out the class we are pointing to
  3462. //
  3463. DWORD dwSize = 0;
  3464. DWORD dwFlavor = 0;
  3465. CIMTYPE ct;
  3466. hres = pReferringClass->GetPropQual(wszReferringProp, L"CIMTYPE", 0, 0,
  3467. &ct, &dwFlavor, &dwSize, NULL);
  3468. if(dwSize == 0)
  3469. return WBEM_E_OUT_OF_MEMORY;
  3470. LPWSTR wszQual = (WCHAR*)TempAlloc(dwSize);
  3471. if(wszQual == NULL)
  3472. return WBEM_E_OUT_OF_MEMORY;
  3473. CTempFreeMe tfm(wszQual, dwSize);
  3474. hres = pReferringClass->GetPropQual(wszReferringProp, L"CIMTYPE", 0, dwSize,
  3475. &ct, &dwFlavor, &dwSize, wszQual);
  3476. if(FAILED(hres))
  3477. return hres;
  3478. //
  3479. // Parse out the class name
  3480. //
  3481. WCHAR* pwcColon = wcschr(wszQual, L':');
  3482. if(pwcColon == NULL)
  3483. return S_OK; // untyped reference requires no bookkeeping
  3484. LPCWSTR wszReferredToClass = pwcColon+1;
  3485. //
  3486. // Figure out the name of the file for the reference.
  3487. //
  3488. CFileName wszReferenceFile;
  3489. if (wszReferenceFile == NULL)
  3490. return WBEM_E_OUT_OF_MEMORY;
  3491. hres = ConstructClassReferenceFileName(wszReferredToClass,
  3492. wszReferringFile, wszReferringProp,
  3493. wszReferenceFile);
  3494. if(FAILED(hres))
  3495. return hres;
  3496. //
  3497. // Delete the file
  3498. //
  3499. long lRes = g_Glob.GetFileCache()->DeleteFile(wszReferenceFile);
  3500. if (lRes == ERROR_FILE_NOT_FOUND)
  3501. return WBEM_E_NOT_FOUND;
  3502. else if(lRes != ERROR_SUCCESS)
  3503. return WBEM_E_FAILED;
  3504. return S_OK;
  3505. }
  3506. CHR CNamespaceHandle::DeleteClassInstances(LPCWSTR wszClassName,
  3507. _IWmiObject* pClass,
  3508. CEventCollector &aEvents)
  3509. {
  3510. HRESULT hres;
  3511. //
  3512. // Find the link directory for this class
  3513. //
  3514. CFileName wszLinkDir;
  3515. if (wszLinkDir == NULL)
  3516. return WBEM_E_OUT_OF_MEMORY;
  3517. hres = ConstructLinkDirFromClass(wszLinkDir, wszClassName);
  3518. if(FAILED(hres))
  3519. return hres;
  3520. //
  3521. // Enumerate all links in it
  3522. //
  3523. CFileName wszSearchPrefix;
  3524. if (wszSearchPrefix == NULL)
  3525. return WBEM_E_OUT_OF_MEMORY;
  3526. wcscpy(wszSearchPrefix, wszLinkDir);
  3527. wcscat(wszSearchPrefix, L"\\" A51_INSTLINK_FILE_PREFIX);
  3528. WIN32_FIND_DATAW fd;
  3529. void* hSearch;
  3530. long lRes = g_Glob.GetFileCache()->FindFirst(wszSearchPrefix, &fd, &hSearch);
  3531. if(lRes != ERROR_SUCCESS)
  3532. {
  3533. if(lRes != ERROR_FILE_NOT_FOUND)
  3534. {
  3535. return WBEM_E_FAILED;
  3536. }
  3537. // Still need to do directory cleanup!
  3538. }
  3539. else
  3540. {
  3541. CFileCache::CFindCloseMe fcm(g_Glob.GetFileCache(), hSearch);
  3542. //
  3543. // Prepare a buffer for instance definition file path
  3544. //
  3545. CFileName wszFullFileName;
  3546. if (wszFullFileName == NULL)
  3547. return WBEM_E_OUT_OF_MEMORY;
  3548. hres = ConstructKeyRootDirFromClass(wszFullFileName, wszClassName);
  3549. if(FAILED(hres))
  3550. {
  3551. if(hres == WBEM_E_CANNOT_BE_ABSTRACT)
  3552. return WBEM_S_NO_ERROR;
  3553. return hres;
  3554. }
  3555. long lDirLen = wcslen(wszFullFileName);
  3556. wszFullFileName[lDirLen] = L'\\';
  3557. lDirLen++;
  3558. do
  3559. {
  3560. hres = ConstructInstDefNameFromLinkName(wszFullFileName+lDirLen,
  3561. fd.cFileName);
  3562. if(FAILED(hres))
  3563. return hres;
  3564. _IWmiObject* pInst;
  3565. hres = FileToInstance(wszFullFileName, &pInst);
  3566. if(FAILED(hres))
  3567. return hres;
  3568. CReleaseMe rm1(pInst);
  3569. //
  3570. // Delete the instance, knowing that we are deleting its class. That
  3571. // has an affect on how we deal with the references
  3572. //
  3573. hres = DeleteInstanceByFile(wszFullFileName, pInst, true, aEvents);
  3574. if(FAILED(hres))
  3575. return hres;
  3576. }
  3577. while(g_Glob.GetFileCache()->FindNext(hSearch, &fd) == ERROR_SUCCESS);
  3578. }
  3579. //
  3580. // Erase our instance directories. It must be
  3581. // empty at this point
  3582. //
  3583. lRes = g_Glob.GetFileCache()->RemoveDirectory(wszLinkDir);
  3584. if(lRes != ERROR_SUCCESS && lRes != ERROR_FILE_NOT_FOUND &&
  3585. lRes != ERROR_PATH_NOT_FOUND)
  3586. {
  3587. return WBEM_E_FAILED;
  3588. }
  3589. //
  3590. // Erase our key root directory, if we have a key root directory
  3591. //
  3592. CFileName wszPutativeKeyRootDir;
  3593. if (wszPutativeKeyRootDir == NULL)
  3594. return WBEM_E_OUT_OF_MEMORY;
  3595. hres = ConstructKeyRootDirFromKeyRoot(wszPutativeKeyRootDir, wszClassName);
  3596. if(FAILED(hres))
  3597. return hres;
  3598. lRes = g_Glob.GetFileCache()->RemoveDirectory(wszPutativeKeyRootDir);
  3599. if(lRes != ERROR_SUCCESS && lRes != ERROR_FILE_NOT_FOUND &&
  3600. lRes != ERROR_PATH_NOT_FOUND)
  3601. {
  3602. return WBEM_E_FAILED;
  3603. }
  3604. return S_OK;
  3605. }
  3606. class CExecQueryObject : public CFiberTask
  3607. {
  3608. protected:
  3609. IWbemQuery* m_pQuery;
  3610. CDbIterator* m_pIter;
  3611. CNamespaceHandle* m_pNs;
  3612. DWORD m_lFlags;
  3613. public:
  3614. CExecQueryObject(CNamespaceHandle* pNs, IWbemQuery* pQuery,
  3615. CDbIterator* pIter, DWORD lFlags)
  3616. : m_pQuery(pQuery), m_pIter(pIter), m_pNs(pNs), m_lFlags(lFlags)
  3617. {
  3618. m_pQuery->AddRef();
  3619. m_pNs->AddRef();
  3620. //
  3621. // Does not AddRef the iterator --- iterator owns and cleans up the req
  3622. //
  3623. }
  3624. ~CExecQueryObject()
  3625. {
  3626. if(m_pQuery)
  3627. m_pQuery->Release();
  3628. if(m_pNs)
  3629. m_pNs->Release();
  3630. }
  3631. HRESULT Execute()
  3632. {
  3633. HRESULT hres = m_pNs->ExecQuerySink(m_pQuery, m_lFlags, 0, m_pIter, NULL);
  3634. m_pIter->SetStatus(WBEM_STATUS_COMPLETE, hres, NULL, NULL);
  3635. return hres;
  3636. }
  3637. };
  3638. CHR CNamespaceHandle::ExecQuery(
  3639. IWbemQuery *pQuery,
  3640. DWORD dwFlags,
  3641. DWORD dwRequestedHandleType,
  3642. DWORD *dwMessageFlags,
  3643. IWmiDbIterator **ppQueryResult
  3644. )
  3645. {
  3646. CDbIterator* pIter = new CDbIterator(m_pControl, m_bUseIteratorLock);
  3647. m_bUseIteratorLock = true;
  3648. if (pIter == NULL)
  3649. return WBEM_E_OUT_OF_MEMORY;
  3650. pIter->AddRef();
  3651. CReleaseMe rm1((IWmiDbIterator*)pIter);
  3652. //
  3653. // Create a fiber execution object
  3654. //
  3655. CExecQueryObject* pReq = new CExecQueryObject(this, pQuery, pIter, dwFlags);
  3656. if(pReq == NULL)
  3657. return WBEM_E_OUT_OF_MEMORY;
  3658. #ifdef A51_USE_FIBER
  3659. //
  3660. // Create a fiber for it
  3661. //
  3662. void* pFiber = CreateFiberForTask(pReq);
  3663. if(pFiber == NULL)
  3664. {
  3665. delete pReq;
  3666. return WBEM_E_OUT_OF_MEMORY;
  3667. }
  3668. pIter->SetExecFiber(pFiber, pReq);
  3669. #else
  3670. HRESULT hRes = pReq->Execute();
  3671. delete pReq;
  3672. if (FAILED(hRes))
  3673. return hRes;
  3674. #endif
  3675. return pIter->QueryInterface(IID_IWmiDbIterator, (void**)ppQueryResult);
  3676. }
  3677. CHR CNamespaceHandle::ExecQuerySink(
  3678. IWbemQuery *pQuery,
  3679. DWORD dwFlags,
  3680. DWORD dwRequestedHandleType,
  3681. IWbemObjectSink* pSink,
  3682. DWORD *dwMessageFlags
  3683. )
  3684. {
  3685. if (g_bShuttingDown)
  3686. return WBEM_E_SHUTTING_DOWN;
  3687. HRESULT hres;
  3688. LPWSTR wszQuery = NULL;
  3689. hres = pQuery->GetAnalysis(WMIQ_ANALYSIS_QUERY_TEXT, 0, (void**)&wszQuery);
  3690. if (FAILED(hres))
  3691. return hres;
  3692. DWORD dwLen = ((wcslen(wszQuery) + 1) * sizeof(wchar_t));
  3693. LPWSTR strParse = (LPWSTR)TempAlloc(dwLen);
  3694. if(strParse == NULL)
  3695. {
  3696. pQuery->FreeMemory(wszQuery);
  3697. return WBEM_E_OUT_OF_MEMORY;
  3698. }
  3699. CTempFreeMe tfm(strParse, dwLen);
  3700. wcscpy(strParse, wszQuery);
  3701. if(!_wcsicmp(wcstok(strParse, L" "), L"references"))
  3702. {
  3703. hres = ExecReferencesQuery(wszQuery, pSink);
  3704. pQuery->FreeMemory(wszQuery);
  3705. return hres;
  3706. }
  3707. QL_LEVEL_1_RPN_EXPRESSION* pExpr = NULL;
  3708. CTextLexSource Source(wszQuery);
  3709. QL1_Parser Parser(&Source);
  3710. int nRet = Parser.Parse(&pExpr);
  3711. CDeleteMe<QL_LEVEL_1_RPN_EXPRESSION> dm(pExpr);
  3712. pQuery->FreeMemory(wszQuery);
  3713. if (nRet == QL1_Parser::OUT_OF_MEMORY)
  3714. return WBEM_E_OUT_OF_MEMORY;
  3715. if (nRet != QL1_Parser::SUCCESS)
  3716. return WBEM_E_FAILED;
  3717. if(!_wcsicmp(pExpr->bsClassName, L"meta_class"))
  3718. {
  3719. return ExecClassQuery(pExpr, pSink, dwFlags);
  3720. }
  3721. else
  3722. {
  3723. return ExecInstanceQuery(pExpr, pExpr->bsClassName,
  3724. (dwFlags & WBEM_FLAG_SHALLOW ? false : true),
  3725. pSink);
  3726. }
  3727. }
  3728. CHR CNamespaceHandle::ExecClassQuery(QL_LEVEL_1_RPN_EXPRESSION* pExpr,
  3729. IWbemObjectSink* pSink,
  3730. DWORD dwFlags)
  3731. {
  3732. if (g_bShuttingDown)
  3733. return WBEM_E_SHUTTING_DOWN;
  3734. HRESULT hres = ERROR_SUCCESS;
  3735. //
  3736. // Optimizations:
  3737. //
  3738. LPCWSTR wszClassName = NULL;
  3739. LPCWSTR wszSuperClass = NULL;
  3740. LPCWSTR wszAncestor = NULL;
  3741. bool bDontIncludeAncestorInResultSet = false;
  3742. if(pExpr->nNumTokens == 1)
  3743. {
  3744. QL_LEVEL_1_TOKEN* pToken = pExpr->pArrayOfTokens;
  3745. if(!_wcsicmp(pToken->PropertyName.GetStringAt(0), L"__SUPERCLASS") &&
  3746. pToken->nOperator == QL1_OPERATOR_EQUALS)
  3747. {
  3748. wszSuperClass = V_BSTR(&pToken->vConstValue);
  3749. }
  3750. else if(!_wcsicmp(pToken->PropertyName.GetStringAt(0), L"__THIS") &&
  3751. pToken->nOperator == QL1_OPERATOR_ISA)
  3752. {
  3753. wszAncestor = V_BSTR(&pToken->vConstValue);
  3754. }
  3755. else if(!_wcsicmp(pToken->PropertyName.GetStringAt(0), L"__CLASS") &&
  3756. pToken->nOperator == QL1_OPERATOR_EQUALS)
  3757. {
  3758. wszClassName = V_BSTR(&pToken->vConstValue);
  3759. }
  3760. }
  3761. else if (pExpr->nNumTokens == 3)
  3762. {
  3763. //
  3764. // This is a special optimisation used for deep enumeration of classes,
  3765. // and is expecting a query of:
  3766. // select * from meta_class where __this isa '<class_name>'
  3767. // and __class <> '<class_name>'
  3768. // where the <class_name> is the same class iin both cases. This will
  3769. // set the wszAncestor to <class_name> and propagate a flag to not
  3770. // include the actual ancestor in the list.
  3771. //
  3772. QL_LEVEL_1_TOKEN* pToken = pExpr->pArrayOfTokens;
  3773. if ((pToken[0].nTokenType == QL1_OP_EXPRESSION) &&
  3774. (pToken[1].nTokenType == QL1_OP_EXPRESSION) &&
  3775. (pToken[2].nTokenType == QL1_AND) &&
  3776. (pToken[0].nOperator == QL1_OPERATOR_ISA) &&
  3777. (pToken[1].nOperator == QL1_OPERATOR_NOTEQUALS) &&
  3778. (_wcsicmp(pToken[0].PropertyName.GetStringAt(0), L"__THIS") == 0) &&
  3779. (_wcsicmp(pToken[1].PropertyName.GetStringAt(0), L"__CLASS") == 0)
  3780. &&
  3781. (wcscmp(V_BSTR(&pToken[0].vConstValue),
  3782. V_BSTR(&pToken[1].vConstValue)) == 0)
  3783. )
  3784. {
  3785. wszAncestor = V_BSTR(&pToken[0].vConstValue);
  3786. bDontIncludeAncestorInResultSet = true;
  3787. }
  3788. }
  3789. if(wszClassName)
  3790. {
  3791. _IWmiObject* pClass = NULL;
  3792. hres = GetClassDirect(wszClassName, IID__IWmiObject, (void**)&pClass,
  3793. true, NULL, NULL, NULL);
  3794. if(hres == WBEM_E_NOT_FOUND)
  3795. {
  3796. //
  3797. // Class not there --- but that's success for us!
  3798. //
  3799. if (dwFlags & WBEM_FLAG_VALIDATE_CLASS_EXISTENCE)
  3800. return hres;
  3801. else
  3802. return S_OK;
  3803. }
  3804. else if(FAILED(hres))
  3805. {
  3806. return hres;
  3807. }
  3808. else
  3809. {
  3810. CReleaseMe rm1(pClass);
  3811. //
  3812. // Get the class
  3813. //
  3814. hres = pSink->Indicate(1, (IWbemClassObject**)&pClass);
  3815. if(FAILED(hres))
  3816. return hres;
  3817. return S_OK;
  3818. }
  3819. }
  3820. if (dwFlags & WBEM_FLAG_VALIDATE_CLASS_EXISTENCE)
  3821. {
  3822. _IWmiObject* pClass = NULL;
  3823. if (wszSuperClass)
  3824. hres = GetClassDirect(wszSuperClass, IID__IWmiObject, (void**)&pClass, false, NULL, NULL, NULL);
  3825. else if (wszAncestor)
  3826. hres = GetClassDirect(wszAncestor, IID__IWmiObject, (void**)&pClass, false, NULL, NULL, NULL);
  3827. if (FAILED(hres))
  3828. return hres;
  3829. if (pClass)
  3830. pClass->Release();
  3831. }
  3832. hres = EnumerateClasses(pSink, wszSuperClass, wszAncestor, true,
  3833. bDontIncludeAncestorInResultSet);
  3834. if(FAILED(hres))
  3835. return hres;
  3836. return S_OK;
  3837. }
  3838. CHR CNamespaceHandle::EnumerateClasses(IWbemObjectSink* pSink,
  3839. LPCWSTR wszSuperClass, LPCWSTR wszAncestor,
  3840. bool bClone,
  3841. bool bDontIncludeAncestorInResultSet)
  3842. {
  3843. if (g_bShuttingDown)
  3844. return WBEM_E_SHUTTING_DOWN;
  3845. CWStringArray wsClasses;
  3846. HRESULT hres;
  3847. //
  3848. // If superclass is given, check if its record is complete wrt children
  3849. //
  3850. if(wszSuperClass)
  3851. {
  3852. hres = m_pClassCache->EnumChildren(wszSuperClass, false, wsClasses);
  3853. if(hres == WBEM_S_FALSE)
  3854. {
  3855. //
  3856. // Not in cache --- get the info from files
  3857. //
  3858. return GetChildDefs(wszSuperClass, false, pSink, bClone);
  3859. }
  3860. else
  3861. {
  3862. if(FAILED(hres))
  3863. return hres;
  3864. return ListToEnum(wsClasses, pSink, bClone);
  3865. }
  3866. }
  3867. else
  3868. {
  3869. if(wszAncestor == NULL)
  3870. wszAncestor = L"";
  3871. hres = m_pClassCache->EnumChildren(wszAncestor, true, wsClasses);
  3872. if(hres == WBEM_S_FALSE)
  3873. {
  3874. //
  3875. // Not in cache --- get the info from files
  3876. //
  3877. hres = GetChildDefs(wszAncestor, true, pSink, bClone);
  3878. if(FAILED(hres))
  3879. return hres;
  3880. if(*wszAncestor && !bDontIncludeAncestorInResultSet)
  3881. {
  3882. //
  3883. // The class is derived from itself
  3884. //
  3885. _IWmiObject* pClass = NULL;
  3886. hres = GetClassDirect(wszAncestor, IID__IWmiObject,
  3887. (void**)&pClass, bClone, NULL, NULL, NULL);
  3888. if(FAILED(hres))
  3889. return hres;
  3890. CReleaseMe rm1(pClass);
  3891. hres = pSink->Indicate(1, (IWbemClassObject**)&pClass);
  3892. if(FAILED(hres))
  3893. return hres;
  3894. }
  3895. return S_OK;
  3896. }
  3897. else
  3898. {
  3899. if(FAILED(hres))
  3900. return hres;
  3901. if(*wszAncestor && !bDontIncludeAncestorInResultSet)
  3902. {
  3903. wsClasses.Add(wszAncestor);
  3904. }
  3905. return ListToEnum(wsClasses, pSink, bClone);
  3906. }
  3907. }
  3908. }
  3909. CHR CNamespaceHandle::ListToEnum(CWStringArray& wsClasses,
  3910. IWbemObjectSink* pSink, bool bClone)
  3911. {
  3912. HRESULT hres;
  3913. for(int i = 0; i < wsClasses.Size(); i++)
  3914. {
  3915. _IWmiObject* pClass = NULL;
  3916. if(wsClasses[i] == NULL || wsClasses[i][0] == 0)
  3917. continue;
  3918. hres = GetClassDirect(wsClasses[i], IID__IWmiObject, (void**)&pClass,
  3919. bClone, NULL, NULL, NULL);
  3920. if(FAILED(hres))
  3921. {
  3922. if(hres == WBEM_E_NOT_FOUND)
  3923. {
  3924. // That's OK --- class got removed
  3925. }
  3926. else
  3927. return hres;
  3928. }
  3929. else
  3930. {
  3931. CReleaseMe rm1(pClass);
  3932. hres = pSink->Indicate(1, (IWbemClassObject**)&pClass);
  3933. if(FAILED(hres))
  3934. return hres;
  3935. }
  3936. }
  3937. return WBEM_S_NO_ERROR;
  3938. }
  3939. CHR CNamespaceHandle::ExecInstanceQuery(QL_LEVEL_1_RPN_EXPRESSION* pQuery,
  3940. LPCWSTR wszClassName, bool bDeep,
  3941. IWbemObjectSink* pSink)
  3942. {
  3943. if (g_bShuttingDown)
  3944. return WBEM_E_SHUTTING_DOWN;
  3945. HRESULT hres;
  3946. WCHAR wszHash[MAX_HASH_LEN+1];
  3947. if(!Hash(wszClassName, wszHash))
  3948. return WBEM_E_OUT_OF_MEMORY;
  3949. if(bDeep)
  3950. hres = ExecDeepInstanceQuery(pQuery, wszHash, pSink);
  3951. else
  3952. hres = ExecShallowInstanceQuery(pQuery, wszHash, pSink);
  3953. if(FAILED(hres))
  3954. return hres;
  3955. return S_OK;
  3956. }
  3957. CHR CNamespaceHandle::ExecDeepInstanceQuery(
  3958. QL_LEVEL_1_RPN_EXPRESSION* pQuery,
  3959. LPCWSTR wszClassHash,
  3960. IWbemObjectSink* pSink)
  3961. {
  3962. if (g_bShuttingDown)
  3963. return WBEM_E_SHUTTING_DOWN;
  3964. HRESULT hres;
  3965. //
  3966. // Get all our instances
  3967. //
  3968. hres = ExecShallowInstanceQuery(pQuery, wszClassHash, pSink);
  3969. if(FAILED(hres))
  3970. return hres;
  3971. CWStringArray awsChildHashes;
  3972. //
  3973. // Check if the list of child classes is known to the cache
  3974. //
  3975. hres = m_pClassCache->EnumChildKeysByKey(wszClassHash, awsChildHashes);
  3976. if (hres == WBEM_S_FALSE)
  3977. {
  3978. //
  3979. // OK --- get them from the disk
  3980. //
  3981. hres = GetChildHashesByHash(wszClassHash, awsChildHashes);
  3982. }
  3983. if (FAILED(hres))
  3984. {
  3985. return hres;
  3986. }
  3987. //
  3988. // We have our hashes --- call them recursively
  3989. //
  3990. for(int i = 0; i < awsChildHashes.Size(); i++)
  3991. {
  3992. LPCWSTR wszChildHash = awsChildHashes[i];
  3993. hres = ExecDeepInstanceQuery(pQuery, wszChildHash, pSink);
  3994. if(FAILED(hres))
  3995. return hres;
  3996. }
  3997. return S_OK;
  3998. }
  3999. CHR CNamespaceHandle::ExecShallowInstanceQuery(
  4000. QL_LEVEL_1_RPN_EXPRESSION* pQuery,
  4001. LPCWSTR wszClassHash,
  4002. IWbemObjectSink* pSink)
  4003. {
  4004. if (g_bShuttingDown)
  4005. return WBEM_E_SHUTTING_DOWN;
  4006. HRESULT hres;
  4007. //
  4008. // Enumerate all files in the link directory
  4009. //
  4010. CFileName wszSearchPrefix;
  4011. if (wszSearchPrefix == NULL)
  4012. return WBEM_E_OUT_OF_MEMORY;
  4013. hres = ConstructLinkDirFromClassHash(wszSearchPrefix, wszClassHash);
  4014. if(FAILED(hres))
  4015. return hres;
  4016. wcscat(wszSearchPrefix, L"\\" A51_INSTLINK_FILE_PREFIX);
  4017. WIN32_FIND_DATAW fd;
  4018. void* hSearch;
  4019. long lRes = g_Glob.GetFileCache()->FindFirst(wszSearchPrefix, &fd, &hSearch);
  4020. if(lRes != ERROR_SUCCESS)
  4021. {
  4022. if(lRes == ERROR_FILE_NOT_FOUND)
  4023. return WBEM_S_NO_ERROR;
  4024. else
  4025. return WBEM_E_FAILED;
  4026. }
  4027. CFileCache::CFindCloseMe fcm(g_Glob.GetFileCache(), hSearch);
  4028. //
  4029. // Get Class definition
  4030. //
  4031. _IWmiObject* pClass = NULL;
  4032. hres = GetClassByHash(wszClassHash, false, &pClass, NULL, NULL, NULL);
  4033. if(FAILED(hres))
  4034. return hres;
  4035. CReleaseMe rm1(pClass);
  4036. //
  4037. // Prepare a buffer for file path
  4038. //
  4039. CFileName wszFullFileName;
  4040. if (wszFullFileName == NULL)
  4041. return WBEM_E_OUT_OF_MEMORY;
  4042. hres = ConstructKeyRootDirFromClassHash(wszFullFileName, wszClassHash);
  4043. if(FAILED(hres))
  4044. return hres;
  4045. long lDirLen = wcslen(wszFullFileName);
  4046. wszFullFileName[lDirLen] = L'\\';
  4047. lDirLen++;
  4048. do
  4049. {
  4050. hres = ConstructInstDefNameFromLinkName(wszFullFileName+lDirLen,
  4051. fd.cFileName);
  4052. if(FAILED(hres))
  4053. return hres;
  4054. _IWmiObject* pInstance = NULL;
  4055. hres = FileToInstance(wszFullFileName, &pInstance, true);
  4056. if(FAILED(hres))
  4057. return hres;
  4058. CReleaseMe rm1(pInstance);
  4059. hres = pSink->Indicate(1, (IWbemClassObject**)&pInstance);
  4060. if(FAILED(hres))
  4061. return hres;
  4062. }
  4063. while(g_Glob.GetFileCache()->FindNext(hSearch, &fd) == ERROR_SUCCESS);
  4064. return S_OK;
  4065. }
  4066. CHR CNamespaceHandle::ExecReferencesQuery(LPCWSTR wszQuery,
  4067. IWbemObjectSink* pSink)
  4068. {
  4069. if (g_bShuttingDown)
  4070. return WBEM_E_SHUTTING_DOWN;
  4071. HRESULT hres;
  4072. //
  4073. // Make a copy for parsing
  4074. //
  4075. LPWSTR wszParse = new WCHAR[wcslen(wszQuery)+1];
  4076. if (wszParse == NULL)
  4077. return WBEM_E_OUT_OF_MEMORY;
  4078. CVectorDeleteMe<WCHAR> vdm(wszParse);
  4079. wcscpy(wszParse, wszQuery);
  4080. //
  4081. // Extract the path of the target object.
  4082. //
  4083. //
  4084. // Find the first brace
  4085. //
  4086. WCHAR* pwcStart = wcschr(wszParse, L'{');
  4087. if(pwcStart == NULL)
  4088. return WBEM_E_INVALID_QUERY;
  4089. //
  4090. // Find the beginning of the path
  4091. //
  4092. while(*pwcStart && iswspace(*pwcStart)) pwcStart++;
  4093. if(!*pwcStart)
  4094. return WBEM_E_INVALID_QUERY;
  4095. pwcStart++;
  4096. //
  4097. // Find the ending curly brace
  4098. //
  4099. WCHAR* pwc = pwcStart;
  4100. WCHAR wcCurrentQuote = 0;
  4101. while(*pwc && (wcCurrentQuote || *pwc != L'}'))
  4102. {
  4103. if(wcCurrentQuote)
  4104. {
  4105. if(*pwc == L'\\')
  4106. {
  4107. pwc++;
  4108. }
  4109. else if(*pwc == wcCurrentQuote)
  4110. wcCurrentQuote = 0;
  4111. }
  4112. else if(*pwc == L'\'' || *pwc == L'"')
  4113. wcCurrentQuote = *pwc;
  4114. pwc++;
  4115. }
  4116. if(*pwc != L'}')
  4117. return WBEM_E_INVALID_QUERY;
  4118. //
  4119. // Find the end of the path
  4120. //
  4121. WCHAR* pwcEnd = pwc-1;
  4122. while(iswspace(*pwcEnd)) pwcEnd--;
  4123. pwcEnd[1] = 0;
  4124. LPWSTR wszTargetPath = pwcStart;
  4125. if(wszTargetPath == NULL)
  4126. return WBEM_E_INVALID_QUERY;
  4127. //
  4128. // Parse the path
  4129. //
  4130. DWORD dwLen = (wcslen(wszTargetPath)+1) * sizeof(WCHAR);
  4131. LPWSTR wszKey = (LPWSTR)TempAlloc(dwLen);
  4132. if(wszKey == NULL)
  4133. return WBEM_E_OUT_OF_MEMORY;
  4134. CTempFreeMe tfm(wszKey, dwLen);
  4135. LPWSTR wszClassName = NULL;
  4136. bool bIsClass;
  4137. hres = ComputeKeyFromPath(wszTargetPath, wszKey, &wszClassName, &bIsClass);
  4138. if(FAILED(hres))
  4139. return hres;
  4140. CTempFreeMe tfm1(wszClassName, (wcslen(wszClassName)+1) * sizeof(WCHAR*));
  4141. if(bIsClass)
  4142. {
  4143. //
  4144. // Need to execute an instance reference query to find all instances
  4145. // pointing to this class
  4146. //
  4147. hres = ExecInstanceRefQuery(wszQuery, NULL, wszClassName, pSink);
  4148. if(FAILED(hres))
  4149. return hres;
  4150. hres = ExecClassRefQuery(wszQuery, wszClassName, pSink);
  4151. if(FAILED(hres))
  4152. return hres;
  4153. }
  4154. else
  4155. {
  4156. hres = ExecInstanceRefQuery(wszQuery, wszClassName, wszKey, pSink);
  4157. if(FAILED(hres))
  4158. return hres;
  4159. }
  4160. return S_OK;
  4161. }
  4162. CHR CNamespaceHandle::ExecInstanceRefQuery(LPCWSTR wszQuery,
  4163. LPCWSTR wszClassName,
  4164. LPCWSTR wszKey,
  4165. IWbemObjectSink* pSink)
  4166. {
  4167. if (g_bShuttingDown)
  4168. return WBEM_E_SHUTTING_DOWN;
  4169. HRESULT hres;
  4170. //
  4171. // Find the instance's ref dir.
  4172. //
  4173. CFileName wszReferenceDir;
  4174. if (wszReferenceDir == NULL)
  4175. return WBEM_E_OUT_OF_MEMORY;
  4176. hres = ConstructReferenceDirFromKey(wszClassName, wszKey, wszReferenceDir);
  4177. if(FAILED(hres))
  4178. return hres;
  4179. CFileName wszReferenceMask;
  4180. if (wszReferenceMask == NULL)
  4181. return WBEM_E_OUT_OF_MEMORY;
  4182. wcscpy(wszReferenceMask, wszReferenceDir);
  4183. wcscat(wszReferenceMask, L"\\" A51_REF_FILE_PREFIX);
  4184. //
  4185. // Enumerate all files in it
  4186. //
  4187. WIN32_FIND_DATAW fd;
  4188. void* hSearch;
  4189. long lRes = g_Glob.GetFileCache()->FindFirst(wszReferenceMask, &fd, &hSearch);
  4190. if(lRes == ERROR_FILE_NOT_FOUND || lRes == ERROR_PATH_NOT_FOUND)
  4191. {
  4192. //
  4193. // No files in dir --- no problem
  4194. //
  4195. return WBEM_S_NO_ERROR;
  4196. }
  4197. else if(lRes != ERROR_SUCCESS)
  4198. {
  4199. return WBEM_E_FAILED;
  4200. }
  4201. CFileCache::CFindCloseMe fcm(g_Glob.GetFileCache(), hSearch);
  4202. //
  4203. // Prepare a buffer for file path
  4204. //
  4205. CFileName wszFullFileName;
  4206. if (wszFullFileName == NULL)
  4207. return WBEM_E_OUT_OF_MEMORY;
  4208. wcscpy(wszFullFileName, wszReferenceDir);
  4209. wcscat(wszFullFileName, L"\\");
  4210. long lDirLen = wcslen(wszFullFileName);
  4211. HRESULT hresGlobal = WBEM_S_NO_ERROR;
  4212. CFileName wszReferrerFileName;
  4213. if (wszReferrerFileName == NULL)
  4214. return WBEM_E_OUT_OF_MEMORY;
  4215. wcscpy(wszReferrerFileName, g_Glob.GetRootDir());
  4216. do
  4217. {
  4218. if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  4219. continue;
  4220. wcscpy(wszFullFileName+lDirLen, fd.cFileName);
  4221. LPWSTR wszReferrerClass = NULL;
  4222. LPWSTR wszReferrerProp = NULL;
  4223. LPWSTR wszReferrerNamespace = NULL;
  4224. hres = GetReferrerFromFile(wszFullFileName,
  4225. wszReferrerFileName + g_Glob.GetRootDirLen(),
  4226. &wszReferrerNamespace,
  4227. &wszReferrerClass, &wszReferrerProp);
  4228. if(FAILED(hres))
  4229. continue;
  4230. CVectorDeleteMe<WCHAR> vdm1(wszReferrerClass);
  4231. CVectorDeleteMe<WCHAR> vdm2(wszReferrerProp);
  4232. CVectorDeleteMe<WCHAR> vdm3(wszReferrerNamespace);
  4233. //
  4234. // Check if the namespace of the referring object is the same as ours
  4235. //
  4236. CNamespaceHandle* pReferrerHandle = NULL;
  4237. if(wbem_wcsicmp(wszReferrerNamespace, m_wsNamespace))
  4238. {
  4239. //
  4240. // Open the other namespace
  4241. //
  4242. hres = m_pRepository->GetNamespaceHandle(wszReferrerNamespace,
  4243. &pReferrerHandle);
  4244. if(FAILED(hres))
  4245. {
  4246. ERRORTRACE((LOG_WBEMCORE, "Unable to open referring namespace "
  4247. "'%S' in namespace '%S'\n", wszReferrerNamespace,
  4248. (LPCWSTR)m_wsNamespace));
  4249. hresGlobal = hres;
  4250. continue;
  4251. }
  4252. }
  4253. else
  4254. {
  4255. pReferrerHandle = this;
  4256. pReferrerHandle->AddRef();
  4257. }
  4258. CReleaseMe rm1(pReferrerHandle);
  4259. _IWmiObject* pInstance = NULL;
  4260. hres = pReferrerHandle->FileToInstance(wszReferrerFileName, &pInstance);
  4261. if(FAILED(hres))
  4262. {
  4263. // Oh well --- continue;
  4264. hresGlobal = hres;
  4265. }
  4266. else
  4267. {
  4268. CReleaseMe rm1(pInstance);
  4269. hres = pSink->Indicate(1, (IWbemClassObject**)&pInstance);
  4270. if(FAILED(hres))
  4271. return hres;
  4272. }
  4273. }
  4274. while(g_Glob.GetFileCache()->FindNext(hSearch, &fd) == ERROR_SUCCESS);
  4275. return hresGlobal;
  4276. }
  4277. CHR CNamespaceHandle::GetReferrerFromFile(LPCWSTR wszReferenceFile,
  4278. LPWSTR wszReferrerRelFile,
  4279. LPWSTR* pwszReferrerNamespace,
  4280. LPWSTR* pwszReferrerClass,
  4281. LPWSTR* pwszReferrerProp)
  4282. {
  4283. //
  4284. // Get the entire buffer from the file
  4285. //
  4286. BYTE* pBuffer = NULL;
  4287. DWORD dwBufferLen = 0;
  4288. long lRes = g_Glob.GetFileCache()->ReadFile(wszReferenceFile, &dwBufferLen,
  4289. &pBuffer);
  4290. if(lRes != ERROR_SUCCESS)
  4291. return WBEM_E_FAILED;
  4292. CTempFreeMe tfm(pBuffer, dwBufferLen);
  4293. if(dwBufferLen == 0)
  4294. return WBEM_E_OUT_OF_MEMORY;
  4295. BYTE* pCurrent = pBuffer;
  4296. DWORD dwStringLen;
  4297. //
  4298. // Get the referrer namespace
  4299. //
  4300. memcpy(&dwStringLen, pCurrent, sizeof(DWORD));
  4301. pCurrent += sizeof(DWORD);
  4302. *pwszReferrerNamespace = new WCHAR[dwStringLen+1];
  4303. if (*pwszReferrerNamespace == NULL)
  4304. return WBEM_E_OUT_OF_MEMORY;
  4305. (*pwszReferrerNamespace)[dwStringLen] = 0;
  4306. memcpy(*pwszReferrerNamespace, pCurrent, dwStringLen*sizeof(WCHAR));
  4307. pCurrent += sizeof(WCHAR)*dwStringLen;
  4308. //
  4309. // Get the referrer class name
  4310. //
  4311. memcpy(&dwStringLen, pCurrent, sizeof(DWORD));
  4312. pCurrent += sizeof(DWORD);
  4313. *pwszReferrerClass = new WCHAR[dwStringLen+1];
  4314. if (*pwszReferrerClass == NULL)
  4315. return WBEM_E_OUT_OF_MEMORY;
  4316. (*pwszReferrerClass)[dwStringLen] = 0;
  4317. memcpy(*pwszReferrerClass, pCurrent, dwStringLen*sizeof(WCHAR));
  4318. pCurrent += sizeof(WCHAR)*dwStringLen;
  4319. //
  4320. // Get the referrer property
  4321. //
  4322. memcpy(&dwStringLen, pCurrent, sizeof(DWORD));
  4323. pCurrent += sizeof(DWORD);
  4324. *pwszReferrerProp = new WCHAR[dwStringLen+1];
  4325. if (*pwszReferrerProp == NULL)
  4326. return WBEM_E_OUT_OF_MEMORY;
  4327. (*pwszReferrerProp)[dwStringLen] = 0;
  4328. memcpy(*pwszReferrerProp, pCurrent, dwStringLen*sizeof(WCHAR));
  4329. pCurrent += sizeof(WCHAR)*dwStringLen;
  4330. //
  4331. // Get referrer file path
  4332. //
  4333. memcpy(&dwStringLen, pCurrent, sizeof(DWORD));
  4334. pCurrent += sizeof(DWORD);
  4335. wszReferrerRelFile[dwStringLen] = 0;
  4336. memcpy(wszReferrerRelFile, pCurrent, dwStringLen*sizeof(WCHAR));
  4337. pCurrent += sizeof(WCHAR)*dwStringLen;
  4338. return S_OK;
  4339. }
  4340. CHR CNamespaceHandle::ExecClassRefQuery(LPCWSTR wszQuery,
  4341. LPCWSTR wszClassName,
  4342. IWbemObjectSink* pSink)
  4343. {
  4344. if (g_bShuttingDown)
  4345. return WBEM_E_SHUTTING_DOWN;
  4346. HRESULT hres;
  4347. //Execute against system class namespace first
  4348. if (g_pSystemClassNamespace && (wcscmp(m_wsNamespace, A51_SYSTEMCLASS_NS) != 0))
  4349. {
  4350. hres = g_pSystemClassNamespace->ExecClassRefQuery(wszQuery, wszClassName, pSink);
  4351. if (FAILED(hres))
  4352. return hres;
  4353. }
  4354. //
  4355. // Find the class's ref dir.
  4356. //
  4357. CFileName wszReferenceDir;
  4358. if (wszReferenceDir == NULL)
  4359. return WBEM_E_OUT_OF_MEMORY;
  4360. hres = ConstructClassRelationshipsDir(wszClassName, wszReferenceDir);
  4361. CFileName wszReferenceMask;
  4362. if (wszReferenceMask == NULL)
  4363. return WBEM_E_OUT_OF_MEMORY;
  4364. wcscpy(wszReferenceMask, wszReferenceDir);
  4365. wcscat(wszReferenceMask, L"\\" A51_REF_FILE_PREFIX);
  4366. //
  4367. // Enumerate all files in it
  4368. //
  4369. WIN32_FIND_DATAW fd;
  4370. void* hSearch;
  4371. long lRes = g_Glob.GetFileCache()->FindFirst(wszReferenceMask, &fd, &hSearch);
  4372. if(lRes == ERROR_FILE_NOT_FOUND || lRes == ERROR_PATH_NOT_FOUND)
  4373. {
  4374. //
  4375. // No files in dir --- no problem
  4376. //
  4377. return WBEM_S_NO_ERROR;
  4378. }
  4379. else if(lRes != ERROR_SUCCESS)
  4380. {
  4381. return WBEM_E_FAILED;
  4382. }
  4383. CFileCache::CFindCloseMe fcm(g_Glob.GetFileCache(), hSearch);
  4384. do
  4385. {
  4386. //
  4387. // Extract the class hash from the name of the file
  4388. //
  4389. LPCWSTR wszReferrerHash = fd.cFileName + wcslen(A51_REF_FILE_PREFIX);
  4390. //
  4391. // Get the class from that hash
  4392. //
  4393. _IWmiObject* pClass = NULL;
  4394. hres = GetClassByHash(wszReferrerHash, true, &pClass, NULL, NULL, NULL);
  4395. if(FAILED(hres))
  4396. {
  4397. if(hres == WBEM_E_NOT_FOUND)
  4398. continue;
  4399. else
  4400. return hres;
  4401. }
  4402. CReleaseMe rm1(pClass);
  4403. hres = pSink->Indicate(1, (IWbemClassObject**)&pClass);
  4404. if(FAILED(hres))
  4405. return hres;
  4406. }
  4407. while(g_Glob.GetFileCache()->FindNext(hSearch, &fd) == ERROR_SUCCESS);
  4408. return S_OK;
  4409. }
  4410. bool CNamespaceHandle::Hash(LPCWSTR wszName, LPWSTR wszHash)
  4411. {
  4412. return A51Hash(wszName, wszHash);
  4413. }
  4414. CHR CNamespaceHandle::InstanceToFile(IWbemClassObject* pInst,
  4415. LPCWSTR wszClassName, LPCWSTR wszFileName,
  4416. __int64 nClassTime)
  4417. {
  4418. HRESULT hres;
  4419. //
  4420. // Allocate enough space for the buffer
  4421. //
  4422. _IWmiObject* pInstEx;
  4423. pInst->QueryInterface(IID__IWmiObject, (void**)&pInstEx);
  4424. CReleaseMe rm1(pInstEx);
  4425. DWORD dwInstancePartLen = 0;
  4426. hres = pInstEx->Unmerge(0, 0, &dwInstancePartLen, NULL);
  4427. //
  4428. // Add enough room for the class hash
  4429. //
  4430. DWORD dwClassHashLen = MAX_HASH_LEN * sizeof(WCHAR);
  4431. DWORD dwTotalLen = dwInstancePartLen + dwClassHashLen + sizeof(__int64)*2;
  4432. BYTE* pBuffer = (BYTE*)TempAlloc(dwTotalLen);
  4433. if (pBuffer == NULL)
  4434. return WBEM_E_OUT_OF_MEMORY;
  4435. CTempFreeMe vdm(pBuffer, dwTotalLen);
  4436. //
  4437. // Write the class hash
  4438. //
  4439. if(!Hash(wszClassName, (LPWSTR)pBuffer))
  4440. return WBEM_E_OUT_OF_MEMORY;
  4441. memcpy(pBuffer + dwClassHashLen, &g_nCurrentTime, sizeof(__int64));
  4442. g_nCurrentTime++;
  4443. memcpy(pBuffer + dwClassHashLen + sizeof(__int64), &nClassTime,
  4444. sizeof(__int64));
  4445. //
  4446. // Unmerge the instance into a buffer
  4447. //
  4448. DWORD dwLen;
  4449. hres = pInstEx->Unmerge(0, dwInstancePartLen, &dwLen,
  4450. pBuffer + dwClassHashLen + sizeof(__int64)*2);
  4451. if(FAILED(hres))
  4452. return hres;
  4453. //
  4454. // Write to the file only as much as we have actually used!
  4455. //
  4456. long lRes = g_Glob.GetFileCache()->WriteFile(wszFileName,
  4457. dwClassHashLen + sizeof(__int64)*2 + dwLen, pBuffer);
  4458. if(lRes != ERROR_SUCCESS)
  4459. return WBEM_E_FAILED;
  4460. return WBEM_S_NO_ERROR;
  4461. }
  4462. CHR CNamespaceHandle::ClassToFile(_IWmiObject* pParentClass,
  4463. _IWmiObject* pClass, LPCWSTR wszFileName,
  4464. __int64 nFakeUpdateTime)
  4465. {
  4466. HRESULT hres;
  4467. //
  4468. // Get superclass name
  4469. //
  4470. VARIANT vSuper;
  4471. hres = pClass->Get(L"__SUPERCLASS", 0, &vSuper, NULL, NULL);
  4472. if(FAILED(hres))
  4473. return hres;
  4474. CClearMe cm1(&vSuper);
  4475. LPCWSTR wszSuper;
  4476. if(V_VT(&vSuper) == VT_BSTR)
  4477. wszSuper = V_BSTR(&vSuper);
  4478. else
  4479. wszSuper = L"";
  4480. VARIANT vClassName;
  4481. hres = pClass->Get(L"__CLASS", 0, &vClassName, NULL, NULL);
  4482. if(FAILED(hres))
  4483. return hres;
  4484. CClearMe cm2(&vClassName);
  4485. LPCWSTR wszClassName;
  4486. if(V_VT(&vClassName) == VT_BSTR)
  4487. wszClassName = V_BSTR(&vClassName);
  4488. else
  4489. wszClassName = L"";
  4490. //
  4491. // Get unmerge length
  4492. //
  4493. DWORD dwUnmergedLen = 0;
  4494. hres = pClass->Unmerge(0, 0, &dwUnmergedLen, NULL);
  4495. //
  4496. // Add enough space for the parent class name and the timestamp
  4497. //
  4498. DWORD dwSuperLen = sizeof(DWORD) + wcslen(wszSuper)*sizeof(WCHAR);
  4499. DWORD dwLen = dwUnmergedLen + dwSuperLen + sizeof(__int64);
  4500. BYTE* pBuffer = (BYTE*)TempAlloc(dwLen);
  4501. if (pBuffer == NULL)
  4502. return WBEM_E_OUT_OF_MEMORY;
  4503. CTempFreeMe vdm(pBuffer, dwLen);
  4504. //
  4505. // Write superclass name
  4506. //
  4507. DWORD dwActualSuperLen = wcslen(wszSuper);
  4508. memcpy(pBuffer, &dwActualSuperLen, sizeof(DWORD));
  4509. memcpy(pBuffer + sizeof(DWORD), wszSuper, wcslen(wszSuper)*sizeof(WCHAR));
  4510. //
  4511. // Write the timestamp
  4512. //
  4513. if(nFakeUpdateTime == 0)
  4514. {
  4515. nFakeUpdateTime = g_nCurrentTime;
  4516. g_nCurrentTime++;
  4517. }
  4518. memcpy(pBuffer + dwSuperLen, &nFakeUpdateTime, sizeof(__int64));
  4519. //
  4520. // Write the unmerged portion
  4521. //
  4522. BYTE* pUnmergedPortion = pBuffer + dwSuperLen + sizeof(__int64);
  4523. hres = pClass->Unmerge(0, dwUnmergedLen, &dwUnmergedLen,
  4524. pUnmergedPortion);
  4525. if(FAILED(hres))
  4526. return hres;
  4527. //
  4528. // Stash away the real length
  4529. //
  4530. DWORD dwFileLen = dwUnmergedLen + dwSuperLen + sizeof(__int64);
  4531. long lRes = g_Glob.GetFileCache()->WriteFile(wszFileName, dwFileLen, pBuffer);
  4532. if(lRes != ERROR_SUCCESS)
  4533. return WBEM_E_FAILED;
  4534. //
  4535. // To properly cache the new class definition, first invalidate it
  4536. //
  4537. hres = m_pClassCache->InvalidateClass(wszClassName);
  4538. if(FAILED(hres))
  4539. return hres;
  4540. //
  4541. // Now, remerge the unmerged portion back in
  4542. //
  4543. if(pParentClass == NULL)
  4544. {
  4545. //
  4546. // Get the empty class
  4547. //
  4548. hres = GetClassDirect(NULL, IID__IWmiObject, (void**)&pParentClass,
  4549. false, NULL, NULL, NULL);
  4550. if(FAILED(hres))
  4551. return hres;
  4552. }
  4553. else
  4554. pParentClass->AddRef();
  4555. CReleaseMe rm0(pParentClass);
  4556. _IWmiObject* pNewObj;
  4557. hres = pParentClass->Merge(0, dwUnmergedLen, pUnmergedPortion, &pNewObj);
  4558. if(FAILED(hres))
  4559. return hres;
  4560. CReleaseMe rm1(pNewObj);
  4561. hres = pNewObj->SetDecoration(m_wszMachineName, m_wsNamespace);
  4562. if(FAILED(hres))
  4563. return hres;
  4564. hres = m_pClassCache->AssertClass(pNewObj, wszClassName, false,
  4565. nFakeUpdateTime, false);
  4566. if(FAILED(hres))
  4567. return hres;
  4568. return WBEM_S_NO_ERROR;
  4569. }
  4570. CHR CNamespaceHandle::ConstructInstanceDefName(LPWSTR wszInstanceDefName,
  4571. LPCWSTR wszKey)
  4572. {
  4573. wcscpy(wszInstanceDefName, A51_INSTDEF_FILE_PREFIX);
  4574. if(!Hash(wszKey, wszInstanceDefName + wcslen(A51_INSTDEF_FILE_PREFIX)))
  4575. {
  4576. return WBEM_E_OUT_OF_MEMORY;
  4577. }
  4578. return WBEM_S_NO_ERROR;
  4579. }
  4580. CHR CNamespaceHandle::ConstructInstDefNameFromLinkName(
  4581. LPWSTR wszInstanceDefName,
  4582. LPCWSTR wszInstanceLinkName)
  4583. {
  4584. wcscpy(wszInstanceDefName, A51_INSTDEF_FILE_PREFIX);
  4585. wcscat(wszInstanceDefName,
  4586. wszInstanceLinkName + wcslen(A51_INSTLINK_FILE_PREFIX));
  4587. return WBEM_S_NO_ERROR;
  4588. }
  4589. CHR CNamespaceHandle::ConstructClassDefFileName(LPCWSTR wszClassName,
  4590. LPWSTR wszFileName)
  4591. {
  4592. wcscpy(wszFileName, A51_CLASSDEF_FILE_PREFIX);
  4593. if(!Hash(wszClassName, wszFileName+wcslen(A51_CLASSDEF_FILE_PREFIX)))
  4594. return WBEM_E_INVALID_OBJECT;
  4595. return WBEM_S_NO_ERROR;
  4596. }
  4597. CHR CNamespaceHandle::ConstructClassDefFileNameFromHash(LPCWSTR wszHash,
  4598. LPWSTR wszFileName)
  4599. {
  4600. wcscpy(wszFileName, A51_CLASSDEF_FILE_PREFIX);
  4601. wcscat(wszFileName, wszHash);
  4602. return WBEM_S_NO_ERROR;
  4603. }
  4604. //=============================================================================
  4605. //
  4606. // CNamespaceHandle::CreateSystemClasses
  4607. //
  4608. // We are in a pseudo namespace. We need to determine if we already have
  4609. // the system classes in this namespace. The system classes that we create
  4610. // are those that exist in all namespaces, and no others. If they do not exist
  4611. // we create them.
  4612. // The whole creation process happens within the confines of a transaction
  4613. // that we create and own within this method.
  4614. //
  4615. //=============================================================================
  4616. HRESULT CNamespaceHandle::CreateSystemClasses(CFlexArray &aSystemClasses)
  4617. {
  4618. HRESULT hRes = WBEM_S_NO_ERROR;
  4619. //Now we need to determine if the system classes already exist. Lets do this by looking for the __thisnamespace
  4620. //class!
  4621. _IWmiObject *pObj = NULL;
  4622. hRes = GetClassDirect(L"__thisnamespace", IID__IWmiObject, (void**)&pObj, false, NULL, NULL, NULL);
  4623. if (SUCCEEDED(hRes))
  4624. {
  4625. //All done! They already exist!
  4626. pObj->Release();
  4627. return WBEM_S_NO_ERROR;
  4628. }
  4629. else if (hRes != WBEM_E_NOT_FOUND)
  4630. {
  4631. //Something went bad, so we just fail!
  4632. return hRes;
  4633. }
  4634. //There are no system classes so we need to create them.
  4635. hRes = g_Glob.GetFileCache()->BeginTransaction();
  4636. if(hRes != ERROR_SUCCESS)
  4637. hRes = A51TranslateErrorCode(hRes);
  4638. CEventCollector eventCollector;
  4639. _IWmiObject *Objects[256];
  4640. _IWmiObject **ppObjects = NULL;
  4641. ULONG uSize = 256;
  4642. if (SUCCEEDED(hRes) && aSystemClasses.Size())
  4643. {
  4644. //If we have a system-class array we need to use that instead of using the ones retrieved from the core
  4645. //not doing so will cause a mismatch. We retrieved these as part of the upgrade process...
  4646. uSize = aSystemClasses.Size();
  4647. ppObjects = (_IWmiObject**)&aSystemClasses[0];
  4648. }
  4649. else if (SUCCEEDED(hRes))
  4650. {
  4651. //None retrieved from upgrade process so we must be a clean install. Therefore we should
  4652. //get the list from the core...
  4653. _IWmiCoreServices * pSvcs = g_Glob.GetCoreSvcs();
  4654. CReleaseMe rm(pSvcs);
  4655. hRes = pSvcs->GetSystemObjects(GET_SYSTEM_STD_OBJECTS, &uSize, Objects);
  4656. ppObjects = Objects;
  4657. }
  4658. if (SUCCEEDED(hRes))
  4659. {
  4660. for (int i = 0; i < uSize; i++)
  4661. {
  4662. IWbemClassObject *pObj;
  4663. if (SUCCEEDED(hRes))
  4664. {
  4665. hRes = ppObjects[i]->QueryInterface(IID_IWbemClassObject, (LPVOID *) &pObj);
  4666. if (SUCCEEDED(hRes))
  4667. {
  4668. hRes = PutObject(IID_IWbemClassObject, pObj, WMIDB_DISABLE_EVENTS, 0, 0, eventCollector);
  4669. pObj->Release();
  4670. if (FAILED(hRes))
  4671. {
  4672. ERRORTRACE((LOG_WBEMCORE, "Creation of system class failed during repository creation <0x%X>!\n", hRes));
  4673. }
  4674. }
  4675. }
  4676. ppObjects[i]->Release();
  4677. }
  4678. }
  4679. //Clear out the array that was sent to us.
  4680. aSystemClasses.Empty();
  4681. if (FAILED(hRes))
  4682. {
  4683. g_Glob.GetFileCache()->AbortTransaction();
  4684. }
  4685. else
  4686. {
  4687. hRes = g_Glob.GetFileCache()->CommitTransaction();
  4688. if(hRes != ERROR_SUCCESS)
  4689. {
  4690. hRes = A51TranslateErrorCode(hRes);
  4691. g_Glob.GetFileCache()->AbortTransaction();
  4692. }
  4693. }
  4694. return hRes;
  4695. }
  4696. class CSystemClassDeletionSink : public CUnkBase<IWbemObjectSink, &IID_IWbemObjectSink>
  4697. {
  4698. CWStringArray &m_aChildNamespaces;
  4699. public:
  4700. CSystemClassDeletionSink(CWStringArray &aChildNamespaces)
  4701. : m_aChildNamespaces(aChildNamespaces)
  4702. {
  4703. }
  4704. ~CSystemClassDeletionSink()
  4705. {
  4706. }
  4707. STDMETHOD(Indicate)(long lNumObjects, IWbemClassObject** apObjects)
  4708. {
  4709. HRESULT hRes;
  4710. for (int i = 0; i != lNumObjects; i++)
  4711. {
  4712. if (apObjects[i] != NULL)
  4713. {
  4714. _IWmiObject *pInst = NULL;
  4715. hRes = apObjects[i]->QueryInterface(IID__IWmiObject, (void**)&pInst);
  4716. if (FAILED(hRes))
  4717. return hRes;
  4718. CReleaseMe rm(pInst);
  4719. BSTR strKey = NULL;
  4720. hRes = pInst->GetKeyString(0, &strKey);
  4721. if(FAILED(hRes))
  4722. return hRes;
  4723. CSysFreeMe sfm(strKey);
  4724. if (m_aChildNamespaces.Add(strKey) != CWStringArray::no_error)
  4725. return WBEM_E_OUT_OF_MEMORY;
  4726. }
  4727. }
  4728. return WBEM_S_NO_ERROR;
  4729. }
  4730. STDMETHOD(SetStatus)(long lFlags, HRESULT hresResult, BSTR, IWbemClassObject*)
  4731. {
  4732. return WBEM_S_NO_ERROR;
  4733. }
  4734. };
  4735. //=============================================================================
  4736. //
  4737. // CNamespaceHandle::RecursiveDeleteSystemClasses
  4738. //
  4739. // Delete the system class representation object from this namespace, then
  4740. // recurse through all sub-namespaces and do the same. It is expected that
  4741. // we already have a lock, and if anything fails we return and error, and
  4742. // the caller fails the owning transaction.
  4743. //
  4744. //=============================================================================
  4745. HRESULT CNamespaceHandle::RecursiveDeleteSystemClasses(CWStringArray &aSystemClasses,
  4746. CWStringArray &aSystemClassesSuperclass)
  4747. {
  4748. if (g_bShuttingDown)
  4749. return WBEM_E_SHUTTING_DOWN;
  4750. HRESULT hRes;
  4751. // Query for all child namespaces so we delete classes in them first...
  4752. CWStringArray aChildNamespaces;
  4753. CSystemClassDeletionSink *pSink = new CSystemClassDeletionSink(aChildNamespaces);
  4754. if (pSink == NULL)
  4755. return WBEM_E_OUT_OF_MEMORY;
  4756. pSink->AddRef();
  4757. CReleaseMe rm(pSink);
  4758. hRes = ExecInstanceQuery(NULL, L"__namespace", true, pSink);
  4759. if (FAILED(hRes))
  4760. return hRes;
  4761. //Now iterate through the list of namespaces... and do the deletion on them...
  4762. for (int i = 0; i != aChildNamespaces.Size(); i++)
  4763. {
  4764. CNamespaceHandle *pNs = new CNamespaceHandle(m_pControl, m_pRepository);
  4765. if (pNs == NULL)
  4766. return WBEM_E_OUT_OF_MEMORY;
  4767. CReleaseMe rm2(pNs);
  4768. pNs->AddRef();
  4769. int pNsPathLen = (wcslen(m_wsNamespace) + wcslen(aChildNamespaces[i]) + wcslen(L"\\") + 1) * sizeof(wchar_t);
  4770. wchar_t *pNsPath = (wchar_t*)TempAlloc(pNsPathLen);
  4771. if (pNsPath == NULL)
  4772. return WBEM_E_OUT_OF_MEMORY;
  4773. CTempFreeMe tfm(pNsPath, pNsPathLen);
  4774. wcscpy(pNsPath, m_wsNamespace);
  4775. wcscat(pNsPath, L"\\");
  4776. wcscat(pNsPath, aChildNamespaces[i]);
  4777. pNs->Initialize(pNsPath);
  4778. hRes = pNs->RecursiveDeleteSystemClasses(aSystemClasses, aSystemClassesSuperclass);
  4779. if (FAILED(hRes))
  4780. return hRes;
  4781. }
  4782. //Now the children are done, loop through each of the system classes to
  4783. //delete them from this one...
  4784. //----------------------------------------------------------------------
  4785. for (i = 0; i != aSystemClasses.Size(); i++)
  4786. {
  4787. //Construct class filename
  4788. //------------------------
  4789. CFileName wszFileName;
  4790. if (wszFileName == NULL)
  4791. return WBEM_E_OUT_OF_MEMORY;
  4792. hRes = ConstructClassDefFileName(aSystemClasses[i], wszFileName);
  4793. if(FAILED(hRes))
  4794. return hRes;
  4795. //Construct the full path and filename of the class
  4796. //-------------------------------------------------
  4797. CFileName wszFilePath;
  4798. if (wszFilePath == NULL)
  4799. return WBEM_E_OUT_OF_MEMORY;
  4800. swprintf(wszFilePath, L"%s\\%s", m_wszClassRootDir, wszFileName);
  4801. //Need to delete our parent/child class relationship...
  4802. //--------------------------------------------------------
  4803. EraseParentChildRelationship(wszFileName, aSystemClassesSuperclass[i]);
  4804. //Next we need to delete our representation object
  4805. //------------------------------------------------
  4806. hRes = g_Glob.GetFileCache()->DeleteFile(wszFilePath);
  4807. if(hRes == ERROR_FILE_NOT_FOUND || hRes == ERROR_PATH_NOT_FOUND)
  4808. hRes = WBEM_S_NO_ERROR;
  4809. else if(hRes != ERROR_SUCCESS)
  4810. return WBEM_E_FAILED;
  4811. m_pClassCache->InvalidateClass(aSystemClasses[i]);
  4812. }
  4813. return WBEM_S_NO_ERROR;
  4814. }
  4815. //=============================================================================
  4816. //=============================================================================
  4817. CDbIterator::CDbIterator(CLifeControl* pControl, bool bUseLock)
  4818. : TUnkBase(pControl), m_lCurrentIndex(0), m_hresStatus(WBEM_S_FALSE),
  4819. m_pMainFiber(NULL), m_pExecFiber(NULL), m_dwNumRequested(0),
  4820. m_pExecReq(NULL), m_hresCancellationStatus(WBEM_S_NO_ERROR),
  4821. m_bExecFiberRunning(false), m_bUseLock(bUseLock)
  4822. {
  4823. }
  4824. CDbIterator::~CDbIterator()
  4825. {
  4826. #ifdef A51_USE_FIBER
  4827. if(m_pExecFiber)
  4828. Cancel(0);
  4829. #endif
  4830. if(m_pExecReq)
  4831. delete m_pExecReq;
  4832. }
  4833. void CDbIterator::SetExecFiber(void* pFiber, CFiberTask* pReq)
  4834. {
  4835. m_pExecFiber = pFiber;
  4836. m_pExecReq = pReq;
  4837. }
  4838. STDMETHODIMP CDbIterator::Cancel(DWORD dwFlags)
  4839. {
  4840. CInCritSec ics(&m_cs);
  4841. m_qObjects.Clear();
  4842. #ifdef A51_USE_FIBER
  4843. //
  4844. // Mark the iterator as cancelled and allow the execution fiber to resume
  4845. // and complete --- that guarantees that any memory it allocated will be
  4846. // cleaned up. The exception to this rule is if the fiber has not started
  4847. // execution yet; in that case, we do not want to switch to it, as it would
  4848. // have to run until the first Indicate to find out that it's been
  4849. // cancelled. (In the normal case, the execution fiber is suspended
  4850. // inside Indicate, so when we switch back we will immediately give it
  4851. // WBEM_E_CALL_CANCELLED so that it can clean up and return)
  4852. //
  4853. m_hresCancellationStatus = WBEM_E_CALL_CANCELLED;
  4854. if(m_pExecFiber)
  4855. {
  4856. if(m_bExecFiberRunning)
  4857. {
  4858. _ASSERT(m_pMainFiber == NULL && m_pExecFiber != NULL,
  4859. L"Fiber trouble");
  4860. //
  4861. // Make sure the calling thread has a fiber
  4862. //
  4863. m_pMainFiber = CreateOrGetCurrentFiber();
  4864. if(m_pMainFiber == NULL)
  4865. return WBEM_E_OUT_OF_MEMORY;
  4866. SwitchToFiber(m_pExecFiber);
  4867. }
  4868. //
  4869. // At this point, the executing fiber is dead. We know, because in the
  4870. // cancelled state we do not switch to the main fiber in Indicate.
  4871. //
  4872. ReturnFiber(m_pExecFiber);
  4873. m_pExecFiber = NULL;
  4874. }
  4875. #endif
  4876. return S_OK;
  4877. }
  4878. STDMETHODIMP CDbIterator::NextBatch(
  4879. DWORD dwNumRequested,
  4880. DWORD dwTimeOutSeconds,
  4881. DWORD dwFlags,
  4882. DWORD dwRequestedHandleType,
  4883. REFIID riid,
  4884. DWORD *pdwNumReturned,
  4885. LPVOID *ppObjects
  4886. )
  4887. {
  4888. CInCritSec ics(&m_cs);
  4889. //
  4890. // TEMP CODE: Someone is calling us on an impersonated thread. Let's catch
  4891. // the, ahem, bastard
  4892. //
  4893. HANDLE hToken;
  4894. BOOL bRes = OpenThreadToken(GetCurrentThread(), TOKEN_READ, TRUE, &hToken);
  4895. if(bRes)
  4896. {
  4897. //_ASSERT(false, L"Called with a thread token");
  4898. ERRORTRACE((LOG_WBEMCORE, "Repository called with a thread token! "
  4899. "It shall be removed\n"));
  4900. CloseHandle(hToken);
  4901. SetThreadToken(NULL, NULL);
  4902. }
  4903. _ASSERT(SUCCEEDED(m_hresCancellationStatus), L"Next called after Cancel");
  4904. #ifdef A51_USE_FIBER
  4905. m_bExecFiberRunning = true;
  4906. //
  4907. // Wait until it's over or the right number of objects has been received
  4908. //
  4909. if(m_qObjects.GetQueueSize() < dwNumRequested)
  4910. {
  4911. _ASSERT(m_pMainFiber == NULL && m_pExecFiber != NULL, L"Fiber trouble");
  4912. //
  4913. // Make sure the calling thread has a fiber
  4914. //
  4915. m_pMainFiber = CreateOrGetCurrentFiber();
  4916. if(m_pMainFiber == NULL)
  4917. return WBEM_E_OUT_OF_MEMORY;
  4918. m_dwNumRequested = dwNumRequested;
  4919. //
  4920. // We need to acquire the read lock for the duration of the continuation
  4921. // of the retrieval
  4922. //
  4923. {
  4924. CAutoReadLock lock(&g_readWriteLock, FALSE);
  4925. if (m_bUseLock)
  4926. {
  4927. lock.Lock();
  4928. }
  4929. if (g_bShuttingDown)
  4930. {
  4931. m_pMainFiber = NULL;
  4932. return WBEM_E_SHUTTING_DOWN;
  4933. }
  4934. SwitchToFiber(m_pExecFiber);
  4935. }
  4936. m_pMainFiber = NULL;
  4937. }
  4938. #endif
  4939. //
  4940. // We have as much as we are going to have!
  4941. //
  4942. DWORD dwReqIndex = 0;
  4943. while(dwReqIndex < dwNumRequested)
  4944. {
  4945. if(0 == m_qObjects.GetQueueSize())
  4946. {
  4947. //
  4948. // That's it --- we waited for production, so there are simply no
  4949. // more objects in the enumeration
  4950. //
  4951. *pdwNumReturned = dwReqIndex;
  4952. return m_hresStatus;
  4953. }
  4954. IWbemClassObject* pObj = m_qObjects.Dequeue();
  4955. CReleaseMe rm1(pObj);
  4956. pObj->QueryInterface(riid, ppObjects + dwReqIndex);
  4957. dwReqIndex++;
  4958. }
  4959. //
  4960. // Got everything
  4961. //
  4962. *pdwNumReturned= dwNumRequested;
  4963. return S_OK;
  4964. }
  4965. HRESULT CDbIterator::Indicate(long lNumObjects, IWbemClassObject** apObjects)
  4966. {
  4967. #ifdef A51_USE_FIBER
  4968. if(FAILED(m_hresCancellationStatus))
  4969. {
  4970. //
  4971. // --- the fiber called back with Indicate even after we
  4972. // cancelled! Oh well.
  4973. //
  4974. _ASSERT(false, L"Execution code ignored cancel return code!");
  4975. return m_hresCancellationStatus;
  4976. }
  4977. #endif
  4978. //
  4979. // Add the objects received to the array
  4980. //
  4981. for(long i = 0; i < lNumObjects; i++)
  4982. {
  4983. m_qObjects.Enqueue(apObjects[i]);
  4984. }
  4985. #ifdef A51_USE_FIBER
  4986. //
  4987. // Check if we have compiled enough for the current request and should
  4988. // therefore interrupt the gatherer
  4989. //
  4990. if(m_qObjects.GetQueueSize() >= m_dwNumRequested)
  4991. {
  4992. //
  4993. // Switch us back to the original fiber
  4994. //
  4995. SwitchToFiber(m_pMainFiber);
  4996. }
  4997. #endif
  4998. return m_hresCancellationStatus;
  4999. }
  5000. HRESULT CDbIterator::SetStatus(long lFlags, HRESULT hresResult,
  5001. BSTR, IWbemClassObject*)
  5002. {
  5003. _ASSERT(m_hresStatus == WBEM_S_FALSE, L"SetStatus called twice!");
  5004. _ASSERT(lFlags == WBEM_STATUS_COMPLETE, L"SetStatus flags invalid");
  5005. m_hresStatus = hresResult;
  5006. #ifdef A51_USE_FIBER
  5007. //
  5008. // Switch us back to the original thread, we are done
  5009. //
  5010. m_bExecFiberRunning = false;
  5011. SwitchToFiber(m_pMainFiber);
  5012. #endif
  5013. return WBEM_S_NO_ERROR;
  5014. }
  5015. CRepEvent::CRepEvent(DWORD dwType, LPCWSTR wszNamespace, LPCWSTR wszArg1,
  5016. _IWmiObject* pObj1, _IWmiObject* pObj2)
  5017. {
  5018. m_dwType = dwType;
  5019. m_pObj1 = 0;
  5020. m_pObj2 = 0;
  5021. m_wszArg1 = m_wszNamespace = NULL;
  5022. if (wszArg1)
  5023. {
  5024. m_wszArg1 = (WCHAR*)TempAlloc((wcslen(wszArg1)+1)*sizeof(WCHAR));
  5025. if (m_wszArg1 == NULL)
  5026. throw CX_MemoryException();
  5027. wcscpy(m_wszArg1, wszArg1);
  5028. }
  5029. if (wszNamespace)
  5030. {
  5031. m_wszNamespace = (WCHAR*)TempAlloc((wcslen(wszNamespace)+1)*sizeof(WCHAR));
  5032. if (m_wszNamespace == NULL)
  5033. throw CX_MemoryException();
  5034. wcscpy(m_wszNamespace, wszNamespace);
  5035. }
  5036. if (pObj1)
  5037. {
  5038. m_pObj1 = pObj1;
  5039. pObj1->AddRef();
  5040. }
  5041. if (pObj2)
  5042. {
  5043. m_pObj2 = pObj2;
  5044. pObj2->AddRef();
  5045. }
  5046. }
  5047. CRepEvent::~CRepEvent()
  5048. {
  5049. TempFree(m_wszArg1, (wcslen(m_wszArg1)+1)*sizeof(WCHAR));
  5050. TempFree(m_wszNamespace, (wcslen(m_wszNamespace)+1)*sizeof(WCHAR));
  5051. if (m_pObj1)
  5052. m_pObj1->Release();
  5053. if (m_pObj2)
  5054. m_pObj2->Release();
  5055. };
  5056. HRESULT CEventCollector::SendEvents(_IWmiCoreServices* pCore)
  5057. {
  5058. HRESULT hresGlobal = WBEM_S_NO_ERROR;
  5059. for (int i = 0; i != m_apEvents.GetSize(); i++)
  5060. {
  5061. CRepEvent *pEvent = m_apEvents[i];
  5062. _IWmiObject* apObjs[2];
  5063. apObjs[0] = pEvent->m_pObj1;
  5064. apObjs[1] = pEvent->m_pObj2;
  5065. HRESULT hres = pCore->DeliverIntrinsicEvent(
  5066. pEvent->m_wszNamespace, pEvent->m_dwType, NULL,
  5067. pEvent->m_wszArg1, NULL, (pEvent->m_pObj2?2:1), apObjs);
  5068. if(FAILED(hres))
  5069. hresGlobal = hres;
  5070. }
  5071. return hresGlobal;
  5072. }
  5073. bool CEventCollector::AddEvent(CRepEvent* pEvent)
  5074. {
  5075. EnterCriticalSection(&m_csLock);
  5076. if(m_bNamespaceOnly)
  5077. {
  5078. if(pEvent->m_dwType != WBEM_EVENTTYPE_NamespaceCreation &&
  5079. pEvent->m_dwType != WBEM_EVENTTYPE_NamespaceDeletion &&
  5080. pEvent->m_dwType != WBEM_EVENTTYPE_NamespaceModification)
  5081. {
  5082. delete pEvent;
  5083. LeaveCriticalSection(&m_csLock);
  5084. return true;
  5085. }
  5086. }
  5087. bool bRet = (m_apEvents.Add(pEvent) >= 0);
  5088. LeaveCriticalSection(&m_csLock);
  5089. return bRet;
  5090. }
  5091. void CEventCollector::DeleteAllEvents()
  5092. {
  5093. EnterCriticalSection(&m_csLock);
  5094. m_bNamespaceOnly = false;
  5095. m_apEvents.RemoveAll();
  5096. LeaveCriticalSection(&m_csLock);
  5097. }
  5098. void CEventCollector::TransferEvents(CEventCollector &aEventsToTransfer)
  5099. {
  5100. m_bNamespaceOnly = aEventsToTransfer.m_bNamespaceOnly;
  5101. while(aEventsToTransfer.m_apEvents.GetSize())
  5102. {
  5103. CRepEvent *pEvent = 0;
  5104. aEventsToTransfer.m_apEvents.RemoveAt(0, &pEvent);
  5105. m_apEvents.Add(pEvent);
  5106. }
  5107. }