fixed another sparse files related bug in storage

This commit is contained in:
Arvid Norberg 2007-05-04 03:28:42 +00:00
parent f942308ccb
commit c39462c125
1 changed files with 78 additions and 73 deletions

View File

@ -628,6 +628,9 @@ namespace libtorrent
++file_iter; ++file_iter;
} }
int buf_pos = 0;
try
{
boost::shared_ptr<file> in(m_files.open_file( boost::shared_ptr<file> in(m_files.open_file(
this, m_save_path / file_iter->path, file::in)); this, m_save_path / file_iter->path, file::in));
@ -642,10 +645,10 @@ namespace libtorrent
throw file_error("slot has no storage"); throw file_error("slot has no storage");
} }
#ifndef NDEBUG #ifndef NDEBUG
size_type in_tell = in->tell(); size_type in_tell = in->tell();
assert(in_tell == file_offset); assert(in_tell == file_offset);
#endif #endif
int left_to_read = size; int left_to_read = size;
int slot_size = static_cast<int>(m_info.piece_size(slot)); int slot_size = static_cast<int>(m_info.piece_size(slot));
@ -656,11 +659,10 @@ namespace libtorrent
assert(left_to_read >= 0); assert(left_to_read >= 0);
size_type result = left_to_read; size_type result = left_to_read;
int buf_pos = 0;
#ifndef NDEBUG #ifndef NDEBUG
int counter = 0; int counter = 0;
#endif #endif
while (left_to_read > 0) while (left_to_read > 0)
{ {
@ -670,28 +672,22 @@ namespace libtorrent
if (read_bytes > 0) if (read_bytes > 0)
{ {
#ifndef NDEBUG #ifndef NDEBUG
assert(int(slices.size()) > counter); assert(int(slices.size()) > counter);
size_type slice_size = slices[counter].size; size_type slice_size = slices[counter].size;
assert(slice_size == read_bytes); assert(slice_size == read_bytes);
assert(m_info.file_at(slices[counter].file_index).path assert(m_info.file_at(slices[counter].file_index).path
== file_iter->path); == file_iter->path);
#endif #endif
size_type actual_read = in->read(buf + buf_pos, read_bytes); size_type actual_read = in->read(buf + buf_pos, read_bytes);
if (read_bytes != actual_read) if (read_bytes != actual_read)
{ {
if (fill_zero && actual_read < read_bytes) if (actual_read > 0) buf_pos += actual_read;
{
std::memset(buf + buf_pos + actual_read, 0, read_bytes - actual_read);
}
else
{
// the file was not big enough // the file was not big enough
throw file_error("slot has no storage"); throw file_error("slot has no storage");
} }
}
left_to_read -= read_bytes; left_to_read -= read_bytes;
buf_pos += read_bytes; buf_pos += read_bytes;
@ -702,11 +698,11 @@ namespace libtorrent
if (left_to_read > 0) if (left_to_read > 0)
{ {
++file_iter; ++file_iter;
#ifndef NDEBUG #ifndef NDEBUG
// empty files are not returned by map_block, so if // empty files are not returned by map_block, so if
// this file was empty, don't increment the slice counter // this file was empty, don't increment the slice counter
if (read_bytes > 0) ++counter; if (read_bytes > 0) ++counter;
#endif #endif
path path = m_save_path / file_iter->path; path path = m_save_path / file_iter->path;
file_offset = 0; file_offset = 0;
@ -715,9 +711,18 @@ namespace libtorrent
in->seek(0); in->seek(0);
} }
} }
return result; return result;
} }
catch (std::exception&)
{
if (fill_zero)
{
std::memset(buf + buf_pos, 0, size - buf_pos);
return size;
}
throw;
}
}
// throws file_error if it fails to write // throws file_error if it fails to write
void storage::write( void storage::write(