2003-10-23 01:00:57 +02:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2003-2018, 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
|
|
|
|
2015-05-05 07:37:01 +02:00
|
|
|
#include "libtorrent/config.hpp"
|
|
|
|
|
2015-04-21 03:16:28 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
2015-04-18 04:33:39 +02:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
#include <vector>
|
2009-10-26 02:29:39 +01:00
|
|
|
#include <sys/types.h>
|
2009-11-23 09:38:50 +01:00
|
|
|
#include <boost/function/function2.hpp>
|
2014-07-06 21:18:00 +02:00
|
|
|
#include <boost/function/function0.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>
|
2014-07-06 21:18:00 +02:00
|
|
|
#include <boost/enable_shared_from_this.hpp>
|
2009-11-23 09:38:50 +01:00
|
|
|
#include <boost/scoped_ptr.hpp>
|
2014-07-06 21:18:00 +02:00
|
|
|
#include <boost/unordered_set.hpp>
|
2014-08-01 09:32:54 +02:00
|
|
|
#include <boost/atomic.hpp>
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2015-04-21 03:16:28 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
2004-01-25 19:18:36 +01:00
|
|
|
|
2007-05-08 13:13:13 +02:00
|
|
|
#include "libtorrent/piece_picker.hpp"
|
2007-06-10 22:46:09 +02:00
|
|
|
#include "libtorrent/peer_request.hpp"
|
|
|
|
#include "libtorrent/hasher.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"
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/file_pool.hpp" // pool_file_status
|
|
|
|
#include "libtorrent/part_file.hpp"
|
|
|
|
#include "libtorrent/stat_cache.hpp"
|
2015-03-12 06:20:12 +01:00
|
|
|
#include "libtorrent/bdecode.hpp"
|
2013-12-30 03:50:29 +01:00
|
|
|
#include "libtorrent/bitfield.hpp"
|
2014-10-25 22:07:50 +02:00
|
|
|
#include "libtorrent/performance_counters.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2013-08-11 20:13:21 +02:00
|
|
|
// OVERVIEW
|
|
|
|
//
|
2014-04-28 06:46:33 +02:00
|
|
|
// libtorrent provides a customization point for storage of data. By default,
|
|
|
|
// (``default_storage``) downloaded files are saved to disk according with the
|
|
|
|
// general conventions of bittorrent clients, mimicing the original file layout
|
|
|
|
// when the torrent was created. The libtorrent user may define a custom
|
|
|
|
// storage to store piece data in a different way.
|
|
|
|
//
|
|
|
|
// A custom storage implementation must derive from and implement the
|
|
|
|
// storage_interface. You must also provide a function that constructs the
|
|
|
|
// custom storage object and provide this function to the add_torrent() call
|
|
|
|
// via add_torrent_params. Either passed in to the constructor or by setting
|
|
|
|
// the add_torrent_params::storage field.
|
|
|
|
//
|
|
|
|
// 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.
|
2013-08-11 20:13:21 +02:00
|
|
|
//
|
2014-09-03 08:52:45 +02:00
|
|
|
// .. code:: c++
|
2013-08-11 20:13:21 +02:00
|
|
|
//
|
|
|
|
// struct temp_storage : storage_interface
|
|
|
|
// {
|
|
|
|
// temp_storage(file_storage const& fs) : m_files(fs) {}
|
2014-07-06 21:18:00 +02:00
|
|
|
// virtual bool initialize(storage_error& se) { return false; }
|
2013-08-11 20:13:21 +02:00
|
|
|
// virtual bool has_any_file() { return false; }
|
2015-11-19 07:38:04 +01:00
|
|
|
// virtual int read(char* buf, int piece, int offset, int size)
|
2013-08-11 20:13:21 +02:00
|
|
|
// {
|
2015-11-19 07:38:04 +01:00
|
|
|
// std::map<int, std::vector<char> >::const_iterator i = m_file_data.find(piece);
|
2013-08-11 20:13:21 +02:00
|
|
|
// 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;
|
|
|
|
// }
|
2015-11-19 07:38:04 +01:00
|
|
|
// virtual int write(const char* buf, int piece, int offset, int size)
|
2013-08-11 20:13:21 +02:00
|
|
|
// {
|
2015-11-19 07:38:04 +01:00
|
|
|
// std::vector<char>& data = m_file_data[piece];
|
2013-08-11 20:13:21 +02:00
|
|
|
// 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; }
|
2015-03-21 01:12:40 +01:00
|
|
|
// virtual bool verify_resume_data(bdecode_node const& rd
|
|
|
|
// , std::vector<std::string> const* links
|
|
|
|
// , storage_error& error) { return false; }
|
2013-08-11 20:13:21 +02:00
|
|
|
// virtual bool write_resume_data(entry& rd) const { return false; }
|
2015-11-19 07:38:04 +01:00
|
|
|
// virtual boost::int64_t physical_offset(int piece, int offset)
|
|
|
|
// { return piece * m_files.piece_length() + offset; };
|
|
|
|
// virtual sha1_hash hash_for_slot(int piece, partial_hash& ph, int piece_size)
|
2013-08-11 20:13:21 +02:00
|
|
|
// {
|
|
|
|
// int left = piece_size - ph.offset;
|
|
|
|
// assert(left >= 0);
|
|
|
|
// if (left > 0)
|
|
|
|
// {
|
2015-11-19 07:38:04 +01:00
|
|
|
// std::vector<char>& data = m_file_data[piece];
|
2013-08-11 20:13:21 +02:00
|
|
|
// // 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
|
2017-06-17 17:35:53 +02:00
|
|
|
// // and fill it with zeros.
|
2013-08-11 20:13:21 +02:00
|
|
|
// 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;
|
|
|
|
// };
|
|
|
|
//
|
2014-07-06 21:18:00 +02:00
|
|
|
// storage_interface* temp_storage_constructor(storage_params const& params)
|
2013-08-11 20:13:21 +02:00
|
|
|
// {
|
2014-07-06 21:18:00 +02:00
|
|
|
// return new temp_storage(*params.files);
|
2013-08-11 20:13:21 +02:00
|
|
|
// }
|
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;
|
2014-07-06 21:18:00 +02:00
|
|
|
struct cache_status;
|
|
|
|
namespace aux { struct session_settings; }
|
|
|
|
struct cached_piece_entry;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2015-02-15 08:31:35 +01:00
|
|
|
TORRENT_EXTRA_EXPORT int copy_bufs(file::iovec_t const* bufs, int bytes, file::iovec_t* target);
|
|
|
|
TORRENT_EXTRA_EXPORT void advance_bufs(file::iovec_t*& bufs, int bytes);
|
|
|
|
TORRENT_EXTRA_EXPORT void clear_bufs(file::iovec_t const* bufs, int num_bufs);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// flags for async_move_storage
|
|
|
|
enum move_flags_t
|
2007-06-10 22:46:09 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
// replace any files in the destination when copying
|
|
|
|
// or moving the storage
|
|
|
|
always_replace_files,
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
fail_if_exist,
|
|
|
|
|
|
|
|
// if any file exist in the target, take those files instead
|
|
|
|
// of the ones we may have in the source.
|
|
|
|
dont_replace
|
2007-06-10 22:46:09 +02:00
|
|
|
};
|
|
|
|
|
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
|
2014-04-28 06:46:33 +02:00
|
|
|
// 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
|
2017-06-17 17:35:53 +02:00
|
|
|
// received for instance), or saving multi file torrents in a single file in
|
2014-04-28 06:46:33 +02:00
|
|
|
// order to be able to take advantage of optimized disk-I/O.
|
2013-08-11 08:04:24 +02:00
|
|
|
//
|
2014-04-28 06:46:33 +02:00
|
|
|
// 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.
|
2013-08-11 08:04:24 +02:00
|
|
|
//
|
2015-11-19 07:38:04 +01:00
|
|
|
// The storage interface is based on pieces. Avery read and write operation
|
|
|
|
// happens in the piece-space. Each piece fits 'piece_size' number
|
2013-08-11 08:04:24 +02:00
|
|
|
// of bytes. All access is done by writing and reading whole or partial
|
2015-11-19 07:38:04 +01:00
|
|
|
// pieces.
|
2013-08-11 08:04:24 +02:00
|
|
|
//
|
2014-04-28 06:46:33 +02:00
|
|
|
// 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
|
2013-08-11 08:04:24 +02:00
|
|
|
// 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
|
|
|
{
|
2013-11-29 06:29:39 +01:00
|
|
|
// hidden
|
2014-07-06 21:18:00 +02:00
|
|
|
storage_interface(): m_settings(0) {}
|
2013-08-11 20:13:21 +02:00
|
|
|
|
2014-02-28 11:19:29 +01:00
|
|
|
|
2017-11-28 22:59:46 +01:00
|
|
|
// This function is called when the *storage* on disk 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.
|
|
|
|
//
|
|
|
|
// This function may be called multiple time on a single instance. When a
|
|
|
|
// torrent is force-rechecked, the storage is re-initialized to trigger
|
|
|
|
// the re-check from scratch.
|
|
|
|
//
|
|
|
|
// The function is not necessarily called before other member functions.
|
|
|
|
// For instance has_any_files() and verify_resume_data() are
|
|
|
|
// called early to determine whether we may have to check all files or
|
|
|
|
// not. If we're doing a full check of the files every piece will be
|
|
|
|
// hashed, causing readv() to be called as well.
|
|
|
|
//
|
|
|
|
// Any required internals that need initialization should be done in the
|
|
|
|
// constructor. This function is called before the torrent starts to
|
|
|
|
// download.
|
2013-08-11 20:13:21 +02:00
|
|
|
//
|
2014-07-06 21:18:00 +02:00
|
|
|
// If an error occurs, ``storage_error`` should be set to reflect it.
|
|
|
|
virtual void initialize(storage_error& ec) = 0;
|
|
|
|
|
|
|
|
// These functions should read and write the data in or to the given
|
|
|
|
// ``piece`` 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::
|
|
|
|
//
|
2014-04-28 06:46:33 +02:00
|
|
|
// struct iovec_t { void* iov_base; size_t iov_len; };
|
2014-07-06 21:18:00 +02:00
|
|
|
//
|
|
|
|
// These functions may be called simultaneously from multiple threads.
|
|
|
|
// Make sure they are thread safe. The ``file`` in libtorrent is thread
|
|
|
|
// safe when it can fall back to ``pread``, ``preadv`` or the windows
|
|
|
|
// equivalents. On targets where read operations cannot be thread safe
|
|
|
|
// (i.e one has to seek first and then read), only one disk thread is
|
|
|
|
// used.
|
|
|
|
//
|
2014-04-28 06:46:33 +02:00
|
|
|
// 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.
|
2013-08-11 20:13:21 +02:00
|
|
|
//
|
2015-12-11 14:45:08 +01:00
|
|
|
// The ``offset`` is aligned to 16 kiB boundaries *most of the time*, but
|
2014-04-28 06:46:33 +02:00
|
|
|
// there are rare exceptions when it's not. Specifically if the read
|
2014-07-06 21:18:00 +02:00
|
|
|
// cache is disabled/or full and a peer requests unaligned data. Most
|
|
|
|
// clients request aligned data.
|
|
|
|
//
|
|
|
|
// The number of bytes read or written should be returned, or -1 on
|
|
|
|
// error. If there's an error, the ``storage_error`` must be filled out
|
|
|
|
// to represent the error that occurred.
|
|
|
|
virtual int readv(file::iovec_t const* bufs, int num_bufs
|
|
|
|
, int piece, int offset, int flags, storage_error& ec) = 0;
|
|
|
|
virtual int writev(file::iovec_t const* bufs, int num_bufs
|
|
|
|
, int piece, int offset, int flags, storage_error& ec) = 0;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// If an error occurs, ``storage_error`` should be set to reflect it.
|
|
|
|
virtual bool has_any_file(storage_error& ec) = 0;
|
|
|
|
|
|
|
|
// change the priorities of files. This is a fenced job and is
|
|
|
|
// guaranteed to be the only running function on this storage
|
|
|
|
// when called
|
2018-05-23 12:09:22 +02:00
|
|
|
virtual void set_file_priority(std::vector<boost::uint8_t>& prio
|
2014-07-06 21:18:00 +02:00
|
|
|
, storage_error& ec) = 0;
|
2009-02-17 01:11:38 +01:00
|
|
|
|
2014-04-28 06:46:33 +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.
|
2013-08-11 20:13:21 +02:00
|
|
|
//
|
2014-04-28 06:46:33 +02:00
|
|
|
// Before moving the files, any open file handles may have to be closed,
|
|
|
|
// like ``release_files()``.
|
2014-07-06 21:18:00 +02:00
|
|
|
//
|
|
|
|
//If an error occurs, ``storage_error`` should be set to reflect it.
|
2013-08-11 20:13:21 +02:00
|
|
|
//
|
|
|
|
// returns one of:
|
|
|
|
// | no_error = 0
|
2014-07-21 05:03:59 +02:00
|
|
|
// | fatal_disk_error = -1
|
|
|
|
// | need_full_check = -2
|
2013-08-11 20:13:21 +02:00
|
|
|
// | file_exist = -4
|
2014-07-06 21:18:00 +02:00
|
|
|
virtual int move_storage(std::string const& save_path, int flags
|
|
|
|
, storage_error& ec) = 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.
|
2014-07-06 21:18:00 +02:00
|
|
|
//
|
2013-08-11 20:13:21 +02:00
|
|
|
// The default storage may compare file sizes and time stamps of the files.
|
2014-07-06 21:18:00 +02:00
|
|
|
//
|
|
|
|
// If an error occurs, ``storage_error`` should be set to reflect it.
|
|
|
|
//
|
|
|
|
// 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.
|
2015-03-21 01:12:40 +01:00
|
|
|
//
|
|
|
|
// If the ``links`` pointer is non-null, it has the same number
|
|
|
|
// of elements as there are files. Each element is either empty or contains
|
|
|
|
// the absolute path to a file identical to the corresponding file in this
|
|
|
|
// torrent. The storage must create hard links (or copy) those files. If
|
|
|
|
// any file does not exist or is inaccessible, the disk job must fail.
|
|
|
|
virtual bool verify_resume_data(bdecode_node const& rd
|
|
|
|
, std::vector<std::string> const* links
|
|
|
|
, storage_error& ec) = 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.
|
|
|
|
//
|
2014-07-06 21:18:00 +02:00
|
|
|
// If an error occurs, ``storage_error`` should be set to reflect it.
|
2013-08-11 20:13:21 +02:00
|
|
|
//
|
2014-07-06 21:18:00 +02:00
|
|
|
virtual void write_resume_data(entry& rd, storage_error& ec) const = 0;
|
2007-04-30 03:06:29 +02:00
|
|
|
|
2014-07-06 21:18:00 +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().
|
2013-08-11 20:13:21 +02:00
|
|
|
//
|
2014-07-06 21:18:00 +02:00
|
|
|
// If an error occurs, ``storage_error`` should be set to reflect it.
|
2013-08-11 20:13:21 +02:00
|
|
|
//
|
2014-07-06 21:18:00 +02:00
|
|
|
virtual void release_files(storage_error& ec) = 0;
|
2007-05-16 03:16:08 +02:00
|
|
|
|
2017-06-17 17:35:53 +02:00
|
|
|
// Rename the file with index ``file`` to name ``new_name``.
|
2013-08-11 20:13:21 +02:00
|
|
|
//
|
2014-07-06 21:18:00 +02:00
|
|
|
// If an error occurs, ``storage_error`` should be set to reflect it.
|
2013-08-11 20:13:21 +02:00
|
|
|
//
|
2015-01-16 05:02:44 +01:00
|
|
|
virtual void rename_file(int index, std::string const& new_filename
|
2014-07-06 21:18:00 +02:00
|
|
|
, storage_error& ec) = 0;
|
2008-05-28 10:44:40 +02:00
|
|
|
|
2016-03-13 08:50:37 +01:00
|
|
|
// This function should delete some or all of the storage for this torrent.
|
|
|
|
// The ``options`` parameter specifies whether to delete all files or just
|
|
|
|
// the partfile. ``options`` are set to the same value as the options
|
|
|
|
// passed to session::remove_torrent().
|
2013-08-11 20:13:21 +02:00
|
|
|
//
|
2014-07-06 21:18:00 +02:00
|
|
|
// If an error occurs, ``storage_error`` should be set to reflect it.
|
2013-08-11 20:13:21 +02:00
|
|
|
//
|
2014-04-28 06:46:33 +02:00
|
|
|
// The ``disk_buffer_pool`` is used to allocate and free disk buffers. It
|
2017-06-17 17:35:53 +02:00
|
|
|
// has the following members:
|
2014-07-06 21:18:00 +02:00
|
|
|
//
|
2017-06-17 17:35:53 +02:00
|
|
|
// .. code:: c++
|
|
|
|
//
|
|
|
|
// struct disk_buffer_pool : boost::noncopyable
|
|
|
|
// {
|
|
|
|
// char* allocate_buffer(char const* category);
|
|
|
|
// void free_buffer(char* buf);
|
2013-08-11 20:13:21 +02:00
|
|
|
//
|
2017-06-17 17:35:53 +02:00
|
|
|
// char* allocate_buffers(int blocks, char const* category);
|
|
|
|
// void free_buffers(char* buf, int blocks);
|
2013-08-11 20:13:21 +02:00
|
|
|
//
|
2017-06-17 17:35:53 +02:00
|
|
|
// int block_size() const { return m_block_size; }
|
2013-08-11 20:13:21 +02:00
|
|
|
//
|
2017-06-17 17:35:53 +02:00
|
|
|
// void release_memory();
|
|
|
|
// };
|
2016-03-13 08:50:37 +01:00
|
|
|
virtual void delete_files(int options, storage_error& ec) = 0;
|
2008-02-14 04:48:20 +01:00
|
|
|
|
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
|
2014-04-28 06:46:33 +02:00
|
|
|
// storage implementation can perform last operations on a file. The file
|
|
|
|
// will not be opened for writing after this.
|
2014-07-06 21:18:00 +02:00
|
|
|
//
|
|
|
|
// ``index`` is the index of the file that completed.
|
|
|
|
//
|
|
|
|
// On windows the default storage implementation clears the sparse file
|
|
|
|
// flag on the specified file.
|
2013-08-11 20:13:21 +02:00
|
|
|
//
|
2014-07-06 21:18:00 +02:00
|
|
|
// If an error occurs, ``storage_error`` should be set to reflect it.
|
2013-08-11 20:13:21 +02:00
|
|
|
//
|
2014-07-06 21:18:00 +02:00
|
|
|
virtual void finalize_file(int, storage_error&) {}
|
2013-02-16 09:26:55 +01:00
|
|
|
#endif
|
2010-01-09 19:40:05 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// called periodically (useful for deferred flushing). When returning
|
|
|
|
// false, it means no more ticks are necessary. Any disk job submitted
|
|
|
|
// will re-enable ticking. The default will always turn ticking back
|
|
|
|
// off again.
|
|
|
|
virtual bool tick() { return false; }
|
2013-09-01 19:34:05 +02:00
|
|
|
|
|
|
|
// access global session_settings
|
2014-07-06 21:18:00 +02:00
|
|
|
aux::session_settings const& settings() const { return *m_settings; }
|
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
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// initialized in disk_io_thread::perform_async_job
|
|
|
|
aux::session_settings* m_settings;
|
2007-03-16 06:29:23 +01:00
|
|
|
};
|
2004-12-21 13:30:09 +01:00
|
|
|
|
2014-04-28 06:46:33 +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
|
|
|
{
|
2015-11-16 04:36:55 +01:00
|
|
|
friend struct write_fileop;
|
|
|
|
friend struct read_fileop;
|
2011-04-17 00:58:11 +02:00
|
|
|
public:
|
2014-02-03 03:10:55 +01:00
|
|
|
// constructs the default_storage based on the give file_storage (fs).
|
|
|
|
// ``mapped`` is an optional argument (it may be NULL). If non-NULL it
|
2016-01-09 22:20:27 +01:00
|
|
|
// represents the file mapping that have been made to the torrent before
|
2014-02-03 03:10:55 +01:00
|
|
|
// adding it. That's where files are supposed to be saved and looked for
|
|
|
|
// on disk. ``save_path`` is the root save folder for this torrent.
|
|
|
|
// ``file_pool`` is the cache of file handles that the storage will use.
|
|
|
|
// All files it opens will ask the file_pool to open them. ``file_prio``
|
|
|
|
// is a vector indicating the priority of files on startup. It may be
|
|
|
|
// an empty vector. Any file whose index is not represented by the vector
|
|
|
|
// (because the vector is too short) are assumed to have priority 1.
|
|
|
|
// this is used to treat files with priority 0 slightly differently.
|
2014-07-06 21:18:00 +02:00
|
|
|
default_storage(storage_params const& params);
|
2013-11-29 06:29:39 +01:00
|
|
|
|
|
|
|
// hidden
|
2011-04-17 00:58:11 +02:00
|
|
|
~default_storage();
|
|
|
|
|
2013-02-16 09:26:55 +01:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2015-05-05 07:37:01 +02:00
|
|
|
void finalize_file(int file, storage_error& ec) TORRENT_OVERRIDE;
|
2013-02-16 09:26:55 +01:00
|
|
|
#endif
|
2015-05-05 07:37:01 +02:00
|
|
|
virtual bool has_any_file(storage_error& ec) TORRENT_OVERRIDE;
|
2018-05-23 12:09:22 +02:00
|
|
|
virtual void set_file_priority(std::vector<boost::uint8_t>& prio
|
2015-05-05 07:37:01 +02:00
|
|
|
, storage_error& ec) TORRENT_OVERRIDE;
|
|
|
|
virtual void rename_file(int index, std::string const& new_filename
|
|
|
|
, storage_error& ec) TORRENT_OVERRIDE;
|
|
|
|
virtual void release_files(storage_error& ec) TORRENT_OVERRIDE;
|
2016-03-13 08:50:37 +01:00
|
|
|
virtual void delete_files(int options, storage_error& ec) TORRENT_OVERRIDE;
|
2015-05-05 07:37:01 +02:00
|
|
|
virtual void initialize(storage_error& ec) TORRENT_OVERRIDE;
|
|
|
|
virtual int move_storage(std::string const& save_path, int flags
|
|
|
|
, storage_error& ec) TORRENT_OVERRIDE;
|
|
|
|
virtual bool verify_resume_data(bdecode_node const& rd
|
2015-03-21 01:12:40 +01:00
|
|
|
, std::vector<std::string> const* links
|
2015-05-05 07:37:01 +02:00
|
|
|
, storage_error& error) TORRENT_OVERRIDE;
|
|
|
|
virtual void write_resume_data(entry& rd, storage_error& ec) const TORRENT_OVERRIDE;
|
|
|
|
virtual bool tick() TORRENT_OVERRIDE;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
int readv(file::iovec_t const* bufs, int num_bufs
|
2015-05-05 07:37:01 +02:00
|
|
|
, int piece, int offset, int flags, storage_error& ec) TORRENT_OVERRIDE;
|
2014-07-06 21:18:00 +02:00
|
|
|
int writev(file::iovec_t const* bufs, int num_bufs
|
2015-05-05 07:37:01 +02:00
|
|
|
, int piece, int offset, int flags, storage_error& ec) TORRENT_OVERRIDE;
|
2011-04-17 00:58:11 +02:00
|
|
|
|
2014-02-03 03:10:55 +01:00
|
|
|
// if the files in this storage are mapped, returns the mapped
|
|
|
|
// file_storage, otherwise returns the original file_storage object.
|
2013-11-09 03:01:26 +01:00
|
|
|
file_storage const& files() const { return m_mapped_files?*m_mapped_files:m_files; }
|
|
|
|
|
2015-08-03 06:25:14 +02:00
|
|
|
#ifdef TORRENT_DISK_STATS
|
|
|
|
static bool disk_write_access_log();
|
|
|
|
static void disk_write_access_log(bool enable);
|
|
|
|
#endif
|
|
|
|
|
2013-11-09 03:01:26 +01:00
|
|
|
private:
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
void delete_one_file(std::string const& p, error_code& ec);
|
2011-04-17 00:58:11 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
void need_partfile();
|
2011-04-17 00:58:11 +02:00
|
|
|
|
|
|
|
boost::scoped_ptr<file_storage> m_mapped_files;
|
|
|
|
file_storage const& m_files;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// in order to avoid calling stat() on each file multiple times
|
|
|
|
// during startup, cache the results in here, and clear it all
|
|
|
|
// out once the torrent starts (to avoid getting stale results)
|
2015-11-19 07:38:04 +01:00
|
|
|
// each entry represents the size and timestamp of the file
|
2014-07-06 21:18:00 +02:00
|
|
|
mutable stat_cache m_stat_cache;
|
|
|
|
|
2011-04-17 00:58:11 +02:00
|
|
|
// helper function to open a file in the file pool with the right mode
|
2015-01-03 17:57:21 +01:00
|
|
|
file_handle open_file(int file, int mode, storage_error& ec) const;
|
|
|
|
file_handle open_file_impl(int file, int mode, error_code& ec) const;
|
2011-04-17 00:58:11 +02:00
|
|
|
|
2018-02-25 17:17:42 +01:00
|
|
|
bool use_partfile(int index);
|
|
|
|
void use_partfile(int index, bool b);
|
|
|
|
|
2011-04-17 00:58:11 +02:00
|
|
|
std::vector<boost::uint8_t> m_file_priority;
|
|
|
|
std::string m_save_path;
|
2014-07-06 21:18:00 +02:00
|
|
|
std::string m_part_file_name;
|
2018-01-14 16:14:30 +01:00
|
|
|
|
2018-02-25 17:17:42 +01:00
|
|
|
// this this is an array indexed by file-index. Each slot represents
|
|
|
|
// whether this file has the part-file enabled for it. This is used for
|
|
|
|
// backwards compatibility with pre-partfile versions of libtorrent. If
|
|
|
|
// this vector is empty, the default is that files *do* use the partfile.
|
|
|
|
// on startup, any 0-priority file that's found in it's original location
|
|
|
|
// is expected to be an old-style (pre-partfile) torrent storage, and
|
|
|
|
// those files have their slot set to false in this vector.
|
|
|
|
// note that the vector is *sparse*, it's only allocated if a file has its
|
|
|
|
// entry set to false, and only indices up to that entry.
|
|
|
|
std::vector<bool> m_use_partfile;
|
2018-01-14 16:14:30 +01:00
|
|
|
|
2011-04-17 00:58:11 +02:00
|
|
|
// the file pool is typically stored in
|
|
|
|
// the session, to make all storage
|
|
|
|
// instances use the same pool
|
|
|
|
file_pool& m_pool;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// used for skipped files
|
|
|
|
boost::scoped_ptr<part_file> m_part_file;
|
|
|
|
|
2013-12-30 03:50:29 +01:00
|
|
|
// this is a bitfield with one bit per file. A bit being set means
|
|
|
|
// we've written to that file previously. If we do write to a file
|
|
|
|
// whose bit is 0, we set the file size, to make the file allocated
|
|
|
|
// on disk (in full allocation mode) and just sparsely allocated in
|
|
|
|
// case of sparse allocation mode
|
2017-05-07 17:01:42 +02:00
|
|
|
mutable mutex m_file_created_mutex;
|
2015-11-16 04:36:55 +01:00
|
|
|
mutable bitfield m_file_created;
|
2013-12-30 03:50:29 +01:00
|
|
|
|
2011-04-17 00:58:11 +02:00
|
|
|
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
|
2015-08-02 21:55:05 +02:00
|
|
|
class disabled_storage TORRENT_FINAL : public storage_interface, boost::noncopyable
|
2011-04-17 00:58:11 +02:00
|
|
|
{
|
|
|
|
public:
|
2015-05-05 07:37:01 +02:00
|
|
|
virtual bool has_any_file(storage_error&) TORRENT_OVERRIDE { return false; }
|
2018-05-23 12:09:22 +02:00
|
|
|
virtual void set_file_priority(std::vector<boost::uint8_t>&
|
2015-05-05 07:37:01 +02:00
|
|
|
, storage_error&) TORRENT_OVERRIDE {}
|
|
|
|
virtual void rename_file(int, std::string const&, storage_error&) TORRENT_OVERRIDE {}
|
|
|
|
virtual void release_files(storage_error&) TORRENT_OVERRIDE {}
|
2016-03-13 08:50:37 +01:00
|
|
|
virtual void delete_files(int, storage_error&) TORRENT_OVERRIDE {}
|
2015-05-05 07:37:01 +02:00
|
|
|
virtual void initialize(storage_error&) TORRENT_OVERRIDE {}
|
|
|
|
virtual int move_storage(std::string const&, int, storage_error&) TORRENT_OVERRIDE { return 0; }
|
|
|
|
|
|
|
|
virtual int readv(file::iovec_t const* bufs, int num_bufs, int piece
|
|
|
|
, int offset, int flags, storage_error& ec) TORRENT_OVERRIDE;
|
|
|
|
virtual int writev(file::iovec_t const* bufs, int num_bufs, int piece
|
|
|
|
, int offset, int flags, storage_error& ec) TORRENT_OVERRIDE;
|
|
|
|
|
|
|
|
virtual bool verify_resume_data(bdecode_node const&
|
2015-04-19 00:00:27 +02:00
|
|
|
, std::vector<std::string> const*
|
2015-05-05 07:37:01 +02:00
|
|
|
, storage_error&) TORRENT_OVERRIDE { return false; }
|
|
|
|
virtual void write_resume_data(entry&, storage_error&) const TORRENT_OVERRIDE {}
|
2011-04-17 00:58:11 +02:00
|
|
|
};
|
|
|
|
|
2017-06-17 17:35:53 +02:00
|
|
|
// this storage implementation always reads zeros, and always discards
|
2014-07-06 21:18:00 +02:00
|
|
|
// anything written to it
|
2015-08-02 21:55:05 +02:00
|
|
|
struct zero_storage TORRENT_FINAL : storage_interface
|
2013-05-09 04:50:16 +02:00
|
|
|
{
|
2015-05-05 07:37:01 +02:00
|
|
|
virtual void initialize(storage_error&) TORRENT_OVERRIDE {}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
virtual int readv(file::iovec_t const* bufs, int num_bufs
|
2015-05-05 07:37:01 +02:00
|
|
|
, int piece, int offset, int flags, storage_error& ec) TORRENT_OVERRIDE;
|
2014-07-06 21:18:00 +02:00
|
|
|
virtual int writev(file::iovec_t const* bufs, int num_bufs
|
2015-05-05 07:37:01 +02:00
|
|
|
, int piece, int offset, int flags, storage_error& ec) TORRENT_OVERRIDE;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2015-05-05 07:37:01 +02:00
|
|
|
virtual bool has_any_file(storage_error&) TORRENT_OVERRIDE { return false; }
|
2018-05-23 12:09:22 +02:00
|
|
|
virtual void set_file_priority(std::vector<boost::uint8_t>& /* prio */
|
2015-05-05 07:37:01 +02:00
|
|
|
, storage_error&) TORRENT_OVERRIDE {}
|
2014-07-06 21:18:00 +02:00
|
|
|
virtual int move_storage(std::string const& /* save_path */
|
2015-05-05 07:37:01 +02:00
|
|
|
, int /* flags */, storage_error&) TORRENT_OVERRIDE { return 0; }
|
2015-03-21 01:12:40 +01:00
|
|
|
virtual bool verify_resume_data(bdecode_node const& /* rd */
|
|
|
|
, std::vector<std::string> const* /* links */
|
2015-05-05 07:37:01 +02:00
|
|
|
, storage_error&) TORRENT_OVERRIDE
|
2014-07-06 21:18:00 +02:00
|
|
|
{ return false; }
|
2015-05-05 07:37:01 +02:00
|
|
|
virtual void write_resume_data(entry&, storage_error&) const TORRENT_OVERRIDE {}
|
|
|
|
virtual void release_files(storage_error&) TORRENT_OVERRIDE {}
|
2014-07-06 21:18:00 +02:00
|
|
|
virtual void rename_file(int /* index */
|
2015-05-05 07:37:01 +02:00
|
|
|
, std::string const& /* new_filenamem */, storage_error&) TORRENT_OVERRIDE {}
|
2016-03-13 08:50:37 +01:00
|
|
|
virtual void delete_files(int, storage_error&) TORRENT_OVERRIDE {}
|
2014-07-06 21:18:00 +02:00
|
|
|
};
|
2013-05-09 04:50:16 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
struct disk_io_thread;
|
2013-05-09 04:50:16 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// implements the disk I/O job fence used by the piece_manager
|
|
|
|
// to provide to the disk thread. Whenever a disk job needs
|
|
|
|
// exclusive access to the storage for that torrent, it raises
|
|
|
|
// the fence, blocking all new jobs, until there are no longer
|
|
|
|
// any outstanding jobs on the torrent, then the fence is lowered
|
|
|
|
// and it can be performed, along with the backlog of jobs that
|
|
|
|
// accrued while the fence was up
|
|
|
|
struct TORRENT_EXTRA_EXPORT disk_job_fence
|
|
|
|
{
|
|
|
|
disk_job_fence();
|
|
|
|
~disk_job_fence()
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(int(m_outstanding_jobs) == 0);
|
|
|
|
TORRENT_ASSERT(m_blocked_jobs.size() == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns one of the fence_* enums.
|
|
|
|
// if there are no outstanding jobs on the
|
|
|
|
// storage, fence_post_fence is returned, the flush job is expected
|
|
|
|
// to be discarded by the caller.
|
|
|
|
// fence_post_flush is returned if the fence job was blocked and queued,
|
|
|
|
// but the flush job should be posted (i.e. put on the job queue)
|
|
|
|
// fence_post_none if both the fence and the flush jobs were queued.
|
|
|
|
enum { fence_post_fence = 0, fence_post_flush = 1, fence_post_none = 2 };
|
2014-08-01 09:32:54 +02:00
|
|
|
int raise_fence(disk_io_job* fence_job, disk_io_job* flush_job
|
2014-10-25 22:07:50 +02:00
|
|
|
, counters& cnt);
|
2014-07-06 21:18:00 +02:00
|
|
|
bool has_fence() const;
|
|
|
|
|
|
|
|
// called whenever a job completes and is posted back to the
|
|
|
|
// main network thread. the tailqueue of jobs will have the
|
|
|
|
// backed-up jobs prepended to it in case this resulted in the
|
|
|
|
// fence being lowered.
|
2015-08-19 15:22:00 +02:00
|
|
|
int job_complete(disk_io_job* j, tailqueue<disk_io_job>& job_queue);
|
2014-07-06 21:18:00 +02:00
|
|
|
int num_outstanding_jobs() const { return m_outstanding_jobs; }
|
|
|
|
|
|
|
|
// if there is a fence up, returns true and adds the job
|
|
|
|
// to the queue of blocked jobs
|
|
|
|
bool is_blocked(disk_io_job* j);
|
2015-08-02 05:57:11 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// the number of blocked jobs
|
|
|
|
int num_blocked() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// when > 0, this storage is blocked for new async
|
|
|
|
// operations until all outstanding jobs have completed.
|
|
|
|
// at that point, the m_blocked_jobs are issued
|
|
|
|
// the count is the number of fence job currently in the queue
|
|
|
|
int m_has_fence;
|
|
|
|
|
|
|
|
// when there's a fence up, jobs are queued up in here
|
|
|
|
// until the fence is lowered
|
2015-08-19 15:22:00 +02:00
|
|
|
tailqueue<disk_io_job> m_blocked_jobs;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// the number of disk_io_job objects there are, belonging
|
|
|
|
// to this torrent, currently pending, hanging off of
|
|
|
|
// cached_piece_entry objects. This is used to determine
|
|
|
|
// when the fence can be lowered
|
2014-08-01 09:32:54 +02:00
|
|
|
boost::atomic<int> m_outstanding_jobs;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// must be held when accessing m_has_fence and
|
|
|
|
// m_blocked_jobs
|
|
|
|
mutable mutex m_mutex;
|
2013-05-09 04:50:16 +02:00
|
|
|
};
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// this class keeps track of which pieces, belonging to
|
|
|
|
// a specific storage, are in the cache right now. It's
|
|
|
|
// used for quickly being able to evict all pieces for a
|
|
|
|
// specific torrent
|
|
|
|
struct TORRENT_EXTRA_EXPORT storage_piece_set
|
|
|
|
{
|
|
|
|
void add_piece(cached_piece_entry* p);
|
|
|
|
void remove_piece(cached_piece_entry* p);
|
2015-08-02 05:57:11 +02:00
|
|
|
bool has_piece(cached_piece_entry const* p) const;
|
2015-11-19 01:51:17 +01:00
|
|
|
int num_pieces() const { return int(m_cached_pieces.size()); }
|
2014-07-06 21:18:00 +02:00
|
|
|
boost::unordered_set<cached_piece_entry*> const& cached_pieces() const
|
|
|
|
{ return m_cached_pieces; }
|
|
|
|
private:
|
|
|
|
// these are cached pieces belonging to this storage
|
|
|
|
boost::unordered_set<cached_piece_entry*> m_cached_pieces;
|
|
|
|
};
|
2007-04-18 02:36:09 +02:00
|
|
|
|
2012-03-19 00:31:04 +01:00
|
|
|
class TORRENT_EXTRA_EXPORT piece_manager
|
2014-07-06 21:18:00 +02:00
|
|
|
: public boost::enable_shared_from_this<piece_manager>
|
|
|
|
, public disk_job_fence
|
|
|
|
, public storage_piece_set
|
2007-06-10 22:46:09 +02:00
|
|
|
, boost::noncopyable
|
2003-11-26 15:11:25 +01:00
|
|
|
{
|
2007-06-10 22:46:09 +02:00
|
|
|
friend struct disk_io_thread;
|
2003-11-26 15:11:25 +01:00
|
|
|
public:
|
2003-12-07 02:26:57 +01:00
|
|
|
|
|
|
|
piece_manager(
|
2014-07-06 21:18:00 +02:00
|
|
|
storage_interface* storage_impl
|
|
|
|
, boost::shared_ptr<void> const& torrent
|
|
|
|
, file_storage* files);
|
2003-12-07 02:26:57 +01:00
|
|
|
|
2004-01-04 13:54:38 +01:00
|
|
|
~piece_manager();
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
file_storage const* files() const { return &m_files; }
|
2008-04-13 20:54:36 +02:00
|
|
|
|
2008-03-08 07:06:31 +01:00
|
|
|
enum return_t
|
|
|
|
{
|
2014-07-20 10:59:02 +02:00
|
|
|
// return values from check_fastresume, and move_storage
|
2008-03-08 07:06:31 +01:00
|
|
|
no_error = 0,
|
2014-07-06 21:18:00 +02:00
|
|
|
fatal_disk_error = -1,
|
|
|
|
need_full_check = -2,
|
2013-05-09 04:50:16 +02:00
|
|
|
disk_check_aborted = -3,
|
2014-07-05 01:40:31 +02:00
|
|
|
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(); }
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
void write_resume_data(entry& rd, storage_error& ec) const;
|
2008-03-08 07:06:31 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifdef TORRENT_DEBUG
|
|
|
|
void assert_torrent_refcount() const;
|
|
|
|
#endif
|
|
|
|
private:
|
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
|
2015-03-21 01:12:40 +01:00
|
|
|
int check_fastresume(bdecode_node const& rd
|
|
|
|
, std::vector<std::string> const* links
|
|
|
|
, storage_error& error);
|
2007-06-10 22:46:09 +02:00
|
|
|
|
2015-08-06 08:30:06 +02:00
|
|
|
// helper functions for check_fastresume
|
2014-07-06 21:18:00 +02:00
|
|
|
int check_no_fastresume(storage_error& error);
|
|
|
|
int check_init_storage(storage_error& error);
|
2007-06-10 22:46:09 +02:00
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2014-07-06 21:18:00 +02:00
|
|
|
std::string name() const { return m_files.name(); }
|
2007-06-10 22:46:09 +02:00
|
|
|
#endif
|
|
|
|
|
2014-01-21 20:26:09 +01:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
2007-06-10 22:46:09 +02:00
|
|
|
void check_invariant() const;
|
2013-02-25 05:13:46 +01:00
|
|
|
#endif
|
2008-05-28 10:44:40 +02:00
|
|
|
file_storage const& m_files;
|
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
boost::scoped_ptr<storage_interface> m_storage;
|
|
|
|
|
|
|
|
// 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
|
|
|
};
|
|
|
|
|
2015-11-16 04:36:55 +01:00
|
|
|
// this identifies a read or write operation so that readwritev() knows
|
|
|
|
// what to do when it's actually touching the file
|
|
|
|
struct fileop
|
|
|
|
{
|
|
|
|
virtual int file_op(int file_index, boost::int64_t file_offset, int size
|
|
|
|
, file::iovec_t const* bufs, storage_error& ec) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
// this function is responsible for turning read and write operations in the
|
|
|
|
// torrent space (pieces) into read and write operations in the filesystem
|
|
|
|
// space (files on disk).
|
|
|
|
TORRENT_EXTRA_EXPORT int readwritev(file_storage const& files
|
|
|
|
, file::iovec_t const* bufs, int piece, int offset, int num_bufs
|
|
|
|
, fileop& op, storage_error& ec);
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_STORAGE_HPP_INCLUDED
|
2003-11-26 15:11:25 +01:00
|
|
|
|