2003-10-23 01:00:57 +02:00
|
|
|
/*
|
|
|
|
|
2003-11-29 02:54:41 +01:00
|
|
|
Copyright (c) 2003, Arvid Norberg, Daniel Wallin
|
2003-10-23 01:00:57 +02:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctime>
|
|
|
|
#include <iterator>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <set>
|
2004-12-21 13:30:09 +01:00
|
|
|
#include <functional>
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 1)
|
|
|
|
#endif
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include <boost/filesystem/convenience.hpp>
|
2004-10-18 12:46:55 +02:00
|
|
|
#include <boost/filesystem/operations.hpp>
|
2004-01-16 17:36:09 +01:00
|
|
|
#include <boost/filesystem/fstream.hpp>
|
2003-10-30 00:28:09 +01:00
|
|
|
#include <boost/thread/mutex.hpp>
|
2003-11-26 15:11:25 +01:00
|
|
|
#include <boost/ref.hpp>
|
2004-12-21 13:30:09 +01:00
|
|
|
#include <boost/date_time/posix_time/posix_time_types.hpp>
|
|
|
|
#include <boost/bind.hpp>
|
2005-03-05 00:45:16 +01:00
|
|
|
#include <boost/version.hpp>
|
2005-06-28 00:04:34 +02:00
|
|
|
#include <boost/multi_index_container.hpp>
|
|
|
|
#include <boost/multi_index/member.hpp>
|
|
|
|
#include <boost/multi_index/ordered_index.hpp>
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
#include "libtorrent/storage.hpp"
|
|
|
|
#include "libtorrent/torrent.hpp"
|
|
|
|
#include "libtorrent/hasher.hpp"
|
2003-10-30 00:28:09 +01:00
|
|
|
#include "libtorrent/session.hpp"
|
2003-12-07 06:53:04 +01:00
|
|
|
#include "libtorrent/peer_id.hpp"
|
2004-01-16 00:57:11 +01:00
|
|
|
#include "libtorrent/file.hpp"
|
2004-01-25 19:18:36 +01:00
|
|
|
#include "libtorrent/invariant_check.hpp"
|
2006-10-11 16:02:21 +02:00
|
|
|
#include "libtorrent/aux_/session_impl.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2005-09-01 23:04:21 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
#include <ios>
|
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <cstdio>
|
|
|
|
#endif
|
|
|
|
|
2005-08-18 13:20:17 +02:00
|
|
|
#if defined(_WIN32) && defined(UNICODE)
|
2005-08-16 20:39:38 +02:00
|
|
|
|
|
|
|
#include <windows.h>
|
2005-08-16 21:06:25 +02:00
|
|
|
#include <boost/filesystem/exception.hpp>
|
|
|
|
#include "libtorrent/utf8.hpp"
|
2005-08-16 20:39:38 +02:00
|
|
|
|
2005-11-01 19:30:39 +01:00
|
|
|
namespace libtorrent
|
2005-08-16 20:39:38 +02:00
|
|
|
{
|
|
|
|
std::wstring safe_convert(std::string const& s)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return libtorrent::utf8_wchar(s);
|
|
|
|
}
|
|
|
|
catch (std::exception)
|
|
|
|
{
|
|
|
|
std::wstring ret;
|
|
|
|
for (const char* i = &*s.begin(); i < &*s.end(); ++i)
|
|
|
|
{
|
|
|
|
wchar_t c;
|
|
|
|
c = '.';
|
|
|
|
std::mbtowc(&c, i, 1);
|
|
|
|
ret += c;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2005-11-01 19:30:39 +01:00
|
|
|
}
|
2005-08-16 20:39:38 +02:00
|
|
|
|
2005-11-01 19:30:39 +01:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
using libtorrent::safe_convert;
|
2005-08-16 21:09:06 +02:00
|
|
|
using namespace boost::filesystem;
|
|
|
|
|
2005-08-16 20:39:38 +02:00
|
|
|
// based on code from Boost.Fileystem
|
|
|
|
bool create_directories_win(const path& ph)
|
|
|
|
{
|
|
|
|
if (ph.empty() || exists(ph))
|
|
|
|
{
|
|
|
|
if ( !ph.empty() && !is_directory(ph) )
|
|
|
|
boost::throw_exception( filesystem_error(
|
|
|
|
"boost::filesystem::create_directories",
|
|
|
|
ph, "path exists and is not a directory",
|
|
|
|
not_directory_error ) );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First create branch, by calling ourself recursively
|
|
|
|
create_directories_win(ph.branch_path());
|
|
|
|
// Now that parent's path exists, create the directory
|
|
|
|
std::wstring wph(safe_convert(ph.native_directory_string()));
|
|
|
|
CreateDirectory(wph.c_str(), 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-08-16 21:35:14 +02:00
|
|
|
bool exists_win( const path & ph )
|
|
|
|
{
|
2005-10-17 22:37:40 +02:00
|
|
|
std::wstring wpath(safe_convert(ph.string()));
|
|
|
|
if(::GetFileAttributes( wpath.c_str() ) == 0xFFFFFFFF)
|
2005-08-16 21:35:14 +02:00
|
|
|
{
|
|
|
|
UINT err = ::GetLastError();
|
|
|
|
if((err == ERROR_FILE_NOT_FOUND)
|
|
|
|
|| (err == ERROR_INVALID_PARAMETER)
|
|
|
|
|| (err == ERROR_NOT_READY)
|
|
|
|
|| (err == ERROR_PATH_NOT_FOUND)
|
|
|
|
|| (err == ERROR_INVALID_NAME)
|
|
|
|
|| (err == ERROR_BAD_NETPATH ))
|
|
|
|
return false; // GetFileAttributes failed because the path does not exist
|
|
|
|
// for any other error we assume the file does exist and fall through,
|
|
|
|
// this may not be the best policy though... (JM 20040330)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-10-17 23:00:57 +02:00
|
|
|
boost::intmax_t file_size_win( const path & ph )
|
2005-10-17 22:37:40 +02:00
|
|
|
{
|
|
|
|
std::wstring wpath(safe_convert(ph.string()));
|
|
|
|
// by now, intmax_t is 64-bits on all Windows compilers
|
|
|
|
WIN32_FILE_ATTRIBUTE_DATA fad;
|
|
|
|
if ( !::GetFileAttributesExW( wpath.c_str(),
|
|
|
|
::GetFileExInfoStandard, &fad ) )
|
|
|
|
boost::throw_exception( filesystem_error(
|
|
|
|
"boost::filesystem::file_size",
|
2005-10-17 23:09:42 +02:00
|
|
|
ph, detail::system_error_code() ) );
|
2005-10-17 22:37:40 +02:00
|
|
|
if ( (fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) !=0 )
|
|
|
|
boost::throw_exception( filesystem_error(
|
|
|
|
"boost::filesystem::file_size",
|
|
|
|
ph, "invalid: is a directory",
|
|
|
|
is_directory_error ) );
|
|
|
|
return (static_cast<boost::intmax_t>(fad.nFileSizeHigh)
|
|
|
|
<< (sizeof(fad.nFileSizeLow)*8))
|
|
|
|
+ fad.nFileSizeLow;
|
|
|
|
}
|
2005-08-16 21:35:14 +02:00
|
|
|
|
2005-08-16 20:39:38 +02:00
|
|
|
std::time_t last_write_time_win( const path & ph )
|
|
|
|
{
|
2005-08-16 21:35:14 +02:00
|
|
|
struct _stat path_stat;
|
2005-08-16 20:39:38 +02:00
|
|
|
std::wstring wph(safe_convert(ph.native_file_string()));
|
2005-08-16 21:06:25 +02:00
|
|
|
if ( ::_wstat( wph.c_str(), &path_stat ) != 0 )
|
2005-08-16 20:39:38 +02:00
|
|
|
boost::throw_exception( filesystem_error(
|
|
|
|
"boost::filesystem::last_write_time",
|
2005-08-16 21:06:25 +02:00
|
|
|
ph, detail::system_error_code() ) );
|
2005-08-16 20:39:38 +02:00
|
|
|
return path_stat.st_mtime;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rename_win( const path & old_path,
|
|
|
|
const path & new_path )
|
|
|
|
{
|
|
|
|
std::wstring wold_path(safe_convert(old_path.string()));
|
|
|
|
std::wstring wnew_path(safe_convert(new_path.string()));
|
|
|
|
if ( !::MoveFile( wold_path.c_str(), wnew_path.c_str() ) )
|
|
|
|
boost::throw_exception( filesystem_error(
|
|
|
|
"boost::filesystem::rename",
|
2005-08-16 21:06:25 +02:00
|
|
|
old_path, new_path, detail::system_error_code() ) );
|
2005-08-16 20:39:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2005-03-05 00:45:16 +01:00
|
|
|
#if BOOST_VERSION < 103200
|
|
|
|
bool operator<(boost::filesystem::path const& lhs
|
|
|
|
, boost::filesystem::path const& rhs)
|
|
|
|
{
|
|
|
|
return lhs.string() < rhs.string();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-10-10 02:42:48 +02:00
|
|
|
using namespace boost::filesystem;
|
2004-12-21 13:30:09 +01:00
|
|
|
namespace pt = boost::posix_time;
|
|
|
|
using boost::bind;
|
2005-06-28 00:04:34 +02:00
|
|
|
using namespace ::boost::multi_index;
|
2006-01-08 23:55:02 +01:00
|
|
|
using boost::multi_index::multi_index_container;
|
2003-12-07 02:26:57 +01:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
namespace
|
|
|
|
{
|
2004-12-21 13:30:09 +01:00
|
|
|
using namespace libtorrent;
|
2003-12-07 15:12:14 +01:00
|
|
|
|
|
|
|
void print_to_log(const std::string& s)
|
|
|
|
{
|
|
|
|
static std::ofstream log("log.txt");
|
|
|
|
log << s;
|
2003-12-10 01:24:16 +01:00
|
|
|
log.flush();
|
2003-12-07 15:12:14 +01:00
|
|
|
}
|
2005-09-28 23:46:35 +02:00
|
|
|
/*
|
2005-06-28 00:04:34 +02:00
|
|
|
struct file_key
|
|
|
|
{
|
|
|
|
file_key(sha1_hash ih, path f): info_hash(ih), file_path(f) {}
|
|
|
|
file_key() {}
|
|
|
|
sha1_hash info_hash;
|
|
|
|
path file_path;
|
|
|
|
bool operator<(file_key const& fk) const
|
|
|
|
{
|
|
|
|
if (info_hash < fk.info_hash) return true;
|
|
|
|
if (fk.info_hash < info_hash) return false;
|
|
|
|
return file_path < fk.file_path;
|
|
|
|
}
|
|
|
|
};
|
2005-09-28 23:46:35 +02:00
|
|
|
*/
|
2006-04-25 23:04:48 +02:00
|
|
|
struct lru_file_entry
|
2004-12-21 13:30:09 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
lru_file_entry(boost::shared_ptr<file> const& f)
|
2005-06-28 00:04:34 +02:00
|
|
|
: file_ptr(f)
|
2004-12-21 13:30:09 +01:00
|
|
|
, last_use(pt::second_clock::universal_time()) {}
|
2005-06-28 00:04:34 +02:00
|
|
|
mutable boost::shared_ptr<file> file_ptr;
|
2005-09-28 23:46:35 +02:00
|
|
|
path file_path;
|
|
|
|
void* key;
|
2004-12-21 13:30:09 +01:00
|
|
|
pt::ptime last_use;
|
|
|
|
file::open_mode mode;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct file_pool
|
|
|
|
{
|
|
|
|
file_pool(int size): m_size(size) {}
|
|
|
|
|
2005-09-28 23:46:35 +02:00
|
|
|
boost::shared_ptr<file> open_file(void* st, path const& p, file::open_mode m)
|
2004-12-21 13:30:09 +01:00
|
|
|
{
|
2005-09-28 23:46:35 +02:00
|
|
|
assert(st != 0);
|
2005-01-14 15:10:38 +01:00
|
|
|
assert(p.is_complete());
|
2006-01-08 23:55:02 +01:00
|
|
|
typedef nth_index<file_set, 0>::type path_view;
|
|
|
|
path_view& pt = get<0>(m_files);
|
2005-09-28 23:46:35 +02:00
|
|
|
path_view::iterator i = pt.find(p);
|
2005-06-28 00:04:34 +02:00
|
|
|
if (i != pt.end())
|
2004-12-21 13:30:09 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
lru_file_entry e = *i;
|
2005-06-28 00:04:34 +02:00
|
|
|
e.last_use = pt::second_clock::universal_time();
|
2005-09-28 23:46:35 +02:00
|
|
|
|
|
|
|
// if you hit this assert, you probably have more than one
|
|
|
|
// storage/torrent using the same file at the same time!
|
|
|
|
assert(e.key == st);
|
|
|
|
|
|
|
|
e.key = st;
|
2005-06-28 00:04:34 +02:00
|
|
|
if ((e.mode & m) != m)
|
2004-12-21 13:30:09 +01:00
|
|
|
{
|
2005-03-03 01:40:44 +01:00
|
|
|
// close the file before we open it with
|
|
|
|
// the new read/write privilages
|
2005-06-30 23:38:28 +02:00
|
|
|
i->file_ptr.reset();
|
|
|
|
assert(e.file_ptr.unique());
|
2005-06-28 00:04:34 +02:00
|
|
|
e.file_ptr.reset();
|
|
|
|
e.file_ptr.reset(new file(p, m));
|
|
|
|
e.mode = m;
|
2004-12-21 13:30:09 +01:00
|
|
|
}
|
2005-06-28 00:04:34 +02:00
|
|
|
pt.replace(i, e);
|
|
|
|
return e.file_ptr;
|
2004-12-21 13:30:09 +01:00
|
|
|
}
|
|
|
|
// the file is not in our cache
|
|
|
|
if ((int)m_files.size() >= m_size)
|
|
|
|
{
|
2005-06-28 00:04:34 +02:00
|
|
|
// the file cache is at its maximum size, close
|
|
|
|
// the least recently used (lru) file from it
|
2006-01-08 23:55:02 +01:00
|
|
|
typedef nth_index<file_set, 1>::type lru_view;
|
|
|
|
lru_view& lt = get<1>(m_files);
|
2005-06-28 00:04:34 +02:00
|
|
|
lru_view::iterator i = lt.begin();
|
|
|
|
// the first entry in this view is the least recently used
|
2005-06-28 01:11:09 +02:00
|
|
|
/* for (lru_view::iterator i = lt.begin(); i != lt.end(); ++i)
|
|
|
|
{
|
|
|
|
std::cerr << i->last_use << "\n";
|
|
|
|
}
|
|
|
|
*/ assert(lt.size() == 1 || (i->last_use <= boost::next(i)->last_use));
|
2005-06-28 00:04:34 +02:00
|
|
|
lt.erase(i);
|
2004-12-21 13:30:09 +01:00
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
lru_file_entry e(boost::shared_ptr<file>(new file(p, m)));
|
2004-12-21 13:30:09 +01:00
|
|
|
e.mode = m;
|
2005-09-28 23:46:35 +02:00
|
|
|
e.key = st;
|
|
|
|
e.file_path = p;
|
2005-06-28 00:04:34 +02:00
|
|
|
pt.insert(e);
|
|
|
|
return e.file_ptr;
|
2004-12-21 13:30:09 +01:00
|
|
|
}
|
|
|
|
|
2005-09-28 23:46:35 +02:00
|
|
|
void release(void* st)
|
2004-12-21 13:30:09 +01:00
|
|
|
{
|
2005-09-28 23:46:35 +02:00
|
|
|
assert(st != 0);
|
|
|
|
using boost::tie;
|
|
|
|
|
2006-01-08 23:55:02 +01:00
|
|
|
typedef nth_index<file_set, 2>::type key_view;
|
|
|
|
key_view& kt = get<2>(m_files);
|
2005-09-28 23:46:35 +02:00
|
|
|
|
|
|
|
key_view::iterator start, end;
|
|
|
|
tie(start, end) = kt.equal_range(st);
|
2005-06-28 00:04:34 +02:00
|
|
|
|
2005-06-30 14:59:23 +02:00
|
|
|
/*
|
|
|
|
std::cerr << "releasing files!\n";
|
|
|
|
for (path_view::iterator i = r.first; i != r.second; ++i)
|
|
|
|
{
|
|
|
|
std::cerr << i->key.file_path.native_file_string() << "\n";
|
|
|
|
}
|
|
|
|
*/
|
2005-09-28 23:46:35 +02:00
|
|
|
kt.erase(start, end);
|
2005-06-30 14:59:23 +02:00
|
|
|
/*
|
|
|
|
std::cerr << "files left: " << pt.size() << "\n";
|
|
|
|
for (path_view::iterator i = pt.begin(); i != pt.end(); ++i)
|
|
|
|
{
|
|
|
|
std::cerr << i->key.file_path.native_file_string() << "\n";
|
|
|
|
}
|
|
|
|
*/
|
2004-12-21 13:30:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int m_size;
|
2005-06-28 00:04:34 +02:00
|
|
|
|
2006-01-08 23:55:02 +01:00
|
|
|
typedef multi_index_container<
|
2006-04-25 23:04:48 +02:00
|
|
|
lru_file_entry, indexed_by<
|
|
|
|
ordered_unique<member<lru_file_entry, path
|
|
|
|
, &lru_file_entry::file_path> >
|
|
|
|
, ordered_non_unique<member<lru_file_entry, pt::ptime
|
|
|
|
, &lru_file_entry::last_use> >
|
|
|
|
, ordered_non_unique<member<lru_file_entry, void*
|
|
|
|
, &lru_file_entry::key> >
|
2005-06-28 00:04:34 +02:00
|
|
|
>
|
|
|
|
> file_set;
|
|
|
|
|
|
|
|
file_set m_files;
|
2004-12-21 13:30:09 +01:00
|
|
|
};
|
2003-12-07 15:12:14 +01:00
|
|
|
}
|
|
|
|
|
2004-01-17 21:04:19 +01:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
|
2005-03-05 00:45:16 +01:00
|
|
|
std::vector<std::pair<size_type, std::time_t> > get_filesizes(
|
2005-10-16 11:15:46 +02:00
|
|
|
torrent_info const& t, path p)
|
2004-01-17 21:04:19 +01:00
|
|
|
{
|
2004-10-10 02:42:48 +02:00
|
|
|
p = complete(p);
|
2005-03-05 00:45:16 +01:00
|
|
|
std::vector<std::pair<size_type, std::time_t> > sizes;
|
2004-01-17 21:04:19 +01:00
|
|
|
for (torrent_info::file_iterator i = t.begin_files();
|
2005-06-30 14:59:23 +02:00
|
|
|
i != t.end_files(); ++i)
|
2004-01-17 21:04:19 +01:00
|
|
|
{
|
2005-03-05 00:45:16 +01:00
|
|
|
size_type size = 0;
|
|
|
|
std::time_t time = 0;
|
2004-01-18 11:22:18 +01:00
|
|
|
try
|
|
|
|
{
|
2005-05-12 01:03:12 +02:00
|
|
|
path f = p / i->path;
|
2005-08-18 13:20:17 +02:00
|
|
|
#if defined(_WIN32) && defined(UNICODE)
|
2005-10-17 22:37:40 +02:00
|
|
|
size = file_size_win(f);
|
2005-08-16 20:39:38 +02:00
|
|
|
time = last_write_time_win(f);
|
|
|
|
#else
|
2005-10-17 22:37:40 +02:00
|
|
|
size = file_size(f);
|
2005-03-05 00:45:16 +01:00
|
|
|
time = last_write_time(f);
|
2005-08-16 20:39:38 +02:00
|
|
|
#endif
|
2004-01-18 11:22:18 +01:00
|
|
|
}
|
2005-03-05 15:17:17 +01:00
|
|
|
catch (std::exception&) {}
|
2005-03-05 00:45:16 +01:00
|
|
|
sizes.push_back(std::make_pair(size, time));
|
2004-01-17 21:04:19 +01:00
|
|
|
}
|
|
|
|
return sizes;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool match_filesizes(
|
2004-10-10 02:42:48 +02:00
|
|
|
torrent_info const& t
|
|
|
|
, path p
|
2005-06-16 17:41:04 +02:00
|
|
|
, std::vector<std::pair<size_type, std::time_t> > const& sizes
|
|
|
|
, std::string* error)
|
2004-01-17 21:04:19 +01:00
|
|
|
{
|
2005-06-16 17:41:04 +02:00
|
|
|
if ((int)sizes.size() != t.num_files())
|
|
|
|
{
|
|
|
|
if (error) *error = "mismatching number of files";
|
|
|
|
return false;
|
|
|
|
}
|
2004-10-10 02:42:48 +02:00
|
|
|
p = complete(p);
|
2004-01-17 21:04:19 +01:00
|
|
|
|
2005-03-05 00:45:16 +01:00
|
|
|
std::vector<std::pair<size_type, std::time_t> >::const_iterator s
|
|
|
|
= sizes.begin();
|
2004-01-17 21:04:19 +01:00
|
|
|
for (torrent_info::file_iterator i = t.begin_files();
|
2005-06-16 17:41:04 +02:00
|
|
|
i != t.end_files(); ++i, ++s)
|
2004-01-17 21:04:19 +01:00
|
|
|
{
|
2005-03-05 00:45:16 +01:00
|
|
|
size_type size = 0;
|
|
|
|
std::time_t time = 0;
|
2004-01-18 11:22:18 +01:00
|
|
|
try
|
|
|
|
{
|
2005-05-12 01:03:12 +02:00
|
|
|
path f = p / i->path;
|
2005-08-18 13:20:17 +02:00
|
|
|
#if defined(_WIN32) && defined(UNICODE)
|
2005-10-17 22:37:40 +02:00
|
|
|
size = file_size_win(f);
|
2005-08-16 20:39:38 +02:00
|
|
|
time = last_write_time_win(f);
|
|
|
|
#else
|
2005-10-17 22:37:40 +02:00
|
|
|
size = file_size(f);
|
2005-03-05 00:45:16 +01:00
|
|
|
time = last_write_time(f);
|
2005-08-16 20:39:38 +02:00
|
|
|
#endif
|
2004-01-18 11:22:18 +01:00
|
|
|
}
|
2005-03-05 15:17:17 +01:00
|
|
|
catch (std::exception&) {}
|
2005-06-16 17:41:04 +02:00
|
|
|
if (size != s->first)
|
|
|
|
{
|
|
|
|
if (error) *error = "filesize mismatch for file '"
|
|
|
|
+ i->path.native_file_string()
|
|
|
|
+ "', expected to be " + boost::lexical_cast<std::string>(s->first)
|
|
|
|
+ " bytes";
|
2005-03-05 00:45:16 +01:00
|
|
|
return false;
|
2005-06-16 17:41:04 +02:00
|
|
|
}
|
|
|
|
if (time != s->second)
|
|
|
|
{
|
|
|
|
if (error) *error = "timestamp mismatch for file '"
|
|
|
|
+ i->path.native_file_string()
|
|
|
|
+ "', expected to have modification date "
|
|
|
|
+ boost::lexical_cast<std::string>(s->second);
|
|
|
|
return false;
|
|
|
|
}
|
2004-01-17 21:04:19 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2003-12-07 02:26:57 +01:00
|
|
|
|
2003-12-07 17:26:16 +01:00
|
|
|
struct thread_safe_storage
|
|
|
|
{
|
|
|
|
thread_safe_storage(std::size_t n)
|
|
|
|
: slots(n, false)
|
2004-01-25 13:37:15 +01:00
|
|
|
{}
|
2003-12-07 17:26:16 +01:00
|
|
|
|
|
|
|
boost::mutex mutex;
|
|
|
|
boost::condition condition;
|
|
|
|
std::vector<bool> slots;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct slot_lock
|
|
|
|
{
|
|
|
|
slot_lock(thread_safe_storage& s, int slot_)
|
|
|
|
: storage_(s)
|
|
|
|
, slot(slot_)
|
|
|
|
{
|
2004-01-25 13:37:15 +01:00
|
|
|
assert(slot_>=0 && slot_ < (int)s.slots.size());
|
2003-12-07 17:26:16 +01:00
|
|
|
boost::mutex::scoped_lock lock(storage_.mutex);
|
|
|
|
|
|
|
|
while (storage_.slots[slot])
|
|
|
|
storage_.condition.wait(lock);
|
|
|
|
storage_.slots[slot] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
~slot_lock()
|
|
|
|
{
|
|
|
|
storage_.slots[slot] = false;
|
|
|
|
storage_.condition.notify_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
thread_safe_storage& storage_;
|
|
|
|
int slot;
|
|
|
|
};
|
|
|
|
|
2005-09-28 23:46:35 +02:00
|
|
|
class storage::impl : public thread_safe_storage, boost::noncopyable
|
2003-12-07 17:26:16 +01:00
|
|
|
{
|
2004-01-25 19:18:36 +01:00
|
|
|
public:
|
2004-10-10 02:42:48 +02:00
|
|
|
impl(torrent_info const& info, path const& path)
|
2003-12-07 17:26:16 +01:00
|
|
|
: thread_safe_storage(info.num_pieces())
|
|
|
|
, info(info)
|
2004-10-10 02:42:48 +02:00
|
|
|
{
|
|
|
|
save_path = complete(path);
|
2004-10-18 12:36:47 +02:00
|
|
|
assert(save_path.is_complete());
|
2004-10-10 02:42:48 +02:00
|
|
|
}
|
2003-12-07 17:26:16 +01:00
|
|
|
|
2004-10-10 02:42:48 +02:00
|
|
|
impl(impl const& x)
|
2003-12-07 17:26:16 +01:00
|
|
|
: thread_safe_storage(x.info.num_pieces())
|
|
|
|
, info(x.info)
|
|
|
|
, save_path(x.save_path)
|
|
|
|
{}
|
|
|
|
|
2005-06-30 14:59:23 +02:00
|
|
|
~impl()
|
|
|
|
{
|
2005-09-28 23:46:35 +02:00
|
|
|
files.release(this);
|
2005-06-30 14:59:23 +02:00
|
|
|
}
|
|
|
|
|
2004-06-05 14:53:06 +02:00
|
|
|
torrent_info const& info;
|
2004-10-10 02:42:48 +02:00
|
|
|
path save_path;
|
2005-06-28 00:04:34 +02:00
|
|
|
static file_pool files;
|
2003-12-07 17:26:16 +01:00
|
|
|
};
|
|
|
|
|
2005-06-28 01:11:09 +02:00
|
|
|
file_pool storage::impl::files(40);
|
2005-06-28 00:04:34 +02:00
|
|
|
|
2005-10-16 11:15:46 +02:00
|
|
|
storage::storage(torrent_info const& info, path const& path)
|
2003-12-09 09:49:49 +01:00
|
|
|
: m_pimpl(new impl(info, path))
|
2003-12-07 06:53:04 +01:00
|
|
|
{
|
|
|
|
assert(info.begin_files() != info.end_files());
|
|
|
|
}
|
2003-12-07 02:26:57 +01:00
|
|
|
|
2005-03-10 12:26:55 +01:00
|
|
|
void storage::release_files()
|
2004-12-21 13:30:09 +01:00
|
|
|
{
|
2005-09-28 23:46:35 +02:00
|
|
|
m_pimpl->files.release(m_pimpl.get());
|
2004-12-21 13:30:09 +01:00
|
|
|
}
|
|
|
|
|
2003-12-07 17:26:16 +01:00
|
|
|
void storage::swap(storage& other)
|
|
|
|
{
|
2003-12-09 09:49:49 +01:00
|
|
|
m_pimpl.swap(other.m_pimpl);
|
2003-12-07 17:26:16 +01:00
|
|
|
}
|
|
|
|
|
2004-07-18 02:39:58 +02:00
|
|
|
// returns true on success
|
2004-10-10 02:42:48 +02:00
|
|
|
bool storage::move_storage(path save_path)
|
2004-07-18 02:39:58 +02:00
|
|
|
{
|
2005-03-19 13:22:40 +01:00
|
|
|
path old_path;
|
|
|
|
path new_path;
|
2004-07-18 02:39:58 +02:00
|
|
|
|
2004-10-10 02:42:48 +02:00
|
|
|
save_path = complete(save_path);
|
|
|
|
|
2005-08-18 13:20:17 +02:00
|
|
|
#if defined(_WIN32) && defined(UNICODE)
|
2005-08-16 22:18:11 +02:00
|
|
|
std::wstring wsave_path(safe_convert(save_path.native_file_string()));
|
2005-08-16 21:35:14 +02:00
|
|
|
if (!exists_win(save_path))
|
2005-08-16 20:39:38 +02:00
|
|
|
{
|
|
|
|
CreateDirectory(wsave_path.c_str(), 0);
|
|
|
|
}
|
2005-08-16 21:06:25 +02:00
|
|
|
else if ((GetFileAttributes(wsave_path.c_str()) & FILE_ATTRIBUTE_DIRECTORY) == 0)
|
2005-08-16 20:39:38 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#else
|
2004-10-10 02:42:48 +02:00
|
|
|
if(!exists(save_path))
|
|
|
|
create_directory(save_path);
|
|
|
|
else if(!is_directory(save_path))
|
2004-08-01 21:17:38 +02:00
|
|
|
return false;
|
2005-08-16 20:39:38 +02:00
|
|
|
#endif
|
2004-07-18 13:40:22 +02:00
|
|
|
|
2005-09-28 23:46:35 +02:00
|
|
|
m_pimpl->files.release(m_pimpl.get());
|
2004-12-21 13:30:09 +01:00
|
|
|
|
2004-07-18 02:39:58 +02:00
|
|
|
if (m_pimpl->info.num_files() == 1)
|
|
|
|
{
|
2004-10-10 02:42:48 +02:00
|
|
|
path single_file = m_pimpl->info.begin_files()->path;
|
2004-07-18 13:40:22 +02:00
|
|
|
if (single_file.has_branch_path())
|
2005-08-16 21:35:14 +02:00
|
|
|
{
|
2005-08-18 13:20:17 +02:00
|
|
|
#if defined(_WIN32) && defined(UNICODE)
|
2005-08-16 22:18:11 +02:00
|
|
|
std::wstring wsave_path(safe_convert((save_path / single_file.branch_path())
|
|
|
|
.native_directory_string()));
|
2005-08-16 21:35:14 +02:00
|
|
|
CreateDirectory(wsave_path.c_str(), 0);
|
|
|
|
#else
|
2004-10-10 02:42:48 +02:00
|
|
|
create_directory(save_path / single_file.branch_path());
|
2005-08-16 21:35:14 +02:00
|
|
|
#endif
|
|
|
|
}
|
2004-07-18 13:40:22 +02:00
|
|
|
|
2005-03-19 13:22:40 +01:00
|
|
|
old_path = m_pimpl->save_path / single_file;
|
|
|
|
new_path = save_path / m_pimpl->info.begin_files()->path;
|
2004-07-18 02:39:58 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(m_pimpl->info.num_files() > 1);
|
2005-03-19 13:22:40 +01:00
|
|
|
old_path = m_pimpl->save_path / m_pimpl->info.name();
|
|
|
|
new_path = save_path / m_pimpl->info.name();
|
2004-07-18 02:39:58 +02:00
|
|
|
}
|
|
|
|
|
2005-03-19 13:22:40 +01:00
|
|
|
try
|
2004-07-18 02:39:58 +02:00
|
|
|
{
|
2005-08-18 13:20:17 +02:00
|
|
|
#if defined(_WIN32) && defined(UNICODE)
|
2005-08-16 20:39:38 +02:00
|
|
|
rename_win(old_path, new_path);
|
|
|
|
#else
|
2005-03-19 13:22:40 +01:00
|
|
|
rename(old_path, new_path);
|
2005-08-16 20:39:38 +02:00
|
|
|
#endif
|
2004-07-18 02:39:58 +02:00
|
|
|
m_pimpl->save_path = save_path;
|
|
|
|
return true;
|
|
|
|
}
|
2005-03-19 13:22:40 +01:00
|
|
|
catch (std::exception&) {}
|
2004-07-18 02:39:58 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-01-27 00:38:08 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
|
|
|
|
void storage::shuffle()
|
|
|
|
{
|
|
|
|
int num_pieces = m_pimpl->info.num_pieces();
|
|
|
|
|
|
|
|
std::vector<int> pieces(num_pieces);
|
|
|
|
for (std::vector<int>::iterator i = pieces.begin();
|
|
|
|
i != pieces.end();
|
|
|
|
++i)
|
|
|
|
{
|
2004-01-28 12:37:46 +01:00
|
|
|
*i = static_cast<int>(i - pieces.begin());
|
2004-01-27 00:38:08 +01:00
|
|
|
}
|
2004-01-27 18:48:58 +01:00
|
|
|
std::srand((unsigned int)std::time(0));
|
2004-01-27 00:38:08 +01:00
|
|
|
std::vector<int> targets(pieces);
|
|
|
|
std::random_shuffle(pieces.begin(), pieces.end());
|
|
|
|
std::random_shuffle(targets.begin(), targets.end());
|
|
|
|
|
2005-08-16 21:06:25 +02:00
|
|
|
for (int i = 0; i < (std::max)(num_pieces / 50, 1); ++i)
|
2004-01-27 00:38:08 +01:00
|
|
|
{
|
|
|
|
const int slot_index = targets[i];
|
|
|
|
const int piece_index = pieces[i];
|
2004-03-07 21:50:56 +01:00
|
|
|
const int slot_size =static_cast<int>(m_pimpl->info.piece_size(slot_index));
|
2004-01-27 00:38:08 +01:00
|
|
|
std::vector<char> buf(slot_size);
|
|
|
|
read(&buf[0], piece_index, 0, slot_size);
|
|
|
|
write(&buf[0], slot_index, 0, slot_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2004-01-18 02:58:33 +01:00
|
|
|
size_type storage::read(
|
2003-12-07 02:26:57 +01:00
|
|
|
char* buf
|
2005-03-19 13:22:40 +01:00
|
|
|
, int slot
|
|
|
|
, int offset
|
|
|
|
, int size)
|
2003-12-07 02:26:57 +01:00
|
|
|
{
|
2004-01-25 19:18:36 +01:00
|
|
|
assert(buf != 0);
|
2004-01-25 05:18:08 +01:00
|
|
|
assert(slot >= 0 && slot < m_pimpl->info.num_pieces());
|
2003-12-17 17:37:20 +01:00
|
|
|
assert(offset >= 0);
|
|
|
|
assert(offset < m_pimpl->info.piece_size(slot));
|
2003-12-07 15:12:14 +01:00
|
|
|
assert(size > 0);
|
2003-12-07 17:26:16 +01:00
|
|
|
|
|
|
|
slot_lock lock(*m_pimpl, slot);
|
2003-12-09 09:49:49 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
std::vector<file_slice> slices
|
|
|
|
= m_pimpl->info.map_block(slot, offset, size);
|
|
|
|
assert(!slices.empty());
|
|
|
|
#endif
|
|
|
|
|
2004-02-23 23:54:54 +01:00
|
|
|
size_type start = slot * (size_type)m_pimpl->info.piece_length() + offset;
|
2006-07-26 12:21:25 +02:00
|
|
|
assert(start + size <= m_pimpl->info.total_size());
|
2003-12-07 02:26:57 +01:00
|
|
|
|
|
|
|
// find the file iterator and file offset
|
|
|
|
size_type file_offset = start;
|
2004-01-16 00:57:11 +01:00
|
|
|
std::vector<file_entry>::const_iterator file_iter;
|
2003-12-07 02:26:57 +01:00
|
|
|
|
2003-12-07 17:26:16 +01:00
|
|
|
for (file_iter = m_pimpl->info.begin_files();;)
|
2003-12-07 02:26:57 +01:00
|
|
|
{
|
|
|
|
if (file_offset < file_iter->size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
file_offset -= file_iter->size;
|
|
|
|
++file_iter;
|
|
|
|
}
|
2004-01-25 19:18:36 +01:00
|
|
|
|
2004-12-21 13:30:09 +01:00
|
|
|
boost::shared_ptr<file> in(m_pimpl->files.open_file(
|
2005-09-28 23:46:35 +02:00
|
|
|
m_pimpl.get()
|
2005-06-28 00:04:34 +02:00
|
|
|
, m_pimpl->save_path / file_iter->path
|
2004-12-21 13:30:09 +01:00
|
|
|
, file::in));
|
2003-12-07 02:26:57 +01:00
|
|
|
|
|
|
|
assert(file_offset < file_iter->size);
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
assert(slices[0].offset == file_offset);
|
|
|
|
|
|
|
|
size_type new_pos = in->seek(file_offset);
|
|
|
|
if (new_pos != file_offset)
|
2004-01-25 19:18:36 +01:00
|
|
|
{
|
|
|
|
// the file was not big enough
|
|
|
|
throw file_error("slot has no storage");
|
|
|
|
}
|
2003-12-07 15:12:14 +01:00
|
|
|
|
2004-01-16 14:07:47 +01:00
|
|
|
#ifndef NDEBUG
|
2004-12-21 13:30:09 +01:00
|
|
|
size_type in_tell = in->tell();
|
2004-01-16 16:18:02 +01:00
|
|
|
assert(in_tell == file_offset);
|
2004-01-16 14:07:47 +01:00
|
|
|
#endif
|
2003-12-07 02:26:57 +01:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
int left_to_read = size;
|
2004-03-07 21:50:56 +01:00
|
|
|
int slot_size = static_cast<int>(m_pimpl->info.piece_size(slot));
|
2003-12-07 02:26:57 +01:00
|
|
|
|
|
|
|
if (offset + left_to_read > slot_size)
|
|
|
|
left_to_read = slot_size - offset;
|
|
|
|
|
|
|
|
assert(left_to_read >= 0);
|
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
size_type result = left_to_read;
|
2003-12-07 02:26:57 +01:00
|
|
|
int buf_pos = 0;
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
int counter = 0;
|
|
|
|
#endif
|
|
|
|
|
2003-12-07 02:26:57 +01:00
|
|
|
while (left_to_read > 0)
|
|
|
|
{
|
|
|
|
int read_bytes = left_to_read;
|
|
|
|
if (file_offset + read_bytes > file_iter->size)
|
2004-01-25 19:18:36 +01:00
|
|
|
read_bytes = static_cast<int>(file_iter->size - file_offset);
|
2003-12-07 02:26:57 +01:00
|
|
|
|
2006-08-10 21:18:11 +02:00
|
|
|
if (read_bytes > 0)
|
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
#ifndef NDEBUG
|
2006-08-10 21:18:11 +02:00
|
|
|
assert(int(slices.size()) > counter);
|
|
|
|
size_type slice_size = slices[counter].size;
|
|
|
|
assert(slice_size == read_bytes);
|
|
|
|
assert(m_pimpl->info.file_at(slices[counter].file_index).path
|
|
|
|
== file_iter->path);
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
|
|
|
|
2006-08-10 21:18:11 +02:00
|
|
|
size_type actual_read = in->read(buf + buf_pos, read_bytes);
|
2003-12-07 02:26:57 +01:00
|
|
|
|
2006-08-10 21:18:11 +02:00
|
|
|
if (read_bytes != actual_read)
|
|
|
|
{
|
|
|
|
// the file was not big enough
|
|
|
|
throw file_error("slot has no storage");
|
|
|
|
}
|
2003-12-07 02:26:57 +01:00
|
|
|
|
2006-08-10 21:18:11 +02:00
|
|
|
left_to_read -= read_bytes;
|
|
|
|
buf_pos += read_bytes;
|
|
|
|
assert(buf_pos >= 0);
|
|
|
|
file_offset += read_bytes;
|
|
|
|
}
|
2003-12-07 02:26:57 +01:00
|
|
|
|
|
|
|
if (left_to_read > 0)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2003-12-07 02:26:57 +01:00
|
|
|
++file_iter;
|
2006-04-25 23:04:48 +02:00
|
|
|
#ifndef NDEBUG
|
2006-08-10 21:18:11 +02:00
|
|
|
// empty files are not returned by map_block, so if
|
|
|
|
// this file was empty, don't increment the slice counter
|
|
|
|
if (read_bytes > 0) ++counter;
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
2005-05-12 01:03:12 +02:00
|
|
|
path path = m_pimpl->save_path / file_iter->path;
|
2003-12-07 02:26:57 +01:00
|
|
|
|
|
|
|
file_offset = 0;
|
2005-06-28 00:04:34 +02:00
|
|
|
in = m_pimpl->files.open_file(
|
2005-09-28 23:46:35 +02:00
|
|
|
m_pimpl.get()
|
2005-06-28 00:04:34 +02:00
|
|
|
, path, file::in);
|
2004-12-21 13:30:09 +01:00
|
|
|
in->seek(0);
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2003-12-07 02:26:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-01-26 11:29:00 +01:00
|
|
|
// throws file_error if it fails to write
|
2004-01-25 19:18:36 +01:00
|
|
|
void storage::write(
|
|
|
|
const char* buf
|
|
|
|
, int slot
|
|
|
|
, int offset
|
|
|
|
, int size)
|
2003-12-07 02:26:57 +01:00
|
|
|
{
|
2004-01-25 13:37:15 +01:00
|
|
|
assert(buf != 0);
|
2004-01-25 19:18:36 +01:00
|
|
|
assert(slot >= 0);
|
|
|
|
assert(slot < m_pimpl->info.num_pieces());
|
2004-01-25 05:18:08 +01:00
|
|
|
assert(offset >= 0);
|
2003-12-07 15:12:14 +01:00
|
|
|
assert(size > 0);
|
|
|
|
|
2003-12-07 17:26:16 +01:00
|
|
|
slot_lock lock(*m_pimpl, slot);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
std::vector<file_slice> slices
|
|
|
|
= m_pimpl->info.map_block(slot, offset, size);
|
|
|
|
assert(!slices.empty());
|
|
|
|
#endif
|
2003-12-07 17:26:16 +01:00
|
|
|
|
2004-02-23 23:54:54 +01:00
|
|
|
size_type start = slot * (size_type)m_pimpl->info.piece_length() + offset;
|
2003-12-07 02:26:57 +01:00
|
|
|
|
|
|
|
// find the file iterator and file offset
|
|
|
|
size_type file_offset = start;
|
2004-01-16 00:57:11 +01:00
|
|
|
std::vector<file_entry>::const_iterator file_iter;
|
2003-12-07 02:26:57 +01:00
|
|
|
|
2003-12-07 17:26:16 +01:00
|
|
|
for (file_iter = m_pimpl->info.begin_files();;)
|
2003-12-07 02:26:57 +01:00
|
|
|
{
|
|
|
|
if (file_offset < file_iter->size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
file_offset -= file_iter->size;
|
|
|
|
++file_iter;
|
2005-03-12 11:12:36 +01:00
|
|
|
assert(file_iter != m_pimpl->info.end_files());
|
2003-12-07 02:26:57 +01:00
|
|
|
}
|
|
|
|
|
2005-05-12 01:03:12 +02:00
|
|
|
path p(m_pimpl->save_path / file_iter->path);
|
2005-06-28 00:04:34 +02:00
|
|
|
boost::shared_ptr<file> out = m_pimpl->files.open_file(
|
2005-09-28 23:46:35 +02:00
|
|
|
m_pimpl.get()
|
2005-06-28 00:04:34 +02:00
|
|
|
, p, file::out | file::in);
|
2003-12-07 02:26:57 +01:00
|
|
|
|
|
|
|
assert(file_offset < file_iter->size);
|
2006-04-25 23:04:48 +02:00
|
|
|
assert(slices[0].offset == file_offset);
|
2003-12-07 02:26:57 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
size_type pos = out->seek(file_offset);
|
2003-12-07 15:12:14 +01:00
|
|
|
|
2004-03-31 01:55:52 +02:00
|
|
|
if (pos != file_offset)
|
2004-01-26 11:29:00 +01:00
|
|
|
{
|
|
|
|
std::stringstream s;
|
|
|
|
s << "no storage for slot " << slot;
|
|
|
|
throw file_error(s.str());
|
|
|
|
}
|
2003-12-07 02:26:57 +01:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
int left_to_write = size;
|
2004-03-07 21:50:56 +01:00
|
|
|
int slot_size = static_cast<int>(m_pimpl->info.piece_size(slot));
|
2003-12-07 02:26:57 +01:00
|
|
|
|
|
|
|
if (offset + left_to_write > slot_size)
|
|
|
|
left_to_write = slot_size - offset;
|
|
|
|
|
|
|
|
assert(left_to_write >= 0);
|
|
|
|
|
|
|
|
int buf_pos = 0;
|
2006-04-25 23:04:48 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
int counter = 0;
|
|
|
|
#endif
|
2003-12-07 02:26:57 +01:00
|
|
|
while (left_to_write > 0)
|
|
|
|
{
|
|
|
|
int write_bytes = left_to_write;
|
|
|
|
if (file_offset + write_bytes > file_iter->size)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2005-03-12 11:12:36 +01:00
|
|
|
assert(file_iter->size >= file_offset);
|
2004-01-25 19:18:36 +01:00
|
|
|
write_bytes = static_cast<int>(file_iter->size - file_offset);
|
2003-12-07 02:26:57 +01:00
|
|
|
}
|
|
|
|
|
2006-08-10 21:18:11 +02:00
|
|
|
if (write_bytes > 0)
|
|
|
|
{
|
|
|
|
assert(int(slices.size()) > counter);
|
|
|
|
assert(slices[counter].size == write_bytes);
|
|
|
|
assert(m_pimpl->info.file_at(slices[counter].file_index).path
|
|
|
|
== file_iter->path);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2006-08-10 21:18:11 +02:00
|
|
|
assert(buf_pos >= 0);
|
|
|
|
assert(write_bytes >= 0);
|
|
|
|
size_type written = out->write(buf + buf_pos, write_bytes);
|
2004-01-26 11:29:00 +01:00
|
|
|
|
2006-08-10 21:18:11 +02:00
|
|
|
if (written != write_bytes)
|
|
|
|
{
|
|
|
|
std::stringstream s;
|
|
|
|
s << "no storage for slot " << slot;
|
|
|
|
throw file_error(s.str());
|
|
|
|
}
|
2003-12-07 02:26:57 +01:00
|
|
|
|
2006-08-10 21:18:11 +02:00
|
|
|
left_to_write -= write_bytes;
|
|
|
|
buf_pos += write_bytes;
|
|
|
|
assert(buf_pos >= 0);
|
|
|
|
file_offset += write_bytes;
|
|
|
|
assert(file_offset <= file_iter->size);
|
|
|
|
}
|
2003-12-07 02:26:57 +01:00
|
|
|
|
|
|
|
if (left_to_write > 0)
|
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
#ifndef NDEBUG
|
2006-08-10 21:18:11 +02:00
|
|
|
if (write_bytes > 0) ++counter;
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
2003-12-07 02:26:57 +01:00
|
|
|
++file_iter;
|
|
|
|
|
2003-12-07 17:26:16 +01:00
|
|
|
assert(file_iter != m_pimpl->info.end_files());
|
2005-05-12 01:03:12 +02:00
|
|
|
path p = m_pimpl->save_path / file_iter->path;
|
2003-12-07 02:26:57 +01:00
|
|
|
file_offset = 0;
|
2005-06-28 00:04:34 +02:00
|
|
|
out = m_pimpl->files.open_file(
|
2005-09-28 23:46:35 +02:00
|
|
|
m_pimpl.get()
|
2005-06-28 00:04:34 +02:00
|
|
|
, p, file::out | file::in);
|
|
|
|
|
2004-12-21 13:30:09 +01:00
|
|
|
out->seek(0);
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
}
|
2003-12-09 09:49:49 +01:00
|
|
|
}
|
2003-12-07 02:26:57 +01:00
|
|
|
|
2003-12-09 09:49:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// -- piece_manager -----------------------------------------------------
|
|
|
|
|
|
|
|
class piece_manager::impl
|
|
|
|
{
|
2004-01-25 19:18:36 +01:00
|
|
|
friend class invariant_access;
|
2003-12-09 09:49:49 +01:00
|
|
|
public:
|
|
|
|
|
|
|
|
impl(
|
2005-10-16 11:15:46 +02:00
|
|
|
torrent_info const& info
|
|
|
|
, path const& path);
|
2003-12-09 09:49:49 +01:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
bool check_fastresume(
|
2006-10-11 16:02:21 +02:00
|
|
|
aux::piece_checker_data& d
|
2005-05-13 02:39:39 +02:00
|
|
|
, std::vector<bool>& pieces
|
2006-04-25 23:04:48 +02:00
|
|
|
, int& num_pieces
|
2005-05-13 02:39:39 +02:00
|
|
|
, bool compact_mode);
|
2003-12-09 09:49:49 +01:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
std::pair<bool, float> check_files(
|
2006-04-25 23:04:48 +02:00
|
|
|
std::vector<bool>& pieces
|
|
|
|
, int& num_pieces);
|
2005-10-13 09:59:05 +02:00
|
|
|
|
2005-03-10 12:26:55 +01:00
|
|
|
void release_files();
|
2004-12-21 13:30:09 +01:00
|
|
|
|
2003-12-09 09:49:49 +01:00
|
|
|
void allocate_slots(int num_slots);
|
2004-01-12 04:05:10 +01:00
|
|
|
void mark_failed(int index);
|
2004-01-24 18:14:03 +01:00
|
|
|
unsigned long piece_crc(
|
|
|
|
int slot_index
|
|
|
|
, int block_size
|
|
|
|
, const std::bitset<256>& bitmask);
|
|
|
|
|
|
|
|
int slot_for_piece(int piece_index) const;
|
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
size_type read(
|
|
|
|
char* buf
|
|
|
|
, int piece_index
|
|
|
|
, int offset
|
|
|
|
, int size);
|
|
|
|
|
|
|
|
void write(
|
|
|
|
const char* buf
|
|
|
|
, int piece_index
|
|
|
|
, int offset
|
|
|
|
, int size);
|
2003-12-09 09:49:49 +01:00
|
|
|
|
2004-10-10 02:42:48 +02:00
|
|
|
path const& save_path() const
|
2003-12-09 09:49:49 +01:00
|
|
|
{ return m_save_path; }
|
|
|
|
|
2004-10-10 02:42:48 +02:00
|
|
|
bool move_storage(path save_path)
|
2004-07-18 02:39:58 +02:00
|
|
|
{
|
|
|
|
if (m_storage.move_storage(save_path))
|
|
|
|
{
|
2004-11-18 23:33:50 +01:00
|
|
|
m_save_path = complete(save_path);
|
2004-07-18 02:39:58 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-12-27 02:34:50 +01:00
|
|
|
void export_piece_map(std::vector<int>& p) const;
|
2003-12-25 13:11:31 +01:00
|
|
|
|
2003-12-09 09:49:49 +01:00
|
|
|
// returns the slot currently associated with the given
|
|
|
|
// piece or assigns the given piece_index to a free slot
|
2005-05-13 02:39:39 +02:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
int identify_data(
|
|
|
|
const std::vector<char>& piece_data
|
|
|
|
, int current_slot
|
|
|
|
, std::vector<bool>& have_pieces
|
2006-04-25 23:04:48 +02:00
|
|
|
, int& num_pieces
|
2004-01-25 19:18:36 +01:00
|
|
|
, const std::multimap<sha1_hash, int>& hash_to_piece);
|
|
|
|
|
2004-01-24 18:14:03 +01:00
|
|
|
int allocate_slot_for_piece(int piece_index);
|
2004-01-17 21:04:19 +01:00
|
|
|
#ifndef NDEBUG
|
2003-12-09 09:49:49 +01:00
|
|
|
void check_invariant() const;
|
2004-01-24 18:14:03 +01:00
|
|
|
#ifdef TORRENT_STORAGE_DEBUG
|
2003-12-09 09:49:49 +01:00
|
|
|
void debug_log() const;
|
2004-01-24 18:14:03 +01:00
|
|
|
#endif
|
2004-01-17 21:04:19 +01:00
|
|
|
#endif
|
2003-12-09 09:49:49 +01:00
|
|
|
storage m_storage;
|
|
|
|
|
2005-05-13 02:39:39 +02:00
|
|
|
// if this is true, pieces are always allocated at the
|
|
|
|
// lowest possible slot index. If it is false, pieces
|
|
|
|
// are always written to their final place immediately
|
|
|
|
bool m_compact_mode;
|
|
|
|
|
|
|
|
// if this is true, pieces that haven't been downloaded
|
|
|
|
// will be filled with zeroes. Not filling with zeroes
|
|
|
|
// will not work in some cases (where a seek cannot pass
|
|
|
|
// the end of the file).
|
|
|
|
bool m_fill_mode;
|
|
|
|
|
2003-12-09 09:49:49 +01:00
|
|
|
// a bitmask representing the pieces we have
|
|
|
|
std::vector<bool> m_have_piece;
|
|
|
|
|
2005-10-16 11:15:46 +02:00
|
|
|
torrent_info const& m_info;
|
2003-12-09 09:49:49 +01:00
|
|
|
|
2005-08-12 14:40:58 +02:00
|
|
|
// slots that haven't had any file storage allocated
|
2003-12-09 09:49:49 +01:00
|
|
|
std::vector<int> m_unallocated_slots;
|
2005-08-12 14:40:58 +02:00
|
|
|
// slots that have file storage, but isn't assigned to a piece
|
2003-12-09 09:49:49 +01:00
|
|
|
std::vector<int> m_free_slots;
|
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
has_no_slot = -3 // the piece has no storage
|
|
|
|
};
|
|
|
|
|
|
|
|
// maps piece indices to slots. If a piece doesn't
|
|
|
|
// have any storage, it is set to 'has_no_slot'
|
|
|
|
std::vector<int> m_piece_to_slot;
|
|
|
|
|
2004-01-16 17:36:09 +01:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
unallocated = -1, // the slot is unallocated
|
|
|
|
unassigned = -2 // the slot is allocated but not assigned to a piece
|
2004-01-16 17:19:27 +01:00
|
|
|
};
|
2004-01-16 17:36:09 +01:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
// maps slots to piece indices, if a slot doesn't have a piece
|
|
|
|
// it can either be 'unassigned' or 'unallocated'
|
2003-12-09 09:49:49 +01:00
|
|
|
std::vector<int> m_slot_to_piece;
|
|
|
|
|
2004-10-10 02:42:48 +02:00
|
|
|
path m_save_path;
|
2003-12-09 09:49:49 +01:00
|
|
|
|
|
|
|
mutable boost::recursive_mutex m_mutex;
|
|
|
|
|
|
|
|
bool m_allocating;
|
|
|
|
boost::mutex m_allocating_monitor;
|
|
|
|
boost::condition m_allocating_condition;
|
2005-10-13 09:59:05 +02:00
|
|
|
|
|
|
|
// these states are used while checking/allocating the torrent
|
|
|
|
|
|
|
|
enum {
|
|
|
|
// the default initial state
|
|
|
|
state_none,
|
|
|
|
// the file checking is complete
|
|
|
|
state_finished,
|
|
|
|
// creating the directories
|
|
|
|
state_create_files,
|
|
|
|
// checking the files
|
|
|
|
state_full_check,
|
|
|
|
// allocating files (in non-compact mode)
|
|
|
|
state_allocating
|
|
|
|
} m_state;
|
|
|
|
int m_current_slot;
|
|
|
|
|
|
|
|
std::vector<char> m_piece_data;
|
|
|
|
|
|
|
|
// this maps a piece hash to piece index. It will be
|
|
|
|
// build the first time it is used (to save time if it
|
|
|
|
// isn't needed)
|
|
|
|
std::multimap<sha1_hash, int> m_hash_to_piece;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
// used as temporary piece data storage in allocate_slots
|
|
|
|
// it is a member in order to avoid allocating it on
|
|
|
|
// the heap every time a new slot is allocated. (This is quite
|
|
|
|
// frequent with high download speeds)
|
|
|
|
std::vector<char> m_scratch_buffer;
|
2003-12-09 09:49:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
piece_manager::impl::impl(
|
2005-10-16 11:15:46 +02:00
|
|
|
torrent_info const& info
|
|
|
|
, path const& save_path)
|
2003-12-07 02:26:57 +01:00
|
|
|
: m_storage(info, save_path)
|
2005-05-13 02:39:39 +02:00
|
|
|
, m_compact_mode(false)
|
|
|
|
, m_fill_mode(true)
|
2003-12-07 02:26:57 +01:00
|
|
|
, m_info(info)
|
2004-10-18 12:46:55 +02:00
|
|
|
, m_save_path(complete(save_path))
|
2004-01-25 19:18:36 +01:00
|
|
|
, m_allocating(false)
|
2003-12-07 02:26:57 +01:00
|
|
|
{
|
2004-10-18 12:36:47 +02:00
|
|
|
assert(m_save_path.is_complete());
|
2003-12-07 02:26:57 +01:00
|
|
|
}
|
|
|
|
|
2003-12-09 09:49:49 +01:00
|
|
|
piece_manager::piece_manager(
|
2005-10-16 11:15:46 +02:00
|
|
|
torrent_info const& info
|
|
|
|
, path const& save_path)
|
2003-12-09 09:49:49 +01:00
|
|
|
: m_pimpl(new impl(info, save_path))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2004-01-04 13:54:38 +01:00
|
|
|
piece_manager::~piece_manager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2005-03-10 12:26:55 +01:00
|
|
|
void piece_manager::release_files()
|
2004-12-21 13:30:09 +01:00
|
|
|
{
|
2005-03-10 12:26:55 +01:00
|
|
|
m_pimpl->release_files();
|
2004-12-21 13:30:09 +01:00
|
|
|
}
|
|
|
|
|
2005-03-10 12:26:55 +01:00
|
|
|
void piece_manager::impl::release_files()
|
2004-12-21 13:30:09 +01:00
|
|
|
{
|
2005-03-10 12:26:55 +01:00
|
|
|
m_storage.release_files();
|
2004-12-21 13:30:09 +01:00
|
|
|
}
|
|
|
|
|
2003-12-25 13:11:31 +01:00
|
|
|
void piece_manager::impl::export_piece_map(
|
2003-12-27 02:34:50 +01:00
|
|
|
std::vector<int>& p) const
|
2003-12-25 13:11:31 +01:00
|
|
|
{
|
2004-01-12 04:05:10 +01:00
|
|
|
// synchronization ------------------------------------------------------
|
|
|
|
boost::recursive_mutex::scoped_lock lock(m_mutex);
|
|
|
|
// ----------------------------------------------------------------------
|
2004-01-25 22:37:19 +01:00
|
|
|
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2003-12-25 13:11:31 +01:00
|
|
|
p.clear();
|
2003-12-27 02:34:50 +01:00
|
|
|
std::vector<int>::const_reverse_iterator last;
|
|
|
|
for (last = m_slot_to_piece.rbegin();
|
2005-05-13 02:39:39 +02:00
|
|
|
last != m_slot_to_piece.rend(); ++last)
|
2003-12-27 02:34:50 +01:00
|
|
|
{
|
2004-01-16 17:19:27 +01:00
|
|
|
if (*last != unallocated) break;
|
2003-12-27 02:34:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (std::vector<int>::const_iterator i =
|
|
|
|
m_slot_to_piece.begin();
|
2005-05-13 02:39:39 +02:00
|
|
|
i != last.base(); ++i)
|
2003-12-25 13:11:31 +01:00
|
|
|
{
|
2003-12-27 02:34:50 +01:00
|
|
|
p.push_back(*i);
|
2003-12-25 13:11:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void piece_manager::export_piece_map(
|
2003-12-27 02:34:50 +01:00
|
|
|
std::vector<int>& p) const
|
2003-12-25 13:11:31 +01:00
|
|
|
{
|
|
|
|
m_pimpl->export_piece_map(p);
|
|
|
|
}
|
2004-01-12 04:05:10 +01:00
|
|
|
|
2004-01-13 04:08:59 +01:00
|
|
|
void piece_manager::impl::mark_failed(int piece_index)
|
2004-01-12 04:05:10 +01:00
|
|
|
{
|
|
|
|
// synchronization ------------------------------------------------------
|
|
|
|
boost::recursive_mutex::scoped_lock lock(m_mutex);
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
|
2004-01-25 22:37:19 +01:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2004-01-25 13:37:15 +01:00
|
|
|
assert(piece_index >= 0 && piece_index < (int)m_piece_to_slot.size());
|
2004-01-13 04:08:59 +01:00
|
|
|
assert(m_piece_to_slot[piece_index] >= 0);
|
|
|
|
|
|
|
|
int slot_index = m_piece_to_slot[piece_index];
|
|
|
|
|
|
|
|
assert(slot_index >= 0);
|
2004-01-12 04:05:10 +01:00
|
|
|
|
2004-01-16 17:19:27 +01:00
|
|
|
m_slot_to_piece[slot_index] = unassigned;
|
|
|
|
m_piece_to_slot[piece_index] = has_no_slot;
|
2004-01-13 04:08:59 +01:00
|
|
|
m_free_slots.push_back(slot_index);
|
2004-01-12 04:05:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void piece_manager::mark_failed(int index)
|
|
|
|
{
|
|
|
|
m_pimpl->mark_failed(index);
|
|
|
|
}
|
|
|
|
|
2005-10-17 15:45:53 +02:00
|
|
|
bool piece_manager::is_allocating() const
|
|
|
|
{
|
|
|
|
return m_pimpl->m_state
|
|
|
|
== impl::state_allocating;
|
|
|
|
}
|
|
|
|
|
2004-01-24 18:14:03 +01:00
|
|
|
int piece_manager::slot_for_piece(int piece_index) const
|
|
|
|
{
|
|
|
|
return m_pimpl->slot_for_piece(piece_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
int piece_manager::impl::slot_for_piece(int piece_index) const
|
|
|
|
{
|
|
|
|
assert(piece_index >= 0 && piece_index < m_info.num_pieces());
|
|
|
|
return m_piece_to_slot[piece_index];
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long piece_manager::piece_crc(
|
|
|
|
int index
|
|
|
|
, int block_size
|
|
|
|
, const std::bitset<256>& bitmask)
|
|
|
|
{
|
|
|
|
return m_pimpl->piece_crc(index, block_size, bitmask);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long piece_manager::impl::piece_crc(
|
|
|
|
int slot_index
|
|
|
|
, int block_size
|
|
|
|
, const std::bitset<256>& bitmask)
|
|
|
|
{
|
2004-01-25 13:37:15 +01:00
|
|
|
assert(slot_index >= 0);
|
|
|
|
assert(slot_index < m_info.num_pieces());
|
|
|
|
assert(block_size > 0);
|
2004-01-25 05:18:08 +01:00
|
|
|
|
2004-01-24 18:14:03 +01:00
|
|
|
adler32_crc crc;
|
|
|
|
std::vector<char> buf(block_size);
|
2004-03-07 21:50:56 +01:00
|
|
|
int num_blocks = static_cast<int>(m_info.piece_size(slot_index)) / block_size;
|
|
|
|
int last_block_size = static_cast<int>(m_info.piece_size(slot_index)) % block_size;
|
2004-01-24 18:14:03 +01:00
|
|
|
if (last_block_size == 0) last_block_size = block_size;
|
|
|
|
|
|
|
|
for (int i = 0; i < num_blocks-1; ++i)
|
|
|
|
{
|
|
|
|
if (!bitmask[i]) continue;
|
|
|
|
m_storage.read(
|
|
|
|
&buf[0]
|
|
|
|
, slot_index
|
|
|
|
, i * block_size
|
|
|
|
, block_size);
|
|
|
|
crc.update(&buf[0], block_size);
|
|
|
|
}
|
|
|
|
if (bitmask[num_blocks - 1])
|
|
|
|
{
|
|
|
|
m_storage.read(
|
|
|
|
&buf[0]
|
|
|
|
, slot_index
|
|
|
|
, block_size * (num_blocks - 1)
|
|
|
|
, last_block_size);
|
|
|
|
crc.update(&buf[0], last_block_size);
|
|
|
|
}
|
|
|
|
return crc.final();
|
|
|
|
}
|
2004-01-12 04:05:10 +01:00
|
|
|
|
2004-01-18 02:58:33 +01:00
|
|
|
size_type piece_manager::impl::read(
|
2003-12-07 06:53:04 +01:00
|
|
|
char* buf
|
2003-12-09 09:49:49 +01:00
|
|
|
, int piece_index
|
2004-01-25 19:18:36 +01:00
|
|
|
, int offset
|
|
|
|
, int size)
|
2003-12-07 02:26:57 +01:00
|
|
|
{
|
2004-01-24 20:19:17 +01:00
|
|
|
assert(buf);
|
|
|
|
assert(offset >= 0);
|
|
|
|
assert(size > 0);
|
2004-01-25 13:37:15 +01:00
|
|
|
assert(piece_index >= 0 && piece_index < (int)m_piece_to_slot.size());
|
|
|
|
assert(m_piece_to_slot[piece_index] >= 0 && m_piece_to_slot[piece_index] < (int)m_slot_to_piece.size());
|
2003-12-07 02:26:57 +01:00
|
|
|
int slot = m_piece_to_slot[piece_index];
|
2004-01-25 13:37:15 +01:00
|
|
|
assert(slot >= 0 && slot < (int)m_slot_to_piece.size());
|
2003-12-07 02:26:57 +01:00
|
|
|
return m_storage.read(buf, slot, offset, size);
|
|
|
|
}
|
|
|
|
|
2004-01-18 02:58:33 +01:00
|
|
|
size_type piece_manager::read(
|
2003-12-09 09:49:49 +01:00
|
|
|
char* buf
|
|
|
|
, int piece_index
|
2004-01-25 19:18:36 +01:00
|
|
|
, int offset
|
|
|
|
, int size)
|
2003-12-09 09:49:49 +01:00
|
|
|
{
|
|
|
|
return m_pimpl->read(buf, piece_index, offset, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void piece_manager::impl::write(
|
2003-12-07 06:53:04 +01:00
|
|
|
const char* buf
|
2003-12-09 09:49:49 +01:00
|
|
|
, int piece_index
|
2004-01-25 19:18:36 +01:00
|
|
|
, int offset
|
|
|
|
, int size)
|
2003-12-07 02:26:57 +01:00
|
|
|
{
|
2004-01-24 20:19:17 +01:00
|
|
|
assert(buf);
|
|
|
|
assert(offset >= 0);
|
|
|
|
assert(size > 0);
|
2004-01-25 13:37:15 +01:00
|
|
|
assert(piece_index >= 0 && piece_index < (int)m_piece_to_slot.size());
|
2004-01-24 18:14:03 +01:00
|
|
|
int slot = allocate_slot_for_piece(piece_index);
|
2004-01-25 13:37:15 +01:00
|
|
|
assert(slot >= 0 && slot < (int)m_slot_to_piece.size());
|
2003-12-07 02:26:57 +01:00
|
|
|
m_storage.write(buf, slot, offset, size);
|
|
|
|
}
|
|
|
|
|
2003-12-09 09:49:49 +01:00
|
|
|
void piece_manager::write(
|
|
|
|
const char* buf
|
|
|
|
, int piece_index
|
2004-01-25 19:18:36 +01:00
|
|
|
, int offset
|
|
|
|
, int size)
|
2003-12-09 09:49:49 +01:00
|
|
|
{
|
|
|
|
m_pimpl->write(buf, piece_index, offset, size);
|
|
|
|
}
|
2004-01-03 03:10:11 +01:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
int piece_manager::impl::identify_data(
|
|
|
|
const std::vector<char>& piece_data
|
|
|
|
, int current_slot
|
|
|
|
, std::vector<bool>& have_pieces
|
2006-04-25 23:04:48 +02:00
|
|
|
, int& num_pieces
|
2004-01-25 19:18:36 +01:00
|
|
|
, const std::multimap<sha1_hash, int>& hash_to_piece)
|
|
|
|
{
|
2004-01-27 00:38:08 +01:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2004-01-26 11:29:00 +01:00
|
|
|
assert((int)have_pieces.size() == m_info.num_pieces());
|
2004-01-25 19:18:36 +01:00
|
|
|
|
2004-03-07 21:50:56 +01:00
|
|
|
const int piece_size = static_cast<int>(m_info.piece_length());
|
|
|
|
const int last_piece_size = static_cast<int>(m_info.piece_size(
|
|
|
|
m_info.num_pieces() - 1));
|
2004-01-25 19:18:36 +01:00
|
|
|
|
|
|
|
assert((int)piece_data.size() >= last_piece_size);
|
|
|
|
|
|
|
|
// calculate a small digest, with the same
|
|
|
|
// size as the last piece. And a large digest
|
|
|
|
// which has the same size as a normal piece
|
|
|
|
hasher small_digest;
|
|
|
|
small_digest.update(&piece_data[0], last_piece_size);
|
|
|
|
hasher large_digest(small_digest);
|
2004-06-14 01:30:42 +02:00
|
|
|
assert(piece_size - last_piece_size >= 0);
|
2004-06-05 14:53:06 +02:00
|
|
|
if (piece_size - last_piece_size > 0)
|
|
|
|
{
|
|
|
|
large_digest.update(
|
|
|
|
&piece_data[last_piece_size]
|
|
|
|
, piece_size - last_piece_size);
|
|
|
|
}
|
2004-01-25 19:18:36 +01:00
|
|
|
sha1_hash large_hash = large_digest.final();
|
|
|
|
sha1_hash small_hash = small_digest.final();
|
|
|
|
|
|
|
|
typedef std::multimap<sha1_hash, int>::const_iterator map_iter;
|
|
|
|
map_iter begin1;
|
|
|
|
map_iter end1;
|
|
|
|
map_iter begin2;
|
|
|
|
map_iter end2;
|
|
|
|
|
|
|
|
// makes the lookups for the small digest and the large digest
|
|
|
|
boost::tie(begin1, end1) = hash_to_piece.equal_range(small_hash);
|
|
|
|
boost::tie(begin2, end2) = hash_to_piece.equal_range(large_hash);
|
|
|
|
|
|
|
|
// copy all potential piece indices into this vector
|
|
|
|
std::vector<int> matching_pieces;
|
|
|
|
for (map_iter i = begin1; i != end1; ++i)
|
|
|
|
matching_pieces.push_back(i->second);
|
|
|
|
for (map_iter i = begin2; i != end2; ++i)
|
|
|
|
matching_pieces.push_back(i->second);
|
|
|
|
|
|
|
|
// no piece matched the data in the slot
|
|
|
|
if (matching_pieces.empty())
|
2004-01-27 18:48:58 +01:00
|
|
|
return unassigned;
|
2004-01-25 19:18:36 +01:00
|
|
|
|
|
|
|
// ------------------------------------------
|
|
|
|
// CHECK IF THE PIECE IS IN ITS CORRECT PLACE
|
|
|
|
// ------------------------------------------
|
|
|
|
|
|
|
|
if (std::find(
|
|
|
|
matching_pieces.begin()
|
|
|
|
, matching_pieces.end()
|
|
|
|
, current_slot) != matching_pieces.end())
|
|
|
|
{
|
|
|
|
const int piece_index = current_slot;
|
|
|
|
|
|
|
|
if (have_pieces[piece_index])
|
|
|
|
{
|
|
|
|
// we have already found a piece with
|
|
|
|
// this index.
|
|
|
|
int other_slot = m_piece_to_slot[piece_index];
|
|
|
|
assert(other_slot >= 0);
|
|
|
|
|
|
|
|
// take one of the other matching pieces
|
|
|
|
// that hasn't already been assigned
|
|
|
|
int other_piece = -1;
|
|
|
|
for (std::vector<int>::iterator i = matching_pieces.begin();
|
2005-11-01 19:30:39 +01:00
|
|
|
i != matching_pieces.end(); ++i)
|
2004-01-25 19:18:36 +01:00
|
|
|
{
|
|
|
|
if (have_pieces[*i] || *i == piece_index) continue;
|
|
|
|
other_piece = *i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (other_piece >= 0)
|
|
|
|
{
|
2004-01-27 00:38:08 +01:00
|
|
|
// replace the old slot with 'other_piece'
|
2004-01-25 19:18:36 +01:00
|
|
|
assert(have_pieces[other_piece] == false);
|
|
|
|
have_pieces[other_piece] = true;
|
|
|
|
m_slot_to_piece[other_slot] = other_piece;
|
|
|
|
m_piece_to_slot[other_piece] = other_slot;
|
2006-06-24 16:15:11 +02:00
|
|
|
++num_pieces;
|
2004-01-25 19:18:36 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-01-27 00:38:08 +01:00
|
|
|
// this index is the only piece with this
|
|
|
|
// hash. The previous slot we found with
|
2006-04-25 23:04:48 +02:00
|
|
|
// this hash must be the same piece. Mark
|
2004-01-27 00:38:08 +01:00
|
|
|
// that piece as unassigned, since this slot
|
|
|
|
// is the correct place for the piece.
|
2004-01-25 19:18:36 +01:00
|
|
|
m_slot_to_piece[other_slot] = unassigned;
|
|
|
|
m_free_slots.push_back(other_slot);
|
|
|
|
}
|
2004-01-26 02:08:59 +01:00
|
|
|
assert(m_piece_to_slot[piece_index] != current_slot);
|
|
|
|
assert(m_piece_to_slot[piece_index] >= 0);
|
|
|
|
m_piece_to_slot[piece_index] = has_no_slot;
|
2006-04-25 23:04:48 +02:00
|
|
|
#ifndef NDEBUG
|
2006-06-24 16:15:11 +02:00
|
|
|
// to make the assert happy, a few lines down
|
2004-01-26 02:45:30 +01:00
|
|
|
have_pieces[piece_index] = false;
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++num_pieces;
|
2004-01-25 19:18:36 +01:00
|
|
|
}
|
|
|
|
|
2004-01-26 02:45:30 +01:00
|
|
|
assert(have_pieces[piece_index] == false);
|
|
|
|
assert(m_piece_to_slot[piece_index] == has_no_slot);
|
2004-01-25 19:18:36 +01:00
|
|
|
have_pieces[piece_index] = true;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
return piece_index;
|
|
|
|
}
|
|
|
|
|
2004-01-26 02:08:59 +01:00
|
|
|
// find a matching piece that hasn't
|
|
|
|
// already been assigned
|
2004-01-27 18:48:58 +01:00
|
|
|
int free_piece = unassigned;
|
2004-01-26 02:08:59 +01:00
|
|
|
for (std::vector<int>::iterator i = matching_pieces.begin();
|
2005-05-13 02:39:39 +02:00
|
|
|
i != matching_pieces.end(); ++i)
|
2004-01-26 02:08:59 +01:00
|
|
|
{
|
|
|
|
if (have_pieces[*i]) continue;
|
|
|
|
free_piece = *i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-01-26 02:45:30 +01:00
|
|
|
if (free_piece >= 0)
|
|
|
|
{
|
|
|
|
assert(have_pieces[free_piece] == false);
|
|
|
|
assert(m_piece_to_slot[free_piece] == has_no_slot);
|
|
|
|
have_pieces[free_piece] = true;
|
2006-04-25 23:04:48 +02:00
|
|
|
++num_pieces;
|
2004-01-27 00:38:08 +01:00
|
|
|
|
2004-01-26 02:45:30 +01:00
|
|
|
return free_piece;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-01-27 18:48:58 +01:00
|
|
|
assert(free_piece == unassigned);
|
|
|
|
return unassigned;
|
2004-01-26 02:45:30 +01:00
|
|
|
}
|
2004-01-25 19:18:36 +01:00
|
|
|
}
|
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
// check if the fastresume data is up to date
|
|
|
|
// if it is, use it and return true. If it
|
|
|
|
// isn't return false and the full check
|
|
|
|
// will be run
|
|
|
|
bool piece_manager::impl::check_fastresume(
|
2006-10-11 16:02:21 +02:00
|
|
|
aux::piece_checker_data& data
|
2005-05-13 02:39:39 +02:00
|
|
|
, std::vector<bool>& pieces
|
2006-04-25 23:04:48 +02:00
|
|
|
, int& num_pieces, bool compact_mode)
|
2004-01-25 19:18:36 +01:00
|
|
|
{
|
2004-06-05 14:53:06 +02:00
|
|
|
assert(m_info.piece_length() > 0);
|
2004-01-25 19:18:36 +01:00
|
|
|
// synchronization ------------------------------------------------------
|
|
|
|
boost::recursive_mutex::scoped_lock lock(m_mutex);
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2005-05-13 02:39:39 +02:00
|
|
|
m_compact_mode = compact_mode;
|
|
|
|
|
2004-01-27 00:39:24 +01:00
|
|
|
// This will corrupt the storage
|
|
|
|
// use while debugging to find
|
|
|
|
// states that cannot be scanned
|
|
|
|
// by check_pieces.
|
2004-01-27 00:38:08 +01:00
|
|
|
// m_storage.shuffle();
|
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
m_piece_to_slot.resize(m_info.num_pieces(), has_no_slot);
|
|
|
|
m_slot_to_piece.resize(m_info.num_pieces(), unallocated);
|
|
|
|
m_free_slots.clear();
|
|
|
|
m_unallocated_slots.clear();
|
2004-04-03 00:21:20 +02:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
pieces.clear();
|
|
|
|
pieces.resize(m_info.num_pieces(), false);
|
2006-04-25 23:04:48 +02:00
|
|
|
num_pieces = 0;
|
2004-01-27 00:39:24 +01:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
// if we have fast-resume info
|
|
|
|
// use it instead of doing the actual checking
|
|
|
|
if (!data.piece_map.empty()
|
|
|
|
&& data.piece_map.size() <= m_slot_to_piece.size())
|
|
|
|
{
|
|
|
|
for (int i = 0; i < (int)data.piece_map.size(); ++i)
|
|
|
|
{
|
|
|
|
m_slot_to_piece[i] = data.piece_map[i];
|
|
|
|
if (data.piece_map[i] >= 0)
|
|
|
|
{
|
|
|
|
m_piece_to_slot[data.piece_map[i]] = i;
|
|
|
|
int found_piece = data.piece_map[i];
|
|
|
|
|
|
|
|
// if the piece is not in the unfinished list
|
|
|
|
// we have all of it
|
|
|
|
if (std::find_if(
|
|
|
|
data.unfinished_pieces.begin()
|
|
|
|
, data.unfinished_pieces.end()
|
|
|
|
, piece_picker::has_index(found_piece))
|
|
|
|
== data.unfinished_pieces.end())
|
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
++num_pieces;
|
2004-01-25 19:18:36 +01:00
|
|
|
pieces[found_piece] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (data.piece_map[i] == unassigned)
|
|
|
|
{
|
|
|
|
m_free_slots.push_back(i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(data.piece_map[i] == unallocated);
|
|
|
|
m_unallocated_slots.push_back(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
m_unallocated_slots.reserve(int(pieces.size() - data.piece_map.size()));
|
2004-01-25 19:18:36 +01:00
|
|
|
for (int i = (int)data.piece_map.size(); i < (int)pieces.size(); ++i)
|
|
|
|
{
|
|
|
|
m_unallocated_slots.push_back(i);
|
|
|
|
}
|
2005-05-13 02:39:39 +02:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
if (!m_compact_mode && !m_unallocated_slots.empty())
|
|
|
|
{
|
|
|
|
m_state = state_allocating;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_state = state_finished;
|
|
|
|
return true;
|
|
|
|
}
|
2004-01-25 19:18:36 +01:00
|
|
|
}
|
2004-01-27 00:39:24 +01:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
m_state = state_create_files;
|
|
|
|
return false;
|
|
|
|
}
|
2004-01-25 22:51:30 +01:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
// performs the full check and full allocation
|
|
|
|
// (if necessary). returns true if finished and
|
|
|
|
// false if it should be called again
|
|
|
|
// the second return value is the progress the
|
|
|
|
// file check is at. 0 is nothing done, and 1
|
|
|
|
// is finished
|
|
|
|
std::pair<bool, float> piece_manager::impl::check_files(
|
2006-04-25 23:04:48 +02:00
|
|
|
std::vector<bool>& pieces, int& num_pieces)
|
2005-10-13 09:59:05 +02:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
assert(num_pieces == std::count(pieces.begin(), pieces.end(), true));
|
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
if (m_state == state_allocating)
|
|
|
|
{
|
|
|
|
if (m_compact_mode)
|
|
|
|
{
|
|
|
|
m_state = state_finished;
|
|
|
|
return std::make_pair(true, 1.f);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_unallocated_slots.empty())
|
|
|
|
{
|
|
|
|
m_state = state_finished;
|
|
|
|
return std::make_pair(true, 1.f);
|
|
|
|
}
|
2005-08-16 22:42:10 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// if we're not in compact mode, make sure the
|
|
|
|
// pieces are spread out and placed at their
|
|
|
|
// final position.
|
|
|
|
assert(!m_unallocated_slots.empty());
|
|
|
|
allocate_slots(1);
|
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
return std::make_pair(false, 1.f - (float)m_unallocated_slots.size()
|
|
|
|
/ (float)m_slot_to_piece.size());
|
|
|
|
}
|
2005-08-16 22:42:10 +02:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
if (m_state == state_create_files)
|
2005-08-16 22:42:10 +02:00
|
|
|
{
|
2005-10-13 09:59:05 +02:00
|
|
|
// first, create all missing directories
|
|
|
|
path last_path;
|
|
|
|
for (torrent_info::file_iterator file_iter = m_info.begin_files(),
|
2005-11-01 19:30:39 +01:00
|
|
|
end_iter = m_info.end_files(); file_iter != end_iter; ++file_iter)
|
2005-10-13 09:59:05 +02:00
|
|
|
{
|
|
|
|
path dir = (m_save_path / file_iter->path).branch_path();
|
2006-07-08 21:41:39 +02:00
|
|
|
|
|
|
|
// if the file is empty, just create it. But also make sure
|
|
|
|
// the directory exits.
|
|
|
|
if (dir == last_path
|
|
|
|
&& file_iter->size == 0)
|
|
|
|
file(m_save_path / file_iter->path, file::out);
|
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
if (dir == last_path) continue;
|
|
|
|
last_path = dir;
|
2005-08-16 22:42:10 +02:00
|
|
|
|
2005-08-18 13:20:17 +02:00
|
|
|
#if defined(_WIN32) && defined(UNICODE)
|
2005-10-13 09:59:05 +02:00
|
|
|
if (!exists_win(last_path))
|
|
|
|
create_directories_win(last_path);
|
2005-08-16 22:42:10 +02:00
|
|
|
#else
|
2005-10-13 09:59:05 +02:00
|
|
|
if (!exists(last_path))
|
|
|
|
create_directories(last_path);
|
2005-08-16 22:42:10 +02:00
|
|
|
#endif
|
2006-07-08 21:41:39 +02:00
|
|
|
|
|
|
|
if (file_iter->size == 0)
|
|
|
|
file(m_save_path / file_iter->path, file::out);
|
2005-10-13 09:59:05 +02:00
|
|
|
}
|
|
|
|
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);
|
2005-08-16 22:42:10 +02:00
|
|
|
}
|
2004-01-25 19:18:36 +01:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
assert(m_state == state_full_check);
|
|
|
|
|
|
|
|
// ------------------------
|
|
|
|
// DO THE FULL CHECK
|
|
|
|
// ------------------------
|
|
|
|
|
|
|
|
try
|
2004-01-25 19:18:36 +01:00
|
|
|
{
|
2004-01-27 00:38:08 +01:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
m_storage.read(
|
2006-09-24 13:22:25 +02:00
|
|
|
&m_piece_data[0]
|
|
|
|
, m_current_slot
|
|
|
|
, 0
|
|
|
|
, int(m_info.piece_size(m_current_slot)));
|
2004-01-25 19:18:36 +01:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
if (m_hash_to_piece.empty())
|
|
|
|
{
|
|
|
|
for (int i = 0; i < m_info.num_pieces(); ++i)
|
2005-05-25 12:01:01 +02:00
|
|
|
{
|
2005-10-13 09:59:05 +02:00
|
|
|
m_hash_to_piece.insert(std::make_pair(m_info.hash_for_piece(i), i));
|
2005-05-25 12:01:01 +02:00
|
|
|
}
|
2005-10-13 09:59:05 +02:00
|
|
|
}
|
2005-05-25 12:01:01 +02:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
int piece_index = identify_data(
|
2006-09-24 13:22:25 +02:00
|
|
|
m_piece_data
|
|
|
|
, m_current_slot
|
|
|
|
, pieces
|
|
|
|
, num_pieces
|
|
|
|
, m_hash_to_piece);
|
2004-01-27 00:38:08 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
assert(num_pieces == std::count(pieces.begin(), pieces.end(), true));
|
2005-10-13 09:59:05 +02:00
|
|
|
assert(piece_index == unassigned || piece_index >= 0);
|
2004-01-27 00:38:08 +01:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
const bool this_should_move = piece_index >= 0 && m_slot_to_piece[piece_index] != unallocated;
|
|
|
|
const bool other_should_move = m_piece_to_slot[m_current_slot] != has_no_slot;
|
2004-01-27 00:38:08 +01:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
// check if this piece should be swapped with any other slot
|
|
|
|
// this section will ensure that the storage is correctly sorted
|
|
|
|
// libtorrent will never leave the storage in a state that
|
|
|
|
// requires this sorting, but other clients may.
|
2004-01-27 00:38:08 +01:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
// example of worst case:
|
|
|
|
// | m_current_slot = 5
|
|
|
|
// V
|
|
|
|
// +---+- - - +---+- - - +---+- -
|
|
|
|
// | x | | 5 | | 3 | <- piece data in slots
|
|
|
|
// +---+- - - +---+- - - +---+- -
|
|
|
|
// 3 y 5 <- slot index
|
|
|
|
|
|
|
|
// in this example, the data in the m_current_slot (5)
|
|
|
|
// is piece 3. It has to be moved into slot 3. The data
|
|
|
|
// in slot y (piece 5) should be moved into the m_current_slot.
|
|
|
|
// and the data in slot 3 (piece x) should be moved to slot y.
|
|
|
|
|
|
|
|
// there are three possible cases.
|
|
|
|
// 1. There's another piece that should be placed into this slot
|
|
|
|
// 2. This piece should be placed into another slot.
|
|
|
|
// 3. There's another piece that should be placed into this slot
|
|
|
|
// and this piece should be placed into another slot
|
|
|
|
|
|
|
|
// swap piece_index with this slot
|
|
|
|
|
|
|
|
// case 1
|
|
|
|
if (this_should_move && !other_should_move)
|
|
|
|
{
|
|
|
|
assert(piece_index != m_current_slot);
|
|
|
|
|
|
|
|
const int other_slot = piece_index;
|
|
|
|
assert(other_slot >= 0);
|
|
|
|
int other_piece = m_slot_to_piece[other_slot];
|
|
|
|
|
|
|
|
m_slot_to_piece[other_slot] = piece_index;
|
|
|
|
m_slot_to_piece[m_current_slot] = other_piece;
|
|
|
|
m_piece_to_slot[piece_index] = piece_index;
|
|
|
|
if (other_piece >= 0) m_piece_to_slot[other_piece] = m_current_slot;
|
|
|
|
|
|
|
|
if (other_piece == unassigned)
|
|
|
|
{
|
|
|
|
std::vector<int>::iterator i =
|
|
|
|
std::find(m_free_slots.begin(), m_free_slots.end(), other_slot);
|
|
|
|
assert(i != m_free_slots.end());
|
|
|
|
m_free_slots.erase(i);
|
|
|
|
m_free_slots.push_back(m_current_slot);
|
2004-01-27 18:48:58 +01:00
|
|
|
}
|
2005-10-13 09:59:05 +02:00
|
|
|
|
|
|
|
const int slot1_size = static_cast<int>(m_info.piece_size(piece_index));
|
|
|
|
const int slot2_size = other_piece >= 0 ? static_cast<int>(m_info.piece_size(other_piece)) : 0;
|
|
|
|
std::vector<char> buf1(slot1_size);
|
|
|
|
m_storage.read(&buf1[0], m_current_slot, 0, slot1_size);
|
|
|
|
if (slot2_size > 0)
|
2004-01-27 18:48:58 +01:00
|
|
|
{
|
2005-10-13 09:59:05 +02:00
|
|
|
std::vector<char> buf2(slot2_size);
|
|
|
|
m_storage.read(&buf2[0], piece_index, 0, slot2_size);
|
|
|
|
m_storage.write(&buf2[0], m_current_slot, 0, slot2_size);
|
|
|
|
}
|
|
|
|
m_storage.write(&buf1[0], piece_index, 0, slot1_size);
|
|
|
|
assert(m_slot_to_piece[m_current_slot] == unassigned
|
|
|
|
|| m_piece_to_slot[m_slot_to_piece[m_current_slot]] == m_current_slot);
|
|
|
|
}
|
|
|
|
// case 2
|
|
|
|
else if (!this_should_move && other_should_move)
|
|
|
|
{
|
|
|
|
assert(piece_index != m_current_slot);
|
2004-01-27 00:38:08 +01:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
const int other_piece = m_current_slot;
|
|
|
|
const int other_slot = m_piece_to_slot[other_piece];
|
|
|
|
assert(other_slot >= 0);
|
|
|
|
|
|
|
|
m_slot_to_piece[m_current_slot] = other_piece;
|
|
|
|
m_slot_to_piece[other_slot] = piece_index;
|
|
|
|
m_piece_to_slot[other_piece] = m_current_slot;
|
|
|
|
if (piece_index >= 0) m_piece_to_slot[piece_index] = other_slot;
|
|
|
|
|
|
|
|
if (piece_index == unassigned)
|
|
|
|
{
|
|
|
|
m_free_slots.push_back(other_slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
const int slot1_size = static_cast<int>(m_info.piece_size(other_piece));
|
|
|
|
const int slot2_size = piece_index >= 0 ? static_cast<int>(m_info.piece_size(piece_index)) : 0;
|
|
|
|
std::vector<char> buf1(slot1_size);
|
|
|
|
m_storage.read(&buf1[0], other_slot, 0, slot1_size);
|
|
|
|
if (slot2_size > 0)
|
|
|
|
{
|
|
|
|
std::vector<char> buf2(slot2_size);
|
|
|
|
m_storage.read(&buf2[0], m_current_slot, 0, slot2_size);
|
|
|
|
m_storage.write(&buf2[0], other_slot, 0, slot2_size);
|
2004-01-27 18:48:58 +01:00
|
|
|
}
|
2005-10-13 09:59:05 +02:00
|
|
|
m_storage.write(&buf1[0], m_current_slot, 0, slot1_size);
|
|
|
|
assert(m_slot_to_piece[m_current_slot] == unassigned
|
|
|
|
|| m_piece_to_slot[m_slot_to_piece[m_current_slot]] == m_current_slot);
|
|
|
|
}
|
|
|
|
else if (this_should_move && other_should_move)
|
|
|
|
{
|
|
|
|
assert(piece_index != m_current_slot);
|
|
|
|
assert(piece_index >= 0);
|
|
|
|
|
|
|
|
const int piece1 = m_slot_to_piece[piece_index];
|
|
|
|
const int piece2 = m_current_slot;
|
|
|
|
const int slot1 = piece_index;
|
|
|
|
const int slot2 = m_piece_to_slot[piece2];
|
|
|
|
|
|
|
|
assert(slot1 >= 0);
|
|
|
|
assert(slot2 >= 0);
|
|
|
|
assert(piece2 >= 0);
|
|
|
|
|
|
|
|
if (slot1 == slot2)
|
2004-01-27 18:48:58 +01:00
|
|
|
{
|
2005-10-13 09:59:05 +02:00
|
|
|
// this means there are only two pieces involved in the swap
|
|
|
|
assert(piece1 >= 0);
|
2005-09-11 11:48:05 +02:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
// movement diagram:
|
|
|
|
// +-------------------------------+
|
|
|
|
// | |
|
|
|
|
// +--> slot1 --> m_current_slot --+
|
2005-09-11 11:48:05 +02:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
m_slot_to_piece[slot1] = piece_index;
|
|
|
|
m_slot_to_piece[m_current_slot] = piece1;
|
2005-09-11 11:48:05 +02:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
m_piece_to_slot[piece_index] = slot1;
|
|
|
|
m_piece_to_slot[piece1] = m_current_slot;
|
2005-09-11 11:48:05 +02:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
assert(piece1 == m_current_slot);
|
|
|
|
assert(piece_index == slot1);
|
2005-09-11 11:48:05 +02:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
const int slot1_size = static_cast<int>(m_info.piece_size(piece1));
|
|
|
|
const int slot3_size = static_cast<int>(m_info.piece_size(piece_index));
|
|
|
|
std::vector<char> buf1(static_cast<int>(slot1_size));
|
|
|
|
std::vector<char> buf2(static_cast<int>(slot3_size));
|
2005-09-11 11:48:05 +02:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
m_storage.read(&buf2[0], m_current_slot, 0, slot3_size);
|
|
|
|
m_storage.read(&buf1[0], slot1, 0, slot1_size);
|
|
|
|
m_storage.write(&buf1[0], m_current_slot, 0, slot1_size);
|
|
|
|
m_storage.write(&buf2[0], slot1, 0, slot3_size);
|
2005-09-11 11:48:05 +02:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
assert(m_slot_to_piece[m_current_slot] == unassigned
|
|
|
|
|| m_piece_to_slot[m_slot_to_piece[m_current_slot]] == m_current_slot);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-09-11 11:48:05 +02:00
|
|
|
assert(slot1 != slot2);
|
|
|
|
assert(piece1 != piece2);
|
2004-01-26 02:45:30 +01:00
|
|
|
|
2004-01-27 18:48:58 +01:00
|
|
|
// movement diagram:
|
2005-10-13 09:59:05 +02:00
|
|
|
// +-----------------------------------------+
|
|
|
|
// | |
|
|
|
|
// +--> slot1 --> slot2 --> m_current_slot --+
|
2004-01-27 18:48:58 +01:00
|
|
|
|
|
|
|
m_slot_to_piece[slot1] = piece_index;
|
|
|
|
m_slot_to_piece[slot2] = piece1;
|
2005-10-13 09:59:05 +02:00
|
|
|
m_slot_to_piece[m_current_slot] = piece2;
|
2004-01-27 18:48:58 +01:00
|
|
|
|
|
|
|
m_piece_to_slot[piece_index] = slot1;
|
2005-10-13 09:59:05 +02:00
|
|
|
m_piece_to_slot[m_current_slot] = piece2;
|
2004-01-27 18:48:58 +01:00
|
|
|
if (piece1 >= 0) m_piece_to_slot[piece1] = slot2;
|
|
|
|
|
|
|
|
if (piece1 == unassigned)
|
|
|
|
{
|
|
|
|
std::vector<int>::iterator i =
|
|
|
|
std::find(m_free_slots.begin(), m_free_slots.end(), slot1);
|
|
|
|
assert(i != m_free_slots.end());
|
|
|
|
m_free_slots.erase(i);
|
|
|
|
m_free_slots.push_back(slot2);
|
2004-01-27 00:38:08 +01:00
|
|
|
}
|
2004-01-27 18:48:58 +01:00
|
|
|
|
2004-03-07 21:50:56 +01:00
|
|
|
const int slot1_size = piece1 >= 0 ? static_cast<int>(m_info.piece_size(piece1)) : 0;
|
|
|
|
const int slot2_size = static_cast<int>(m_info.piece_size(piece2));
|
|
|
|
const int slot3_size = static_cast<int>(m_info.piece_size(piece_index));
|
2004-01-27 18:48:58 +01:00
|
|
|
|
2004-03-07 21:50:56 +01:00
|
|
|
std::vector<char> buf1(static_cast<int>(m_info.piece_length()));
|
|
|
|
std::vector<char> buf2(static_cast<int>(m_info.piece_length()));
|
2004-01-27 18:48:58 +01:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
m_storage.read(&buf2[0], m_current_slot, 0, slot3_size);
|
2004-01-27 18:48:58 +01:00
|
|
|
m_storage.read(&buf1[0], slot2, 0, slot2_size);
|
2005-10-13 09:59:05 +02:00
|
|
|
m_storage.write(&buf1[0], m_current_slot, 0, slot2_size);
|
2004-01-27 18:48:58 +01:00
|
|
|
if (slot1_size > 0)
|
|
|
|
{
|
|
|
|
m_storage.read(&buf1[0], slot1, 0, slot1_size);
|
|
|
|
m_storage.write(&buf1[0], slot2, 0, slot1_size);
|
|
|
|
}
|
|
|
|
m_storage.write(&buf2[0], slot1, 0, slot3_size);
|
2005-10-13 09:59:05 +02:00
|
|
|
assert(m_slot_to_piece[m_current_slot] == unassigned
|
|
|
|
|| m_piece_to_slot[m_slot_to_piece[m_current_slot]] == m_current_slot);
|
2004-01-25 19:18:36 +01:00
|
|
|
}
|
2005-10-13 09:59:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(m_piece_to_slot[m_current_slot] == has_no_slot || piece_index != m_current_slot);
|
|
|
|
assert(m_slot_to_piece[m_current_slot] == unallocated);
|
|
|
|
assert(piece_index == unassigned || m_piece_to_slot[piece_index] == has_no_slot);
|
2004-01-27 18:48:58 +01:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
// the slot was identified as piece 'piece_index'
|
|
|
|
if (piece_index != unassigned)
|
|
|
|
m_piece_to_slot[piece_index] = m_current_slot;
|
|
|
|
else
|
|
|
|
m_free_slots.push_back(m_current_slot);
|
2004-01-27 18:48:58 +01:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
m_slot_to_piece[m_current_slot] = piece_index;
|
2004-01-27 18:48:58 +01:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
assert(m_slot_to_piece[m_current_slot] == unassigned
|
|
|
|
|| m_piece_to_slot[m_slot_to_piece[m_current_slot]] == m_current_slot);
|
2004-01-25 19:18:36 +01:00
|
|
|
}
|
2005-10-13 09:59:05 +02:00
|
|
|
}
|
|
|
|
catch (file_error&)
|
|
|
|
{
|
|
|
|
// find the file that failed, and skip all the blocks in that file
|
|
|
|
size_type file_offset = 0;
|
|
|
|
size_type current_offset = m_current_slot * m_info.piece_length();
|
|
|
|
for (torrent_info::file_iterator i = m_info.begin_files();
|
2004-03-24 23:50:07 +01:00
|
|
|
i != m_info.end_files(); ++i)
|
2005-10-13 09:59:05 +02:00
|
|
|
{
|
|
|
|
file_offset += i->size;
|
|
|
|
if (file_offset > current_offset) break;
|
|
|
|
}
|
2004-03-24 23:50:07 +01:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
assert(file_offset > current_offset);
|
|
|
|
int skip_blocks = static_cast<int>(
|
2004-03-28 19:45:37 +02:00
|
|
|
(file_offset - current_offset + m_info.piece_length() - 1)
|
|
|
|
/ m_info.piece_length());
|
2004-03-24 23:50:07 +01:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
for (int i = m_current_slot; i < m_current_slot + skip_blocks; ++i)
|
2004-01-25 19:18:36 +01:00
|
|
|
{
|
2005-10-13 09:59:05 +02:00
|
|
|
assert(m_slot_to_piece[i] == unallocated);
|
|
|
|
m_unallocated_slots.push_back(i);
|
2004-01-25 19:18:36 +01:00
|
|
|
}
|
2005-10-13 09:59:05 +02:00
|
|
|
|
|
|
|
// current slot will increase by one at the end of the for-loop too
|
|
|
|
m_current_slot += skip_blocks - 1;
|
2004-01-25 19:18:36 +01:00
|
|
|
}
|
2005-10-13 09:59:05 +02:00
|
|
|
++m_current_slot;
|
2005-05-13 02:39:39 +02:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
if (m_current_slot >= m_info.num_pieces())
|
|
|
|
{
|
|
|
|
assert(m_current_slot == m_info.num_pieces());
|
|
|
|
|
|
|
|
// clear the memory we've been using
|
|
|
|
std::vector<char>().swap(m_piece_data);
|
|
|
|
std::multimap<sha1_hash, int>().swap(m_hash_to_piece);
|
|
|
|
m_state = state_allocating;
|
2006-04-25 23:04:48 +02:00
|
|
|
assert(num_pieces == std::count(pieces.begin(), pieces.end(), true));
|
2005-10-13 09:59:05 +02:00
|
|
|
return std::make_pair(false, 1.f);
|
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
assert(num_pieces == std::count(pieces.begin(), pieces.end(), true));
|
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
return std::make_pair(false, (float)m_current_slot / m_info.num_pieces());
|
2004-01-25 19:18:36 +01:00
|
|
|
}
|
2003-12-07 02:26:57 +01:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
bool piece_manager::check_fastresume(
|
2006-10-11 16:02:21 +02:00
|
|
|
aux::piece_checker_data& d, std::vector<bool>& pieces
|
2006-04-25 23:04:48 +02:00
|
|
|
, int& num_pieces, bool compact_mode)
|
2003-12-09 09:49:49 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
return m_pimpl->check_fastresume(d, pieces, num_pieces, compact_mode);
|
2003-12-09 09:49:49 +01:00
|
|
|
}
|
2005-10-13 09:59:05 +02:00
|
|
|
|
|
|
|
std::pair<bool, float> piece_manager::check_files(
|
2006-04-25 23:04:48 +02:00
|
|
|
std::vector<bool>& pieces
|
|
|
|
, int& num_pieces)
|
2005-10-13 09:59:05 +02:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
return m_pimpl->check_files(pieces, num_pieces);
|
2005-10-13 09:59:05 +02:00
|
|
|
}
|
|
|
|
|
2004-01-24 18:14:03 +01:00
|
|
|
int piece_manager::impl::allocate_slot_for_piece(int piece_index)
|
2003-12-07 02:26:57 +01:00
|
|
|
{
|
|
|
|
// synchronization ------------------------------------------------------
|
|
|
|
boost::recursive_mutex::scoped_lock lock(m_mutex);
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
// INVARIANT_CHECK;
|
2003-12-10 01:24:16 +01:00
|
|
|
|
2004-01-25 13:37:15 +01:00
|
|
|
assert(piece_index >= 0);
|
2005-10-13 09:59:05 +02:00
|
|
|
assert(piece_index < (int)m_piece_to_slot.size());
|
2003-12-07 02:26:57 +01:00
|
|
|
assert(m_piece_to_slot.size() == m_slot_to_piece.size());
|
|
|
|
|
|
|
|
int slot_index = m_piece_to_slot[piece_index];
|
|
|
|
|
2004-01-16 17:19:27 +01:00
|
|
|
if (slot_index != has_no_slot)
|
2003-12-07 02:26:57 +01:00
|
|
|
{
|
2004-01-25 13:37:15 +01:00
|
|
|
assert(slot_index >= 0);
|
|
|
|
assert(slot_index < (int)m_slot_to_piece.size());
|
2003-12-07 02:26:57 +01:00
|
|
|
return slot_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_free_slots.empty())
|
|
|
|
{
|
2004-01-26 11:29:00 +01:00
|
|
|
allocate_slots(1);
|
2003-12-07 02:26:57 +01:00
|
|
|
assert(!m_free_slots.empty());
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2003-12-07 02:26:57 +01:00
|
|
|
|
2003-12-07 06:53:04 +01:00
|
|
|
std::vector<int>::iterator iter(
|
2003-12-07 02:26:57 +01:00
|
|
|
std::find(
|
|
|
|
m_free_slots.begin()
|
2003-12-07 06:53:04 +01:00
|
|
|
, m_free_slots.end()
|
|
|
|
, piece_index));
|
2003-12-07 02:26:57 +01:00
|
|
|
|
|
|
|
if (iter == m_free_slots.end())
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2004-01-16 17:19:27 +01:00
|
|
|
assert(m_slot_to_piece[piece_index] != unassigned);
|
2004-04-03 00:21:20 +02:00
|
|
|
assert(!m_free_slots.empty());
|
2003-12-07 02:26:57 +01:00
|
|
|
iter = m_free_slots.end() - 1;
|
|
|
|
|
|
|
|
// special case to make sure we don't use the last slot
|
|
|
|
// when we shouldn't, since it's smaller than ordinary slots
|
|
|
|
if (*iter == m_info.num_pieces() - 1 && piece_index != *iter)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2003-12-07 02:26:57 +01:00
|
|
|
if (m_free_slots.size() == 1)
|
2004-01-26 11:29:00 +01:00
|
|
|
allocate_slots(1);
|
2003-12-07 02:26:57 +01:00
|
|
|
assert(m_free_slots.size() > 1);
|
2004-01-26 11:29:00 +01:00
|
|
|
// assumes that all allocated slots
|
2003-12-07 02:26:57 +01:00
|
|
|
// are put at the end of the free_slots vector
|
|
|
|
iter = m_free_slots.end() - 1;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2003-12-07 02:26:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
slot_index = *iter;
|
2003-12-07 06:53:04 +01:00
|
|
|
m_free_slots.erase(iter);
|
2003-12-07 02:26:57 +01:00
|
|
|
|
2004-01-16 17:19:27 +01:00
|
|
|
assert(m_slot_to_piece[slot_index] == unassigned);
|
2003-12-07 02:26:57 +01:00
|
|
|
|
|
|
|
m_slot_to_piece[slot_index] = piece_index;
|
|
|
|
m_piece_to_slot[piece_index] = slot_index;
|
2005-10-13 09:59:05 +02:00
|
|
|
|
2003-12-07 02:26:57 +01:00
|
|
|
// there is another piece already assigned to
|
|
|
|
// the slot we are interested in, swap positions
|
2003-12-07 15:12:14 +01:00
|
|
|
if (slot_index != piece_index
|
|
|
|
&& m_slot_to_piece[piece_index] >= 0)
|
2003-12-07 02:26:57 +01:00
|
|
|
{
|
2004-01-25 19:18:36 +01:00
|
|
|
|
|
|
|
#if !defined(NDEBUG) && defined(TORRENT_STORAGE_DEBUG)
|
2003-12-07 15:12:14 +01:00
|
|
|
std::stringstream s;
|
|
|
|
|
|
|
|
s << "there is another piece at our slot, swapping..";
|
|
|
|
|
|
|
|
s << "\n piece_index: ";
|
|
|
|
s << piece_index;
|
|
|
|
s << "\n slot_index: ";
|
|
|
|
s << slot_index;
|
|
|
|
s << "\n piece at our slot: ";
|
|
|
|
s << m_slot_to_piece[piece_index];
|
|
|
|
s << "\n";
|
2004-01-25 19:18:36 +01:00
|
|
|
|
2003-12-07 15:12:14 +01:00
|
|
|
print_to_log(s.str());
|
|
|
|
debug_log();
|
2004-01-24 18:14:03 +01:00
|
|
|
#endif
|
2004-01-25 19:18:36 +01:00
|
|
|
|
|
|
|
int piece_at_our_slot = m_slot_to_piece[piece_index];
|
|
|
|
assert(m_piece_to_slot[piece_at_our_slot] == piece_index);
|
|
|
|
|
2003-12-07 02:26:57 +01:00
|
|
|
std::swap(
|
|
|
|
m_slot_to_piece[piece_index]
|
|
|
|
, m_slot_to_piece[slot_index]);
|
2003-10-30 00:28:09 +01:00
|
|
|
|
2003-12-07 02:26:57 +01:00
|
|
|
std::swap(
|
|
|
|
m_piece_to_slot[piece_index]
|
2003-12-07 15:19:04 +01:00
|
|
|
, m_piece_to_slot[piece_at_our_slot]);
|
2003-12-07 02:26:57 +01:00
|
|
|
|
2004-03-07 21:50:56 +01:00
|
|
|
const int slot_size = static_cast<int>(m_info.piece_size(slot_index));
|
2004-01-27 00:38:08 +01:00
|
|
|
std::vector<char> buf(slot_size);
|
|
|
|
m_storage.read(&buf[0], piece_index, 0, slot_size);
|
|
|
|
m_storage.write(&buf[0], slot_index, 0, slot_size);
|
2004-01-17 21:04:19 +01:00
|
|
|
|
2004-01-16 16:07:01 +01:00
|
|
|
assert(m_slot_to_piece[piece_index] == piece_index);
|
|
|
|
assert(m_piece_to_slot[piece_index] == piece_index);
|
|
|
|
|
2003-12-07 02:26:57 +01:00
|
|
|
slot_index = piece_index;
|
2004-01-25 19:18:36 +01:00
|
|
|
|
2004-01-24 18:14:03 +01:00
|
|
|
#if !defined(NDEBUG) && defined(TORRENT_STORAGE_DEBUG)
|
2003-12-07 15:12:14 +01:00
|
|
|
debug_log();
|
2004-01-17 21:04:19 +01:00
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2003-12-07 02:26:57 +01:00
|
|
|
|
2004-01-25 13:37:15 +01:00
|
|
|
assert(slot_index >= 0);
|
2005-10-13 09:59:05 +02:00
|
|
|
assert(slot_index < (int)m_slot_to_piece.size());
|
2003-12-07 02:26:57 +01:00
|
|
|
return slot_index;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2003-12-09 09:55:03 +01:00
|
|
|
|
2004-01-25 22:37:19 +01:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
// this is used to notify potential other
|
|
|
|
// threads that the allocation-function has exited
|
2004-01-26 11:29:00 +01:00
|
|
|
struct allocation_syncronization
|
2004-01-25 22:37:19 +01:00
|
|
|
{
|
2004-01-26 11:29:00 +01:00
|
|
|
allocation_syncronization(
|
|
|
|
bool& flag
|
|
|
|
, boost::condition& cond
|
|
|
|
, boost::mutex& monitor)
|
2004-01-25 22:37:19 +01:00
|
|
|
: m_flag(flag)
|
|
|
|
, m_cond(cond)
|
2004-01-26 11:29:00 +01:00
|
|
|
, m_monitor(monitor)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_monitor);
|
|
|
|
|
|
|
|
while (m_flag)
|
|
|
|
m_cond.wait(lock);
|
|
|
|
|
|
|
|
m_flag = true;
|
|
|
|
}
|
2004-01-25 22:37:19 +01:00
|
|
|
|
2004-01-26 11:29:00 +01:00
|
|
|
~allocation_syncronization()
|
2004-01-25 22:37:19 +01:00
|
|
|
{
|
2004-01-26 11:29:00 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_monitor);
|
2004-01-25 22:37:19 +01:00
|
|
|
m_flag = false;
|
|
|
|
m_cond.notify_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool& m_flag;
|
|
|
|
boost::condition& m_cond;
|
2004-01-26 11:29:00 +01:00
|
|
|
boost::mutex& m_monitor;
|
2004-01-25 22:37:19 +01:00
|
|
|
};
|
2005-10-13 09:59:05 +02:00
|
|
|
|
2004-01-25 22:37:19 +01:00
|
|
|
}
|
|
|
|
|
2003-12-09 09:49:49 +01:00
|
|
|
void piece_manager::impl::allocate_slots(int num_slots)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2004-01-25 19:18:36 +01:00
|
|
|
assert(num_slots > 0);
|
2004-01-24 20:19:17 +01:00
|
|
|
|
2004-01-26 11:29:00 +01:00
|
|
|
// this object will syncronize the allocation with
|
|
|
|
// potential other threads
|
|
|
|
allocation_syncronization sync_obj(
|
2006-04-25 23:04:48 +02:00
|
|
|
m_allocating
|
|
|
|
, m_allocating_condition
|
|
|
|
, m_allocating_monitor);
|
2004-01-25 22:37:19 +01:00
|
|
|
|
2003-12-07 02:26:57 +01:00
|
|
|
// synchronization ------------------------------------------------------
|
|
|
|
boost::recursive_mutex::scoped_lock lock(m_mutex);
|
|
|
|
// ----------------------------------------------------------------------
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2005-10-13 09:59:05 +02:00
|
|
|
// INVARIANT_CHECK;
|
2003-12-10 01:24:16 +01:00
|
|
|
|
2004-04-03 00:21:20 +02:00
|
|
|
assert(!m_unallocated_slots.empty());
|
2005-10-13 09:59:05 +02:00
|
|
|
|
2004-03-07 21:50:56 +01:00
|
|
|
const int piece_size = static_cast<int>(m_info.piece_length());
|
2003-12-07 02:26:57 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
std::vector<char>& buffer = m_scratch_buffer;
|
|
|
|
buffer.resize(piece_size);
|
2003-12-08 09:55:24 +01:00
|
|
|
|
2005-05-13 02:39:39 +02:00
|
|
|
for (int i = 0; i < num_slots && !m_unallocated_slots.empty(); ++i)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2004-01-25 22:37:19 +01:00
|
|
|
int pos = m_unallocated_slots.front();
|
2005-10-13 09:59:05 +02:00
|
|
|
// int piece_pos = pos;
|
2005-05-13 02:39:39 +02:00
|
|
|
bool write_back = false;
|
2003-12-07 02:26:57 +01:00
|
|
|
|
|
|
|
int new_free_slot = pos;
|
2004-01-16 17:19:27 +01:00
|
|
|
if (m_piece_to_slot[pos] != has_no_slot)
|
2003-12-07 02:26:57 +01:00
|
|
|
{
|
|
|
|
assert(m_piece_to_slot[pos] >= 0);
|
2005-05-13 02:39:39 +02:00
|
|
|
m_storage.read(&buffer[0], m_piece_to_slot[pos], 0, static_cast<int>(m_info.piece_size(pos)));
|
2003-12-07 02:26:57 +01:00
|
|
|
new_free_slot = m_piece_to_slot[pos];
|
|
|
|
m_slot_to_piece[pos] = pos;
|
|
|
|
m_piece_to_slot[pos] = pos;
|
2005-05-13 02:39:39 +02:00
|
|
|
write_back = true;
|
2003-12-07 02:26:57 +01:00
|
|
|
}
|
2004-05-10 08:12:29 +02:00
|
|
|
m_unallocated_slots.erase(m_unallocated_slots.begin());
|
|
|
|
m_slot_to_piece[new_free_slot] = unassigned;
|
|
|
|
m_free_slots.push_back(new_free_slot);
|
2003-12-07 02:26:57 +01:00
|
|
|
|
2005-05-13 02:39:39 +02:00
|
|
|
if (write_back || m_fill_mode)
|
|
|
|
m_storage.write(&buffer[0], pos, 0, static_cast<int>(m_info.piece_size(pos)));
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2004-01-25 22:37:19 +01:00
|
|
|
assert(m_free_slots.size() > 0);
|
2003-12-07 02:26:57 +01:00
|
|
|
}
|
2003-12-09 09:49:49 +01:00
|
|
|
|
|
|
|
void piece_manager::allocate_slots(int num_slots)
|
|
|
|
{
|
|
|
|
m_pimpl->allocate_slots(num_slots);
|
|
|
|
}
|
|
|
|
|
2004-10-10 02:42:48 +02:00
|
|
|
path const& piece_manager::save_path() const
|
2003-12-09 09:49:49 +01:00
|
|
|
{
|
|
|
|
return m_pimpl->save_path();
|
|
|
|
}
|
2004-01-17 21:04:19 +01:00
|
|
|
|
2004-10-10 02:42:48 +02:00
|
|
|
bool piece_manager::move_storage(path const& save_path)
|
2004-07-18 02:39:58 +02:00
|
|
|
{
|
|
|
|
return m_pimpl->move_storage(save_path);
|
|
|
|
}
|
|
|
|
|
2004-01-17 21:04:19 +01:00
|
|
|
#ifndef NDEBUG
|
2003-12-09 09:49:49 +01:00
|
|
|
void piece_manager::impl::check_invariant() const
|
2003-12-07 02:26:57 +01:00
|
|
|
{
|
|
|
|
// synchronization ------------------------------------------------------
|
|
|
|
boost::recursive_mutex::scoped_lock lock(m_mutex);
|
|
|
|
// ----------------------------------------------------------------------
|
2004-01-24 20:19:17 +01:00
|
|
|
if (m_piece_to_slot.empty()) return;
|
|
|
|
|
2004-01-26 11:29:00 +01:00
|
|
|
assert((int)m_piece_to_slot.size() == m_info.num_pieces());
|
|
|
|
assert((int)m_slot_to_piece.size() == m_info.num_pieces());
|
2004-01-24 20:19:17 +01:00
|
|
|
|
2004-01-25 22:37:19 +01:00
|
|
|
for (std::vector<int>::const_iterator i = m_free_slots.begin();
|
2005-05-13 02:39:39 +02:00
|
|
|
i != m_free_slots.end(); ++i)
|
2004-01-24 20:19:17 +01:00
|
|
|
{
|
2004-01-25 22:37:19 +01:00
|
|
|
assert(*i < (int)m_slot_to_piece.size());
|
|
|
|
assert(*i >= 0);
|
|
|
|
assert(m_slot_to_piece[*i] == unassigned);
|
2005-08-12 14:40:58 +02:00
|
|
|
assert(std::find(i+1, m_free_slots.end(), *i)
|
|
|
|
== m_free_slots.end());
|
2004-01-24 20:19:17 +01:00
|
|
|
}
|
|
|
|
|
2004-01-25 22:37:19 +01:00
|
|
|
for (std::vector<int>::const_iterator i = m_unallocated_slots.begin();
|
2005-05-13 02:39:39 +02:00
|
|
|
i != m_unallocated_slots.end(); ++i)
|
2004-01-24 20:19:17 +01:00
|
|
|
{
|
2004-01-25 22:37:19 +01:00
|
|
|
assert(*i < (int)m_slot_to_piece.size());
|
|
|
|
assert(*i >= 0);
|
|
|
|
assert(m_slot_to_piece[*i] == unallocated);
|
2005-08-12 14:40:58 +02:00
|
|
|
assert(std::find(i+1, m_unallocated_slots.end(), *i)
|
|
|
|
== m_unallocated_slots.end());
|
2004-01-24 20:19:17 +01:00
|
|
|
}
|
2003-12-01 23:09:58 +01:00
|
|
|
|
2003-12-07 02:26:57 +01:00
|
|
|
for (int i = 0; i < m_info.num_pieces(); ++i)
|
2003-12-07 06:53:04 +01:00
|
|
|
{
|
2004-01-25 03:59:27 +01:00
|
|
|
// Check domain of piece_to_slot's elements
|
2004-01-25 13:37:15 +01:00
|
|
|
if (m_piece_to_slot[i] != has_no_slot)
|
|
|
|
{
|
|
|
|
assert(m_piece_to_slot[i] >= 0);
|
|
|
|
assert(m_piece_to_slot[i] < (int)m_slot_to_piece.size());
|
|
|
|
}
|
2004-01-24 20:19:17 +01:00
|
|
|
|
2004-01-25 03:59:27 +01:00
|
|
|
// Check domain of slot_to_piece's elements
|
2004-01-25 13:37:15 +01:00
|
|
|
if (m_slot_to_piece[i] != unallocated
|
|
|
|
&& m_slot_to_piece[i] != unassigned)
|
|
|
|
{
|
|
|
|
assert(m_slot_to_piece[i] >= 0);
|
|
|
|
assert(m_slot_to_piece[i] < (int)m_piece_to_slot.size());
|
|
|
|
}
|
2004-01-24 20:19:17 +01:00
|
|
|
|
|
|
|
// do more detailed checks on piece_to_slot
|
2004-01-25 13:37:15 +01:00
|
|
|
if (m_piece_to_slot[i] >= 0)
|
2004-01-24 20:19:17 +01:00
|
|
|
{
|
2004-01-27 00:38:08 +01:00
|
|
|
assert(m_slot_to_piece[m_piece_to_slot[i]] == i);
|
2004-01-24 20:19:17 +01:00
|
|
|
if (m_piece_to_slot[i] != i)
|
|
|
|
{
|
|
|
|
assert(m_slot_to_piece[i] == unallocated);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-01-27 00:38:08 +01:00
|
|
|
assert(m_piece_to_slot[i] == has_no_slot);
|
2004-01-24 20:19:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// do more detailed checks on slot_to_piece
|
2003-12-10 01:24:16 +01:00
|
|
|
|
2004-01-25 22:37:19 +01:00
|
|
|
if (m_slot_to_piece[i] >= 0)
|
2004-01-16 17:19:27 +01:00
|
|
|
{
|
2004-01-25 13:37:15 +01:00
|
|
|
assert(m_slot_to_piece[i] < (int)m_piece_to_slot.size());
|
|
|
|
assert(m_piece_to_slot[m_slot_to_piece[i]] == i);
|
2004-01-24 18:14:03 +01:00
|
|
|
#ifdef TORRENT_STORAGE_DEBUG
|
|
|
|
assert(
|
2004-01-16 17:19:27 +01:00
|
|
|
std::find(
|
|
|
|
m_unallocated_slots.begin()
|
|
|
|
, m_unallocated_slots.end()
|
|
|
|
, i) == m_unallocated_slots.end()
|
|
|
|
);
|
|
|
|
assert(
|
|
|
|
std::find(
|
|
|
|
m_free_slots.begin()
|
|
|
|
, m_free_slots.end()
|
|
|
|
, i) == m_free_slots.end()
|
|
|
|
);
|
2004-01-24 18:14:03 +01:00
|
|
|
#endif
|
|
|
|
}
|
2004-01-16 17:19:27 +01:00
|
|
|
else if (m_slot_to_piece[i] == unallocated)
|
|
|
|
{
|
2004-01-24 18:14:03 +01:00
|
|
|
#ifdef TORRENT_STORAGE_DEBUG
|
2004-04-03 00:21:20 +02:00
|
|
|
assert(m_unallocated_slots.empty()
|
|
|
|
|| (std::find(
|
2004-01-16 17:19:27 +01:00
|
|
|
m_unallocated_slots.begin()
|
|
|
|
, m_unallocated_slots.end()
|
2004-04-03 00:21:20 +02:00
|
|
|
, i) != m_unallocated_slots.end())
|
2004-01-16 17:19:27 +01:00
|
|
|
);
|
2004-01-24 18:14:03 +01:00
|
|
|
#endif
|
|
|
|
}
|
2004-01-16 17:19:27 +01:00
|
|
|
else if (m_slot_to_piece[i] == unassigned)
|
2003-12-10 01:24:16 +01:00
|
|
|
{
|
2004-01-24 18:14:03 +01:00
|
|
|
#ifdef TORRENT_STORAGE_DEBUG
|
|
|
|
assert(
|
2003-12-10 01:24:16 +01:00
|
|
|
std::find(
|
|
|
|
m_free_slots.begin()
|
|
|
|
, m_free_slots.end()
|
|
|
|
, i) != m_free_slots.end()
|
|
|
|
);
|
2004-01-24 18:14:03 +01:00
|
|
|
#endif
|
|
|
|
}
|
2004-01-16 17:19:27 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(false && "m_slot_to_piece[i] is invalid");
|
|
|
|
}
|
2003-12-07 02:26:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-24 18:14:03 +01:00
|
|
|
#ifdef TORRENT_STORAGE_DEBUG
|
2003-12-09 09:49:49 +01:00
|
|
|
void piece_manager::impl::debug_log() const
|
2003-12-07 15:12:14 +01:00
|
|
|
{
|
|
|
|
std::stringstream s;
|
|
|
|
|
|
|
|
s << "index\tslot\tpiece\n";
|
|
|
|
|
|
|
|
for (int i = 0; i < m_info.num_pieces(); ++i)
|
|
|
|
{
|
|
|
|
s << i << "\t" << m_slot_to_piece[i] << "\t";
|
|
|
|
s << m_piece_to_slot[i] << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
s << "---------------------------------\n";
|
|
|
|
|
|
|
|
print_to_log(s.str());
|
|
|
|
}
|
2004-01-17 21:04:19 +01:00
|
|
|
#endif
|
2004-01-24 18:14:03 +01:00
|
|
|
#endif
|
2003-12-07 02:26:57 +01:00
|
|
|
} // namespace libtorrent
|
2003-12-01 23:09:58 +01:00
|
|
|
|