From 59c8a74ba80ecab10f8594fea3a3ea09ec7e7429 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Thu, 5 Jun 2008 16:56:39 +0000 Subject: [PATCH] in case a regular rename fails, try to copy the storage to the new location. Fixes #231 --- src/storage.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/src/storage.cpp b/src/storage.cpp index ae3b0a9cf..3a8090144 100755 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -252,7 +252,53 @@ namespace namespace libtorrent { + template + void recursive_copy(Path const& old_path, Path const& new_path, std::string& error) + { + using boost::filesystem::directory_iterator; +#ifndef BOOST_NO_EXCEPTIONS + try { +#endif + TORRENT_ASSERT(error.empty()); + if (is_directory(old_path)) + { + create_directory(new_path); + for (directory_iterator i(old_path), end; i != end; ++i) + { + recursive_copy(i->path(), new_path / i->leaf(), error); + if (!error.empty()) return; + } + } + else + { + copy_file(old_path, new_path); + } +#ifndef BOOST_NO_EXCEPTIONS + } catch (std::exception& e) { error = e.what(); } +#endif + } + template + void recursive_remove(Path const& old_path) + { + using boost::filesystem::directory_iterator; +#ifndef BOOST_NO_EXCEPTIONS + try { +#endif + if (is_directory(old_path)) + { + for (directory_iterator i(old_path), end; i != end; ++i) + recursive_remove(i->path()); + remove(old_path); + } + else + { + remove(old_path); + } +#ifndef BOOST_NO_EXCEPTIONS + } catch (std::exception& e) {} +#endif + } std::vector > get_filesizes( file_storage const& s, fs::path p) { @@ -782,8 +828,15 @@ namespace libtorrent } catch (std::exception& e) { - set_error((m_save_path / files().name()).string(), e.what()); - return true; + std::string err; + recursive_copy(old_path, new_path, err); + if (!err.empty()) + { + set_error((m_save_path / files().name()).string(), e.what()); + return true; + } + m_save_path = save_path; + recursive_remove(old_path); } #endif return false;