2010-10-10 04:22:57 +02:00
|
|
|
/*
|
|
|
|
|
2016-01-18 00:57:46 +01:00
|
|
|
Copyright (c) 2011-2016, Arvid Norberg
|
2010-10-10 04:22: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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/disk_io_job.hpp"
|
|
|
|
#include "libtorrent/block_cache.hpp" // for cached_piece_entry
|
2010-10-10 04:22:57 +02:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
2016-11-21 05:58:48 +01:00
|
|
|
namespace {
|
|
|
|
struct caller_visitor : boost::static_visitor<>
|
|
|
|
{
|
|
|
|
explicit caller_visitor(disk_io_job& j) : m_job(j) {}
|
|
|
|
|
|
|
|
void operator()(disk_io_job::read_handler& h) const
|
|
|
|
{
|
|
|
|
if (!h) return;
|
|
|
|
h(m_job.d.io.ref, m_job.buffer.disk_block, m_job.flags, m_job.error);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(disk_io_job::write_handler& h) const
|
|
|
|
{
|
|
|
|
if (!h) return;
|
|
|
|
h(m_job.error);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(disk_io_job::hash_handler& h) const
|
|
|
|
{
|
|
|
|
if (!h) return;
|
2016-11-26 07:51:47 +01:00
|
|
|
h(m_job.piece, sha1_hash(m_job.d.piece_hash), m_job.error);
|
2016-11-21 05:58:48 +01:00
|
|
|
}
|
|
|
|
|
2016-11-22 07:48:14 +01:00
|
|
|
void operator()(disk_io_job::move_handler& h) const
|
|
|
|
{
|
|
|
|
if (!h) return;
|
|
|
|
h(m_job.ret, std::string(m_job.buffer.string), m_job.error);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(disk_io_job::release_handler& h) const
|
|
|
|
{
|
|
|
|
if (!h) return;
|
|
|
|
h();
|
|
|
|
}
|
|
|
|
|
2016-11-23 01:03:27 +01:00
|
|
|
void operator()(disk_io_job::check_handler& h) const
|
|
|
|
{
|
|
|
|
if (!h) return;
|
|
|
|
h(m_job.ret, m_job.error);
|
|
|
|
}
|
|
|
|
|
2016-11-23 07:43:57 +01:00
|
|
|
void operator()(disk_io_job::rename_handler& h) const
|
2016-11-21 05:58:48 +01:00
|
|
|
{
|
|
|
|
if (!h) return;
|
2016-11-23 07:43:57 +01:00
|
|
|
h(m_job.buffer.string, m_job.piece, m_job.error);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(disk_io_job::clear_piece_handler& h) const
|
|
|
|
{
|
|
|
|
if (!h) return;
|
|
|
|
h(m_job.piece);
|
2016-11-21 05:58:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
disk_io_job& m_job;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
disk_io_job::disk_io_job()
|
2016-10-27 02:41:57 +02:00
|
|
|
: piece(0)
|
2014-07-06 21:18:00 +02:00
|
|
|
, action(read)
|
|
|
|
{
|
2016-07-09 22:26:26 +02:00
|
|
|
buffer.disk_block = nullptr;
|
2014-07-06 21:18:00 +02:00
|
|
|
d.io.offset = 0;
|
|
|
|
d.io.buffer_size = 0;
|
2016-07-09 22:26:26 +02:00
|
|
|
d.io.ref.storage = nullptr;
|
2014-07-06 21:18:00 +02:00
|
|
|
d.io.ref.piece = 0;
|
|
|
|
d.io.ref.block = 0;
|
|
|
|
}
|
2010-10-10 04:22:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
disk_io_job::~disk_io_job()
|
2013-11-29 06:29:39 +01:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
if (action == rename_file || action == move_storage)
|
2015-05-16 22:41:37 +02:00
|
|
|
free(buffer.string);
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
2010-10-10 04:22:57 +02:00
|
|
|
|
2016-11-21 05:58:48 +01:00
|
|
|
void disk_io_job::call_callback()
|
|
|
|
{
|
|
|
|
boost::apply_visitor(caller_visitor(*this), callback);
|
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
bool disk_io_job::completed(cached_piece_entry const* pe, int block_size)
|
2010-10-10 04:22:57 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
if (action != write) return false;
|
2010-10-10 04:22:57 +02:00
|
|
|
|
2016-08-29 14:31:23 +02:00
|
|
|
int block_offset = d.io.offset & (block_size - 1);
|
2014-07-06 21:18:00 +02:00
|
|
|
int size = d.io.buffer_size;
|
|
|
|
int start = d.io.offset / block_size;
|
|
|
|
int end = block_offset > 0 && (size > block_size - block_offset) ? start + 2 : start + 1;
|
2015-05-16 22:41:37 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
for (int i = start; i < end; ++i)
|
|
|
|
if (pe->blocks[i].dirty || pe->blocks[i].pending) return false;
|
2010-10-10 04:22:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// if all our blocks are not pending and not dirty, it means they
|
|
|
|
// were successfully written to disk. This job is complete
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|