From cc3e6621ed868d20d3b7231fce6f925133263eb8 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 22 Jun 2009 00:52:57 +0000 Subject: [PATCH] fixed rename_file() bug where the new name would not be saved in the resume data in some cases --- ChangeLog | 2 ++ docs/manual.rst | 2 +- include/libtorrent/create_torrent.hpp | 4 ++-- include/libtorrent/storage.hpp | 4 ++-- src/storage.cpp | 23 +++++++++++++++++++---- src/torrent.cpp | 2 +- test/test_storage.cpp | 4 ++-- test/test_transfer.cpp | 4 ++-- 8 files changed, 31 insertions(+), 14 deletions(-) diff --git a/ChangeLog b/ChangeLog index b84a71245..fd49c2eb5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -67,6 +67,8 @@ release 0.14.5 * fixed memory leak in disk io thread when not using the cache * fixed bug in connect candidate counter * allow 0 upload slots + * fixed bug in rename_file(). The new name would not always be saved in + the resume data release 0.14.4 diff --git a/docs/manual.rst b/docs/manual.rst index 86f216f45..6b49f7f9d 100644 --- a/docs/manual.rst +++ b/docs/manual.rst @@ -314,7 +314,7 @@ add_torrent() :: typedef storage_interface* (&storage_constructor_type)( - file_storage const&, fs::path const&, file_pool&); + file_storage const&, file_storage const*, fs::path const&, file_pool&); struct add_torrent_params { diff --git a/include/libtorrent/create_torrent.hpp b/include/libtorrent/create_torrent.hpp index 2e1e08abf..7b247da3c 100644 --- a/include/libtorrent/create_torrent.hpp +++ b/include/libtorrent/create_torrent.hpp @@ -230,7 +230,7 @@ namespace libtorrent { file_pool fp; boost::scoped_ptr st( - default_storage_constructor(const_cast(t.files()), p, fp)); + default_storage_constructor(const_cast(t.files()), 0, p, fp)); // calculate the hash for all pieces int num = t.num_pieces(); @@ -304,7 +304,7 @@ namespace libtorrent std::string utf8; wchar_utf8(p.string(), utf8); boost::scoped_ptr st( - default_storage_constructor(const_cast(t.files()), utf8, fp)); + default_storage_constructor(const_cast(t.files()), 0, utf8, fp)); // calculate the hash for all pieces int num = t.num_pieces(); diff --git a/include/libtorrent/storage.hpp b/include/libtorrent/storage.hpp index 4ac899152..e71c9e16b 100644 --- a/include/libtorrent/storage.hpp +++ b/include/libtorrent/storage.hpp @@ -190,10 +190,10 @@ namespace libtorrent }; typedef storage_interface* (*storage_constructor_type)( - file_storage const&, fs::path const&, file_pool&); + file_storage const&, file_storage const*, fs::path const&, file_pool&); TORRENT_EXPORT storage_interface* default_storage_constructor( - file_storage const&, fs::path const&, file_pool&); + file_storage const&, file_storage const* orig, fs::path const&, file_pool&); struct disk_io_thread; diff --git a/src/storage.cpp b/src/storage.cpp index 1a373da0c..0ef476aeb 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -398,12 +398,14 @@ namespace libtorrent class storage : public storage_interface, boost::noncopyable { public: - storage(file_storage const& fs, fs::path const& path, file_pool& fp) + storage(file_storage const& fs, file_storage const* orig, fs::path const& path, file_pool& fp) : m_files(fs) , m_pool(fp) , m_page_size(4096) , m_allocate_files(false) { + if (orig) m_mapped_files.reset(new file_storage(*orig)); + TORRENT_ASSERT(m_files.begin() != m_files.end()); m_save_path = fs::complete(path); TORRENT_ASSERT(m_save_path.is_complete()); @@ -890,6 +892,18 @@ namespace libtorrent bool storage::verify_resume_data(lazy_entry const& rd, std::string& error) { + lazy_entry const* mapped_files = rd.dict_find_list("mapped_files"); + if (mapped_files && mapped_files->list_size() == m_files.num_files()) + { + m_mapped_files.reset(new file_storage(m_files)); + for (int i = 0; i < m_files.num_files(); ++i) + { + std::string new_filename = mapped_files->list_string_value_at(i); + if (new_filename.empty()) continue; + m_mapped_files->rename_file(i, new_filename); + } + } + lazy_entry const* file_priority = rd.dict_find_list("file_priority"); if (file_priority && file_priority->list_size() == files().num_files()) @@ -1471,9 +1485,9 @@ ret: } storage_interface* default_storage_constructor(file_storage const& fs - , fs::path const& path, file_pool& fp) + , file_storage const* orig, fs::path const& path, file_pool& fp) { - return new storage(fs, path, fp); + return new storage(fs, orig, path, fp); } // -- piece_manager ----------------------------------------------------- @@ -1488,7 +1502,8 @@ ret: , storage_mode_t sm) : m_info(info) , m_files(m_info->files()) - , m_storage(sc(m_files, save_path, fp)) + , m_storage(sc(m_info->files(), &m_info->files() != &m_info->orig_files() + ? &m_info->orig_files() : 0, save_path, fp)) , m_storage_mode(sm) , m_save_path(complete(save_path)) , m_state(state_none) diff --git a/src/torrent.cpp b/src/torrent.cpp index e49a16efd..958ef4b73 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -3257,7 +3257,7 @@ namespace libtorrent { std::string new_filename = mapped_files->list_string_value_at(i); if (new_filename.empty()) continue; - rename_file(i, new_filename); + m_torrent_file->rename_file(i, new_filename); } } diff --git a/test/test_storage.cpp b/test/test_storage.cpp index 2baed30cc..a2a705861 100644 --- a/test/test_storage.cpp +++ b/test/test_storage.cpp @@ -146,7 +146,7 @@ void run_storage_tests(boost::intrusive_ptr info file_pool fp; disk_buffer_pool dp(16 * 1024); boost::scoped_ptr s( - default_storage_constructor(fs, test_path, fp)); + default_storage_constructor(fs, 0, test_path, fp)); s->m_settings = &set; s->m_disk_pool = &dp; @@ -324,7 +324,7 @@ void test_remove(path const& test_path, bool unbuffered) file_pool fp; disk_buffer_pool dp(16 * 1024); boost::scoped_ptr s( - default_storage_constructor(fs, test_path, fp)); + default_storage_constructor(fs, 0, test_path, fp)); s->m_settings = &set; s->m_disk_pool = &dp; diff --git a/test/test_transfer.cpp b/test/test_transfer.cpp index 47271c6a6..d48539d11 100644 --- a/test/test_transfer.cpp +++ b/test/test_transfer.cpp @@ -115,7 +115,7 @@ void print_alert(alert const& a) struct test_storage : storage_interface { test_storage(file_storage const& fs, fs::path const& p, file_pool& fp) - : m_lower_layer(default_storage_constructor(fs, p, fp)) + : m_lower_layer(default_storage_constructor(fs, 0, p, fp)) , m_written(0) , m_limit(16 * 1024 * 2) {} @@ -204,7 +204,7 @@ struct test_storage : storage_interface }; storage_interface* test_storage_constructor(file_storage const& fs - , fs::path const& path, file_pool& fp) + , file_storage const*, fs::path const& path, file_pool& fp) { return new test_storage(fs, path, fp); }