Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

652 lines
13 KiB

  1. /*++
  2. Module Name:
  3. fsaunmdb.cpp
  4. Abstract:
  5. Defines the functions for the Unmanage Db & record classes.
  6. Author:
  7. Ran Kalach [rankala] 05-Dec-2000
  8. Revision History:
  9. --*/
  10. #include "stdafx.h"
  11. #include "wsb.h"
  12. #include "fsaunmdb.h"
  13. #define WSB_TRACE_IS WSB_TRACE_BIT_FSA
  14. static USHORT iCountUnmRec = 0; // Count of existing objects
  15. HRESULT
  16. CFsaUnmanageDb::FinalConstruct(
  17. void
  18. )
  19. /*++
  20. Implements:
  21. CComObjectRoot::FinalConstruct().
  22. --*/
  23. {
  24. HRESULT hr = S_OK;
  25. try {
  26. WsbAssertHr(CWsbDb::FinalConstruct());
  27. m_version = 1;
  28. } WsbCatch(hr);
  29. return(hr);
  30. }
  31. HRESULT
  32. CFsaUnmanageDb::FinalRelease(
  33. void
  34. )
  35. /*++
  36. Implements:
  37. CComObjectRoot::FinalRelease
  38. --*/
  39. {
  40. HRESULT hr = S_OK;
  41. CWsbDb::FinalRelease();
  42. return(hr);
  43. }
  44. HRESULT
  45. CFsaUnmanageDb::GetClassID(
  46. OUT CLSID* pClsid
  47. )
  48. /*++
  49. Implements:
  50. IPersist::GetClassID().
  51. --*/
  52. {
  53. HRESULT hr = S_OK;
  54. try {
  55. WsbAssert(0 != pClsid, E_POINTER);
  56. *pClsid = CLSID_CFsaUnmanageDb;
  57. } WsbCatch(hr);
  58. return(hr);
  59. }
  60. HRESULT
  61. CFsaUnmanageDb::Init(
  62. IN OLECHAR* path,
  63. IN IWsbDbSys* pDbSys,
  64. OUT BOOL* pCreated
  65. )
  66. /*++
  67. Implements:
  68. IFsaUnmanageDb::Init
  69. --*/
  70. {
  71. BOOL created = FALSE;
  72. HRESULT hr = S_OK;
  73. WsbTraceIn(OLESTR("CFsaUnmanageDb::Init"),OLESTR(""));
  74. try {
  75. m_pWsbDbSys = pDbSys;
  76. WsbAffirmPointer(m_pWsbDbSys);
  77. // Attempt to find the DB
  78. // If we find it - delete it, since we always want to start with a new db !!
  79. hr = Locate(path);
  80. if (STG_E_FILENOTFOUND == hr) {
  81. // Expected...
  82. WsbTrace(OLESTR("CFsaUnmanageDb::Init: db Locate failed with not-found, will create a new one...\n"));
  83. hr = S_OK;
  84. } else if (S_OK == hr) {
  85. // Cleanup wasn't done in previous run
  86. WsbTrace(OLESTR("CFsaUnmanageDb::Init: db Locate succeeded - will delete so a new one can be created\n"));
  87. WsbAffirmHr(Delete(path, IDB_DELETE_FLAG_NO_ERROR));
  88. } else {
  89. // Still try to delete and continue...
  90. // (Db could be corrupted for example due to abnormal termination of previous run -
  91. // we don't care since all we want is to always try creating a new one).
  92. WsbTrace(OLESTR("CFsaUnmanageDb::Init: db Locate failed with <%ls> - will try to delete and continue\n"),
  93. WsbHrAsString(hr));
  94. // Ignore Delete errors...
  95. hr = Delete(path, IDB_DELETE_FLAG_NO_ERROR);
  96. WsbTrace(OLESTR("CFsaUnmanageDb::Init: db Delete finished with <%ls> - will try to create a new db\n"),
  97. WsbHrAsString(hr));
  98. hr = S_OK;
  99. }
  100. // If we got that far, it means that the Unmanage Db doesn't exist and we can re-create
  101. ULONG memSize;
  102. m_nRecTypes = 1;
  103. memSize = m_nRecTypes * sizeof(IDB_REC_INFO);
  104. m_RecInfo = (IDB_REC_INFO*)WsbAlloc(memSize);
  105. WsbAffirm(0 != m_RecInfo, E_OUTOFMEMORY);
  106. ZeroMemory(m_RecInfo, memSize);
  107. // Unmanage record type
  108. m_RecInfo[0].Type = UNMANAGE_REC_TYPE;
  109. m_RecInfo[0].EntityClassId = CLSID_CFsaUnmanageRec;
  110. m_RecInfo[0].Flags = 0;
  111. m_RecInfo[0].MinSize = (WSB_BYTE_SIZE_GUID + // media id
  112. WSB_BYTE_SIZE_LONGLONG + // file offset on media
  113. WSB_BYTE_SIZE_LONGLONG); // file id
  114. m_RecInfo[0].MaxSize = m_RecInfo[0].MinSize;
  115. m_RecInfo[0].nKeys = 1;
  116. memSize = m_RecInfo[0].nKeys * sizeof(IDB_KEY_INFO);
  117. m_RecInfo[0].Key = (IDB_KEY_INFO*)WsbAlloc(memSize);
  118. WsbAffirm(0 != m_RecInfo[0].Key, E_OUTOFMEMORY);
  119. ZeroMemory(m_RecInfo[0].Key, memSize);
  120. m_RecInfo[0].Key[0].Type = UNMANAGE_KEY_TYPE;
  121. m_RecInfo[0].Key[0].Size = WSB_BYTE_SIZE_GUID + WSB_BYTE_SIZE_LONGLONG;;
  122. m_RecInfo[0].Key[0].Flags = IDB_KEY_FLAG_DUP_ALLOWED;
  123. // same key possible on tape if a placeholder is restored to a new location on the same volume.
  124. // same key is common in optical
  125. // TEMPORARY - check if we shouldn't use IDB_KEY_FLAG_PRIMARY for performance (even if it means not allow dup !!
  126. // (==> add one more part to the index, maybe auto-increment-coloumn, so it is always uniqe)
  127. // Attempt to create the DB
  128. WsbAssertHr(Create(path, (IDB_CREATE_FLAG_NO_TRANSACTION | IDB_CREATE_FLAG_FIXED_SCHEMA)));
  129. created = TRUE;
  130. } WsbCatch(hr);
  131. if (pCreated) {
  132. *pCreated = created;
  133. }
  134. WsbTraceOut(OLESTR("CFsaUnmanageDb::Init"), OLESTR("hr = <%ls>, Created = %ls"),
  135. WsbHrAsString(hr), WsbBoolAsString(created));
  136. return(hr);
  137. }
  138. HRESULT
  139. CFsaUnmanageDb::Load(
  140. IN IStream* pStream
  141. )
  142. /*++
  143. Implements:
  144. IPersistStream::Load().
  145. Note:
  146. This database is not expected to be persistent by the using class.
  147. However, the base class CWsbDb is persistable so we need to implement this
  148. --*/
  149. {
  150. HRESULT hr = S_OK;
  151. WsbTraceIn(OLESTR("CFsaUnmanageDb::Load"),OLESTR(""));
  152. hr = CWsbDb::Load(pStream);
  153. if (S_OK != hr && STG_E_FILENOTFOUND != hr) {
  154. // Got some error; delete the DB (we'll recreate it later if
  155. // we need it
  156. WsbTrace(OLESTR("CFsaUnmanageDb::Load: deleting DB\n"));
  157. if (S_OK == Delete(NULL, IDB_DELETE_FLAG_NO_ERROR)) {
  158. hr = STG_E_FILENOTFOUND;
  159. }
  160. }
  161. WsbTraceOut(OLESTR("CFsaUnmanageDb::Load"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  162. return(hr);
  163. }
  164. HRESULT
  165. CFsaUnmanageDb::Save(
  166. IN IStream* pStream,
  167. IN BOOL clearDirty
  168. )
  169. /*++
  170. Implements:
  171. IPersistStream::Save().
  172. Note:
  173. This database is not expected to be persistent by the using class.
  174. However, the base class CWsbDb is persistable so we need to implement this
  175. --*/
  176. {
  177. HRESULT hr = S_OK;
  178. WsbTraceIn(OLESTR("CFsaUnmanageDb::Save"),OLESTR(""));
  179. try {
  180. WsbAffirmHr(CWsbDb::Save(pStream, clearDirty));
  181. } WsbCatch(hr);
  182. WsbTraceOut(OLESTR("CFsaUnmanageDb::Save"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  183. return(hr);
  184. }
  185. HRESULT
  186. CFsaUnmanageRec::GetMediaId(
  187. OUT GUID* pId
  188. )
  189. /*++
  190. Implements:
  191. IFsaUnmanageRec::GetMediaId
  192. --*/
  193. {
  194. HRESULT hr = S_OK;
  195. WsbTraceIn(OLESTR("CFsaUnmanageRec::GetMediaId"),OLESTR(""));
  196. try {
  197. WsbAssert(0 != pId, E_POINTER);
  198. *pId = m_MediaId;
  199. } WsbCatch(hr);
  200. WsbTraceOut(OLESTR("CFsaUnmanageRec::GetMediaId"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  201. return(hr);
  202. }
  203. HRESULT
  204. CFsaUnmanageRec::GetFileOffset(
  205. OUT LONGLONG* pOffset
  206. )
  207. /*++
  208. Implements:
  209. IFsaUnmanageRec::GetFileOffset
  210. --*/
  211. {
  212. HRESULT hr = S_OK;
  213. WsbTraceIn(OLESTR("CFsaUnmanageRec::GetFileOffset"),OLESTR(""));
  214. try {
  215. WsbAssert(0 != pOffset, E_POINTER);
  216. *pOffset = m_FileOffset;
  217. } WsbCatch(hr);
  218. WsbTraceOut(OLESTR("CFsaUnmanageRec::GetFileOffset"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  219. return(hr);
  220. }
  221. HRESULT
  222. CFsaUnmanageRec::GetFileId(
  223. OUT LONGLONG* pFileId
  224. )
  225. /*++
  226. Implements:
  227. IFsaUnmanageRec::GetFileId
  228. --*/
  229. {
  230. HRESULT hr = S_OK;
  231. WsbTraceIn(OLESTR("CFsaUnmanageRec::GetFileId"),OLESTR(""));
  232. try {
  233. WsbAssert(0 != pFileId, E_POINTER);
  234. *pFileId = m_FileId;
  235. } WsbCatch(hr);
  236. WsbTraceOut(OLESTR("CFsaUnmanageRec::GetFileId"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  237. return(hr);
  238. }
  239. HRESULT
  240. CFsaUnmanageRec::SetMediaId(
  241. IN GUID id
  242. )
  243. /*++
  244. Implements:
  245. IFsaUnmanageRec::SetMediaId
  246. --*/
  247. {
  248. HRESULT hr = S_OK;
  249. WsbTraceIn(OLESTR("CFsaUnmanageRec::SetMediaId"),OLESTR(""));
  250. m_MediaId = id;
  251. WsbTraceOut(OLESTR("CFsaUnmanageRec::SetMediaId"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  252. return(hr);
  253. }
  254. HRESULT
  255. CFsaUnmanageRec::SetFileOffset(
  256. IN LONGLONG offset
  257. )
  258. /*++
  259. Implements:
  260. IFsaUnmanageRec::SetFileOffset
  261. --*/
  262. {
  263. HRESULT hr = S_OK;
  264. WsbTraceIn(OLESTR("CFsaUnmanageRec::SetFileOffset"),OLESTR(""));
  265. m_FileOffset = offset;
  266. WsbTraceOut(OLESTR("CFsaUnmanageRec::SetFileOffset"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  267. return(hr);
  268. }
  269. HRESULT
  270. CFsaUnmanageRec::SetFileId(
  271. IN LONGLONG FileId
  272. )
  273. /*++
  274. Implements:
  275. IFsaUnmanageRec::SetFileId
  276. --*/
  277. {
  278. HRESULT hr = S_OK;
  279. WsbTraceIn(OLESTR("CFsaUnmanageRec::SetFileId"),OLESTR(""));
  280. m_FileId = FileId;
  281. WsbTraceOut(OLESTR("CFsaUnmanageRec::SetFileId"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  282. return(hr);
  283. }
  284. HRESULT
  285. CFsaUnmanageRec::FinalConstruct(
  286. void
  287. )
  288. /*++
  289. Implements:
  290. CComObjectRoot::FinalConstruct().
  291. --*/
  292. {
  293. HRESULT hr = S_OK;
  294. try {
  295. WsbAssertHr(CWsbDbEntity::FinalConstruct());
  296. m_MediaId = GUID_NULL;
  297. m_FileOffset = 0;
  298. m_FileId = 0;
  299. } WsbCatch(hr);
  300. iCountUnmRec++;
  301. return(hr);
  302. }
  303. HRESULT
  304. CFsaUnmanageRec::FinalRelease(
  305. void
  306. )
  307. /*++
  308. Implements:
  309. CComObjectRoot::FinalRelease
  310. --*/
  311. {
  312. HRESULT hr = S_OK;
  313. CWsbDbEntity::FinalRelease();
  314. iCountUnmRec--;
  315. return(hr);
  316. }
  317. HRESULT CFsaUnmanageRec::GetClassID
  318. (
  319. OUT LPCLSID pclsid
  320. )
  321. /*++
  322. Implements:
  323. IPerist::GetClassID
  324. --*/
  325. {
  326. HRESULT hr = S_OK;
  327. WsbTraceIn(OLESTR("CFsaUnmanageRec::GetClassID"), OLESTR(""));
  328. try {
  329. WsbAssert(0 != pclsid, E_POINTER);
  330. *pclsid = CLSID_CFsaUnmanageRec;
  331. } WsbCatch(hr);
  332. WsbTraceOut(OLESTR("CFsaUnmanageRec::GetClassID"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  333. return(hr);
  334. }
  335. HRESULT CFsaUnmanageRec::GetSizeMax
  336. (
  337. OUT ULARGE_INTEGER* pcbSize
  338. )
  339. /*++
  340. Implements:
  341. IPersistStream::GetSizeMax().
  342. --*/
  343. {
  344. HRESULT hr = S_OK;
  345. WsbTraceIn(OLESTR("CFsaUnmanageRec::GetSizeMax"), OLESTR(""));
  346. try {
  347. WsbAssert(0 != pcbSize, E_POINTER);
  348. pcbSize->QuadPart = WsbPersistSizeOf(GUID) +
  349. WsbPersistSizeOf(LONGLONG) +
  350. WsbPersistSizeOf(LONGLONG);
  351. } WsbCatch(hr);
  352. WsbTraceOut(OLESTR("CFsaUnmanageRec::GetSizeMax"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  353. return(hr);
  354. }
  355. HRESULT CFsaUnmanageRec::Load
  356. (
  357. IN IStream* pStream
  358. )
  359. /*++
  360. Implements:
  361. IPersistStream::Load
  362. --*/
  363. {
  364. HRESULT hr = S_OK;
  365. WsbTraceIn(OLESTR("CFsaUnmanageRec::Load"), OLESTR(""));
  366. try {
  367. WsbAssert(0 != pStream, E_POINTER);
  368. WsbAssertHr(WsbLoadFromStream(pStream, &m_MediaId));
  369. WsbAssertHr(WsbLoadFromStream(pStream, &m_FileOffset));
  370. WsbAssertHr(WsbLoadFromStream(pStream, &m_FileId));
  371. } WsbCatch(hr);
  372. WsbTraceOut(OLESTR("CFsaUnmanageRec::Load"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  373. return(hr);
  374. }
  375. HRESULT CFsaUnmanageRec::Print
  376. (
  377. IN IStream* pStream
  378. )
  379. /*++
  380. Implements:
  381. IWsbDbEntity::Print
  382. --*/
  383. {
  384. HRESULT hr = S_OK;
  385. WsbTraceIn(OLESTR("CFsaUnmanageRec::Print"), OLESTR(""));
  386. try {
  387. WsbAssert(0 != pStream, E_POINTER);
  388. CWsbStringPtr strGuid;
  389. WsbAffirmHr(WsbSafeGuidAsString(m_MediaId, strGuid));
  390. WsbAffirmHr(WsbPrintfToStream(pStream, OLESTR(" MediaId = %ls"),
  391. (WCHAR *)strGuid));
  392. WsbAffirmHr(WsbPrintfToStream(pStream, OLESTR(", FileOffset = %ls"),
  393. WsbLonglongAsString(m_FileOffset)));
  394. WsbAffirmHr(WsbPrintfToStream(pStream, OLESTR(", FileId = %ls"),
  395. WsbLonglongAsString(m_FileId)));
  396. WsbAffirmHr(CWsbDbEntity::Print(pStream));
  397. } WsbCatch(hr);
  398. WsbTraceOut(OLESTR("CFsaUnmanageRec::Print"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  399. return(hr);
  400. }
  401. HRESULT CFsaUnmanageRec::Save
  402. (
  403. IN IStream* pStream,
  404. IN BOOL clearDirty
  405. )
  406. /*++
  407. Implements:
  408. IPersistStream::Save
  409. --*/
  410. {
  411. HRESULT hr = S_OK;
  412. WsbTraceIn(OLESTR("CFsaUnmanageRec::Save"), OLESTR("clearDirty = <%ls>"), WsbBoolAsString(clearDirty));
  413. try {
  414. WsbAssert(0 != pStream, E_POINTER);
  415. WsbAssertHr(WsbSaveToStream(pStream, m_MediaId));
  416. WsbAssertHr(WsbSaveToStream(pStream, m_FileOffset));
  417. WsbAssertHr(WsbSaveToStream(pStream, m_FileId));
  418. if (clearDirty) {
  419. m_isDirty = FALSE;
  420. }
  421. } WsbCatch(hr);
  422. WsbTraceOut(OLESTR("CFsaUnmanageRec::Save"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  423. return(hr);
  424. }
  425. HRESULT
  426. CFsaUnmanageRec::UpdateKey(
  427. IWsbDbKey *pKey
  428. )
  429. /*++
  430. Implements:
  431. IWsbDbEntity::UpdateKey
  432. --*/
  433. {
  434. HRESULT hr = S_OK;
  435. try {
  436. WsbAffirmHr(pKey->SetToGuid(m_MediaId));
  437. WsbAffirmHr(pKey->AppendLonglong(m_FileOffset));
  438. } WsbCatch(hr);
  439. return(hr);
  440. }