Boost C++ Libraries: Ticket #2996: [iostreams] Can't open mapped_file data file for read/write access on Windows https://svn.boost.org/trac10/ticket/2996 <p> I see access errors when trying to use mapped_file open a data file for reading and writing under Windows (built with VS2005). The same code works fine under Mac OS X, which is posix, of course. </p> <p> Note that I originally found this problem under boost 1.35, but it is still present in the just released 1.39 version. Unfortunately it looks like trac has not been updated, so I cannot set the "version" to 1.38, or the "milestone" to 1.40. </p> <p> Here's a code sample that works on Mac OS X and fails under Windows: </p> <p> --snip-- </p> <p> /<strong> Test boost iostream mapped_file. */ static int <a class="missing wiki">BoostMappedFileTest</a>(void) { </strong></p> <blockquote> <p> int result = 0; </p> </blockquote> <p> </p> <blockquote> <p> std::cout &lt;&lt; "\nRunning <a class="missing wiki">BoostMappedFileTest</a>..." &lt;&lt; std::endl; </p> </blockquote> <p> </p> <blockquote> <p> <em> Initialize our test directory fs::path testDirPath( "<a class="missing wiki">BoostMappedFileTest</a>" ); create_directory(testDirPath); </em></p> </blockquote> <p> </p> <blockquote> <p> <em> Initialize a test file buffer with some data const size_t kTestFileSize = 4096; boost::scoped_array&lt;char&gt; fileBuffer( new char[kTestFileSize] ); char* pCur = fileBuffer.get(); char curChar = 0; for( size_t i = 0; i &lt; kTestFileSize; ++i ) { </em></p> <blockquote> <p> *pCur++ = curChar++; </p> </blockquote> <p> } </p> </blockquote> <p> </p> <blockquote> <p> <em> Write the data to a test file. Be sure to explicitly close it. fs::path testFilePath( testDirPath / "test_mapped_file" ); boost::filesystem::ofstream testFile( testFilePath, ios::trunc | ios::binary ); testFile.write(fileBuffer.get(), kTestFileSize); testFile.close(); </em></p> </blockquote> <p> </p> <blockquote> <p> <em> Try and open the test file as a memory mapped file. string testFilePathStr = testFilePath.file_string(); mapped_file mappedFile(testFilePathStr); </em></p> </blockquote> <p> </p> <blockquote> <p> <em> We never get here under Windows. The file fails to </em> open with an access error. </p> </blockquote> <p> </p> <blockquote> <p> <em> Compare the mapped file data to our original buffer const char* mmConstData = mappedFile.const_data(); bool compareSucceeded = (0 == memcmp(fileBuffer.get(), mmConstData, kTestFileSize)); if (!compareSucceeded) { </em></p> <blockquote> <p> throw runtime_error("The boost memory mapped file data doesn't match our buffer!"); </p> </blockquote> <p> } </p> </blockquote> <p> </p> <blockquote> <p> return result; </p> </blockquote> <p> } </p> <p> --snip-- </p> <p> The problem seems to come from passing GENERIC_ALL as the "dwDesiredAccess" parameter to the CreateFileA call. If I change the implementation to pass GENERIC_READ | GENERIC_WRITE, then it works in my test scenario. Please note that I haven't tried pointing the current code at an actual executable to see if it works with GENERIC_ALL. </p> <p> If you assume a strict interpretation of the mapped_file API, the mode of BOOST_IOS::in would map to GENERIC_READ in the Windows world, and BOOST_IOS::out would map to GENERIC_WRITE. There is no corresponding "execute" flag that I can see in this API. </p> <p> I've attached a diff created from the boost 1.39 release that fixes the problem. </p> <p> My email address (in case my trac preferences doesn't work) is acronce@…. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2996 Trac 1.4.3 Allen Cronce <acronce@…> Tue, 05 May 2009 16:31:39 GMT attachment set https://svn.boost.org/trac10/ticket/2996 https://svn.boost.org/trac10/ticket/2996 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">mapped_file.patch.zip</span> </li> </ul> <p> Patch file for mapped_file.cpp. Apply to the 1.39 release version to receive the patch. </p> Ticket anonymous Thu, 19 Nov 2009 09:15:30 GMT <link>https://svn.boost.org/trac10/ticket/2996#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2996#comment:1</guid> <description> <p> GENERIC_ALL maps to Full Control in windows... Whereas GENERIC_READ and GENERIC_WRITE map to your standard Read and Write permissions. Full control adds extra permissions for execute, plus things like Take Ownership and Change Permissions. </p> <p> I cant imagine why you would need GENERIC_ALL in the case for opening the mapped file for writing. So I agree with the original poster that the reference to GENERIC_ALL should be GENERIC_READ | GENERIC_WRITE instead. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Steven Watanabe</dc:creator> <pubDate>Thu, 17 Jun 2010 03:19:17 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/2996#comment:2 https://svn.boost.org/trac10/ticket/2996#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/63035" title="Replace GENERIC_ALL with GENERIC_READ | GENERIC_WRITE. Fixes #2996.">[63035]</a>) Replace GENERIC_ALL with GENERIC_READ | GENERIC_WRITE. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/2996" title="#2996: Bugs: [iostreams] Can't open mapped_file data file for read/write access on ... (closed: fixed)">#2996</a>. </p> Ticket