From e98dd14760142bf93315a5d65e16ce2171c2fb1f Mon Sep 17 00:00:00 2001 From: arvidn Date: Tue, 17 Jan 2017 18:06:43 -0500 Subject: [PATCH] factor out delete_files function into storage_utils --- include/libtorrent/aux_/storage_utils.hpp | 7 +++ include/libtorrent/part_file.hpp | 5 ++ src/storage.cpp | 73 +---------------------- src/storage_utils.cpp | 71 ++++++++++++++++++++++ 4 files changed, 84 insertions(+), 72 deletions(-) diff --git a/include/libtorrent/aux_/storage_utils.hpp b/include/libtorrent/aux_/storage_utils.hpp index ab20e7868..03f0f2623 100644 --- a/include/libtorrent/aux_/storage_utils.hpp +++ b/include/libtorrent/aux_/storage_utils.hpp @@ -90,6 +90,13 @@ namespace libtorrent , std::string const& destination_save_path , part_file* pf , int const flags, storage_error& ec); + + // deletes the files on fs from save_path according to options. Options may + // opt to only delete the partfile + TORRENT_EXTRA_EXPORT void + delete_files(file_storage const& fs, std::string const& save_path + , std::string const& part_file_name, int const options, storage_error& ec); + } #endif diff --git a/include/libtorrent/part_file.hpp b/include/libtorrent/part_file.hpp index b17594677..bca8e56b8 100644 --- a/include/libtorrent/part_file.hpp +++ b/include/libtorrent/part_file.hpp @@ -30,6 +30,9 @@ POSSIBILITY OF SUCH DAMAGE. */ +#ifndef TORRENT_PART_FILE_HPP_INCLUDE +#define TORRENT_PART_FILE_HPP_INCLUDE + #include #include #include @@ -115,3 +118,5 @@ namespace libtorrent file m_file; }; } + +#endif diff --git a/src/storage.cpp b/src/storage.cpp index e5bfa517a..891af85f8 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -65,7 +65,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/storage.hpp" #include "libtorrent/torrent.hpp" -#include "libtorrent/session.hpp" #include "libtorrent/file.hpp" #include "libtorrent/invariant_check.hpp" #include "libtorrent/file_pool.hpp" @@ -597,17 +596,6 @@ namespace libtorrent m_pool.release(storage_index()); } - void default_storage::delete_one_file(std::string const& p, error_code& ec) - { - remove(p, ec); - - DFLOG(stderr, "[%p] delete_one_file: %s [%s]\n", static_cast(this) - , p.c_str(), ec.message().c_str()); - - if (ec == boost::system::errc::no_such_file_or_directory) - ec.clear(); - } - void default_storage::delete_files(int const options, storage_error& ec) { DFLOG(stderr, "[%p] delete_files [%x]\n", static_cast(this) @@ -632,66 +620,7 @@ namespace libtorrent // delete it if (m_part_file) m_part_file.reset(); - if (options == session::delete_files) - { -#if TORRENT_USE_ASSERTS - m_pool.mark_deleted(m_files); -#endif - // delete the files from disk - std::set directories; - using iter_t = std::set::iterator; - file_storage const& fs = files(); - for (file_index_t i(0); i < fs.end_file(); ++i) - { - std::string const fp = files().file_path(i); - bool const complete = files().file_absolute_path(i); - std::string const p = complete ? fp : combine_path(m_save_path, fp); - if (!complete) - { - std::string bp = parent_path(fp); - std::pair ret; - ret.second = true; - while (ret.second && !bp.empty()) - { - ret = directories.insert(combine_path(m_save_path, bp)); - bp = parent_path(bp); - } - } - delete_one_file(p, ec.ec); - if (ec) { ec.file(i); ec.operation = storage_error::remove; } - } - - // remove the directories. Reverse order to delete - // subdirectories first - - for (auto i = directories.rbegin() - , end(directories.rend()); i != end; ++i) - { - error_code error; - delete_one_file(*i, error); - if (error && !ec) - { - ec.file(file_index_t(-1)); - ec.ec = error; - ec.operation = storage_error::remove; - } - } - } - - if (options == session::delete_files - || options == session::delete_partfile) - { - error_code error; - remove(combine_path(m_save_path, m_part_file_name), error); - DFLOG(stderr, "[%p] delete partfile %s/%s [%s]\n", static_cast(this) - , m_save_path.c_str(), m_part_file_name.c_str(), error.message().c_str()); - if (error && error != boost::system::errc::no_such_file_or_directory) - { - ec.file(file_index_t(-1)); - ec.ec = error; - ec.operation = storage_error::remove; - } - } + libtorrent::delete_files(files(), m_save_path, m_part_file_name, options, ec); DFLOG(stderr, "[%p] delete_files result: %s\n", static_cast(this) , ec.ec.message().c_str()); diff --git a/src/storage_utils.cpp b/src/storage_utils.cpp index 0db28a756..b1a6582fc 100644 --- a/src/storage_utils.cpp +++ b/src/storage_utils.cpp @@ -35,6 +35,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/alloca.hpp" #include "libtorrent/file.hpp" // for count_bufs #include "libtorrent/part_file.hpp" +#include "libtorrent/session.hpp" // for session::delete_files #include #include @@ -374,5 +375,75 @@ namespace libtorrent return { ret, new_save_path }; } + namespace { + + void delete_one_file(std::string const& p, error_code& ec) + { + remove(p, ec); + + if (ec == boost::system::errc::no_such_file_or_directory) + ec.clear(); + } + + } + + void delete_files(file_storage const& fs, std::string const& save_path + , std::string const& part_file_name, int const options, storage_error& ec) + { + if (options == session::delete_files) + { + // delete the files from disk + std::set directories; + using iter_t = std::set::iterator; + for (file_index_t i(0); i < fs.end_file(); ++i) + { + std::string const fp = fs.file_path(i); + bool const complete = fs.file_absolute_path(i); + std::string const p = complete ? fp : combine_path(save_path, fp); + if (!complete) + { + std::string bp = parent_path(fp); + std::pair ret; + ret.second = true; + while (ret.second && !bp.empty()) + { + ret = directories.insert(combine_path(save_path, bp)); + bp = parent_path(bp); + } + } + delete_one_file(p, ec.ec); + if (ec) { ec.file(i); ec.operation = storage_error::remove; } + } + + // remove the directories. Reverse order to delete + // subdirectories first + + for (auto i = directories.rbegin() + , end(directories.rend()); i != end; ++i) + { + error_code error; + delete_one_file(*i, error); + if (error && !ec) + { + ec.file(file_index_t(-1)); + ec.ec = error; + ec.operation = storage_error::remove; + } + } + } + + if (options == session::delete_files + || options == session::delete_partfile) + { + error_code error; + remove(combine_path(save_path, part_file_name), error); + if (error && error != boost::system::errc::no_such_file_or_directory) + { + ec.file(file_index_t(-1)); + ec.ec = error; + ec.operation = storage_error::remove; + } + } + } }