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.

243 lines
6.2 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1994.
  5. //
  6. // File: mapfile.cxx
  7. //
  8. // Contents: Mapped file class implementation
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 12-Feb-96 PhilipLa Created
  15. //
  16. //----------------------------------------------------------------------------
  17. #include "layouthd.cxx"
  18. #pragma hdrstop
  19. //#define TEST_MAPPING_LOWMEM
  20. CMappedFile::~CMappedFile(void)
  21. {
  22. #ifdef SUPPORT_FILE_MAPPING
  23. if (_pbBase != NULL)
  24. {
  25. UnmapViewOfFile(_pbBase);
  26. }
  27. if (_hMapping != INVALID_HANDLE_VALUE)
  28. {
  29. CloseHandle(_hMapping);
  30. }
  31. #endif
  32. if (_hFile != INVALID_HANDLE_VALUE)
  33. {
  34. CloseHandle(_hFile);
  35. }
  36. }
  37. //+---------------------------------------------------------------------------
  38. //
  39. // Member: CMappedFile::InitFromHandle, public
  40. //
  41. // Synopsis: Initialize object based on file handle
  42. //
  43. // Arguments: [h] -- File handle
  44. // [fReadOnly] -- TRUE if mapping is to be read-only
  45. // [fDuplicate] -- TRUE if handle is to be duplicated
  46. // [pvDesiredBaseAddress] -- Desired address for mapping
  47. //
  48. // Returns: Appropriate status code
  49. //
  50. // History: 13-Feb-96 PhilipLa Created
  51. //
  52. //----------------------------------------------------------------------------
  53. SCODE CMappedFile::InitFromHandle(HANDLE h,
  54. BOOL fReadOnly,
  55. BOOL fDuplicate,
  56. void *pvDesiredBaseAddress)
  57. {
  58. SCODE sc = S_OK;
  59. if (fDuplicate)
  60. {
  61. if (!DuplicateHandle(GetCurrentProcess(),
  62. h,
  63. GetCurrentProcess(),
  64. &_hFile,
  65. 0,
  66. TRUE,
  67. DUPLICATE_SAME_ACCESS))
  68. {
  69. layErr(Err, STG_SCODE(GetLastError()));
  70. }
  71. }
  72. else
  73. {
  74. _hFile = h;
  75. }
  76. #ifdef SUPPORT_FILE_MAPPING
  77. #ifndef UNICODE
  78. _hMapping = CreateFileMappingA
  79. #else
  80. _hMapping = CreateFileMapping
  81. #endif
  82. (_hFile,
  83. NULL, // No security
  84. (fReadOnly) ? PAGE_READONLY : PAGE_READWRITE,
  85. 0,
  86. 0, //File size determines map size
  87. NULL); // Unnamed
  88. #ifdef TEST_MAPPING_LOWMEM
  89. CloseHandle(_hMapping);
  90. _hMapping = NULL;
  91. #endif
  92. if (_hMapping != NULL)
  93. {
  94. //Mapping created OK, now map view
  95. _pbBase = MapViewOfFileEx(_hMapping,
  96. (fReadOnly) ? FILE_MAP_READ : FILE_MAP_WRITE,
  97. 0,
  98. 0,
  99. 0,
  100. pvDesiredBaseAddress);
  101. if (_pbBase == NULL)
  102. {
  103. sc = STG_SCODE(GetLastError());
  104. CloseHandle(_hMapping);
  105. _hMapping = INVALID_HANDLE_VALUE;
  106. }
  107. }
  108. #endif //SUPPORT_FILE_MAPPING
  109. Err:
  110. return sc;
  111. }
  112. //+---------------------------------------------------------------------------
  113. //
  114. // Member: CMappedFile::Init, public
  115. //
  116. // Synopsis: Initialize based on a filename
  117. //
  118. // Arguments: [pwcsName] -- Filename for file
  119. // [dwSize] -- Desired size of file, 0 for no size change
  120. // [dwAccess] -- Access mode for file (see CreateFile)
  121. // [dwCreationDisposition] -- Creation for file (see CreateFile)
  122. // [pvDesiredBaseAddress] -- Desired base address for mapping
  123. //
  124. // Returns: Appropriate status code
  125. //
  126. // History: 13-Feb-96 PhilipLa Created
  127. //
  128. //----------------------------------------------------------------------------
  129. SCODE CMappedFile::Init(OLECHAR const *pwcsName,
  130. DWORD dwSize,
  131. DWORD dwAccess,
  132. DWORD dwCreationDisposition,
  133. void *pvDesiredBaseAddress)
  134. {
  135. SCODE sc;
  136. BOOL fReadOnly = ((dwAccess & GENERIC_WRITE) == 0);
  137. layAssert(!fReadOnly || (dwSize == 0));
  138. if (pwcsName == NULL)
  139. return STG_E_INVALIDNAME;
  140. #if (!defined(UNICODE) && !defined(_MAC))
  141. TCHAR atcPath[MAX_PATH + 1];
  142. UINT uCodePage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
  143. if (!WideCharToMultiByte(
  144. uCodePage,
  145. 0,
  146. pwcsName,
  147. -1,
  148. atcPath,
  149. MAX_PATH + 1,
  150. NULL,
  151. NULL))
  152. {
  153. return STG_E_INVALIDNAME;
  154. }
  155. _hFile = CreateFileA(atcPath,
  156. dwAccess,
  157. 0, //No sharing
  158. NULL, //No security
  159. dwCreationDisposition,
  160. FILE_ATTRIBUTE_NORMAL,
  161. NULL); // No template file
  162. #else
  163. _hFile = CreateFile(pwcsName,
  164. dwAccess,
  165. 0, //No sharing
  166. NULL, //No security
  167. dwCreationDisposition,
  168. FILE_ATTRIBUTE_NORMAL,
  169. NULL); // No template file
  170. #endif
  171. if (_hFile == INVALID_HANDLE_VALUE)
  172. {
  173. layErr(Err, STG_SCODE(GetLastError()));
  174. }
  175. if (dwSize != 0)
  176. {
  177. DWORD dw = SetFilePointer(_hFile,
  178. dwSize,
  179. NULL,
  180. FILE_BEGIN);
  181. if (dw == 0xFFFFFFFF)
  182. {
  183. layErr(Err, STG_SCODE(GetLastError()));
  184. }
  185. if (!SetEndOfFile(_hFile))
  186. {
  187. layErr(Err, STG_SCODE(GetLastError()));
  188. }
  189. }
  190. else
  191. {
  192. DWORD dwFileSize;
  193. dwFileSize = GetFileSize(_hFile, NULL);
  194. if (dwFileSize == 0xFFFFFFFF)
  195. {
  196. layErr(Err, STG_SCODE(GetLastError()));
  197. }
  198. if ( dwFileSize == 0 )
  199. {
  200. return STG_S_FILEEMPTY;
  201. }
  202. }
  203. layChk(InitFromHandle(_hFile, fReadOnly, FALSE, pvDesiredBaseAddress));
  204. Err:
  205. return sc;
  206. }