file allocation fix for enc branch

This commit is contained in:
Marcos Pinto 2007-05-31 00:10:43 +00:00
parent ff87f4bde5
commit 61761160ba
2 changed files with 59 additions and 20 deletions

View File

@ -248,15 +248,15 @@ namespace libtorrent
void set_size(size_type s)
{
size_type pos = tell();
seek(1, 0);
seek(s);
char dummy = 0;
read(&dummy, 1);
seek(1, 0);
seek(s);
write(&dummy, 1);
seek(pos, 1);
seek(pos);
}
size_type seek(size_type offset, int m)
size_type seek(size_type offset, int m = 1)
{
assert(m_open_mode);
assert(m_fd != -1);

View File

@ -309,7 +309,7 @@ namespace libtorrent
#endif
}
catch (std::exception&) {}
if (size != s->first
if ((compact_mode && size != s->first)
|| (!compact_mode && size < s->first))
{
if (error) *error = "filesize mismatch for file '"
@ -319,7 +319,7 @@ namespace libtorrent
+ " bytes";
return false;
}
if (time != s->second
if ((compact_mode && time != s->second)
|| (!compact_mode && time < s->second))
{
if (error) *error = "timestamp mismatch for file '"
@ -1613,22 +1613,42 @@ namespace libtorrent
m_unallocated_slots.push_back(i);
}
if (!m_compact_mode && !m_unallocated_slots.empty())
{
m_state = state_allocating;
return false;
}
else
if (m_compact_mode || m_unallocated_slots.empty())
{
m_state = state_finished;
return true;
}
}
m_state = state_create_files;
m_current_slot = 0;
m_state = state_full_check;
return false;
}
/*
state chart:
check_fastresume()
| |
| v
| +------------+
| | full_check |
| +------------+
| |
| v
| +------------+ +--------------+
| | allocating |-->| create_files |
| +------------+ +--------------+
| | |
| v |
| +----------+ |
+---->| finished |<--------+
+----------+
*/
// performs the full check and full allocation
// (if necessary). returns true if finished and
// false if it should be called again
@ -1654,6 +1674,15 @@ namespace libtorrent
return std::make_pair(true, 1.f);
}
if (int(m_unallocated_slots.size()) == m_info.num_pieces()
&& !m_fill_mode)
{
// if there is not a single file on disk, just
// create the files
m_state = state_create_files;
return std::make_pair(false, 1.f);
}
// if we're not in compact mode, make sure the
// pieces are spread out and placed at their
// final position.
@ -1682,10 +1711,19 @@ namespace libtorrent
{
m_storage->initialize(!m_fill_mode && !m_compact_mode);
m_current_slot = 0;
m_state = state_full_check;
m_piece_data.resize(int(m_info.piece_length()));
return std::make_pair(false, 0.f);
if (!m_unallocated_slots.empty())
{
assert(!m_fill_mode);
assert(!m_compact_mode);
std::vector<int>().swap(m_unallocated_slots);
std::fill(m_slot_to_piece.begin(), m_slot_to_piece.end(), int(unassigned));
m_free_slots.resize(m_info.num_pieces());
for (int i = 0; i < m_info.num_pieces(); ++i)
m_free_slots[i] = i;
}
m_state = state_finished;
return std::make_pair(true, 1.f);
}
assert(m_state == state_full_check);
@ -1697,6 +1735,7 @@ namespace libtorrent
try
{
m_piece_data.resize(int(m_info.piece_length()));
int piece_size = int(m_info.piece_size(m_current_slot));
int num_read = m_storage->read(&m_piece_data[0]
, m_current_slot, 0, piece_size);