Boost logo

Boost :

Subject: [boost] [iostreams] mapped_file_source fails for zero sized file
From: Michael Marcin (mike.marcin_at_[hidden])
Date: 2013-02-11 04:00:58


While testing out iostreams memory mapped file source I ran into an
issue where it throws when opening a zero sized file (testing on windows
7 vc11 x64).

for example something like:

namespace io = boost::iostreams;
namespace fs = boost::filesystem;
fs::path path("C:\\boost\\boost_1_53_0\\libs\\context\\src\\dummy.cpp");
io::stream<io::mapped_file_source> file( path );

Will throw:

failed create mapping: The volume for a file has been externally altered
so that the opened file is no longer valid.

From:

http://tinyurl.com/cx2b9ru

"When you do not want the size of the file to change (for example, when
mapping read-only files), call CreateFileMapping and specify zero for
both dwMaximumSizeHigh and dwMaximumSizeLow. Doing this creates a file
mapping object that is exactly the same size as the file. Otherwise, you
must calculate or estimate the size of the finished file because file
mapping objects are static in size; once created, their size cannot be
increased or decreased. An attempt to map a file with a length of zero
in this manner fails with an error code of ERROR_FILE_INVALID. Programs
should test for files with a length of zero and reject such files."

I don't see anything in the docs that would lead me to believe this
should throw. Rather I would expect:

std::istream_iterator<char>(file) == std::istream_iterator<char>()


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk