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.

149 lines
3.3 KiB

  1. /*++
  2. Copyright (c) 2000 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. HRESULT CPrecompiledManifestWriterStream::WriteWithDelay(void const *pv, ULONG cb, ULONG *pcbWritten)
  15. {
  16. HRESULT hr=NOERROR;
  17. if (pcbWritten)
  18. *pcbWritten = 0 ;
  19. if (m_fBuffer)
  20. {
  21. if (FAILED(hr = m_buffer.Append(reinterpret_cast<const BYTE*>(pv), cb)))
  22. goto Exit;
  23. if ( pcbWritten)
  24. *pcbWritten = cb;
  25. }
  26. else
  27. {
  28. DWORD dwBytesWritten = 0;
  29. ASSERT(m_hFile != INVALID_HANDLE_VALUE);
  30. BOOL fSuccess = (cb == 0) || WriteFile(m_hFile, pv, cb, &dwBytesWritten, NULL);
  31. if (!fSuccess)
  32. hr = FusionpHresultFromLastError();
  33. else if (dwBytesWritten != cb)
  34. hr = E_FAIL;
  35. if ( pcbWritten)
  36. *pcbWritten = dwBytesWritten;
  37. }
  38. hr = NOERROR;
  39. Exit:
  40. return hr;
  41. }
  42. BOOL
  43. CPrecompiledManifestWriterStream::SetSink(
  44. const CBaseStringBuffer &rbuff,
  45. DWORD openOrCreate
  46. )
  47. {
  48. BOOL fSuccess = FALSE;
  49. DWORD dwBytesWritten = 0;
  50. DWORD dwBufferSize = 0;
  51. //
  52. // NTRAID#NTBUG9-164736-2000/8/17-a-JayK share should be 0
  53. //
  54. if (!Base::OpenForWrite(rbuff, FILE_SHARE_WRITE, openOrCreate))
  55. goto Exit;
  56. dwBufferSize = static_cast<DWORD>(m_buffer.GetCurrentCb());
  57. fSuccess = TRUE;
  58. if (dwBufferSize > 0){
  59. ASSERT ( m_hFile != INVALID_HANDLE_VALUE );
  60. fSuccess = WriteFile(m_hFile, m_buffer, dwBufferSize, &dwBytesWritten, NULL/*overlapped*/);
  61. if (fSuccess && dwBytesWritten != dwBufferSize){
  62. ::FusionpSetLastWin32Error(ERROR_WRITE_FAULT);
  63. fSuccess = FALSE;
  64. }
  65. }
  66. m_fBuffer = FALSE;
  67. Exit:
  68. if (!fSuccess){
  69. DWORD dwLastError = ::FusionpGetLastWin32Error();
  70. m_buffer.Clear(true);
  71. ::FusionpSetLastWin32Error(dwLastError);
  72. }
  73. else
  74. m_buffer.Clear(true);
  75. return fSuccess;
  76. }
  77. //besides close, rewrite MaxNodeCount, RecordCount into the header of the file
  78. HRESULT CPrecompiledManifestWriterStream::Close(ULONG ulRecordCount, DWORD dwMaxNodeCount)
  79. {
  80. HRESULT hr = NOERROR;
  81. LARGE_INTEGER liMove ;
  82. ASSERT(m_hFile != INVALID_HANDLE_VALUE);
  83. // write RecordCount;
  84. liMove.LowPart = offsetof(PCMHeader, ulRecordCount);
  85. liMove.HighPart = 0 ;
  86. hr = Base::Seek(liMove, FILE_BEGIN, NULL);
  87. if ( FAILED(hr))
  88. goto Exit;
  89. hr = WriteWithDelay((PVOID)&ulRecordCount, sizeof(ULONG), NULL);
  90. if ( FAILED(hr))
  91. goto Exit;
  92. // write MaxNodeCount;
  93. liMove.LowPart = offsetof(PCMHeader, usMaxNodeCount);
  94. liMove.HighPart = 0 ;
  95. hr = Base::Seek(liMove, FILE_BEGIN, NULL);
  96. if ( FAILED(hr))
  97. goto Exit;
  98. hr = WriteWithDelay((PVOID)&dwMaxNodeCount, sizeof(ULONG), NULL);
  99. if ( FAILED(hr))
  100. goto Exit;
  101. if ( ! Base::Close()) {
  102. hr = HRESULT_FROM_WIN32(::FusionpGetLastWin32Error());
  103. goto Exit;
  104. }
  105. hr = NOERROR;
  106. Exit:
  107. return hr;
  108. }
  109. BOOL CPrecompiledManifestWriterStream::IsSinkedStream()
  110. {
  111. if ((m_fBuffer == FALSE) && (m_hFile != INVALID_HANDLE_VALUE))
  112. return TRUE;
  113. else
  114. return FALSE;
  115. }