Changing size of memory-mapped file on Windows

My email address is barrywhart@yahoo.com.
There is a bug in
mapped_file_source::open_impl(mapped_file_params p)
This function does not correctly check for errors from
SetFilePointer, as documented in the MSDN page below.
(See "Case Two: calling the function with
lpDistanceToMoveHigh != NULL")
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/setfilepointer.asp
Current code:
::SetFilePointer(pimpl_->handle_, sizelow,
&sizehigh, FILE_BEGIN);
if (::GetLastError() != NO_ERROR ||
!::SetEndOfFile(pimpl_->handle_))
detail::cleanup_and_throw(*pimpl_, "failed
setting file size");
Should be:
DWORD dwResult =
::SetFilePointer(pimpl_->handle_, sizelow, &sizehigh,
FILE_BEGIN);
if (dwResult == INVALID_SET_FILE_POINTER
::GetLastError() != NO_ERROR ||
!::SetEndOfFile(pimpl_->handle_))
detail::cleanup_and_throw(*pimpl_, "failed
setting file size");
--
Even after this fix, the ability to resize an existing
file is not really useful because the file is
apparently recreated rather than resized. I am working
around this by resizing the file using system calls,
but I wonder if this is how the function is supposed to
work?!

Regarding the ability to resize an existing file, this is anot a supported feature. The new_file_size property of mapped_file_param is just a way to create a new file of a specified size.

I realize that this is an old ticket, so you may not be interested in the issue anymore; however, if you would like to submit a feature request, I will consider it for 1.36. My initial feeling is that it should be a general file-resizing function, not limited to memory-mapped files; in that case, it would probably be best o put it in Boost.Filesystem.

Add Comment

This ticket has been modified since you started editing. You should review the
other modifications which have been appended above,
and any conflicts shown in the preview below.
You can nevertheless proceed and submit your changes if you wish so.