From ed79929528d42b2163b2c51d58c84b797d560a42 Mon Sep 17 00:00:00 2001 From: arvidn Date: Fri, 23 Jun 2017 15:04:32 -0400 Subject: [PATCH] factor out has_any_file to storage_utils --- include/libtorrent/aux_/storage_utils.hpp | 8 +++++++ src/storage.cpp | 22 +++-------------- src/storage_utils.cpp | 29 +++++++++++++++++++++++ 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/include/libtorrent/aux_/storage_utils.hpp b/include/libtorrent/aux_/storage_utils.hpp index 0b5ed9fa7..ba6b71e6e 100644 --- a/include/libtorrent/aux_/storage_utils.hpp +++ b/include/libtorrent/aux_/storage_utils.hpp @@ -95,6 +95,14 @@ namespace aux { , stat_cache& stat , std::string const& save_path , storage_error& ec); + + // given the save_path, stat all files on file_storage until one exists. If a + // file exists, return true, otherwise return false. + TORRENT_EXTRA_EXPORT bool has_any_file( + file_storage const& fs + , std::string const& save_path + , stat_cache& stat + , storage_error& ec); }} #endif diff --git a/src/storage.cpp b/src/storage.cpp index d6e37ffce..0682af607 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -293,27 +293,11 @@ namespace libtorrent { { m_stat_cache.reserve(files().num_files()); - file_storage const& fs = files(); - for (file_index_t i(0); i < fs.end_file(); ++i) - { - std::int64_t const sz = m_stat_cache.get_filesize( - i, files(), m_save_path, ec.ec); + if (aux::has_any_file(files(), m_save_path, m_stat_cache, ec)) + return true; - if (sz < 0) - { - if (ec && ec.ec != boost::system::errc::no_such_file_or_directory) - { - ec.file(i); - ec.operation = operation_t::file_stat; - m_stat_cache.clear(); - return false; - } - // some files not existing is expected and not an error - ec.ec.clear(); - } + if (ec) return false; - if (sz > 0) return true; - } file_status s; stat_file(combine_path(m_save_path, m_part_file_name), &s, ec.ec); if (!ec) return true; diff --git a/src/storage_utils.cpp b/src/storage_utils.cpp index 2a66dc8d1..8b193bd89 100644 --- a/src/storage_utils.cpp +++ b/src/storage_utils.cpp @@ -566,4 +566,33 @@ namespace libtorrent { namespace aux { return true; } + bool has_any_file( + file_storage const& fs + , std::string const& save_path + , stat_cache& cache + , storage_error& ec) + { + for (file_index_t i(0); i < fs.end_file(); ++i) + { + std::int64_t const sz = cache.get_filesize( + i, fs, save_path, ec.ec); + + if (sz < 0) + { + if (ec && ec.ec != boost::system::errc::no_such_file_or_directory) + { + ec.file(i); + ec.operation = operation_t::file_stat; + cache.clear(); + return false; + } + // some files not existing is expected and not an error + ec.ec.clear(); + } + + if (sz > 0) return true; + } + return false; + } + }}