From 9172874b5c8ba31141bf5f7ef37e0d5a5d73f3f3 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Tue, 13 Jan 2015 18:28:06 +0000 Subject: [PATCH] fix storage error (recently introduced) where the mapped_files object would not be updated when renaming files that did not exist on disk --- src/storage.cpp | 67 +++++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 35 deletions(-) diff --git a/src/storage.cpp b/src/storage.cpp index 1ce9f4431..c48242bfd 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -481,48 +481,45 @@ namespace libtorrent // fail later when we try to write to the file the first time, because // the user then will have had a chance to make the destination directory // valid. - if (!exists(old_name, ec.ec)) + if (exists(old_name, ec.ec)) { - if (ec.ec == boost::system::errc::no_such_file_or_directory) +#if TORRENT_DEBUG_FILE_LEAKS + print_open_files("release files", m_files.name().c_str()); +#endif + + std::string new_path; + if (is_complete(new_filename)) new_path = new_filename; + else new_path = combine_path(m_save_path, new_filename); + std::string new_dir = parent_path(new_path); + + // create any missing directories that the new filename + // lands in + create_directories(new_dir, ec.ec); + if (ec.ec) { - ec.ec.clear(); + ec.file = index; + ec.operation = storage_error::rename; return; } - ec.file = index; - ec.operation = storage_error::rename; - return; + rename(old_name, new_path, ec.ec); + + // if old_name doesn't exist, that's not an error + // here. Once we start writing to the file, it will + // be written to the new filename + if (ec.ec == boost::system::errc::no_such_file_or_directory) + ec.ec.clear(); + + if (ec) + { + ec.file = index; + ec.operation = storage_error::rename; + return; + } } - -#if TORRENT_DEBUG_FILE_LEAKS - print_open_files("release files", m_files.name().c_str()); -#endif - - std::string new_path; - if (is_complete(new_filename)) new_path = new_filename; - else new_path = combine_path(m_save_path, new_filename); - std::string new_dir = parent_path(new_path); - - // create any missing directories that the new filename - // lands in - create_directories(new_dir, ec.ec); - if (ec.ec) - { - ec.file = index; - ec.operation = storage_error::rename; - return; - } - - rename(old_name, new_path, ec.ec); - - // if old_name doesn't exist, that's not an error - // here. Once we start writing to the file, it will - // be written to the new filename - if (ec.ec == boost::system::errc::no_such_file_or_directory) - ec.ec.clear(); - - if (ec) + else if (ec.ec) { + // if exists fails, report that error ec.file = index; ec.operation = storage_error::rename; return;