separated file_storage from torrent_info and create_torrent

This commit is contained in:
Arvid Norberg
2008-05-28 08:44:40 +00:00
parent cf37d8544d
commit 3910fe78de
31 changed files with 1859 additions and 1514 deletions

View File

@@ -170,10 +170,10 @@ boost::intrusive_ptr<torrent_info> create_torrent(std::ostream* file)
using namespace boost::filesystem;
libtorrent::create_torrent t;
file_storage fs;
int total_size = 2 * 1024 * 1024;
t.add_file(path("temporary"), total_size);
t.set_piece_size(16 * 1024);
fs.add_file(path("temporary"), total_size);
libtorrent::create_torrent t(fs, 16 * 1024);
t.add_tracker(tracker_url);
std::vector<char> piece(16 * 1024);

View File

@@ -51,9 +51,11 @@ void on_check_files(int ret, disk_io_job const& j)
}
void run_storage_tests(boost::intrusive_ptr<torrent_info> info
, file_storage& fs
, path const& test_path
, libtorrent::storage_mode_t storage_mode)
{
TORRENT_ASSERT(fs.num_files() > 0);
create_directory(test_path / "temp_storage");
int num_pieces = (1 + 612 + 17 + piece_size - 1) / piece_size;
@@ -64,7 +66,7 @@ void run_storage_tests(boost::intrusive_ptr<torrent_info> info
{ // avoid having two storages use the same files
file_pool fp;
boost::scoped_ptr<storage_interface> s(
default_storage_constructor(info, test_path, fp));
default_storage_constructor(fs, test_path, fp));
// write piece 1 (in slot 0)
s->write(piece1, 0, 0, half);
@@ -100,12 +102,17 @@ void run_storage_tests(boost::intrusive_ptr<torrent_info> info
entry frd;
pm->async_check_fastresume(&frd, &on_check_resume_data);
ios.reset();
ios.run();
pm->async_check_files(&on_check_files);
for (int i = 0; i < 4; ++i)
{
ios.reset();
ios.run_one();
}
// test move_storage
boost::function<void(int, disk_io_job const&)> none;
TEST_CHECK(exists(test_path / "temp_storage"));
pm->async_move_storage(test_path / "temp_storage2", none);
@@ -117,6 +124,15 @@ void run_storage_tests(boost::intrusive_ptr<torrent_info> info
TEST_CHECK(!exists(test_path / "temp_storage2/temp_storage"));
remove_all(test_path / "temp_storage2");
// test rename_file
remove(test_path / "part0");
TEST_CHECK(exists(test_path / "temp_storage/test1.tmp"));
TEST_CHECK(!exists(test_path / "part0"));
pm->async_rename_file(0, "part0", none);
test_sleep(2000);
TEST_CHECK(!exists(test_path / "temp_storage/test1.tmp"));
TEST_CHECK(exists(test_path / "part0"));
peer_request r;
r.piece = 0;
r.start = 0;
@@ -128,6 +144,10 @@ void run_storage_tests(boost::intrusive_ptr<torrent_info> info
pm->async_read(r, bind(&on_read_piece, _1, _2, piece2, piece_size));
pm->async_release_files(none);
pm->async_rename_file(0, "temp_storage/test1.tmp", none);
test_sleep(1000);
TEST_CHECK(!exists(test_path / "part0"));
ios.run();
io.join();
@@ -136,13 +156,13 @@ void run_storage_tests(boost::intrusive_ptr<torrent_info> info
void test_remove(path const& test_path)
{
libtorrent::create_torrent t;
t.set_piece_size(4);
t.add_file("temp_storage/test1.tmp", 8);
t.add_file("temp_storage/folder1/test2.tmp", 8);
t.add_file("temp_storage/folder2/test3.tmp", 0);
t.add_file("temp_storage/_folder3/test4.tmp", 0);
t.add_file("temp_storage/_folder3/subfolder/test5.tmp", 8);
file_storage fs;
fs.add_file("temp_storage/test1.tmp", 8);
fs.add_file("temp_storage/folder1/test2.tmp", 8);
fs.add_file("temp_storage/folder2/test3.tmp", 0);
fs.add_file("temp_storage/_folder3/test4.tmp", 0);
fs.add_file("temp_storage/_folder3/subfolder/test5.tmp", 8);
libtorrent::create_torrent t(fs, 4);
char buf[4] = {0, 0, 0, 0};
sha1_hash h = hasher(buf, 4).final();
@@ -152,7 +172,7 @@ void test_remove(path const& test_path)
file_pool fp;
boost::scoped_ptr<storage_interface> s(
default_storage_constructor(info, test_path, fp));
default_storage_constructor(fs, test_path, fp));
// allocate the files and create the directories
s->initialize(true);
@@ -172,14 +192,15 @@ void run_test(path const& test_path)
boost::intrusive_ptr<torrent_info> info;
{
libtorrent::create_torrent t;
t.set_piece_size(piece_size);
t.add_file("temp_storage/test1.tmp", 17);
t.add_file("temp_storage/test2.tmp", 612);
t.add_file("temp_storage/test3.tmp", 0);
t.add_file("temp_storage/test4.tmp", 0);
t.add_file("temp_storage/test5.tmp", 1);
remove_all(test_path / "temp_storage");
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);
fs.add_file("temp_storage/test5.tmp", 1);
libtorrent::create_torrent t(fs, piece_size);
t.set_hash(0, hasher(piece0, piece_size).final());
t.set_hash(1, hasher(piece1, piece_size).final());
t.set_hash(2, hasher(piece2, piece_size).final());
@@ -188,7 +209,7 @@ void run_test(path const& test_path)
info = new torrent_info(t.generate());
std::cerr << "=== test 1 ===" << std::endl;
run_storage_tests(info, test_path, storage_mode_compact);
run_storage_tests(info, fs, test_path, storage_mode_compact);
// make sure the files have the correct size
std::cerr << file_size(test_path / "temp_storage" / "test1.tmp") << std::endl;
@@ -200,63 +221,39 @@ void run_test(path const& test_path)
remove_all(test_path / "temp_storage");
}
// ==============================================
// make sure remap_files works
std::vector<file_entry> map;
file_entry fe;
fe.path = "temp_storage/test.tmp";
fe.size = 17;
fe.file_base = 612 + 1;
map.push_back(fe);
fe.path = "temp_storage/test.tmp";
fe.size = 612 + 1;
fe.file_base = 0;
map.push_back(fe);
bool ret = info->remap_files(map);
TEST_CHECK(ret);
std::cerr << "=== test 2 ===" << std::endl;
run_storage_tests(info, test_path, storage_mode_compact);
std::cerr << file_size(test_path / "temp_storage" / "test.tmp") << std::endl;
TEST_CHECK(file_size(test_path / "temp_storage" / "test.tmp") == 17 + 612 + 1);
remove_all(test_path / "temp_storage");
// ==============================================
{
libtorrent::create_torrent t;
t.set_piece_size(piece_size);
t.add_file("temp_storage/test1.tmp", 17 + 612 + 1);
file_storage fs;
fs.add_file("temp_storage/test1.tmp", 17 + 612 + 1);
libtorrent::create_torrent t(fs, piece_size);
TEST_CHECK(fs.begin()->path == "temp_storage/test1.tmp");
t.set_hash(0, hasher(piece0, piece_size).final());
t.set_hash(1, hasher(piece1, piece_size).final());
t.set_hash(2, hasher(piece2, piece_size).final());
info = new torrent_info(t.generate());
std::cerr << "=== test 3 ===" << std::endl;
run_storage_tests(info, test_path, storage_mode_compact);
run_storage_tests(info, fs, test_path, storage_mode_compact);
// 48 = piece_size * 3
TEST_CHECK(file_size(test_path / "temp_storage" / "test1.tmp") == 48);
remove_all(test_path / "temp_storage");
}
// ==============================================
std::cerr << "=== test 4 ===" << std::endl;
run_storage_tests(info, test_path, storage_mode_allocate);
run_storage_tests(info, fs, test_path, storage_mode_allocate);
std::cerr << file_size(test_path / "temp_storage" / "test1.tmp") << std::endl;
TEST_CHECK(file_size(test_path / "temp_storage" / "test1.tmp") == 17 + 612 + 1);
remove_all(test_path / "temp_storage");
}
// ==============================================

View File

@@ -14,12 +14,12 @@ int test_main()
session ses(fingerprint("LT", 0, 1, 0, 0), std::make_pair(48130, 48140));
libtorrent::create_torrent t;
file_storage fs;
size_type file_size = 1 * 1024 * 1024 * 1024;
t.add_file("test_torrent/tmp1", file_size);
t.add_file("test_torrent/tmp2", file_size);
t.add_file("test_torrent/tmp3", file_size);
t.set_piece_size(4 * 1024 * 1024);
fs.add_file("test_torrent/tmp1", file_size);
fs.add_file("test_torrent/tmp2", file_size);
fs.add_file("test_torrent/tmp3", file_size);
libtorrent::create_torrent t(fs, 4 * 1024 * 1024);
t.add_tracker("http://non-existing.com/announce");
std::vector<char> piece(4 * 1024 * 1024);

View File

@@ -15,22 +15,6 @@
using namespace boost::filesystem;
using namespace libtorrent;
void add_files(libtorrent::create_torrent& t, path const& p, path const& l)
{
if (l.leaf()[0] == '.') return;
path f(p / l);
if (is_directory(f))
{
for (directory_iterator i(f), end; i != end; ++i)
add_files(t, p, l / i->leaf());
}
else
{
std::cerr << "adding \"" << l.string() << "\"\n";
t.add_file(l, file_size(f));
}
}
// proxy: 0=none, 1=socks4, 2=socks5, 3=socks5_pw 4=http 5=http_pw
void test_transfer(boost::intrusive_ptr<torrent_info> torrent_file, int proxy)
{
@@ -87,9 +71,6 @@ int test_main()
using namespace libtorrent;
using namespace boost::filesystem;
libtorrent::create_torrent t;
t.add_url_seed("http://127.0.0.1:8000/");
create_directory("test_torrent");
char random_data[300000];
std::srand(std::time(0));
@@ -102,7 +83,11 @@ int test_main()
std::ofstream("./test_torrent/test6").write(random_data, 300000);
std::ofstream("./test_torrent/test7").write(random_data, 300000);
add_files(t, complete("."), "test_torrent");
file_storage fs;
add_files(fs, path("test_torrent"));
libtorrent::create_torrent t(fs, 16 * 1024);
t.add_url_seed("http://127.0.0.1:8000/");
start_web_server(8000);
@@ -111,20 +96,17 @@ int test_main()
std::vector<char> buf(t.piece_length());
file_pool fp;
boost::intrusive_ptr<torrent_info> torrent_file(new torrent_info(t.generate()));
boost::scoped_ptr<storage_interface> s(default_storage_constructor(
torrent_file, ".", fp));
fs, ".", fp));
for (int i = 0; i < num; ++i)
{
s->read(&buf[0], i, 0, t.piece_size(i));
hasher h(&buf[0], t.piece_size(i));
s->read(&buf[0], i, 0, fs.piece_size(i));
hasher h(&buf[0], fs.piece_size(i));
t.set_hash(i, h.final());
}
entry e = t.generate();
torrent_file = new torrent_info(e);
s.reset(default_storage_constructor(torrent_file, ".", fp));
boost::intrusive_ptr<torrent_info> torrent_file(new torrent_info(t.generate()));
for (int i = 0; i < 6; ++i)
test_transfer(torrent_file, i);