2003-10-23 01:00:57 +02:00
|
|
|
/*
|
|
|
|
|
2012-10-02 05:16:33 +02:00
|
|
|
Copyright (c) 2003-2012, Arvid Norberg
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_STORAGE_HPP_INCLUDE
|
|
|
|
#define TORRENT_STORAGE_HPP_INCLUDE
|
2003-12-10 01:39:47 +01:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
#include <vector>
|
2009-10-26 02:29:39 +01:00
|
|
|
#include <sys/types.h>
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 1)
|
|
|
|
#endif
|
|
|
|
|
2009-11-23 09:38:50 +01:00
|
|
|
#include <boost/function/function2.hpp>
|
2003-10-23 01:00:57 +02:00
|
|
|
#include <boost/limits.hpp>
|
2004-04-14 14:14:28 +02:00
|
|
|
#include <boost/shared_ptr.hpp>
|
2009-11-23 09:38:50 +01:00
|
|
|
#include <boost/scoped_ptr.hpp>
|
2007-09-01 05:00:31 +02:00
|
|
|
#include <boost/intrusive_ptr.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/torrent_info.hpp"
|
2007-05-08 13:13:13 +02:00
|
|
|
#include "libtorrent/piece_picker.hpp"
|
2007-06-10 22:46:09 +02:00
|
|
|
#include "libtorrent/intrusive_ptr_base.hpp"
|
|
|
|
#include "libtorrent/peer_request.hpp"
|
|
|
|
#include "libtorrent/hasher.hpp"
|
2005-11-01 19:30:39 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
2009-01-03 09:11:31 +01:00
|
|
|
#include "libtorrent/file.hpp"
|
2009-01-11 03:02:34 +01:00
|
|
|
#include "libtorrent/disk_buffer_holder.hpp"
|
2009-10-20 04:49:56 +02:00
|
|
|
#include "libtorrent/thread.hpp"
|
2009-11-23 09:38:50 +01:00
|
|
|
#include "libtorrent/storage_defs.hpp"
|
2011-02-22 03:53:26 +01:00
|
|
|
#include "libtorrent/allocator.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2013-08-11 20:13:21 +02:00
|
|
|
// OVERVIEW
|
|
|
|
//
|
|
|
|
// This is an example storage implementation that stores all pieces in a ``std::map``,
|
|
|
|
// i.e. in RAM. It's not necessarily very useful in practice, but illustrates the
|
|
|
|
// basics of implementing a custom storage.
|
|
|
|
//
|
|
|
|
//::
|
|
|
|
//
|
|
|
|
// struct temp_storage : storage_interface
|
|
|
|
// {
|
|
|
|
// temp_storage(file_storage const& fs) : m_files(fs) {}
|
|
|
|
// virtual bool initialize(bool allocate_files) { return false; }
|
|
|
|
// virtual bool has_any_file() { return false; }
|
|
|
|
// virtual int read(char* buf, int slot, int offset, int size)
|
|
|
|
// {
|
|
|
|
// std::map<int, std::vector<char> >::const_iterator i = m_file_data.find(slot);
|
|
|
|
// if (i == m_file_data.end()) return 0;
|
|
|
|
// int available = i->second.size() - offset;
|
|
|
|
// if (available <= 0) return 0;
|
|
|
|
// if (available > size) available = size;
|
|
|
|
// memcpy(buf, &i->second[offset], available);
|
|
|
|
// return available;
|
|
|
|
// }
|
|
|
|
// virtual int write(const char* buf, int slot, int offset, int size)
|
|
|
|
// {
|
|
|
|
// std::vector<char>& data = m_file_data[slot];
|
|
|
|
// if (data.size() < offset + size) data.resize(offset + size);
|
|
|
|
// std::memcpy(&data[offset], buf, size);
|
|
|
|
// return size;
|
|
|
|
// }
|
|
|
|
// virtual bool rename_file(int file, std::string const& new_name)
|
|
|
|
// { assert(false); return false; }
|
|
|
|
// virtual bool move_storage(std::string const& save_path) { return false; }
|
|
|
|
// virtual bool verify_resume_data(lazy_entry const& rd, error_code& error) { return false; }
|
|
|
|
// virtual bool write_resume_data(entry& rd) const { return false; }
|
|
|
|
// virtual bool move_slot(int src_slot, int dst_slot) { assert(false); return false; }
|
|
|
|
// virtual bool swap_slots(int slot1, int slot2) { assert(false); return false; }
|
|
|
|
// virtual bool swap_slots3(int slot1, int slot2, int slot3) { assert(false); return false; }
|
|
|
|
// virtual size_type physical_offset(int slot, int offset)
|
|
|
|
// { return slot * m_files.piece_length() + offset; };
|
|
|
|
// virtual sha1_hash hash_for_slot(int slot, partial_hash& ph, int piece_size)
|
|
|
|
// {
|
|
|
|
// int left = piece_size - ph.offset;
|
|
|
|
// assert(left >= 0);
|
|
|
|
// if (left > 0)
|
|
|
|
// {
|
|
|
|
// std::vector<char>& data = m_file_data[slot];
|
|
|
|
// // if there are padding files, those blocks will be considered
|
|
|
|
// // completed even though they haven't been written to the storage.
|
|
|
|
// // in this case, just extend the piece buffer to its full size
|
|
|
|
// // and fill it with zeroes.
|
|
|
|
// if (data.size() < piece_size) data.resize(piece_size, 0);
|
|
|
|
// ph.h.update(&data[ph.offset], left);
|
|
|
|
// }
|
|
|
|
// return ph.h.final();
|
|
|
|
// }
|
|
|
|
// virtual bool release_files() { return false; }
|
|
|
|
// virtual bool delete_files() { return false; }
|
|
|
|
//
|
|
|
|
// std::map<int, std::vector<char> > m_file_data;
|
|
|
|
// file_storage m_files;
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// storage_interface* temp_storage_constructor(
|
|
|
|
// file_storage const& fs, file_storage const* mapped
|
|
|
|
// , std::string const& path, file_pool& fp
|
|
|
|
// , std::vector<boost::uint8_t> const& prio)
|
|
|
|
// {
|
|
|
|
// return new temp_storage(fs);
|
|
|
|
// }
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
class session;
|
2006-11-15 22:39:58 +01:00
|
|
|
struct file_pool;
|
2007-06-10 22:46:09 +02:00
|
|
|
struct disk_io_job;
|
2009-01-21 08:31:49 +01:00
|
|
|
struct disk_buffer_pool;
|
2011-02-07 00:40:21 +01:00
|
|
|
struct session_settings;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2012-03-19 00:31:04 +01:00
|
|
|
TORRENT_EXTRA_EXPORT std::vector<std::pair<size_type, std::time_t> > get_filesizes(
|
2008-05-28 10:44:40 +02:00
|
|
|
file_storage const& t
|
2009-10-26 02:29:39 +01:00
|
|
|
, std::string const& p);
|
2004-01-17 21:04:19 +01:00
|
|
|
|
2012-03-19 00:31:04 +01:00
|
|
|
TORRENT_EXTRA_EXPORT bool match_filesizes(
|
2008-05-28 10:44:40 +02:00
|
|
|
file_storage const& t
|
2009-10-26 02:29:39 +01:00
|
|
|
, std::string const& p
|
2005-06-16 17:41:04 +02:00
|
|
|
, std::vector<std::pair<size_type, std::time_t> > const& sizes
|
2007-05-24 20:53:55 +02:00
|
|
|
, bool compact_mode
|
2005-06-16 17:41:04 +02:00
|
|
|
, std::string* error = 0);
|
2012-03-19 00:31:04 +01:00
|
|
|
/*
|
|
|
|
struct TORRENT_EXTRA_EXPORT file_allocation_failed: std::exception
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
|
|
|
file_allocation_failed(const char* error_msg): m_msg(error_msg) {}
|
|
|
|
virtual const char* what() const throw() { return m_msg.c_str(); }
|
2004-01-13 04:08:59 +01:00
|
|
|
virtual ~file_allocation_failed() throw() {}
|
2003-10-23 01:00:57 +02:00
|
|
|
std::string m_msg;
|
|
|
|
};
|
2012-03-19 00:31:04 +01:00
|
|
|
*/
|
|
|
|
struct TORRENT_EXTRA_EXPORT partial_hash
|
2007-06-10 22:46:09 +02:00
|
|
|
{
|
|
|
|
partial_hash(): offset(0) {}
|
|
|
|
// the number of bytes in the piece that has been hashed
|
|
|
|
int offset;
|
|
|
|
// the sha-1 context
|
|
|
|
hasher h;
|
|
|
|
};
|
|
|
|
|
2013-08-11 08:04:24 +02:00
|
|
|
// The storage interface is a pure virtual class that can be implemented to
|
|
|
|
// customize how and where data for a torrent is stored. The default storage
|
|
|
|
// implementation uses regular files in the filesystem, mapping the files in the
|
|
|
|
// torrent in the way one would assume a torrent is saved to disk. Implementing
|
|
|
|
// your own storage interface makes it possible to store all data in RAM, or in
|
|
|
|
// some optimized order on disk (the order the pieces are received for instance),
|
|
|
|
// or saving multifile torrents in a single file in order to be able to take
|
|
|
|
// advantage of optimized disk-I/O.
|
|
|
|
//
|
|
|
|
// It is also possible to write a thin class that uses the default storage but
|
|
|
|
// modifies some particular behavior, for instance encrypting the data before
|
|
|
|
// it's written to disk, and decrypting it when it's read again.
|
|
|
|
//
|
|
|
|
// The storage interface is based on slots, each slot is 'piece_size' number
|
|
|
|
// of bytes. All access is done by writing and reading whole or partial
|
|
|
|
// slots. One slot is one piece in the torrent, but the data in the slot
|
|
|
|
// does not necessarily correspond to the piece with the same index (in
|
|
|
|
// compact allocation mode it won't).
|
|
|
|
//
|
|
|
|
// libtorrent comes with two built-in storage implementations; ``default_storage``
|
|
|
|
// and ``disabled_storage``. Their constructor functions are called ``default_storage_constructor``
|
|
|
|
// and ``disabled_storage_constructor`` respectively. The disabled storage does
|
|
|
|
// just what it sounds like. It throws away data that's written, and it
|
|
|
|
// reads garbage. It's useful mostly for benchmarking and profiling purpose.
|
2013-08-11 20:13:21 +02:00
|
|
|
//
|
2007-03-16 06:29:23 +01:00
|
|
|
struct TORRENT_EXPORT storage_interface
|
2003-12-07 02:26:57 +01:00
|
|
|
{
|
2009-01-21 08:31:49 +01:00
|
|
|
storage_interface(): m_disk_pool(0), m_settings(0) {}
|
2013-08-11 20:13:21 +02:00
|
|
|
|
|
|
|
// This function is called when the storage is to be initialized. The default storage
|
|
|
|
// will create directories and empty files at this point. If ``allocate_files`` is true,
|
|
|
|
// it will also ``ftruncate`` all files to their target size.
|
|
|
|
//
|
|
|
|
// Returning ``true`` indicates an error occurred.
|
2008-02-14 04:48:20 +01:00
|
|
|
virtual bool initialize(bool allocate_files) = 0;
|
2007-04-19 05:06:15 +02:00
|
|
|
|
2013-08-11 20:13:21 +02:00
|
|
|
// This function is called when first checking (or re-checking) the storage for a torrent.
|
|
|
|
// It should return true if any of the files that is used in this storage exists on disk.
|
|
|
|
// If so, the storage will be checked for existing pieces before starting the download.
|
2009-04-10 09:22:27 +02:00
|
|
|
virtual bool has_any_file() = 0;
|
|
|
|
|
2013-08-11 20:13:21 +02:00
|
|
|
// These functions should read or write the data in or to the given ``slot`` at the given ``offset``.
|
|
|
|
// It should read or write ``num_bufs`` buffers sequentially, where the size of each buffer
|
|
|
|
// is specified in the buffer array ``bufs``. The file::iovec_t type has the following members::
|
|
|
|
//
|
|
|
|
// struct iovec_t
|
|
|
|
// {
|
|
|
|
// void* iov_base;
|
|
|
|
// size_t iov_len;
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// The return value is the number of bytes actually read or written, or -1 on failure. If
|
|
|
|
// it returns -1, the error code is expected to be set to
|
|
|
|
//
|
|
|
|
// Every buffer in ``bufs`` can be assumed to be page aligned and be of a page aligned size,
|
|
|
|
// except for the last buffer of the torrent. The allocated buffer can be assumed to fit a
|
|
|
|
// fully page aligned number of bytes though. This is useful when reading and writing the
|
|
|
|
// last piece of a file in unbuffered mode.
|
|
|
|
//
|
|
|
|
// The ``offset`` is aligned to 16 kiB boundries *most of the time*, but there are rare
|
|
|
|
// exceptions when it's not. Specifically if the read cache is disabled/or full and a
|
|
|
|
// client requests unaligned data, or the file itself is not aligned in the torrent.
|
|
|
|
// Most clients request aligned data.
|
2013-03-03 05:47:19 +01:00
|
|
|
virtual int readv(file::iovec_t const* bufs, int slot, int offset, int num_bufs, int flags = file::random_access);
|
|
|
|
virtual int writev(file::iovec_t const* bufs, int slot, int offset, int num_bufs, int flags = file::random_access);
|
2009-01-03 09:11:31 +01:00
|
|
|
|
2013-08-11 20:13:21 +02:00
|
|
|
// This function is called when a read job is queued. It gives the storage wrapper an
|
|
|
|
// opportunity to hint the operating system about this coming read. For instance, the
|
|
|
|
// storage may call ``posix_fadvise(POSIX_FADV_WILLNEED)`` or ``fcntl(F_RDADVISE)``.
|
2012-07-02 03:34:05 +02:00
|
|
|
virtual void hint_read(int, int, int) {}
|
2013-08-11 20:13:21 +02:00
|
|
|
|
2008-02-14 04:48:20 +01:00
|
|
|
// negative return value indicates an error
|
2008-04-05 23:18:27 +02:00
|
|
|
virtual int read(char* buf, int slot, int offset, int size) = 0;
|
2004-01-25 19:18:36 +01:00
|
|
|
|
2008-02-14 04:48:20 +01:00
|
|
|
// negative return value indicates an error
|
2008-04-05 23:18:27 +02:00
|
|
|
virtual int write(const char* buf, int slot, int offset, int size) = 0;
|
2003-12-07 02:26:57 +01:00
|
|
|
|
2009-09-05 09:21:10 +02:00
|
|
|
virtual size_type physical_offset(int slot, int offset) = 0;
|
|
|
|
|
2013-08-11 20:13:21 +02:00
|
|
|
// This function is optional. It is supposed to return the first piece, starting at
|
|
|
|
// ``start`` that is fully contained within a data-region on disk (i.e. non-sparse
|
|
|
|
// region). The purpose of this is to skip parts of files that can be known to contain
|
|
|
|
// zeros when checking files.
|
2009-02-17 01:11:38 +01:00
|
|
|
virtual int sparse_end(int start) const { return start; }
|
|
|
|
|
2013-08-11 20:13:21 +02:00
|
|
|
// This function should move all the files belonging to the storage to the new save_path.
|
|
|
|
// The default storage moves the single file or the directory of the torrent.
|
|
|
|
//
|
|
|
|
// Before moving the files, any open file handles may have to be closed, like
|
|
|
|
// ``release_files()``.
|
|
|
|
//
|
|
|
|
// returns one of:
|
|
|
|
// | no_error = 0
|
|
|
|
// | need_full_check = -1
|
|
|
|
// | fatal_disk_error = -2
|
|
|
|
// | file_exist = -4
|
2013-05-09 04:50:16 +02:00
|
|
|
virtual int move_storage(std::string const& save_path, int flags) = 0;
|
2004-07-18 02:39:58 +02:00
|
|
|
|
2013-08-11 20:13:21 +02:00
|
|
|
// This function should verify the resume data ``rd`` with the files
|
|
|
|
// on disk. If the resume data seems to be up-to-date, return true. If
|
|
|
|
// not, set ``error`` to a description of what mismatched and return false.
|
|
|
|
//
|
|
|
|
// The default storage may compare file sizes and time stamps of the files.
|
|
|
|
//
|
|
|
|
// Returning ``false`` indicates an error occurred.
|
2009-06-28 02:36:41 +02:00
|
|
|
virtual bool verify_resume_data(lazy_entry const& rd, error_code& error) = 0;
|
2007-03-28 01:49:05 +02:00
|
|
|
|
2013-08-11 20:13:21 +02:00
|
|
|
// This function should fill in resume data, the current state of the
|
|
|
|
// storage, in ``rd``. The default storage adds file timestamps and
|
|
|
|
// sizes.
|
|
|
|
//
|
|
|
|
// Returning ``true`` indicates an error occurred.
|
2008-02-14 04:48:20 +01:00
|
|
|
virtual bool write_resume_data(entry& rd) const = 0;
|
2007-05-24 20:53:55 +02:00
|
|
|
|
2013-08-11 20:13:21 +02:00
|
|
|
// This function should copy or move the data in slot ``src_slot`` to
|
|
|
|
// the slot ``dst_slot``. This is only used in compact mode.
|
|
|
|
//
|
|
|
|
// If the storage caches slots, this could be implemented more
|
|
|
|
// efficient than reading and writing the data.
|
|
|
|
//
|
|
|
|
// Returning ``true`` indicates an error occurred.
|
2008-02-14 04:48:20 +01:00
|
|
|
virtual bool move_slot(int src_slot, int dst_slot) = 0;
|
2007-04-30 03:06:29 +02:00
|
|
|
|
2013-08-11 20:13:21 +02:00
|
|
|
// This function should swap the data in ``slot1`` and ``slot2``. The default
|
|
|
|
// storage uses a scratch buffer to read the data into, then moving the other
|
|
|
|
// slot and finally writing back the temporary slot's data
|
|
|
|
//
|
|
|
|
// This is only used in compact mode.
|
|
|
|
//
|
|
|
|
// Returning ``true`` indicates an error occurred.
|
2008-02-14 04:48:20 +01:00
|
|
|
virtual bool swap_slots(int slot1, int slot2) = 0;
|
2007-05-16 03:16:08 +02:00
|
|
|
|
2013-08-11 20:13:21 +02:00
|
|
|
// This function should do a 3-way swap, or shift of the slots. ``slot1``
|
|
|
|
// should move to ``slot2``, which should be moved to ``slot3`` which in turn
|
|
|
|
// should be moved to ``slot1``.
|
|
|
|
//
|
|
|
|
// This is only used in compact mode.
|
|
|
|
//
|
|
|
|
// Returning ``true`` indicates an error occurred.
|
2008-02-14 04:48:20 +01:00
|
|
|
virtual bool swap_slots3(int slot1, int slot2, int slot3) = 0;
|
2007-05-16 03:16:08 +02:00
|
|
|
|
2013-08-11 20:13:21 +02:00
|
|
|
// This function should release all the file handles that it keeps open to files
|
|
|
|
// belonging to this storage. The default implementation just calls
|
|
|
|
// ``file_pool::release_files(this)``.
|
|
|
|
//
|
|
|
|
// Returning ``true`` indicates an error occurred.
|
2008-02-14 04:48:20 +01:00
|
|
|
virtual bool release_files() = 0;
|
2007-10-13 05:33:33 +02:00
|
|
|
|
2013-08-11 20:13:21 +02:00
|
|
|
// Rename file with index ``file`` to the thame ``new_name``. If there is an error,
|
|
|
|
// ``true`` should be returned.
|
2008-05-28 10:44:40 +02:00
|
|
|
virtual bool rename_file(int index, std::string const& new_filename) = 0;
|
|
|
|
|
2013-08-11 20:13:21 +02:00
|
|
|
// This function should delete all files and directories belonging to this storage.
|
|
|
|
//
|
|
|
|
// Returning ``true`` indicates an error occurred.
|
|
|
|
//
|
|
|
|
// The ``disk_buffer_pool`` is used to allocate and free disk buffers. It has the
|
|
|
|
// following members::
|
|
|
|
//
|
|
|
|
// struct disk_buffer_pool : boost::noncopyable
|
|
|
|
// {
|
|
|
|
// char* allocate_buffer(char const* category);
|
|
|
|
// void free_buffer(char* buf);
|
|
|
|
//
|
|
|
|
// char* allocate_buffers(int blocks, char const* category);
|
|
|
|
// void free_buffers(char* buf, int blocks);
|
|
|
|
//
|
|
|
|
// int block_size() const { return m_block_size; }
|
|
|
|
//
|
|
|
|
// void release_memory();
|
|
|
|
// };
|
2008-02-14 04:48:20 +01:00
|
|
|
virtual bool delete_files() = 0;
|
|
|
|
|
2013-02-16 09:26:55 +01:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2013-08-11 20:13:21 +02:00
|
|
|
// This function is called each time a file is completely downloaded. The
|
|
|
|
// storage implementation can perform last operations on a file. The file will
|
|
|
|
// not be opened for writing after this.
|
|
|
|
//
|
|
|
|
// ``index`` is the index of the file that completed.
|
|
|
|
//
|
|
|
|
// On windows the default storage implementation clears the sparse file flag
|
|
|
|
// on the specified file.
|
2012-07-02 03:34:05 +02:00
|
|
|
virtual void finalize_file(int) {}
|
2013-02-16 09:26:55 +01:00
|
|
|
#endif
|
2010-01-09 19:40:05 +01:00
|
|
|
|
2013-09-01 19:34:05 +02:00
|
|
|
// access global disk_buffer_pool, for allocating and freeing disk buffers
|
2009-01-21 08:31:49 +01:00
|
|
|
disk_buffer_pool* disk_pool() { return m_disk_pool; }
|
2013-09-01 19:34:05 +02:00
|
|
|
|
|
|
|
// access global session_settings
|
2009-01-21 08:31:49 +01:00
|
|
|
session_settings const& settings() const { return *m_settings; }
|
2009-01-11 03:02:34 +01:00
|
|
|
|
2013-09-01 19:34:05 +02:00
|
|
|
// called by the storage implementation to set it into an
|
|
|
|
// error state. Typically whenever a critical file operation
|
|
|
|
// fails.
|
2011-09-05 01:29:47 +02:00
|
|
|
void set_error(std::string const& file, error_code const& ec) const;
|
2008-04-13 00:08:07 +02:00
|
|
|
|
2013-09-01 19:34:05 +02:00
|
|
|
// returns the currently set error code and file path associated with it,
|
|
|
|
// if set.
|
2008-07-18 01:41:46 +02:00
|
|
|
error_code const& error() const { return m_error; }
|
2008-04-13 00:08:07 +02:00
|
|
|
std::string const& error_file() const { return m_error_file; }
|
2013-09-01 19:34:05 +02:00
|
|
|
|
|
|
|
// reset the error state to allow continuing reading and writing
|
|
|
|
// to the storage
|
2011-03-13 22:07:46 +01:00
|
|
|
virtual void clear_error() { m_error = error_code(); m_error_file.resize(0); }
|
2008-04-13 00:08:07 +02:00
|
|
|
|
2013-09-01 19:34:05 +02:00
|
|
|
// hidden
|
2008-07-18 01:41:46 +02:00
|
|
|
mutable error_code m_error;
|
2008-04-13 00:08:07 +02:00
|
|
|
mutable std::string m_error_file;
|
2007-10-13 05:33:33 +02:00
|
|
|
|
2013-08-16 07:07:09 +02:00
|
|
|
// hidden
|
2007-03-16 06:29:23 +01:00
|
|
|
virtual ~storage_interface() {}
|
2009-01-11 03:02:34 +01:00
|
|
|
|
2013-09-01 19:34:05 +02:00
|
|
|
// hidden
|
2009-01-21 08:31:49 +01:00
|
|
|
disk_buffer_pool* m_disk_pool;
|
|
|
|
session_settings* m_settings;
|
2007-03-16 06:29:23 +01:00
|
|
|
};
|
2004-12-21 13:30:09 +01:00
|
|
|
|
2013-09-01 19:34:05 +02:00
|
|
|
// The default implementation of storage_interface. Behaves as a normal bittorrent client.
|
|
|
|
// It is possible to derive from this class in order to override some of its behavior, when
|
|
|
|
// implementing a custom storage.
|
2011-10-07 02:26:19 +02:00
|
|
|
class TORRENT_EXPORT default_storage : public storage_interface, boost::noncopyable
|
2011-04-17 00:58:11 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
default_storage(file_storage const& fs, file_storage const* mapped, std::string const& path
|
|
|
|
, file_pool& fp, std::vector<boost::uint8_t> const& file_prio);
|
|
|
|
~default_storage();
|
|
|
|
|
2013-02-16 09:26:55 +01:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2011-04-17 00:58:11 +02:00
|
|
|
void finalize_file(int file);
|
2013-02-16 09:26:55 +01:00
|
|
|
#endif
|
2011-04-17 00:58:11 +02:00
|
|
|
bool has_any_file();
|
|
|
|
bool rename_file(int index, std::string const& new_filename);
|
|
|
|
bool release_files();
|
|
|
|
bool delete_files();
|
|
|
|
bool initialize(bool allocate_files);
|
2013-05-09 04:50:16 +02:00
|
|
|
int move_storage(std::string const& save_path, int flags);
|
2011-04-17 00:58:11 +02:00
|
|
|
int read(char* buf, int slot, int offset, int size);
|
|
|
|
int write(char const* buf, int slot, int offset, int size);
|
|
|
|
int sparse_end(int start) const;
|
2011-04-26 09:03:05 +02:00
|
|
|
void hint_read(int slot, int offset, int len);
|
2013-03-03 05:47:19 +01:00
|
|
|
int readv(file::iovec_t const* bufs, int slot, int offset, int num_bufs, int flags = file::random_access);
|
|
|
|
int writev(file::iovec_t const* buf, int slot, int offset, int num_bufs, int flags = file::random_access);
|
2011-04-17 00:58:11 +02:00
|
|
|
size_type physical_offset(int slot, int offset);
|
|
|
|
bool move_slot(int src_slot, int dst_slot);
|
|
|
|
bool swap_slots(int slot1, int slot2);
|
|
|
|
bool swap_slots3(int slot1, int slot2, int slot3);
|
|
|
|
bool verify_resume_data(lazy_entry const& rd, error_code& error);
|
|
|
|
bool write_resume_data(entry& rd) const;
|
|
|
|
|
|
|
|
// this identifies a read or write operation
|
|
|
|
// so that default_storage::readwritev() knows what to
|
|
|
|
// do when it's actually touching the file
|
|
|
|
struct fileop
|
|
|
|
{
|
|
|
|
size_type (file::*regular_op)(size_type file_offset
|
|
|
|
, file::iovec_t const* bufs, int num_bufs, error_code& ec);
|
|
|
|
size_type (default_storage::*unaligned_op)(boost::intrusive_ptr<file> const& f
|
|
|
|
, size_type file_offset, file::iovec_t const* bufs, int num_bufs
|
|
|
|
, error_code& ec);
|
|
|
|
int cache_setting;
|
|
|
|
int mode;
|
|
|
|
};
|
|
|
|
|
|
|
|
void delete_one_file(std::string const& p);
|
|
|
|
int readwritev(file::iovec_t const* bufs, int slot, int offset
|
|
|
|
, int num_bufs, fileop const&);
|
|
|
|
|
|
|
|
size_type read_unaligned(boost::intrusive_ptr<file> const& file_handle
|
|
|
|
, size_type file_offset, file::iovec_t const* bufs, int num_bufs, error_code& ec);
|
|
|
|
size_type write_unaligned(boost::intrusive_ptr<file> const& file_handle
|
|
|
|
, size_type file_offset, file::iovec_t const* bufs, int num_bufs, error_code& ec);
|
|
|
|
|
|
|
|
file_storage const& files() const { return m_mapped_files?*m_mapped_files:m_files; }
|
|
|
|
|
|
|
|
boost::scoped_ptr<file_storage> m_mapped_files;
|
|
|
|
file_storage const& m_files;
|
|
|
|
|
|
|
|
// helper function to open a file in the file pool with the right mode
|
2013-08-12 09:30:57 +02:00
|
|
|
boost::intrusive_ptr<file> open_file(int file, int mode
|
2011-04-17 00:58:11 +02:00
|
|
|
, error_code& ec) const;
|
|
|
|
|
|
|
|
std::vector<boost::uint8_t> m_file_priority;
|
|
|
|
std::string m_save_path;
|
|
|
|
// the file pool is typically stored in
|
|
|
|
// the session, to make all storage
|
|
|
|
// instances use the same pool
|
|
|
|
file_pool& m_pool;
|
|
|
|
|
|
|
|
int m_page_size;
|
|
|
|
bool m_allocate_files;
|
|
|
|
};
|
|
|
|
|
|
|
|
// this storage implementation does not write anything to disk
|
|
|
|
// and it pretends to read, and just leaves garbage in the buffers
|
|
|
|
// this is useful when simulating many clients on the same machine
|
|
|
|
// or when running stress tests and want to take the cost of the
|
|
|
|
// disk I/O out of the picture. This cannot be used for any kind
|
|
|
|
// of normal bittorrent operation, since it will just send garbage
|
|
|
|
// to peers and throw away all the data it downloads. It would end
|
|
|
|
// up being banned immediately
|
|
|
|
class disabled_storage : public storage_interface, boost::noncopyable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
disabled_storage(int piece_size) : m_piece_size(piece_size) {}
|
|
|
|
bool has_any_file() { return false; }
|
2012-07-02 03:34:05 +02:00
|
|
|
bool rename_file(int, std::string const&) { return false; }
|
2011-04-17 00:58:11 +02:00
|
|
|
bool release_files() { return false; }
|
|
|
|
bool delete_files() { return false; }
|
2012-07-02 03:34:05 +02:00
|
|
|
bool initialize(bool) { return false; }
|
2013-05-09 04:50:16 +02:00
|
|
|
int move_storage(std::string const&, int flags) { return 0; }
|
2012-07-02 03:34:05 +02:00
|
|
|
int read(char*, int, int, int size) { return size; }
|
|
|
|
int write(char const*, int, int, int size) { return size; }
|
|
|
|
size_type physical_offset(int, int) { return 0; }
|
2013-03-03 05:47:19 +01:00
|
|
|
int readv(file::iovec_t const* bufs, int slot, int offset, int num_bufs, int flags = file::random_access);
|
|
|
|
int writev(file::iovec_t const* bufs, int slot, int offset, int num_bufs, int flags = file::random_access);
|
2012-07-02 03:34:05 +02:00
|
|
|
bool move_slot(int, int) { return false; }
|
|
|
|
bool swap_slots(int, int) { return false; }
|
|
|
|
bool swap_slots3(int, int, int) { return false; }
|
|
|
|
bool verify_resume_data(lazy_entry const&, error_code&) { return false; }
|
|
|
|
bool write_resume_data(entry&) const { return false; }
|
2011-04-17 00:58:11 +02:00
|
|
|
|
|
|
|
int m_piece_size;
|
|
|
|
};
|
|
|
|
|
2013-05-09 04:50:16 +02:00
|
|
|
// flags for async_move_storage
|
|
|
|
enum move_flags_t
|
|
|
|
{
|
|
|
|
// replace any files in the destination when copying
|
|
|
|
// or moving the storage
|
2013-07-21 17:47:30 +02:00
|
|
|
always_replace_files,
|
2013-05-09 04:50:16 +02:00
|
|
|
|
|
|
|
// if any files that we want to copy exist in the destination
|
|
|
|
// exist, fail the whole operation and don't perform
|
|
|
|
// any copy or move. There is an inherent race condition
|
|
|
|
// in this mode. The files are checked for existence before
|
|
|
|
// the operation starts. In between the check and performing
|
|
|
|
// the copy, the destination files may be created, in which
|
|
|
|
// case they are replaced.
|
2013-07-21 17:47:30 +02:00
|
|
|
fail_if_exist,
|
2013-05-09 04:50:16 +02:00
|
|
|
|
|
|
|
// if any file exist in the target, take those files instead
|
|
|
|
// of the ones we may have in the source.
|
2013-07-21 17:47:30 +02:00
|
|
|
dont_replace,
|
2013-05-09 04:50:16 +02:00
|
|
|
};
|
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
struct disk_io_thread;
|
2007-04-18 02:36:09 +02:00
|
|
|
|
2012-03-19 00:31:04 +01:00
|
|
|
class TORRENT_EXTRA_EXPORT piece_manager
|
2007-06-10 22:46:09 +02:00
|
|
|
: public intrusive_ptr_base<piece_manager>
|
|
|
|
, boost::noncopyable
|
2003-11-26 15:11:25 +01:00
|
|
|
{
|
2007-06-10 22:46:09 +02:00
|
|
|
friend class invariant_access;
|
|
|
|
friend struct disk_io_thread;
|
2003-11-26 15:11:25 +01:00
|
|
|
public:
|
2003-12-07 02:26:57 +01:00
|
|
|
|
|
|
|
piece_manager(
|
2007-06-10 22:46:09 +02:00
|
|
|
boost::shared_ptr<void> const& torrent
|
2008-05-28 10:44:40 +02:00
|
|
|
, boost::intrusive_ptr<torrent_info const> info
|
2009-10-26 02:29:39 +01:00
|
|
|
, std::string const& path
|
2007-03-16 06:29:23 +01:00
|
|
|
, file_pool& fp
|
2007-06-10 22:46:09 +02:00
|
|
|
, disk_io_thread& io
|
2008-03-08 07:06:31 +01:00
|
|
|
, storage_constructor_type sc
|
2010-07-15 03:14:36 +02:00
|
|
|
, storage_mode_t sm
|
|
|
|
, std::vector<boost::uint8_t> const& file_prio);
|
2003-12-07 02:26:57 +01:00
|
|
|
|
2004-01-04 13:54:38 +01:00
|
|
|
~piece_manager();
|
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
boost::intrusive_ptr<torrent_info const> info() const { return m_info; }
|
2008-04-13 20:54:36 +02:00
|
|
|
void write_resume_data(entry& rd) const;
|
2007-06-10 22:46:09 +02:00
|
|
|
|
2008-07-01 01:14:31 +02:00
|
|
|
void async_check_fastresume(lazy_entry const* resume_data
|
2008-03-08 07:06:31 +01:00
|
|
|
, boost::function<void(int, disk_io_job const&)> const& handler);
|
2007-10-08 22:01:36 +02:00
|
|
|
|
2008-03-08 07:06:31 +01:00
|
|
|
void async_check_files(boost::function<void(int, disk_io_job const&)> const& handler);
|
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
void async_rename_file(int index, std::string const& name
|
|
|
|
, boost::function<void(int, disk_io_job const&)> const& handler);
|
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
void async_read(
|
|
|
|
peer_request const& r
|
2007-09-11 19:45:20 +02:00
|
|
|
, boost::function<void(int, disk_io_job const&)> const& handler
|
2010-01-31 20:14:00 +01:00
|
|
|
, int cache_line_size = 0
|
|
|
|
, int cache_expiry = 0);
|
2007-06-10 22:46:09 +02:00
|
|
|
|
2009-02-03 08:46:24 +01:00
|
|
|
void async_read_and_hash(
|
|
|
|
peer_request const& r
|
|
|
|
, boost::function<void(int, disk_io_job const&)> const& handler
|
2010-01-31 20:14:00 +01:00
|
|
|
, int cache_expiry = 0);
|
2009-02-03 08:46:24 +01:00
|
|
|
|
2010-01-15 17:45:42 +01:00
|
|
|
void async_cache(int piece
|
|
|
|
, boost::function<void(int, disk_io_job const&)> const& handler
|
2010-01-31 20:14:00 +01:00
|
|
|
, int cache_expiry = 0);
|
2010-01-15 17:45:42 +01:00
|
|
|
|
2011-03-16 08:45:51 +01:00
|
|
|
// returns the write queue size
|
|
|
|
int async_write(
|
2007-06-10 22:46:09 +02:00
|
|
|
peer_request const& r
|
2008-04-10 12:03:23 +02:00
|
|
|
, disk_buffer_holder& buffer
|
2007-06-10 22:46:09 +02:00
|
|
|
, boost::function<void(int, disk_io_job const&)> const& f);
|
|
|
|
|
|
|
|
void async_hash(int piece, boost::function<void(int, disk_io_job const&)> const& f);
|
|
|
|
|
2007-06-11 23:24:14 +02:00
|
|
|
void async_release_files(
|
2007-07-02 21:31:46 +02:00
|
|
|
boost::function<void(int, disk_io_job const&)> const& handler
|
|
|
|
= boost::function<void(int, disk_io_job const&)>());
|
|
|
|
|
2008-11-17 02:19:46 +01:00
|
|
|
void abort_disk_io();
|
|
|
|
|
2008-07-18 17:31:22 +02:00
|
|
|
void async_clear_read_cache(
|
|
|
|
boost::function<void(int, disk_io_job const&)> const& handler
|
|
|
|
= boost::function<void(int, disk_io_job const&)>());
|
|
|
|
|
2007-10-13 05:33:33 +02:00
|
|
|
void async_delete_files(
|
|
|
|
boost::function<void(int, disk_io_job const&)> const& handler
|
|
|
|
= boost::function<void(int, disk_io_job const&)>());
|
|
|
|
|
2013-05-09 04:50:16 +02:00
|
|
|
void async_move_storage(std::string const& p, int flags
|
2007-06-11 23:24:14 +02:00
|
|
|
, boost::function<void(int, disk_io_job const&)> const& handler);
|
2007-06-10 22:46:09 +02:00
|
|
|
|
2008-04-13 20:54:36 +02:00
|
|
|
void async_save_resume_data(
|
|
|
|
boost::function<void(int, disk_io_job const&)> const& handler);
|
|
|
|
|
2008-03-08 07:06:31 +01:00
|
|
|
enum return_t
|
|
|
|
{
|
|
|
|
// return values from check_fastresume and check_files
|
|
|
|
no_error = 0,
|
|
|
|
need_full_check = -1,
|
|
|
|
fatal_disk_error = -2,
|
2013-05-09 04:50:16 +02:00
|
|
|
disk_check_aborted = -3,
|
|
|
|
file_exist = -4,
|
2008-03-08 07:06:31 +01:00
|
|
|
};
|
|
|
|
|
2008-09-04 18:20:19 +02:00
|
|
|
storage_interface* get_storage_impl() { return m_storage.get(); }
|
|
|
|
|
2008-03-08 07:06:31 +01:00
|
|
|
private:
|
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
std::string save_path() const;
|
2008-03-08 07:06:31 +01:00
|
|
|
|
2010-03-06 08:16:39 +01:00
|
|
|
bool verify_resume_data(lazy_entry const& rd, error_code& e)
|
|
|
|
{ return m_storage->verify_resume_data(rd, e); }
|
2008-03-08 07:06:31 +01:00
|
|
|
|
|
|
|
bool is_allocating() const
|
|
|
|
{ return m_state == state_expand_pieces; }
|
|
|
|
|
|
|
|
void mark_failed(int index);
|
|
|
|
|
2008-07-18 01:41:46 +02:00
|
|
|
error_code const& error() const { return m_storage->error(); }
|
2008-04-13 00:08:07 +02:00
|
|
|
std::string const& error_file() const { return m_storage->error_file(); }
|
2009-05-31 21:44:56 +02:00
|
|
|
int last_piece() const { return m_last_piece; }
|
2008-03-08 07:06:31 +01:00
|
|
|
void clear_error() { m_storage->clear_error(); }
|
|
|
|
|
|
|
|
int slot_for(int piece) const;
|
|
|
|
int piece_for(int slot) const;
|
|
|
|
|
|
|
|
// helper functions for check_dastresume
|
2009-06-28 02:36:41 +02:00
|
|
|
int check_no_fastresume(error_code& error);
|
|
|
|
int check_init_storage(error_code& error);
|
2008-03-08 07:06:31 +01:00
|
|
|
|
|
|
|
// if error is set and return value is 'no_error' or 'need_full_check'
|
|
|
|
// the error message indicates that the fast resume data was rejected
|
|
|
|
// if 'fatal_disk_error' is returned, the error message indicates what
|
|
|
|
// when wrong in the disk access
|
2009-06-28 02:36:41 +02:00
|
|
|
int check_fastresume(lazy_entry const& rd, error_code& error);
|
2008-03-08 07:06:31 +01:00
|
|
|
|
|
|
|
// this function returns true if the checking is complete
|
2009-06-28 02:36:41 +02:00
|
|
|
int check_files(int& current_slot, int& have_piece, error_code& error);
|
2007-06-10 22:46:09 +02:00
|
|
|
|
2011-08-22 02:51:14 +02:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2007-06-10 22:46:09 +02:00
|
|
|
bool compact_allocation() const
|
2007-10-08 22:01:36 +02:00
|
|
|
{ return m_storage_mode == storage_mode_compact; }
|
2011-08-22 02:51:14 +02:00
|
|
|
#endif
|
2007-06-10 22:46:09 +02:00
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2007-09-01 05:00:31 +02:00
|
|
|
std::string name() const { return m_info->name(); }
|
2007-06-10 22:46:09 +02:00
|
|
|
#endif
|
|
|
|
|
2009-12-25 17:02:45 +01:00
|
|
|
bool allocate_slots_impl(int num_slots, mutex::scoped_lock& l, bool abort_on_disk = false);
|
2007-06-10 22:46:09 +02:00
|
|
|
|
2009-05-21 18:15:05 +02:00
|
|
|
// updates the ph.h hasher object with the data at the given slot
|
|
|
|
// and optionally a 'small hash' as well, the hash for
|
|
|
|
// the partial slot. Returns the number of bytes read
|
|
|
|
int hash_for_slot(int slot, partial_hash& h, int piece_size
|
|
|
|
, int small_piece_size = 0, sha1_hash* small_hash = 0);
|
|
|
|
|
2011-04-26 09:03:05 +02:00
|
|
|
void hint_read_impl(int piece_index, int offset, int size);
|
|
|
|
|
2008-04-05 23:18:27 +02:00
|
|
|
int read_impl(
|
2009-01-03 09:11:31 +01:00
|
|
|
file::iovec_t* bufs
|
2004-01-25 19:18:36 +01:00
|
|
|
, int piece_index
|
|
|
|
, int offset
|
2009-01-03 09:11:31 +01:00
|
|
|
, int num_bufs);
|
2004-01-25 19:18:36 +01:00
|
|
|
|
2008-04-05 23:18:27 +02:00
|
|
|
int write_impl(
|
2009-01-03 09:11:31 +01:00
|
|
|
file::iovec_t* bufs
|
2004-01-25 19:18:36 +01:00
|
|
|
, int piece_index
|
|
|
|
, int offset
|
2009-01-03 09:11:31 +01:00
|
|
|
, int num_bufs);
|
2003-12-07 06:53:04 +01:00
|
|
|
|
2009-09-05 09:21:10 +02:00
|
|
|
size_type physical_offset(int piece_index, int offset);
|
|
|
|
|
2009-02-14 05:31:08 +01:00
|
|
|
// returns the number of pieces left in the
|
|
|
|
// file currently being checked
|
|
|
|
int skip_file() const;
|
|
|
|
// -1=error 0=ok >0=skip this many pieces
|
2008-07-18 01:41:46 +02:00
|
|
|
int check_one_piece(int& have_piece);
|
2008-03-08 07:06:31 +01:00
|
|
|
int identify_data(
|
2009-05-21 18:15:05 +02:00
|
|
|
sha1_hash const& large_hash
|
|
|
|
, sha1_hash const& small_hash
|
2008-03-08 07:06:31 +01:00
|
|
|
, int current_slot);
|
2008-02-14 04:48:20 +01:00
|
|
|
|
2007-10-08 22:01:36 +02:00
|
|
|
void switch_to_full_mode();
|
2011-03-20 02:19:14 +01:00
|
|
|
sha1_hash hash_for_piece_impl(int piece, int* readback = 0);
|
2003-11-26 15:11:25 +01:00
|
|
|
|
2008-02-14 04:48:20 +01:00
|
|
|
int release_files_impl() { return m_storage->release_files(); }
|
|
|
|
int delete_files_impl() { return m_storage->delete_files(); }
|
2008-05-28 10:44:40 +02:00
|
|
|
int rename_file_impl(int index, std::string const& new_filename)
|
|
|
|
{ return m_storage->rename_file(index, new_filename); }
|
2003-12-25 13:11:31 +01:00
|
|
|
|
2013-05-09 04:50:16 +02:00
|
|
|
int move_storage_impl(std::string const& save_path, int flags);
|
2007-04-17 23:54:40 +02:00
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
int allocate_slot_for_piece(int piece_index);
|
2013-02-25 05:13:46 +01:00
|
|
|
#if defined TORRENT_DEBUG && !defined TORRENT_DISABLE_INVARIANT_CHECKS
|
2007-06-10 22:46:09 +02:00
|
|
|
void check_invariant() const;
|
2013-02-25 05:13:46 +01:00
|
|
|
#endif
|
2007-06-10 22:46:09 +02:00
|
|
|
#ifdef TORRENT_STORAGE_DEBUG
|
|
|
|
void debug_log() const;
|
|
|
|
#endif
|
2008-05-28 10:44:40 +02:00
|
|
|
boost::intrusive_ptr<torrent_info const> m_info;
|
|
|
|
file_storage const& m_files;
|
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
boost::scoped_ptr<storage_interface> m_storage;
|
|
|
|
|
2007-10-08 22:01:36 +02:00
|
|
|
storage_mode_t m_storage_mode;
|
2007-06-10 22:46:09 +02:00
|
|
|
|
|
|
|
// slots that haven't had any file storage allocated
|
|
|
|
std::vector<int> m_unallocated_slots;
|
|
|
|
// slots that have file storage, but isn't assigned to a piece
|
|
|
|
std::vector<int> m_free_slots;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
unallocated = -1, // the slot is unallocated
|
|
|
|
unassigned = -2 // the slot is allocated but not assigned to a piece
|
|
|
|
};
|
|
|
|
|
|
|
|
// maps slots to piece indices, if a slot doesn't have a piece
|
|
|
|
// it can either be 'unassigned' or 'unallocated'
|
|
|
|
std::vector<int> m_slot_to_piece;
|
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
std::string m_save_path;
|
2007-06-10 22:46:09 +02:00
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
mutable mutex m_mutex;
|
2007-06-10 22:46:09 +02:00
|
|
|
|
|
|
|
enum {
|
|
|
|
// the default initial state
|
|
|
|
state_none,
|
|
|
|
// the file checking is complete
|
|
|
|
state_finished,
|
|
|
|
// checking the files
|
|
|
|
state_full_check,
|
2007-10-08 22:01:36 +02:00
|
|
|
// move pieces to their final position
|
|
|
|
state_expand_pieces
|
2007-06-10 22:46:09 +02:00
|
|
|
} m_state;
|
|
|
|
int m_current_slot;
|
2007-10-08 22:01:36 +02:00
|
|
|
// used during check. If any piece is found
|
|
|
|
// that is not in its final position, this
|
|
|
|
// is set to true
|
|
|
|
bool m_out_of_place;
|
|
|
|
// used to move pieces while expanding
|
|
|
|
// the storage from compact allocation
|
|
|
|
// to full allocation
|
2011-02-22 03:53:26 +01:00
|
|
|
aligned_holder m_scratch_buffer;
|
|
|
|
aligned_holder m_scratch_buffer2;
|
2007-10-08 22:01:36 +02:00
|
|
|
// the piece that is in the scratch buffer
|
|
|
|
int m_scratch_piece;
|
2009-05-31 21:44:56 +02:00
|
|
|
|
|
|
|
// the last piece we wrote to or read from
|
|
|
|
int m_last_piece;
|
|
|
|
|
2007-09-01 05:00:31 +02:00
|
|
|
// this is saved in case we need to instantiate a new
|
|
|
|
// storage (osed when remapping files)
|
|
|
|
storage_constructor_type m_storage_constructor;
|
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
// this maps a piece hash to piece index. It will be
|
|
|
|
// build the first time it is used (to save time if it
|
2009-05-31 21:44:56 +02:00
|
|
|
// isn't needed)
|
2007-06-10 22:46:09 +02:00
|
|
|
std::multimap<sha1_hash, int> m_hash_to_piece;
|
|
|
|
|
2007-09-01 05:00:31 +02:00
|
|
|
// this map contains partial hashes for downloading
|
2007-09-19 21:55:11 +02:00
|
|
|
// pieces. This is only accessed from within the
|
|
|
|
// disk-io thread.
|
2007-06-10 22:46:09 +02:00
|
|
|
std::map<int, partial_hash> m_piece_hasher;
|
|
|
|
|
|
|
|
disk_io_thread& m_io_thread;
|
|
|
|
|
|
|
|
// the reason for this to be a void pointer
|
|
|
|
// is to avoid creating a dependency on the
|
|
|
|
// torrent. This shared_ptr is here only
|
|
|
|
// to keep the torrent object alive until
|
|
|
|
// the piece_manager destructs. This is because
|
|
|
|
// the torrent_info object is owned by the torrent.
|
|
|
|
boost::shared_ptr<void> m_torrent;
|
2003-10-23 01:00:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_STORAGE_HPP_INCLUDED
|
2003-11-26 15:11:25 +01:00
|
|
|
|