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.

163 lines
4.1 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation
  3. Module Name:
  4. pcmWriterStream.cpp
  5. Abstract:
  6. implementation of a filestream for Precompiled manifest writer
  7. Author:
  8. Xiaoyu Wu (xiaoyuw) June 2000
  9. Revision History:
  10. --*/
  11. #include "stdinc.h"
  12. #include "pcm.h"
  13. #include "pcmwriterstream.h"
  14. // NTRAID#NTBUG9 - 587991 - 2002/03/26 - xiaoyuw:
  15. // (1) currently not in the build
  16. // (2) no fusion tracing macros used
  17. //
  18. HRESULT CPrecompiledManifestWriterStream::WriteWithDelay(void const *pv, ULONG cb, ULONG *pcbWritten)
  19. {
  20. HRESULT hr=NOERROR;
  21. if (pcbWritten)
  22. *pcbWritten = 0 ;
  23. if (m_fBuffer)
  24. {
  25. if (FAILED(hr = m_buffer.Append(reinterpret_cast<const BYTE*>(pv), cb)))
  26. goto Exit;
  27. if ( pcbWritten)
  28. *pcbWritten = cb;
  29. }
  30. else
  31. {
  32. DWORD dwBytesWritten = 0;
  33. ASSERT(m_hFile != INVALID_HANDLE_VALUE);
  34. BOOL fSuccess = (cb == 0) || WriteFile(m_hFile, pv, cb, &dwBytesWritten, NULL);
  35. if (!fSuccess)
  36. // NTRAID#NTBUG9 - 587991 - 2002/03/26 - xiaoyuw:
  37. // in the case of (cb ==0), no LastError is set, and hr would be S_OK;
  38. hr = ::FusionpHresultFromLastError();
  39. else if (dwBytesWritten != cb)
  40. hr = E_FAIL;
  41. if ( pcbWritten)
  42. *pcbWritten = dwBytesWritten;
  43. }
  44. // NTRAID#NTBUG9 - 587991 - 2002/03/26 - xiaoyuw:
  45. // NOERROR would overwrite all hr error code ever set.
  46. hr = NOERROR;
  47. Exit:
  48. return hr;
  49. }
  50. BOOL
  51. CPrecompiledManifestWriterStream::SetSink(
  52. const CBaseStringBuffer &rbuff,
  53. DWORD openOrCreate
  54. )
  55. {
  56. BOOL fSuccess = FALSE;
  57. DWORD dwBytesWritten = 0;
  58. DWORD dwBufferSize = 0;
  59. //
  60. // NTRAID#NTBUG9-164736-2000/8/17-a-JayK,JayKrell share should be 0
  61. //
  62. if (!Base::OpenForWrite(rbuff, FILE_SHARE_WRITE, openOrCreate))
  63. goto Exit;
  64. dwBufferSize = static_cast<DWORD>(m_buffer.GetCurrentCb());
  65. fSuccess = TRUE;
  66. if (dwBufferSize > 0){
  67. // NTRAID#NTBUG9 - 587991 - 2002/03/26 - xiaoyuw:
  68. // change assert to INTERNAL_ERROR_CHECK
  69. ASSERT ( m_hFile != INVALID_HANDLE_VALUE );
  70. fSuccess = WriteFile(m_hFile, m_buffer, dwBufferSize, &dwBytesWritten, NULL/*overlapped*/);
  71. if (fSuccess && dwBytesWritten != dwBufferSize){
  72. ::FusionpSetLastWin32Error(ERROR_WRITE_FAULT);
  73. fSuccess = FALSE;
  74. }
  75. }
  76. m_fBuffer = FALSE;
  77. Exit:
  78. if (!fSuccess){
  79. // NTRAID#NTBUG9 - 587991 - 2002/03/26 - xiaoyuw:
  80. // CSxsPreserveLastError should be used here to make the code cleaner.
  81. //
  82. DWORD dwLastError = ::FusionpGetLastWin32Error();
  83. m_buffer.Clear(true);
  84. ::FusionpSetLastWin32Error(dwLastError);
  85. }
  86. else
  87. m_buffer.Clear(true);
  88. return fSuccess;
  89. }
  90. //besides close, rewrite MaxNodeCount, RecordCount into the header of the file
  91. HRESULT CPrecompiledManifestWriterStream::Close(ULONG ulRecordCount, DWORD dwMaxNodeCount)
  92. {
  93. HRESULT hr = NOERROR;
  94. LARGE_INTEGER liMove ;
  95. ASSERT(m_hFile != INVALID_HANDLE_VALUE);
  96. // write RecordCount;
  97. liMove.LowPart = offsetof(PCMHeader, ulRecordCount);
  98. liMove.HighPart = 0 ;
  99. hr = Base::Seek(liMove, FILE_BEGIN, NULL);
  100. if ( FAILED(hr))
  101. goto Exit;
  102. hr = WriteWithDelay((PVOID)&ulRecordCount, sizeof(ULONG), NULL);
  103. if ( FAILED(hr))
  104. goto Exit;
  105. // write MaxNodeCount;
  106. liMove.LowPart = offsetof(PCMHeader, usMaxNodeCount);
  107. liMove.HighPart = 0 ;
  108. hr = Base::Seek(liMove, FILE_BEGIN, NULL);
  109. if ( FAILED(hr))
  110. goto Exit;
  111. hr = WriteWithDelay((PVOID)&dwMaxNodeCount, sizeof(ULONG), NULL);
  112. if ( FAILED(hr))
  113. goto Exit;
  114. if ( ! Base::Close()) {
  115. hr = HRESULT_FROM_WIN32(::FusionpGetLastWin32Error());
  116. goto Exit;
  117. }
  118. hr = NOERROR;
  119. Exit:
  120. return hr;
  121. }
  122. BOOL CPrecompiledManifestWriterStream::IsSinkedStream()
  123. {
  124. if ((m_fBuffer == FALSE) && (m_hFile != INVALID_HANDLE_VALUE))
  125. return TRUE;
  126. else
  127. return FALSE;
  128. }