2008-07-01 20:59:13 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2008, Arvid Norberg
|
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2015-04-24 05:48:08 +02:00
|
|
|
#include "test.hpp"
|
|
|
|
#include "setup_transfer.hpp"
|
|
|
|
#include "test_utils.hpp"
|
2016-02-16 07:43:06 +01:00
|
|
|
#include "settings.hpp"
|
2015-04-24 05:48:08 +02:00
|
|
|
|
2005-09-11 11:48:05 +02:00
|
|
|
#include "libtorrent/storage.hpp"
|
2006-11-19 16:25:02 +01:00
|
|
|
#include "libtorrent/file_pool.hpp"
|
2005-09-11 11:48:05 +02:00
|
|
|
#include "libtorrent/hasher.hpp"
|
|
|
|
#include "libtorrent/session.hpp"
|
2008-02-25 06:07:29 +01:00
|
|
|
#include "libtorrent/alert_types.hpp"
|
2008-05-14 07:29:42 +02:00
|
|
|
#include "libtorrent/create_torrent.hpp"
|
2015-09-18 06:23:45 +02:00
|
|
|
#include "libtorrent/torrent_info.hpp"
|
2016-02-16 07:43:06 +01:00
|
|
|
#include "libtorrent/read_resume_data.hpp"
|
2017-03-09 00:01:59 +01:00
|
|
|
#include "libtorrent/write_resume_data.hpp"
|
2017-04-11 06:52:46 +02:00
|
|
|
#include "libtorrent/aux_/path.hpp"
|
2017-06-21 04:44:57 +02:00
|
|
|
#include "libtorrent/aux_/storage_utils.hpp"
|
2005-09-11 11:48:05 +02:00
|
|
|
|
2017-10-10 14:25:12 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <functional> // for bind
|
2005-09-11 11:48:05 +02:00
|
|
|
|
2009-12-15 14:11:07 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
2005-09-11 11:48:05 +02:00
|
|
|
|
2017-04-16 22:37:39 +02:00
|
|
|
#include <boost/variant/get.hpp>
|
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
using namespace std::placeholders;
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2005-09-11 11:48:05 +02:00
|
|
|
|
2018-01-23 19:23:57 +01:00
|
|
|
namespace {
|
|
|
|
|
2018-11-01 23:05:30 +01:00
|
|
|
constexpr int piece_size = 16 * 1024 * 16;
|
|
|
|
constexpr int half = piece_size / 2;
|
2008-05-14 07:29:42 +02:00
|
|
|
|
2016-11-26 07:51:47 +01:00
|
|
|
void on_check_resume_data(status_t const status, storage_error const& error, bool* done)
|
2008-03-14 11:17:27 +01:00
|
|
|
{
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << time_now_string() << " on_check_resume_data ret: "
|
2016-11-26 07:51:47 +01:00
|
|
|
<< static_cast<int>(status);
|
2016-11-23 01:03:27 +01:00
|
|
|
switch (status)
|
2008-07-17 17:08:29 +02:00
|
|
|
{
|
2016-11-26 07:51:47 +01:00
|
|
|
case status_t::no_error:
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << time_now_string() << " success" << std::endl;
|
2010-08-08 09:53:47 +02:00
|
|
|
break;
|
2016-11-26 07:51:47 +01:00
|
|
|
case status_t::fatal_disk_error:
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << time_now_string() << " disk error: " << error.ec.message()
|
2016-12-22 16:42:33 +01:00
|
|
|
<< " file: " << error.file() << std::endl;
|
2010-08-08 09:53:47 +02:00
|
|
|
break;
|
2016-11-26 07:51:47 +01:00
|
|
|
case status_t::need_full_check:
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << time_now_string() << " need full check" << std::endl;
|
2010-08-08 09:53:47 +02:00
|
|
|
break;
|
2016-11-26 07:51:47 +01:00
|
|
|
case status_t::file_exist:
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << time_now_string() << " file exist" << std::endl;
|
2010-08-08 09:53:47 +02:00
|
|
|
break;
|
2008-07-17 17:08:29 +02:00
|
|
|
}
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << std::endl;
|
2009-05-13 19:17:33 +02:00
|
|
|
*done = true;
|
2008-03-14 11:17:27 +01:00
|
|
|
}
|
|
|
|
|
2018-04-21 18:46:54 +02:00
|
|
|
void on_piece_checked(piece_index_t, sha1_hash const&
|
|
|
|
, storage_error const& error, bool* done)
|
|
|
|
{
|
|
|
|
std::cout << time_now_string() << " on_piece_checked err: "
|
|
|
|
<< error.ec.message() << '\n';
|
2009-05-13 19:17:33 +02:00
|
|
|
*done = true;
|
2008-03-14 11:17:27 +01:00
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
void print_error(char const* call, int ret, storage_error const& ec)
|
2008-03-14 11:17:27 +01:00
|
|
|
{
|
2017-06-18 00:18:19 +02:00
|
|
|
std::printf("%s: %s() returned: %d error: \"%s\" in file: %d operation: %s\n"
|
2016-12-22 16:42:33 +01:00
|
|
|
, time_now_string(), call, ret, ec.ec.message().c_str()
|
2017-06-18 00:18:19 +02:00
|
|
|
, static_cast<int>(ec.file()), operation_name(ec.operation));
|
2009-09-05 09:21:10 +02:00
|
|
|
}
|
|
|
|
|
2010-08-08 09:53:47 +02:00
|
|
|
void run_until(io_service& ios, bool const& done)
|
|
|
|
{
|
|
|
|
while (!done)
|
|
|
|
{
|
|
|
|
ios.reset();
|
|
|
|
error_code ec;
|
|
|
|
ios.run_one(ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "run_one: " << ec.message().c_str() << std::endl;
|
2010-08-08 09:53:47 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << time_now_string() << " done: " << done << std::endl;
|
2010-08-08 09:53:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
void nop() {}
|
|
|
|
|
2016-11-10 01:53:55 +01:00
|
|
|
std::shared_ptr<torrent_info> setup_torrent_info(file_storage& fs
|
2016-11-05 06:38:55 +01:00
|
|
|
, std::vector<char>& buf)
|
2015-01-16 05:02:44 +01:00
|
|
|
{
|
2016-03-16 01:41:12 +01:00
|
|
|
fs.add_file(combine_path("temp_storage", "test1.tmp"), 8);
|
|
|
|
fs.add_file(combine_path("temp_storage", combine_path("folder1", "test2.tmp")), 8);
|
|
|
|
fs.add_file(combine_path("temp_storage", combine_path("folder2", "test3.tmp")), 0);
|
|
|
|
fs.add_file(combine_path("temp_storage", combine_path("_folder3", "test4.tmp")), 0);
|
|
|
|
fs.add_file(combine_path("temp_storage", combine_path("_folder3", combine_path("subfolder", "test5.tmp"))), 8);
|
2017-08-03 12:02:03 +02:00
|
|
|
lt::create_torrent t(fs, 4, -1, {});
|
2015-01-16 05:02:44 +01:00
|
|
|
|
|
|
|
char buf_[4] = {0, 0, 0, 0};
|
2016-07-22 16:29:39 +02:00
|
|
|
sha1_hash h = hasher(buf_).final();
|
2016-12-22 16:42:33 +01:00
|
|
|
for (piece_index_t i(0); i < piece_index_t(6); ++i) t.set_hash(i, h);
|
2015-06-21 04:34:44 +02:00
|
|
|
|
2015-01-16 05:02:44 +01:00
|
|
|
bencode(std::back_inserter(buf), t.generate());
|
|
|
|
error_code ec;
|
|
|
|
|
2017-07-20 09:27:47 +02:00
|
|
|
auto info = std::make_shared<torrent_info>(buf, ec, from_span);
|
2015-01-16 05:02:44 +01:00
|
|
|
|
|
|
|
if (ec)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("torrent_info constructor failed: %s\n"
|
2015-01-16 05:02:44 +01:00
|
|
|
, ec.message().c_str());
|
|
|
|
}
|
|
|
|
|
2016-11-05 06:38:55 +01:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2016-11-10 01:53:55 +01:00
|
|
|
std::shared_ptr<default_storage> setup_torrent(file_storage& fs
|
2016-11-05 06:38:55 +01:00
|
|
|
, file_pool& fp
|
|
|
|
, std::vector<char>& buf
|
|
|
|
, std::string const& test_path
|
|
|
|
, aux::session_settings& set)
|
|
|
|
{
|
2016-11-10 01:53:55 +01:00
|
|
|
std::shared_ptr<torrent_info> info = setup_torrent_info(fs, buf);
|
2016-11-05 06:38:55 +01:00
|
|
|
|
2017-10-29 00:44:40 +02:00
|
|
|
aux::vector<download_priority_t, file_index_t> priorities;
|
2017-06-20 04:29:04 +02:00
|
|
|
sha1_hash info_hash;
|
|
|
|
storage_params p{
|
|
|
|
fs,
|
|
|
|
nullptr,
|
|
|
|
test_path,
|
|
|
|
storage_mode_allocate,
|
|
|
|
priorities,
|
|
|
|
info_hash
|
|
|
|
};
|
2017-04-07 00:11:24 +02:00
|
|
|
std::shared_ptr<default_storage> s(new default_storage(p, fp));
|
2015-01-16 05:02:44 +01:00
|
|
|
s->m_settings = &set;
|
|
|
|
|
|
|
|
// allocate the files and create the directories
|
|
|
|
storage_error se;
|
|
|
|
s->initialize(se);
|
|
|
|
if (se)
|
|
|
|
{
|
|
|
|
TEST_ERROR(se.ec.message().c_str());
|
2016-12-22 16:42:33 +01:00
|
|
|
std::printf("default_storage::initialize %s: %d\n"
|
|
|
|
, se.ec.message().c_str(), static_cast<int>(se.file()));
|
2015-01-16 05:02:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-04-29 06:27:55 +02:00
|
|
|
std::vector<char> new_piece(std::size_t const size)
|
2015-11-16 08:30:14 +01:00
|
|
|
{
|
2016-07-22 16:29:39 +02:00
|
|
|
std::vector<char> ret(size);
|
|
|
|
std::generate(ret.begin(), ret.end(), random_byte);
|
2015-11-16 08:30:14 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-08-17 23:26:35 +02:00
|
|
|
void run_storage_tests(std::shared_ptr<torrent_info> info
|
2008-05-28 10:44:40 +02:00
|
|
|
, file_storage& fs
|
2009-10-26 02:29:39 +01:00
|
|
|
, std::string const& test_path
|
2017-04-12 20:05:53 +02:00
|
|
|
, lt::storage_mode_t storage_mode
|
2009-01-21 08:31:49 +01:00
|
|
|
, bool unbuffered)
|
2005-09-11 11:48:05 +02:00
|
|
|
{
|
2008-05-28 10:44:40 +02:00
|
|
|
TORRENT_ASSERT(fs.num_files() > 0);
|
2018-01-29 12:40:44 +01:00
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
error_code ec;
|
|
|
|
create_directory(combine_path(test_path, "temp_storage"), ec);
|
2017-01-02 16:16:33 +01:00
|
|
|
if (ec) std::cout << "create_directory '" << combine_path(test_path, "temp_storage")
|
2010-10-18 09:15:57 +02:00
|
|
|
<< "': " << ec.message() << std::endl;
|
2009-10-26 02:29:39 +01:00
|
|
|
remove_all(combine_path(test_path, "temp_storage2"), ec);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ec && ec != boost::system::errc::no_such_file_or_directory)
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "remove_all '" << combine_path(test_path, "temp_storage2")
|
2010-10-18 09:15:57 +02:00
|
|
|
<< "': " << ec.message() << std::endl;
|
2009-10-26 02:29:39 +01:00
|
|
|
remove_all(combine_path(test_path, "part0"), ec);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ec && ec != boost::system::errc::no_such_file_or_directory)
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "remove_all '" << combine_path(test_path, "part0")
|
2010-10-18 09:15:57 +02:00
|
|
|
<< "': " << ec.message() << std::endl;
|
2018-01-29 12:40:44 +01:00
|
|
|
}
|
2009-01-21 08:31:49 +01:00
|
|
|
int num_pieces = fs.num_pieces();
|
2007-09-01 05:00:31 +02:00
|
|
|
TEST_CHECK(info->num_pieces() == num_pieces);
|
2005-09-28 23:46:35 +02:00
|
|
|
|
2016-07-22 16:29:39 +02:00
|
|
|
std::vector<char> piece0 = new_piece(piece_size);
|
|
|
|
std::vector<char> piece1 = new_piece(piece_size);
|
|
|
|
std::vector<char> piece2 = new_piece(piece_size);
|
2015-11-16 08:30:14 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
aux::session_settings set;
|
|
|
|
set.set_int(settings_pack::disk_io_write_mode
|
|
|
|
, unbuffered ? settings_pack::disable_os_cache
|
|
|
|
: settings_pack::enable_os_cache);
|
|
|
|
set.set_int(settings_pack::disk_io_read_mode
|
|
|
|
, unbuffered ? settings_pack::disable_os_cache
|
|
|
|
: settings_pack::enable_os_cache);
|
2009-01-21 08:31:49 +01:00
|
|
|
|
2016-03-20 22:00:49 +01:00
|
|
|
char* piece = static_cast<char*>(malloc(piece_size));
|
2005-09-28 23:46:35 +02:00
|
|
|
|
2015-06-06 07:22:53 +02:00
|
|
|
{ // avoid having two storages use the same files
|
2006-11-19 16:25:02 +01:00
|
|
|
file_pool fp;
|
2015-06-06 07:22:53 +02:00
|
|
|
boost::asio::io_service ios;
|
2018-01-03 12:54:03 +01:00
|
|
|
disk_buffer_pool dp(ios, std::bind(&nop));
|
2017-06-20 04:29:04 +02:00
|
|
|
|
2017-10-29 00:44:40 +02:00
|
|
|
aux::vector<download_priority_t, file_index_t> priorities;
|
2017-06-20 04:29:04 +02:00
|
|
|
sha1_hash info_hash;
|
|
|
|
storage_params p{
|
|
|
|
fs,
|
|
|
|
nullptr,
|
|
|
|
test_path,
|
|
|
|
storage_mode,
|
|
|
|
priorities,
|
|
|
|
info_hash
|
|
|
|
};
|
2017-04-07 00:11:24 +02:00
|
|
|
std::unique_ptr<storage_interface> s(new default_storage(p, fp));
|
2009-01-21 08:31:49 +01:00
|
|
|
s->m_settings = &set;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
storage_error ec;
|
|
|
|
s->initialize(ec);
|
|
|
|
TEST_CHECK(!ec);
|
|
|
|
if (ec) print_error("initialize", 0, ec);
|
2005-09-11 11:48:05 +02:00
|
|
|
|
2009-02-17 01:11:38 +01:00
|
|
|
int ret = 0;
|
|
|
|
|
2005-09-11 11:48:05 +02:00
|
|
|
// write piece 1 (in slot 0)
|
2017-01-11 06:42:10 +01:00
|
|
|
iovec_t iov = { piece1.data(), half};
|
2017-07-15 02:59:20 +02:00
|
|
|
ret = s->writev(iov, piece_index_t(0), 0, open_mode::read_write, ec);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ret != half) print_error("writev", ret, ec);
|
|
|
|
|
2017-04-29 06:27:55 +02:00
|
|
|
iov = { piece1.data() + half, half };
|
2017-07-15 02:59:20 +02:00
|
|
|
ret = s->writev(iov, piece_index_t(0), half, open_mode::read_write, ec);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ret != half) print_error("writev", ret, ec);
|
2005-09-11 11:48:05 +02:00
|
|
|
|
2009-01-21 08:31:49 +01:00
|
|
|
// test unaligned read (where the bytes are aligned)
|
2017-04-29 06:27:55 +02:00
|
|
|
iov = { piece + 3, piece_size - 9};
|
2017-07-15 02:59:20 +02:00
|
|
|
ret = s->readv(iov, piece_index_t(0), 3, open_mode::read_write, ec);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ret != piece_size - 9) print_error("readv",ret, ec);
|
2016-07-22 16:29:39 +02:00
|
|
|
TEST_CHECK(std::equal(piece+3, piece + piece_size-9, piece1.data()+3));
|
2015-06-06 07:22:53 +02:00
|
|
|
|
2009-01-21 08:31:49 +01:00
|
|
|
// test unaligned read (where the bytes are not aligned)
|
2017-04-29 06:27:55 +02:00
|
|
|
iov = { piece, piece_size - 9};
|
2017-07-15 02:59:20 +02:00
|
|
|
ret = s->readv(iov, piece_index_t(0), 3, open_mode::read_write, ec);
|
2011-11-13 05:12:56 +01:00
|
|
|
TEST_CHECK(ret == piece_size - 9);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ret != piece_size - 9) print_error("readv", ret, ec);
|
2016-07-22 16:29:39 +02:00
|
|
|
TEST_CHECK(std::equal(piece, piece + piece_size-9, piece1.data()+3));
|
2009-01-21 08:31:49 +01:00
|
|
|
|
2005-09-11 11:48:05 +02:00
|
|
|
// verify piece 1
|
2017-04-29 06:27:55 +02:00
|
|
|
iov = { piece, piece_size };
|
2017-07-15 02:59:20 +02:00
|
|
|
ret = s->readv(iov, piece_index_t(0), 0, open_mode::read_write, ec);
|
2011-11-13 05:12:56 +01:00
|
|
|
TEST_CHECK(ret == piece_size);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ret != piece_size) print_error("readv", ret, ec);
|
2016-07-22 16:29:39 +02:00
|
|
|
TEST_CHECK(std::equal(piece, piece + piece_size, piece1.data()));
|
2015-06-21 04:34:44 +02:00
|
|
|
|
2005-09-11 11:48:05 +02:00
|
|
|
// do the same with piece 0 and 2 (in slot 1 and 2)
|
2017-04-29 06:27:55 +02:00
|
|
|
iov = { piece0.data(), piece_size };
|
2017-07-15 02:59:20 +02:00
|
|
|
ret = s->writev(iov, piece_index_t(1), 0, open_mode::read_write, ec);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ret != piece_size) print_error("writev", ret, ec);
|
|
|
|
|
2017-04-29 06:27:55 +02:00
|
|
|
iov = { piece2.data(), piece_size };
|
2017-07-15 02:59:20 +02:00
|
|
|
ret = s->writev(iov, piece_index_t(2), 0, open_mode::read_write, ec);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ret != piece_size) print_error("writev", ret, ec);
|
2005-09-11 11:48:05 +02:00
|
|
|
|
|
|
|
// verify piece 0 and 2
|
2017-04-29 06:27:55 +02:00
|
|
|
iov = { piece, piece_size };
|
2017-07-15 02:59:20 +02:00
|
|
|
ret = s->readv(iov, piece_index_t(1), 0, open_mode::read_write, ec);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ret != piece_size) print_error("readv", ret, ec);
|
2016-07-22 16:29:39 +02:00
|
|
|
TEST_CHECK(std::equal(piece, piece + piece_size, piece0.data()));
|
2005-09-11 11:48:05 +02:00
|
|
|
|
2017-04-29 06:27:55 +02:00
|
|
|
iov = { piece, piece_size };
|
2017-07-15 02:59:20 +02:00
|
|
|
ret = s->readv(iov, piece_index_t(2), 0, open_mode::read_write, ec);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ret != piece_size) print_error("readv", ret, ec);
|
2016-07-22 16:29:39 +02:00
|
|
|
TEST_CHECK(std::equal(piece, piece + piece_size, piece2.data()));
|
2009-01-21 08:31:49 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
s->release_files(ec);
|
2005-09-28 23:46:35 +02:00
|
|
|
}
|
2005-09-11 11:48:05 +02:00
|
|
|
|
2016-03-20 22:00:49 +01:00
|
|
|
free(piece);
|
2007-02-25 22:11:29 +01:00
|
|
|
}
|
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
void test_remove(std::string const& test_path, bool unbuffered)
|
2007-10-21 02:19:37 +02:00
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
error_code ec;
|
|
|
|
remove_all(combine_path(test_path, "temp_storage"), ec);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ec && ec != boost::system::errc::no_such_file_or_directory)
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "remove_all '" << combine_path(test_path, "temp_storage")
|
2010-10-18 09:15:57 +02:00
|
|
|
<< "': " << ec.message() << std::endl;
|
2015-06-21 04:34:44 +02:00
|
|
|
TEST_CHECK(!exists(combine_path(test_path, "temp_storage")));
|
2007-10-21 02:19:37 +02:00
|
|
|
|
2015-01-16 05:02:44 +01:00
|
|
|
file_storage fs;
|
2010-03-05 07:27:23 +01:00
|
|
|
std::vector<char> buf;
|
2015-01-16 05:02:44 +01:00
|
|
|
file_pool fp;
|
|
|
|
io_service ios;
|
2018-01-03 12:54:03 +01:00
|
|
|
disk_buffer_pool dp(ios, std::bind(&nop));
|
2007-10-21 02:19:37 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
aux::session_settings set;
|
|
|
|
set.set_int(settings_pack::disk_io_write_mode
|
|
|
|
, unbuffered ? settings_pack::disable_os_cache
|
|
|
|
: settings_pack::enable_os_cache);
|
|
|
|
set.set_int(settings_pack::disk_io_read_mode
|
|
|
|
, unbuffered ? settings_pack::disable_os_cache
|
|
|
|
: settings_pack::enable_os_cache);
|
2009-01-21 08:31:49 +01:00
|
|
|
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<default_storage> s = setup_torrent(fs, fp, buf, test_path, set);
|
2007-10-21 02:19:37 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// directories are not created up-front, unless they contain
|
|
|
|
// an empty file (all of which are created up-front, along with
|
|
|
|
// all required directories)
|
2013-12-30 03:50:29 +01:00
|
|
|
// files are created on first write
|
|
|
|
TEST_CHECK(!exists(combine_path(test_path, combine_path("temp_storage"
|
2015-06-21 04:34:44 +02:00
|
|
|
, combine_path("_folder3", combine_path("subfolder", "test5.tmp"))))));
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// this directory and file is created up-front because it's an empty file
|
2013-12-30 03:50:29 +01:00
|
|
|
TEST_CHECK(exists(combine_path(test_path, combine_path("temp_storage"
|
2015-06-21 04:34:44 +02:00
|
|
|
, combine_path("folder2", "test3.tmp")))));
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// this isn't
|
2013-12-30 03:50:29 +01:00
|
|
|
TEST_CHECK(!exists(combine_path(test_path, combine_path("temp_storage"
|
2015-06-21 04:34:44 +02:00
|
|
|
, combine_path("folder1", "test2.tmp")))));
|
2013-12-30 03:50:29 +01:00
|
|
|
|
2017-01-11 06:42:10 +01:00
|
|
|
iovec_t b = {&buf[0], 4};
|
2015-01-16 05:02:44 +01:00
|
|
|
storage_error se;
|
2017-07-15 02:59:20 +02:00
|
|
|
s->writev(b, piece_index_t(2), 0, open_mode::read_write, se);
|
2013-12-30 03:50:29 +01:00
|
|
|
|
|
|
|
TEST_CHECK(exists(combine_path(test_path, combine_path("temp_storage"
|
2015-06-21 04:34:44 +02:00
|
|
|
, combine_path("folder1", "test2.tmp")))));
|
2013-12-30 03:50:29 +01:00
|
|
|
TEST_CHECK(!exists(combine_path(test_path, combine_path("temp_storage"
|
2015-06-21 04:34:44 +02:00
|
|
|
, combine_path("_folder3", combine_path("subfolder", "test5.tmp"))))));
|
2013-12-30 03:50:29 +01:00
|
|
|
file_status st;
|
|
|
|
stat_file(combine_path(test_path, combine_path("temp_storage"
|
|
|
|
, combine_path("folder1", "test2.tmp"))), &st, ec);
|
|
|
|
TEST_EQUAL(st.file_size, 8);
|
|
|
|
|
2017-07-15 02:59:20 +02:00
|
|
|
s->writev(b, piece_index_t(4), 0, open_mode::read_write, se);
|
2013-12-30 03:50:29 +01:00
|
|
|
|
|
|
|
TEST_CHECK(exists(combine_path(test_path, combine_path("temp_storage"
|
2015-06-21 04:34:44 +02:00
|
|
|
, combine_path("_folder3", combine_path("subfolder", "test5.tmp"))))));
|
2013-12-30 03:50:29 +01:00
|
|
|
stat_file(combine_path(test_path, combine_path("temp_storage"
|
|
|
|
, combine_path("_folder3", "test5.tmp"))), &st, ec);
|
|
|
|
TEST_EQUAL(st.file_size, 8);
|
2007-10-21 02:19:37 +02:00
|
|
|
|
2016-03-13 08:50:37 +01:00
|
|
|
s->delete_files(session::delete_files, se);
|
2016-10-08 20:07:11 +02:00
|
|
|
if (se) print_error("delete_files", 0, se);
|
2007-10-21 02:19:37 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (se)
|
2013-06-24 05:26:17 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
TEST_ERROR(se.ec.message().c_str());
|
2016-12-22 16:42:33 +01:00
|
|
|
std::printf("default_storage::delete_files %s: %d\n"
|
|
|
|
, se.ec.message().c_str(), static_cast<int>(se.file()));
|
2013-06-24 05:26:17 +02:00
|
|
|
}
|
|
|
|
|
2015-06-21 04:34:44 +02:00
|
|
|
TEST_CHECK(!exists(combine_path(test_path, "temp_storage")));
|
2007-10-21 02:19:37 +02:00
|
|
|
}
|
|
|
|
|
2015-01-16 05:02:44 +01:00
|
|
|
void test_rename(std::string const& test_path)
|
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
remove_all(combine_path(test_path, "temp_storage"), ec);
|
|
|
|
if (ec && ec != boost::system::errc::no_such_file_or_directory)
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "remove_all '" << combine_path(test_path, "temp_storage")
|
2015-01-16 05:02:44 +01:00
|
|
|
<< "': " << ec.message() << std::endl;
|
2015-06-21 04:34:44 +02:00
|
|
|
TEST_CHECK(!exists(combine_path(test_path, "temp_storage")));
|
2015-01-16 05:02:44 +01:00
|
|
|
|
|
|
|
file_storage fs;
|
|
|
|
std::vector<char> buf;
|
|
|
|
file_pool fp;
|
|
|
|
io_service ios;
|
2018-01-03 12:54:03 +01:00
|
|
|
disk_buffer_pool dp(ios, std::bind(&nop));
|
2015-01-16 05:02:44 +01:00
|
|
|
aux::session_settings set;
|
|
|
|
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<default_storage> s = setup_torrent(fs, fp, buf, test_path
|
2015-01-16 05:02:44 +01:00
|
|
|
, set);
|
|
|
|
|
|
|
|
// directories are not created up-front, unless they contain
|
|
|
|
// an empty file
|
2016-12-22 16:42:33 +01:00
|
|
|
std::string first_file = fs.file_path(file_index_t(0));
|
2018-06-02 19:29:40 +02:00
|
|
|
for (auto const i : fs.file_range())
|
2015-01-16 05:02:44 +01:00
|
|
|
{
|
|
|
|
TEST_CHECK(!exists(combine_path(test_path, combine_path("temp_storage"
|
2015-06-21 04:34:44 +02:00
|
|
|
, fs.file_path(i)))));
|
2015-01-16 05:02:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
storage_error se;
|
2016-12-22 16:42:33 +01:00
|
|
|
s->rename_file(file_index_t(0), "new_filename", se);
|
2015-01-16 05:02:44 +01:00
|
|
|
if (se.ec)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("default_storage::rename_file failed: %s\n"
|
2015-01-16 05:02:44 +01:00
|
|
|
, se.ec.message().c_str());
|
|
|
|
}
|
|
|
|
TEST_CHECK(!se.ec);
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(s->files().file_path(file_index_t(0)), "new_filename");
|
2015-01-16 05:02:44 +01:00
|
|
|
}
|
|
|
|
|
2018-04-21 18:35:55 +02:00
|
|
|
struct test_error_storage : default_storage
|
|
|
|
{
|
2018-04-21 18:46:54 +02:00
|
|
|
explicit test_error_storage(storage_params const& params, file_pool& fp)
|
|
|
|
: default_storage(params, fp) {}
|
2018-04-21 18:35:55 +02:00
|
|
|
|
2018-04-21 18:46:54 +02:00
|
|
|
bool has_any_file(storage_error& ec) override
|
2018-04-21 18:35:55 +02:00
|
|
|
{
|
|
|
|
ec.ec = make_error_code(boost::system::errc::permission_denied);
|
2018-04-21 18:46:54 +02:00
|
|
|
ec.operation = operation_t::file_stat;
|
2018-04-21 18:35:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-21 18:46:54 +02:00
|
|
|
storage_interface* error_storage_constructor(
|
|
|
|
storage_params const& params, file_pool& fp)
|
|
|
|
{
|
|
|
|
return new test_error_storage(params, fp);
|
|
|
|
}
|
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
void test_check_files(std::string const& test_path
|
2018-04-21 18:46:54 +02:00
|
|
|
, lt::storage_mode_t storage_mode)
|
2008-08-20 10:07:31 +02:00
|
|
|
{
|
2016-08-17 23:26:35 +02:00
|
|
|
std::shared_ptr<torrent_info> info;
|
2008-08-20 10:07:31 +02:00
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
error_code ec;
|
2018-01-29 15:58:22 +01:00
|
|
|
constexpr int piece_size_check = 16 * 1024;
|
2009-10-26 02:29:39 +01:00
|
|
|
remove_all(combine_path(test_path, "temp_storage"), ec);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ec && ec != boost::system::errc::no_such_file_or_directory)
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "remove_all '" << combine_path(test_path, "temp_storage")
|
2010-10-18 09:15:57 +02:00
|
|
|
<< "': " << ec.message() << std::endl;
|
2008-08-20 10:07:31 +02:00
|
|
|
file_storage fs;
|
2018-01-29 15:58:22 +01:00
|
|
|
fs.add_file("temp_storage/test1.tmp", piece_size_check);
|
|
|
|
fs.add_file("temp_storage/test2.tmp", piece_size_check * 2);
|
|
|
|
fs.add_file("temp_storage/test3.tmp", piece_size_check);
|
2008-08-20 10:07:31 +02:00
|
|
|
|
2018-01-29 15:58:22 +01:00
|
|
|
std::vector<char> piece0 = new_piece(piece_size_check);
|
|
|
|
std::vector<char> piece2 = new_piece(piece_size_check);
|
2008-08-20 10:07:31 +02:00
|
|
|
|
2018-01-29 15:58:22 +01:00
|
|
|
lt::create_torrent t(fs, piece_size_check, -1, {});
|
2016-12-22 16:42:33 +01:00
|
|
|
t.set_hash(piece_index_t(0), hasher(piece0).final());
|
2018-01-29 15:58:22 +01:00
|
|
|
t.set_hash(piece_index_t(1), {});
|
|
|
|
t.set_hash(piece_index_t(2), {});
|
2016-12-22 16:42:33 +01:00
|
|
|
t.set_hash(piece_index_t(3), hasher(piece2).final());
|
2008-08-20 10:07:31 +02:00
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
create_directory(combine_path(test_path, "temp_storage"), ec);
|
2017-01-02 16:16:33 +01:00
|
|
|
if (ec) std::cout << "create_directory: " << ec.message() << std::endl;
|
2008-08-20 10:07:31 +02:00
|
|
|
|
|
|
|
std::ofstream f;
|
2013-02-09 23:02:05 +01:00
|
|
|
f.open(combine_path(test_path, combine_path("temp_storage", "test1.tmp")).c_str()
|
2009-10-26 02:29:39 +01:00
|
|
|
, std::ios::trunc | std::ios::binary);
|
2018-01-29 15:58:22 +01:00
|
|
|
f.write(piece0.data(), piece_size_check);
|
2008-08-20 10:07:31 +02:00
|
|
|
f.close();
|
2013-02-09 23:02:05 +01:00
|
|
|
f.open(combine_path(test_path, combine_path("temp_storage", "test3.tmp")).c_str()
|
2009-10-26 02:29:39 +01:00
|
|
|
, std::ios::trunc | std::ios::binary);
|
2018-01-29 15:58:22 +01:00
|
|
|
f.write(piece2.data(), piece_size_check);
|
2008-08-20 10:07:31 +02:00
|
|
|
f.close();
|
|
|
|
|
2010-03-05 07:27:23 +01:00
|
|
|
std::vector<char> buf;
|
|
|
|
bencode(std::back_inserter(buf), t.generate());
|
2017-07-20 09:27:47 +02:00
|
|
|
info = std::make_shared<torrent_info>(buf, ec, from_span);
|
2008-08-20 10:07:31 +02:00
|
|
|
|
|
|
|
file_pool fp;
|
2015-06-06 07:22:53 +02:00
|
|
|
boost::asio::io_service ios;
|
2014-08-01 08:07:48 +02:00
|
|
|
counters cnt;
|
2018-04-21 18:46:54 +02:00
|
|
|
|
2016-10-30 23:21:07 +01:00
|
|
|
disk_io_thread io(ios, cnt);
|
2016-11-06 19:39:09 +01:00
|
|
|
settings_pack sett;
|
|
|
|
sett.set_int(settings_pack::aio_threads, 1);
|
2016-12-26 01:07:31 +01:00
|
|
|
io.set_settings(&sett);
|
2016-11-06 19:39:09 +01:00
|
|
|
|
2018-01-03 12:54:03 +01:00
|
|
|
disk_buffer_pool dp(ios, std::bind(&nop));
|
2017-06-20 04:29:04 +02:00
|
|
|
|
2018-04-21 18:46:54 +02:00
|
|
|
aux::vector<download_priority_t, file_index_t> priorities(
|
|
|
|
std::size_t(info->num_files()), download_priority_t{});
|
2017-06-20 04:29:04 +02:00
|
|
|
sha1_hash info_hash;
|
|
|
|
storage_params p{
|
|
|
|
fs,
|
|
|
|
nullptr,
|
|
|
|
test_path,
|
|
|
|
storage_mode,
|
|
|
|
priorities,
|
|
|
|
info_hash
|
|
|
|
};
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2018-04-21 18:46:54 +02:00
|
|
|
auto st = io.new_torrent(error_storage_constructor, std::move(p)
|
2017-04-07 00:11:24 +02:00
|
|
|
, std::shared_ptr<void>());
|
2008-08-20 10:07:31 +02:00
|
|
|
|
2009-05-13 19:17:33 +02:00
|
|
|
bool done = false;
|
2016-02-15 00:17:32 +01:00
|
|
|
add_torrent_params frd;
|
2016-12-22 16:42:33 +01:00
|
|
|
aux::vector<std::string, file_index_t> links;
|
2016-12-31 18:35:10 +01:00
|
|
|
io.async_check_files(st, &frd, links
|
2016-11-23 01:03:27 +01:00
|
|
|
, std::bind(&on_check_resume_data, _1, _2, &done));
|
2014-07-06 21:18:00 +02:00
|
|
|
io.submit_jobs();
|
2008-08-20 10:07:31 +02:00
|
|
|
ios.reset();
|
2010-08-23 07:51:12 +02:00
|
|
|
run_until(ios, done);
|
2008-08-20 10:07:31 +02:00
|
|
|
|
2018-06-02 19:29:40 +02:00
|
|
|
for (auto const i : info->piece_range())
|
2018-04-21 18:35:55 +02:00
|
|
|
{
|
|
|
|
done = false;
|
2018-04-21 18:46:54 +02:00
|
|
|
io.async_hash(st, i, disk_interface::sequential_access | disk_interface::volatile_read
|
|
|
|
, std::bind(&on_piece_checked, _1, _2, _3, &done));
|
2018-04-21 18:35:55 +02:00
|
|
|
io.submit_jobs();
|
2018-04-21 18:46:54 +02:00
|
|
|
ios.reset();
|
2018-04-21 18:35:55 +02:00
|
|
|
run_until(ios, done);
|
|
|
|
}
|
|
|
|
|
2016-06-16 02:49:28 +02:00
|
|
|
io.abort(true);
|
2008-08-20 10:07:31 +02:00
|
|
|
}
|
|
|
|
|
2015-11-16 04:36:55 +01:00
|
|
|
// TODO: 2 split this test up into smaller parts
|
2015-06-21 04:34:44 +02:00
|
|
|
void run_test(bool unbuffered)
|
2007-02-25 22:11:29 +01:00
|
|
|
{
|
2015-06-21 04:34:44 +02:00
|
|
|
std::string test_path = current_working_directory();
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "\n=== " << test_path << " ===\n" << std::endl;
|
2007-10-08 22:01:36 +02:00
|
|
|
|
2016-08-17 23:26:35 +02:00
|
|
|
std::shared_ptr<torrent_info> info;
|
2008-05-14 07:29:42 +02:00
|
|
|
|
2016-07-22 16:29:39 +02:00
|
|
|
std::vector<char> piece0 = new_piece(piece_size);
|
|
|
|
std::vector<char> piece1 = new_piece(piece_size);
|
|
|
|
std::vector<char> piece2 = new_piece(piece_size);
|
|
|
|
std::vector<char> piece3 = new_piece(piece_size);
|
2015-11-16 08:30:14 +01:00
|
|
|
|
2008-05-14 07:29:42 +02:00
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
error_code ec;
|
|
|
|
remove_all(combine_path(test_path, "temp_storage"), ec);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ec && ec != boost::system::errc::no_such_file_or_directory)
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "remove_all '" << combine_path(test_path, "temp_storage")
|
2010-10-18 09:15:57 +02:00
|
|
|
<< "': " << ec.message() << std::endl;
|
2008-05-28 10:44:40 +02:00
|
|
|
file_storage fs;
|
|
|
|
fs.add_file("temp_storage/test1.tmp", 17);
|
|
|
|
fs.add_file("temp_storage/test2.tmp", 612);
|
|
|
|
fs.add_file("temp_storage/test3.tmp", 0);
|
|
|
|
fs.add_file("temp_storage/test4.tmp", 0);
|
2009-01-21 08:31:49 +01:00
|
|
|
fs.add_file("temp_storage/test5.tmp", 3253);
|
|
|
|
fs.add_file("temp_storage/test6.tmp", 841);
|
2018-01-29 15:58:22 +01:00
|
|
|
int const last_file_size = 4 * int(piece_size) - int(fs.total_size());
|
2009-01-21 08:31:49 +01:00
|
|
|
fs.add_file("temp_storage/test7.tmp", last_file_size);
|
2008-05-28 10:44:40 +02:00
|
|
|
|
2013-08-01 02:21:56 +02:00
|
|
|
// File layout
|
|
|
|
// +-+--+++-------+-------+----------------------------------------------------------------------------------------+
|
|
|
|
// |1| 2||| file5 | file6 | file7 |
|
|
|
|
// +-+--+++-------+-------+----------------------------------------------------------------------------------------+
|
|
|
|
// | | | | |
|
|
|
|
// | piece 0 | piece 1 | piece 2 | piece 3 |
|
|
|
|
|
2017-08-03 12:02:03 +02:00
|
|
|
lt::create_torrent t(fs, piece_size, -1, {});
|
2013-08-01 03:02:26 +02:00
|
|
|
TEST_CHECK(t.num_pieces() == 4);
|
2016-12-22 16:42:33 +01:00
|
|
|
t.set_hash(piece_index_t(0), hasher(piece0).final());
|
|
|
|
t.set_hash(piece_index_t(1), hasher(piece1).final());
|
|
|
|
t.set_hash(piece_index_t(2), hasher(piece2).final());
|
|
|
|
t.set_hash(piece_index_t(3), hasher(piece3).final());
|
2015-06-21 04:34:44 +02:00
|
|
|
|
2010-03-05 07:27:23 +01:00
|
|
|
std::vector<char> buf;
|
|
|
|
bencode(std::back_inserter(buf), t.generate());
|
2017-07-20 09:27:47 +02:00
|
|
|
info = std::make_shared<torrent_info>(buf, from_span);
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "=== test 1 === " << (unbuffered?"unbuffered":"buffered") << std::endl;
|
2007-10-08 22:01:36 +02:00
|
|
|
|
2013-08-01 02:21:56 +02:00
|
|
|
// run_storage_tests writes piece 0, 1 and 2. not 3
|
2016-03-20 21:31:17 +01:00
|
|
|
run_storage_tests(info, fs, test_path, storage_mode_sparse, unbuffered);
|
2007-02-25 22:11:29 +01:00
|
|
|
|
|
|
|
// make sure the files have the correct size
|
2009-10-26 02:29:39 +01:00
|
|
|
std::string base = combine_path(test_path, "temp_storage");
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("base = \"%s\"\n", base.c_str());
|
2009-10-26 02:29:39 +01:00
|
|
|
TEST_EQUAL(file_size(combine_path(base, "test1.tmp")), 17);
|
|
|
|
TEST_EQUAL(file_size(combine_path(base, "test2.tmp")), 612);
|
2015-06-21 04:34:44 +02:00
|
|
|
|
2013-08-01 02:21:56 +02:00
|
|
|
// these files should have been allocated as 0 size
|
2009-10-26 02:29:39 +01:00
|
|
|
TEST_CHECK(exists(combine_path(base, "test3.tmp")));
|
|
|
|
TEST_CHECK(exists(combine_path(base, "test4.tmp")));
|
2013-08-01 02:21:56 +02:00
|
|
|
TEST_CHECK(file_size(combine_path(base, "test3.tmp")) == 0);
|
|
|
|
TEST_CHECK(file_size(combine_path(base, "test4.tmp")) == 0);
|
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
TEST_EQUAL(file_size(combine_path(base, "test5.tmp")), 3253);
|
|
|
|
TEST_EQUAL(file_size(combine_path(base, "test6.tmp")), 841);
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("file: %d expected: %d last_file_size: %d, piece_size: %d\n"
|
2015-04-11 20:44:43 +02:00
|
|
|
, int(file_size(combine_path(base, "test7.tmp")))
|
2018-01-29 15:58:22 +01:00
|
|
|
, last_file_size - int(piece_size), last_file_size, int(piece_size));
|
2017-04-29 06:27:55 +02:00
|
|
|
TEST_EQUAL(file_size(combine_path(base, "test7.tmp")), last_file_size - int(piece_size));
|
2009-10-26 02:29:39 +01:00
|
|
|
remove_all(combine_path(test_path, "temp_storage"), ec);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ec && ec != boost::system::errc::no_such_file_or_directory)
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "remove_all '" << combine_path(test_path, "temp_storage")
|
2010-10-18 09:15:57 +02:00
|
|
|
<< "': " << ec.message() << std::endl;
|
2008-05-14 07:29:42 +02:00
|
|
|
}
|
2007-02-25 22:11:29 +01:00
|
|
|
|
2007-08-25 20:26:43 +02:00
|
|
|
// ==============================================
|
2008-05-14 07:29:42 +02:00
|
|
|
|
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
error_code ec;
|
2008-05-28 10:44:40 +02:00
|
|
|
file_storage fs;
|
2013-07-23 09:32:38 +02:00
|
|
|
fs.add_file(combine_path("temp_storage", "test1.tmp"), 3 * piece_size);
|
2017-08-03 12:02:03 +02:00
|
|
|
lt::create_torrent t(fs, piece_size, -1, {});
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(fs.file_path(file_index_t(0)) == combine_path("temp_storage", "test1.tmp"));
|
|
|
|
t.set_hash(piece_index_t(0), hasher(piece0).final());
|
|
|
|
t.set_hash(piece_index_t(1), hasher(piece1).final());
|
|
|
|
t.set_hash(piece_index_t(2), hasher(piece2).final());
|
2008-05-28 10:44:40 +02:00
|
|
|
|
2010-03-05 07:27:23 +01:00
|
|
|
std::vector<char> buf;
|
|
|
|
bencode(std::back_inserter(buf), t.generate());
|
2017-07-20 09:27:47 +02:00
|
|
|
info = std::make_shared<torrent_info>(buf, ec, from_span);
|
2007-02-25 22:11:29 +01:00
|
|
|
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "=== test 3 ===" << std::endl;
|
2007-10-08 22:01:36 +02:00
|
|
|
|
2016-03-20 21:31:17 +01:00
|
|
|
run_storage_tests(info, fs, test_path, storage_mode_sparse, unbuffered);
|
2007-02-25 22:11:29 +01:00
|
|
|
|
2013-07-23 09:32:38 +02:00
|
|
|
TEST_EQUAL(file_size(combine_path(test_path, combine_path("temp_storage", "test1.tmp"))), piece_size * 3);
|
2009-10-26 02:29:39 +01:00
|
|
|
remove_all(combine_path(test_path, "temp_storage"), ec);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ec && ec != boost::system::errc::no_such_file_or_directory)
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "remove_all '" << combine_path(test_path, "temp_storage")
|
2010-10-18 09:15:57 +02:00
|
|
|
<< "': " << ec.message() << std::endl;
|
2007-02-25 22:11:29 +01:00
|
|
|
|
2007-08-25 20:26:43 +02:00
|
|
|
// ==============================================
|
|
|
|
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "=== test 4 ===" << std::endl;
|
2007-10-08 22:01:36 +02:00
|
|
|
|
2009-01-21 08:31:49 +01:00
|
|
|
run_storage_tests(info, fs, test_path, storage_mode_allocate, unbuffered);
|
2007-10-08 22:01:36 +02:00
|
|
|
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << file_size(combine_path(test_path, combine_path("temp_storage", "test1.tmp"))) << std::endl;
|
2013-07-23 09:32:38 +02:00
|
|
|
TEST_EQUAL(file_size(combine_path(test_path, combine_path("temp_storage", "test1.tmp"))), 3 * piece_size);
|
2007-06-01 01:35:48 +02:00
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
remove_all(combine_path(test_path, "temp_storage"), ec);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ec && ec != boost::system::errc::no_such_file_or_directory)
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "remove_all '" << combine_path(test_path, "temp_storage")
|
2010-10-18 09:15:57 +02:00
|
|
|
<< "': " << ec.message() << std::endl;
|
2007-10-21 02:19:37 +02:00
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
}
|
2007-10-21 02:19:37 +02:00
|
|
|
|
|
|
|
// ==============================================
|
|
|
|
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "=== test 5 ===" << std::endl;
|
2009-01-21 08:31:49 +01:00
|
|
|
test_remove(test_path, unbuffered);
|
2007-10-21 02:19:37 +02:00
|
|
|
|
2008-08-20 10:07:31 +02:00
|
|
|
// ==============================================
|
|
|
|
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "=== test 6 ===" << std::endl;
|
2018-04-21 18:46:54 +02:00
|
|
|
test_check_files(test_path, storage_mode_sparse);
|
2015-01-16 05:02:44 +01:00
|
|
|
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "=== test 7 ===" << std::endl;
|
2015-01-16 05:02:44 +01:00
|
|
|
test_rename(test_path);
|
2007-10-08 22:01:36 +02:00
|
|
|
}
|
2007-06-01 01:35:48 +02:00
|
|
|
|
2016-02-16 07:43:06 +01:00
|
|
|
void test_fastresume(bool const test_deprecated)
|
2008-02-25 06:07:29 +01:00
|
|
|
{
|
2015-06-21 04:34:44 +02:00
|
|
|
std::string test_path = current_working_directory();
|
2009-10-26 02:29:39 +01:00
|
|
|
error_code ec;
|
2008-10-01 01:37:42 +02:00
|
|
|
std::cout << "\n\n=== test fastresume ===" << std::endl;
|
2009-10-26 02:29:39 +01:00
|
|
|
remove_all(combine_path(test_path, "tmp1"), ec);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ec && ec != boost::system::errc::no_such_file_or_directory)
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "remove_all '" << combine_path(test_path, "tmp1")
|
2010-10-18 09:15:57 +02:00
|
|
|
<< "': " << ec.message() << std::endl;
|
2009-10-26 02:29:39 +01:00
|
|
|
create_directory(combine_path(test_path, "tmp1"), ec);
|
2017-01-02 16:16:33 +01:00
|
|
|
if (ec) std::cout << "create_directory '" << combine_path(test_path, "tmp1")
|
2010-10-18 09:15:57 +02:00
|
|
|
<< "': " << ec.message() << std::endl;
|
2009-10-26 02:29:39 +01:00
|
|
|
std::ofstream file(combine_path(test_path, "tmp1/temporary").c_str());
|
2016-08-17 23:26:35 +02:00
|
|
|
std::shared_ptr<torrent_info> t = ::create_torrent(&file);
|
2008-02-25 06:07:29 +01:00
|
|
|
file.close();
|
2009-10-26 02:29:39 +01:00
|
|
|
TEST_CHECK(exists(combine_path(test_path, "tmp1/temporary")));
|
2013-06-10 00:30:02 +02:00
|
|
|
if (!exists(combine_path(test_path, "tmp1/temporary")))
|
|
|
|
return;
|
2008-02-25 06:07:29 +01:00
|
|
|
|
|
|
|
entry resume;
|
|
|
|
{
|
2016-02-16 07:43:06 +01:00
|
|
|
settings_pack pack = settings();
|
2014-12-31 16:51:45 +01:00
|
|
|
lt::session ses(pack);
|
2008-02-25 06:07:29 +01:00
|
|
|
|
2009-12-16 11:42:34 +01:00
|
|
|
add_torrent_params p;
|
2016-08-17 23:26:35 +02:00
|
|
|
p.ti = std::make_shared<torrent_info>(std::cref(*t));
|
2009-12-16 11:42:34 +01:00
|
|
|
p.save_path = combine_path(test_path, "tmp1");
|
2016-03-20 21:31:17 +01:00
|
|
|
p.storage_mode = storage_mode_sparse;
|
2018-01-29 15:58:22 +01:00
|
|
|
error_code ignore;
|
|
|
|
torrent_handle h = ses.add_torrent(std::move(p), ignore);
|
2013-06-10 00:30:02 +02:00
|
|
|
TEST_CHECK(exists(combine_path(p.save_path, "temporary")));
|
|
|
|
if (!exists(combine_path(p.save_path, "temporary")))
|
|
|
|
return;
|
2015-06-10 07:22:01 +02:00
|
|
|
|
2013-06-10 00:30:02 +02:00
|
|
|
torrent_status s;
|
2013-08-01 03:02:26 +02:00
|
|
|
for (int i = 0; i < 50; ++i)
|
2008-02-25 06:07:29 +01:00
|
|
|
{
|
2008-10-01 01:37:42 +02:00
|
|
|
print_alerts(ses, "ses");
|
2013-06-10 00:30:02 +02:00
|
|
|
s = h.status();
|
2015-12-06 18:45:20 +01:00
|
|
|
if (s.progress == 1.0f)
|
2008-02-25 06:07:29 +01:00
|
|
|
{
|
|
|
|
std::cout << "progress: 1.0f" << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
2016-06-26 15:24:06 +02:00
|
|
|
std::this_thread::sleep_for(lt::milliseconds(100));
|
2008-02-25 06:07:29 +01:00
|
|
|
}
|
2013-06-10 00:30:02 +02:00
|
|
|
|
|
|
|
// the whole point of the test is to have a resume
|
|
|
|
// data which expects the file to exist in full. If
|
|
|
|
// we failed to do that, we might as well abort
|
|
|
|
TEST_EQUAL(s.progress, 1.0f);
|
|
|
|
if (s.progress != 1.0f)
|
|
|
|
return;
|
|
|
|
|
2013-06-18 09:33:49 +02:00
|
|
|
h.save_resume_data();
|
2015-04-03 22:15:48 +02:00
|
|
|
alert const* ra = wait_for_alert(ses, save_resume_data_alert::alert_type);
|
|
|
|
TEST_CHECK(ra);
|
2017-03-09 00:01:59 +01:00
|
|
|
if (ra) resume = write_resume_data(alert_cast<save_resume_data_alert>(ra)->params);
|
2014-07-06 21:18:00 +02:00
|
|
|
ses.remove_torrent(h, lt::session::delete_files);
|
2015-04-03 22:15:48 +02:00
|
|
|
alert const* da = wait_for_alert(ses, torrent_deleted_alert::alert_type);
|
|
|
|
TEST_CHECK(da);
|
2008-02-25 06:07:29 +01:00
|
|
|
}
|
2013-07-23 09:32:38 +02:00
|
|
|
TEST_CHECK(!exists(combine_path(test_path, combine_path("tmp1", "temporary"))));
|
|
|
|
if (exists(combine_path(test_path, combine_path("tmp1", "temporary"))))
|
2013-06-10 00:30:02 +02:00
|
|
|
return;
|
2013-12-19 09:30:17 +01:00
|
|
|
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << resume.to_string() << "\n";
|
2008-02-25 06:07:29 +01:00
|
|
|
|
2008-10-01 01:37:42 +02:00
|
|
|
// make sure the fast resume check fails! since we removed the file
|
2008-02-25 06:07:29 +01:00
|
|
|
{
|
2016-02-16 07:43:06 +01:00
|
|
|
settings_pack pack = settings();
|
2014-12-31 16:51:45 +01:00
|
|
|
lt::session ses(pack);
|
2009-12-16 11:42:34 +01:00
|
|
|
|
2016-02-16 07:43:06 +01:00
|
|
|
std::vector<char> resume_data;
|
|
|
|
bencode(std::back_inserter(resume_data), resume);
|
|
|
|
|
2009-12-16 11:42:34 +01:00
|
|
|
add_torrent_params p;
|
2018-02-02 13:28:01 +01:00
|
|
|
TORRENT_UNUSED(test_deprecated);
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2016-02-16 07:43:06 +01:00
|
|
|
if (test_deprecated)
|
|
|
|
{
|
|
|
|
p.resume_data = resume_data;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2018-10-07 02:44:17 +02:00
|
|
|
p = read_resume_data(resume_data);
|
2016-02-16 07:43:06 +01:00
|
|
|
}
|
|
|
|
|
2017-07-09 16:32:01 +02:00
|
|
|
p.flags &= ~torrent_flags::paused;
|
|
|
|
p.flags &= ~torrent_flags::auto_managed;
|
2016-08-17 23:26:35 +02:00
|
|
|
p.ti = std::make_shared<torrent_info>(std::cref(*t));
|
2009-12-16 11:42:34 +01:00
|
|
|
p.save_path = combine_path(test_path, "tmp1");
|
2016-03-20 21:31:17 +01:00
|
|
|
p.storage_mode = storage_mode_sparse;
|
2017-03-26 20:54:16 +02:00
|
|
|
torrent_handle h = ses.add_torrent(std::move(p), ec);
|
2015-06-10 07:22:01 +02:00
|
|
|
|
2016-11-12 04:01:18 +01:00
|
|
|
std::printf("expecting fastresume to be rejected becase the files were removed");
|
2015-04-03 22:15:48 +02:00
|
|
|
alert const* a = wait_for_alert(ses, fastresume_rejected_alert::alert_type
|
|
|
|
, "ses");
|
2013-06-10 00:30:02 +02:00
|
|
|
// we expect the fast resume to be rejected because the files were removed
|
2016-07-09 22:26:26 +02:00
|
|
|
TEST_CHECK(alert_cast<fastresume_rejected_alert>(a) != nullptr);
|
2008-02-25 06:07:29 +01:00
|
|
|
}
|
2009-10-26 02:29:39 +01:00
|
|
|
remove_all(combine_path(test_path, "tmp1"), ec);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ec && ec != boost::system::errc::no_such_file_or_directory)
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "remove_all '" << combine_path(test_path, "tmp1")
|
2010-10-18 09:15:57 +02:00
|
|
|
<< "': " << ec.message() << std::endl;
|
2008-10-01 01:37:42 +02:00
|
|
|
}
|
|
|
|
|
2018-01-23 19:23:57 +01:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2016-02-16 07:43:06 +01:00
|
|
|
TORRENT_TEST(fastresume)
|
|
|
|
{
|
|
|
|
test_fastresume(false);
|
|
|
|
}
|
|
|
|
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2016-02-16 07:43:06 +01:00
|
|
|
TORRENT_TEST(fastresume_deprecated)
|
|
|
|
{
|
|
|
|
test_fastresume(true);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-01-23 20:56:03 +01:00
|
|
|
namespace {
|
2016-02-16 07:43:06 +01:00
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
bool got_file_rename_alert(alert const* a)
|
2009-01-02 10:42:51 +01:00
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
return alert_cast<lt::file_renamed_alert>(a)
|
|
|
|
|| alert_cast<lt::file_rename_failed_alert>(a);
|
2009-01-02 10:42:51 +01:00
|
|
|
}
|
|
|
|
|
2018-01-23 20:56:03 +01:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2016-11-05 06:38:55 +01:00
|
|
|
TORRENT_TEST(rename_file)
|
|
|
|
{
|
|
|
|
std::vector<char> buf;
|
|
|
|
file_storage fs;
|
2016-11-10 01:53:55 +01:00
|
|
|
std::shared_ptr<torrent_info> info = setup_torrent_info(fs, buf);
|
2016-11-05 06:38:55 +01:00
|
|
|
|
2017-01-13 06:39:49 +01:00
|
|
|
settings_pack pack = settings();
|
2016-11-05 06:38:55 +01:00
|
|
|
pack.set_bool(settings_pack::disable_hash_checks, true);
|
|
|
|
lt::session ses(pack);
|
|
|
|
|
|
|
|
add_torrent_params p;
|
|
|
|
p.ti = info;
|
|
|
|
p.save_path = ".";
|
|
|
|
error_code ec;
|
2017-03-26 20:54:16 +02:00
|
|
|
torrent_handle h = ses.add_torrent(std::move(p), ec);
|
2016-11-05 06:38:55 +01:00
|
|
|
|
|
|
|
// make it a seed
|
2018-01-23 20:56:03 +01:00
|
|
|
std::vector<char> tmp(std::size_t(info->piece_length()));
|
2018-06-02 19:29:40 +02:00
|
|
|
for (auto const i : fs.piece_range())
|
2016-11-05 06:38:55 +01:00
|
|
|
h.add_piece(i, &tmp[0]);
|
|
|
|
|
|
|
|
// wait for the files to have been written
|
2017-05-18 05:04:50 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < info->num_pieces(); ++i)
|
|
|
|
{
|
|
|
|
alert const* pf = wait_for_alert(ses, piece_finished_alert::alert_type
|
|
|
|
, "ses", pop_alerts::cache_alerts);
|
|
|
|
TEST_CHECK(pf);
|
|
|
|
}
|
2016-11-05 06:38:55 +01:00
|
|
|
|
|
|
|
// now rename them. This is the test
|
2018-06-02 19:29:40 +02:00
|
|
|
for (auto const i : fs.file_range())
|
2016-11-05 06:38:55 +01:00
|
|
|
{
|
|
|
|
std::string name = fs.file_path(i);
|
2017-01-13 06:39:49 +01:00
|
|
|
h.rename_file(i, "temp_storage__" + name.substr(12));
|
2016-11-05 06:38:55 +01:00
|
|
|
}
|
|
|
|
|
2017-05-18 05:04:50 +02:00
|
|
|
// wait for the files to have been renamed
|
|
|
|
for (int i = 0; i < info->num_files(); ++i)
|
|
|
|
{
|
|
|
|
alert const* fra = wait_for_alert(ses, file_renamed_alert::alert_type
|
|
|
|
, "ses", pop_alerts::cache_alerts);
|
|
|
|
TEST_CHECK(fra);
|
|
|
|
}
|
2016-11-05 06:38:55 +01:00
|
|
|
|
2017-01-13 06:39:49 +01:00
|
|
|
TEST_CHECK(exists(info->name() + "__"));
|
2016-11-05 06:38:55 +01:00
|
|
|
|
|
|
|
h.save_resume_data();
|
|
|
|
alert const* ra = wait_for_alert(ses, save_resume_data_alert::alert_type);
|
|
|
|
TEST_CHECK(ra);
|
|
|
|
if (!ra) return;
|
2017-03-09 00:01:59 +01:00
|
|
|
add_torrent_params resume = alert_cast<save_resume_data_alert>(ra)->params;
|
2016-11-05 06:38:55 +01:00
|
|
|
|
2017-03-09 00:01:59 +01:00
|
|
|
auto const files = resume.renamed_files;
|
|
|
|
for (auto const& i : files)
|
2017-03-09 00:32:58 +01:00
|
|
|
{
|
2017-03-09 00:01:59 +01:00
|
|
|
TEST_EQUAL(i.second.substr(0, 14), "temp_storage__");
|
2017-03-09 00:32:58 +01:00
|
|
|
}
|
2016-11-05 06:38:55 +01:00
|
|
|
}
|
|
|
|
|
2018-01-23 20:56:03 +01:00
|
|
|
namespace {
|
|
|
|
|
2016-02-16 07:43:06 +01:00
|
|
|
void test_rename_file_fastresume(bool test_deprecated)
|
2008-10-01 01:37:42 +02:00
|
|
|
{
|
2015-06-21 04:34:44 +02:00
|
|
|
std::string test_path = current_working_directory();
|
2009-10-26 02:29:39 +01:00
|
|
|
error_code ec;
|
2008-10-01 01:37:42 +02:00
|
|
|
std::cout << "\n\n=== test rename file in fastresume ===" << std::endl;
|
2009-10-26 02:29:39 +01:00
|
|
|
remove_all(combine_path(test_path, "tmp2"), ec);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ec && ec != boost::system::errc::no_such_file_or_directory)
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "remove_all '" << combine_path(test_path, "tmp2")
|
2010-10-18 09:15:57 +02:00
|
|
|
<< "': " << ec.message() << std::endl;
|
2009-10-26 02:29:39 +01:00
|
|
|
create_directory(combine_path(test_path, "tmp2"), ec);
|
2017-01-02 16:16:33 +01:00
|
|
|
if (ec) std::cout << "create_directory: " << ec.message() << std::endl;
|
2009-10-26 02:29:39 +01:00
|
|
|
std::ofstream file(combine_path(test_path, "tmp2/temporary").c_str());
|
2016-08-17 23:26:35 +02:00
|
|
|
std::shared_ptr<torrent_info> t = ::create_torrent(&file);
|
2008-10-01 01:37:42 +02:00
|
|
|
file.close();
|
2009-10-26 02:29:39 +01:00
|
|
|
TEST_CHECK(exists(combine_path(test_path, "tmp2/temporary")));
|
2008-10-01 01:37:42 +02:00
|
|
|
|
2017-03-09 00:01:59 +01:00
|
|
|
add_torrent_params resume;
|
2008-10-01 01:37:42 +02:00
|
|
|
{
|
2016-02-16 07:43:06 +01:00
|
|
|
settings_pack pack = settings();
|
2014-12-31 16:51:45 +01:00
|
|
|
lt::session ses(pack);
|
2009-12-16 11:42:34 +01:00
|
|
|
|
|
|
|
add_torrent_params p;
|
2016-08-17 23:26:35 +02:00
|
|
|
p.ti = std::make_shared<torrent_info>(std::cref(*t));
|
2009-12-16 11:42:34 +01:00
|
|
|
p.save_path = combine_path(test_path, "tmp2");
|
2016-03-20 21:31:17 +01:00
|
|
|
p.storage_mode = storage_mode_sparse;
|
2017-03-26 20:54:16 +02:00
|
|
|
torrent_handle h = ses.add_torrent(std::move(p), ec);
|
2008-10-01 01:37:42 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
h.rename_file(file_index_t(0), "testing_renamed_files");
|
2009-01-02 10:42:51 +01:00
|
|
|
std::cout << "renaming file" << std::endl;
|
2009-02-09 07:19:31 +01:00
|
|
|
bool renamed = false;
|
2016-07-01 06:23:56 +02:00
|
|
|
for (int i = 0; i < 30; ++i)
|
2008-10-01 01:37:42 +02:00
|
|
|
{
|
2017-04-02 20:22:17 +02:00
|
|
|
if (print_alerts(ses, "ses", true, true, &got_file_rename_alert)) renamed = true;
|
2008-10-01 01:37:42 +02:00
|
|
|
torrent_status s = h.status();
|
2012-02-19 20:00:52 +01:00
|
|
|
if (s.state == torrent_status::seeding && renamed) break;
|
2016-06-26 15:24:06 +02:00
|
|
|
std::this_thread::sleep_for(lt::milliseconds(100));
|
2008-10-01 01:37:42 +02:00
|
|
|
}
|
2009-01-02 10:42:51 +01:00
|
|
|
std::cout << "stop loop" << std::endl;
|
2009-02-09 07:19:31 +01:00
|
|
|
torrent_status s = h.status();
|
|
|
|
TEST_CHECK(s.state == torrent_status::seeding);
|
2013-06-18 09:33:49 +02:00
|
|
|
|
|
|
|
h.save_resume_data();
|
2015-04-03 22:15:48 +02:00
|
|
|
alert const* ra = wait_for_alert(ses, save_resume_data_alert::alert_type);
|
|
|
|
TEST_CHECK(ra);
|
2017-03-09 00:01:59 +01:00
|
|
|
if (ra) resume = alert_cast<save_resume_data_alert>(ra)->params;
|
2008-10-01 01:37:42 +02:00
|
|
|
ses.remove_torrent(h);
|
|
|
|
}
|
2009-10-26 02:29:39 +01:00
|
|
|
TEST_CHECK(!exists(combine_path(test_path, "tmp2/temporary")));
|
|
|
|
TEST_CHECK(exists(combine_path(test_path, "tmp2/testing_renamed_files")));
|
2017-03-09 00:01:59 +01:00
|
|
|
TEST_CHECK(!resume.renamed_files.empty());
|
2013-12-19 09:30:17 +01:00
|
|
|
|
2017-03-09 00:01:59 +01:00
|
|
|
entry resume_ent = write_resume_data(resume);
|
2013-12-19 09:30:17 +01:00
|
|
|
|
2017-03-09 00:01:59 +01:00
|
|
|
std::cout << resume_ent.to_string() << "\n";
|
2008-10-01 01:37:42 +02:00
|
|
|
|
|
|
|
// make sure the fast resume check succeeds, even though we renamed the file
|
|
|
|
{
|
2016-02-16 07:43:06 +01:00
|
|
|
settings_pack pack = settings();
|
2014-12-31 16:51:45 +01:00
|
|
|
lt::session ses(pack);
|
2009-12-16 11:42:34 +01:00
|
|
|
|
|
|
|
add_torrent_params p;
|
2016-02-16 07:43:06 +01:00
|
|
|
std::vector<char> resume_data;
|
2017-03-09 00:01:59 +01:00
|
|
|
bencode(std::back_inserter(resume_data), resume_ent);
|
2018-02-02 13:28:01 +01:00
|
|
|
TORRENT_UNUSED(test_deprecated);
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2016-02-16 07:43:06 +01:00
|
|
|
if (test_deprecated)
|
|
|
|
{
|
|
|
|
p.resume_data = resume_data;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2018-10-07 02:44:17 +02:00
|
|
|
p = read_resume_data(resume_data);
|
2016-02-16 07:43:06 +01:00
|
|
|
}
|
2016-08-17 23:26:35 +02:00
|
|
|
p.ti = std::make_shared<torrent_info>(std::cref(*t));
|
2009-12-16 11:42:34 +01:00
|
|
|
p.save_path = combine_path(test_path, "tmp2");
|
2016-03-20 21:31:17 +01:00
|
|
|
p.storage_mode = storage_mode_sparse;
|
2017-03-26 20:54:16 +02:00
|
|
|
torrent_handle h = ses.add_torrent(std::move(p), ec);
|
2009-12-16 11:42:34 +01:00
|
|
|
|
2013-08-01 03:02:26 +02:00
|
|
|
torrent_status stat;
|
|
|
|
for (int i = 0; i < 50; ++i)
|
2008-10-01 01:37:42 +02:00
|
|
|
{
|
2013-08-01 03:02:26 +02:00
|
|
|
stat = h.status();
|
2008-10-01 01:37:42 +02:00
|
|
|
print_alerts(ses, "ses");
|
2013-08-01 03:02:26 +02:00
|
|
|
if (stat.state == torrent_status::seeding)
|
|
|
|
break;
|
2016-06-26 15:24:06 +02:00
|
|
|
std::this_thread::sleep_for(lt::milliseconds(100));
|
2008-10-01 01:37:42 +02:00
|
|
|
}
|
|
|
|
TEST_CHECK(stat.state == torrent_status::seeding);
|
2009-02-09 07:19:31 +01:00
|
|
|
|
2013-06-18 09:33:49 +02:00
|
|
|
h.save_resume_data();
|
2015-04-03 22:15:48 +02:00
|
|
|
alert const* ra = wait_for_alert(ses, save_resume_data_alert::alert_type);
|
|
|
|
TEST_CHECK(ra);
|
2017-03-09 00:01:59 +01:00
|
|
|
if (ra) resume = alert_cast<save_resume_data_alert>(ra)->params;
|
2009-02-09 07:19:31 +01:00
|
|
|
ses.remove_torrent(h);
|
2008-10-01 01:37:42 +02:00
|
|
|
}
|
2017-03-09 00:01:59 +01:00
|
|
|
TEST_CHECK(!resume.renamed_files.empty());
|
2013-12-19 09:30:17 +01:00
|
|
|
|
2017-03-09 00:01:59 +01:00
|
|
|
resume_ent = write_resume_data(resume);
|
|
|
|
std::cout << resume_ent.to_string() << "\n";
|
2013-12-19 09:30:17 +01:00
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
remove_all(combine_path(test_path, "tmp2"), ec);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ec && ec != boost::system::errc::no_such_file_or_directory)
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "remove_all '" << combine_path(test_path, "tmp2")
|
2010-10-18 09:15:57 +02:00
|
|
|
<< "': " << ec.message() << std::endl;
|
2008-02-25 06:07:29 +01:00
|
|
|
}
|
|
|
|
|
2018-01-23 20:56:03 +01:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2016-02-16 07:43:06 +01:00
|
|
|
TORRENT_TEST(rename_file_fastresume)
|
|
|
|
{
|
|
|
|
test_rename_file_fastresume(false);
|
|
|
|
}
|
|
|
|
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2016-02-16 07:43:06 +01:00
|
|
|
TORRENT_TEST(rename_file_fastresume_deprecated)
|
|
|
|
{
|
|
|
|
test_rename_file_fastresume(true);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-01-23 20:56:03 +01:00
|
|
|
namespace {
|
|
|
|
|
2017-01-11 06:42:10 +01:00
|
|
|
void alloc_iov(iovec_t* iov, int num_bufs)
|
2015-02-15 08:31:35 +01:00
|
|
|
{
|
|
|
|
for (int i = 0; i < num_bufs; ++i)
|
|
|
|
{
|
2018-10-20 12:50:53 +02:00
|
|
|
iov[i] = { new char[static_cast<std::size_t>(num_bufs * (i + 1))]
|
2018-11-01 23:05:30 +01:00
|
|
|
, num_bufs * (i + 1) };
|
2015-02-15 08:31:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-29 06:27:55 +02:00
|
|
|
// TODO: this should take a span of iovec_ts
|
2017-01-11 06:42:10 +01:00
|
|
|
void fill_pattern(iovec_t* iov, int num_bufs)
|
2015-02-15 08:31:35 +01:00
|
|
|
{
|
|
|
|
int counter = 0;
|
|
|
|
for (int i = 0; i < num_bufs; ++i)
|
|
|
|
{
|
2017-04-29 06:27:55 +02:00
|
|
|
for (char& v : iov[i])
|
2015-02-15 08:31:35 +01:00
|
|
|
{
|
2018-01-23 20:56:03 +01:00
|
|
|
v = char(counter & 0xff);
|
2015-02-15 08:31:35 +01:00
|
|
|
++counter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-16 08:30:14 +01:00
|
|
|
bool check_pattern(std::vector<char> const& buf, int counter)
|
|
|
|
{
|
2018-01-23 20:56:03 +01:00
|
|
|
unsigned char const* p = reinterpret_cast<unsigned char const*>(buf.data());
|
2015-11-16 08:30:14 +01:00
|
|
|
for (int k = 0; k < int(buf.size()); ++k)
|
|
|
|
{
|
|
|
|
if (p[k] != (counter & 0xff)) return false;
|
|
|
|
++counter;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-29 06:27:55 +02:00
|
|
|
// TODO: this should take a span
|
2017-01-11 06:42:10 +01:00
|
|
|
void free_iov(iovec_t* iov, int num_bufs)
|
2015-02-15 08:31:35 +01:00
|
|
|
{
|
|
|
|
for (int i = 0; i < num_bufs; ++i)
|
|
|
|
{
|
2017-04-29 06:27:55 +02:00
|
|
|
delete[] iov[i].data();
|
|
|
|
iov[i] = { nullptr, 0 };
|
2015-02-15 08:31:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:56:03 +01:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2015-05-30 23:46:59 +02:00
|
|
|
TORRENT_TEST(iovec_copy_bufs)
|
2015-02-15 08:31:35 +01:00
|
|
|
{
|
2017-01-11 06:42:10 +01:00
|
|
|
iovec_t iov1[10];
|
|
|
|
iovec_t iov2[10];
|
2015-02-15 08:31:35 +01:00
|
|
|
|
|
|
|
alloc_iov(iov1, 10);
|
|
|
|
fill_pattern(iov1, 10);
|
|
|
|
|
2016-10-27 02:40:56 +02:00
|
|
|
TEST_CHECK(bufs_size({iov1, 10}) >= 106);
|
2015-02-15 08:31:35 +01:00
|
|
|
|
|
|
|
// copy exactly 106 bytes from iov1 to iov2
|
2017-01-20 06:01:00 +01:00
|
|
|
int num_bufs = aux::copy_bufs(iov1, 106, iov2);
|
2015-02-15 08:31:35 +01:00
|
|
|
|
|
|
|
// verify that the first 100 bytes is pattern 1
|
|
|
|
// and that the remaining bytes are pattern 2
|
|
|
|
|
|
|
|
int counter = 0;
|
|
|
|
for (int i = 0; i < num_bufs; ++i)
|
|
|
|
{
|
2017-04-29 06:27:55 +02:00
|
|
|
for (char v : iov2[i])
|
2015-02-15 08:31:35 +01:00
|
|
|
{
|
2017-04-29 06:27:55 +02:00
|
|
|
TEST_EQUAL(int(v), (counter & 0xff));
|
2015-02-15 08:31:35 +01:00
|
|
|
++counter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TEST_EQUAL(counter, 106);
|
|
|
|
|
|
|
|
free_iov(iov1, 10);
|
|
|
|
}
|
|
|
|
|
2015-05-30 23:46:59 +02:00
|
|
|
TORRENT_TEST(iovec_clear_bufs)
|
2015-02-15 08:31:35 +01:00
|
|
|
{
|
2017-01-11 06:42:10 +01:00
|
|
|
iovec_t iov[10];
|
2015-02-15 08:31:35 +01:00
|
|
|
alloc_iov(iov, 10);
|
|
|
|
fill_pattern(iov, 10);
|
|
|
|
|
2017-06-21 04:44:57 +02:00
|
|
|
lt::aux::clear_bufs({iov, 10});
|
2015-02-15 08:31:35 +01:00
|
|
|
for (int i = 0; i < 10; ++i)
|
|
|
|
{
|
2017-04-29 06:27:55 +02:00
|
|
|
for (char v : iov[i])
|
2015-02-15 08:31:35 +01:00
|
|
|
{
|
2017-04-29 06:27:55 +02:00
|
|
|
TEST_EQUAL(int(v), 0);
|
2015-02-15 08:31:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free_iov(iov, 10);
|
|
|
|
}
|
|
|
|
|
2015-05-30 23:46:59 +02:00
|
|
|
TORRENT_TEST(iovec_bufs_size)
|
2015-02-15 08:31:35 +01:00
|
|
|
{
|
2017-01-11 06:42:10 +01:00
|
|
|
iovec_t iov[10];
|
2015-02-15 08:31:35 +01:00
|
|
|
|
|
|
|
for (int i = 1; i < 10; ++i)
|
|
|
|
{
|
|
|
|
alloc_iov(iov, i);
|
|
|
|
|
|
|
|
int expected_size = 0;
|
|
|
|
for (int k = 0; k < i; ++k) expected_size += i * (k + 1);
|
2018-11-01 23:05:30 +01:00
|
|
|
TEST_EQUAL(bufs_size({iov, i}), expected_size);
|
2015-02-15 08:31:35 +01:00
|
|
|
|
|
|
|
free_iov(iov, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-30 23:46:59 +02:00
|
|
|
TORRENT_TEST(iovec_advance_bufs)
|
2015-02-15 08:31:35 +01:00
|
|
|
{
|
2017-01-11 06:42:10 +01:00
|
|
|
iovec_t iov1[10];
|
|
|
|
iovec_t iov2[10];
|
2015-02-15 08:31:35 +01:00
|
|
|
alloc_iov(iov1, 10);
|
|
|
|
fill_pattern(iov1, 10);
|
|
|
|
|
|
|
|
memcpy(iov2, iov1, sizeof(iov1));
|
|
|
|
|
2017-01-11 06:42:10 +01:00
|
|
|
span<iovec_t> iov = iov2;
|
2015-02-15 08:31:35 +01:00
|
|
|
|
|
|
|
// advance iov 13 bytes. Make sure what's left fits pattern 1 shifted
|
|
|
|
// 13 bytes
|
2017-01-20 06:01:00 +01:00
|
|
|
iov = aux::advance_bufs(iov, 13);
|
2015-02-15 08:31:35 +01:00
|
|
|
|
2016-02-16 20:20:04 +01:00
|
|
|
// make sure what's in
|
2015-02-15 08:31:35 +01:00
|
|
|
int counter = 13;
|
2016-10-22 20:43:40 +02:00
|
|
|
for (auto buf : iov)
|
2015-02-15 08:31:35 +01:00
|
|
|
{
|
2017-04-29 06:27:55 +02:00
|
|
|
for (char v : buf)
|
2015-02-15 08:31:35 +01:00
|
|
|
{
|
2017-04-29 06:27:55 +02:00
|
|
|
TEST_EQUAL(v, static_cast<char>(counter));
|
2015-02-15 08:31:35 +01:00
|
|
|
++counter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free_iov(iov1, 10);
|
|
|
|
}
|
|
|
|
|
2015-11-16 08:30:14 +01:00
|
|
|
TORRENT_TEST(unbuffered) { run_test(true); }
|
|
|
|
TORRENT_TEST(buffered) { run_test(false); }
|
|
|
|
|
2018-01-23 20:56:03 +01:00
|
|
|
namespace {
|
|
|
|
|
2015-11-16 08:30:14 +01:00
|
|
|
file_storage make_fs()
|
2007-10-08 22:01:36 +02:00
|
|
|
{
|
2015-11-16 08:30:14 +01:00
|
|
|
file_storage fs;
|
|
|
|
fs.add_file(combine_path("readwritev", "1"), 3);
|
|
|
|
fs.add_file(combine_path("readwritev", "2"), 9);
|
|
|
|
fs.add_file(combine_path("readwritev", "3"), 81);
|
|
|
|
fs.add_file(combine_path("readwritev", "4"), 6561);
|
|
|
|
fs.set_piece_length(0x1000);
|
2016-04-20 06:45:32 +02:00
|
|
|
fs.set_num_pieces(int((fs.total_size() + 0xfff) / 0x1000));
|
2015-11-16 08:30:14 +01:00
|
|
|
return fs;
|
|
|
|
}
|
2008-10-01 01:37:42 +02:00
|
|
|
|
2017-05-02 06:07:50 +02:00
|
|
|
struct test_fileop
|
2015-11-16 08:30:14 +01:00
|
|
|
{
|
2016-07-10 20:27:42 +02:00
|
|
|
explicit test_fileop(int stripe_size) : m_stripe_size(stripe_size) {}
|
2015-11-16 08:30:14 +01:00
|
|
|
|
2017-05-02 06:07:50 +02:00
|
|
|
int operator()(file_index_t const file_index, std::int64_t const file_offset
|
2018-01-23 20:56:03 +01:00
|
|
|
, span<iovec_t const> bufs, storage_error&)
|
2007-10-08 22:01:36 +02:00
|
|
|
{
|
2018-01-23 20:56:03 +01:00
|
|
|
std::size_t offset = size_t(file_offset);
|
2016-12-22 16:42:33 +01:00
|
|
|
if (file_index >= m_file_data.end_index())
|
2015-11-16 08:30:14 +01:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
m_file_data.resize(static_cast<int>(file_index) + 1);
|
2015-11-16 08:30:14 +01:00
|
|
|
}
|
|
|
|
|
2018-01-23 20:56:03 +01:00
|
|
|
std::size_t const write_size = std::size_t(std::min(m_stripe_size, bufs_size(bufs)));
|
2015-11-16 08:30:14 +01:00
|
|
|
|
|
|
|
std::vector<char>& file = m_file_data[file_index];
|
|
|
|
|
2016-04-20 06:45:32 +02:00
|
|
|
if (offset + write_size > file.size())
|
2015-11-16 08:30:14 +01:00
|
|
|
{
|
2016-04-20 06:45:32 +02:00
|
|
|
file.resize(offset + write_size);
|
2015-11-16 08:30:14 +01:00
|
|
|
}
|
|
|
|
|
2018-01-23 20:56:03 +01:00
|
|
|
int left = int(write_size);
|
2015-11-16 08:30:14 +01:00
|
|
|
while (left > 0)
|
|
|
|
{
|
2018-01-23 20:56:03 +01:00
|
|
|
std::size_t const copy_size = std::size_t(std::min(left, int(bufs.front().size())));
|
|
|
|
std::memcpy(&file[offset], bufs.front().data(), copy_size);
|
2016-10-27 02:40:56 +02:00
|
|
|
bufs = bufs.subspan(1);
|
2016-04-20 06:45:32 +02:00
|
|
|
offset += copy_size;
|
2018-01-23 20:56:03 +01:00
|
|
|
left -= int(copy_size);
|
2015-11-16 08:30:14 +01:00
|
|
|
}
|
2018-01-23 20:56:03 +01:00
|
|
|
return int(write_size);
|
2007-10-08 22:01:36 +02:00
|
|
|
}
|
2015-11-16 08:30:14 +01:00
|
|
|
|
|
|
|
int m_stripe_size;
|
2016-12-22 16:42:33 +01:00
|
|
|
aux::vector<std::vector<char>, file_index_t> m_file_data;
|
2015-11-16 08:30:14 +01:00
|
|
|
};
|
|
|
|
|
2017-05-02 06:07:50 +02:00
|
|
|
struct test_read_fileop
|
2015-11-16 08:30:14 +01:00
|
|
|
{
|
|
|
|
// EOF after size bytes read
|
2016-07-10 20:27:42 +02:00
|
|
|
explicit test_read_fileop(int size) : m_size(size), m_counter(0) {}
|
2015-11-16 08:30:14 +01:00
|
|
|
|
2018-01-23 20:56:03 +01:00
|
|
|
int operator()(file_index_t, std::int64_t /*file_offset*/
|
|
|
|
, span<iovec_t const> bufs, storage_error&)
|
2007-10-08 22:01:36 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
int local_size = std::min(m_size, bufs_size(bufs));
|
2016-04-20 06:45:32 +02:00
|
|
|
const int read = local_size;
|
|
|
|
while (local_size > 0)
|
2007-10-08 22:01:36 +02:00
|
|
|
{
|
2017-04-29 06:27:55 +02:00
|
|
|
int const len = std::min(int(bufs.front().size()), local_size);
|
2018-11-01 23:05:30 +01:00
|
|
|
auto local_buf = bufs.front().first(len);
|
2017-04-29 06:27:55 +02:00
|
|
|
for (char& v : local_buf)
|
2015-11-16 08:30:14 +01:00
|
|
|
{
|
2018-01-23 20:56:03 +01:00
|
|
|
v = char(m_counter & 0xff);
|
2015-11-16 08:30:14 +01:00
|
|
|
++m_counter;
|
|
|
|
}
|
2016-04-20 06:45:32 +02:00
|
|
|
local_size -= len;
|
2015-11-16 08:30:14 +01:00
|
|
|
m_size -= len;
|
2016-10-27 02:40:56 +02:00
|
|
|
bufs = bufs.subspan(1);
|
2007-10-08 22:01:36 +02:00
|
|
|
}
|
2015-11-16 08:30:14 +01:00
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
int m_size;
|
|
|
|
int m_counter;
|
|
|
|
};
|
|
|
|
|
2017-05-02 06:07:50 +02:00
|
|
|
struct test_error_fileop
|
2015-11-16 08:30:14 +01:00
|
|
|
{
|
|
|
|
// EOF after size bytes read
|
2016-12-22 16:42:33 +01:00
|
|
|
explicit test_error_fileop(file_index_t error_file)
|
2015-11-16 08:30:14 +01:00
|
|
|
: m_error_file(error_file) {}
|
|
|
|
|
2018-01-23 20:56:03 +01:00
|
|
|
int operator()(file_index_t const file_index, std::int64_t /*file_offset*/
|
2017-05-02 06:07:50 +02:00
|
|
|
, span<iovec_t const> bufs, storage_error& ec)
|
2015-11-16 08:30:14 +01:00
|
|
|
{
|
|
|
|
if (m_error_file == file_index)
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
ec.file(file_index);
|
2015-11-16 08:30:14 +01:00
|
|
|
ec.ec.assign(boost::system::errc::permission_denied
|
|
|
|
, boost::system::generic_category());
|
2017-06-18 00:18:19 +02:00
|
|
|
ec.operation = operation_t::file_read;
|
2015-11-16 08:30:14 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2016-10-27 02:40:56 +02:00
|
|
|
return bufs_size(bufs);
|
2015-11-16 08:30:14 +01:00
|
|
|
}
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
file_index_t m_error_file;
|
2015-11-16 08:30:14 +01:00
|
|
|
};
|
|
|
|
|
2017-01-11 06:42:10 +01:00
|
|
|
int count_bufs(iovec_t const* bufs, int bytes)
|
2015-11-16 08:30:14 +01:00
|
|
|
{
|
|
|
|
int size = 0;
|
|
|
|
int count = 1;
|
|
|
|
if (bytes == 0) return 0;
|
2017-01-11 06:42:10 +01:00
|
|
|
for (iovec_t const* i = bufs;; ++i, ++count)
|
2015-11-16 08:30:14 +01:00
|
|
|
{
|
2017-04-29 06:27:55 +02:00
|
|
|
size += int(i->size());
|
2015-11-16 08:30:14 +01:00
|
|
|
if (size >= bytes) return count;
|
2007-10-08 22:01:36 +02:00
|
|
|
}
|
2005-09-11 11:48:05 +02:00
|
|
|
}
|
|
|
|
|
2018-01-23 20:56:03 +01:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2015-11-16 08:30:14 +01:00
|
|
|
TORRENT_TEST(readwritev_stripe_1)
|
|
|
|
{
|
|
|
|
const int num_bufs = 30;
|
2017-01-11 06:42:10 +01:00
|
|
|
iovec_t iov[num_bufs];
|
2015-11-16 08:30:14 +01:00
|
|
|
|
|
|
|
alloc_iov(iov, num_bufs);
|
|
|
|
fill_pattern(iov, num_bufs);
|
|
|
|
|
|
|
|
file_storage fs = make_fs();
|
|
|
|
test_fileop fop(1);
|
|
|
|
storage_error ec;
|
|
|
|
|
2018-11-01 23:05:30 +01:00
|
|
|
TEST_CHECK(bufs_size({iov, num_bufs}) >= fs.total_size());
|
2015-11-16 08:30:14 +01:00
|
|
|
|
2017-01-11 06:42:10 +01:00
|
|
|
iovec_t iov2[num_bufs];
|
2017-01-20 06:01:00 +01:00
|
|
|
aux::copy_bufs(iov, int(fs.total_size()), iov2);
|
2016-04-20 06:45:32 +02:00
|
|
|
int num_bufs2 = count_bufs(iov2, int(fs.total_size()));
|
2015-11-16 08:30:14 +01:00
|
|
|
TEST_CHECK(num_bufs2 <= num_bufs);
|
|
|
|
|
2018-11-01 23:05:30 +01:00
|
|
|
int ret = readwritev(fs, {iov2, num_bufs2}, piece_index_t(0), 0, ec
|
2017-05-02 06:07:50 +02:00
|
|
|
, std::ref(fop));
|
2015-11-16 08:30:14 +01:00
|
|
|
|
|
|
|
TEST_EQUAL(ret, fs.total_size());
|
|
|
|
TEST_EQUAL(fop.m_file_data.size(), 4);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(fop.m_file_data[file_index_t(0)].size(), 3);
|
|
|
|
TEST_EQUAL(fop.m_file_data[file_index_t(1)].size(), 9);
|
|
|
|
TEST_EQUAL(fop.m_file_data[file_index_t(2)].size(), 81);
|
|
|
|
TEST_EQUAL(fop.m_file_data[file_index_t(3)].size(), 6561);
|
2015-11-16 08:30:14 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(check_pattern(fop.m_file_data[file_index_t(0)], 0));
|
|
|
|
TEST_CHECK(check_pattern(fop.m_file_data[file_index_t(1)], 3));
|
|
|
|
TEST_CHECK(check_pattern(fop.m_file_data[file_index_t(2)], 3 + 9));
|
|
|
|
TEST_CHECK(check_pattern(fop.m_file_data[file_index_t(3)], 3 + 9 + 81));
|
2015-11-16 08:30:14 +01:00
|
|
|
|
|
|
|
free_iov(iov, num_bufs);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(readwritev_single_buffer)
|
|
|
|
{
|
|
|
|
file_storage fs = make_fs();
|
|
|
|
test_fileop fop(10000000);
|
|
|
|
storage_error ec;
|
|
|
|
|
2016-04-20 06:45:32 +02:00
|
|
|
std::vector<char> buf(size_t(fs.total_size()));
|
2018-11-01 23:05:30 +01:00
|
|
|
iovec_t iov = { &buf[0], int(buf.size()) };
|
2015-11-16 08:30:14 +01:00
|
|
|
fill_pattern(&iov, 1);
|
|
|
|
|
2017-05-02 06:07:50 +02:00
|
|
|
int ret = readwritev(fs, iov, piece_index_t(0), 0, ec, std::ref(fop));
|
2015-11-16 08:30:14 +01:00
|
|
|
|
|
|
|
TEST_EQUAL(ret, fs.total_size());
|
|
|
|
TEST_EQUAL(fop.m_file_data.size(), 4);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(fop.m_file_data[file_index_t(0)].size(), 3);
|
|
|
|
TEST_EQUAL(fop.m_file_data[file_index_t(1)].size(), 9);
|
|
|
|
TEST_EQUAL(fop.m_file_data[file_index_t(2)].size(), 81);
|
|
|
|
TEST_EQUAL(fop.m_file_data[file_index_t(3)].size(), 6561);
|
|
|
|
|
|
|
|
TEST_CHECK(check_pattern(fop.m_file_data[file_index_t(0)], 0));
|
|
|
|
TEST_CHECK(check_pattern(fop.m_file_data[file_index_t(1)], 3));
|
|
|
|
TEST_CHECK(check_pattern(fop.m_file_data[file_index_t(2)], 3 + 9));
|
|
|
|
TEST_CHECK(check_pattern(fop.m_file_data[file_index_t(3)], 3 + 9 + 81));
|
2015-11-16 08:30:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(readwritev_read)
|
|
|
|
{
|
|
|
|
file_storage fs = make_fs();
|
|
|
|
test_read_fileop fop(10000000);
|
|
|
|
storage_error ec;
|
|
|
|
|
2016-04-20 06:45:32 +02:00
|
|
|
std::vector<char> buf(size_t(fs.total_size()));
|
2018-11-01 23:05:30 +01:00
|
|
|
iovec_t iov = { &buf[0], int(buf.size()) };
|
2015-11-16 08:30:14 +01:00
|
|
|
|
|
|
|
// read everything
|
2017-05-02 06:07:50 +02:00
|
|
|
int ret = readwritev(fs, iov, piece_index_t(0), 0, ec, std::ref(fop));
|
2015-11-16 08:30:14 +01:00
|
|
|
|
|
|
|
TEST_EQUAL(ret, fs.total_size());
|
|
|
|
TEST_CHECK(check_pattern(buf, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(readwritev_read_short)
|
|
|
|
{
|
|
|
|
file_storage fs = make_fs();
|
|
|
|
test_read_fileop fop(100);
|
|
|
|
storage_error ec;
|
|
|
|
|
2016-04-20 06:45:32 +02:00
|
|
|
std::vector<char> buf(size_t(fs.total_size()));
|
2018-11-01 23:05:30 +01:00
|
|
|
iovec_t iov = { buf.data(), static_cast<std::ptrdiff_t>(fs.total_size()) };
|
2015-11-16 08:30:14 +01:00
|
|
|
|
|
|
|
// read everything
|
2017-05-02 06:07:50 +02:00
|
|
|
int ret = readwritev(fs, iov, piece_index_t(0), 0, ec, std::ref(fop));
|
2015-11-16 08:30:14 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(static_cast<int>(ec.file()), 3);
|
2015-11-16 08:30:14 +01:00
|
|
|
|
|
|
|
TEST_EQUAL(ret, 100);
|
|
|
|
buf.resize(100);
|
|
|
|
TEST_CHECK(check_pattern(buf, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(readwritev_error)
|
|
|
|
{
|
|
|
|
file_storage fs = make_fs();
|
2016-12-22 16:42:33 +01:00
|
|
|
test_error_fileop fop(file_index_t(2));
|
2015-11-16 08:30:14 +01:00
|
|
|
storage_error ec;
|
|
|
|
|
2016-04-20 06:45:32 +02:00
|
|
|
std::vector<char> buf(size_t(fs.total_size()));
|
2018-11-01 23:05:30 +01:00
|
|
|
iovec_t iov = { buf.data(), static_cast<std::ptrdiff_t>(fs.total_size()) };
|
2015-11-16 08:30:14 +01:00
|
|
|
|
|
|
|
// read everything
|
2017-05-02 06:07:50 +02:00
|
|
|
int ret = readwritev(fs, iov, piece_index_t(0), 0, ec, std::ref(fop));
|
2015-11-16 08:30:14 +01:00
|
|
|
|
|
|
|
TEST_EQUAL(ret, -1);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(static_cast<int>(ec.file()), 2);
|
2017-06-18 00:18:19 +02:00
|
|
|
TEST_CHECK(ec.operation == operation_t::file_read);
|
2015-11-16 08:30:14 +01:00
|
|
|
TEST_EQUAL(ec.ec, boost::system::errc::permission_denied);
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("error: %s\n", ec.ec.message().c_str());
|
2015-11-16 08:30:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(readwritev_zero_size_files)
|
|
|
|
{
|
|
|
|
file_storage fs;
|
|
|
|
fs.add_file(combine_path("readwritev", "1"), 3);
|
|
|
|
fs.add_file(combine_path("readwritev", "2"), 0);
|
|
|
|
fs.add_file(combine_path("readwritev", "3"), 81);
|
|
|
|
fs.add_file(combine_path("readwritev", "4"), 0);
|
|
|
|
fs.add_file(combine_path("readwritev", "5"), 6561);
|
|
|
|
fs.set_piece_length(0x1000);
|
2016-04-20 06:45:32 +02:00
|
|
|
fs.set_num_pieces(int((fs.total_size() + 0xfff) / 0x1000));
|
2015-11-16 08:30:14 +01:00
|
|
|
test_read_fileop fop(10000000);
|
|
|
|
storage_error ec;
|
|
|
|
|
2016-04-20 06:45:32 +02:00
|
|
|
std::vector<char> buf(size_t(fs.total_size()));
|
2018-11-01 23:05:30 +01:00
|
|
|
iovec_t iov = { buf.data(), static_cast<std::ptrdiff_t>(fs.total_size()) };
|
2015-11-16 08:30:14 +01:00
|
|
|
|
|
|
|
// read everything
|
2017-05-02 06:07:50 +02:00
|
|
|
int ret = readwritev(fs, iov, piece_index_t(0), 0, ec, std::ref(fop));
|
2015-11-16 08:30:14 +01:00
|
|
|
|
|
|
|
TEST_EQUAL(ret, fs.total_size());
|
|
|
|
TEST_CHECK(check_pattern(buf, 0));
|
|
|
|
}
|
2015-06-21 04:34:44 +02:00
|
|
|
|
2018-01-23 20:56:03 +01:00
|
|
|
namespace {
|
|
|
|
|
2016-05-01 18:23:02 +02:00
|
|
|
void delete_dirs(std::string path)
|
2016-03-16 01:41:12 +01:00
|
|
|
{
|
2016-05-01 04:18:38 +02:00
|
|
|
error_code ec;
|
2016-05-01 18:23:02 +02:00
|
|
|
remove_all(path, ec);
|
2016-05-01 04:18:38 +02:00
|
|
|
if (ec && ec != boost::system::errc::no_such_file_or_directory)
|
2016-05-01 18:23:02 +02:00
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("remove_all \"%s\": %s\n"
|
2016-05-01 18:23:02 +02:00
|
|
|
, path.c_str(), ec.message().c_str());
|
|
|
|
}
|
|
|
|
TEST_CHECK(!exists(path));
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:56:03 +01:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2017-01-02 00:04:18 +01:00
|
|
|
TORRENT_TEST(move_storage_to_self)
|
|
|
|
{
|
|
|
|
// call move_storage with the path to the exising storage. should be a no-op
|
|
|
|
std::string const save_path = current_working_directory();
|
|
|
|
std::string const test_path = combine_path(save_path, "temp_storage");
|
|
|
|
delete_dirs(test_path);
|
|
|
|
|
|
|
|
aux::session_settings set;
|
|
|
|
file_storage fs;
|
|
|
|
std::vector<char> buf;
|
|
|
|
file_pool fp;
|
|
|
|
io_service ios;
|
2018-01-03 12:54:03 +01:00
|
|
|
disk_buffer_pool dp(ios, std::bind(&nop));
|
2017-01-08 16:18:41 +01:00
|
|
|
std::shared_ptr<default_storage> s = setup_torrent(fs, fp, buf, save_path, set);
|
2017-01-02 00:04:18 +01:00
|
|
|
|
2017-01-11 06:42:10 +01:00
|
|
|
iovec_t const b = {&buf[0], 4};
|
2017-01-02 00:04:18 +01:00
|
|
|
storage_error se;
|
2017-07-15 02:59:20 +02:00
|
|
|
s->writev(b, piece_index_t(1), 0, open_mode::read_write, se);
|
2017-01-02 00:04:18 +01:00
|
|
|
|
|
|
|
TEST_CHECK(exists(combine_path(test_path, combine_path("folder2", "test3.tmp"))));
|
|
|
|
TEST_CHECK(exists(combine_path(test_path, combine_path("_folder3", "test4.tmp"))));
|
|
|
|
|
2017-06-01 10:15:15 +02:00
|
|
|
s->move_storage(save_path, move_flags_t::always_replace_files, se);
|
2017-01-02 00:04:18 +01:00
|
|
|
TEST_EQUAL(se.ec, boost::system::errc::success);
|
|
|
|
|
|
|
|
TEST_CHECK(exists(test_path));
|
|
|
|
|
|
|
|
TEST_CHECK(exists(combine_path(test_path, combine_path("folder2", "test3.tmp"))));
|
|
|
|
TEST_CHECK(exists(combine_path(test_path, combine_path("_folder3", "test4.tmp"))));
|
|
|
|
}
|
|
|
|
|
2016-05-01 18:23:02 +02:00
|
|
|
TORRENT_TEST(move_storage_into_self)
|
|
|
|
{
|
|
|
|
std::string const save_path = current_working_directory();
|
|
|
|
delete_dirs(combine_path(save_path, "temp_storage"));
|
2016-05-01 04:18:38 +02:00
|
|
|
|
2016-03-16 01:41:12 +01:00
|
|
|
aux::session_settings set;
|
|
|
|
file_storage fs;
|
|
|
|
std::vector<char> buf;
|
|
|
|
file_pool fp;
|
|
|
|
io_service ios;
|
2018-01-03 12:54:03 +01:00
|
|
|
disk_buffer_pool dp(ios, std::bind(&nop));
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<default_storage> s = setup_torrent(fs, fp, buf, save_path, set);
|
2016-03-16 01:41:12 +01:00
|
|
|
|
2017-01-11 06:42:10 +01:00
|
|
|
iovec_t const b = {&buf[0], 4};
|
2016-03-16 01:41:12 +01:00
|
|
|
storage_error se;
|
2017-07-15 02:59:20 +02:00
|
|
|
s->writev(b, piece_index_t(2), 0, open_mode::read_write, se);
|
2016-03-16 01:41:12 +01:00
|
|
|
|
2016-05-01 18:23:02 +02:00
|
|
|
std::string const test_path = combine_path(save_path, combine_path("temp_storage", "folder1"));
|
2017-06-01 10:15:15 +02:00
|
|
|
s->move_storage(test_path, move_flags_t::always_replace_files, se);
|
2016-05-01 04:18:38 +02:00
|
|
|
TEST_EQUAL(se.ec, boost::system::errc::success);
|
2016-03-16 01:41:12 +01:00
|
|
|
|
2016-05-01 04:18:38 +02:00
|
|
|
TEST_CHECK(exists(combine_path(test_path, combine_path("temp_storage"
|
|
|
|
, combine_path("folder1", "test2.tmp")))));
|
|
|
|
|
|
|
|
// these directories and files are created up-front because they are empty files
|
|
|
|
TEST_CHECK(exists(combine_path(test_path, combine_path("temp_storage"
|
|
|
|
, combine_path("folder2", "test3.tmp")))));
|
|
|
|
TEST_CHECK(exists(combine_path(test_path, combine_path("temp_storage"
|
|
|
|
, combine_path("_folder3", "test4.tmp")))));
|
|
|
|
}
|
|
|
|
|
2016-05-12 22:45:23 +02:00
|
|
|
TORRENT_TEST(storage_paths_string_pooling)
|
|
|
|
{
|
|
|
|
file_storage file_storage;
|
|
|
|
file_storage.add_file(combine_path("test_storage", "root.txt"), 0x4000);
|
|
|
|
file_storage.add_file(combine_path("test_storage", combine_path("sub", "test1.txt")), 0x4000);
|
|
|
|
file_storage.add_file(combine_path("test_storage", combine_path("sub", "test2.txt")), 0x4000);
|
|
|
|
file_storage.add_file(combine_path("test_storage", combine_path("sub", "test3.txt")), 0x4000);
|
|
|
|
|
2016-10-22 17:47:24 +02:00
|
|
|
// "sub" paths should point to same string item, so paths.size() must not grow
|
2016-05-12 22:45:23 +02:00
|
|
|
TEST_CHECK(file_storage.paths().size() <= 2);
|
|
|
|
}
|
|
|
|
|
2016-05-01 04:18:38 +02:00
|
|
|
TORRENT_TEST(dont_move_intermingled_files)
|
|
|
|
{
|
2016-05-01 18:23:02 +02:00
|
|
|
std::string const save_path = combine_path(current_working_directory(), "save_path_1");
|
|
|
|
delete_dirs(combine_path(save_path, "temp_storage"));
|
2016-05-01 04:18:38 +02:00
|
|
|
|
|
|
|
std::string test_path = combine_path(current_working_directory(), "save_path_2");
|
2016-05-01 18:23:02 +02:00
|
|
|
delete_dirs(combine_path(test_path, "temp_storage"));
|
2016-05-01 04:18:38 +02:00
|
|
|
|
|
|
|
aux::session_settings set;
|
|
|
|
file_storage fs;
|
|
|
|
std::vector<char> buf;
|
|
|
|
file_pool fp;
|
|
|
|
io_service ios;
|
2018-01-03 12:54:03 +01:00
|
|
|
disk_buffer_pool dp(ios, std::bind(&nop));
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<default_storage> s = setup_torrent(fs, fp, buf, save_path, set);
|
2016-05-01 04:18:38 +02:00
|
|
|
|
2017-01-11 06:42:10 +01:00
|
|
|
iovec_t b = {&buf[0], 4};
|
2016-05-01 04:18:38 +02:00
|
|
|
storage_error se;
|
2017-07-15 02:59:20 +02:00
|
|
|
s->writev(b, piece_index_t(2), 0, open_mode::read_write, se);
|
2016-05-01 04:18:38 +02:00
|
|
|
|
2016-05-01 18:23:02 +02:00
|
|
|
error_code ec;
|
2016-05-01 04:18:38 +02:00
|
|
|
create_directory(combine_path(save_path, combine_path("temp_storage"
|
|
|
|
, combine_path("_folder3", "alien_folder1"))), ec);
|
|
|
|
TEST_EQUAL(ec, boost::system::errc::success);
|
|
|
|
file f;
|
|
|
|
f.open(combine_path(save_path, combine_path("temp_storage", "alien1.tmp"))
|
2017-07-15 02:59:20 +02:00
|
|
|
, open_mode::write_only, ec);
|
2016-05-01 04:18:38 +02:00
|
|
|
f.close();
|
|
|
|
TEST_EQUAL(ec, boost::system::errc::success);
|
|
|
|
f.open(combine_path(save_path, combine_path("temp_storage"
|
2017-07-15 02:59:20 +02:00
|
|
|
, combine_path("folder1", "alien2.tmp"))), open_mode::write_only, ec);
|
2016-05-01 04:18:38 +02:00
|
|
|
f.close();
|
|
|
|
TEST_EQUAL(ec, boost::system::errc::success);
|
|
|
|
|
2017-06-01 10:15:15 +02:00
|
|
|
s->move_storage(test_path, move_flags_t::always_replace_files, se);
|
2016-05-01 04:18:38 +02:00
|
|
|
TEST_EQUAL(se.ec, boost::system::errc::success);
|
|
|
|
|
|
|
|
// torrent files moved to new place
|
|
|
|
TEST_CHECK(exists(combine_path(test_path, combine_path("temp_storage"
|
|
|
|
, combine_path("folder1", "test2.tmp")))));
|
|
|
|
// these directories and files are created up-front because they are empty files
|
|
|
|
TEST_CHECK(exists(combine_path(test_path, combine_path("temp_storage"
|
|
|
|
, combine_path("folder2", "test3.tmp")))));
|
|
|
|
TEST_CHECK(exists(combine_path(test_path, combine_path("temp_storage"
|
|
|
|
, combine_path("_folder3", "test4.tmp")))));
|
|
|
|
|
|
|
|
// intermingled files and directories are still in old place
|
|
|
|
TEST_CHECK(exists(combine_path(save_path, combine_path("temp_storage"
|
|
|
|
, "alien1.tmp"))));
|
|
|
|
TEST_CHECK(!exists(combine_path(test_path, combine_path("temp_storage"
|
|
|
|
, "alien1.tmp"))));
|
|
|
|
TEST_CHECK(exists(combine_path(save_path, combine_path("temp_storage"
|
|
|
|
, combine_path("folder1", "alien2.tmp")))));
|
|
|
|
TEST_CHECK(!exists(combine_path(test_path, combine_path("temp_storage"
|
|
|
|
, combine_path("folder1", "alien2.tmp")))));
|
|
|
|
TEST_CHECK(exists(combine_path(save_path, combine_path("temp_storage"
|
|
|
|
, combine_path("_folder3", "alien_folder1")))));
|
|
|
|
TEST_CHECK(!exists(combine_path(test_path, combine_path("temp_storage"
|
|
|
|
, combine_path("_folder3", "alien_folder1")))));
|
2016-03-16 01:41:12 +01:00
|
|
|
}
|