--- detail/file_wrapper.hpp Thu Jan 13 01:26:04 2011 +++ detail/file_wrapper.hpp Thu Jan 13 01:26:04 2011 @@ -179,7 +179,7 @@ inline void file_wrapper::truncate(offset_t length) { - if(!truncate_file(m_handle, length)){ + if(!truncate_file(m_handle, length, true)){ error_info err(system_error_code()); throw interprocess_exception(err); } --- detail/os_file_functions.hpp Thu Jan 13 01:26:04 2011 +++ detail/os_file_functions.hpp Thu Jan 13 01:26:04 2011 @@ -121,19 +121,21 @@ inline bool delete_file(const char *name) { return winapi::unlink_file(name); } -inline bool truncate_file (file_handle_t hnd, std::size_t size) +inline bool truncate_file (file_handle_t hnd, offset_t size, bool fill_with_zeros ) { + if(fill_with_zeros){ offset_t filesize; if(!winapi::get_file_size(hnd, filesize)) return false; - if(size > (unsigned long long)filesize){ - if(!winapi::set_file_pointer_ex(hnd, filesize, 0, winapi::file_begin)){ + if(size > filesize){ + if(! (winapi::set_file_pointer_ex(hnd, filesize, 0, winapi::file_begin) && + winapi::set_end_of_file(hnd) ) ){ return false; } //We will write zeros in the end of the file //since set_end_of_file does not guarantee this - for(std::size_t remaining = size - filesize, write_size = 0 + for(offset_t remaining = size - filesize, write_size = 0 ;remaining > 0 ;remaining -= write_size){ const std::size_t DataSize = 512; @@ -145,16 +147,13 @@ return false; } } - } - else{ - if(!winapi::set_file_pointer_ex(hnd, size, 0, winapi::file_begin)){ - return false; - } - if(!winapi::set_end_of_file(hnd)){ - return false; - } - } return true; + } //if(static_cast(size) > filesize){ + } // if(fill_with_zeros){ + + return + winapi::set_file_pointer_ex(hnd, size, 0, winapi::file_begin) && + winapi::set_end_of_file(hnd); } inline bool get_file_size(file_handle_t hnd, offset_t &size) @@ -437,7 +436,7 @@ inline bool delete_file(const char *name) { return ::unlink(name) == 0; } -inline bool truncate_file (file_handle_t hnd, std::size_t size) +inline bool truncate_file (file_handle_t hnd, std::size_t size, bool /*fill_with_zeros*/) { return 0 == ::ftruncate(hnd, size); } inline bool get_file_size(file_handle_t hnd, offset_t &size) --- file_mapping.hpp Thu Oct 15 21:46:26 2009 +++ file_mapping.hpp Thu Oct 15 21:46:26 2009 @@ -90,6 +90,10 @@ //!being used other processes and no deletion permission was shared. static bool remove(const char *filename); + //!Resizes file to the specified size + //!The function might fail if file cannot be resized. + void resize( offset_t new_size, bool fill_tail_with_zeros ); + /// @cond private: //!Closes a previously opened file mapping. Never throws. @@ -159,6 +163,13 @@ } ///@endcond + +inline void file_mapping::resize( offset_t new_size, bool fill_tail_with_zeros ) { + if( !detail::truncate_file( m_handle, new_size, fill_tail_with_zeros ) ) { + error_info err = system_error_code(); + throw interprocess_exception(err); + } +} //!A class that stores the name of a file //!and tries to remove it in its destructor --- shared_memory_object.hpp Thu Jan 13 01:26:04 2011 +++ shared_memory_object.hpp Thu Jan 13 01:26:04 2011 @@ -229,7 +229,7 @@ inline void shared_memory_object::truncate(offset_t length) { - if(!detail::truncate_file(m_handle, length)){ + if(!detail::truncate_file(m_handle, length, true)){ error_info err = system_error_code(); throw interprocess_exception(err); }